taglib-wasm
Version:
TagLib for TypeScript platforms: Deno, Node.js, Bun, Electron, browsers, and Cloudflare Workers
53 lines (52 loc) • 1.58 kB
JavaScript
function isDenoCompiled() {
return typeof Deno !== "undefined" && Deno.mainModule?.startsWith("file:///") && Deno.execPath()?.includes("deno") === false;
}
async function loadWasmForDeno(options = {}) {
const strategies = [
// 1. User-provided binary
async () => options.wasmBinary,
// 2. Local file (development)
async () => {
if (typeof Deno !== "undefined" && !isDenoCompiled()) {
try {
const path = options.wasmPath ?? "./build/taglib-web.wasm";
return await Deno.readFile(path);
} catch {
}
}
},
// 3. Fetch from URL
async () => {
const url = options.wasmUrl ?? options.fallbackUrl ?? "https://cdn.jsdelivr.net/npm/taglib-wasm@latest/dist/taglib-web.wasm";
try {
const response = await fetch(url);
if (response.ok) {
return new Uint8Array(await response.arrayBuffer());
}
} catch {
}
}
];
for (const strategy of strategies) {
const result = await strategy();
if (result) {
return result instanceof Uint8Array ? result : new Uint8Array(result);
}
}
return void 0;
}
async function initializeForDeno(options = {}) {
const { TagLib } = await import("../src/taglib.js");
const wasmBinary = await loadWasmForDeno(options);
if (!wasmBinary) {
throw new Error(
"Failed to load WASM module. For compiled binaries, either bundle the WASM or ensure network access."
);
}
return TagLib.initialize({ wasmBinary });
}
export {
initializeForDeno,
isDenoCompiled,
loadWasmForDeno
};