Repository: xenova/transformers.js
File: packages/transformers/src/backends/utils/cacheWasm.js (JavaScript)

Code before:
    const response = await loadAndCacheFile(wasmURL);
    if (!response || typeof response === 'string') return null;

    try {
        return await response.arrayBuffer();
    } catch (error) {
        logger.warn('Failed to read WASM binary:', error);
        return null;
    }
}

/**
 * Loads and caches the WASM Factory for ONNX Runtime.
 * @param {string} libURL The URL of the WASM Factory to load.
 * @returns {Promise<string|null>} The blob URL of the WASM Factory, or null if loading failed.
 */
export async function loadWasmFactory(libURL) {
    const response = await loadAndCacheFile(libURL);
    if (!response || typeof response === 'string') return null;

    try {
        let code = await response.text();
        // Fix relative paths when loading factory from blob, overwrite import.meta.url with actual baseURL
        const baseUrl = libURL.split('/').slice(0, -1).join('/');
        code = code.replace(/import\.meta\.url/g, `"${baseUrl}"`);
        const blob = new Blob([code], { type: 'text/javascript' });
        return URL.createObjectURL(blob);
    } catch (error) {
        logger.warn('Failed to read WASM binary:', error);
        return null;
    }
}

/**
 * Checks if the given URL is a blob URL (created via URL.createObjectURL).
 * Blob URLs should not be cached as they are temporary in-memory references.
 * @param {string} url - The URL to check.
 * @returns {boolean} True if the URL is a blob URL, false otherwise.
 */
export function isBlobURL(url) {
    return isValidUrl(url, ['blob:']);
}

/**
 * Converts any URL to an absolute URL if needed.
 * If the URL is already absolute (http://, https://, or blob:), returns it unchanged (handled by new URL(...)).
 * Otherwise, resolves it relative to the current page location (browser) or module location (Node/Bun/Deno).
 * @param {string} url - The URL to convert (can be relative or absolute).
 * @returns {string} The absolute URL.
 */
export function toAbsoluteURL(url) {

Review comment (suggestion):
I see. There are two occurences of `globalThis.process?.versions?.node` in [ort-wasm-simd-threaded.asyncify.mjs](https://cdn.jsdelivr.net/npm/onnxruntime-web@1.25.0-dev.20260125-727db0d3dc/dist/ort-wasm-simd-threaded.asyncify.mjs). And the idea is that the factory thinks we are not in a node context.
Why do we need to do this everywhere? If it was only if `IS_DENO_WEB_RUNTIME` I could see the point. But otherwise it seems like a potential source of sideeffects.

Code after:
    const response = await loadAndCacheFile(wasmURL);
    if (!response || typeof response === 'string') return null;

    try {
        return await response.arrayBuffer();
    } catch (error) {
        logger.warn('Failed to read WASM binary:', error);
        return null;
    }
}

/**
 * Loads and caches the WASM Factory for ONNX Runtime.
 * @param {string} libURL The URL of the WASM Factory to load.
 * @returns {Promise<string|null>} The blob URL of the WASM Factory, or null if loading failed.
 */
export async function loadWasmFactory(libURL) {
    const response = await loadAndCacheFile(libURL);
    if (!response || typeof response === 'string') return null;

    try {
        let code = await response.text();
        // Fix relative paths when loading factory from blob, overwrite import.meta.url with actual baseURL
        const baseUrl = libURL.split('/').slice(0, -1).join('/');
        code = code.replaceAll('import.meta.url', `"${baseUrl}"`);
        code = code.replaceAll('globalThis.process?.versions?.node', 'false');
        const blob = new Blob([code], { type: 'text/javascript' });
        return URL.createObjectURL(blob);
    } catch (error) {
        logger.warn('Failed to read WASM binary:', error);
        return null;
    }
}

/**
 * Checks if the given URL is a blob URL (created via URL.createObjectURL).
 * Blob URLs should not be cached as they are temporary in-memory references.
 * @param {string} url - The URL to check.
 * @returns {boolean} True if the URL is a blob URL, false otherwise.
 */
export function isBlobURL(url) {
    return isValidUrl(url, ['blob:']);
}

/**
 * Converts any URL to an absolute URL if needed.
 * If the URL is already absolute (http://, https://, or blob:), returns it unchanged (handled by new URL(...)).
 * Otherwise, resolves it relative to the current page location (browser) or module location (Node/Bun/Deno).
 * @param {string} url - The URL to convert (can be relative or absolute).
 * @returns {string} The absolute URL.
 */