@absolutejs/sync 1.7.0 → 1.7.2
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/dist/engine/index.js +72 -23
- package/dist/engine/index.js.map +3 -3
- package/dist/engine/sandbox.d.ts +26 -25
- package/dist/index.js +72 -23
- package/dist/index.js.map +3 -3
- package/package.json +3 -3
package/dist/engine/sandbox.d.ts
CHANGED
|
@@ -21,18 +21,16 @@
|
|
|
21
21
|
* For long-lived mutations choose `memoryLimit` ≥ 128 (the default 32
|
|
22
22
|
* trips after a few dozen calls without pressure for GC).
|
|
23
23
|
*
|
|
24
|
-
* **Backend default: `'
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* via `sandbox: { backend: 'ffi' }` — they get the ~300 KB cold heap and
|
|
35
|
-
* interrupt-driven timeouts. Document this clearly when you do.
|
|
24
|
+
* **Backend default: `'auto'`** — isolated-jsc 0.4 added an async host-fn
|
|
25
|
+
* pump on the FFI backend (alternates Bun event-loop yields with JSC
|
|
26
|
+
* microtask drains, bounded by `Script.run`'s `timeout`), so the
|
|
27
|
+
* `actions.insert/update/delete/change` async References settle on FFI
|
|
28
|
+
* just like they do on Worker. `'auto'` picks FFI when libJSC is reachable
|
|
29
|
+
* (~300 KB cold heap, interrupt-driven CPU timeouts) and falls back to
|
|
30
|
+
* Worker (~46 MB cold heap, postMessage round-trips) otherwise. Pin to
|
|
31
|
+
* `'worker'` if you specifically need Web APIs (`URL`, `TextEncoder`,
|
|
32
|
+
* `WebSocket`) inside your handler — those live in the Bun-Worker
|
|
33
|
+
* environment, not the bare JSC C API.
|
|
36
34
|
*
|
|
37
35
|
* The runner is built lazily per-mutation: nothing is spawned until the
|
|
38
36
|
* mutation actually runs for the first time. No engine teardown hook is
|
|
@@ -46,25 +44,28 @@ export type SandboxConfig = {
|
|
|
46
44
|
/** Wall-clock cap per call (ms). Default 5000. */
|
|
47
45
|
timeout?: number;
|
|
48
46
|
/**
|
|
49
|
-
* isolated-jsc backend. Defaults to `'
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* supports both sync and async host fns.
|
|
47
|
+
* isolated-jsc backend. Defaults to `'auto'` (FFI when libJSC is
|
|
48
|
+
* reachable, Worker otherwise) since isolated-jsc 0.4 added async
|
|
49
|
+
* host-fn support on FFI — `actions.insert/update/delete/change`
|
|
50
|
+
* now settle on both backends.
|
|
54
51
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* (~300 KB vs ~46 MB) + interrupt-driven timeout benefits.
|
|
52
|
+
* Pin to `'worker'` if your handler needs Web APIs (`URL`,
|
|
53
|
+
* `TextEncoder`, `WebSocket`) — those live in the Bun-Worker
|
|
54
|
+
* environment, not the bare JSC C API.
|
|
59
55
|
*
|
|
60
|
-
* `'
|
|
61
|
-
*
|
|
56
|
+
* Pin to `'ffi'` for hot-path read-only handlers (~300 KB cold heap
|
|
57
|
+
* vs ~46 MB on Worker, interrupt-driven CPU timeouts).
|
|
62
58
|
*/
|
|
63
59
|
backend?: 'auto' | 'ffi' | 'worker';
|
|
64
60
|
};
|
|
65
61
|
/**
|
|
66
62
|
* Build a lazy runner for one mutation's sandboxed source. The first call
|
|
67
|
-
* compiles + spawns; subsequent calls reuse the isolate
|
|
68
|
-
*
|
|
63
|
+
* compiles + spawns; subsequent calls reuse the isolate AND the context
|
|
64
|
+
* (the router Reference is installed once on isolate creation; per-call
|
|
65
|
+
* cost is just two `setGlobal`s for `args` + `ctx`). Calls are serialised
|
|
66
|
+
* via a promise queue so the shared-slot router stays coherent. The
|
|
67
|
+
* context is recycled every {@link DEFAULT_RECYCLE_CONTEXT_AFTER} calls
|
|
68
|
+
* to bound JSC per-call metadata accumulation. If the isolate has been
|
|
69
|
+
* disposed (timeout, memory cap), the next call re-spawns transparently.
|
|
69
70
|
*/
|
|
70
71
|
export declare const makeSandboxedHandler: (source: string, config?: SandboxConfig) => ((args: unknown, ctx: unknown, actions: MutationActions) => Promise<unknown>);
|
package/dist/index.js
CHANGED
|
@@ -734,22 +734,57 @@ var wrap = (source) => `
|
|
|
734
734
|
);
|
|
735
735
|
}
|
|
736
736
|
const actions = {
|
|
737
|
-
insert:
|
|
738
|
-
update:
|
|
739
|
-
delete:
|
|
740
|
-
change:
|
|
737
|
+
insert: (table, data) => __syncAction('insert', table, data),
|
|
738
|
+
update: (table, data) => __syncAction('update', table, data),
|
|
739
|
+
delete: (table, row) => __syncAction('delete', table, row),
|
|
740
|
+
change: (collection, change) => __syncAction('change', collection, change)
|
|
741
741
|
};
|
|
742
742
|
return await userFn(args, ctx, actions);
|
|
743
743
|
})()
|
|
744
744
|
`;
|
|
745
|
+
var DEFAULT_RECYCLE_CONTEXT_AFTER = 256;
|
|
746
|
+
var installRouter = async (context, currentActions, Reference) => {
|
|
747
|
+
const router = new Reference((op, ...rest) => {
|
|
748
|
+
const a = currentActions.value;
|
|
749
|
+
if (a === undefined) {
|
|
750
|
+
throw new Error("__syncAction invoked outside an active sandboxed call (shared-slot router)");
|
|
751
|
+
}
|
|
752
|
+
switch (op) {
|
|
753
|
+
case "insert":
|
|
754
|
+
return a.insert(rest[0], rest[1]);
|
|
755
|
+
case "update":
|
|
756
|
+
return a.update(rest[0], rest[1]);
|
|
757
|
+
case "delete":
|
|
758
|
+
return a.delete(rest[0], rest[1]);
|
|
759
|
+
case "change":
|
|
760
|
+
return a.change(rest[0], rest[1]);
|
|
761
|
+
default:
|
|
762
|
+
throw new Error(`unknown sandbox action op: ${String(op)}`);
|
|
763
|
+
}
|
|
764
|
+
});
|
|
765
|
+
await context.setGlobal("__syncAction", router);
|
|
766
|
+
};
|
|
745
767
|
var compile = async (source, config) => {
|
|
746
|
-
const { createIsolate } = await loadIsolatedJsc();
|
|
768
|
+
const { createIsolate, Reference } = await loadIsolatedJsc();
|
|
747
769
|
const isolate = await createIsolate({
|
|
748
|
-
backend: config.backend ?? "
|
|
770
|
+
backend: config.backend ?? "auto",
|
|
749
771
|
memoryLimit: config.memoryLimit ?? 32
|
|
750
772
|
});
|
|
751
773
|
const script = await isolate.compileScript(wrap(source));
|
|
752
|
-
|
|
774
|
+
const context = await isolate.createContext();
|
|
775
|
+
const currentActions = {
|
|
776
|
+
value: undefined
|
|
777
|
+
};
|
|
778
|
+
await installRouter(context, currentActions, Reference);
|
|
779
|
+
return {
|
|
780
|
+
context,
|
|
781
|
+
currentActions,
|
|
782
|
+
isolate,
|
|
783
|
+
runQueue: Promise.resolve(undefined),
|
|
784
|
+
script,
|
|
785
|
+
servedCalls: 0,
|
|
786
|
+
timeoutMs: config.timeout ?? 5000
|
|
787
|
+
};
|
|
753
788
|
};
|
|
754
789
|
var makeSandboxedHandler = (source, config = {}) => {
|
|
755
790
|
let pending;
|
|
@@ -763,23 +798,37 @@ var makeSandboxedHandler = (source, config = {}) => {
|
|
|
763
798
|
pending = compile(source, config);
|
|
764
799
|
return pending;
|
|
765
800
|
};
|
|
766
|
-
|
|
801
|
+
const recycleContextIfNeeded = async (compiled) => {
|
|
802
|
+
if (compiled.servedCalls < DEFAULT_RECYCLE_CONTEXT_AFTER)
|
|
803
|
+
return;
|
|
767
804
|
const { Reference } = await loadIsolatedJsc();
|
|
805
|
+
await compiled.context.dispose().catch(() => {});
|
|
806
|
+
compiled.context = await compiled.isolate.createContext();
|
|
807
|
+
await installRouter(compiled.context, compiled.currentActions, Reference);
|
|
808
|
+
compiled.servedCalls = 0;
|
|
809
|
+
};
|
|
810
|
+
return async (args, ctx, actions) => {
|
|
768
811
|
const compiled = await getCompiled();
|
|
769
|
-
const
|
|
770
|
-
|
|
771
|
-
await
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
812
|
+
const prev = compiled.runQueue;
|
|
813
|
+
const turn = prev.then(async () => {
|
|
814
|
+
await recycleContextIfNeeded(compiled);
|
|
815
|
+
compiled.currentActions.value = actions;
|
|
816
|
+
try {
|
|
817
|
+
await compiled.context.setGlobal("args", args);
|
|
818
|
+
await compiled.context.setGlobal("ctx", ctx);
|
|
819
|
+
const result = await compiled.script.run(compiled.context, {
|
|
820
|
+
timeout: compiled.timeoutMs
|
|
821
|
+
});
|
|
822
|
+
return result;
|
|
823
|
+
} finally {
|
|
824
|
+
compiled.currentActions.value = undefined;
|
|
825
|
+
compiled.servedCalls += 1;
|
|
826
|
+
}
|
|
827
|
+
});
|
|
828
|
+
compiled.runQueue = turn.catch(() => {
|
|
829
|
+
return;
|
|
830
|
+
});
|
|
831
|
+
return turn;
|
|
783
832
|
};
|
|
784
833
|
};
|
|
785
834
|
|
|
@@ -2301,5 +2350,5 @@ export {
|
|
|
2301
2350
|
createPresenceHub
|
|
2302
2351
|
};
|
|
2303
2352
|
|
|
2304
|
-
//# debugId=
|
|
2353
|
+
//# debugId=3D61A1E35B6A5BE864756E2164756E21
|
|
2305
2354
|
//# sourceMappingURL=index.js.map
|