From 6b0473d4956e2e34397ed6889a2df3108b0eb251 Mon Sep 17 00:00:00 2001 From: Bernd Schoolmann Date: Thu, 9 Mar 2023 20:40:48 +0100 Subject: [PATCH] Make simd load automatically --- build-wasm.sh | 3 ++- lib/argon2.js | 13 ++++++++----- package-lock.json | 18 ++++++++++++++++-- package.json | 5 ++++- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/build-wasm.sh b/build-wasm.sh index 270616c..1ed6a4b 100755 --- a/build-wasm.sh +++ b/build-wasm.sh @@ -33,5 +33,6 @@ shasum dist/argon2.wasm perl -pi -e 's/"argon2.js.mem"/null/g' dist/argon2.js perl -pi -e 's/$/if(typeof module!=="undefined")module.exports=Module;Module.unloadRuntime=function(){if(typeof self!=="undefined"){delete self.Module}Module=jsModule=wasmMemory=wasmTable=asm=buffer=HEAP8=HEAPU8=HEAP16=HEAPU16=HEAP32=HEAPU32=HEAPF32=HEAPF64=undefined;if(typeof module!=="undefined"){delete module.exports}};/' dist/argon2.js -perl -pi -e 's/typeof Module!=="undefined"\?Module:\{};/typeof self!=="undefined"&&typeof self.Module!=="undefined"?self.Module:{};var jsModule=Module;/g' dist/argon2.js +perl -pi -e 's/typeof Module!="undefined"\?Module:\{};/typeof self!=="undefined"&&typeof self.Module!=="undefined"?self.Module:{};var jsModule=Module;/g' dist/argon2.js perl -pi -e 's/receiveInstantiatedSource\(output\)\{/receiveInstantiatedSource(output){Module=jsModule;if(typeof self!=="undefined")self.Module=Module;/g' dist/argon2.js +perl -pi -e 's/argon2.wasm/argon2-simd.wasm/g' dist/argon2.js \ No newline at end of file diff --git a/lib/argon2.js b/lib/argon2.js index 98d3fb0..6836eed 100644 --- a/lib/argon2.js +++ b/lib/argon2.js @@ -79,15 +79,18 @@ return global.loadArgon2WasmBinary(); } if (typeof require === 'function') { - return Promise.resolve(require('../dist/argon2.wasm')).then( - (wasmModule) => { - return decodeWasmBinary(wasmModule); - } + var simd = require('wasm-feature-detect').simd; + return Promise.resolve( + simd() + .then((simd) => require('../dist/argon2' + (simd? "-simd" : "") + '.wasm')) + .then((wasmModule) => { + return decodeWasmBinary(wasmModule); + }) ); } const wasmPath = global.argon2WasmPath || - 'node_modules/argon2-browser/dist/argon2.wasm'; + 'node_modules/argon2-browser/dist/argon2-simd.wasm'; return fetch(wasmPath) .then((response) => response.arrayBuffer()) .then((ab) => new Uint8Array(ab)); diff --git a/package-lock.json b/package-lock.json index f1df357..72fab03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,16 @@ { "name": "argon2-browser", - "version": "1.18.0", + "version": "1.18.1", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "1.18.0", + "name": "argon2-browser", + "version": "1.18.1", "license": "MIT", + "dependencies": { + "wasm-feature-detect": "^1.5.1" + }, "devDependencies": { "base64-loader": "^1.0.0", "chai": "^4.3.4", @@ -2492,6 +2496,11 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, + "node_modules/wasm-feature-detect": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/wasm-feature-detect/-/wasm-feature-detect-1.5.1.tgz", + "integrity": "sha512-GHr23qmuehNXHY4902/hJ6EV5sUANIJC3R/yMfQ7hWDg3nfhlcJfnIL96R2ohpIwa62araN6aN4bLzzzq5GXkg==" + }, "node_modules/watchpack": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.2.0.tgz", @@ -4830,6 +4839,11 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, + "wasm-feature-detect": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/wasm-feature-detect/-/wasm-feature-detect-1.5.1.tgz", + "integrity": "sha512-GHr23qmuehNXHY4902/hJ6EV5sUANIJC3R/yMfQ7hWDg3nfhlcJfnIL96R2ohpIwa62araN6aN4bLzzzq5GXkg==" + }, "watchpack": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.2.0.tgz", diff --git a/package.json b/package.json index 51ff9b4..10d1ad0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "argon2-browser", - "version": "1.18.0", + "version": "1.18.1", "description": "Argon2 library compiled for browser runtime", "main": "lib/argon2.js", "directories": { @@ -30,5 +30,8 @@ "puppeteer": "^9.1.1", "webpack": "^5.37.1", "webpack-cli": "^4.7.0" + }, + "dependencies": { + "wasm-feature-detect": "^1.5.1" } }