@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,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host transport contract behind standard `wasi:http`, independent of a platform adapter.
|
|
3
|
+
*
|
|
4
|
+
* An `HttpProvider` receives one grant-checked request with raw byte fields, a pull-based body
|
|
5
|
+
* stream and a trailers promise, and returns raw response fields, a body stream and optional
|
|
6
|
+
* trailers. Mocks and alternate transports implement the full WASI semantics through it
|
|
7
|
+
* (duplicate raw fields, trailers both ways, uploads that stream while the response is read).
|
|
8
|
+
* The browser Fetch adapter is implemented separately in `fetch-http-provider.ts`.
|
|
9
|
+
*/
|
|
10
|
+
import type { ErrorCode } from "wasi:http/types@0.3.0";
|
|
11
|
+
|
|
12
|
+
/** One field: a lowercase name and its raw value bytes. Order and duplicates are significant. */
|
|
13
|
+
export type HttpField = readonly [name: string, value: Uint8Array];
|
|
14
|
+
|
|
15
|
+
/** WASI `request-options` durations in nanoseconds; absent means no timeout was requested. */
|
|
16
|
+
export interface HttpTimeouts {
|
|
17
|
+
readonly connect?: bigint | undefined;
|
|
18
|
+
readonly firstByte?: bigint | undefined;
|
|
19
|
+
readonly betweenBytes?: bigint | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* One outgoing request. The host enforces `firstByte` (until the response promise settles) and
|
|
24
|
+
* `betweenBytes` (per response body read) for every provider; `connect` is the provider's to
|
|
25
|
+
* honor or refuse.
|
|
26
|
+
*/
|
|
27
|
+
export interface HttpProviderRequest {
|
|
28
|
+
/**
|
|
29
|
+
* The granted canonical origin with the WHATWG-normalized path, for URL-based transports
|
|
30
|
+
* such as Fetch (which resolve dot segments and backslashes and escape some characters).
|
|
31
|
+
*/
|
|
32
|
+
readonly url: URL;
|
|
33
|
+
/** The request target exactly as the guest validated it (origin-form path and query). */
|
|
34
|
+
readonly pathWithQuery: string;
|
|
35
|
+
/** `GET`, `POST`, … or an extension token verbatim. */
|
|
36
|
+
readonly method: string;
|
|
37
|
+
readonly fields: readonly HttpField[];
|
|
38
|
+
/**
|
|
39
|
+
* The request contents, pulled one bounded chunk at a time from the guest (no read-ahead), or
|
|
40
|
+
* `null` when the request has none. It closes only after the whole upload, including a
|
|
41
|
+
* declared `Content-Length` and the trailers result, succeeded; a failed or abandoned upload
|
|
42
|
+
* errors it with an `HttpProviderError` and never closes it, so a provider cannot commit a
|
|
43
|
+
* truncated body. A provider that will not read it must cancel it; the host ends it
|
|
44
|
+
* anyway when the response is released early or fails, or the revision retires. Cancelling
|
|
45
|
+
* it stops the guest's upload.
|
|
46
|
+
*/
|
|
47
|
+
readonly body: ReadableStream<Uint8Array> | null;
|
|
48
|
+
/**
|
|
49
|
+
* Trailers, settled once the body ended: `undefined` for none. Rejects with the upload's
|
|
50
|
+
* `HttpProviderError` when the upload fails.
|
|
51
|
+
*/
|
|
52
|
+
readonly trailers: Promise<readonly HttpField[] | undefined>;
|
|
53
|
+
readonly timeouts: HttpTimeouts;
|
|
54
|
+
/**
|
|
55
|
+
* Aborts when the originating callback is cancelled or the first-byte timeout expires before
|
|
56
|
+
* the response promise settled, and when the host releases the response early (dropped before
|
|
57
|
+
* its end, a body failure, or retirement before its end). A cleanly ended response body does
|
|
58
|
+
* not abort it. Retirement still errors any unfinished upload body and its trailers.
|
|
59
|
+
*/
|
|
60
|
+
readonly signal: AbortSignal;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One response, revision-owned by the host once returned. */
|
|
64
|
+
export interface HttpProviderResponse {
|
|
65
|
+
/** 100..=999. */
|
|
66
|
+
readonly status: number;
|
|
67
|
+
readonly fields: readonly HttpField[];
|
|
68
|
+
/**
|
|
69
|
+
* The response contents, or `null` for none. Empty chunks are allowed. The host reads it only
|
|
70
|
+
* as the guest reads, enforces a declared `Content-Length` (except for `HEAD`, 1xx, 204 and
|
|
71
|
+
* 304), and cancels it once when it releases the response early; that cancel promise is the
|
|
72
|
+
* physical acknowledgement the host waits for.
|
|
73
|
+
*/
|
|
74
|
+
readonly body: ReadableStream<Uint8Array> | null;
|
|
75
|
+
/** Trailers, read after the body ended; absent means none. */
|
|
76
|
+
readonly trailers?: Promise<readonly HttpField[] | undefined>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A host HTTP transport. The promise is physical work: the host tracks it until it settles. */
|
|
80
|
+
export interface HttpProvider {
|
|
81
|
+
send(request: HttpProviderRequest): Promise<HttpProviderResponse>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A transport failure with a standard `error-code`; any other failure maps to `internal-error`. */
|
|
85
|
+
export class HttpProviderError extends Error {
|
|
86
|
+
constructor(
|
|
87
|
+
readonly code: ErrorCode,
|
|
88
|
+
message = `HTTP provider failed: ${code.tag}`,
|
|
89
|
+
) {
|
|
90
|
+
super(message);
|
|
91
|
+
this.name = "HttpProviderError";
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Field values are byte strings; Fetch carries them as one UTF-16 unit per byte. */
|
|
96
|
+
export function byteString(bytes: Uint8Array): string {
|
|
97
|
+
let text = "";
|
|
98
|
+
for (let at = 0; at < bytes.length; at += 8192)
|
|
99
|
+
text += String.fromCharCode(...bytes.subarray(at, at + 8192));
|
|
100
|
+
return text;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function bytesOf(text: string): Uint8Array {
|
|
104
|
+
return Uint8Array.from(text, (unit) => unit.charCodeAt(0) & 0xff);
|
|
105
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { BrowserNetwork, type BrowserNetworkProviders } from "./browser-network.ts";
|
|
2
|
+
import type { FilesystemProvider } from "./filesystem.ts";
|
|
3
|
+
import type { StandardMount } from "./standard-filesystem.ts";
|
|
4
|
+
import type { Grants } from "../authoring/types.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Installed grants remain host capabilities. Filesystem mounts, HTTP and sockets are reached
|
|
8
|
+
* only through the standard WASI host; there is no other host I/O channel.
|
|
9
|
+
*/
|
|
10
|
+
export class BrowserCapabilities {
|
|
11
|
+
readonly #mounts: StandardMount[] = [];
|
|
12
|
+
#preopens: readonly StandardMount[] = [];
|
|
13
|
+
|
|
14
|
+
private constructor(readonly network: BrowserNetwork) {}
|
|
15
|
+
|
|
16
|
+
static async open(
|
|
17
|
+
grants: Grants,
|
|
18
|
+
providers: BrowserNetworkProviders & {
|
|
19
|
+
filesystem?: Readonly<Record<string, FilesystemProvider>>;
|
|
20
|
+
},
|
|
21
|
+
): Promise<BrowserCapabilities> {
|
|
22
|
+
const capabilities = new BrowserCapabilities(new BrowserNetwork(grants, providers));
|
|
23
|
+
try {
|
|
24
|
+
for (const grant of grants.filesystem ?? []) {
|
|
25
|
+
const provider = providers.filesystem?.[grant.provider];
|
|
26
|
+
if (!provider) throw new Error(`Unknown filesystem provider ${grant.provider}`);
|
|
27
|
+
capabilities.#mounts.push({
|
|
28
|
+
path: grant.mount,
|
|
29
|
+
writable: grant.writable,
|
|
30
|
+
filesystem: provider.open(grant.mount, grant.writable),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
capabilities.#preopens = Object.freeze([...capabilities.#mounts]);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
try {
|
|
36
|
+
await capabilities.dispose();
|
|
37
|
+
} catch (disposal) {
|
|
38
|
+
throw new AggregateError(
|
|
39
|
+
[error, disposal],
|
|
40
|
+
"Filesystem initialization and disposal failed",
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
return capabilities;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Mounts in grant order for standard `wasi:filesystem` preopens. */
|
|
49
|
+
get mounts(): readonly StandardMount[] {
|
|
50
|
+
return this.#preopens;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Called after the transport has awaited every physically admitted host operation. */
|
|
54
|
+
async dispose(): Promise<void> {
|
|
55
|
+
const mounts = this.#mounts.splice(0);
|
|
56
|
+
this.#preopens = [];
|
|
57
|
+
const settled = await Promise.allSettled([
|
|
58
|
+
Promise.resolve().then(() => this.network.dispose()),
|
|
59
|
+
...mounts.map(async (mount) => mount.filesystem.dispose()),
|
|
60
|
+
]);
|
|
61
|
+
const errors = settled
|
|
62
|
+
.filter((result) => result.status === "rejected")
|
|
63
|
+
.map((result) => result.reason);
|
|
64
|
+
if (errors.length) throw new AggregateError(errors, "Filesystem disposal failed");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import type { HostOptions, OrbArtifact, OrbDefinition } from "../authoring/types.ts";
|
|
2
|
+
import { Host } from "../host.ts";
|
|
3
|
+
import { isolate, type AdaptedHostOptions } from "#tembra-adapters";
|
|
4
|
+
import { Revision } from "./revision.ts";
|
|
5
|
+
import { decodeAsyncHostRequest, decodeHostRequest } from "./host-protocol.ts";
|
|
6
|
+
import { refusal, reply } from "./codec.ts";
|
|
7
|
+
import { cliPacket } from "./cli.ts";
|
|
8
|
+
import type { IngressExchange } from "./http-ingress.ts";
|
|
9
|
+
import type { HttpField } from "./http-provider.ts";
|
|
10
|
+
import { ingressPacket, NativeIngress, type CarrierEvent } from "./native-ingress.ts";
|
|
11
|
+
import {
|
|
12
|
+
answer,
|
|
13
|
+
query,
|
|
14
|
+
type ContractPort,
|
|
15
|
+
type DirectoryFilesystem,
|
|
16
|
+
type ExecutionScope,
|
|
17
|
+
type IngressDispatch,
|
|
18
|
+
type InstallationPlan,
|
|
19
|
+
type InterpreterPort,
|
|
20
|
+
} from "./types.ts";
|
|
21
|
+
|
|
22
|
+
/** Platform-owned objects retain all native handles behind the mobile facade. */
|
|
23
|
+
export interface MobileArtifact extends ContractPort {
|
|
24
|
+
description(): string;
|
|
25
|
+
free(): void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The native UniFFI incoming HTTP carrier (`begin_ingress`, `receive_ingress`, `supply_request`,
|
|
29
|
+
* `pull_response`, `disconnect`) in lowerCamelCase. Bytes are typed `Uint8Array`s end to end:
|
|
30
|
+
* a bridge that cannot carry binary data (number arrays, base64) must not provide this group.
|
|
31
|
+
*/
|
|
32
|
+
export interface MobileField {
|
|
33
|
+
name: string;
|
|
34
|
+
value: Uint8Array;
|
|
35
|
+
}
|
|
36
|
+
export interface MobileRequestHead {
|
|
37
|
+
method: string;
|
|
38
|
+
/** Present exactly when `authority` is. */
|
|
39
|
+
scheme?: string;
|
|
40
|
+
authority?: string;
|
|
41
|
+
pathWithQuery?: string;
|
|
42
|
+
fields: MobileField[];
|
|
43
|
+
}
|
|
44
|
+
/** The answer to one `requestPull`. */
|
|
45
|
+
export type MobileRequestChunk =
|
|
46
|
+
| { kind: "data"; bytes: Uint8Array }
|
|
47
|
+
/** The end of the request body; `[]` for no trailers. */
|
|
48
|
+
| { kind: "end"; trailers: MobileField[] }
|
|
49
|
+
| { kind: "error"; message: string };
|
|
50
|
+
export type MobileIngressEvent =
|
|
51
|
+
| { kind: "host"; id: number; asynchronous: boolean; packet: string }
|
|
52
|
+
/** The completion envelope JSON; `finish` follows. */
|
|
53
|
+
| { kind: "complete"; result: string }
|
|
54
|
+
| { kind: "head"; status: number; fields: MobileField[] }
|
|
55
|
+
| { kind: "requestPull" }
|
|
56
|
+
| { kind: "data"; bytes: Uint8Array }
|
|
57
|
+
| { kind: "trailers"; fields: MobileField[] }
|
|
58
|
+
| { kind: "end" }
|
|
59
|
+
| { kind: "error"; message: string }
|
|
60
|
+
/** The handler's WIT `error-code` in the standard binary codec; never with a head. */
|
|
61
|
+
| { kind: "refused"; code: Uint8Array };
|
|
62
|
+
/** The whole carrier, or nothing: a port either serves incoming HTTP or does not. */
|
|
63
|
+
export interface MobileIngressCarrier {
|
|
64
|
+
/** Start the exchange; its events then come only from `receiveIngress`. */
|
|
65
|
+
beginIngress(packet: string, head: MobileRequestHead): void;
|
|
66
|
+
/** The single receive loop of the active exchange. */
|
|
67
|
+
receiveIngress(): Promise<MobileIngressEvent>;
|
|
68
|
+
/** `false` when the answer is no longer wanted. */
|
|
69
|
+
supplyRequest(chunk: MobileRequestChunk): boolean;
|
|
70
|
+
pullResponse(): void;
|
|
71
|
+
disconnect(): void;
|
|
72
|
+
}
|
|
73
|
+
export interface MobilePort {
|
|
74
|
+
description(): string;
|
|
75
|
+
begin(packet: string, declaration: boolean): void;
|
|
76
|
+
receive(): Promise<string>;
|
|
77
|
+
reply(id: number, packet: string): boolean;
|
|
78
|
+
finish(): void;
|
|
79
|
+
abort(): Promise<void>;
|
|
80
|
+
release(environment: number): void;
|
|
81
|
+
/** Cancel standard WASI work owned by the active callback. */
|
|
82
|
+
cancel(): void;
|
|
83
|
+
close(): void;
|
|
84
|
+
/**
|
|
85
|
+
* The typed incoming HTTP carrier, absent where the platform bridge cannot carry raw bytes;
|
|
86
|
+
* `Host.handleHttp` is then refused for this port's Orbs.
|
|
87
|
+
*/
|
|
88
|
+
readonly ingress?: MobileIngressCarrier | undefined;
|
|
89
|
+
}
|
|
90
|
+
export interface MobileBackend {
|
|
91
|
+
artifact(bytes: Uint8Array): MobileArtifact;
|
|
92
|
+
/** Open a source or compiled revision; both answer the same scoped host requests. */
|
|
93
|
+
open(
|
|
94
|
+
artifact: MobileArtifact,
|
|
95
|
+
bindings: string,
|
|
96
|
+
grants: string,
|
|
97
|
+
cli: string,
|
|
98
|
+
): Promise<MobilePort>;
|
|
99
|
+
}
|
|
100
|
+
export interface MobileExecutionOptions {
|
|
101
|
+
onLog?: (record: { orb: string; level: string; message: string }) => void;
|
|
102
|
+
providers?: {
|
|
103
|
+
filesystem?: Readonly<Record<string, DirectoryFilesystem>>;
|
|
104
|
+
network?: readonly string[];
|
|
105
|
+
};
|
|
106
|
+
limits?: { memoryBytes?: number; fuel?: number; packetBytes?: number; timeoutMs?: number };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Adapt asynchronous platform delivery while guest factories remain strictly synchronous. */
|
|
110
|
+
export class MobileDriver implements InterpreterPort {
|
|
111
|
+
/** Present only when the port provides the complete typed carrier. */
|
|
112
|
+
declare readonly ingress?: NonNullable<InterpreterPort["ingress"]>;
|
|
113
|
+
constructor(
|
|
114
|
+
readonly port: MobilePort,
|
|
115
|
+
readonly dispose: () => void,
|
|
116
|
+
) {
|
|
117
|
+
const carrier = port.ingress;
|
|
118
|
+
if (carrier)
|
|
119
|
+
this.ingress = (dispatch, exchange, scope) => this.serve(carrier, dispatch, exchange, scope);
|
|
120
|
+
}
|
|
121
|
+
/** Return already checked executable metadata. */
|
|
122
|
+
description(): string {
|
|
123
|
+
return this.port.description();
|
|
124
|
+
}
|
|
125
|
+
/** Drop guest bookkeeping only after the environment has drained. */
|
|
126
|
+
release(environment: number): void {
|
|
127
|
+
this.port.release(environment);
|
|
128
|
+
}
|
|
129
|
+
/** Keep declaration requests attached to their issued factory authority. */
|
|
130
|
+
async declare(packet: string, scope: ExecutionScope): Promise<unknown> {
|
|
131
|
+
return JSON.parse(await this.drive(packet, scope, true));
|
|
132
|
+
}
|
|
133
|
+
/** Retain captured result bytes until the shared contract validator checks them. */
|
|
134
|
+
execute(packet: string, scope: ExecutionScope): Promise<string> {
|
|
135
|
+
return this.drive(packet, scope, false);
|
|
136
|
+
}
|
|
137
|
+
/** Release the native revision and its prepared artifact together. */
|
|
138
|
+
close(): void {
|
|
139
|
+
try {
|
|
140
|
+
this.port.close();
|
|
141
|
+
} finally {
|
|
142
|
+
this.dispose();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Serve one incoming HTTP request over the typed carrier: one receive loop feeds the shared
|
|
148
|
+
* exchange protocol until `complete`; a broken receive ends both bodies, drains admitted host
|
|
149
|
+
* work and quarantines the native exchange, exactly as an ordinary callback does.
|
|
150
|
+
*/
|
|
151
|
+
private async serve(
|
|
152
|
+
carrier: MobileIngressCarrier,
|
|
153
|
+
dispatch: IngressDispatch,
|
|
154
|
+
exchange: IngressExchange,
|
|
155
|
+
scope: ExecutionScope,
|
|
156
|
+
): Promise<void> {
|
|
157
|
+
const port = this.port;
|
|
158
|
+
const exchanged = new NativeIngress(
|
|
159
|
+
{
|
|
160
|
+
supply: (answer) =>
|
|
161
|
+
carrier.supplyRequest(
|
|
162
|
+
answer.kind === "data"
|
|
163
|
+
? { kind: "data", bytes: answer.data }
|
|
164
|
+
: answer.kind === "end"
|
|
165
|
+
? { kind: "end", trailers: answer.trailers.map(mobileField) }
|
|
166
|
+
: answer,
|
|
167
|
+
),
|
|
168
|
+
pull: () => carrier.pullResponse(),
|
|
169
|
+
disconnect: () => carrier.disconnect(),
|
|
170
|
+
cancel: () => port.cancel(),
|
|
171
|
+
reply: (id, packet) => port.reply(id, packet),
|
|
172
|
+
finish: () => port.finish(),
|
|
173
|
+
},
|
|
174
|
+
exchange,
|
|
175
|
+
scope,
|
|
176
|
+
);
|
|
177
|
+
const { method, scheme, authority, pathWithQuery, fields } = exchange.request;
|
|
178
|
+
try {
|
|
179
|
+
carrier.beginIngress(ingressPacket(dispatch), {
|
|
180
|
+
method,
|
|
181
|
+
...(scheme === undefined ? {} : { scheme }),
|
|
182
|
+
...(authority === undefined ? {} : { authority }),
|
|
183
|
+
...(pathWithQuery === undefined ? {} : { pathWithQuery }),
|
|
184
|
+
fields: fields.map(mobileField),
|
|
185
|
+
});
|
|
186
|
+
} catch (error) {
|
|
187
|
+
// Refused before admission: nothing native began.
|
|
188
|
+
await exchanged.abandon(error);
|
|
189
|
+
return exchanged.settled;
|
|
190
|
+
}
|
|
191
|
+
exchanged.begun();
|
|
192
|
+
try {
|
|
193
|
+
for (;;) {
|
|
194
|
+
const received = await carrier.receiveIngress();
|
|
195
|
+
let event: CarrierEvent;
|
|
196
|
+
try {
|
|
197
|
+
event = carrierEvent(received);
|
|
198
|
+
} catch (error) {
|
|
199
|
+
// A malformed frame fails this call (disconnect, cancel); the exchange still completes.
|
|
200
|
+
exchanged.fail(error);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
exchanged.event(event);
|
|
204
|
+
if (event.kind === "complete") break;
|
|
205
|
+
}
|
|
206
|
+
} catch (error) {
|
|
207
|
+
await exchanged.abandon(error);
|
|
208
|
+
const aborted = await Promise.allSettled([port.abort()]);
|
|
209
|
+
if (aborted[0]?.status === "rejected")
|
|
210
|
+
throw new AggregateError([error, aborted[0].reason], "Mobile exchange and cleanup failed");
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
return exchanged.settled;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Drain every admitted host operation before reopening the physical guest ticket. */
|
|
217
|
+
private async drive(
|
|
218
|
+
packet: string,
|
|
219
|
+
scope: ExecutionScope,
|
|
220
|
+
declaration: boolean,
|
|
221
|
+
): Promise<string> {
|
|
222
|
+
this.port.begin(packet, declaration);
|
|
223
|
+
const pending = new Set<Promise<void>>();
|
|
224
|
+
let failure: unknown;
|
|
225
|
+
let finished = false;
|
|
226
|
+
const signal = scope.signal;
|
|
227
|
+
const cancel = () => this.port.cancel();
|
|
228
|
+
const detach = () => signal?.removeEventListener("abort", cancel);
|
|
229
|
+
try {
|
|
230
|
+
// Standard WASI calls stay inside native execution and emit no SDK host request.
|
|
231
|
+
if (signal?.aborted) cancel();
|
|
232
|
+
else signal?.addEventListener("abort", cancel, { once: true });
|
|
233
|
+
for (;;) {
|
|
234
|
+
const event = JSON.parse(await this.port.receive());
|
|
235
|
+
if (event.event === "complete") {
|
|
236
|
+
detach();
|
|
237
|
+
scope.cancel();
|
|
238
|
+
await Promise.allSettled([...pending]);
|
|
239
|
+
this.port.finish();
|
|
240
|
+
finished = true;
|
|
241
|
+
if (failure !== undefined) throw failure;
|
|
242
|
+
const value = answer<unknown>(JSON.stringify(event.result));
|
|
243
|
+
if (typeof value !== "string") throw new Error("Invalid guest result packet");
|
|
244
|
+
return value;
|
|
245
|
+
}
|
|
246
|
+
if (event.event !== "host") throw new Error("Unknown interpreter event");
|
|
247
|
+
const respond = (operation: () => unknown): boolean => {
|
|
248
|
+
let response: string;
|
|
249
|
+
try {
|
|
250
|
+
response = `{"ok":${reply(operation())}}`;
|
|
251
|
+
} catch (error) {
|
|
252
|
+
response = refusal(error);
|
|
253
|
+
}
|
|
254
|
+
return this.port.reply(event.id, response);
|
|
255
|
+
};
|
|
256
|
+
if (!event.asynchronous) respond(() => scope.sync(decodeHostRequest(event.packet)));
|
|
257
|
+
else if (declaration)
|
|
258
|
+
respond(() => {
|
|
259
|
+
throw new Error("Factories cannot perform asynchronous host operations");
|
|
260
|
+
});
|
|
261
|
+
else {
|
|
262
|
+
let request!: ReturnType<typeof decodeAsyncHostRequest>;
|
|
263
|
+
const operation = Promise.resolve()
|
|
264
|
+
.then(() => {
|
|
265
|
+
request = decodeAsyncHostRequest(event.packet);
|
|
266
|
+
return scope.async(request);
|
|
267
|
+
})
|
|
268
|
+
.then(
|
|
269
|
+
(value) => scope.deliver(request, value, (value) => respond(() => value)),
|
|
270
|
+
(error) =>
|
|
271
|
+
void respond(() => {
|
|
272
|
+
throw error;
|
|
273
|
+
}),
|
|
274
|
+
)
|
|
275
|
+
.catch((error) => {
|
|
276
|
+
failure = error;
|
|
277
|
+
});
|
|
278
|
+
pending.add(operation);
|
|
279
|
+
void operation.finally(() => pending.delete(operation));
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (finished) throw error;
|
|
284
|
+
detach();
|
|
285
|
+
scope.cancel();
|
|
286
|
+
const results = await Promise.allSettled([this.port.abort(), ...pending]);
|
|
287
|
+
const abort = results[0];
|
|
288
|
+
if (abort?.status === "rejected")
|
|
289
|
+
throw new AggregateError([error, abort.reason], "Mobile exchange and cleanup failed");
|
|
290
|
+
throw error;
|
|
291
|
+
} finally {
|
|
292
|
+
detach();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function mobileField([name, value]: HttpField): MobileField {
|
|
298
|
+
return { name, value };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Only typed bytes cross; an array or string from a bridge is refused, never converted. */
|
|
302
|
+
function typed(value: unknown): Uint8Array {
|
|
303
|
+
if (!(value instanceof Uint8Array))
|
|
304
|
+
throw new TypeError("the mobile ingress carrier must deliver bytes as Uint8Array");
|
|
305
|
+
return value;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function fieldsOf(value: readonly MobileField[]): readonly HttpField[] {
|
|
309
|
+
if (!Array.isArray(value)) throw new TypeError("mobile ingress fields must be an array");
|
|
310
|
+
return value.map((field) => {
|
|
311
|
+
if (typeof field?.name !== "string")
|
|
312
|
+
throw new TypeError("mobile ingress field names are strings");
|
|
313
|
+
return [field.name, typed(field.value)] as const;
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The UniFFI event in the shared carrier form. */
|
|
318
|
+
function carrierEvent(event: MobileIngressEvent): CarrierEvent {
|
|
319
|
+
switch (event.kind) {
|
|
320
|
+
case "host":
|
|
321
|
+
case "complete":
|
|
322
|
+
case "end":
|
|
323
|
+
case "error":
|
|
324
|
+
return event;
|
|
325
|
+
case "requestPull":
|
|
326
|
+
return { kind: "request-pull" };
|
|
327
|
+
case "head":
|
|
328
|
+
return { kind: "head", status: event.status, fields: fieldsOf(event.fields) };
|
|
329
|
+
case "data":
|
|
330
|
+
return { kind: "data", data: typed(event.bytes) };
|
|
331
|
+
case "trailers":
|
|
332
|
+
return { kind: "trailers", fields: fieldsOf(event.fields) };
|
|
333
|
+
case "refused":
|
|
334
|
+
return { kind: "refused", code: typed(event.code) };
|
|
335
|
+
default:
|
|
336
|
+
throw new TypeError("Unknown mobile ingress event");
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Connect native execution to the existing host without copying lifecycle or permission rules. */
|
|
341
|
+
export function createMobileHost(
|
|
342
|
+
options: HostOptions,
|
|
343
|
+
backend: MobileBackend,
|
|
344
|
+
isolated: MobileExecutionOptions,
|
|
345
|
+
): Host {
|
|
346
|
+
return new Host({
|
|
347
|
+
...options,
|
|
348
|
+
[isolate]: async (definition: OrbArtifact): Promise<OrbDefinition> => {
|
|
349
|
+
const artifact = backend.artifact(definition.artifact);
|
|
350
|
+
try {
|
|
351
|
+
const identity = JSON.parse(artifact.description()) as {
|
|
352
|
+
name: string;
|
|
353
|
+
apiVersion: number;
|
|
354
|
+
kind: string;
|
|
355
|
+
};
|
|
356
|
+
const installation = query<InstallationPlan>(artifact, {
|
|
357
|
+
kind: "install",
|
|
358
|
+
name: identity.name,
|
|
359
|
+
apiVersion: identity.apiVersion,
|
|
360
|
+
requires: definition.requires ?? {},
|
|
361
|
+
grants: definition.grants ?? {},
|
|
362
|
+
filesystem: Object.keys(isolated.providers?.filesystem ?? {}),
|
|
363
|
+
network: isolated.providers?.network ?? [],
|
|
364
|
+
http: isolated.providers?.network ?? [],
|
|
365
|
+
});
|
|
366
|
+
const bindings = JSON.stringify(installation.bindings),
|
|
367
|
+
grants = JSON.stringify(definition.grants ?? {});
|
|
368
|
+
const port = await backend.open(artifact, bindings, grants, cliPacket(definition.cli));
|
|
369
|
+
const driver = new MobileDriver(port, () => artifact.free());
|
|
370
|
+
try {
|
|
371
|
+
return new Revision(driver, artifact, isolated.limits?.timeoutMs ?? 10000).definition(
|
|
372
|
+
identity,
|
|
373
|
+
installation,
|
|
374
|
+
);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
driver.close();
|
|
377
|
+
throw error;
|
|
378
|
+
}
|
|
379
|
+
} catch (error) {
|
|
380
|
+
artifact.free();
|
|
381
|
+
throw error;
|
|
382
|
+
}
|
|
383
|
+
},
|
|
384
|
+
} as AdaptedHostOptions);
|
|
385
|
+
}
|