@syntropika/tembra-sdk 0.2.2 → 0.3.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/README.md +43 -17
- package/dist/authoring/types.d.ts +64 -3
- package/dist/authoring/wit.d.ts +6 -0
- package/dist/bridge/commands.d.ts +1190 -0
- package/dist/bridge/identifiers.d.ts +2 -0
- package/dist/bridge/kernel.d.ts +18 -107
- package/dist/bridge/protocol.d.ts +3 -138
- package/dist/browser.d.ts +14 -1
- package/dist/browser.js +141 -3332
- package/dist/chunks/browser-b8jamxw2.js +3399 -0
- package/dist/chunks/execution-nsqc6nrt.js +1419 -0
- package/dist/chunks/index-01zk0xqx.js +160 -0
- package/dist/chunks/index-3fjdfdfd.js +152 -0
- package/dist/chunks/index-aaymjn7v.js +787 -0
- package/dist/chunks/index-c1w7z9hx.js +356 -0
- package/dist/chunks/index-h8t0n42q.js +738 -0
- package/dist/chunks/index-hfds9mzp.js +308 -0
- package/dist/chunks/index-kr4x67wt.js +177 -0
- package/dist/chunks/index-kr6bnh34.js +117 -0
- package/dist/chunks/index-kyt9dj0a.js +1421 -0
- package/dist/chunks/index-n1wxzc9w.js +830 -0
- package/dist/chunks/index-pvac426d.js +74 -0
- package/dist/chunks/index-smyds5ce.js +3234 -0
- package/dist/chunks/index-sse8aapb.js +121 -0
- package/dist/chunks/index-v6heph77.js +643 -0
- package/dist/chunks/index-xrdvnv37.js +3448 -0
- package/dist/chunks/index-za4mg94e.js +26 -0
- package/dist/chunks/index-zkqmnjj8.js +167 -0
- package/dist/coordinator.js +9 -500
- package/dist/error.d.ts +2 -0
- package/dist/errors.d.ts +3 -0
- package/dist/guest.d.ts +173 -0
- package/dist/guest.js +26 -0
- package/dist/host.d.ts +36 -30
- package/dist/host.js +37 -3310
- package/dist/http/bytes.d.ts +8 -0
- package/dist/http/events.d.ts +7 -0
- package/dist/http.d.ts +45 -0
- package/dist/http.js +291 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +49 -862
- package/dist/infrastructure/rpc/client.d.ts +2 -1
- package/dist/infrastructure/rpc/resources.d.ts +28 -0
- package/dist/interpreter/abi.d.ts +54 -0
- package/dist/interpreter/acquire.d.ts +18 -0
- package/dist/interpreter/browser-network.d.ts +52 -0
- package/dist/interpreter/browser-port.d.ts +55 -0
- package/dist/interpreter/browser.d.ts +79 -0
- package/dist/interpreter/cli.d.ts +5 -0
- package/dist/interpreter/codec.d.ts +42 -0
- package/dist/interpreter/component/execution.d.ts +42 -0
- package/dist/interpreter/component/host.d.ts +69 -0
- package/dist/interpreter/component/instance.d.ts +40 -0
- package/dist/interpreter/component/kernel.d.ts +120 -0
- package/dist/interpreter/component/names.d.ts +5 -0
- package/dist/interpreter/component/port.d.ts +85 -0
- package/dist/interpreter/component/prepared.d.ts +97 -0
- package/dist/interpreter/component/resources.d.ts +45 -0
- package/dist/interpreter/component/revision.d.ts +87 -0
- package/dist/interpreter/component/scope.d.ts +116 -0
- package/dist/interpreter/component/streams.d.ts +28 -0
- package/dist/interpreter/component/values.d.ts +79 -0
- package/dist/interpreter/component/wasi-filesystem.d.ts +21 -0
- package/dist/interpreter/component/wasi-host.d.ts +64 -0
- package/dist/interpreter/component/wasi-http.d.ts +33 -0
- package/dist/interpreter/component/wasi-sockets.d.ts +183 -0
- package/dist/interpreter/component/wasi.d.ts +41 -0
- package/dist/interpreter/core.d.ts +64 -0
- package/dist/interpreter/fetch-http-provider.d.ts +17 -0
- package/dist/interpreter/filesystem/contract.d.ts +85 -0
- package/dist/interpreter/filesystem/memory.d.ts +3 -0
- package/dist/interpreter/filesystem/opfs.d.ts +3 -0
- package/dist/interpreter/filesystem/scope.d.ts +35 -0
- package/dist/interpreter/filesystem.d.ts +8 -0
- package/dist/interpreter/gate.d.ts +6 -0
- package/dist/interpreter/host-protocol.d.ts +100 -0
- package/dist/interpreter/http-ingress-fetch.d.ts +7 -0
- package/dist/interpreter/http-ingress.d.ts +96 -0
- package/dist/interpreter/http-provider.d.ts +85 -0
- package/dist/interpreter/io.d.ts +20 -0
- package/dist/interpreter/mobile.d.ts +149 -0
- package/dist/interpreter/native-ingress.d.ts +95 -0
- package/dist/interpreter/node-port.d.ts +25 -0
- package/dist/interpreter/node.d.ts +36 -0
- package/dist/interpreter/revision.d.ts +64 -0
- package/dist/interpreter/scope.d.ts +89 -0
- package/dist/interpreter/socket-addresses.d.ts +50 -0
- package/dist/interpreter/socket-provider.d.ts +110 -0
- package/dist/interpreter/standard-cli.d.ts +34 -0
- package/dist/interpreter/standard-codec.d.ts +96 -0
- package/dist/interpreter/standard-filesystem.d.ts +47 -0
- package/dist/interpreter/standard-host.d.ts +71 -0
- package/dist/interpreter/standard-http-values.d.ts +159 -0
- package/dist/interpreter/standard-http.d.ts +9 -0
- package/dist/interpreter/standard-ingress.d.ts +63 -0
- package/dist/interpreter/standard-sockets.d.ts +177 -0
- package/dist/interpreter/standard-streams.d.ts +218 -0
- package/dist/interpreter/standard-system.d.ts +35 -0
- package/dist/interpreter/standard-utf8.d.ts +4 -0
- package/dist/interpreter/standard.d.ts +18 -0
- package/dist/interpreter/types.d.ts +152 -0
- package/dist/interpreter/wit-shape.d.ts +92 -0
- package/dist/manager.js +5 -332
- package/dist/msgpack.js +9 -615
- package/dist/node.d.ts +8 -1
- package/dist/node.js +248 -3321
- package/dist/rpc.js +196 -699
- package/dist/runtime/adapters.d.ts +120 -0
- package/dist/runtime/adapters.js +38 -0
- package/dist/runtime/calls.d.ts +7 -6
- package/dist/runtime/lifecycle.d.ts +3 -1
- package/dist/runtime/orbs.d.ts +5 -4
- package/dist/runtime/references.d.ts +27 -1
- package/dist/runtime/reload.d.ts +5 -3
- package/dist/runtime/scopes.d.ts +1 -1
- package/dist/runtime/signals.d.ts +4 -0
- package/dist/runtime/wit-client.d.ts +38 -0
- package/dist/telemetry.js +4 -25
- package/dist/tooling/build.d.ts +2 -2
- package/dist/tooling/client-resources.d.ts +23 -0
- package/dist/tooling/client.d.ts +10 -0
- package/dist/tooling/component-glue.d.ts +17 -0
- package/dist/tooling/component.d.ts +11 -0
- package/dist/tooling/index.d.ts +3 -1
- package/dist/tooling/index.js +1155 -102
- package/dist/tooling/resolver.d.ts +4 -4
- package/dist/tooling/typecheck.d.ts +2 -1
- package/dist/tooling/types.d.ts +27 -12
- package/dist/tooling/wit.d.ts +9 -0
- package/dist/wasi/wasi-clocks-types.d.ts +2 -0
- package/dist/wasi/wasi-http-client.d.ts +5 -0
- package/dist/wasi/wasi-http-types.d.ts +285 -0
- package/dist/wasi/wasi-sockets-ip-name-lookup.d.ts +23 -0
- package/dist/wasi/wasi-sockets-types.d.ts +146 -0
- package/package.json +62 -35
- package/src/authoring/declaration.ts +1 -1
- package/src/authoring/types.ts +53 -3
- package/src/authoring/wit.ts +15 -0
- package/src/bridge/commands.ts +1105 -0
- package/src/bridge/identifiers.ts +2 -0
- package/src/bridge/kernel.ts +56 -98
- package/src/bridge/protocol.ts +3 -142
- package/src/browser.ts +90 -6
- package/src/context-key.ts +4 -7
- package/src/error.ts +1 -1
- package/src/errors.ts +7 -0
- package/src/guest.ts +327 -0
- package/src/host.ts +128 -23
- package/src/http/bytes.ts +87 -0
- package/src/http/events.ts +34 -0
- package/src/http.ts +272 -0
- package/src/index.ts +3 -0
- package/src/infrastructure/rpc/client.ts +136 -17
- package/src/infrastructure/rpc/resources.ts +123 -0
- package/src/interpreter/abi.ts +69 -0
- package/src/interpreter/acquire.ts +58 -0
- package/src/interpreter/browser-network.ts +170 -0
- package/src/interpreter/browser-port.ts +425 -0
- package/src/interpreter/browser.ts +340 -0
- package/src/interpreter/cli.ts +30 -0
- package/src/interpreter/codec.ts +298 -0
- package/src/interpreter/component/execution.ts +264 -0
- package/src/interpreter/component/host.ts +409 -0
- package/src/interpreter/component/instance.ts +138 -0
- package/src/interpreter/component/kernel.ts +611 -0
- package/src/interpreter/component/names.ts +13 -0
- package/src/interpreter/component/port.ts +598 -0
- package/src/interpreter/component/prepared.ts +194 -0
- package/src/interpreter/component/resources.ts +345 -0
- package/src/interpreter/component/revision.ts +702 -0
- package/src/interpreter/component/scope.ts +447 -0
- package/src/interpreter/component/streams.ts +180 -0
- package/src/interpreter/component/values.ts +254 -0
- package/src/interpreter/component/wasi-filesystem.ts +472 -0
- package/src/interpreter/component/wasi-host.ts +197 -0
- package/src/interpreter/component/wasi-http.ts +939 -0
- package/src/interpreter/component/wasi-sockets.ts +958 -0
- package/src/interpreter/component/wasi.ts +238 -0
- package/src/interpreter/core.ts +386 -0
- package/src/interpreter/fetch-http-provider.ts +98 -0
- package/src/interpreter/filesystem/contract.ts +138 -0
- package/src/interpreter/filesystem/memory.ts +180 -0
- package/src/interpreter/filesystem/opfs.ts +294 -0
- package/src/interpreter/filesystem/scope.ts +390 -0
- package/src/interpreter/filesystem.ts +20 -0
- package/src/interpreter/gate.ts +55 -0
- package/src/interpreter/host-protocol.ts +242 -0
- package/src/interpreter/http-ingress-fetch.ts +133 -0
- package/src/interpreter/http-ingress.ts +203 -0
- package/src/interpreter/http-provider.ts +105 -0
- package/src/interpreter/io.ts +66 -0
- package/src/interpreter/mobile.ts +385 -0
- package/src/interpreter/native-ingress.ts +550 -0
- package/src/interpreter/node-port.ts +218 -0
- package/src/interpreter/node.ts +114 -0
- package/src/interpreter/revision.ts +450 -0
- package/src/interpreter/scope.ts +453 -0
- package/src/interpreter/socket-addresses.ts +246 -0
- package/src/interpreter/socket-provider.ts +126 -0
- package/src/interpreter/standard-cli.ts +184 -0
- package/src/interpreter/standard-codec.ts +403 -0
- package/src/interpreter/standard-filesystem.ts +545 -0
- package/src/interpreter/standard-host.ts +840 -0
- package/src/interpreter/standard-http-values.ts +624 -0
- package/src/interpreter/standard-http.ts +622 -0
- package/src/interpreter/standard-ingress.ts +344 -0
- package/src/interpreter/standard-sockets.ts +847 -0
- package/src/interpreter/standard-streams.ts +710 -0
- package/src/interpreter/standard-system.ts +182 -0
- package/src/interpreter/standard-utf8.ts +72 -0
- package/src/interpreter/standard.ts +283 -0
- package/src/interpreter/types.ts +163 -0
- package/src/interpreter/wit-shape.ts +175 -0
- package/src/node.ts +43 -14
- package/src/runtime/activities.ts +6 -6
- package/src/runtime/adapters.ts +314 -0
- package/src/runtime/calls.ts +213 -90
- package/src/runtime/context.ts +7 -7
- package/src/runtime/lifecycle.ts +36 -17
- package/src/runtime/orbs.ts +81 -39
- package/src/runtime/programs.ts +16 -8
- package/src/runtime/references.ts +128 -11
- package/src/runtime/reload.ts +71 -30
- package/src/runtime/resources.ts +2 -2
- package/src/runtime/scopes.ts +17 -17
- package/src/runtime/session.ts +3 -1
- package/src/runtime/signals.ts +31 -0
- package/src/runtime/wit-client.ts +228 -0
- package/src/tooling/build.ts +152 -54
- package/src/tooling/client-resources.ts +74 -0
- package/src/tooling/client.ts +531 -0
- package/src/tooling/component-glue.ts +288 -0
- package/src/tooling/component.ts +225 -0
- package/src/tooling/index.ts +3 -7
- package/src/tooling/resolver.ts +42 -25
- package/src/tooling/typecheck.ts +59 -4
- package/src/tooling/types.ts +27 -12
- package/src/tooling/wit.ts +188 -0
- package/dist/embedded.d.ts +0 -1
- package/dist/embedded.js +0 -1
- package/src/embedded.ts +0 -301
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import { reply } from "./codec.ts";
|
|
2
|
+
import type { Completion, Guest, Outcome } from "./core.ts";
|
|
3
|
+
import { GuestTrap } from "./core.ts";
|
|
4
|
+
import { decodeAsyncHostRequest, decodeHostRequest, type HostRequest } from "./host-protocol.ts";
|
|
5
|
+
import { combinedSignal } from "../runtime/signals.ts";
|
|
6
|
+
import type { StandardHost } from "./standard-host.ts";
|
|
7
|
+
import { DROP } from "./standard-codec.ts";
|
|
8
|
+
import type { ExecutionScope, IngressDispatch, InterpreterPort } from "./types.ts";
|
|
9
|
+
import type { IngressExchange } from "./http-ingress.ts";
|
|
10
|
+
|
|
11
|
+
/** The guest's question about the refusal of the synchronous call that just failed. */
|
|
12
|
+
const REFUSAL = '{"operation":"refusal"}';
|
|
13
|
+
/** The prefix of the guest's naming of the refusal its callback fails with. */
|
|
14
|
+
const REFUSED = '{"operation":"refused",';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The host's refusals one callback's guest received, by the identity the guest names them by: the
|
|
18
|
+
* effect that delivered one, or the number this port gave a synchronous one, which the guest asks
|
|
19
|
+
* for at once. A callback that fails with one unchanged answers it, as the native executor does;
|
|
20
|
+
* the guest can name only a refusal of the same callback.
|
|
21
|
+
*/
|
|
22
|
+
class Refusals {
|
|
23
|
+
/** A guest rethrows a refusal it has just received, so only the most recent ones are kept. */
|
|
24
|
+
static readonly KEPT = 64;
|
|
25
|
+
readonly #requests = new Map<number, unknown>();
|
|
26
|
+
readonly #effects = new Map<number, unknown>();
|
|
27
|
+
#sequence = 0;
|
|
28
|
+
/** The synchronous request that failed last, if the host refused it. */
|
|
29
|
+
#last: number | undefined;
|
|
30
|
+
/** The refusal the guest named as its failure. */
|
|
31
|
+
named: { error: unknown } | undefined;
|
|
32
|
+
|
|
33
|
+
/** Keep one refusal, forgetting the oldest beyond the bound. */
|
|
34
|
+
static #keep(refusals: Map<number, unknown>, id: number, error: unknown): void {
|
|
35
|
+
refusals.set(id, error);
|
|
36
|
+
if (refusals.size > Refusals.KEPT) refusals.delete(refusals.keys().next().value!);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The host refused a synchronous request. */
|
|
40
|
+
sync(error: unknown): void {
|
|
41
|
+
const id = ++this.#sequence;
|
|
42
|
+
Refusals.#keep(this.#requests, id, error);
|
|
43
|
+
this.#last = id;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Another synchronous request was made. */
|
|
47
|
+
forget(): void {
|
|
48
|
+
this.#last = undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The host refused an effect's request. */
|
|
52
|
+
effect(id: number, error: unknown): void {
|
|
53
|
+
Refusals.#keep(this.#effects, id, error);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Answer the guest's question about the synchronous request that just failed. */
|
|
57
|
+
question(): string {
|
|
58
|
+
const last = this.#last;
|
|
59
|
+
this.#last = undefined;
|
|
60
|
+
return last === undefined ? "null" : String(last);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Keep the refusal the guest names, if it is one of this callback's. */
|
|
64
|
+
name(packet: string): void {
|
|
65
|
+
let named: { effect?: unknown; request?: unknown } = {};
|
|
66
|
+
try {
|
|
67
|
+
named = JSON.parse(packet) as typeof named;
|
|
68
|
+
} catch {
|
|
69
|
+
// A malformed naming names nothing.
|
|
70
|
+
}
|
|
71
|
+
const [refusals, id] =
|
|
72
|
+
typeof named.effect === "number" && named.request === undefined
|
|
73
|
+
? [this.#effects, named.effect]
|
|
74
|
+
: typeof named.request === "number" && named.effect === undefined
|
|
75
|
+
? [this.#requests, named.request]
|
|
76
|
+
: [undefined, undefined];
|
|
77
|
+
this.named =
|
|
78
|
+
refusals && id !== undefined && refusals.has(id) ? { error: refusals.get(id) } : undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Internal delivery journal; core ABI completion encoding ignores these host-owned identities. */
|
|
83
|
+
interface Delivery extends Completion {
|
|
84
|
+
readonly issued: readonly number[] | undefined;
|
|
85
|
+
readonly reads: readonly number[] | undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A serialized Wasm heap yields data; host promises never enter its execution stack. */
|
|
89
|
+
export class BrowserPort implements InterpreterPort {
|
|
90
|
+
guest: Guest | undefined;
|
|
91
|
+
/** Revision-owned standard WASI host; absent until installation opens it. */
|
|
92
|
+
standard: StandardHost | undefined;
|
|
93
|
+
#description = "";
|
|
94
|
+
#scope: ExecutionScope | undefined;
|
|
95
|
+
#running = false;
|
|
96
|
+
#closed = false;
|
|
97
|
+
#poisoned = false;
|
|
98
|
+
/** The host's refusals of the active callback. */
|
|
99
|
+
#refusals = new Refusals();
|
|
100
|
+
/** Retain per-installation I/O and logging independently of callback authority. */
|
|
101
|
+
constructor(
|
|
102
|
+
readonly timeout: number,
|
|
103
|
+
readonly dispose: () => Promise<void>,
|
|
104
|
+
readonly log: (record: Extract<HostRequest, { operation: "console" }>) => void,
|
|
105
|
+
readonly packetBytes = 8 << 20,
|
|
106
|
+
) {}
|
|
107
|
+
|
|
108
|
+
/** UTF-8 uses at most three bytes per UTF-16 code unit; only large packets need encoding. */
|
|
109
|
+
private bounded(packet: string): string {
|
|
110
|
+
if (
|
|
111
|
+
packet.length > this.packetBytes ||
|
|
112
|
+
(packet.length > Math.floor(this.packetBytes / 3) &&
|
|
113
|
+
new TextEncoder().encode(packet).byteLength > this.packetBytes)
|
|
114
|
+
)
|
|
115
|
+
throw new Error("Interpreter packet exceeds its configured bound");
|
|
116
|
+
return packet;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Bound a binary packet or reply by its exact length. */
|
|
120
|
+
private boundedBytes(packet: Uint8Array): Uint8Array {
|
|
121
|
+
if (packet.byteLength > this.packetBytes)
|
|
122
|
+
throw new Error("Interpreter packet exceeds its configured bound");
|
|
123
|
+
return packet;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Decode once and invoke the current host-owned scope without an event envelope. */
|
|
127
|
+
sync(packet: string): string;
|
|
128
|
+
sync(packet: Uint8Array): Uint8Array;
|
|
129
|
+
sync(packet: string | Uint8Array): string | Uint8Array {
|
|
130
|
+
if (packet instanceof Uint8Array) {
|
|
131
|
+
this.#refusals.forget();
|
|
132
|
+
// Standard calls exist only inside a callback; the revision's host owns their handles.
|
|
133
|
+
if (!this.#scope || !this.standard) throw "No active host scope";
|
|
134
|
+
try {
|
|
135
|
+
if (packet[1] !== DROP) this.#scope.check();
|
|
136
|
+
return this.boundedBytes(this.standard.sync(this.boundedBytes(packet)));
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (error instanceof GuestTrap) throw error;
|
|
139
|
+
throw error instanceof Error ? error.message : String(error);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// The guest's question about a refusal, and its naming of the one it fails with.
|
|
143
|
+
if (packet === REFUSAL) return this.#refusals.question();
|
|
144
|
+
if (packet.startsWith(REFUSED)) {
|
|
145
|
+
this.#refusals.name(packet);
|
|
146
|
+
return "null";
|
|
147
|
+
}
|
|
148
|
+
this.#refusals.forget();
|
|
149
|
+
try {
|
|
150
|
+
const request = decodeHostRequest(this.bounded(packet));
|
|
151
|
+
if (request.operation === "console") {
|
|
152
|
+
this.log(request);
|
|
153
|
+
return "null";
|
|
154
|
+
}
|
|
155
|
+
if (!this.#scope) throw new Error("No active host scope");
|
|
156
|
+
let value: unknown;
|
|
157
|
+
try {
|
|
158
|
+
value = this.#scope.sync(request);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
this.#refusals.sync(error);
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
return this.bounded(reply(value));
|
|
164
|
+
} catch (error) {
|
|
165
|
+
throw error instanceof Error ? error.message : String(error);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Observe cleanup and interruption in the same export result as the callback. */
|
|
170
|
+
private value(outcome: Outcome): unknown {
|
|
171
|
+
return JSON.parse(this.text(outcome));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Preserve the bounded result bytes for the prepared contract to check before decoding. */
|
|
175
|
+
private text(outcome: Outcome): string {
|
|
176
|
+
this.#poisoned ||= outcome.interrupted;
|
|
177
|
+
this.bounded(outcome.value);
|
|
178
|
+
// A callback that failed with the host's refusal unchanged answers it.
|
|
179
|
+
if (!outcome.ok)
|
|
180
|
+
throw this.#refusals.named ? this.#refusals.named.error : new Error(outcome.value);
|
|
181
|
+
return outcome.value;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Any exception leaving the guest is a trap; the instance is never entered again. */
|
|
185
|
+
private guard<T>(operation: () => T): T {
|
|
186
|
+
try {
|
|
187
|
+
return operation();
|
|
188
|
+
} catch (error) {
|
|
189
|
+
this.#poisoned = true;
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Refuse reuse until the previous stack and its I/O have physically returned. */
|
|
195
|
+
private enter(scope?: ExecutionScope): void {
|
|
196
|
+
if (this.#closed || this.#poisoned || this.#running)
|
|
197
|
+
throw new Error("Interpreter is closed or busy");
|
|
198
|
+
this.#running = true;
|
|
199
|
+
this.#scope = scope;
|
|
200
|
+
this.#refusals = new Refusals();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Initialize one revision before issuing any guest execution scope. */
|
|
204
|
+
async load(packet: string): Promise<void> {
|
|
205
|
+
this.enter();
|
|
206
|
+
try {
|
|
207
|
+
const input = this.bounded(packet);
|
|
208
|
+
const outcome = this.guard(() => this.guest!.load(input));
|
|
209
|
+
this.#description = this.bounded(outcome.value);
|
|
210
|
+
this.value(outcome);
|
|
211
|
+
} finally {
|
|
212
|
+
this.#running = false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Read the validated source callback metadata once. */
|
|
217
|
+
description(): string {
|
|
218
|
+
return this.#description;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Factories receive only the synchronous declaration scope. */
|
|
222
|
+
async declare(packet: string, scope: ExecutionScope): Promise<unknown> {
|
|
223
|
+
this.enter(scope);
|
|
224
|
+
try {
|
|
225
|
+
const input = this.bounded(packet);
|
|
226
|
+
return this.value(this.guard(() => this.guest!.declare(input)));
|
|
227
|
+
} finally {
|
|
228
|
+
this.#scope = undefined;
|
|
229
|
+
this.#running = false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Serve one incoming HTTP request: issue its request handle, then run the handler callback.
|
|
235
|
+
* The callback (and so this call) lasts until the response delivery ended; whatever the
|
|
236
|
+
* delivery had not finished when it ends is aborted with the callback's failure.
|
|
237
|
+
*/
|
|
238
|
+
async ingress(
|
|
239
|
+
dispatch: IngressDispatch,
|
|
240
|
+
exchange: IngressExchange,
|
|
241
|
+
scope: ExecutionScope,
|
|
242
|
+
): Promise<void> {
|
|
243
|
+
if (this.#closed || this.#poisoned || this.#running)
|
|
244
|
+
throw new Error("Interpreter is closed or busy");
|
|
245
|
+
if (!this.standard) throw new Error("No standard WASI host");
|
|
246
|
+
const request = this.standard.beginIngress(exchange);
|
|
247
|
+
let failure: unknown = new Error("the HTTP handler callback ended");
|
|
248
|
+
try {
|
|
249
|
+
await this.execute(JSON.stringify({ kind: "ingress", ...dispatch, request }), scope);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
failure = error;
|
|
252
|
+
throw error;
|
|
253
|
+
} finally {
|
|
254
|
+
this.standard.endIngress(failure);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Wait between steps in the host, preserving Promise.race and concurrent effect completion. */
|
|
259
|
+
async execute(packet: string, scope: ExecutionScope): Promise<string> {
|
|
260
|
+
this.enter(scope);
|
|
261
|
+
const refusals = this.#refusals;
|
|
262
|
+
const pending = new Map<number, Promise<Delivery>>();
|
|
263
|
+
const ready: Delivery[] = [];
|
|
264
|
+
let wake: (() => void) | undefined;
|
|
265
|
+
let finished = false;
|
|
266
|
+
let rejectDeadline!: (error: Error) => void;
|
|
267
|
+
const deadline = new Promise<never>((_, reject) => {
|
|
268
|
+
rejectDeadline = reject;
|
|
269
|
+
});
|
|
270
|
+
deadline.catch(() => {});
|
|
271
|
+
// Standard calls observe the caller's cancellation and end with this callback.
|
|
272
|
+
const ended = new AbortController();
|
|
273
|
+
const signal = scope.signal ? combinedSignal(scope.signal, ended.signal) : ended.signal;
|
|
274
|
+
// The one timeout of this callback, which poisons the port when it first happens.
|
|
275
|
+
let timeout: Error | undefined;
|
|
276
|
+
const expired = () => {
|
|
277
|
+
if (!timeout) {
|
|
278
|
+
timeout = new Error("Interpreter callback exceeded its wall time limit");
|
|
279
|
+
this.#poisoned = true;
|
|
280
|
+
ended.abort();
|
|
281
|
+
scope.cancel();
|
|
282
|
+
}
|
|
283
|
+
return timeout;
|
|
284
|
+
};
|
|
285
|
+
const started = performance.now();
|
|
286
|
+
const timer = setTimeout(() => rejectDeadline(expired()), this.timeout);
|
|
287
|
+
// An outcome of a guest step after the deadline, a result or a failure, is a timeout too: a
|
|
288
|
+
// step holds the thread past it and runs the timer late.
|
|
289
|
+
const step = <V>(operation: () => V): V => {
|
|
290
|
+
let outcome: { value: V } | { error: unknown };
|
|
291
|
+
try {
|
|
292
|
+
outcome = { value: this.guard(operation) };
|
|
293
|
+
} catch (error) {
|
|
294
|
+
outcome = { error };
|
|
295
|
+
}
|
|
296
|
+
if (performance.now() - started > this.timeout) throw expired();
|
|
297
|
+
if ("error" in outcome) throw outcome.error;
|
|
298
|
+
return outcome.value;
|
|
299
|
+
};
|
|
300
|
+
try {
|
|
301
|
+
const input = this.bounded(packet);
|
|
302
|
+
let progress = step(() => this.guest!.start(input));
|
|
303
|
+
while (!progress.outcome) {
|
|
304
|
+
if (timeout) throw timeout;
|
|
305
|
+
for (const effect of progress.effects) {
|
|
306
|
+
if (pending.size >= 128 || pending.has(effect.id))
|
|
307
|
+
throw new Error("Invalid interpreter effect queue");
|
|
308
|
+
const packet = effect.packet;
|
|
309
|
+
let issued: readonly number[] | undefined;
|
|
310
|
+
let reads: readonly number[] | undefined;
|
|
311
|
+
const completed = (
|
|
312
|
+
typeof packet === "string"
|
|
313
|
+
? Promise.resolve()
|
|
314
|
+
.then(() => decodeAsyncHostRequest(this.bounded(packet)))
|
|
315
|
+
.then((request) =>
|
|
316
|
+
scope.async(request).catch((error: unknown) => {
|
|
317
|
+
// The guest may fail with this refusal, by its effect's id.
|
|
318
|
+
refusals.effect(effect.id, error);
|
|
319
|
+
throw error;
|
|
320
|
+
}),
|
|
321
|
+
)
|
|
322
|
+
.then((value) => this.bounded(reply(value)))
|
|
323
|
+
: Promise.resolve().then(async () => {
|
|
324
|
+
if (!this.standard) throw new Error("No standard WASI host");
|
|
325
|
+
scope.check();
|
|
326
|
+
const delivery = await this.standard.async(this.boundedBytes(packet), signal);
|
|
327
|
+
issued = delivery.issued;
|
|
328
|
+
reads = delivery.reads;
|
|
329
|
+
return this.boundedBytes(delivery.bytes);
|
|
330
|
+
})
|
|
331
|
+
)
|
|
332
|
+
.then((value): Delivery => ({ id: effect.id, ok: true, value, issued, reads }))
|
|
333
|
+
.catch((error): Delivery => ({
|
|
334
|
+
id: effect.id,
|
|
335
|
+
ok: false,
|
|
336
|
+
issued,
|
|
337
|
+
reads,
|
|
338
|
+
// Standard failures carry the bare message, as the native dispatcher does.
|
|
339
|
+
value:
|
|
340
|
+
typeof packet !== "string" && error instanceof Error
|
|
341
|
+
? error.message
|
|
342
|
+
: String(error),
|
|
343
|
+
}))
|
|
344
|
+
.then((response) => {
|
|
345
|
+
ready.push(response);
|
|
346
|
+
wake?.();
|
|
347
|
+
return response;
|
|
348
|
+
});
|
|
349
|
+
pending.set(effect.id, completed);
|
|
350
|
+
}
|
|
351
|
+
if (!ready.length)
|
|
352
|
+
await Promise.race([
|
|
353
|
+
new Promise<void>((resolve) => {
|
|
354
|
+
wake = resolve;
|
|
355
|
+
}),
|
|
356
|
+
deadline,
|
|
357
|
+
]);
|
|
358
|
+
wake = undefined;
|
|
359
|
+
const completed = ready.splice(0);
|
|
360
|
+
for (const response of completed) {
|
|
361
|
+
pending.delete(response.id);
|
|
362
|
+
if (!response.ok)
|
|
363
|
+
this.standard?.discard({ issued: response.issued ?? [], reads: response.reads });
|
|
364
|
+
}
|
|
365
|
+
progress = step(() => this.guest!.resume(completed));
|
|
366
|
+
}
|
|
367
|
+
finished = true;
|
|
368
|
+
if (timeout) throw timeout;
|
|
369
|
+
return this.text(progress.outcome);
|
|
370
|
+
} finally {
|
|
371
|
+
clearTimeout(timer);
|
|
372
|
+
ended.abort();
|
|
373
|
+
scope.cancel();
|
|
374
|
+
if (!finished) {
|
|
375
|
+
this.#poisoned = true;
|
|
376
|
+
try {
|
|
377
|
+
this.guest!.cancel();
|
|
378
|
+
} catch {
|
|
379
|
+
/* A trapped instance is never reused. */
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
this.#scope = undefined;
|
|
383
|
+
await Promise.allSettled(pending.values());
|
|
384
|
+
for (const delivery of ready)
|
|
385
|
+
this.standard?.discard({ issued: delivery.issued ?? [], reads: delivery.reads });
|
|
386
|
+
// Input writers this callback left open end their streams, so no consumer outlives it.
|
|
387
|
+
this.standard?.endCallback();
|
|
388
|
+
await this.standard?.settled();
|
|
389
|
+
this.#running = false;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Pure guest cleanup needs no execution capability after the environment ends. */
|
|
394
|
+
async release(environment: number): Promise<void> {
|
|
395
|
+
if (this.#closed || this.#poisoned) return;
|
|
396
|
+
await this.execute(JSON.stringify({ kind: "release", environment }), {
|
|
397
|
+
check() {},
|
|
398
|
+
cancel() {},
|
|
399
|
+
deliver(_request, value, publish) {
|
|
400
|
+
publish(value);
|
|
401
|
+
},
|
|
402
|
+
sync() {
|
|
403
|
+
throw new Error("Release has no host scope");
|
|
404
|
+
},
|
|
405
|
+
async() {
|
|
406
|
+
return Promise.reject(new Error("Release has no host scope"));
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Dispose mounted providers only after their last physical operation has settled. */
|
|
412
|
+
async close(): Promise<void> {
|
|
413
|
+
if (this.#running) throw new Error("Guest still owns physical work");
|
|
414
|
+
if (this.#closed) return;
|
|
415
|
+
this.#closed = true;
|
|
416
|
+
this.guest = undefined;
|
|
417
|
+
const standard = this.standard;
|
|
418
|
+
this.standard = undefined;
|
|
419
|
+
try {
|
|
420
|
+
await standard?.dispose();
|
|
421
|
+
} finally {
|
|
422
|
+
await this.dispose();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|