@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,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
HookCapability2,
|
|
3
|
+
Errors2,
|
|
4
|
+
TembraError2,
|
|
5
|
+
refuse
|
|
6
|
+
} from "./index-c1w7z9hx.js";
|
|
7
|
+
import {
|
|
8
|
+
associateServices
|
|
9
|
+
} from "./index-kyt9dj0a.js";
|
|
10
|
+
|
|
11
|
+
// src/authoring/orb.ts
|
|
12
|
+
function orb3(definition) {
|
|
13
|
+
if (!definition.name || !Number.isInteger(definition.apiVersion) || definition.apiVersion <= 0)
|
|
14
|
+
refuse(Errors2.Invalid.Orb, "An orb needs a name and positive API version");
|
|
15
|
+
const services = Object.fromEntries(Object.entries(definition.services ?? {}).map(([name, service]) => {
|
|
16
|
+
if (!name || !service.methods || typeof service.methods !== "object")
|
|
17
|
+
refuse(Errors2.Invalid.Service);
|
|
18
|
+
if (service.create && typeof service.create !== "function")
|
|
19
|
+
refuse(Errors2.Invalid.Factory);
|
|
20
|
+
for (const handler of Object.values(service.methods)) {
|
|
21
|
+
if (typeof handler !== "function")
|
|
22
|
+
refuse(Errors2.Invalid.Method, "Methods must be functions");
|
|
23
|
+
}
|
|
24
|
+
const copy = {
|
|
25
|
+
...service,
|
|
26
|
+
methods: Object.freeze({ ...service.methods })
|
|
27
|
+
};
|
|
28
|
+
return [name, Object.freeze(copy)];
|
|
29
|
+
}));
|
|
30
|
+
const requires = Object.fromEntries(Object.entries(definition.requires ?? {}).map(([name, requirement]) => [
|
|
31
|
+
name,
|
|
32
|
+
Object.freeze({
|
|
33
|
+
...requirement,
|
|
34
|
+
...requirement.methods ? { methods: Object.freeze([...requirement.methods]) } : {},
|
|
35
|
+
...requirement.hooks ? { hooks: Object.freeze([...requirement.hooks]) } : {},
|
|
36
|
+
...requirement.services ? {
|
|
37
|
+
services: Object.freeze(Object.fromEntries(Object.entries(requirement.services).map(([name, methods]) => [
|
|
38
|
+
name,
|
|
39
|
+
Object.freeze([...methods])
|
|
40
|
+
])))
|
|
41
|
+
} : {}
|
|
42
|
+
})
|
|
43
|
+
]));
|
|
44
|
+
const hooks = Object.fromEntries(Object.entries(definition.hooks ?? {}).map(([name, hook]) => {
|
|
45
|
+
if (typeof hook.run !== "function")
|
|
46
|
+
refuse(Errors2.Invalid.Hook, "Hooks need a run function");
|
|
47
|
+
return [
|
|
48
|
+
name,
|
|
49
|
+
Object.freeze({ ...hook, capabilities: Object.freeze([...hook.capabilities ?? []]) })
|
|
50
|
+
];
|
|
51
|
+
}));
|
|
52
|
+
const contributes = Object.fromEntries(Object.entries(definition.contributes ?? {}).map(([alias, hooks]) => [
|
|
53
|
+
alias,
|
|
54
|
+
Object.freeze(Object.fromEntries(Object.entries(hooks).map(([name, contribution]) => [
|
|
55
|
+
name,
|
|
56
|
+
typeof contribution === "function" ? contribution : Object.freeze({ ...contribution })
|
|
57
|
+
])))
|
|
58
|
+
]));
|
|
59
|
+
const captured = Object.freeze({
|
|
60
|
+
...definition,
|
|
61
|
+
services: Object.freeze(services),
|
|
62
|
+
requires: Object.freeze(requires),
|
|
63
|
+
hooks: Object.freeze(hooks),
|
|
64
|
+
contributes: Object.freeze(contributes)
|
|
65
|
+
});
|
|
66
|
+
associateServices(definition.name, definition.apiVersion, Object.entries(definition.services ?? {}).map(([name, original]) => [
|
|
67
|
+
name,
|
|
68
|
+
original,
|
|
69
|
+
services[name]
|
|
70
|
+
]));
|
|
71
|
+
return captured;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/authoring/hooks.ts
|
|
75
|
+
function series3() {
|
|
76
|
+
return {
|
|
77
|
+
capabilities: [HookCapability2.Observe],
|
|
78
|
+
async run(ctx, input, participants) {
|
|
79
|
+
for (const participant of participants)
|
|
80
|
+
await participant(ctx, input);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function waterfall3() {
|
|
85
|
+
return {
|
|
86
|
+
capabilities: [HookCapability2.Transform],
|
|
87
|
+
async run(ctx, value, participants) {
|
|
88
|
+
for (const participant of participants)
|
|
89
|
+
value = await participant(ctx, value);
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function bail3() {
|
|
95
|
+
return {
|
|
96
|
+
capabilities: [HookCapability2.Decide],
|
|
97
|
+
async run(ctx, value, participants) {
|
|
98
|
+
for (const participant of participants) {
|
|
99
|
+
const result = await participant(ctx, value);
|
|
100
|
+
if (result !== undefined)
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function parallel2() {
|
|
107
|
+
return {
|
|
108
|
+
capabilities: [HookCapability2.Observe],
|
|
109
|
+
async run(ctx, value, participants) {
|
|
110
|
+
const settled = await Promise.allSettled(participants.map((participant) => participant(ctx, value)));
|
|
111
|
+
const error = settled.find((result) => result.status === "rejected");
|
|
112
|
+
if (error?.status === "rejected")
|
|
113
|
+
throw error.reason;
|
|
114
|
+
return settled.map((result) => result.status === "fulfilled" ? result.value : undefined);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function middleware2(terminal) {
|
|
119
|
+
return {
|
|
120
|
+
capabilities: [HookCapability2.Transform, HookCapability2.Decide],
|
|
121
|
+
async run(ctx, input, participants) {
|
|
122
|
+
let active = true;
|
|
123
|
+
async function invoke(index, value) {
|
|
124
|
+
if (!active)
|
|
125
|
+
throw new TembraError2(Errors2.Expired.Next, "Continuation scope has ended");
|
|
126
|
+
ctx.check();
|
|
127
|
+
const participant = participants[index];
|
|
128
|
+
if (!participant)
|
|
129
|
+
return terminal(ctx, value);
|
|
130
|
+
let used = false;
|
|
131
|
+
let pending;
|
|
132
|
+
let open = true;
|
|
133
|
+
const next = (nextValue) => {
|
|
134
|
+
if (!open || !active)
|
|
135
|
+
return Promise.reject(new TembraError2(Errors2.Expired.Next));
|
|
136
|
+
if (used)
|
|
137
|
+
return Promise.reject(new TembraError2(Errors2.Invalid.NextUsed));
|
|
138
|
+
used = true;
|
|
139
|
+
pending = invoke(index + 1, nextValue);
|
|
140
|
+
pending.catch(() => {});
|
|
141
|
+
return pending;
|
|
142
|
+
};
|
|
143
|
+
try {
|
|
144
|
+
return await participant(ctx, value, next);
|
|
145
|
+
} finally {
|
|
146
|
+
open = false;
|
|
147
|
+
if (pending)
|
|
148
|
+
await pending.catch(() => {});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
return await invoke(0, input);
|
|
153
|
+
} finally {
|
|
154
|
+
active = false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { orb3, series3, waterfall3, bail3, parallel2, middleware2 };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
shapes,
|
|
3
|
+
lift,
|
|
4
|
+
VALUE_LIMIT,
|
|
5
|
+
encode,
|
|
6
|
+
context,
|
|
7
|
+
isContext,
|
|
8
|
+
referenceOrigin,
|
|
9
|
+
checkSignal
|
|
10
|
+
} from "./index-kyt9dj0a.js";
|
|
11
|
+
import {
|
|
12
|
+
bindingName,
|
|
13
|
+
resourceRoles,
|
|
14
|
+
resourceDataFunction
|
|
15
|
+
} from "./index-hfds9mzp.js";
|
|
16
|
+
|
|
17
|
+
// src/runtime/wit-client.ts
|
|
18
|
+
var owners = new WeakMap;
|
|
19
|
+
var finalizer = typeof FinalizationRegistry === "undefined" ? undefined : new FinalizationRegistry((owner) => owner.close());
|
|
20
|
+
var backends = new WeakMap;
|
|
21
|
+
function bindWitReference(reference, backend) {
|
|
22
|
+
backends.set(reference, backend);
|
|
23
|
+
}
|
|
24
|
+
function witClient2(reference, schema) {
|
|
25
|
+
const remote = backends.get(reference);
|
|
26
|
+
if (remote)
|
|
27
|
+
return resourceClient(schema, remote(schema));
|
|
28
|
+
if (Object.keys(schema.resources).length === 0)
|
|
29
|
+
return {
|
|
30
|
+
async call(method, args) {
|
|
31
|
+
if (!Object.hasOwn(schema.functions, method))
|
|
32
|
+
throw new Error(`Unknown WIT function ${method}`);
|
|
33
|
+
return reference[method](...args);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const { session, record } = referenceOrigin(reference);
|
|
37
|
+
return resourceClient(schema, {
|
|
38
|
+
identity: session,
|
|
39
|
+
run(supplied, operation) {
|
|
40
|
+
const selected = supplied ? context(supplied, session) : record.context;
|
|
41
|
+
return selected ? session.calls.observe(operation(selected), selected.signal) : session.calls.root((ctx) => session.calls.observe(operation(ctx), ctx.signal), false, undefined, record.address.principal);
|
|
42
|
+
},
|
|
43
|
+
async invoke(method, args, capture, ctx) {
|
|
44
|
+
await session.calls.invokePhysical(capture.receiver ? capture.receiver.reference : record, method, args, ctx, capture);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function resourceClient(schema, backend) {
|
|
49
|
+
const session = backend.identity;
|
|
50
|
+
const compile = shapes(schema);
|
|
51
|
+
const functions = new Map(Object.entries(schema.functions).map(([name, fn]) => {
|
|
52
|
+
const roles = resourceRoles(schema, schema.identity, { ...fn, name });
|
|
53
|
+
if (roles.stream)
|
|
54
|
+
throw new Error("Generated clients do not support stream results");
|
|
55
|
+
return [name, { roles, shape: compile(resourceDataFunction(fn, roles)), fn }];
|
|
56
|
+
}));
|
|
57
|
+
const wrap = (owner) => {
|
|
58
|
+
const value = {
|
|
59
|
+
resourceType: `${owner.nominal.interface}#${owner.nominal.name}`,
|
|
60
|
+
release() {
|
|
61
|
+
finalizer?.unregister(value);
|
|
62
|
+
owner.close();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
owners.set(value, { owner, session });
|
|
66
|
+
finalizer?.register(value, owner, value);
|
|
67
|
+
for (const [name, { roles }] of functions) {
|
|
68
|
+
if (roles.receiver?.name === owner.nominal.name && roles.member) {
|
|
69
|
+
const method = schema.javascriptNames[roles.member] ?? bindingName(roles.member, false);
|
|
70
|
+
value[method] = (...args) => invoke(name, args, owner);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
};
|
|
75
|
+
const invoke = async (name, supplied, receiver) => {
|
|
76
|
+
const selected = functions.get(name);
|
|
77
|
+
if (!selected)
|
|
78
|
+
throw new Error(`Unknown WIT function ${name}`);
|
|
79
|
+
const { roles, shape } = selected;
|
|
80
|
+
const args = [...supplied];
|
|
81
|
+
const count = selected.fn.params.length - Number(Boolean(roles.receiver));
|
|
82
|
+
if (args.length === count + 1 && args[count] === undefined)
|
|
83
|
+
args.pop();
|
|
84
|
+
const last = args.at(-1);
|
|
85
|
+
const ctx = isContext(last) ? args.pop() : undefined;
|
|
86
|
+
const moved = [];
|
|
87
|
+
let accepted = false;
|
|
88
|
+
const call = async (ctx) => {
|
|
89
|
+
let capture;
|
|
90
|
+
try {
|
|
91
|
+
const arguments_ = roles.arguments ? roles.arguments(args, (nominal, value) => {
|
|
92
|
+
const held = typeof value === "object" && value !== null ? owners.get(value) : undefined;
|
|
93
|
+
if (!held || held.session !== session || held.owner.nominal.interface !== nominal.interface || held.owner.nominal.name !== nominal.name)
|
|
94
|
+
throw new Error("Expected an SDK resource of the exact nominal type and host");
|
|
95
|
+
const owner = held.owner.move();
|
|
96
|
+
finalizer?.unregister(value);
|
|
97
|
+
moved.push(owner);
|
|
98
|
+
return moved.length - 1;
|
|
99
|
+
}) : args;
|
|
100
|
+
const input = encode(arguments_, VALUE_LIMIT, shape.params);
|
|
101
|
+
capture = {
|
|
102
|
+
signature: schema.signature,
|
|
103
|
+
input,
|
|
104
|
+
...receiver ? { receiver } : {},
|
|
105
|
+
...moved.length ? {
|
|
106
|
+
arguments: {
|
|
107
|
+
owners: moved,
|
|
108
|
+
get accepted() {
|
|
109
|
+
return accepted;
|
|
110
|
+
},
|
|
111
|
+
accept() {
|
|
112
|
+
accepted = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} : {}
|
|
116
|
+
};
|
|
117
|
+
await backend.invoke(name, arguments_, capture, ctx);
|
|
118
|
+
if (ctx)
|
|
119
|
+
checkSignal(ctx.signal);
|
|
120
|
+
if (!capture.output)
|
|
121
|
+
throw new Error("Provider does not support the matching WIT capability signature");
|
|
122
|
+
const value = lift(shape.result, JSON.parse(capture.output.text));
|
|
123
|
+
const capabilities = capture.capabilities ?? [];
|
|
124
|
+
const consumed = new Set;
|
|
125
|
+
const result = roles.result ? roles.result(value, (nominal, index) => {
|
|
126
|
+
if (typeof index !== "number" || !Number.isInteger(index) || consumed.has(index))
|
|
127
|
+
throw new Error("An owned result must redeem each attachment exactly once");
|
|
128
|
+
const owner = capabilities[index];
|
|
129
|
+
if (owner?.kind !== "resource" || owner.nominal.interface !== nominal.interface || owner.nominal.name !== nominal.name)
|
|
130
|
+
throw new Error("Resource has another nominal type");
|
|
131
|
+
consumed.add(index);
|
|
132
|
+
return wrap(owner);
|
|
133
|
+
}) : value;
|
|
134
|
+
if (consumed.size !== capabilities.length)
|
|
135
|
+
throw new Error("Unused result capability attachment");
|
|
136
|
+
return selected.fn.result == null ? undefined : result;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
for (const capability of capture?.capabilities ?? [])
|
|
139
|
+
capability.close();
|
|
140
|
+
throw error;
|
|
141
|
+
} finally {
|
|
142
|
+
if (!accepted)
|
|
143
|
+
for (const owner of moved)
|
|
144
|
+
owner.close();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
return backend.run(ctx, call);
|
|
148
|
+
};
|
|
149
|
+
return { call: (name, args) => invoke(name, args) };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export { bindWitReference, witClient2 };
|