@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,702 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One installed compiled component revision, adapted to ordinary SDK Orb definitions.
|
|
3
|
+
*
|
|
4
|
+
* The checked contract plans services, hooks, contributions and incoming HTTP exactly as for
|
|
5
|
+
* native hosts; a compiled component is described from its contract and its artifact's roles
|
|
6
|
+
* (contributions, declared hooks, service lifetimes). Every guest entry is one callback admitted
|
|
7
|
+
* by the revision gate and served by the component port, whose instrumented task observation
|
|
8
|
+
* keeps admission and import attribution until physical completion. Kernel state, root resources,
|
|
9
|
+
* declared hooks and the call's context are served by `kernel.ts`.
|
|
10
|
+
*/
|
|
11
|
+
import type { Declaration } from "../../authoring/declaration.ts";
|
|
12
|
+
import type { Contribution, OrbDefinition, ServiceDefinition } from "../../authoring/types.ts";
|
|
13
|
+
import type { Lifetime } from "../../bridge/identifiers.ts";
|
|
14
|
+
import { KernelOp } from "../../bridge/protocol.ts";
|
|
15
|
+
import {
|
|
16
|
+
admitFactory,
|
|
17
|
+
bindCaptured,
|
|
18
|
+
declareFactory,
|
|
19
|
+
disposeOrb,
|
|
20
|
+
isolatedRevision,
|
|
21
|
+
releaseEnvironment,
|
|
22
|
+
signature,
|
|
23
|
+
signatures,
|
|
24
|
+
validateEnvironment,
|
|
25
|
+
type CapturedDelivery,
|
|
26
|
+
type CapturedArguments,
|
|
27
|
+
type ResourceCapability,
|
|
28
|
+
type ResourceReceiver,
|
|
29
|
+
type AdaptedOrb,
|
|
30
|
+
type AdaptedService,
|
|
31
|
+
} from "#tembra-adapters";
|
|
32
|
+
import { Context } from "../../runtime/context.ts";
|
|
33
|
+
import type { Environment } from "../../runtime/records.ts";
|
|
34
|
+
import { check, checkedResult, encode, VALUE_LIMIT, Encoded } from "../codec.ts";
|
|
35
|
+
import { Gate } from "../gate.ts";
|
|
36
|
+
import { HTTP_HANDLER, IngressExchange } from "../http-ingress.ts";
|
|
37
|
+
import { declaredHooks } from "../revision.ts";
|
|
38
|
+
import {
|
|
39
|
+
query,
|
|
40
|
+
type CheckedImplementation,
|
|
41
|
+
type ContractPort,
|
|
42
|
+
type Description,
|
|
43
|
+
type InstallationPlan,
|
|
44
|
+
type ReferenceBinding,
|
|
45
|
+
} from "../types.ts";
|
|
46
|
+
import { lift, shapes, type Signature, type WitFunction, type WitTypes } from "../wit-shape.ts";
|
|
47
|
+
import {
|
|
48
|
+
ResourceOwner,
|
|
49
|
+
resourceRoles,
|
|
50
|
+
resourceDataFunction,
|
|
51
|
+
resourceExport,
|
|
52
|
+
type ResourceRoles,
|
|
53
|
+
} from "./resources.ts";
|
|
54
|
+
import { issueByteStream } from "./streams.ts";
|
|
55
|
+
import { viewMethod, importedResource } from "./host.ts";
|
|
56
|
+
import { ComponentKernel, isKernel, type LogObserver } from "./kernel.ts";
|
|
57
|
+
import type { ComponentPort } from "./port.ts";
|
|
58
|
+
import { ComponentScope, type BoundMethod, type CallbackScope, type ScopeOwner } from "./scope.ts";
|
|
59
|
+
import { converters, type FunctionValues, type GraphFunction, type TypeGraph } from "./values.ts";
|
|
60
|
+
import type { IngressBridge } from "./wasi-http.ts";
|
|
61
|
+
|
|
62
|
+
/** The contract's `describe` answer, as far as component execution reads it. */
|
|
63
|
+
export type Described = WitTypes &
|
|
64
|
+
TypeGraph & {
|
|
65
|
+
imports: {
|
|
66
|
+
binding: string;
|
|
67
|
+
contract?: string;
|
|
68
|
+
type_only?: boolean;
|
|
69
|
+
functions?: Record<string, WitFunction>;
|
|
70
|
+
}[];
|
|
71
|
+
exports: {
|
|
72
|
+
binding: string;
|
|
73
|
+
contract?: string;
|
|
74
|
+
functions?: Record<string, WitFunction>;
|
|
75
|
+
}[];
|
|
76
|
+
/** Checked reference views; artifact metadata alone declares them. */
|
|
77
|
+
references: Readonly<Record<string, ReferenceBinding>>;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The roles a compiled artifact's metadata gives its exports, by interface identity: the hooks
|
|
82
|
+
* it contributes to with their priority, the hooks it declares with their composition, and the
|
|
83
|
+
* lifetime and linger of its services' objects.
|
|
84
|
+
*/
|
|
85
|
+
export interface CompiledRoles {
|
|
86
|
+
readonly contributions: Readonly<Record<string, number>>;
|
|
87
|
+
readonly hooks?: Readonly<Record<string, "series" | "waterfall" | "bail">>;
|
|
88
|
+
readonly services?: Readonly<Record<string, { lifetime: Lifetime; linger_ms: number }>>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A compiled component's description: one service per callable export interface, with the
|
|
93
|
+
* lifetime and linger its policy names, and its declared hooks under their export's alias, as
|
|
94
|
+
* the native executor describes it. Contribution callbacks and the kernel's own callbacks
|
|
95
|
+
* (`tembra:guest`) are answered to their callers, never offered as services.
|
|
96
|
+
*/
|
|
97
|
+
export function compiledDescription(
|
|
98
|
+
graph: Described,
|
|
99
|
+
identity: { name: string; apiVersion: number },
|
|
100
|
+
roles: CompiledRoles,
|
|
101
|
+
): Description {
|
|
102
|
+
const services: Description["services"] = {};
|
|
103
|
+
const hooks: NonNullable<Description["hooks"]> = {};
|
|
104
|
+
const { contributions, hooks: declared = {}, services: policies = {} } = roles;
|
|
105
|
+
const spelling = (name: string) => {
|
|
106
|
+
const value = graph.javascriptNames[name];
|
|
107
|
+
if (value === undefined) throw new Error(`Missing WIT JavaScript name for ${name}`);
|
|
108
|
+
return value;
|
|
109
|
+
};
|
|
110
|
+
for (const entry of graph.exports) {
|
|
111
|
+
if (!entry.contract || !entry.functions || !Object.keys(entry.functions).length) continue;
|
|
112
|
+
const name = entry.contract.slice(entry.contract.indexOf("/") + 1).split("@")[0]!;
|
|
113
|
+
if (Object.hasOwn(declared, entry.contract)) {
|
|
114
|
+
hooks[spelling(name)] = declared[entry.contract]!;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (Object.hasOwn(contributions, entry.contract) || isKernel(entry.contract)) continue;
|
|
118
|
+
const policy = Object.hasOwn(policies, entry.contract) ? policies[entry.contract] : undefined;
|
|
119
|
+
services[spelling(name)] = {
|
|
120
|
+
methods: Object.keys(entry.functions).map(spelling),
|
|
121
|
+
...(policy ? { lifetime: policy.lifetime, linger: policy.linger_ms } : {}),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
name: identity.name,
|
|
126
|
+
apiVersion: identity.apiVersion,
|
|
127
|
+
services,
|
|
128
|
+
hooks,
|
|
129
|
+
contributes: { ...contributions },
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The admitted call's execution context for one guest entry. */
|
|
134
|
+
function entered(context: Context): Context {
|
|
135
|
+
return new Context(
|
|
136
|
+
context.session,
|
|
137
|
+
context.root,
|
|
138
|
+
context.invocation,
|
|
139
|
+
context.session.kernel.command(KernelOp.EnterExecution, {
|
|
140
|
+
call: context.invocation!.id,
|
|
141
|
+
context: context.frame || undefined,
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export class ComponentRevision implements ScopeOwner {
|
|
147
|
+
readonly gate = new Gate();
|
|
148
|
+
readonly imports = new Map<string, string>();
|
|
149
|
+
readonly importSignatures = new Map<string, string>();
|
|
150
|
+
readonly importMethods = new Map<string, Map<string, BoundMethod>>();
|
|
151
|
+
readonly views = new Map<string, Map<string, BoundMethod>>();
|
|
152
|
+
readonly signatures = new Map<number, Signature>();
|
|
153
|
+
readonly scopes = new Map<number, ComponentScope>();
|
|
154
|
+
readonly importedResources = new Set<ResourceCapability>();
|
|
155
|
+
readonly #issuedResources = new Map<number, Set<() => void>>();
|
|
156
|
+
/** The instance's incoming-HTTP bridge from the HTTP adapter (`wasi.ts`). */
|
|
157
|
+
ingressBridge: IngressBridge | undefined;
|
|
158
|
+
/** The kernel's interfaces (`tembra:guest`) and the hooks this Orb declares. */
|
|
159
|
+
readonly kernel: ComponentKernel;
|
|
160
|
+
#closed = false;
|
|
161
|
+
|
|
162
|
+
constructor(
|
|
163
|
+
readonly port: ComponentPort,
|
|
164
|
+
readonly contract: ContractPort,
|
|
165
|
+
readonly graph: Described,
|
|
166
|
+
readonly timeout: number,
|
|
167
|
+
/** The Orb's name and where its `tembra:guest/context` log records go. */
|
|
168
|
+
log: { readonly orb: string; readonly observer?: LogObserver } = { orb: "" },
|
|
169
|
+
) {
|
|
170
|
+
this.kernel = new ComponentKernel(this, log.orb, log.observer);
|
|
171
|
+
port.wasi.retired.addEventListener(
|
|
172
|
+
"abort",
|
|
173
|
+
() => {
|
|
174
|
+
for (const owner of this.importedResources) owner.close();
|
|
175
|
+
for (const owners of this.#issuedResources.values()) for (const retire of owners) retire();
|
|
176
|
+
},
|
|
177
|
+
{ once: true },
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Call one typed export under an admitted callback and answer checked result bytes. */
|
|
182
|
+
private async invoke(
|
|
183
|
+
key: string,
|
|
184
|
+
spelling: string,
|
|
185
|
+
values: FunctionValues,
|
|
186
|
+
slot: number,
|
|
187
|
+
shape: Signature,
|
|
188
|
+
environment: ComponentScope,
|
|
189
|
+
scope: CallbackScope,
|
|
190
|
+
input: Encoded,
|
|
191
|
+
roles: ResourceRoles = {},
|
|
192
|
+
receiver?: ResourceReceiver,
|
|
193
|
+
retain?: (physical: Promise<void>) => void,
|
|
194
|
+
arguments_?: CapturedArguments,
|
|
195
|
+
): Promise<Encoded | CapturedDelivery> {
|
|
196
|
+
const unlock = await this.gate.enter(undefined, scope.signal);
|
|
197
|
+
let released = false;
|
|
198
|
+
const release = () => {
|
|
199
|
+
if (released) return;
|
|
200
|
+
released = true;
|
|
201
|
+
unlock();
|
|
202
|
+
};
|
|
203
|
+
let continued = false;
|
|
204
|
+
const issued: ResourceOwner[] = [];
|
|
205
|
+
try {
|
|
206
|
+
if (!!roles.receiver !== !!receiver)
|
|
207
|
+
throw new Error("Resource receiver differs from signature");
|
|
208
|
+
const borrowed = roles.receiver && receiver?.redeem(this, environment.id, roles.receiver);
|
|
209
|
+
const checkedInput = check(this.contract, slot, true, input);
|
|
210
|
+
let args = lift(shape.params, JSON.parse(checkedInput.text)) as unknown[];
|
|
211
|
+
const consumed = new Set<number>();
|
|
212
|
+
const uniqueOwners = new Set<ResourceCapability>();
|
|
213
|
+
const owners = arguments_?.owners ?? [];
|
|
214
|
+
const deliveries: {
|
|
215
|
+
owner: ResourceCapability;
|
|
216
|
+
nominal: import("./resources.ts").Nominal;
|
|
217
|
+
wrap?: (owner: ResourceCapability) => object;
|
|
218
|
+
}[] = [];
|
|
219
|
+
if (roles.arguments) {
|
|
220
|
+
if (!arguments_) throw new Error("Owned arguments require a private transfer");
|
|
221
|
+
if (arguments_.accepted) throw new Error("Ownership transfer was already accepted");
|
|
222
|
+
args = roles.arguments(args, (nominal, index) => {
|
|
223
|
+
if (
|
|
224
|
+
typeof index !== "number" ||
|
|
225
|
+
!Number.isInteger(index) ||
|
|
226
|
+
index < 0 ||
|
|
227
|
+
consumed.has(index)
|
|
228
|
+
)
|
|
229
|
+
throw new Error("An owned argument must redeem each attachment exactly once");
|
|
230
|
+
const owner = owners[index];
|
|
231
|
+
if (
|
|
232
|
+
!owner ||
|
|
233
|
+
owner.nominal.interface !== nominal.interface ||
|
|
234
|
+
owner.nominal.name !== nominal.name
|
|
235
|
+
)
|
|
236
|
+
throw new Error("Resource argument has another nominal type");
|
|
237
|
+
if (uniqueOwners.has(owner)) throw new Error("Duplicate owned argument attachment");
|
|
238
|
+
uniqueOwners.add(owner);
|
|
239
|
+
const own = owner.issuedBy(this);
|
|
240
|
+
const wrap = own ? undefined : importedResource(this.port, nominal).wrap;
|
|
241
|
+
consumed.add(index);
|
|
242
|
+
deliveries.push({ owner, nominal, ...(wrap ? { wrap } : {}) });
|
|
243
|
+
return deliveries.length - 1;
|
|
244
|
+
}) as unknown[];
|
|
245
|
+
}
|
|
246
|
+
if (consumed.size !== owners.length) throw new Error("Unused argument capability attachment");
|
|
247
|
+
const guestArgs = values.guestArguments(args);
|
|
248
|
+
const call = () => {
|
|
249
|
+
const exports = this.port.exports?.[key];
|
|
250
|
+
if (!exports) throw new Error(`The component does not export ${key}`);
|
|
251
|
+
return resourceExport(
|
|
252
|
+
exports,
|
|
253
|
+
spelling,
|
|
254
|
+
roles,
|
|
255
|
+
borrowed || undefined,
|
|
256
|
+
guestArgs,
|
|
257
|
+
arguments_
|
|
258
|
+
? () => {
|
|
259
|
+
arguments_.accept();
|
|
260
|
+
return roles.arguments
|
|
261
|
+
? (roles.arguments(guestArgs, (_nominal, index) => {
|
|
262
|
+
const delivery = deliveries[index as number]!;
|
|
263
|
+
if (!delivery.wrap) return delivery.owner.take(this, delivery.nominal);
|
|
264
|
+
this.importedResources.add(delivery.owner);
|
|
265
|
+
delivery.owner.whenClosed(() =>
|
|
266
|
+
this.importedResources.delete(delivery.owner),
|
|
267
|
+
);
|
|
268
|
+
return delivery.wrap(delivery.owner);
|
|
269
|
+
}) as unknown[])
|
|
270
|
+
: guestArgs;
|
|
271
|
+
}
|
|
272
|
+
: undefined,
|
|
273
|
+
);
|
|
274
|
+
};
|
|
275
|
+
if (roles.stream) {
|
|
276
|
+
const task = this.port.start(BigInt(environment.id), scope, call);
|
|
277
|
+
retain?.(task.physical);
|
|
278
|
+
const finished = task.finished.finally(() => {
|
|
279
|
+
scope.cancel();
|
|
280
|
+
release();
|
|
281
|
+
});
|
|
282
|
+
continued = true;
|
|
283
|
+
let owner: import("./streams.ts").ByteStreamOwner | undefined;
|
|
284
|
+
try {
|
|
285
|
+
const raw = await task.result;
|
|
286
|
+
owner = issueByteStream(raw, finished, scope.signal ?? new AbortController().signal);
|
|
287
|
+
const value = check(this.contract, slot, false, encode(0));
|
|
288
|
+
// Publication releases only the exclusive entry turn. Its task, imports, scope,
|
|
289
|
+
// deadline and active-group fuel remain owned until physical completion.
|
|
290
|
+
task.park();
|
|
291
|
+
release();
|
|
292
|
+
return { value, capabilities: [owner], finished };
|
|
293
|
+
} catch (error) {
|
|
294
|
+
owner?.close();
|
|
295
|
+
await finished.catch(() => undefined);
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const result = await this.port.run(
|
|
300
|
+
BigInt(environment.id),
|
|
301
|
+
scope,
|
|
302
|
+
async () => {
|
|
303
|
+
const result = await values.fromGuest(call).catch((error: unknown) => {
|
|
304
|
+
if (arguments_?.accepted) this.port.poison(error);
|
|
305
|
+
throw error;
|
|
306
|
+
});
|
|
307
|
+
if (!roles.result) return result;
|
|
308
|
+
return roles.result(result, (nominal, raw) => {
|
|
309
|
+
if (typeof raw !== "object" || raw === null)
|
|
310
|
+
throw new Error("Owned result is not a resource");
|
|
311
|
+
const owner = new ResourceOwner(nominal, this, environment.id, raw, () => {
|
|
312
|
+
return this.port.cleanup(async () => {
|
|
313
|
+
const unlock = await this.gate.enter();
|
|
314
|
+
try {
|
|
315
|
+
if (this.scopes.has(environment.id))
|
|
316
|
+
await this.port.destroy(() => {
|
|
317
|
+
const dispose = (raw as Disposable)[Symbol.dispose];
|
|
318
|
+
if (typeof dispose !== "function")
|
|
319
|
+
throw new Error("Resource has no destructor");
|
|
320
|
+
dispose.call(raw);
|
|
321
|
+
});
|
|
322
|
+
} finally {
|
|
323
|
+
unlock();
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
let owners = this.#issuedResources.get(environment.id);
|
|
328
|
+
if (!owners) this.#issuedResources.set(environment.id, (owners = new Set()));
|
|
329
|
+
const retire = owner.retirement();
|
|
330
|
+
owners.add(retire);
|
|
331
|
+
owner.whenRetired(() => owners.delete(retire));
|
|
332
|
+
issued.push(owner);
|
|
333
|
+
return issued.length - 1;
|
|
334
|
+
});
|
|
335
|
+
},
|
|
336
|
+
retain,
|
|
337
|
+
);
|
|
338
|
+
const value = check(this.contract, slot, false, encode(result, VALUE_LIMIT, shape.result));
|
|
339
|
+
return roles.owned ? { value, capabilities: issued } : value;
|
|
340
|
+
} catch (error) {
|
|
341
|
+
for (const owner of issued) owner.close();
|
|
342
|
+
throw error;
|
|
343
|
+
} finally {
|
|
344
|
+
if (!continued) {
|
|
345
|
+
scope.cancel();
|
|
346
|
+
release();
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* The host-local incoming HTTP service of a `wasi:http/handler` export. It has no contract
|
|
353
|
+
* signature, so no WIT dependency can bind it, and its method accepts only an exchange issued by
|
|
354
|
+
* `Host.handleHttp`. One request is one admitted callback: the response head is published as
|
|
355
|
+
* soon as the handler returns it, while the callback stays admitted until the task's real exit
|
|
356
|
+
* and the end of the response delivery.
|
|
357
|
+
*/
|
|
358
|
+
private ingress(
|
|
359
|
+
contract: string,
|
|
360
|
+
description: Description,
|
|
361
|
+
create: (service: string) => (declaration: Declaration) => Promise<ComponentScope>,
|
|
362
|
+
admit: () => Promise<() => void>,
|
|
363
|
+
): ServiceDefinition {
|
|
364
|
+
if (contract !== HTTP_HANDLER) throw new Error(`Unsupported incoming HTTP export ${contract}`);
|
|
365
|
+
const service = this.graph.javascriptNames["handler"];
|
|
366
|
+
const method = this.graph.javascriptNames["handle"];
|
|
367
|
+
if (!service || !method) throw new Error("Missing prepared incoming HTTP handler names");
|
|
368
|
+
const declared = description.services[service];
|
|
369
|
+
if (!declared?.methods.includes(method))
|
|
370
|
+
throw new Error(`The Orb exports ${HTTP_HANDLER} without a ${service}.${method} handler`);
|
|
371
|
+
return {
|
|
372
|
+
[declareFactory]: create(service),
|
|
373
|
+
lifetime: declared.lifetime,
|
|
374
|
+
linger: declared.linger,
|
|
375
|
+
[admitFactory]: admit,
|
|
376
|
+
methods: {
|
|
377
|
+
handle: async (
|
|
378
|
+
environment: ComponentScope,
|
|
379
|
+
context: Context,
|
|
380
|
+
value: unknown,
|
|
381
|
+
): Promise<void> => {
|
|
382
|
+
const exchange = IngressExchange.claim(value);
|
|
383
|
+
const bridge = this.ingressBridge;
|
|
384
|
+
if (!bridge) throw new Error("This browser host has no incoming HTTP bridge");
|
|
385
|
+
const scope = environment.exchange(entered(context));
|
|
386
|
+
let failure: unknown = new Error("the HTTP handler callback ended");
|
|
387
|
+
let physical: Promise<void> | undefined;
|
|
388
|
+
const release = await this.gate.enter(undefined, scope.signal);
|
|
389
|
+
try {
|
|
390
|
+
const task = this.port.start(BigInt(environment.id), scope, async () => {
|
|
391
|
+
const handle = this.port.exports?.[HTTP_HANDLER]?.[method];
|
|
392
|
+
if (typeof handle !== "function")
|
|
393
|
+
throw new Error(`The component does not export ${HTTP_HANDLER}#${method}`);
|
|
394
|
+
try {
|
|
395
|
+
return { ok: await handle(bridge.request(exchange.own())) };
|
|
396
|
+
} catch (error) {
|
|
397
|
+
// The handler's typed `err` lane refuses the request; anything else is a trap.
|
|
398
|
+
if (error === null || typeof error !== "object" || !Object.hasOwn(error, "payload"))
|
|
399
|
+
throw error;
|
|
400
|
+
return { err: (error as { payload: unknown }).payload };
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
physical = task.physical;
|
|
404
|
+
const { result, finished } = task;
|
|
405
|
+
let delivery: Promise<void> | undefined;
|
|
406
|
+
try {
|
|
407
|
+
const outcome = await result;
|
|
408
|
+
if ("ok" in outcome) delivery = bridge.respond(outcome.ok, exchange);
|
|
409
|
+
else exchange.refuse(outcome.err as never);
|
|
410
|
+
} finally {
|
|
411
|
+
await finished;
|
|
412
|
+
}
|
|
413
|
+
await delivery;
|
|
414
|
+
failure = undefined;
|
|
415
|
+
} catch (error) {
|
|
416
|
+
failure = error;
|
|
417
|
+
throw error;
|
|
418
|
+
} finally {
|
|
419
|
+
exchange.settle(failure);
|
|
420
|
+
scope.cancel();
|
|
421
|
+
await physical?.catch(() => undefined);
|
|
422
|
+
release();
|
|
423
|
+
}
|
|
424
|
+
},
|
|
425
|
+
},
|
|
426
|
+
} as AdaptedService;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** Build callback definitions from the checked plan. */
|
|
430
|
+
definition(
|
|
431
|
+
identity: Pick<OrbDefinition, "name" | "apiVersion">,
|
|
432
|
+
installation: InstallationPlan,
|
|
433
|
+
description: Description,
|
|
434
|
+
): OrbDefinition {
|
|
435
|
+
const checked = query<CheckedImplementation>(this.contract, {
|
|
436
|
+
kind: "implementation",
|
|
437
|
+
name: identity.name,
|
|
438
|
+
apiVersion: identity.apiVersion,
|
|
439
|
+
value: description,
|
|
440
|
+
bindings: installation.bindings,
|
|
441
|
+
});
|
|
442
|
+
const { plan } = checked;
|
|
443
|
+
const graph = this.graph;
|
|
444
|
+
const compile = shapes(graph);
|
|
445
|
+
const convert = converters(graph);
|
|
446
|
+
const names = graph.javascriptNames;
|
|
447
|
+
const exported = (contract: string, method: string): WitFunction => {
|
|
448
|
+
const entry = graph.exports.find((item) => item.contract === contract);
|
|
449
|
+
const found = Object.entries(entry?.functions ?? {}).find(([name]) => names[name] === method);
|
|
450
|
+
if (!found) throw new Error(`Missing prepared signature for ${contract}#${method}`);
|
|
451
|
+
return found[1];
|
|
452
|
+
};
|
|
453
|
+
// A declared hook runs its plan through the kernel, its values checked by its callback.
|
|
454
|
+
this.kernel.declareHooks(checked.hooks.hooks, (hook) =>
|
|
455
|
+
compile(exported(hook.name, hook.method)),
|
|
456
|
+
);
|
|
457
|
+
for (const [binding, service] of Object.entries(installation.importServices)) {
|
|
458
|
+
if (checked.hooks.contributions.some((contribution) => contribution.binding === binding))
|
|
459
|
+
continue;
|
|
460
|
+
this.imports.set(binding, service);
|
|
461
|
+
const provider = installation.bindings[binding];
|
|
462
|
+
const requirement =
|
|
463
|
+
provider?.kind === "orb"
|
|
464
|
+
? plan.orb.requires.find((item) => item.orb === provider.name)
|
|
465
|
+
: undefined;
|
|
466
|
+
const signature_ =
|
|
467
|
+
requirement && Object.hasOwn(requirement.signatures, service)
|
|
468
|
+
? requirement.signatures[service]
|
|
469
|
+
: undefined;
|
|
470
|
+
if (signature_) this.importSignatures.set(binding, signature_);
|
|
471
|
+
const selected = graph.imports.find((item) => item.binding === binding);
|
|
472
|
+
if (!selected?.functions) throw new Error("Missing prepared import signatures");
|
|
473
|
+
const view = graph.references[binding];
|
|
474
|
+
if (view) {
|
|
475
|
+
// Only checked methods are callable; the acquisition result is never data.
|
|
476
|
+
const methods = new Map<string, BoundMethod>();
|
|
477
|
+
for (const [name, provider] of Object.entries(view.methods)) {
|
|
478
|
+
const fn = selected.functions[name];
|
|
479
|
+
if (!fn) throw new Error(`Missing prepared signature for ${binding}#${name}`);
|
|
480
|
+
const slot = this.contract.bind(binding, name, false);
|
|
481
|
+
this.signatures.set(slot, compile(viewMethod(fn)));
|
|
482
|
+
methods.set(name, { name: provider, slot });
|
|
483
|
+
}
|
|
484
|
+
this.views.set(binding, methods);
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
const methods = new Map<string, BoundMethod>();
|
|
488
|
+
for (const [name, fn] of Object.entries(selected.functions)) {
|
|
489
|
+
const spelling = names[name];
|
|
490
|
+
if (!spelling || methods.has(spelling)) throw new Error("Ambiguous prepared import method");
|
|
491
|
+
const slot = this.contract.bind(binding, name, false);
|
|
492
|
+
const roles = resourceRoles(graph, selected.contract ?? binding, {
|
|
493
|
+
...fn,
|
|
494
|
+
name,
|
|
495
|
+
} as GraphFunction);
|
|
496
|
+
this.signatures.set(slot, compile(resourceDataFunction(fn as GraphFunction, roles)));
|
|
497
|
+
methods.set(spelling, { name, slot, roles });
|
|
498
|
+
}
|
|
499
|
+
this.importMethods.set(binding, methods);
|
|
500
|
+
}
|
|
501
|
+
let rootScope: ComponentScope | undefined;
|
|
502
|
+
// A compiled component has no factory code: a factory issues the host scope and declares
|
|
503
|
+
// what the component's kernel callbacks describe for it. The factory's admission holds the
|
|
504
|
+
// gate, so those callbacks enter the component directly.
|
|
505
|
+
const create =
|
|
506
|
+
(service: string | null, contract?: string) => async (declaration: Declaration) => {
|
|
507
|
+
const scope = new ComponentScope(this, declaration, service);
|
|
508
|
+
if (service === null) rootScope = scope;
|
|
509
|
+
this.scopes.set(scope.id, scope);
|
|
510
|
+
await this.kernel.declare(scope, declaration, service, contract);
|
|
511
|
+
return scope;
|
|
512
|
+
};
|
|
513
|
+
const admit = () => this.gate.enter(Math.max(1, Math.floor(this.timeout / 2)));
|
|
514
|
+
const services: Record<string, ServiceDefinition> = {};
|
|
515
|
+
for (const service of checked.services) {
|
|
516
|
+
const { interface: binding, alias } = service;
|
|
517
|
+
const functions = graph.exports.find((item) => item.binding === binding)?.functions;
|
|
518
|
+
services[service.name] = {
|
|
519
|
+
[signature]: service.signature,
|
|
520
|
+
[declareFactory]: create(alias, binding),
|
|
521
|
+
lifetime: service.lifetime,
|
|
522
|
+
linger: service.linger,
|
|
523
|
+
[admitFactory]: admit,
|
|
524
|
+
methods: Object.fromEntries(
|
|
525
|
+
Object.entries(service.methods).map(([method, spelling]) => {
|
|
526
|
+
const slot = this.contract.bind(binding, method, true);
|
|
527
|
+
const fn = functions?.[method];
|
|
528
|
+
if (!fn) throw new Error(`Missing prepared signature for ${binding}#${method}`);
|
|
529
|
+
const roles = resourceRoles(graph, binding, {
|
|
530
|
+
...fn,
|
|
531
|
+
name: method,
|
|
532
|
+
} as GraphFunction);
|
|
533
|
+
const data = resourceDataFunction(fn as GraphFunction, roles);
|
|
534
|
+
const shape = compile(data);
|
|
535
|
+
const values = convert(data);
|
|
536
|
+
this.signatures.set(slot, shape);
|
|
537
|
+
const invoke = (
|
|
538
|
+
environment: ComponentScope,
|
|
539
|
+
context: Context,
|
|
540
|
+
input: Encoded,
|
|
541
|
+
receiver?: ResourceReceiver,
|
|
542
|
+
retain?: (physical: Promise<void>) => void,
|
|
543
|
+
arguments_?: CapturedArguments,
|
|
544
|
+
) =>
|
|
545
|
+
this.invoke(
|
|
546
|
+
binding,
|
|
547
|
+
spelling,
|
|
548
|
+
values,
|
|
549
|
+
slot,
|
|
550
|
+
shape,
|
|
551
|
+
environment,
|
|
552
|
+
environment.exchange(entered(context)),
|
|
553
|
+
input,
|
|
554
|
+
roles,
|
|
555
|
+
receiver,
|
|
556
|
+
retain,
|
|
557
|
+
arguments_,
|
|
558
|
+
);
|
|
559
|
+
const invokeData = async (
|
|
560
|
+
environment: ComponentScope,
|
|
561
|
+
context: Context,
|
|
562
|
+
args: readonly unknown[],
|
|
563
|
+
retain?: (physical: Promise<void>) => void,
|
|
564
|
+
) => {
|
|
565
|
+
const input = check(
|
|
566
|
+
this.contract,
|
|
567
|
+
slot,
|
|
568
|
+
true,
|
|
569
|
+
encode(args, VALUE_LIMIT, shape.params),
|
|
570
|
+
);
|
|
571
|
+
const output = await invoke(environment, context, input, undefined, retain);
|
|
572
|
+
if (!(output instanceof Encoded)) {
|
|
573
|
+
for (const capability of output.capabilities ?? []) capability.close();
|
|
574
|
+
await output.finished;
|
|
575
|
+
throw new Error("A data-only caller cannot receive a host capability");
|
|
576
|
+
}
|
|
577
|
+
return lift(shape.result, JSON.parse(output.text));
|
|
578
|
+
};
|
|
579
|
+
return [
|
|
580
|
+
method,
|
|
581
|
+
bindCaptured(
|
|
582
|
+
(environment: ComponentScope, context: Context, ...args: unknown[]) =>
|
|
583
|
+
invokeData(environment, context, args),
|
|
584
|
+
service.signature,
|
|
585
|
+
invoke,
|
|
586
|
+
invokeData,
|
|
587
|
+
),
|
|
588
|
+
];
|
|
589
|
+
}),
|
|
590
|
+
),
|
|
591
|
+
} as AdaptedService;
|
|
592
|
+
}
|
|
593
|
+
// Service names are kernel-global, `<orb>::<interface>` like the contract's own services.
|
|
594
|
+
if (plan.ingress)
|
|
595
|
+
services[`${identity.name}::${HTTP_HANDLER}`] = this.ingress(
|
|
596
|
+
plan.ingress,
|
|
597
|
+
description,
|
|
598
|
+
create,
|
|
599
|
+
admit,
|
|
600
|
+
);
|
|
601
|
+
const contributes: Record<string, Record<string, Contribution>> = {};
|
|
602
|
+
for (const binding of checked.hooks.contributions) {
|
|
603
|
+
const fn = exported(binding.name, binding.method);
|
|
604
|
+
const shape = compile(fn);
|
|
605
|
+
const values = convert(fn as GraphFunction);
|
|
606
|
+
this.signatures.set(binding.slot, shape);
|
|
607
|
+
(contributes[binding.target] ??= {})[binding.name] = {
|
|
608
|
+
priority: binding.priority,
|
|
609
|
+
handle: async (context, input) => {
|
|
610
|
+
if (!rootScope || this.scopes.get(rootScope.id) !== rootScope)
|
|
611
|
+
throw new Error("Contribution Orb scope is unavailable");
|
|
612
|
+
const arguments_ = check(
|
|
613
|
+
this.contract,
|
|
614
|
+
binding.slot,
|
|
615
|
+
true,
|
|
616
|
+
encode([input], VALUE_LIMIT, shape.params),
|
|
617
|
+
);
|
|
618
|
+
let physical: Promise<void> | undefined;
|
|
619
|
+
let result: Encoded | CapturedDelivery;
|
|
620
|
+
try {
|
|
621
|
+
result = await this.invoke(
|
|
622
|
+
binding.name,
|
|
623
|
+
binding.method,
|
|
624
|
+
values,
|
|
625
|
+
binding.slot,
|
|
626
|
+
shape,
|
|
627
|
+
rootScope,
|
|
628
|
+
rootScope.exchange(entered(context)),
|
|
629
|
+
arguments_,
|
|
630
|
+
{},
|
|
631
|
+
undefined,
|
|
632
|
+
(actual) => {
|
|
633
|
+
physical = actual;
|
|
634
|
+
},
|
|
635
|
+
);
|
|
636
|
+
} finally {
|
|
637
|
+
// Contributions have no early capability lane: retain their owned operation
|
|
638
|
+
// until actual guest/provider exit, including after a bounded failure.
|
|
639
|
+
await physical?.catch(() => undefined);
|
|
640
|
+
}
|
|
641
|
+
if (!(result instanceof Encoded)) {
|
|
642
|
+
for (const capability of result.capabilities ?? []) capability.close();
|
|
643
|
+
throw new Error("A contribution cannot carry a host capability");
|
|
644
|
+
}
|
|
645
|
+
const value = lift(
|
|
646
|
+
shape.result,
|
|
647
|
+
JSON.parse(checkedResult(this.contract, binding.slot, result.text).text),
|
|
648
|
+
);
|
|
649
|
+
return binding.optional && value === null ? undefined : value;
|
|
650
|
+
},
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
return {
|
|
654
|
+
name: identity.name,
|
|
655
|
+
apiVersion: identity.apiVersion,
|
|
656
|
+
services,
|
|
657
|
+
hooks: declaredHooks(checked.hooks.hooks),
|
|
658
|
+
contributes,
|
|
659
|
+
requires: Object.fromEntries(
|
|
660
|
+
plan.orb.requires.map((requirement) => [
|
|
661
|
+
requirement.orb,
|
|
662
|
+
{ ...requirement, [signatures]: requirement.signatures },
|
|
663
|
+
]),
|
|
664
|
+
),
|
|
665
|
+
[isolatedRevision]: true,
|
|
666
|
+
[declareFactory]: create(null),
|
|
667
|
+
// A reload adopts a retained state only once the component validated it.
|
|
668
|
+
[validateEnvironment]: async (environment: Environment, value: unknown) => {
|
|
669
|
+
const scope = this.scopes.get(environment.id);
|
|
670
|
+
if (!scope) throw new Error("Validator scope is gone");
|
|
671
|
+
const release = await this.gate.enter();
|
|
672
|
+
try {
|
|
673
|
+
await this.kernel.validate(scope, value);
|
|
674
|
+
} finally {
|
|
675
|
+
release();
|
|
676
|
+
}
|
|
677
|
+
},
|
|
678
|
+
[admitFactory]: admit,
|
|
679
|
+
[releaseEnvironment]: async (environment: Environment) => {
|
|
680
|
+
if (environment === environment.orb.scope) {
|
|
681
|
+
for (const owner of this.importedResources) owner.close();
|
|
682
|
+
this.importedResources.clear();
|
|
683
|
+
}
|
|
684
|
+
// Environment release owns any remaining exported instances. Drain while the
|
|
685
|
+
// environment is still registered, so each guest destructor runs exactly once.
|
|
686
|
+
const owners = this.#issuedResources.get(environment.id);
|
|
687
|
+
for (const retire of owners ?? []) retire();
|
|
688
|
+
// Closing schedules drops through the port's physical cleanup queue.
|
|
689
|
+
await this.port.settled();
|
|
690
|
+
this.#issuedResources.delete(environment.id);
|
|
691
|
+
this.kernel.release(environment.id);
|
|
692
|
+
this.scopes.delete(environment.id);
|
|
693
|
+
},
|
|
694
|
+
[disposeOrb]: async () => {
|
|
695
|
+
if (this.#closed) return;
|
|
696
|
+
if (this.scopes.size) throw new Error("Component still owns physical scopes");
|
|
697
|
+
await this.port.close();
|
|
698
|
+
this.#closed = true;
|
|
699
|
+
},
|
|
700
|
+
} as AdaptedOrb;
|
|
701
|
+
}
|
|
702
|
+
}
|