diff --git a/platform/nodejs/main.js b/platform/nodejs/main.js index d2e0110c1..c17e20d47 100644 --- a/platform/nodejs/main.js +++ b/platform/nodejs/main.js @@ -82,33 +82,18 @@ function applyList(name, raw) { async function enableWASM() { const wasmModuleFetcher = function(path) { - return new Promise((resolve, reject) => { - const require = createRequire(import.meta.url); // jshint ignore:line - const fs = require('fs'); - fs.readFile(`${path}.wasm`, null, (err, data) => { - if ( err ) { return reject(err); } - return globals.WebAssembly.compile(data).then(module => { - resolve(module); - }).catch(reason => { - reject(reason); - }); - }); - }); + const require = createRequire(import.meta.url); // jshint ignore:line + const wasm = new Uint8Array(require(`${path}.wasm.json`)); + return globals.WebAssembly.compile(wasm); }; try { const results = await Promise.all([ - globals.publicSuffixList.enableWASM( - wasmModuleFetcher, - './lib/publicsuffixlist/wasm/' - ), - snfe.enableWASM( - wasmModuleFetcher, - './js/wasm/' - ), + globals.publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'), + snfe.enableWASM(wasmModuleFetcher, './js/wasm/'), ]); return results.every(a => a === true); } catch(reason) { - console.info(reason); + console.log(reason); } return false; } @@ -123,7 +108,6 @@ function pslInit(raw) { } } globals.publicSuffixList.parse(raw, globals.punycode.toASCII); - return globals.publicSuffixList; } function restart(lists, options = {}) { diff --git a/tools/make-nodejs.sh b/tools/make-nodejs.sh index 2fe664d5a..93de6aa06 100755 --- a/tools/make-nodejs.sh +++ b/tools/make-nodejs.sh @@ -16,13 +16,20 @@ cp src/js/static-filtering-io.js $DES/js cp src/js/text-iterators.js $DES/js cp src/js/uri-utils.js $DES/js -mkdir -p $DES/js/wasm -cp src/js/wasm/* $DES/js/wasm/ - mkdir -p $DES/lib cp -R src/lib/punycode.js $DES/lib/ -cp -R src/lib/publicsuffixlist $DES/lib/ cp -R src/lib/regexanalyzer $DES/lib/ +cp -R src/lib/publicsuffixlist $DES/lib/ + +# Convert wasm modules into json arrays +mkdir -p $DES/js/wasm +cp src/js/wasm/* $DES/js/wasm/ +node -pe "JSON.stringify(Array.from(fs.readFileSync('src/js/wasm/hntrie.wasm')))" \ + > $DES/js/wasm/hntrie.wasm.json +node -pe "JSON.stringify(Array.from(fs.readFileSync('src/js/wasm/biditrie.wasm')))" \ + > $DES/js/wasm/biditrie.wasm.json +node -pe "JSON.stringify(Array.from(fs.readFileSync('src/lib/publicsuffixlist/wasm/publicsuffixlist.wasm')))" \ + > $DES/lib/publicsuffixlist/wasm/publicsuffixlist.wasm.json git submodule update --depth 1 --init UASSETS=submodules/uAssets