UNPKG

taglib-wasm

Version:

TagLib for TypeScript platforms: Deno, Node.js, Bun, Electron, browsers, and Cloudflare Workers

58 lines (57 loc) 2 kB
import { TagLib } from "./taglib.js"; function isDenoCompiled() { return typeof Deno !== "undefined" && // @ts-ignore: Deno global is only available in Deno runtime typeof Deno.mainModule === "string" && // @ts-ignore: Deno global is only available in Deno runtime Deno.mainModule.includes("deno-compile://"); } async function initializeForDenoCompile(embeddedWasmPath = "./taglib.wasm") { if (isDenoCompiled()) { try { const wasmUrl = new URL(embeddedWasmPath, import.meta.url); const wasmBinary = await Deno.readFile(wasmUrl); return await TagLib.initialize({ wasmBinary }); } catch (error) { console.warn( `Could not load embedded WASM from ${embeddedWasmPath}:`, error ); console.warn("Falling back to network fetch (requires --allow-net)"); } } return await TagLib.initialize(); } async function prepareWasmForEmbedding(outputPath = "./taglib.wasm") { try { const possiblePaths = [ new URL("../dist/taglib.wasm", import.meta.url), new URL("../build/taglib.wasm", import.meta.url), new URL("./node_modules/taglib-wasm/dist/taglib.wasm", import.meta.url) ]; let wasmData = null; let sourcePath = null; for (const path of possiblePaths) { try { wasmData = await Deno.readFile(path); sourcePath = path.pathname; break; } catch { } } if (!wasmData || !sourcePath) { throw new Error("Could not find taglib.wasm in expected locations"); } await Deno.writeFile(outputPath, wasmData); console.log(`WASM file copied from ${sourcePath} to ${outputPath}`); console.log( `Include this file when compiling: deno compile --include ${outputPath} ...` ); } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new Error(`Failed to prepare WASM for embedding: ${message}`); } } export { initializeForDenoCompile, isDenoCompiled, prepareWasmForEmbedding };