@syntropika/tembra-sdk 0.2.1 → 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 -3298
- 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 -3276
- 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 -3287
- 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 +9 -3
- 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 +218 -56
- 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 -293
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { Lifetime } from "../bridge/identifiers.ts";
|
|
2
|
+
import type { KernelOrb } from "../bridge/kernel.ts";
|
|
3
|
+
import { failure } from "../error.ts";
|
|
4
|
+
|
|
5
|
+
/** A host-owned system directory registered as a filesystem provider. */
|
|
6
|
+
export interface DirectoryFilesystem {
|
|
7
|
+
readonly kind: "directory";
|
|
8
|
+
readonly path: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Description {
|
|
12
|
+
name?: string;
|
|
13
|
+
apiVersion?: number;
|
|
14
|
+
services: Record<
|
|
15
|
+
string,
|
|
16
|
+
{ methods: string[]; lifetime?: Lifetime; linger?: number; access?: boolean }
|
|
17
|
+
>;
|
|
18
|
+
hooks?: Record<string, "series" | "waterfall" | "bail">;
|
|
19
|
+
contributes?: Record<string, number>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface HookBinding {
|
|
23
|
+
name: string;
|
|
24
|
+
alias: string;
|
|
25
|
+
method: string;
|
|
26
|
+
slot: number;
|
|
27
|
+
signature: string;
|
|
28
|
+
composition: "series" | "waterfall" | "bail";
|
|
29
|
+
}
|
|
30
|
+
export interface ContributionBinding {
|
|
31
|
+
binding: string;
|
|
32
|
+
target: string;
|
|
33
|
+
name: string;
|
|
34
|
+
alias: string;
|
|
35
|
+
method: string;
|
|
36
|
+
slot: number;
|
|
37
|
+
signature: string;
|
|
38
|
+
priority: number;
|
|
39
|
+
optional: boolean;
|
|
40
|
+
}
|
|
41
|
+
export interface CheckedImplementation {
|
|
42
|
+
services: ServiceBinding[];
|
|
43
|
+
hooks: { hooks: HookBinding[]; contributions: ContributionBinding[] };
|
|
44
|
+
plan: InstallationPlan["plan"] & {
|
|
45
|
+
/** The incoming HTTP export served through `Host.handleHttp`, absent without one. */
|
|
46
|
+
ingress?: string | null;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* What serves one WIT import: another Orb, a host provider, the kernel itself (`tembra:guest`,
|
|
52
|
+
* no provider and no grant), or the plan of a hook the importing Orb declares and also exports.
|
|
53
|
+
*/
|
|
54
|
+
export type ProviderBinding =
|
|
55
|
+
| { kind: "orb"; name: string; api_version: number }
|
|
56
|
+
| { kind: "host"; provider: string }
|
|
57
|
+
| { kind: "kernel" }
|
|
58
|
+
| { kind: "hook" };
|
|
59
|
+
/** Checked install metadata; the kernel's generated types own shared lifecycle records. */
|
|
60
|
+
export interface InstallationPlan {
|
|
61
|
+
bindings: Record<string, ProviderBinding>;
|
|
62
|
+
importServices: Record<string, string>;
|
|
63
|
+
plan: {
|
|
64
|
+
orb: Pick<
|
|
65
|
+
KernelOrb,
|
|
66
|
+
"name" | "apiVersion" | "services" | "requires" | "hooks" | "hookSignatures"
|
|
67
|
+
>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** WIT-checked implementation metadata returned by the Rust contract module. */
|
|
72
|
+
export interface ServiceBinding {
|
|
73
|
+
name: string;
|
|
74
|
+
interface: string;
|
|
75
|
+
alias: string;
|
|
76
|
+
/** Original WIT method names mapped to guest-facing JavaScript names. */
|
|
77
|
+
methods: Record<string, string>;
|
|
78
|
+
lifetime: Lifetime;
|
|
79
|
+
linger: number;
|
|
80
|
+
signature?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** One checked reference-view role of a compiled artifact, keyed by its import binding. */
|
|
84
|
+
export interface ReferenceBinding {
|
|
85
|
+
/** Versioned identity of the provider interface the view addresses. */
|
|
86
|
+
readonly interface: string;
|
|
87
|
+
/** Nominal identity of the view resource the importer declares. */
|
|
88
|
+
readonly resource: { readonly interface: string; readonly name: string };
|
|
89
|
+
/** Original WIT name of the acquisition function. */
|
|
90
|
+
readonly acquire: string;
|
|
91
|
+
/** Original WIT method names (`[method]R.name`) mapped to provider function names. */
|
|
92
|
+
readonly methods: Readonly<Record<string, string>>;
|
|
93
|
+
/** Interface digest of the derived provider service: the requirement signature. */
|
|
94
|
+
readonly signature: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface ContractPort {
|
|
98
|
+
bind(binding: string, method: string, exports: boolean): number;
|
|
99
|
+
/** Check exact captured JSON text through a bound slot; answers the `{ ok }`/`{ error }` envelope. */
|
|
100
|
+
validate(slot: number, arguments_: boolean, value: string): string;
|
|
101
|
+
request(packet: string): string;
|
|
102
|
+
}
|
|
103
|
+
/** Host-issued authority stays local while only data crosses the Wasm boundary. */
|
|
104
|
+
export interface ExecutionScope {
|
|
105
|
+
readonly signal?: AbortSignal;
|
|
106
|
+
check(): void;
|
|
107
|
+
cancel(): void;
|
|
108
|
+
sync(request: import("./host-protocol.ts").HostRequest): unknown;
|
|
109
|
+
async(request: import("./host-protocol.ts").AsyncHostRequest): Promise<unknown>;
|
|
110
|
+
/** Publish a reply, rolling back an acquired reference if its observer abandoned it. */
|
|
111
|
+
deliver(
|
|
112
|
+
request: import("./host-protocol.ts").AsyncHostRequest,
|
|
113
|
+
value: unknown,
|
|
114
|
+
publish: (value: unknown) => boolean,
|
|
115
|
+
): void;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* One exported WIT interface in the interpreter Load's function table. Each function is listed
|
|
119
|
+
* as `[id, JavaScript spelling]`, where the id is the export slot `ContractPort.bind` returns;
|
|
120
|
+
* `invoke` and `contribute` commands select functions only by that id. Mirrors
|
|
121
|
+
* `ExportedInterface` in `crates/executor-wasm/src/protocol.rs`.
|
|
122
|
+
*/
|
|
123
|
+
export interface ExportedInterface {
|
|
124
|
+
/** Versioned WIT interface identity; it also keys a contribution callback. */
|
|
125
|
+
readonly interface: string;
|
|
126
|
+
/** JavaScript service alias whose environments may call these functions. */
|
|
127
|
+
readonly service: string;
|
|
128
|
+
readonly functions: readonly (readonly [id: number, method: string])[];
|
|
129
|
+
}
|
|
130
|
+
/** The guest service and method that serve incoming HTTP in one environment. */
|
|
131
|
+
export interface IngressDispatch {
|
|
132
|
+
environment: number;
|
|
133
|
+
/** The guest's JavaScript service name for the `wasi:http/handler` export. */
|
|
134
|
+
service: string;
|
|
135
|
+
method: string;
|
|
136
|
+
}
|
|
137
|
+
export interface InterpreterPort {
|
|
138
|
+
release(environment: number): void | Promise<void>;
|
|
139
|
+
description(): string;
|
|
140
|
+
declare(packet: string, scope: ExecutionScope): unknown | Promise<unknown>;
|
|
141
|
+
execute(packet: string, scope: ExecutionScope): Promise<string>;
|
|
142
|
+
/**
|
|
143
|
+
* Serve one incoming HTTP request; resolves only after the response delivery ended and the
|
|
144
|
+
* callback physically settled. Absent where this host cannot serve incoming HTTP yet.
|
|
145
|
+
*/
|
|
146
|
+
ingress?(
|
|
147
|
+
dispatch: IngressDispatch,
|
|
148
|
+
exchange: import("./http-ingress.ts").IngressExchange,
|
|
149
|
+
scope: ExecutionScope,
|
|
150
|
+
): Promise<void>;
|
|
151
|
+
close(): void | Promise<void>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Decode a platform result without treating guest error text as host authority. */
|
|
155
|
+
export function answer<T>(packet: string): T {
|
|
156
|
+
const value = JSON.parse(packet);
|
|
157
|
+
if (Object.hasOwn(value, "ok")) return value.ok as T;
|
|
158
|
+
throw failure(value.error ?? "Interpreter operation failed");
|
|
159
|
+
}
|
|
160
|
+
/** Reuse the prepared contract for installation planning. */
|
|
161
|
+
export function query<T>(contract: Pick<ContractPort, "request">, value: unknown): T {
|
|
162
|
+
return answer<T>(contract.request(JSON.stringify(value)));
|
|
163
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WIT `u64`/`s64` on the portable executor wire: JavaScript sees `bigint`, the wire carries a
|
|
3
|
+
* canonical decimal JSON string, and only a position that WIT types as `u64`/`s64` is ever read
|
|
4
|
+
* that way (a WIT `string` stays a string). This module holds the parts shared by the hardened
|
|
5
|
+
* host capture, the host lift and the guest conversion:
|
|
6
|
+
* - the strict integer helpers;
|
|
7
|
+
* - `Shape`: where 64-bit values occur in one WIT type, compiled once per prepared function;
|
|
8
|
+
* - `lift`: restoring `bigint` in data parsed from already checked text.
|
|
9
|
+
* It has no imports and no state, so the guest bootstrap bundles it as is. It is not a WIT
|
|
10
|
+
* validator: the Rust contract still checks every value in full.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export type IntegerKind = "u64" | "s64";
|
|
14
|
+
|
|
15
|
+
const U64_MAX = (1n << 64n) - 1n;
|
|
16
|
+
const S64_MIN = -(1n << 63n);
|
|
17
|
+
const S64_MAX = (1n << 63n) - 1n;
|
|
18
|
+
/** Canonical decimal text: no sign on `u64`, no `+`, no `-0`, no leading zeros or whitespace. */
|
|
19
|
+
const CANONICAL = { u64: /^(?:0|[1-9][0-9]{0,19})$/, s64: /^(?:0|-?[1-9][0-9]{0,18})$/ };
|
|
20
|
+
|
|
21
|
+
/** Whether `value` is a bigint primitive within the range of `kind`. */
|
|
22
|
+
export function isInteger(kind: IntegerKind, value: unknown): value is bigint {
|
|
23
|
+
return (
|
|
24
|
+
typeof value === "bigint" &&
|
|
25
|
+
(kind === "u64" ? value >= 0n && value <= U64_MAX : value >= S64_MIN && value <= S64_MAX)
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Canonical wire text of a `bigint`; every other JavaScript kind is refused. */
|
|
30
|
+
export function integerText(kind: IntegerKind, value: unknown): string {
|
|
31
|
+
if (!isInteger(kind, value))
|
|
32
|
+
throw new TypeError(
|
|
33
|
+
typeof value === "bigint"
|
|
34
|
+
? `${kind} value is out of range`
|
|
35
|
+
: `${kind} requires a bigint, not ${typeof value}`,
|
|
36
|
+
);
|
|
37
|
+
// String concatenation applies the abstract ToString to a bigint primitive.
|
|
38
|
+
return "" + value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The `bigint` of canonical wire text; anything else is refused. */
|
|
42
|
+
export function integerValue(kind: IntegerKind, text: unknown): bigint {
|
|
43
|
+
if (typeof text !== "string" || !CANONICAL[kind].test(text))
|
|
44
|
+
throw new TypeError(`${kind} requires canonical decimal text`);
|
|
45
|
+
const value = BigInt(text);
|
|
46
|
+
if (!isInteger(kind, value)) throw new TypeError(`${kind} value is out of range`);
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Where 64-bit values occur in one WIT type; `undefined` means none below this point. */
|
|
51
|
+
export type Shape =
|
|
52
|
+
| IntegerKind
|
|
53
|
+
| { readonly list: Shape }
|
|
54
|
+
/** Record fields by JavaScript name; every field is present, 64-bit-free ones as `undefined`. */
|
|
55
|
+
| { readonly fields: ReadonlyMap<string, Shape | undefined> }
|
|
56
|
+
/** Tuples and argument lists. */
|
|
57
|
+
| { readonly items: readonly (Shape | undefined)[] }
|
|
58
|
+
| { readonly option: Shape }
|
|
59
|
+
/** Variant and result payloads by case name; every case is present. */
|
|
60
|
+
| { readonly cases: ReadonlyMap<string, Shape | undefined> };
|
|
61
|
+
|
|
62
|
+
/** Argument list and result shapes of one prepared function. */
|
|
63
|
+
export interface Signature {
|
|
64
|
+
readonly params: Shape | undefined;
|
|
65
|
+
readonly result: Shape | undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type WitType = string | number | null | undefined;
|
|
69
|
+
interface WitKind {
|
|
70
|
+
type?: WitType;
|
|
71
|
+
list?: WitType;
|
|
72
|
+
option?: WitType;
|
|
73
|
+
record?: { fields: { name: string; type: WitType }[] };
|
|
74
|
+
tuple?: { types: WitType[] };
|
|
75
|
+
variant?: { cases: { name: string; type?: WitType }[] };
|
|
76
|
+
result?: { ok?: WitType; err?: WitType };
|
|
77
|
+
}
|
|
78
|
+
/** The parts of the contract's `describe` answer this module reads. */
|
|
79
|
+
export interface WitTypes {
|
|
80
|
+
readonly javascriptNames: Readonly<Record<string, string>>;
|
|
81
|
+
readonly types: readonly {
|
|
82
|
+
readonly id: number;
|
|
83
|
+
readonly definition: { kind: WitKind | string };
|
|
84
|
+
}[];
|
|
85
|
+
}
|
|
86
|
+
export interface WitFunction {
|
|
87
|
+
readonly params: readonly { readonly name: string; readonly type: WitType }[];
|
|
88
|
+
readonly result?: WitType;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Compile function signatures against one contract graph; compiled types are memoized. */
|
|
92
|
+
export function shapes(graph: WitTypes): (fn: WitFunction) => Signature {
|
|
93
|
+
const kinds = new Map(graph.types.map(({ id, definition }) => [id, definition.kind]));
|
|
94
|
+
const compiled = new Map<number, Shape | undefined>();
|
|
95
|
+
const field = (name: string) => {
|
|
96
|
+
const spelling = graph.javascriptNames[name];
|
|
97
|
+
if (spelling === undefined) throw new Error(`Missing WIT JavaScript name for ${name}`);
|
|
98
|
+
return spelling;
|
|
99
|
+
};
|
|
100
|
+
const cases = (entries: [string, WitType][]): Shape | undefined => {
|
|
101
|
+
const shapes = new Map(entries.map(([name, type]) => [name, compile(type)] as const));
|
|
102
|
+
return [...shapes.values()].some(Boolean) ? { cases: shapes } : undefined;
|
|
103
|
+
};
|
|
104
|
+
const compile = (type: WitType): Shape | undefined => {
|
|
105
|
+
if (type === "u64" || type === "s64") return type;
|
|
106
|
+
if (typeof type !== "number") return undefined;
|
|
107
|
+
if (compiled.has(type)) return compiled.get(type);
|
|
108
|
+
const kind = kinds.get(type);
|
|
109
|
+
if (!kind) throw new Error("Unknown WIT type reference");
|
|
110
|
+
if (typeof kind === "string") return undefined;
|
|
111
|
+
let shape: Shape | undefined;
|
|
112
|
+
if (kind.type !== undefined) shape = compile(kind.type);
|
|
113
|
+
else if (kind.list !== undefined) {
|
|
114
|
+
const item = compile(kind.list);
|
|
115
|
+
shape = item && { list: item };
|
|
116
|
+
} else if (kind.option !== undefined) {
|
|
117
|
+
const item = compile(kind.option);
|
|
118
|
+
shape = item && { option: item };
|
|
119
|
+
} else if (kind.record) {
|
|
120
|
+
const fields = new Map(
|
|
121
|
+
kind.record.fields.map(({ name, type }) => [field(name), compile(type)] as const),
|
|
122
|
+
);
|
|
123
|
+
shape = [...fields.values()].some(Boolean) ? { fields } : undefined;
|
|
124
|
+
} else if (kind.tuple) {
|
|
125
|
+
const items = kind.tuple.types.map(compile);
|
|
126
|
+
shape = items.some(Boolean) ? { items } : undefined;
|
|
127
|
+
} else if (kind.variant)
|
|
128
|
+
shape = cases(kind.variant.cases.map(({ name, type }) => [name, type] as [string, WitType]));
|
|
129
|
+
else if (kind.result)
|
|
130
|
+
shape = cases([
|
|
131
|
+
["ok", kind.result.ok],
|
|
132
|
+
["err", kind.result.err],
|
|
133
|
+
]);
|
|
134
|
+
compiled.set(type, shape);
|
|
135
|
+
return shape;
|
|
136
|
+
};
|
|
137
|
+
return (fn) => {
|
|
138
|
+
const items = fn.params.map(({ type }) => compile(type));
|
|
139
|
+
return {
|
|
140
|
+
params: items.some(Boolean) ? { items } : undefined,
|
|
141
|
+
result: compile(fn.result),
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Restore `bigint` positions in data parsed from text the Rust contract already checked. Shaped
|
|
148
|
+
* containers are copied, never updated in place, so the caller's parsed data stays wire form;
|
|
149
|
+
* lists of 64-bit values become arrays of `bigint`.
|
|
150
|
+
*/
|
|
151
|
+
export function lift(shape: Shape | undefined, value: unknown): unknown {
|
|
152
|
+
if (shape === undefined || value === null || value === undefined) return value;
|
|
153
|
+
if (typeof shape === "string") return integerValue(shape, value);
|
|
154
|
+
if ("option" in shape) return lift(shape.option, value);
|
|
155
|
+
if ("list" in shape) {
|
|
156
|
+
if (!Array.isArray(value)) throw new TypeError("WIT list requires an array");
|
|
157
|
+
return value.map((item) => lift(shape.list, item));
|
|
158
|
+
}
|
|
159
|
+
if ("items" in shape) {
|
|
160
|
+
if (!Array.isArray(value) || value.length !== shape.items.length)
|
|
161
|
+
throw new TypeError("WIT tuple length differs");
|
|
162
|
+
return value.map((item, index) => lift(shape.items[index], item));
|
|
163
|
+
}
|
|
164
|
+
if (typeof value !== "object" || Array.isArray(value))
|
|
165
|
+
throw new TypeError("WIT record or tagged value requires an object");
|
|
166
|
+
const copy: Record<string, unknown> = { ...value };
|
|
167
|
+
if ("fields" in shape) {
|
|
168
|
+
for (const [name, item] of shape.fields)
|
|
169
|
+
if (item && Object.hasOwn(copy, name)) copy[name] = lift(item, copy[name]);
|
|
170
|
+
} else {
|
|
171
|
+
const item = shape.cases.get(copy.tag as string);
|
|
172
|
+
if (item && Object.hasOwn(copy, "val")) copy.val = lift(item, copy.val);
|
|
173
|
+
}
|
|
174
|
+
return copy;
|
|
175
|
+
}
|
package/src/node.ts
CHANGED
|
@@ -2,38 +2,55 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { pathToFileURL } from "node:url";
|
|
4
4
|
import { orb } from "./authoring/orb.ts";
|
|
5
|
+
import { readFile } from "node:fs/promises";
|
|
6
|
+
import { isolate, type AdaptedHostOptions } from "#tembra-adapters";
|
|
7
|
+
import { NodeExecution, type InterpreterOptions } from "./interpreter/node.ts";
|
|
5
8
|
import type { HostOptions } from "./authoring/types.ts";
|
|
6
9
|
import type { KernelPort } from "./bridge/kernel.ts";
|
|
7
10
|
import { Host } from "./host.ts";
|
|
8
11
|
|
|
9
|
-
export type NodeHostOptions = Omit<HostOptions, "kernel"> & {
|
|
12
|
+
export type NodeHostOptions = Omit<HostOptions, "kernel"> & {
|
|
13
|
+
kernel?: KernelPort;
|
|
14
|
+
isolated?: InterpreterOptions;
|
|
15
|
+
};
|
|
10
16
|
|
|
11
|
-
/**
|
|
17
|
+
/** Configure the ordinary host and lazily create isolated execution for Wasm Orbs. */
|
|
12
18
|
export function createHost(options: NodeHostOptions = {}): Host {
|
|
13
19
|
const require = createRequire(import.meta.url);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
let kernel = options.kernel;
|
|
21
|
+
if (!kernel) {
|
|
22
|
+
const { NodeKernel } = require("@syntropika/tembra-engine-node") as {
|
|
23
|
+
NodeKernel: new () => KernelPort;
|
|
24
|
+
};
|
|
25
|
+
kernel = new NodeKernel();
|
|
26
|
+
}
|
|
27
|
+
let execution: NodeExecution | undefined;
|
|
28
|
+
const backend = () => (execution ??= new NodeExecution(options.isolated ?? {}));
|
|
19
29
|
return new Host({
|
|
30
|
+
[isolate]: (definition) => backend().materialize(definition),
|
|
20
31
|
...options,
|
|
21
|
-
kernel
|
|
32
|
+
kernel,
|
|
22
33
|
load:
|
|
23
34
|
options.load ??
|
|
24
|
-
(async (location) => {
|
|
35
|
+
(async (location, registration) => {
|
|
25
36
|
const url =
|
|
26
37
|
location instanceof URL
|
|
27
38
|
? location
|
|
28
39
|
: /^[a-z]+:/i.test(location)
|
|
29
40
|
? new URL(location)
|
|
30
41
|
: pathToFileURL(path.resolve(location));
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
);
|
|
42
|
+
if (!url.pathname.endsWith(".wasm"))
|
|
43
|
+
throw new Error("Orb loading requires a built .wasm artifact");
|
|
44
|
+
let source: Uint8Array;
|
|
45
|
+
if (url.protocol === "file:") source = new Uint8Array(await readFile(url));
|
|
46
|
+
else {
|
|
47
|
+
const response = await fetch(url);
|
|
48
|
+
if (!response.ok) throw new Error(`Orb request failed: ${response.status}`);
|
|
49
|
+
source = new Uint8Array(await response.arrayBuffer());
|
|
50
|
+
}
|
|
51
|
+
return { ...registration, artifact: source };
|
|
35
52
|
}),
|
|
36
|
-
});
|
|
53
|
+
} as AdaptedHostOptions);
|
|
37
54
|
}
|
|
38
55
|
|
|
39
56
|
export { orb };
|
|
@@ -50,4 +67,16 @@ export {
|
|
|
50
67
|
|
|
51
68
|
export { Errors, type ErrorDescriptor } from "./errors.ts";
|
|
52
69
|
|
|
70
|
+
export {
|
|
71
|
+
HttpHandlerError,
|
|
72
|
+
type HandleHttpOptions,
|
|
73
|
+
type HttpHandlerResponse,
|
|
74
|
+
type IncomingHttpRequest,
|
|
75
|
+
} from "./interpreter/http-ingress.ts";
|
|
76
|
+
export type { HttpField } from "./interpreter/http-provider.ts";
|
|
77
|
+
|
|
53
78
|
export { ContextKey, ContextKind, opaqueFrom, type OpaqueContextValue } from "./context-key.ts";
|
|
79
|
+
|
|
80
|
+
export { directoryFilesystem, type DirectoryFilesystem } from "./interpreter/node.ts";
|
|
81
|
+
export type { InterpreterOptions as IsolatedOptions } from "./interpreter/node.ts";
|
|
82
|
+
export type { Grants, CliOptions } from "./authoring/types.ts";
|
|
@@ -19,13 +19,13 @@ export class Activities {
|
|
|
19
19
|
const source =
|
|
20
20
|
typeof definition.source === "function" ? definition.source() : definition.source;
|
|
21
21
|
if (!source || typeof source.subscribe !== "function") refuse(Errors.Invalid.EventSource);
|
|
22
|
-
const id = this.session.kernel.command
|
|
22
|
+
const id = this.session.kernel.command(KernelOp.Listen, {
|
|
23
23
|
scope: environment.id,
|
|
24
24
|
previous: previous?.id,
|
|
25
25
|
name: definition.name,
|
|
26
26
|
source: this.identity(source),
|
|
27
27
|
sizing: definition.options.sizeOf ? this.identity(definition.options.sizeOf) : 0,
|
|
28
|
-
operation: this.session.kernel.command
|
|
28
|
+
operation: this.session.kernel.command(KernelOp.Allocate),
|
|
29
29
|
maxPending: definition.options.maxPending ?? 64,
|
|
30
30
|
...(definition.options.maxBytes ? { maxBytes: definition.options.maxBytes } : {}),
|
|
31
31
|
coalesce: "select" in source,
|
|
@@ -84,9 +84,9 @@ export class Activities {
|
|
|
84
84
|
private startTask(environment: Environment, definition: TaskDefinition): void {
|
|
85
85
|
if (definition.running || environment.phase !== EnvironmentPhase.Live || this.session.closing)
|
|
86
86
|
return;
|
|
87
|
-
const id = this.session.kernel.command
|
|
87
|
+
const id = this.session.kernel.command(KernelOp.Task, { owner: environment.owner });
|
|
88
88
|
const controller = new AbortController();
|
|
89
|
-
const operation = this.session.kernel.command
|
|
89
|
+
const operation = this.session.kernel.command(KernelOp.Allocate);
|
|
90
90
|
const record: TaskRecord = { id, controller, done: Promise.resolve() };
|
|
91
91
|
definition.running = record;
|
|
92
92
|
this.tasks.set(id, record);
|
|
@@ -97,7 +97,7 @@ export class Activities {
|
|
|
97
97
|
signal: controller.signal,
|
|
98
98
|
/** Issue operation authority under the task's retained version. */
|
|
99
99
|
root: (callback) => {
|
|
100
|
-
const root = this.session.kernel.command
|
|
100
|
+
const root = this.session.kernel.command(KernelOp.TaskRoot, {
|
|
101
101
|
task: id,
|
|
102
102
|
principal: this.session.principalFor(environment.object?.id ?? 0),
|
|
103
103
|
});
|
|
@@ -125,7 +125,7 @@ export class Activities {
|
|
|
125
125
|
private identity(value: object): number {
|
|
126
126
|
let id = this.#identities.get(value);
|
|
127
127
|
if (id === undefined) {
|
|
128
|
-
id = this.session.kernel.command
|
|
128
|
+
id = this.session.kernel.command(KernelOp.Allocate);
|
|
129
129
|
this.#identities.set(value, id);
|
|
130
130
|
}
|
|
131
131
|
return id;
|