@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,218 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { refusal, reply } from "./codec.ts";
|
|
3
|
+
import { failure as fault } from "../error.ts";
|
|
4
|
+
import type {
|
|
5
|
+
NodeField,
|
|
6
|
+
NodeIngressEvent,
|
|
7
|
+
NodeInterpreterPort,
|
|
8
|
+
} from "@syntropika/tembra-engine-node";
|
|
9
|
+
import { decodeAsyncHostRequest, decodeHostRequest } from "./host-protocol.ts";
|
|
10
|
+
import type { IngressExchange } from "./http-ingress.ts";
|
|
11
|
+
import type { HttpField } from "./http-provider.ts";
|
|
12
|
+
import { ingressPacket, NativeIngress, type CarrierEvent } from "./native-ingress.ts";
|
|
13
|
+
import {
|
|
14
|
+
answer,
|
|
15
|
+
type ExecutionScope,
|
|
16
|
+
type IngressDispatch,
|
|
17
|
+
type InterpreterPort,
|
|
18
|
+
} from "./types.ts";
|
|
19
|
+
|
|
20
|
+
/** Native worker events cross a thread; browser execution uses the same scope directly. */
|
|
21
|
+
export class NodePort implements InterpreterPort {
|
|
22
|
+
/** Retain one native transport until the revision physically closes. */
|
|
23
|
+
constructor(readonly transport: NodeInterpreterPort) {}
|
|
24
|
+
|
|
25
|
+
/** Read the validated source metadata once. */
|
|
26
|
+
description(): string {
|
|
27
|
+
return this.transport.description();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Release only guest-owned bookkeeping after its host environment ends. */
|
|
31
|
+
release(environment: number): void {
|
|
32
|
+
this.transport.release(environment);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Keep factory calls synchronous while decoding each host request once. */
|
|
36
|
+
declare(packet: string, scope: ExecutionScope): unknown {
|
|
37
|
+
return answer(
|
|
38
|
+
this.transport.declare(packet, (request) =>
|
|
39
|
+
respond(() => scope.sync(decodeHostRequest(request))),
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Finish the native ticket only after admitted host operations physically settle. */
|
|
45
|
+
execute(packet: string, scope: ExecutionScope): Promise<string> {
|
|
46
|
+
return new Promise((resolve, reject) => {
|
|
47
|
+
let failure: unknown;
|
|
48
|
+
const pending = new Set<Promise<unknown>>();
|
|
49
|
+
// A cancelled caller ends the native callback scope, which also revokes pending standard
|
|
50
|
+
// WASI calls that never pass through `scope.async`. Retained descriptors are unaffected.
|
|
51
|
+
const signal = scope.signal;
|
|
52
|
+
const cancel = () => this.transport.cancel();
|
|
53
|
+
this.transport.dispatch(packet, (raw) => {
|
|
54
|
+
try {
|
|
55
|
+
const event = JSON.parse(raw);
|
|
56
|
+
if (event.event === "complete") {
|
|
57
|
+
signal?.removeEventListener("abort", cancel);
|
|
58
|
+
scope.cancel();
|
|
59
|
+
Promise.allSettled([...pending])
|
|
60
|
+
.then(() => {
|
|
61
|
+
try {
|
|
62
|
+
this.transport.finish();
|
|
63
|
+
if (failure !== undefined) throw failure;
|
|
64
|
+
if (!Object.hasOwn(event.result, "ok")) throw fault(event.result.error);
|
|
65
|
+
if (typeof event.result.ok !== "string")
|
|
66
|
+
throw new Error("Invalid guest result packet");
|
|
67
|
+
resolve(event.result.ok);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
reject(error);
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.catch(reject);
|
|
73
|
+
} else if (event.event === "host") {
|
|
74
|
+
if (!event.asynchronous)
|
|
75
|
+
this.transport.reply(
|
|
76
|
+
event.id,
|
|
77
|
+
respond(() => scope.sync(decodeHostRequest(event.packet))),
|
|
78
|
+
);
|
|
79
|
+
else {
|
|
80
|
+
let request!: ReturnType<typeof decodeAsyncHostRequest>;
|
|
81
|
+
const operation = Promise.resolve()
|
|
82
|
+
.then(() => {
|
|
83
|
+
request = decodeAsyncHostRequest(event.packet);
|
|
84
|
+
return scope.async(request);
|
|
85
|
+
})
|
|
86
|
+
.then(
|
|
87
|
+
(value) =>
|
|
88
|
+
scope.deliver(request, value, (value) =>
|
|
89
|
+
this.transport.reply(
|
|
90
|
+
event.id,
|
|
91
|
+
respond(() => value),
|
|
92
|
+
),
|
|
93
|
+
),
|
|
94
|
+
(error) =>
|
|
95
|
+
this.transport.reply(
|
|
96
|
+
event.id,
|
|
97
|
+
respond(() => {
|
|
98
|
+
throw error;
|
|
99
|
+
}),
|
|
100
|
+
),
|
|
101
|
+
)
|
|
102
|
+
.catch((error) => {
|
|
103
|
+
failure = error;
|
|
104
|
+
});
|
|
105
|
+
pending.add(operation);
|
|
106
|
+
operation.finally(() => pending.delete(operation));
|
|
107
|
+
}
|
|
108
|
+
} else throw new Error("Unknown interpreter event");
|
|
109
|
+
} catch (error) {
|
|
110
|
+
failure = error;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
if (signal?.aborted) cancel();
|
|
114
|
+
else signal?.addEventListener("abort", cancel, { once: true });
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Serve one incoming HTTP request through the typed NAPI carrier. Bytes cross as `Buffer`s;
|
|
120
|
+
* the head is published early and this resolves only after `complete`, the drain of admitted
|
|
121
|
+
* host work and `finish()`.
|
|
122
|
+
*/
|
|
123
|
+
ingress(
|
|
124
|
+
dispatch: IngressDispatch,
|
|
125
|
+
exchange: IngressExchange,
|
|
126
|
+
scope: ExecutionScope,
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
const transport = this.transport;
|
|
129
|
+
const carrier = new NativeIngress(
|
|
130
|
+
{
|
|
131
|
+
supply: (answer) =>
|
|
132
|
+
answer.kind === "data"
|
|
133
|
+
? transport.supplyRequest(answer.data, null, null)
|
|
134
|
+
: answer.kind === "end"
|
|
135
|
+
? transport.supplyRequest(null, answer.trailers.map(nodeField), null)
|
|
136
|
+
: transport.supplyRequest(null, null, answer.message),
|
|
137
|
+
pull: () => transport.pullResponse(),
|
|
138
|
+
disconnect: () => transport.disconnect(),
|
|
139
|
+
cancel: () => transport.cancel(),
|
|
140
|
+
reply: (id, packet) => transport.reply(id, packet),
|
|
141
|
+
finish: () => transport.finish(),
|
|
142
|
+
},
|
|
143
|
+
exchange,
|
|
144
|
+
scope,
|
|
145
|
+
);
|
|
146
|
+
const { method, scheme, authority, pathWithQuery, fields } = exchange.request;
|
|
147
|
+
try {
|
|
148
|
+
transport.ingress(
|
|
149
|
+
ingressPacket(dispatch),
|
|
150
|
+
{
|
|
151
|
+
method,
|
|
152
|
+
...(scheme === undefined ? {} : { scheme }),
|
|
153
|
+
...(authority === undefined ? {} : { authority }),
|
|
154
|
+
...(pathWithQuery === undefined ? {} : { pathWithQuery }),
|
|
155
|
+
fields: fields.map(nodeField),
|
|
156
|
+
},
|
|
157
|
+
(event) => {
|
|
158
|
+
try {
|
|
159
|
+
carrier.event(carrierEvent(event));
|
|
160
|
+
} catch (error) {
|
|
161
|
+
// A malformed event cannot unwind the native callback; the call fails instead.
|
|
162
|
+
carrier.fail(error);
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
// Refused before admission (an invalid head, a closed port): nothing native began.
|
|
168
|
+
void carrier.abandon(error);
|
|
169
|
+
return carrier.settled;
|
|
170
|
+
}
|
|
171
|
+
carrier.begun();
|
|
172
|
+
return carrier.settled;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Drop an idle native Store after the common lifecycle releases its scopes. */
|
|
176
|
+
close(): void {
|
|
177
|
+
this.transport.close();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Encode one native response without throwing through a callback frame. */
|
|
182
|
+
function respond(operation: () => unknown): string {
|
|
183
|
+
try {
|
|
184
|
+
return `{ "ok":${reply(operation())}}`;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
return refusal(error);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** A field as a `Buffer` view of the same bytes; NAPI copies it. */
|
|
191
|
+
function nodeField([name, value]: HttpField): NodeField {
|
|
192
|
+
return { name, value: Buffer.from(value.buffer, value.byteOffset, value.byteLength) };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** A NAPI `Buffer` as a plain `Uint8Array` view, without copying. */
|
|
196
|
+
function bytes(value: Uint8Array): Uint8Array {
|
|
197
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function fields(value: readonly NodeField[]): readonly HttpField[] {
|
|
201
|
+
return value.map((field) => [field.name, bytes(field.value)] as const);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** The NAPI event in the shared carrier form. */
|
|
205
|
+
function carrierEvent(event: NodeIngressEvent): CarrierEvent {
|
|
206
|
+
switch (event.kind) {
|
|
207
|
+
case "refused":
|
|
208
|
+
return { kind: "refused", code: bytes(event.code) };
|
|
209
|
+
case "data":
|
|
210
|
+
return { kind: "data", data: bytes(event.data) };
|
|
211
|
+
case "head":
|
|
212
|
+
return { kind: "head", status: event.status, fields: fields(event.fields) };
|
|
213
|
+
case "trailers":
|
|
214
|
+
return { kind: "trailers", fields: fields(event.fields) };
|
|
215
|
+
default:
|
|
216
|
+
return event;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { NodePort } from "./node-port.ts";
|
|
2
|
+
import { cliPacket } from "./cli.ts";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import type { NodeArtifact, NodeInterpreter } from "@syntropika/tembra-engine-node";
|
|
5
|
+
import type { OrbDefinition, OrbArtifact, LoadOptions } from "../authoring/types.ts";
|
|
6
|
+
import { Revision } from "./revision.ts";
|
|
7
|
+
import { query, type InstallationPlan, type DirectoryFilesystem } from "./types.ts";
|
|
8
|
+
export type { DirectoryFilesystem } from "./types.ts";
|
|
9
|
+
|
|
10
|
+
/** Register a system directory without granting an Orb access to it. */
|
|
11
|
+
export function directoryFilesystem(path: string): DirectoryFilesystem {
|
|
12
|
+
return Object.freeze({ kind: "directory", path });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface InterpreterOptions {
|
|
16
|
+
engine?: Uint8Array;
|
|
17
|
+
providers?: {
|
|
18
|
+
filesystem?: Readonly<Record<string, DirectoryFilesystem>>;
|
|
19
|
+
network?: readonly string[];
|
|
20
|
+
};
|
|
21
|
+
limits?: { memoryBytes?: number; fuel?: number; packetBytes?: number; timeoutMs?: number };
|
|
22
|
+
onLog?: (record: { orb: string; level: string; message: string }) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Platform transport owns Stores; the common host still owns every lifecycle decision. */
|
|
26
|
+
export class NodeExecution {
|
|
27
|
+
#backend: NodeInterpreter | undefined;
|
|
28
|
+
#interpreter = false;
|
|
29
|
+
readonly #binding = createRequire(import.meta.url)(
|
|
30
|
+
"@syntropika/tembra-engine-node",
|
|
31
|
+
) as typeof import("@syntropika/tembra-engine-node");
|
|
32
|
+
|
|
33
|
+
/** Register host configuration without compiling an interpreter for metadata-only hosts. */
|
|
34
|
+
constructor(readonly options: InterpreterOptions) {}
|
|
35
|
+
|
|
36
|
+
/** Materialize source bytes supplied directly through an Orb declaration. */
|
|
37
|
+
async materialize(definition: OrbArtifact): Promise<OrbDefinition> {
|
|
38
|
+
return this.open(new this.#binding.NodeArtifact(definition.artifact), definition);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Lazily share compiled engine code while keeping provider tables local to this host. */
|
|
42
|
+
private backend(): NodeInterpreter {
|
|
43
|
+
if (this.#backend) return this.#backend;
|
|
44
|
+
this.#backend = new this.#binding.NodeInterpreter(
|
|
45
|
+
JSON.stringify({
|
|
46
|
+
limits: this.options.limits ?? {},
|
|
47
|
+
network: this.options.providers?.network ?? [],
|
|
48
|
+
filesystem: Object.fromEntries(
|
|
49
|
+
Object.entries(this.options.providers?.filesystem ?? {}).map(([name, provider]) => [
|
|
50
|
+
name,
|
|
51
|
+
provider.path,
|
|
52
|
+
]),
|
|
53
|
+
),
|
|
54
|
+
}),
|
|
55
|
+
(packet) => {
|
|
56
|
+
try {
|
|
57
|
+
this.options.onLog?.(JSON.parse(packet));
|
|
58
|
+
} catch {
|
|
59
|
+
/* Observers cannot interrupt physical guest work. */
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
);
|
|
63
|
+
return this.#backend;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Resolve grants once, then adapt the opaque artifact through the common lifecycle. */
|
|
67
|
+
private async open(artifact: NodeArtifact, options: LoadOptions): Promise<OrbDefinition> {
|
|
68
|
+
const identity = JSON.parse(artifact.description()) as {
|
|
69
|
+
name: string;
|
|
70
|
+
apiVersion: number;
|
|
71
|
+
kind: "source" | "component";
|
|
72
|
+
};
|
|
73
|
+
const prepared = query<InstallationPlan>(artifact, {
|
|
74
|
+
kind: "install",
|
|
75
|
+
name: identity.name,
|
|
76
|
+
apiVersion: identity.apiVersion,
|
|
77
|
+
requires: options.requires ?? {},
|
|
78
|
+
grants: options.grants ?? {},
|
|
79
|
+
filesystem: Object.keys(this.options.providers?.filesystem ?? {}),
|
|
80
|
+
network: this.options.providers?.network ?? [],
|
|
81
|
+
http: this.options.providers?.network ?? [],
|
|
82
|
+
});
|
|
83
|
+
const backend = this.backend();
|
|
84
|
+
// Compiled components execute without the interpreter; only source revisions load it.
|
|
85
|
+
if (identity.kind === "source" && !this.#interpreter) {
|
|
86
|
+
backend.interpreter(this.options.engine ?? requireEngine());
|
|
87
|
+
this.#interpreter = true;
|
|
88
|
+
}
|
|
89
|
+
const port = backend.open(
|
|
90
|
+
artifact,
|
|
91
|
+
JSON.stringify(prepared.bindings),
|
|
92
|
+
JSON.stringify(options.grants ?? {}),
|
|
93
|
+
cliPacket(options.cli),
|
|
94
|
+
);
|
|
95
|
+
try {
|
|
96
|
+
const transport = new NodePort(port);
|
|
97
|
+
return new Revision(transport, artifact, this.options.limits?.timeoutMs ?? 10000).definition(
|
|
98
|
+
identity,
|
|
99
|
+
prepared,
|
|
100
|
+
);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
port.close();
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Read packaged engine bytes only when an isolated Orb first needs them. */
|
|
109
|
+
function requireEngine(): Uint8Array {
|
|
110
|
+
const require = createRequire(import.meta.url);
|
|
111
|
+
return require("node:fs").readFileSync(
|
|
112
|
+
require.resolve("@syntropika/tembra-engine-node/interpreter.wasm"),
|
|
113
|
+
);
|
|
114
|
+
}
|