@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,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed value conversion between Tembra's host domain form and trusted JCO bindings.
|
|
3
|
+
*
|
|
4
|
+
* The host domain form is the checked wire form with WIT 64-bit positions restored as `bigint`
|
|
5
|
+
* (see `wit-shape.ts`): options are `null` or the value, results and variants are `{ tag, val }`,
|
|
6
|
+
* lists are arrays. JCO bindings use `undefined` for an absent option, typed arrays for numeric
|
|
7
|
+
* lists and, for a function's own `result` return, return the ok value or throw `ComponentError`
|
|
8
|
+
* carrying the err payload. Only the shapes JCO and the contract disagree on are rewritten; the
|
|
9
|
+
* prepared Rust contract stays the single WIT validator of every value that reaches the host.
|
|
10
|
+
*/
|
|
11
|
+
type WitType = string | number | null | undefined;
|
|
12
|
+
interface WitKind {
|
|
13
|
+
type?: WitType;
|
|
14
|
+
list?: WitType;
|
|
15
|
+
option?: WitType;
|
|
16
|
+
record?: { fields: { name: string; type: WitType }[] };
|
|
17
|
+
tuple?: { types: WitType[] };
|
|
18
|
+
variant?: { cases: { name: string; type?: WitType }[] };
|
|
19
|
+
result?: { ok?: WitType; err?: WitType };
|
|
20
|
+
enum?: unknown;
|
|
21
|
+
flags?: unknown;
|
|
22
|
+
handle?: { own?: number; borrow?: number };
|
|
23
|
+
stream?: WitType;
|
|
24
|
+
[other: string]: unknown;
|
|
25
|
+
}
|
|
26
|
+
export interface TypeGraph {
|
|
27
|
+
readonly javascriptNames: Readonly<Record<string, string>>;
|
|
28
|
+
readonly types: readonly {
|
|
29
|
+
readonly id: number;
|
|
30
|
+
readonly definition: { kind: WitKind | string; owner?: string; name?: string };
|
|
31
|
+
}[];
|
|
32
|
+
}
|
|
33
|
+
export interface GraphFunction {
|
|
34
|
+
readonly name?: string;
|
|
35
|
+
readonly kind?: string | Readonly<Record<string, number>>;
|
|
36
|
+
readonly params: readonly { readonly name: string; readonly type: WitType }[];
|
|
37
|
+
readonly result?: WitType;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** One direction of conversion; `undefined` means the value is identical in both forms. */
|
|
41
|
+
type Convert = ((value: unknown) => unknown) | undefined;
|
|
42
|
+
interface Pair {
|
|
43
|
+
/** Host domain form to JCO form. */
|
|
44
|
+
readonly guest: Convert;
|
|
45
|
+
/** JCO form to host domain form. */
|
|
46
|
+
readonly host: Convert;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Typed array constructors JCO uses for lists of numeric primitives. */
|
|
50
|
+
const TYPED: Readonly<Record<string, new (values: ArrayLike<never>) => ArrayLike<unknown>>> = {
|
|
51
|
+
u8: Uint8Array,
|
|
52
|
+
s8: Int8Array,
|
|
53
|
+
u16: Uint16Array,
|
|
54
|
+
s16: Int16Array,
|
|
55
|
+
u32: Uint32Array,
|
|
56
|
+
s32: Int32Array,
|
|
57
|
+
u64: BigUint64Array,
|
|
58
|
+
s64: BigInt64Array,
|
|
59
|
+
f32: Float32Array,
|
|
60
|
+
f64: Float64Array,
|
|
61
|
+
} as never;
|
|
62
|
+
|
|
63
|
+
const IDENTITY: Pair = { guest: undefined, host: undefined };
|
|
64
|
+
const apply = (convert: Convert, value: unknown) => (convert ? convert(value) : value);
|
|
65
|
+
|
|
66
|
+
/** Conversions of one function: arguments in both directions and its result. */
|
|
67
|
+
export interface FunctionValues {
|
|
68
|
+
/** Domain arguments to JCO arguments (host calling a guest export). */
|
|
69
|
+
readonly guestArguments: (args: readonly unknown[]) => unknown[];
|
|
70
|
+
/** JCO arguments to domain arguments (guest calling a host import). */
|
|
71
|
+
readonly hostArguments: (args: readonly unknown[]) => unknown[];
|
|
72
|
+
/** Await a JCO export call and answer the domain result (a `result` return is re-tagged). */
|
|
73
|
+
readonly fromGuest: (call: () => unknown) => Promise<unknown>;
|
|
74
|
+
/** Domain result to the value a JCO import implementation returns. */
|
|
75
|
+
readonly toGuest: (value: unknown) => unknown;
|
|
76
|
+
/** Whether the function's own return type is a WIT `result`. */
|
|
77
|
+
readonly tagged: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Compile conversions against one contract graph; compiled types are memoized. */
|
|
81
|
+
export function converters(graph: TypeGraph): (fn: GraphFunction) => FunctionValues {
|
|
82
|
+
const kinds = new Map(graph.types.map(({ id, definition }) => [id, definition.kind]));
|
|
83
|
+
const memo = new Map<number, Pair>();
|
|
84
|
+
const spelling = (name: string) => {
|
|
85
|
+
const value = graph.javascriptNames[name];
|
|
86
|
+
if (value === undefined) throw new Error(`Missing WIT JavaScript name for ${name}`);
|
|
87
|
+
return value;
|
|
88
|
+
};
|
|
89
|
+
const kindOf = (type: WitType): WitKind | string | undefined =>
|
|
90
|
+
typeof type === "number" ? kinds.get(type) : undefined;
|
|
91
|
+
const resolve = (type: WitType): WitType => {
|
|
92
|
+
let current = type;
|
|
93
|
+
for (let depth = 0; typeof current === "number"; depth++) {
|
|
94
|
+
const kind = kinds.get(current);
|
|
95
|
+
if (typeof kind !== "object" || kind.type === undefined || depth > 64) break;
|
|
96
|
+
current = kind.type;
|
|
97
|
+
}
|
|
98
|
+
return current;
|
|
99
|
+
};
|
|
100
|
+
const compile = (type: WitType): Pair => {
|
|
101
|
+
if (typeof type !== "number") return IDENTITY;
|
|
102
|
+
const known = memo.get(type);
|
|
103
|
+
if (known) return known;
|
|
104
|
+
// Recursive types are impossible in WIT; the placeholder only guards malformed graphs.
|
|
105
|
+
memo.set(type, IDENTITY);
|
|
106
|
+
const pair = build(kindOf(type));
|
|
107
|
+
memo.set(type, pair);
|
|
108
|
+
return pair;
|
|
109
|
+
};
|
|
110
|
+
const build = (kind: WitKind | string | undefined): Pair => {
|
|
111
|
+
if (kind === undefined) throw new Error("Unknown WIT type reference");
|
|
112
|
+
if (typeof kind === "string")
|
|
113
|
+
throw new Error(`WIT ${kind} values are unavailable in browser component execution`);
|
|
114
|
+
if (kind.type !== undefined) return compile(kind.type);
|
|
115
|
+
if (kind.enum !== undefined || kind.flags !== undefined || kind.handle?.own !== undefined)
|
|
116
|
+
return IDENTITY;
|
|
117
|
+
if (kind.list !== undefined) {
|
|
118
|
+
const element = resolve(kind.list);
|
|
119
|
+
const Typed = typeof element === "string" ? TYPED[element] : undefined;
|
|
120
|
+
const item = compile(kind.list);
|
|
121
|
+
return {
|
|
122
|
+
guest: (value) => {
|
|
123
|
+
if (!Array.isArray(value)) throw new TypeError("WIT list requires an array");
|
|
124
|
+
const items = item.guest ? value.map(item.guest) : value;
|
|
125
|
+
return Typed ? new Typed(items as never) : items;
|
|
126
|
+
},
|
|
127
|
+
host: (value) => {
|
|
128
|
+
const items = ArrayBuffer.isView(value)
|
|
129
|
+
? Array.from(value as unknown as ArrayLike<unknown>)
|
|
130
|
+
: Array.isArray(value)
|
|
131
|
+
? value
|
|
132
|
+
: (() => {
|
|
133
|
+
throw new TypeError("WIT list requires an array");
|
|
134
|
+
})();
|
|
135
|
+
return item.host ? items.map(item.host) : [...items];
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (kind.option !== undefined) {
|
|
140
|
+
const inner = compile(kind.option);
|
|
141
|
+
const innerKind = kindOf(resolve(kind.option));
|
|
142
|
+
// JCO spells a nested option's outer level as a tagged variant.
|
|
143
|
+
const nested = typeof innerKind === "object" && innerKind.option !== undefined;
|
|
144
|
+
return {
|
|
145
|
+
guest: (value) =>
|
|
146
|
+
value === null
|
|
147
|
+
? nested
|
|
148
|
+
? { tag: "none" }
|
|
149
|
+
: undefined
|
|
150
|
+
: nested
|
|
151
|
+
? { tag: "some", val: apply(inner.guest, value) }
|
|
152
|
+
: apply(inner.guest, value),
|
|
153
|
+
host: (value) => {
|
|
154
|
+
if (nested) {
|
|
155
|
+
const tagged = value as { tag?: string; val?: unknown };
|
|
156
|
+
return tagged?.tag === "some" ? apply(inner.host, tagged.val) : null;
|
|
157
|
+
}
|
|
158
|
+
return value === undefined || value === null ? null : apply(inner.host, value);
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (kind.record) {
|
|
163
|
+
const fields = kind.record.fields.map(
|
|
164
|
+
({ name, type }) => [spelling(name), compile(type)] as const,
|
|
165
|
+
);
|
|
166
|
+
if (fields.every(([, pair]) => pair === IDENTITY)) return IDENTITY;
|
|
167
|
+
const map = (side: "guest" | "host") => (value: unknown) => {
|
|
168
|
+
if (typeof value !== "object" || value === null)
|
|
169
|
+
throw new TypeError("WIT record requires an object");
|
|
170
|
+
const copy: Record<string, unknown> = { ...(value as object) };
|
|
171
|
+
for (const [name, pair] of fields)
|
|
172
|
+
if (Object.hasOwn(copy, name)) copy[name] = apply(pair[side], copy[name]);
|
|
173
|
+
return copy;
|
|
174
|
+
};
|
|
175
|
+
return { guest: map("guest"), host: map("host") };
|
|
176
|
+
}
|
|
177
|
+
if (kind.tuple) {
|
|
178
|
+
const items = kind.tuple.types.map(compile);
|
|
179
|
+
if (items.every((pair) => pair === IDENTITY)) return IDENTITY;
|
|
180
|
+
const map = (side: "guest" | "host") => (value: unknown) => {
|
|
181
|
+
if (!Array.isArray(value) || value.length !== items.length)
|
|
182
|
+
throw new TypeError("WIT tuple length differs");
|
|
183
|
+
return value.map((item, index) => apply(items[index]![side], item));
|
|
184
|
+
};
|
|
185
|
+
return { guest: map("guest"), host: map("host") };
|
|
186
|
+
}
|
|
187
|
+
if (kind.variant || kind.result) {
|
|
188
|
+
const cases = new Map(
|
|
189
|
+
kind.variant
|
|
190
|
+
? kind.variant.cases.map(({ name, type }) => [name, type] as const)
|
|
191
|
+
: ([
|
|
192
|
+
["ok", kind.result!.ok],
|
|
193
|
+
["err", kind.result!.err],
|
|
194
|
+
] as const),
|
|
195
|
+
);
|
|
196
|
+
const pairs = new Map(
|
|
197
|
+
[...cases].map(([name, type]) => [name, type == null ? undefined : compile(type)]),
|
|
198
|
+
);
|
|
199
|
+
const map = (side: "guest" | "host") => (value: unknown) => {
|
|
200
|
+
if (typeof value !== "object" || value === null)
|
|
201
|
+
throw new TypeError("WIT tagged value requires an object");
|
|
202
|
+
const { tag, val } = value as { tag?: unknown; val?: unknown };
|
|
203
|
+
if (typeof tag !== "string" || !pairs.has(tag)) throw new TypeError("Unknown WIT case");
|
|
204
|
+
const pair = pairs.get(tag);
|
|
205
|
+
// A payload-less case carries no `val` in the host form, whatever JCO attached.
|
|
206
|
+
return pair === undefined ? { tag } : { tag, val: apply(pair[side], val) };
|
|
207
|
+
};
|
|
208
|
+
return { guest: map("guest"), host: map("host") };
|
|
209
|
+
}
|
|
210
|
+
const name = Object.keys(kind)[0] ?? "unknown";
|
|
211
|
+
throw new Error(`WIT ${name} values are unavailable in browser component execution`);
|
|
212
|
+
};
|
|
213
|
+
return (fn) => {
|
|
214
|
+
const params = fn.params.map(({ type }) => compile(type));
|
|
215
|
+
const result = compile(fn.result);
|
|
216
|
+
const resultKind = kindOf(resolve(fn.result));
|
|
217
|
+
const tagged = typeof resultKind === "object" && resultKind.result !== undefined;
|
|
218
|
+
const tags = tagged ? (resultKind as WitKind).result! : undefined;
|
|
219
|
+
const ok = tags?.ok == null ? undefined : compile(tags.ok);
|
|
220
|
+
const err = tags?.err == null ? undefined : compile(tags.err);
|
|
221
|
+
const each = (side: "guest" | "host") => (args: readonly unknown[]) => {
|
|
222
|
+
if (args.length !== params.length) throw new TypeError("WIT argument count differs");
|
|
223
|
+
return args.map((value, index) => apply(params[index]![side], value));
|
|
224
|
+
};
|
|
225
|
+
return {
|
|
226
|
+
tagged,
|
|
227
|
+
guestArguments: each("guest"),
|
|
228
|
+
hostArguments: each("host"),
|
|
229
|
+
fromGuest: async (call) => {
|
|
230
|
+
if (!tagged) {
|
|
231
|
+
const value = await call();
|
|
232
|
+
return fn.result == null ? null : apply(result.host, value);
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
const value = await call();
|
|
236
|
+
return tags!.ok == null ? { tag: "ok" } : { tag: "ok", val: apply(ok?.host, value) };
|
|
237
|
+
} catch (error) {
|
|
238
|
+
// Only JCO's typed err lane is a business error; anything else is a trap.
|
|
239
|
+
if (error === null || typeof error !== "object" || !Object.hasOwn(error, "payload"))
|
|
240
|
+
throw error;
|
|
241
|
+
const payload = (error as { payload: unknown }).payload;
|
|
242
|
+
return tags!.err == null
|
|
243
|
+
? { tag: "err" }
|
|
244
|
+
: { tag: "err", val: apply(err?.host, payload) };
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
toGuest: (value) => {
|
|
248
|
+
if (fn.result == null) return undefined;
|
|
249
|
+
// JCO accepts an explicit `{ tag, val }` for a `result` return, which is never ambiguous.
|
|
250
|
+
return apply(result.guest, value);
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
}
|