payid-rule-engine 0.2.7 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/wasm.ts +63 -30
package/package.json
CHANGED
package/src/wasm.ts
CHANGED
|
@@ -1,39 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
let _wasmUrl = '/rule_engine.wasm';
|
|
2
|
+
|
|
3
|
+
let _instance: WebAssembly.Instance | null = null;
|
|
4
|
+
let _loading: Promise<WebAssembly.Instance> | null = null;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Override URL tempat WASM di-fetch di browser.
|
|
8
|
+
* Panggil sebelum createPayID() / createPayIDServer():
|
|
9
|
+
*
|
|
10
|
+
* import { setWasmUrl } from 'payid-rule-engine';
|
|
11
|
+
* setWasmUrl('https://gateway.pinata.cloud/ipfs/YOUR_CID');
|
|
12
|
+
*/
|
|
13
|
+
export function setWasmUrl(url: string) {
|
|
14
|
+
if (url === _wasmUrl) return;
|
|
15
|
+
_wasmUrl = url;
|
|
16
|
+
_instance = null;
|
|
17
|
+
_loading = null;
|
|
18
|
+
}
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
fd_fdstat_get: () => 8,
|
|
20
|
-
fd_prestat_get: () => 8,
|
|
21
|
-
fd_prestat_dir_name: () => 8,
|
|
22
|
-
environ_get: () => 0,
|
|
23
|
-
environ_sizes_get: () => 0,
|
|
24
|
-
args_get: () => 0,
|
|
25
|
-
args_sizes_get: () => 0,
|
|
26
|
-
clock_time_get: () => 0,
|
|
27
|
-
proc_exit: () => { },
|
|
28
|
-
random_get: () => 0,
|
|
29
|
-
};
|
|
20
|
+
const wasiStub: Record<string, (...args: any[]) => any> = {
|
|
21
|
+
fd_write: () => 8, fd_read: () => 8, fd_close: () => 0,
|
|
22
|
+
fd_seek: () => 8, fd_fdstat_get: () => 8, fd_prestat_get: () => 8,
|
|
23
|
+
fd_prestat_dir_name: () => 8, environ_get: () => 0,
|
|
24
|
+
environ_sizes_get: () => 0, args_get: () => 0, args_sizes_get: () => 0,
|
|
25
|
+
clock_time_get: () => 0, proc_exit: () => { }, random_get: () => 0,
|
|
26
|
+
};
|
|
30
27
|
|
|
28
|
+
async function compile(binary: Uint8Array): Promise<WebAssembly.Instance> {
|
|
29
|
+
const module = await WebAssembly.compile(binary);
|
|
31
30
|
const instance = await WebAssembly.instantiate(module, {
|
|
32
31
|
wasi_snapshot_preview1: wasiStub,
|
|
33
32
|
});
|
|
34
|
-
|
|
35
33
|
const _init = instance.exports._initialize as (() => void) | undefined;
|
|
36
34
|
if (_init) _init();
|
|
37
|
-
|
|
38
35
|
return instance;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function loadWasm(binary?: Buffer | Uint8Array): Promise<WebAssembly.Instance> {
|
|
39
|
+
if (binary && binary.length > 0) {
|
|
40
|
+
return compile(binary instanceof Uint8Array ? binary : new Uint8Array(binary));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (_instance) return _instance;
|
|
44
|
+
|
|
45
|
+
if (_loading) return _loading;
|
|
46
|
+
|
|
47
|
+
_loading = (async () => {
|
|
48
|
+
let wasmBinary: Uint8Array;
|
|
49
|
+
|
|
50
|
+
if (typeof process !== 'undefined' && process.versions?.node) {
|
|
51
|
+
const { readFileSync } = await import(/* webpackIgnore: true */ 'fs');
|
|
52
|
+
const { join, dirname } = await import(/* webpackIgnore: true */ 'path');
|
|
53
|
+
const { fileURLToPath } = await import(/* webpackIgnore: true */ 'url');
|
|
54
|
+
const __dir = dirname(fileURLToPath(import.meta.url));
|
|
55
|
+
const buf = readFileSync(join(__dir, 'wasm/rule_engine.wasm'));
|
|
56
|
+
wasmBinary = new Uint8Array(buf);
|
|
57
|
+
} else {
|
|
58
|
+
const res = await fetch(_wasmUrl);
|
|
59
|
+
if (!res.ok) throw new Error(
|
|
60
|
+
`[PAY.ID] Failed to fetch WASM from "${_wasmUrl}": HTTP ${res.status}`
|
|
61
|
+
);
|
|
62
|
+
wasmBinary = new Uint8Array(await res.arrayBuffer());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const instance = await compile(wasmBinary);
|
|
66
|
+
_instance = instance;
|
|
67
|
+
_loading = null;
|
|
68
|
+
return instance;
|
|
69
|
+
})();
|
|
70
|
+
|
|
71
|
+
return _loading;
|
|
39
72
|
}
|