Only rely on wasm.js (no asm.js duplication) (#25)

* Only rely on wasm.js (no asm.js duplication)

* Add browser entry

* Make asm.js tests pass

* Remove (now-unsued) mock dep
This commit is contained in:
Jaco Greeff
2019-07-19 11:06:31 +02:00
committed by GitHub
parent 309ba91122
commit 8c6d2aea12
8 changed files with 55 additions and 68 deletions
+2 -1
View File
@@ -22,6 +22,7 @@
"devDependencies": {
"@babel/core": "^7.5.4",
"@polkadot/dev": "^0.30.0-beta.23",
"@polkadot/util": "^0.94.0-beta.16"
"@polkadot/util": "^0.94.0-beta.17",
"override-require": "^1.1.1"
}
}
+5 -2
View File
@@ -3,7 +3,6 @@
"version": "0.13.0-beta.2",
"author": "Jaco Greeff <jacogr@gmail.com>",
"files": [
"asm.js",
"crypto-polyfill.js",
"exports.js",
"index.d.ts",
@@ -11,9 +10,13 @@
"wasm.d.ts",
"wasm.js",
"wasm_asm.js",
"wasm_bg.js",
"wasm_asm_stub.js",
"wasm_promise.js",
"wasm_wasm.js"
],
"react-native": {
"./wasm_asm_stub.js": "./wasm_asm.js"
},
"main": "index.js",
"types": "index.d.ts",
"peerDependencies": {
@@ -1,8 +1,5 @@
/* eslint-disable @typescript-eslint/no-var-requires */
// Copyright 2019 @polkadot/wasm-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
const stubbed = require('./asm');
module.exports = require('./exports')(stubbed);
module.exports = {};
@@ -0,0 +1,28 @@
/* eslint-disable @typescript-eslint/no-var-requires */
// Copyright 2019 @polkadot/wasm-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
const pkg = require('./package.json');
const asm = require('./wasm_asm_stub');
const bytes = require('./wasm_wasm');
const js = require('./wasm');
module.exports = async function createExportPromise () {
const imports = {
'./wasm': js
};
if (WebAssembly) {
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
console.error(`ERROR: Unable to initialize WebAssembly for ${pkg.name} ${pkg.version}`);
console.error(error);
}
}
return asm;
};
+8 -2
View File
@@ -7,9 +7,15 @@
// @ts-ignore
global.WebAssembly = null;
require('override-require')(
(request) => request === './wasm_asm_stub',
// @ts-ignore
() => require('../build/wasm_asm')
);
// @ts-ignore
const asm = require('../build/index');
const { beforeAll, runAll } = require('./all')(asm);
const wasm = require('../build/index');
const { beforeAll, runAll } = require('./all')(wasm);
(async () => {
await beforeAll();
+1 -54
View File
@@ -16,7 +16,6 @@ mv pkg build
# shortcuts for files
echo "*** Adjusting output"
BGJ=build/wasm_bg.js
SRC_ASM=build/asm.js
SRC_WASM=build/wasm.js
DEF=build/wasm.d.ts
WSM=build/wasm_bg.wasm
@@ -34,9 +33,6 @@ echo "*** Optimising WASM output"
echo "*** Building asm.js version"
../../binaryen/bin/wasm2js --no-validation --output $TMP $OPT
# cleanup asm
sed -i -e 's/\.\/wasm/\.\/asm/g' $TMP
# get babel to take care of making it sane
NODE_OPTIONS=--max_old_space_size=8192 yarn babel $TMP --presets @babel/preset-env --out-file=$ASM
rm -rf $TMP
@@ -66,13 +62,11 @@ sed -i -e 's/const TextDecoder = require('\''util'\'')\.TextDecoder;/const { u8a
sed -i -e 's/let cachedTextDecoder = new /\/\/ let cachedTextDecoder = new /g' $SRC_WASM
sed -i -e 's/cachedTextDecoder\.decode/u8aToString/g' $SRC_WASM
cp -f $SRC_WASM $SRC_ASM
# construct our promise and add ready helpers (WASM)
echo "
module.exports.abort = function () { throw new Error('abort'); };
const createPromise = require('./wasm_bg');
const createPromise = require('./wasm_promise');
const wasmPromise = createPromise().catch(() => null);
module.exports.isReady = function () { return !!wasm; }
@@ -81,55 +75,8 @@ module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm);
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC_WASM
# construct our promise and add ready helpers (ASM)
echo "
module.exports.abort = function () { throw new Error('abort'); };
const asmjs = require('./wasm_asm');
const wasmPromise = Promise.resolve(asmjs);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC_ASM
# add extra methods to type definitions
echo "
export function isReady(): boolean;
export function waitReady(): Promise<boolean>;
" >> $DEF
# create the init promise handler
echo "
const pkg = require('./package.json');
const bytes = require('./wasm_wasm');
const js = require('./wasm');
module.exports = async function createExportPromise () {
const imports = {
'./wasm': js
};
if (!WebAssembly) {
if (process.env.CRYPTO_ASM) {
return require('./wasm_asm');
}
console.error(\`ERROR: Unable to initialize \${pkg.name}, WebAssembly is not available in this environment. If you requires an asm.js fallback, specify it with the 'CRYPTO_ASM' environment flag.\`);
return null;
}
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
console.error(\`ERROR: Unable to initialize \${pkg.name}:: \${error.message}\`);
// TODO: Return asm version here as a fallback
return null;
}
}
" > $BGJ
+1 -1
View File
@@ -11,7 +11,7 @@ echo "*** Testing WASM"
node ./test/wasm.js
echo "*** Testing ASM.js"
CRYPTO_ASM=1 node ./test/asm.js
node ./test/asm.js
echo "*** Testing via Jest"
yarn jest ./test/jest.spec.js
+9 -4
View File
@@ -2030,10 +2030,10 @@
typescript "^3.5.3"
vuepress "^1.0.2"
"@polkadot/util@^0.94.0-beta.16":
version "0.94.0-beta.16"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.94.0-beta.16.tgz#cf014be6ce108bfc1564cd92dc203a895b7bc655"
integrity sha512-AxpxvWGTHKfQ+SXyIOOc2beJ0UuKd9i/xLvMlyEaeoR/397PqdJGvGh67Jyn7sm3PI6DSH1mtGsxxsaJwCfLQA==
"@polkadot/util@^0.94.0-beta.17":
version "0.94.0-beta.17"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.94.0-beta.17.tgz#998a0cb69d7005d4fcb0c94a9d2db1fa48ae584b"
integrity sha512-NYZF1gN4Yr31uMJe9x6MSTuvBOPn3NOH2RTs5Juz4wCG6Q2ejjPk0l3HG4/6rDq5Uh+x/zw3DREpayBpN36ihw==
dependencies:
"@babel/runtime" "^7.5.5"
"@types/bn.js" "^4.11.5"
@@ -8738,6 +8738,11 @@ output-file-sync@^2.0.0:
is-plain-obj "^1.1.0"
mkdirp "^0.5.1"
override-require@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/override-require/-/override-require-1.1.1.tgz#6ae22fadeb1f850ffb0cf4c20ff7b87e5eb650df"
integrity sha1-auIvresfhQ/7DPTCD/e4fl62UN8=
p-defer@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/p-defer/-/p-defer-1.0.0.tgz#9f6eb182f6c9aa8cd743004a7d4f96b196b0fb0c"