@madebyentropy/tnl 0.0.0-stage → 0.1.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/LICENSE +28 -0
- package/NOTICE +5 -0
- package/THIRD_PARTY_LICENSES.txt +3297 -0
- package/dist/adapters/ioredis.d.ts +7 -0
- package/dist/adapters/ioredis.js +87 -0
- package/dist/adapters/node-postgres.d.ts +8 -0
- package/dist/adapters/node-postgres.js +41 -0
- package/dist/adapters/postgres-js.d.ts +7 -0
- package/dist/adapters/postgres-js.js +55 -0
- package/dist/bin/tnl.d.ts +2 -0
- package/dist/bin/tnl.js +16 -0
- package/dist/browser.d.ts +18 -0
- package/dist/browser.js +43 -0
- package/dist/bun.d.ts +9 -0
- package/dist/bun.js +6 -0
- package/dist/cloudflare.d.ts +9 -0
- package/dist/cloudflare.js +16 -0
- package/dist/config.d.ts +107 -0
- package/dist/config.gen.d.ts +1185 -0
- package/dist/config.gen.js +2 -0
- package/dist/config.js +3 -0
- package/dist/control.d.ts +5 -0
- package/dist/control.js +9 -0
- package/dist/durable-object.d.ts +51 -0
- package/dist/durable-object.js +374 -0
- package/dist/durable-object.worker.d.ts +5 -0
- package/dist/durable-object.worker.js +11 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +230 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +2 -0
- package/dist/internal/connector/cancellation.d.ts +7 -0
- package/dist/internal/connector/cancellation.js +30 -0
- package/dist/internal/connector/client.d.ts +62 -0
- package/dist/internal/connector/client.js +504 -0
- package/dist/internal/connector/client_factory.d.ts +6 -0
- package/dist/internal/connector/client_factory.js +123 -0
- package/dist/internal/connector/connection.d.ts +39 -0
- package/dist/internal/connector/connection.js +739 -0
- package/dist/internal/connector/connection_ipc.d.ts +12 -0
- package/dist/internal/connector/connection_ipc.js +198 -0
- package/dist/internal/connector/context.d.ts +28 -0
- package/dist/internal/connector/context.js +19 -0
- package/dist/internal/connector/contract.d.ts +392 -0
- package/dist/internal/connector/contract.js +56 -0
- package/dist/internal/connector/deployment_types.d.ts +27 -0
- package/dist/internal/connector/deployment_types.js +1 -0
- package/dist/internal/connector/driver_socket.d.ts +21 -0
- package/dist/internal/connector/driver_socket.js +129 -0
- package/dist/internal/connector/management.d.ts +29 -0
- package/dist/internal/connector/management.js +118 -0
- package/dist/internal/connector/native_operations.d.ts +16 -0
- package/dist/internal/connector/native_operations.js +30 -0
- package/dist/internal/connector/native_stream.d.ts +7 -0
- package/dist/internal/connector/native_stream.js +219 -0
- package/dist/internal/connector/operation_handles.d.ts +9 -0
- package/dist/internal/connector/operation_handles.js +18 -0
- package/dist/internal/connector/portable_operations.d.ts +13 -0
- package/dist/internal/connector/portable_operations.js +25 -0
- package/dist/internal/connector/snapshot.d.ts +183 -0
- package/dist/internal/connector/snapshot.js +47 -0
- package/dist/internal/connector/wasm_abi.d.ts +76 -0
- package/dist/internal/connector/wasm_abi.js +5 -0
- package/dist/internal/connector/wasm_client.d.ts +58 -0
- package/dist/internal/connector/wasm_client.js +545 -0
- package/dist/internal/connector/wasm_loader.d.ts +15 -0
- package/dist/internal/connector/wasm_loader.js +99 -0
- package/dist/internal/connector/wasm_stream.d.ts +4 -0
- package/dist/internal/connector/wasm_stream.js +177 -0
- package/dist/internal/connector/workload/contract.d.ts +54 -0
- package/dist/internal/connector/workload/contract.js +14 -0
- package/dist/internal/connector/workload/provider.d.ts +24 -0
- package/dist/internal/connector/workload/provider.js +130 -0
- package/dist/internal/enrollment_source.d.ts +4 -0
- package/dist/internal/enrollment_source.js +34 -0
- package/dist/internal/management/client/client.gen.d.ts +2 -0
- package/dist/internal/management/client/client.gen.js +216 -0
- package/dist/internal/management/client/index.d.ts +10 -0
- package/dist/internal/management/client/index.js +6 -0
- package/dist/internal/management/client/types.gen.d.ts +120 -0
- package/dist/internal/management/client/types.gen.js +2 -0
- package/dist/internal/management/client/utils.gen.d.ts +37 -0
- package/dist/internal/management/client/utils.gen.js +228 -0
- package/dist/internal/management/client.gen.d.ts +12 -0
- package/dist/internal/management/client.gen.js +3 -0
- package/dist/internal/management/core/auth.gen.d.ts +25 -0
- package/dist/internal/management/core/auth.gen.js +14 -0
- package/dist/internal/management/core/bodySerializer.gen.d.ts +25 -0
- package/dist/internal/management/core/bodySerializer.gen.js +57 -0
- package/dist/internal/management/core/params.gen.d.ts +43 -0
- package/dist/internal/management/core/params.gen.js +109 -0
- package/dist/internal/management/core/pathSerializer.gen.d.ts +33 -0
- package/dist/internal/management/core/pathSerializer.gen.js +106 -0
- package/dist/internal/management/core/queryKeySerializer.gen.d.ts +18 -0
- package/dist/internal/management/core/queryKeySerializer.gen.js +92 -0
- package/dist/internal/management/core/serverSentEvents.gen.d.ts +71 -0
- package/dist/internal/management/core/serverSentEvents.gen.js +132 -0
- package/dist/internal/management/core/types.gen.d.ts +83 -0
- package/dist/internal/management/core/types.gen.js +2 -0
- package/dist/internal/management/core/utils.gen.d.ts +19 -0
- package/dist/internal/management/core/utils.gen.js +87 -0
- package/dist/internal/management/index.d.ts +2 -0
- package/dist/internal/management/index.js +2 -0
- package/dist/internal/management/sdk.gen.d.ts +73 -0
- package/dist/internal/management/sdk.gen.js +191 -0
- package/dist/internal/management/types.gen.d.ts +464 -0
- package/dist/internal/management/types.gen.js +2 -0
- package/dist/internal/management/zod.gen.d.ts +850 -0
- package/dist/internal/management/zod.gen.js +252 -0
- package/dist/internal/native/launcher.d.ts +8 -0
- package/dist/internal/native/launcher.js +75 -0
- package/dist/internal/native/native-targets.d.ts +9 -0
- package/dist/internal/native/native-targets.js +8 -0
- package/dist/internal/portable/cloudflare-publisher-client.d.ts +54 -0
- package/dist/internal/portable/cloudflare-publisher-client.js +91 -0
- package/dist/internal/portable/cloudflare-publisher.d.ts +19 -0
- package/dist/internal/portable/cloudflare-publisher.js +210 -0
- package/dist/internal/portable/durable-deployment.d.ts +10 -0
- package/dist/internal/portable/durable-deployment.js +22 -0
- package/dist/internal/portable/enrollment.d.ts +26 -0
- package/dist/internal/portable/enrollment.js +196 -0
- package/dist/internal/portable/publication.d.ts +54 -0
- package/dist/internal/portable/publication.js +304 -0
- package/dist/internal/portable/request.d.ts +1 -0
- package/dist/internal/portable/request.js +20 -0
- package/dist/internal/portable/storage-browser.d.ts +9 -0
- package/dist/internal/portable/storage-browser.js +115 -0
- package/dist/internal/portable/storage-durable-object.d.ts +11 -0
- package/dist/internal/portable/storage-durable-object.js +110 -0
- package/dist/internal/portable/storage-host.d.ts +8 -0
- package/dist/internal/portable/storage-host.js +142 -0
- package/dist/internal/portable/storage.d.ts +15 -0
- package/dist/internal/portable/storage.js +29 -0
- package/dist/internal/project/native_runtime.d.ts +24 -0
- package/dist/internal/project/native_runtime.js +23 -0
- package/dist/internal/project/runtime.d.ts +467 -0
- package/dist/internal/project/runtime.js +183 -0
- package/dist/internal/project/service_metadata.d.ts +3 -0
- package/dist/internal/project/service_metadata.js +12 -0
- package/dist/internal/publication_certificate.d.ts +28 -0
- package/dist/internal/publication_certificate.js +32 -0
- package/dist/internal/publication_run.d.ts +8 -0
- package/dist/internal/publication_run.js +19 -0
- package/dist/internal/publisher/app.d.ts +33 -0
- package/dist/internal/publisher/app.js +317 -0
- package/dist/internal/publisher/fetch.d.ts +6 -0
- package/dist/internal/publisher/fetch.js +135 -0
- package/dist/internal/publisher/fetch_types.d.ts +1 -0
- package/dist/internal/publisher/fetch_types.js +1 -0
- package/dist/internal/publisher/framework.d.ts +27 -0
- package/dist/internal/publisher/framework.js +82 -0
- package/dist/internal/publisher/local.d.ts +39 -0
- package/dist/internal/publisher/local.js +312 -0
- package/dist/internal/publisher/native_publisher.d.ts +25 -0
- package/dist/internal/publisher/native_publisher.js +228 -0
- package/dist/internal/publisher/port.d.ts +2 -0
- package/dist/internal/publisher/port.js +15 -0
- package/dist/internal/publisher/publication.d.ts +88 -0
- package/dist/internal/publisher/publication.js +627 -0
- package/dist/internal/publisher/register.d.ts +31 -0
- package/dist/internal/publisher/register.js +136 -0
- package/dist/internal/publisher/registration.d.ts +385 -0
- package/dist/internal/publisher/registration.js +223 -0
- package/dist/internal/publisher/status.d.ts +47 -0
- package/dist/internal/publisher/status.js +137 -0
- package/dist/internal/publisher/target.d.ts +6 -0
- package/dist/internal/publisher/target.js +44 -0
- package/dist/internal/runtime_resources.d.ts +38 -0
- package/dist/internal/runtime_resources.js +52 -0
- package/dist/internal/tracing.d.ts +10 -0
- package/dist/internal/tracing.js +78 -0
- package/dist/internal/validation.d.ts +3 -0
- package/dist/internal/validation.js +9 -0
- package/dist/internal/values.d.ts +27 -0
- package/dist/internal/values.js +54 -0
- package/dist/next.d.ts +12 -0
- package/dist/next.js +82 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.js +6 -0
- package/dist/project.d.ts +173 -0
- package/dist/project.js +1 -0
- package/dist/response.d.ts +1 -0
- package/dist/response.js +34 -0
- package/dist/third-party-licenses.txt +1537 -0
- package/dist/vite.d.ts +5 -0
- package/dist/vite.js +200 -0
- package/dist/wasm/build.json +16 -0
- package/dist/wasm/connector.wasm +0 -0
- package/dist/wasm/go-license.txt +27 -0
- package/dist/wasm/publisher.wasm +0 -0
- package/dist/wasm/third-party-licenses.txt +3297 -0
- package/dist/wasm/wasm_exec.d.ts +1 -0
- package/dist/wasm/wasm_exec.js +575 -0
- package/dist/workload.d.ts +11 -0
- package/dist/workload.js +4 -0
- package/package.json +159 -4
- package/readme.md +13 -0
- package/README.md +0 -3
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { DurableObject } from "cloudflare:workers";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
import module from "../../wasm/publisher.wasm";
|
|
4
|
+
import { TnlError } from "../../errors.js";
|
|
5
|
+
import { httpsOriginSchema, serviceNameSchema } from "../values.js";
|
|
6
|
+
import { loadWasm } from "../connector/wasm_loader.js";
|
|
7
|
+
import { workerTarget, } from "./cloudflare-publisher-client.js";
|
|
8
|
+
import { enrollPortablePublisher } from "./enrollment.js";
|
|
9
|
+
import { createDurableObjectPublisherStorage } from "./storage-durable-object.js";
|
|
10
|
+
import { PublisherStorageError } from "./storage.js";
|
|
11
|
+
const intentKey = "managed-intent-v1";
|
|
12
|
+
const alarmInterval = 60_000;
|
|
13
|
+
const maximumServices = 32;
|
|
14
|
+
const intentSchema = z.strictObject({
|
|
15
|
+
version: z.literal(1),
|
|
16
|
+
services: z
|
|
17
|
+
.array(z.strictObject({ service: serviceNameSchema, id: z.uuid(), target: workerTarget }))
|
|
18
|
+
.max(maximumServices)
|
|
19
|
+
.refine((values) => new Set(values.map((v) => v.service)).size === values.length),
|
|
20
|
+
});
|
|
21
|
+
// publication intent contains only the target name. the durable object owns
|
|
22
|
+
// the identity, intent and wasm lifetime; go owns each authorized service run.
|
|
23
|
+
export class TnlPublisher extends DurableObject {
|
|
24
|
+
#state;
|
|
25
|
+
#env;
|
|
26
|
+
#storage;
|
|
27
|
+
#identity;
|
|
28
|
+
#queue = Promise.resolve();
|
|
29
|
+
#intent;
|
|
30
|
+
#running = new Map();
|
|
31
|
+
constructor(state, env) {
|
|
32
|
+
super(state, env);
|
|
33
|
+
this.#state = state;
|
|
34
|
+
this.#env = env;
|
|
35
|
+
this.#storage = createDurableObjectPublisherStorage(state.storage, "managed");
|
|
36
|
+
}
|
|
37
|
+
open(controlUrl, enrollment) {
|
|
38
|
+
return this.#serialize(async () => {
|
|
39
|
+
if (controlUrl !== undefined && !httpsOriginSchema.safeParse(controlUrl).success)
|
|
40
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
41
|
+
if (enrollment !== undefined && typeof enrollment !== "function")
|
|
42
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
43
|
+
const identity = await this.#open(controlUrl, enrollment);
|
|
44
|
+
return {
|
|
45
|
+
publisherIdentityID: identity.publisherIdentityID,
|
|
46
|
+
deploymentID: identity.deploymentID,
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
start(service, target) {
|
|
51
|
+
return this.#serialize(async () => {
|
|
52
|
+
const selected = workerTarget.safeParse(target);
|
|
53
|
+
if (!selected.success || !serviceNameSchema.safeParse(service).success)
|
|
54
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
55
|
+
this.#target(selected.data);
|
|
56
|
+
const identity = await this.#open();
|
|
57
|
+
const intents = await this.#intents();
|
|
58
|
+
const current = intents.get(service);
|
|
59
|
+
if (current === undefined && intents.size >= maximumServices)
|
|
60
|
+
throw new TnlError("sdk.client_capacity");
|
|
61
|
+
const intent = current !== undefined && JSON.stringify(current.target) === JSON.stringify(selected.data)
|
|
62
|
+
? current
|
|
63
|
+
: { service, target: selected.data, id: crypto.randomUUID() };
|
|
64
|
+
// arm before committing intent so a restart after the write can recover.
|
|
65
|
+
await this.#arm();
|
|
66
|
+
if (intent !== current)
|
|
67
|
+
await this.#save(new Map(intents).set(service, intent));
|
|
68
|
+
return this.#result(await this.#start(identity, intent));
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
exportCertificate(service, id) {
|
|
72
|
+
return this.#serialize(async () => {
|
|
73
|
+
if (!serviceNameSchema.safeParse(service).success || !z.uuid().safeParse(id).success)
|
|
74
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
75
|
+
const running = this.#running.get(service);
|
|
76
|
+
if (running?.intent.id !== id)
|
|
77
|
+
throw new TnlError("sdk.certificate_unavailable");
|
|
78
|
+
return running.publication.exportCertificate();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
stop(service, id) {
|
|
82
|
+
return this.#serialize(async () => {
|
|
83
|
+
if (!serviceNameSchema.safeParse(service).success || !z.uuid().safeParse(id).success)
|
|
84
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
85
|
+
const intents = await this.#intents();
|
|
86
|
+
const current = intents.get(service);
|
|
87
|
+
if (current !== undefined && current.id !== id)
|
|
88
|
+
return;
|
|
89
|
+
if (current !== undefined) {
|
|
90
|
+
const next = new Map(intents);
|
|
91
|
+
next.delete(service);
|
|
92
|
+
await this.#save(next);
|
|
93
|
+
}
|
|
94
|
+
const running = this.#running.get(service);
|
|
95
|
+
if (running?.intent.id === id)
|
|
96
|
+
await this.#closeRun(running);
|
|
97
|
+
if ((await this.#intents()).size === 0 && this.#running.size === 0) {
|
|
98
|
+
if (this.#identity !== undefined) {
|
|
99
|
+
await this.#identity.close();
|
|
100
|
+
this.#identity = undefined;
|
|
101
|
+
}
|
|
102
|
+
await this.#state.storage.deleteAlarm();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
alarm() {
|
|
107
|
+
return this.#serialize(async () => {
|
|
108
|
+
const intents = await this.#intents();
|
|
109
|
+
if (intents.size === 0)
|
|
110
|
+
return;
|
|
111
|
+
await this.#arm();
|
|
112
|
+
const identity = await this.#open();
|
|
113
|
+
const results = await Promise.allSettled([...intents.values()].map((intent) => this.#start(identity, intent)));
|
|
114
|
+
if (results.some((result) => result.status === "rejected"))
|
|
115
|
+
throw new TnlError("sdk.publication_failed");
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
#serialize(operation) {
|
|
119
|
+
const running = this.#queue.then(operation);
|
|
120
|
+
this.#queue = running.then(() => { }, () => { });
|
|
121
|
+
this.#state.waitUntil(this.#queue);
|
|
122
|
+
return running;
|
|
123
|
+
}
|
|
124
|
+
async #open(controlUrl, enrollment) {
|
|
125
|
+
if (this.#identity === undefined) {
|
|
126
|
+
this.#identity = await enrollPortablePublisher({
|
|
127
|
+
publisherStorage: this.#storage,
|
|
128
|
+
...(controlUrl === undefined ? {} : { controlUrl }),
|
|
129
|
+
// abort signals are local to an event and are not RPC arguments.
|
|
130
|
+
...(enrollment === undefined ? {} : { enrollment: () => enrollment() }),
|
|
131
|
+
}, () => loadWasm(module));
|
|
132
|
+
}
|
|
133
|
+
if (controlUrl !== undefined && controlUrl !== this.#identity.controlUrl)
|
|
134
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
135
|
+
return this.#identity;
|
|
136
|
+
}
|
|
137
|
+
async #intents() {
|
|
138
|
+
if (this.#intent === undefined) {
|
|
139
|
+
const raw = await this.#storage.get(intentKey);
|
|
140
|
+
if (raw === undefined)
|
|
141
|
+
this.#intent = new Map();
|
|
142
|
+
else {
|
|
143
|
+
try {
|
|
144
|
+
const parsed = intentSchema.parse(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(raw)));
|
|
145
|
+
this.#intent = new Map(parsed.services.map((intent) => [intent.service, intent]));
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
throw new PublisherStorageError("sdk.publisher_storage_invalid");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return this.#intent;
|
|
153
|
+
}
|
|
154
|
+
async #save(next) {
|
|
155
|
+
try {
|
|
156
|
+
await this.#storage.put(intentKey, new TextEncoder().encode(JSON.stringify({ version: 1, services: [...next.values()] })));
|
|
157
|
+
await this.#storage.sync(intentKey);
|
|
158
|
+
this.#intent = next;
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
// a storage failure may have happened after the write committed.
|
|
162
|
+
this.#intent = undefined;
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
#arm() {
|
|
167
|
+
return this.#state.storage.setAlarm(Date.now() + alarmInterval);
|
|
168
|
+
}
|
|
169
|
+
#target(target) {
|
|
170
|
+
const bindings = "binding" in target ? this.#env : this.#state.exports;
|
|
171
|
+
const name = "binding" in target ? target.binding : "entrypoint" in target ? target.entrypoint : "default";
|
|
172
|
+
if (typeof bindings !== "object" || bindings === null || name === "TnlPublisher")
|
|
173
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
174
|
+
const binding = Reflect.get(bindings, name);
|
|
175
|
+
if ((typeof binding !== "object" && typeof binding !== "function") ||
|
|
176
|
+
binding === null ||
|
|
177
|
+
typeof Reflect.get(binding, "fetch") !== "function")
|
|
178
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
179
|
+
return (request) => binding.fetch(request);
|
|
180
|
+
}
|
|
181
|
+
async #start(identity, intent) {
|
|
182
|
+
const current = this.#running.get(intent.service);
|
|
183
|
+
if (current?.intent.id === intent.id)
|
|
184
|
+
return current;
|
|
185
|
+
if (current !== undefined)
|
|
186
|
+
await this.#closeRun(current);
|
|
187
|
+
const publication = await identity.publish(intent.service, this.#target(intent.target));
|
|
188
|
+
const running = { intent, publication };
|
|
189
|
+
this.#running.set(intent.service, running);
|
|
190
|
+
const ended = () => {
|
|
191
|
+
if (this.#running.get(intent.service) === running)
|
|
192
|
+
this.#running.delete(intent.service);
|
|
193
|
+
};
|
|
194
|
+
void publication.wait().then(ended, ended);
|
|
195
|
+
return running;
|
|
196
|
+
}
|
|
197
|
+
async #closeRun(running) {
|
|
198
|
+
await running.publication.close();
|
|
199
|
+
if (this.#running.get(running.intent.service) === running)
|
|
200
|
+
this.#running.delete(running.intent.service);
|
|
201
|
+
}
|
|
202
|
+
#result({ intent, publication }) {
|
|
203
|
+
return {
|
|
204
|
+
id: intent.id,
|
|
205
|
+
...(publication.url === undefined ? {} : { url: publication.url }),
|
|
206
|
+
...(publication.run === undefined ? {} : { run: publication.run }),
|
|
207
|
+
...(publication.publisherURL === undefined ? {} : { publisherURL: publication.publisherURL }),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PublisherStorageHost } from "./storage-host.js";
|
|
2
|
+
export declare function durablePublisherDeployment(context: {
|
|
3
|
+
readonly configuration: {
|
|
4
|
+
readonly project: string;
|
|
5
|
+
readonly deployment: string;
|
|
6
|
+
};
|
|
7
|
+
readonly identity: Uint8Array;
|
|
8
|
+
readonly storageHost: PublisherStorageHost;
|
|
9
|
+
readonly signal: AbortSignal;
|
|
10
|
+
}, module: WebAssembly.Module): import("../connector/wasm_client.js").StreamDeployment<Record<never, never>>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TnlError } from "../../errors.js";
|
|
2
|
+
import { createStreamClient } from "../connector/wasm_client.js";
|
|
3
|
+
import { loadWasm } from "../connector/wasm_loader.js";
|
|
4
|
+
// the durable owner supplies its persisted opaque credential and storage host.
|
|
5
|
+
// go validates authority; this adapter only decodes the private transport value.
|
|
6
|
+
export function durablePublisherDeployment(context, module) {
|
|
7
|
+
let token;
|
|
8
|
+
try {
|
|
9
|
+
if (!(context.identity instanceof Uint8Array) ||
|
|
10
|
+
context.identity.byteLength === 0 ||
|
|
11
|
+
context.identity.byteLength > 32 << 10)
|
|
12
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
13
|
+
token = new TextDecoder("utf-8", { fatal: true }).decode(context.identity);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
17
|
+
}
|
|
18
|
+
return createStreamClient({ project: context.configuration.project, token }, () => loadWasm(module), {
|
|
19
|
+
storageHost: context.storageHost,
|
|
20
|
+
signal: context.signal,
|
|
21
|
+
}).deployment(context.configuration.deployment);
|
|
22
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { loadWasm } from "../connector/wasm_loader.js";
|
|
2
|
+
import type { FetchHandler } from "../publisher/fetch_types.js";
|
|
3
|
+
import type { NamedPublicationOptions, Publication } from "../publisher/publication.js";
|
|
4
|
+
import { type PublisherEnrollment } from "../enrollment_source.js";
|
|
5
|
+
import { type PublisherResources } from "../runtime_resources.js";
|
|
6
|
+
import type { PublisherStorage } from "./storage.js";
|
|
7
|
+
export type { PublisherEnrollment } from "../enrollment_source.js";
|
|
8
|
+
export interface PublisherOptions {
|
|
9
|
+
readonly resources?: PublisherResources;
|
|
10
|
+
readonly controlUrl?: string;
|
|
11
|
+
/** stable origin-local identity slot; defaults to "default". */
|
|
12
|
+
readonly instance?: string;
|
|
13
|
+
/** needed only on first enrollment or while recovering a lost redemption. */
|
|
14
|
+
readonly enrollment?: PublisherEnrollment;
|
|
15
|
+
readonly publisherStorage?: PublisherStorage;
|
|
16
|
+
readonly signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
export interface EnrolledPublisher {
|
|
19
|
+
readonly publisherIdentityID: string;
|
|
20
|
+
readonly deploymentID: string;
|
|
21
|
+
readonly controlUrl: string;
|
|
22
|
+
publish(name: string, handler: FetchHandler, options?: NamedPublicationOptions): Promise<Publication>;
|
|
23
|
+
close(): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export declare function isEnrolledOwner(owner: object): owner is EnrolledPublisher;
|
|
26
|
+
export declare function enrollPortablePublisher(options: PublisherOptions, loadPublisher: () => ReturnType<typeof loadWasm>): Promise<EnrolledPublisher>;
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { TnlError } from "../../errors.js";
|
|
3
|
+
import { bindEnrollment, enrollmentSourceSchema, } from "../enrollment_source.js";
|
|
4
|
+
import { serviceNameSchema } from "../values.js";
|
|
5
|
+
import { publisherResourcesSchema, publisherResourceSelection, } from "../runtime_resources.js";
|
|
6
|
+
import { createBrowserPublisherStorage } from "./storage-browser.js";
|
|
7
|
+
import { createPublisherStorageHost } from "./storage-host.js";
|
|
8
|
+
import { portablePublicationOptions, publicationError, publishPortable, } from "./publication.js";
|
|
9
|
+
function isEnrolledPublisher(value) {
|
|
10
|
+
return (typeof value === "object" &&
|
|
11
|
+
value !== null &&
|
|
12
|
+
"publisherIdentityID" in value &&
|
|
13
|
+
typeof value.publisherIdentityID === "string" &&
|
|
14
|
+
/^pub_[A-Za-z0-9]{22}$/.test(value.publisherIdentityID) &&
|
|
15
|
+
"deploymentID" in value &&
|
|
16
|
+
typeof value.deploymentID === "string" &&
|
|
17
|
+
/^dpl_[A-Za-z0-9]{22}$/.test(value.deploymentID) &&
|
|
18
|
+
"controlUrl" in value &&
|
|
19
|
+
typeof value.controlUrl === "string" &&
|
|
20
|
+
(() => {
|
|
21
|
+
const url = URL.parse(value.controlUrl);
|
|
22
|
+
return url?.protocol === "https:" && url.origin === value.controlUrl;
|
|
23
|
+
})() &&
|
|
24
|
+
"publish" in value &&
|
|
25
|
+
typeof value.publish === "function" &&
|
|
26
|
+
"close" in value &&
|
|
27
|
+
typeof value.close === "function");
|
|
28
|
+
}
|
|
29
|
+
const enrollmentSchema = z.strictObject({
|
|
30
|
+
resources: publisherResourcesSchema.exactOptional(),
|
|
31
|
+
controlUrl: z.url().max(2048).exactOptional(),
|
|
32
|
+
instance: serviceNameSchema.exactOptional(),
|
|
33
|
+
enrollment: enrollmentSourceSchema.exactOptional(),
|
|
34
|
+
publisherStorage: z
|
|
35
|
+
.custom((v) => typeof v === "object" &&
|
|
36
|
+
v !== null &&
|
|
37
|
+
"get" in v &&
|
|
38
|
+
typeof v.get === "function" &&
|
|
39
|
+
"put" in v &&
|
|
40
|
+
typeof v.put === "function" &&
|
|
41
|
+
"sync" in v &&
|
|
42
|
+
typeof v.sync === "function" &&
|
|
43
|
+
"withLock" in v &&
|
|
44
|
+
typeof v.withLock === "function")
|
|
45
|
+
.exactOptional(),
|
|
46
|
+
signal: z.instanceof(AbortSignal).exactOptional(),
|
|
47
|
+
});
|
|
48
|
+
const owners = new WeakSet();
|
|
49
|
+
export function isEnrolledOwner(owner) {
|
|
50
|
+
return owners.has(owner);
|
|
51
|
+
}
|
|
52
|
+
// the go identity owner keeps signing material in opaque durable records.
|
|
53
|
+
// this wrapper owns the runtime and joins publications before releasing its
|
|
54
|
+
// cross-tab identity lock. it grants neither management nor connector access.
|
|
55
|
+
export async function enrollPortablePublisher(options, loadPublisher) {
|
|
56
|
+
const parsed = enrollmentSchema.safeParse(options);
|
|
57
|
+
if (!parsed.success)
|
|
58
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
59
|
+
const selected = parsed.data;
|
|
60
|
+
if (selected.controlUrl !== undefined) {
|
|
61
|
+
const server = new URL(selected.controlUrl);
|
|
62
|
+
if (server.protocol !== "https:" || server.origin !== selected.controlUrl)
|
|
63
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
64
|
+
}
|
|
65
|
+
const lifetime = new AbortController();
|
|
66
|
+
const signal = selected.signal === undefined
|
|
67
|
+
? lifetime.signal
|
|
68
|
+
: AbortSignal.any([lifetime.signal, selected.signal]);
|
|
69
|
+
signal.throwIfAborted();
|
|
70
|
+
let ownedStorage;
|
|
71
|
+
const storage = createPublisherStorageHost(selected.publisherStorage ?? (ownedStorage = createBrowserPublisherStorage()));
|
|
72
|
+
let runtime;
|
|
73
|
+
let raw;
|
|
74
|
+
let invalid;
|
|
75
|
+
let closed = false;
|
|
76
|
+
let closing;
|
|
77
|
+
const pending = new Set();
|
|
78
|
+
const publications = new Set();
|
|
79
|
+
const close = () => {
|
|
80
|
+
closed = true;
|
|
81
|
+
signal.removeEventListener("abort", aborted);
|
|
82
|
+
lifetime.abort();
|
|
83
|
+
closing ??= (async () => {
|
|
84
|
+
const stopping = [...publications];
|
|
85
|
+
const results = await Promise.allSettled([...pending, ...stopping.map((p) => p.close())]);
|
|
86
|
+
const alreadyStopping = new Set(stopping);
|
|
87
|
+
results.push(...(await Promise.allSettled([...publications].filter((p) => !alreadyStopping.has(p)).map((p) => p.close()))));
|
|
88
|
+
const owner = raw ?? invalid;
|
|
89
|
+
if (owner !== undefined)
|
|
90
|
+
results.push(...(await Promise.allSettled([Promise.resolve().then(() => owner.close())])));
|
|
91
|
+
if (runtime !== undefined) {
|
|
92
|
+
const current = runtime;
|
|
93
|
+
results.push(...(await Promise.allSettled([
|
|
94
|
+
Promise.resolve().then(() => current.bridge.close()),
|
|
95
|
+
current.exited,
|
|
96
|
+
])));
|
|
97
|
+
}
|
|
98
|
+
if (ownedStorage !== undefined) {
|
|
99
|
+
try {
|
|
100
|
+
await ownedStorage.close();
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
results.push({
|
|
104
|
+
status: "rejected",
|
|
105
|
+
reason: new TnlError("sdk.publication_cleanup_failed"),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const errors = results.flatMap((r) => r.status === "rejected" && !(r.reason instanceof Error && r.reason.name === "AbortError")
|
|
110
|
+
? [publicationError(r.reason, "sdk.publication_cleanup_failed")]
|
|
111
|
+
: []);
|
|
112
|
+
if (errors.length > 0)
|
|
113
|
+
throw new AggregateError(errors, "publisher cleanup failed");
|
|
114
|
+
})();
|
|
115
|
+
return closing;
|
|
116
|
+
};
|
|
117
|
+
const aborted = () => {
|
|
118
|
+
void close().catch(() => { });
|
|
119
|
+
};
|
|
120
|
+
try {
|
|
121
|
+
runtime = await loadPublisher();
|
|
122
|
+
signal.throwIfAborted();
|
|
123
|
+
if (typeof runtime.bridge.enrollPublisher !== "function")
|
|
124
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
125
|
+
const value = await runtime.bridge.enrollPublisher(JSON.stringify({
|
|
126
|
+
...(selected.controlUrl === undefined ? {} : { control_url: selected.controlUrl }),
|
|
127
|
+
instance: selected.instance ?? "default",
|
|
128
|
+
}), {
|
|
129
|
+
storage,
|
|
130
|
+
controlFetch: globalThis.fetch.bind(globalThis),
|
|
131
|
+
signal,
|
|
132
|
+
...(selected.enrollment === undefined
|
|
133
|
+
? {}
|
|
134
|
+
: { enrollment: bindEnrollment(selected.enrollment, signal) }),
|
|
135
|
+
});
|
|
136
|
+
if (!isEnrolledPublisher(value)) {
|
|
137
|
+
if (typeof value === "object" &&
|
|
138
|
+
value !== null &&
|
|
139
|
+
"close" in value &&
|
|
140
|
+
typeof value.close === "function") {
|
|
141
|
+
const closeInvalid = value.close.bind(value);
|
|
142
|
+
invalid = { close: async () => closeInvalid() };
|
|
143
|
+
}
|
|
144
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
145
|
+
}
|
|
146
|
+
raw = value;
|
|
147
|
+
const owner = value;
|
|
148
|
+
signal.throwIfAborted();
|
|
149
|
+
signal.addEventListener("abort", aborted, { once: true });
|
|
150
|
+
const handle = Object.freeze({
|
|
151
|
+
publisherIdentityID: raw.publisherIdentityID,
|
|
152
|
+
deploymentID: raw.deploymentID,
|
|
153
|
+
controlUrl: raw.controlUrl,
|
|
154
|
+
async publish(name, handler, options = {}) {
|
|
155
|
+
if (closed)
|
|
156
|
+
throw new TnlError("sdk.client_closed");
|
|
157
|
+
if (!serviceNameSchema.safeParse(name).success || typeof handler !== "function")
|
|
158
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
159
|
+
if (pending.size + publications.size >= 32)
|
|
160
|
+
throw new TnlError("sdk.client_capacity");
|
|
161
|
+
const parsed = portablePublicationOptions(options, true);
|
|
162
|
+
const work = publishPortable({
|
|
163
|
+
bridge: owner,
|
|
164
|
+
selection: {
|
|
165
|
+
service: name,
|
|
166
|
+
...(selected.resources === undefined
|
|
167
|
+
? {}
|
|
168
|
+
: { resources: publisherResourceSelection(selected.resources) }),
|
|
169
|
+
},
|
|
170
|
+
handler,
|
|
171
|
+
storage,
|
|
172
|
+
signal: parsed.signal === undefined ? signal : AbortSignal.any([signal, parsed.signal]),
|
|
173
|
+
certificate: parsed.certificate,
|
|
174
|
+
retain: (p) => publications.add(p),
|
|
175
|
+
release: (p) => publications.delete(p),
|
|
176
|
+
});
|
|
177
|
+
pending.add(work);
|
|
178
|
+
void work.then(() => pending.delete(work), () => pending.delete(work));
|
|
179
|
+
return work;
|
|
180
|
+
},
|
|
181
|
+
close,
|
|
182
|
+
});
|
|
183
|
+
owners.add(handle);
|
|
184
|
+
return handle;
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
const failure = publicationError(error);
|
|
188
|
+
try {
|
|
189
|
+
await close();
|
|
190
|
+
}
|
|
191
|
+
catch (cleanup) {
|
|
192
|
+
throw new AggregateError([failure, cleanup], "publisher setup and cleanup failed");
|
|
193
|
+
}
|
|
194
|
+
throw failure;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type TnlErrorCode } from "../../errors.js";
|
|
2
|
+
import type { TnlRatePeriod } from "../../config.gen.js";
|
|
3
|
+
import type { WasmBridge } from "../connector/wasm_abi.js";
|
|
4
|
+
import type { FetchHandler } from "../publisher/fetch_types.js";
|
|
5
|
+
import type { Publication, NamedPublicationOptions, TemporaryPublicationOptions } from "../publisher/publication.js";
|
|
6
|
+
import type { PublisherStorageHost } from "./storage-host.js";
|
|
7
|
+
import type { publisherResourceSelection } from "../runtime_resources.js";
|
|
8
|
+
import { type ServiceCertificateSource } from "../publication_certificate.js";
|
|
9
|
+
export declare function portablePublicationOptions(options: NamedPublicationOptions | TemporaryPublicationOptions, named: boolean): {
|
|
10
|
+
signal: AbortSignal | undefined;
|
|
11
|
+
certificate: Readonly<{
|
|
12
|
+
certificatePEM: string;
|
|
13
|
+
privateKeyPEM: string;
|
|
14
|
+
}> | ((signal: AbortSignal) => import("../publication_certificate.js").ServiceCertificate | Promise<import("../publication_certificate.js").ServiceCertificate>) | undefined;
|
|
15
|
+
temporary: {
|
|
16
|
+
public?: boolean;
|
|
17
|
+
require_post_quantum?: boolean;
|
|
18
|
+
access_mode?: "anonymous" | "restricted";
|
|
19
|
+
ip?: string[];
|
|
20
|
+
limits?: {
|
|
21
|
+
total?: number;
|
|
22
|
+
concurrency?: number;
|
|
23
|
+
rate?: {
|
|
24
|
+
count: number;
|
|
25
|
+
per: TnlRatePeriod;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export declare function publicationError(cause: unknown, fallback?: TnlErrorCode): Error;
|
|
31
|
+
export interface PortablePublication extends Publication {
|
|
32
|
+
/** closed has settled: go joined the run, requests, storage and cleanup. */
|
|
33
|
+
readonly joined: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare function publishPortable(options: {
|
|
36
|
+
readonly bridge: Pick<WasmBridge, "publish">;
|
|
37
|
+
readonly selection: {
|
|
38
|
+
readonly resources?: ReturnType<typeof publisherResourceSelection>;
|
|
39
|
+
} & ({
|
|
40
|
+
readonly project: string;
|
|
41
|
+
readonly deployment: string;
|
|
42
|
+
readonly credential: string;
|
|
43
|
+
readonly service?: string;
|
|
44
|
+
readonly temporary?: ReturnType<typeof portablePublicationOptions>["temporary"];
|
|
45
|
+
} | {
|
|
46
|
+
readonly service: string;
|
|
47
|
+
});
|
|
48
|
+
readonly handler: FetchHandler;
|
|
49
|
+
readonly storage: PublisherStorageHost;
|
|
50
|
+
readonly signal: AbortSignal;
|
|
51
|
+
readonly certificate?: ServiceCertificateSource | undefined;
|
|
52
|
+
readonly retain: (publication: PortablePublication) => void;
|
|
53
|
+
readonly release: (publication: PortablePublication) => void;
|
|
54
|
+
}): Promise<PortablePublication>;
|