taglib-wasm
Version:
TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps
80 lines (79 loc) • 2.43 kB
JavaScript
import { detectRuntime } from "../detector.js";
import { ModuleLoadError } from "./types.js";
import { selectWasmType } from "./module-selection.js";
import { loadModule } from "./module-loading.js";
async function loadUnifiedTagLibModule(options = {}) {
if (options.forceWasmType === "wasi" && options.wasmBinary) {
throw new ModuleLoadError(
"wasmBinary is not supported by the WASI backend: it loads from a filesystem path or URL. Use wasmUrl, or omit forceWasmType to select the Emscripten backend",
"wasi"
);
}
const startTime = performance.now();
const runtime = detectRuntime();
if (options.debug) {
console.log(`[UnifiedLoader] Detected runtime: ${runtime.environment}`);
}
const wasmType = selectWasmType(runtime, options);
if (options.debug) {
console.log(
`[UnifiedLoader] Selected ${wasmType} for ${runtime.environment}`
);
}
const { module, actualWasmType } = await loadModule(
wasmType,
runtime,
options
);
const unifiedModule = await createUnifiedModule(
module,
runtime,
actualWasmType,
startTime
);
if (options.debug) {
const initTime = performance.now() - startTime;
console.log(`[UnifiedLoader] Initialized in ${initTime.toFixed(2)}ms`);
}
return unifiedModule;
}
async function createUnifiedModule(module, runtime, wasmType, startTime) {
const isWasi = wasmType === "wasi";
const initTime = performance.now() - startTime;
if (isWasi) {
const wasiModule = module;
const adapter = await createWasiAdapter(wasiModule);
return Object.assign(adapter, {
runtime,
isWasi: true,
isEmscripten: false,
getPerformanceMetrics: () => ({
initTime,
wasmType: "wasi",
environment: runtime.environment,
memoryUsage: wasiModule.memory.buffer.byteLength
})
});
} else {
const emscriptenModule = module;
return {
...emscriptenModule,
runtime,
isWasi: false,
isEmscripten: true,
getPerformanceMetrics: () => ({
initTime,
wasmType: "emscripten",
environment: runtime.environment,
memoryUsage: emscriptenModule.HEAP8?.buffer.byteLength
})
};
}
}
async function createWasiAdapter(wasiModule) {
const { WasiToTagLibAdapter } = await import("../wasi-adapter/index.js");
return new WasiToTagLibAdapter(wasiModule);
}
export {
loadUnifiedTagLibModule
};