Adjust WebAssembly.{Memory, ModuleImports} usage to cater for non-dom TS (#379)

* Adjust `WebAssembly.{Memory, ModuleImports}` usage to cater for non-dom TS

* Remove wbg getter (only internal access required)

* Additional peerDeps for wasm-crypto-init

* Bump @polkadot/dev

* Remove never cast & comment, imports are functions
This commit is contained in:
Jaco
2022-07-01 16:01:56 +02:00
committed by GitHub
parent 2f042819ac
commit e9d034928f
7 changed files with 28 additions and 22 deletions

View File

@@ -5,6 +5,7 @@
Changes:
- Add missing `peerDependencies` to `wasm-crypto` (`bridge` requirement)
- Adjust `WebAssembly.{Memory, ModuleImports}` usage to cater for non-dom TS
## 6.1.5 Jun 23, 2022

View File

@@ -32,7 +32,7 @@
},
"devDependencies": {
"@babel/core": "^7.18.6",
"@polkadot/dev": "^0.67.45",
"@polkadot/dev": "^0.67.46",
"fflate": "^0.7.3",
"override-require": "^1.1.1"
},

View File

@@ -7,7 +7,7 @@
// then ensures that the internal wasm instance here is available
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import type { BridgeBase, InitFn, InitPromise, WasmBaseInstance } from './types';
import type { BridgeBase, InitFn, InitPromise, WasmBaseInstance, WasmImports } from './types';
import { stringToU8a, u8aToString } from '@polkadot/util';
@@ -31,7 +31,7 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
#wasm: C | null;
#wasmError: string | null;
#wasmPromise: InitPromise<C> | null;
#wbg: WebAssembly.ModuleImports;
#wbg: WasmImports;
#type: 'asm' | 'wasm' | 'none';
constructor (createWasm: InitFn<C>) {
@@ -59,11 +59,6 @@ export class Bridge<C extends WasmBaseInstance> implements BridgeBase<C> {
return this.#type;
}
/** @description Returns the created wbg interface */
get wbg (): WebAssembly.ModuleImports {
return this.#wbg;
}
/** @description Returns the created wasm interface */
get wasm (): C | null {
return this.#wasm;

View File

@@ -1,7 +1,7 @@
// Copyright 2019-2022 @polkadot/wasm-bundle authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { InitFn, InitPromise, InitResult, WasmBaseInstance } from './types';
import type { InitFn, InitPromise, InitResult, WasmBaseInstance, WasmImports } from './types';
/**
* @name createWasmFn
@@ -11,8 +11,8 @@ import type { InitFn, InitPromise, InitResult, WasmBaseInstance } from './types'
* It will attempt to create a WASM interface first and if this fails or is not available in
* the environment, will fallback to attempting to create an ASM.js interface.
*/
export function createWasmFn <C extends WasmBaseInstance> (root: string, wasmBytes: null | Uint8Array, asmFn: null | ((wbg: WebAssembly.ModuleImports) => C)): InitFn<C> {
return async (wbg: WebAssembly.ModuleImports): InitPromise<C> => {
export function createWasmFn <C extends WasmBaseInstance> (root: string, wasmBytes: null | Uint8Array, asmFn: null | ((wbg: WasmImports) => C)): InitFn<C> {
return async (wbg: WasmImports): InitPromise<C> => {
const result: InitResult<C> = {
error: null,
type: 'none',

View File

@@ -1,6 +1,14 @@
// Copyright 2019-2022 @polkadot/wasm-bridge authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Use non-strong types instead of WasmImports which may not
// be available as part of the TS environment types (it needs dom)
export type WasmImports = Record<string, (...args: never[]) => unknown>;
// Use non-strong types instead of WebAssembly.Memory which may not
// be available as part of the TS environment types (it needs dom)
export type WasmMemory = { buffer: ArrayBuffer };
export declare interface InitResult<C extends WasmBaseInstance> {
error: string | null;
type: 'asm' | 'wasm' | 'none';
@@ -9,11 +17,9 @@ export declare interface InitResult<C extends WasmBaseInstance> {
export type InitPromise <C extends WasmBaseInstance> = Promise<InitResult<C>>;
export type InitFn <C extends WasmBaseInstance> = (wbg: WebAssembly.ModuleImports) => InitPromise<C>;
export type InitFn <C extends WasmBaseInstance> = (wbg: WasmImports) => InitPromise<C>;
export interface BridgeBase<C extends WasmBaseInstance> extends InitResult<C> {
wbg: WebAssembly.ModuleImports;
init (createWasm?: InitFn<C>): Promise<C | null>;
getObject (idx: number): unknown;
dropObject (idx: number): void;
@@ -30,7 +36,7 @@ export interface BridgeBase<C extends WasmBaseInstance> extends InitResult<C> {
}
export interface WasmBaseInstance {
memory: WebAssembly.Memory;
memory: WasmMemory;
// wbindgen functions (required and used internally)

View File

@@ -31,9 +31,11 @@
"@polkadot/wasm-crypto-wasm": "6.1.6-4-x"
},
"devDependencies": {
"@polkadot/util": "^9.6.2"
"@polkadot/util": "^9.6.2",
"@polkadot/x-randomvalues": "^9.6.2"
},
"peerDependencies": {
"@polkadot/util": "*"
"@polkadot/util": "*",
"@polkadot/x-randomvalues": "*"
}
}

View File

@@ -1941,9 +1941,9 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/dev@npm:^0.67.45":
version: 0.67.45
resolution: "@polkadot/dev@npm:0.67.45"
"@polkadot/dev@npm:^0.67.46":
version: 0.67.46
resolution: "@polkadot/dev@npm:0.67.46"
dependencies:
"@babel/cli": ^7.18.6
"@babel/core": ^7.18.6
@@ -2030,7 +2030,7 @@ __metadata:
polkadot-exec-rollup: scripts/polkadot-exec-rollup.mjs
polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs
polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs
checksum: 0128e457dc980b2fa400809805d703cdedf8c06e301e6c62115709cbe1af3e38cf2d234ef6b949615374963b132f129a8514e608456e0f33c8d53e12777c0622
checksum: 94e84748f6756bffbb6de3aa16e94e2a2b72706bc152a00730992c0cb344612dfc15e7f2478e5baea08dc653e17c274bb5b047c3ba29d0b9a248e500c5d1769c
languageName: node
linkType: hard
@@ -2083,8 +2083,10 @@ __metadata:
"@polkadot/wasm-bridge": 6.1.6-4-x
"@polkadot/wasm-crypto-asmjs": 6.1.6-4-x
"@polkadot/wasm-crypto-wasm": 6.1.6-4-x
"@polkadot/x-randomvalues": ^9.6.2
peerDependencies:
"@polkadot/util": "*"
"@polkadot/x-randomvalues": "*"
languageName: unknown
linkType: soft
@@ -8592,7 +8594,7 @@ resolve@^2.0.0-next.3:
resolution: "root-workspace-0b6124@workspace:."
dependencies:
"@babel/core": ^7.18.6
"@polkadot/dev": ^0.67.45
"@polkadot/dev": ^0.67.46
fflate: ^0.7.3
override-require: ^1.1.1
languageName: unknown