silverbullet/plug-api/syscalls/asset.ts

17 lines
461 B
TypeScript
Raw Normal View History

import { base64DecodeDataUrl } from "$lib/crypto.ts";
import { syscall } from "../syscall.ts";
2022-10-14 21:11:33 +08:00
export async function readAsset(
plugName: string,
2022-10-14 21:11:33 +08:00
name: string,
encoding: "utf8" | "dataurl" = "utf8",
): Promise<string> {
const dataUrl = await syscall("asset.readAsset", plugName, name) as string;
2022-10-14 21:11:33 +08:00
switch (encoding) {
case "utf8":
2022-10-19 15:52:29 +08:00
return new TextDecoder().decode(base64DecodeDataUrl(dataUrl));
2022-10-14 21:11:33 +08:00
case "dataurl":
2022-10-14 21:53:51 +08:00
return dataUrl;
2022-10-14 21:11:33 +08:00
}
}