UNPKG

taglib-wasm

Version:

TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps

76 lines (75 loc) 2.57 kB
import { TagLib } from "./taglib.js"; import { FileOperationError } from "./errors/classes.js"; import { isDenoCompiled } from "./runtime/deno-detect.js"; import { fileUrlToPath } from "./utils/path.js"; import { isDenoCompiled as isDenoCompiled2 } from "./runtime/deno-detect.js"; const WASM_EMBED_SEARCH_PATHS = [ "../build/taglib-web.wasm", "../dist/taglib-web.wasm", "./node_modules/taglib-wasm/dist/taglib-web.wasm" ]; async function initializeForDenoCompile(embeddedWasmPath = "./taglib-web.wasm") { if (isDenoCompiled()) { const strategies = [ // Relative to user's entry point (where --include embeds files) () => Deno.readFile(new URL(embeddedWasmPath, Deno.mainModule)), // Relative to this library module () => Deno.readFile(new URL(embeddedWasmPath, import.meta.url)), // CWD fallback (last resort — depends on where the binary is invoked) () => Deno.readFile(embeddedWasmPath) ]; for (const strategy of strategies) { try { const wasmBinary = await strategy(); return await TagLib.initialize({ wasmBinary, forceWasmType: "emscripten" }); } catch { } } console.warn(`Could not load embedded WASM from ${embeddedWasmPath}`); console.warn("Falling back to network fetch (requires --allow-net)"); } return await TagLib.initialize({ forceWasmType: "emscripten" }); } async function prepareWasmForEmbedding(outputPath = "./taglib-web.wasm") { try { const possiblePaths = WASM_EMBED_SEARCH_PATHS.map( (p) => new URL(p, import.meta.url) ); let wasmData = null; let sourcePath = null; for (const path of possiblePaths) { try { wasmData = await Deno.readFile(path); sourcePath = fileUrlToPath(path); break; } catch { } } if (!wasmData || !sourcePath) { throw new FileOperationError( "read", "Could not find taglib-web.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 FileOperationError( "read", `Failed to prepare WASM for embedding: ${message}` ); } } export { WASM_EMBED_SEARCH_PATHS, initializeForDenoCompile, isDenoCompiled2 as isDenoCompiled, prepareWasmForEmbedding };