payid-rule-engine 0.2.4 → 0.2.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/wasm.ts +21 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payid-rule-engine",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
package/src/wasm.ts CHANGED
@@ -1,12 +1,28 @@
1
+ // URL to fetch WASM binary in browser environments.
2
+ // Override this before calling loadWasm() if hosting on custom CDN/IPFS:
3
+ // import { setWasmUrl } from 'payid-rule-engine/src/wasm';
4
+ // setWasmUrl('https://gateway.pinata.cloud/ipfs/YOUR_CID');
5
+ let _wasmUrl: string = 'https://gateway.pinata.cloud/ipfs/bafkreigwfxsb7oot7v55x7vxslvj23csxl2fhk2w7hsnboe55o26s2mgfy';
6
+
7
+ export function setWasmUrl(url: string) {
8
+ _wasmUrl = url;
9
+ }
10
+
1
11
  export async function loadWasm(binary?: Buffer | Uint8Array): Promise<WebAssembly.Instance> {
2
12
  let wasmBinary: Buffer | Uint8Array | undefined = binary;
3
13
 
4
14
  if (!wasmBinary || wasmBinary.length === 0) {
5
- const { readFileSync } = await import(/* webpackIgnore: true */ "fs");
6
- const { join, dirname } = await import(/* webpackIgnore: true */ "path");
7
- const { fileURLToPath } = await import(/* webpackIgnore: true */ "url");
8
- const __dir = dirname(fileURLToPath(import.meta.url));
9
- wasmBinary = readFileSync(join(__dir, "wasm/rule_engine.wasm"));
15
+ if (typeof process !== 'undefined' && process.versions?.node) {
16
+ const { readFileSync } = await import(/* webpackIgnore: true */ 'fs');
17
+ const { join, dirname } = await import(/* webpackIgnore: true */ 'path');
18
+ const { fileURLToPath } = await import(/* webpackIgnore: true */ 'url');
19
+ const __dir = dirname(fileURLToPath(import.meta.url));
20
+ wasmBinary = readFileSync(join(__dir, 'wasm/rule_engine.wasm'));
21
+ } else {
22
+ const res = await fetch(_wasmUrl);
23
+ if (!res.ok) throw new Error(`[PAY.ID] Failed to fetch WASM from ${_wasmUrl}: ${res.status}`);
24
+ wasmBinary = new Uint8Array(await res.arrayBuffer());
25
+ }
10
26
  }
11
27
 
12
28
  const module = await WebAssembly.compile(wasmBinary);