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

176 lines (175 loc) 4.29 kB
const g = globalThis; const MIN_NODE_MAJOR = 22; const MIN_NODE_MINOR = 6; function checkNodeVersion(nodeVersion) { if (!nodeVersion) return void 0; const parts = nodeVersion.split(".").map(Number); const [major, minor] = parts; if (major < MIN_NODE_MAJOR || major === MIN_NODE_MAJOR && minor < MIN_NODE_MINOR) { return `Node.js v${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}.0 or higher is required. Current version: v${nodeVersion}. Older versions lack WASI and Wasm exception handling support.`; } return void 0; } function hasWASISupport() { if (g.Deno !== void 0) return true; if (g.process !== void 0 && g.process.versions?.node) { const [major] = g.process.versions.node.split(".").map(Number); return major >= 16; } return false; } function isBrowser() { return g.window !== void 0 && g.document !== void 0; } function isWebWorker() { return g.WorkerGlobalScope !== void 0 && g.self !== void 0 && g.self instanceof g.WorkerGlobalScope; } function isCloudflareWorker() { return g.caches !== void 0 && g.Request !== void 0 && typeof g.addEventListener === "function" && g.Deno === void 0 && g.process === void 0; } function isBun() { return g.Bun !== void 0; } function isNode() { return g.process !== void 0 && g.process.versions?.node !== void 0; } function isDeno() { return g.Deno !== void 0; } function detectRuntime() { if (isDeno() && hasWASISupport()) { return { environment: "deno-wasi", wasmType: "wasi", supportsFilesystem: true, supportsStreaming: true, performanceTier: 1 }; } if (isBun()) { return { environment: "bun-wasi", wasmType: "wasi", supportsFilesystem: true, supportsStreaming: true, performanceTier: 1 }; } if (isNode() && hasWASISupport()) { return { environment: "node-wasi", wasmType: "wasi", supportsFilesystem: true, supportsStreaming: true, performanceTier: 1 }; } if (isBrowser()) { return { environment: "browser", wasmType: "emscripten", supportsFilesystem: false, supportsStreaming: true, performanceTier: 2 }; } if (isWebWorker()) { return { environment: "worker", wasmType: "emscripten", supportsFilesystem: false, supportsStreaming: true, performanceTier: 2 }; } if (isCloudflareWorker()) { return { environment: "cloudflare", wasmType: "emscripten", supportsFilesystem: false, supportsStreaming: false, performanceTier: 3 }; } if (isNode()) { return { environment: "node-emscripten", wasmType: "emscripten", supportsFilesystem: true, supportsStreaming: true, performanceTier: 3 }; } return { environment: "browser", wasmType: "emscripten", supportsFilesystem: false, supportsStreaming: true, performanceTier: 3 }; } const EXNREF_PROBE = new Uint8Array([ 0, 97, 115, 109, 1, 0, 0, 0, // Wasm header 6, 6, // Global section, 6 bytes 1, // 1 global 105, 0, // exnref, const 208, 105, 11 // ref.null exnref, end ]); function supportsExnref() { try { return WebAssembly.validate(EXNREF_PROBE); } catch { return false; } } function getEnvironmentDescription(env) { switch (env) { case "deno-wasi": return "Deno with WASI (optimal filesystem performance)"; case "node-wasi": return "Node.js with WASI (high performance)"; case "bun-wasi": return "Bun with WASI (Bun-native file I/O)"; case "browser": return "Browser with Emscripten (web compatibility)"; case "worker": return "Web Worker with Emscripten"; case "cloudflare": return "Cloudflare Workers (limited streaming)"; case "node-emscripten": return "Node.js with Emscripten (fallback mode)"; default: return "Unknown environment"; } } function canLoadWasmType(wasmType) { const result = detectRuntime(); if (wasmType === "wasi") { return result.environment === "deno-wasi" || result.environment === "node-wasi" || result.environment === "bun-wasi"; } return true; } export { canLoadWasmType, checkNodeVersion, detectRuntime, getEnvironmentDescription, isDeno, supportsExnref };