Simplify loading of WASM modules in nodejs

This commit is contained in:
Raymond Hill 2021-07-31 07:06:11 -04:00
parent 5be4d5d877
commit d5da88ea87
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 17 additions and 26 deletions

View File

@ -82,33 +82,18 @@ function applyList(name, raw) {
async function enableWASM() { async function enableWASM() {
const wasmModuleFetcher = function(path) { const wasmModuleFetcher = function(path) {
return new Promise((resolve, reject) => { const require = createRequire(import.meta.url); // jshint ignore:line
const require = createRequire(import.meta.url); // jshint ignore:line const wasm = new Uint8Array(require(`${path}.wasm.json`));
const fs = require('fs'); return globals.WebAssembly.compile(wasm);
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);
});
});
});
}; };
try { try {
const results = await Promise.all([ const results = await Promise.all([
globals.publicSuffixList.enableWASM( globals.publicSuffixList.enableWASM(wasmModuleFetcher, './lib/publicsuffixlist/wasm/'),
wasmModuleFetcher, snfe.enableWASM(wasmModuleFetcher, './js/wasm/'),
'./lib/publicsuffixlist/wasm/'
),
snfe.enableWASM(
wasmModuleFetcher,
'./js/wasm/'
),
]); ]);
return results.every(a => a === true); return results.every(a => a === true);
} catch(reason) { } catch(reason) {
console.info(reason); console.log(reason);
} }
return false; return false;
} }
@ -123,7 +108,6 @@ function pslInit(raw) {
} }
} }
globals.publicSuffixList.parse(raw, globals.punycode.toASCII); globals.publicSuffixList.parse(raw, globals.punycode.toASCII);
return globals.publicSuffixList;
} }
function restart(lists, options = {}) { function restart(lists, options = {}) {

View File

@ -16,13 +16,20 @@ cp src/js/static-filtering-io.js $DES/js
cp src/js/text-iterators.js $DES/js cp src/js/text-iterators.js $DES/js
cp src/js/uri-utils.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 mkdir -p $DES/lib
cp -R src/lib/punycode.js $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/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 git submodule update --depth 1 --init
UASSETS=submodules/uAssets UASSETS=submodules/uAssets