payid-rule-engine 0.1.8 → 0.2.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payid-rule-engine",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
package/src/sandbox.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { WASI } from "wasi";
1
+ // sandbox.ts WASI-free version
2
+ // Lihat wasm.ts untuk penjelasan mengapa WASI dihapus
3
+
2
4
  import type { RuleContext, RuleResult } from "payid-types";
3
5
  import { loadWasm } from "./wasm";
4
6
 
@@ -7,12 +9,12 @@ export async function runWasmRule(
7
9
  context: RuleContext,
8
10
  config: any
9
11
  ): Promise<RuleResult> {
10
- const wasi = new WASI({ version: "preview1" });
11
- const instance = await loadWasm(wasmBinary, wasi);
12
+ // loadWasm tidak lagi butuh WASI instance
13
+ const instance = await loadWasm(wasmBinary);
12
14
 
13
15
  const memory = instance.exports.memory as WebAssembly.Memory;
14
16
  const alloc = instance.exports.alloc as (size: number) => number;
15
- const free = instance.exports.free as (ptr: number, size: number) => void;
17
+ const free_ = instance.exports.free as (ptr: number, size: number) => void;
16
18
  const evaluate = instance.exports.evaluate as (
17
19
  a: number, b: number, c: number, d: number, e: number, f: number
18
20
  ) => number;
@@ -35,7 +37,7 @@ export async function runWasmRule(
35
37
  outPtr, OUT_SIZE
36
38
  );
37
39
 
38
- if (rc < 0) throw new Error(`WASM failed rc=${rc}`);
40
+ if (rc < 0) throw new Error(`WASM evaluate failed rc=${rc}`);
39
41
 
40
42
  const out = Buffer.from(
41
43
  new Uint8Array(memory.buffer).slice(outPtr, outPtr + rc)
@@ -43,8 +45,8 @@ export async function runWasmRule(
43
45
 
44
46
  return JSON.parse(out.toString("utf8"));
45
47
  } finally {
46
- free(ctxPtr, ctxBuf.length);
47
- free(cfgPtr, cfgBuf.length);
48
- free(outPtr, OUT_SIZE);
48
+ free_(ctxPtr, ctxBuf.length);
49
+ free_(cfgPtr, cfgBuf.length);
50
+ free_(outPtr, OUT_SIZE);
49
51
  }
50
- }
52
+ }
package/src/wasm.ts CHANGED
@@ -1,19 +1,41 @@
1
- import { WASI } from "wasi";
1
+ // wasm.ts WASI-free loader
2
+ //
3
+ // WASM rule engine v4 tidak butuh WASI sama sekali (tidak ada file I/O,
4
+ // tidak ada stdout/stderr, tidak ada proc_exit). Bun punya known issues
5
+ // dengan WASI preview1 yang bisa menyebabkan hang.
6
+ //
7
+ // Solusi: pass minimal stub yang satisfy wasi_snapshot_preview1 interface,
8
+ // cukup untuk Rust allocator init tanpa dependency ke WASI runtime.
2
9
 
3
10
  export async function loadWasm(
4
11
  binary: Buffer,
5
- wasi: WASI
6
12
  ): Promise<WebAssembly.Instance> {
13
+
7
14
  const module = await WebAssembly.compile(binary);
8
15
 
16
+ const wasiStub: Record<string, (...args: any[]) => any> = {
17
+ fd_write: () => 8, // EBADF
18
+ fd_read: () => 8,
19
+ fd_close: () => 0,
20
+ fd_seek: () => 8,
21
+ fd_fdstat_get: () => 8,
22
+ fd_prestat_get: () => 8,
23
+ fd_prestat_dir_name: () => 8,
24
+ environ_get: () => 0,
25
+ environ_sizes_get: () => 0,
26
+ args_get: () => 0,
27
+ args_sizes_get: () => 0,
28
+ clock_time_get: () => 0,
29
+ proc_exit: () => { },
30
+ random_get: () => 0,
31
+ };
32
+
9
33
  const instance = await WebAssembly.instantiate(module, {
10
- wasi_snapshot_preview1: wasi.wasiImport
34
+ wasi_snapshot_preview1: wasiStub
11
35
  });
12
36
 
13
- // rule_engine.wasm adalah reactor module (exports alloc/free/evaluate),
14
- // bukan command module. start() memanggil _start dan hang selamanya.
15
- // initialize() adalah yang benar untuk reactor/library WASM.
16
- wasi.initialize(instance);
37
+ const _init = instance.exports._initialize as (() => void) | undefined;
38
+ if (_init) _init();
17
39
 
18
40
  return instance;
19
41
  }