payid-rule-engine 0.2.6 → 0.2.7
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/sandbox.ts +9 -9
package/package.json
CHANGED
package/src/sandbox.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { RuleContext, RuleResult } from "payid-types";
|
|
2
2
|
import { loadWasm } from "./wasm";
|
|
3
|
+
|
|
4
|
+
const enc = new TextEncoder();
|
|
5
|
+
const dec = new TextDecoder();
|
|
6
|
+
|
|
3
7
|
export async function runWasmRule(
|
|
4
8
|
context: RuleContext,
|
|
5
9
|
config: any,
|
|
6
|
-
wasmBinary?: Buffer,
|
|
10
|
+
wasmBinary?: Buffer | Uint8Array,
|
|
7
11
|
): Promise<RuleResult> {
|
|
8
12
|
const instance = await loadWasm(wasmBinary);
|
|
9
13
|
|
|
@@ -18,8 +22,8 @@ export async function runWasmRule(
|
|
|
18
22
|
throw new Error(`WASM missing exports: alloc=${!!alloc} evaluate=${!!evaluate}`);
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
const ctxBuf =
|
|
22
|
-
const cfgBuf =
|
|
25
|
+
const ctxBuf = enc.encode(JSON.stringify(context));
|
|
26
|
+
const cfgBuf = enc.encode(JSON.stringify(config));
|
|
23
27
|
const OUT_SIZE = 4096;
|
|
24
28
|
|
|
25
29
|
const ctxPtr = alloc(ctxBuf.length);
|
|
@@ -38,13 +42,9 @@ export async function runWasmRule(
|
|
|
38
42
|
|
|
39
43
|
if (rc < 0) throw new Error(`WASM evaluate failed rc=${rc}`);
|
|
40
44
|
|
|
41
|
-
const out =
|
|
42
|
-
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
const result = JSON.parse(out.toString("utf8"));
|
|
45
|
+
const out = new Uint8Array(memory.buffer).slice(outPtr, outPtr + rc);
|
|
46
|
+
const result = JSON.parse(dec.decode(out));
|
|
46
47
|
|
|
47
|
-
// free hanya kalau ada — beberapa build tidak export free
|
|
48
48
|
if (free_) {
|
|
49
49
|
free_(ctxPtr, ctxBuf.length);
|
|
50
50
|
free_(cfgPtr, cfgBuf.length);
|