Add initNone (#316)

* Add initNone

* Add force to explicit call
This commit is contained in:
Jaco
2022-03-23 12:54:11 +01:00
committed by GitHub
parent eb445e79e2
commit f7e6acbf9c
11 changed files with 71 additions and 32 deletions

View File

@@ -1,9 +1,16 @@
# CHANGELOG
## master
Changes:
- Allow for `initNone` with no Wasm and no Asm
## 5.0.1 Mar 19, 2022
- **Breaking change** For users of React Native, you are now required to add `import '@polkadot/wasm-crypto/initOnlyAsm'` at your project top-level to ensure that asm.js is initialized. (Or alternatively `import '@polkadot/wasm-crypto/initWasmAsm'` to future-proof when WASM does become available)
- **Breaking change** For users who used to map the `data` and `empty` of the internal `wasm-crypto-{wasm, asmjs}` packages in their bundlers, swap to one of the `@polkadot/wasm-crypto/init*` top-level imports to set the type of interfaces you would prefer. A full writeup of the rationale and other options can be found [in the FAQ](https://polkadot.js.org/docs/util-crypto/FAQ#i-dont-have-wasm-available-in-my-environment)
- **Breaking change** For users who used to map the `data` and `empty` of the internal `wasm-crypto-{wasm, asmjs}` packages in their bundlers, swap to one of the `@polkadot/wasm-crypto/init*` top-level imports to set the type of interfaces you would prefer. A full writeup of the rationale and other options can be found [in the FAQ](https://polkadot.js.org/docs/util-crypto/FAQ#i-dont-have-wasm-available-in-my-environment)
Changes:

View File

@@ -1,8 +1,5 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-asmjs authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { asmJsInit as _asmJsInit } from './cjs/data.js';
export { asmJsInit } from './cjs/data.js';
export { packageInfo } from './packageInfo';
export const asmJsInit = _asmJsInit;

View File

@@ -1,8 +1,8 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { bytes, sizeUncompressed } from './cjs/bytes';
import { base64Decode } from './base64';
import { bytes, sizeUncompressed } from './bytes';
import { unzlibSync } from './fflate';
export { packageInfo } from './packageInfo';

View File

@@ -1,10 +0,0 @@
// Copyright 2019-2022 @polkadot/wasm-crypto-wasm authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { bytes as _bytes, sizeCompressed as _sizeCompressed, sizeUncompressed as _sizeUncompressed } from './cjs/bytes';
export const bytes = _bytes;
export const sizeCompressed = _sizeCompressed;
export const sizeUncompressed = _sizeUncompressed;

View File

@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import { __bridge, allocString, allocU8a, getWasm, resultString, resultU8a, withWasm } from './bridge';
import { setWasmOnlyPromise } from './init';
import { setWasmOnlyPromise } from './initOnlyWasmBase';
export { packageInfo } from './packageInfo';

View File

@@ -3,8 +3,7 @@
import type { AsmCreator, WasmCryptoInstance } from './types';
import { assert } from '@polkadot/util';
import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
import { assert, isFunction } from '@polkadot/util';
import { __bridge } from './bridge';
import * as imports from './imports';
@@ -14,28 +13,29 @@ async function createPromise (wasmBytes: Uint8Array | null, asmFn: AsmCreator |
assert(typeof WebAssembly !== 'undefined' && wasmBytes && wasmBytes.length, 'WebAssembly is not available in your environment');
const source = await WebAssembly.instantiate(wasmBytes, { wbg: imports });
const exports = (source.instance.exports as unknown as WasmCryptoInstance);
__bridge.wasm = source.instance.exports as unknown as WasmCryptoInstance;
// eslint-disable-next-line @typescript-eslint/unbound-method
assert(isFunction(exports.ext_blake2b), 'Invalid instantiated WASM interface');
__bridge.wasm = exports;
} catch (error) {
// if we have a valid supplied asm.js, return that
if (asmFn) {
__bridge.type = 'asm';
__bridge.wasm = asmFn(imports);
} else {
console.error('FATAL: Unable to initialize @polkadot/wasm-crypto');
console.error(error);
console.error(`FATAL: Unable to initialize @polkadot/wasm-crypto:: ${(error as Error).message}`);
__bridge.wasm = null;
}
}
}
export function setPromise (wasmBytes: Uint8Array | null, asmFn: AsmCreator | null): Promise<void> {
__bridge.wasmPromise = createPromise(wasmBytes, asmFn);
export function setPromise (wasmBytes: Uint8Array | null, asmFn: AsmCreator | null, force = false): Promise<void> {
if (!__bridge.wasmPromise || force) {
__bridge.wasmPromise = createPromise(wasmBytes, asmFn);
}
return __bridge.wasmPromise;
}
export function setWasmOnlyPromise (): Promise<void> {
return setPromise(wasmBytes, null);
}

View File

@@ -0,0 +1,23 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AsmCreator } from './types';
import { __bridge } from './bridge';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function setPromise (_wasmBytes: Uint8Array | null, _asmFn: AsmCreator | null): Promise<void> {
__bridge.wasmPromise = Promise.resolve();
return __bridge.wasmPromise;
}
export function setWasmOnlyPromise (): Promise<void> {
return setPromise(null, null);
}
export function initWasm (): Promise<void> {
return setWasmOnlyPromise();
}
initWasm().catch(() => undefined);

View File

@@ -5,4 +5,8 @@ import { asmJsInit } from '@polkadot/wasm-crypto-asmjs';
import { setPromise } from './init';
setPromise(null, asmJsInit).catch(() => undefined);
export function initWasm (): Promise<void> {
return setPromise(null, asmJsInit, true);
}
initWasm().catch(() => undefined);

View File

@@ -1,6 +1,10 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { setWasmOnlyPromise } from './init';
import { setWasmOnlyPromise } from './initOnlyWasmBase';
setWasmOnlyPromise().catch(() => undefined);
export function initWasm (): Promise<void> {
return setWasmOnlyPromise();
}
initWasm().catch(() => undefined);

View File

@@ -0,0 +1,10 @@
// Copyright 2019-2022 @polkadot/wasm-crypto authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
import { setPromise } from './init';
export function setWasmOnlyPromise (force = false): Promise<void> {
return setPromise(wasmBytes, null, force);
}

View File

@@ -6,4 +6,8 @@ import { wasmBytes } from '@polkadot/wasm-crypto-wasm';
import { setPromise } from './init';
setPromise(wasmBytes, asmJsInit).catch(() => undefined);
export function initWasm (): Promise<void> {
return setPromise(wasmBytes, asmJsInit, true);
}
initWasm().catch(() => undefined);