From 04b9e76b703a1e9b70cc71e67a98c90571e1dde7 Mon Sep 17 00:00:00 2001 From: Leon Zandman Date: Fri, 20 Mar 2026 19:58:19 +0100 Subject: [PATCH] Test fix: add WASM fetch polyfill for Node.js 22+ compatibility --- tests/lib/wasmFetchPolyfill.mjs | 29 +++++++++++++++++++++++++++++ tests/operations/index.mjs | 1 + 2 files changed, 30 insertions(+) create mode 100644 tests/lib/wasmFetchPolyfill.mjs diff --git a/tests/lib/wasmFetchPolyfill.mjs b/tests/lib/wasmFetchPolyfill.mjs new file mode 100644 index 00000000..6ec6308d --- /dev/null +++ b/tests/lib/wasmFetchPolyfill.mjs @@ -0,0 +1,29 @@ +/** + * Polyfill for Node.js 22+ where globalThis.fetch is built-in but rejects + * bare filesystem paths. WASM libraries like argon2-browser call fetch() with + * an absolute path (e.g. "/path/to/argon2.wasm") expecting a browser-style + * fallback, but Node.js 22's fetch throws synchronously for non-URL strings. + * + * This wrapper intercepts such calls and serves the file via Node's fs module, + * returning a synthetic Response so the WASM module loads correctly. + */ + +import { readFile } from "fs/promises"; + +if (globalThis.fetch) { + const originalFetch = globalThis.fetch; + globalThis.fetch = async function patchedFetch(url, options) { + const urlStr = typeof url === "string" ? url + : url instanceof URL ? url.href + : String(url); + // Intercept bare filesystem paths (absolute POSIX or Windows) + if (urlStr.startsWith("/") || /^[A-Za-z]:[/\\]/.test(urlStr)) { + const buffer = await readFile(urlStr); + return new Response(buffer, { + status: 200, + headers: { "Content-Type": "application/wasm" }, + }); + } + return originalFetch(url, options); + }; +} diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 2134cdd9..3635b62f 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -11,6 +11,7 @@ * @license Apache-2.0 */ +import "../lib/wasmFetchPolyfill.mjs"; import { setLongTestFailure, logTestReport } from "../lib/utils.mjs"; import TestRegister from "../lib/TestRegister.mjs";