node/test/module-hooks/test-module-hooks-require-wasm.js
Guy Bedford 0df15188d7 esm: unflag --experimental-wasm-modules
PR-URL: https://github.com/nodejs/node/pull/57038
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2025-07-22 13:24:06 -07:00

33 lines
1 KiB
JavaScript

'use strict';
// This tests that module.registerHooks() can be used to support unknown formats, like
// require(wasm) and import(wasm)
const common = require('../common');
const assert = require('assert');
const { registerHooks } = require('module');
const { readFileSync } = require('fs');
registerHooks({
load(url, context, nextLoad) {
assert.match(url, /simple\.wasm$/);
const source =
`const buf = Buffer.from([${Array.from(readFileSync(new URL(url))).join(',')}]);
const compiled = new WebAssembly.Module(buf);
module.exports = (new WebAssembly.Instance(compiled)).exports;`;
return {
shortCircuit: true,
source,
format: 'commonjs',
};
},
});
// Checks that it works with require.
const { add } = require('../fixtures/simple.wasm');
assert.strictEqual(add(1, 2), 3);
(async () => { // Checks that it works with import.
const { default: { add } } = await import('../fixtures/simple.wasm');
assert.strictEqual(add(1, 2), 3);
})().then(common.mustCall());