@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,545 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { TnlError } from "../../errors.js";
|
|
3
|
+
import { hostnameSchema, protocolSchema, serviceNameSchema } from "../values.js";
|
|
4
|
+
import { clientFactory } from "./client_factory.js";
|
|
5
|
+
import { isWasmByteStream, wasmStream } from "./wasm_stream.js";
|
|
6
|
+
import { portablePublicationOptions, publishPortable, } from "../portable/publication.js";
|
|
7
|
+
import { createPublisherStorageHost } from "../portable/storage-host.js";
|
|
8
|
+
import { createBrowserPublisherStorage } from "../portable/storage-browser.js";
|
|
9
|
+
import { connectorResourcesSchema, publisherResourcesSchema, connectorResourceSelection, publisherResourceSelection, } from "../runtime_resources.js";
|
|
10
|
+
function isClosable(value) {
|
|
11
|
+
return (typeof value === "object" &&
|
|
12
|
+
value !== null &&
|
|
13
|
+
"close" in value &&
|
|
14
|
+
typeof value.close === "function");
|
|
15
|
+
}
|
|
16
|
+
function isPublisherStorage(value) {
|
|
17
|
+
return (typeof value === "object" &&
|
|
18
|
+
value !== null &&
|
|
19
|
+
"get" in value &&
|
|
20
|
+
typeof value.get === "function" &&
|
|
21
|
+
"put" in value &&
|
|
22
|
+
typeof value.put === "function" &&
|
|
23
|
+
"delete" in value &&
|
|
24
|
+
typeof value.delete === "function" &&
|
|
25
|
+
"sync" in value &&
|
|
26
|
+
typeof value.sync === "function" &&
|
|
27
|
+
"withLock" in value &&
|
|
28
|
+
typeof value.withLock === "function");
|
|
29
|
+
}
|
|
30
|
+
const optionsSchema = z
|
|
31
|
+
.strictObject({
|
|
32
|
+
project: z
|
|
33
|
+
.string()
|
|
34
|
+
.regex(/^[^/]+\/[^/]+$/)
|
|
35
|
+
.exactOptional(),
|
|
36
|
+
token: z
|
|
37
|
+
.string()
|
|
38
|
+
.min(1)
|
|
39
|
+
.max(32 << 10)
|
|
40
|
+
.exactOptional(),
|
|
41
|
+
services: z
|
|
42
|
+
.record(serviceNameSchema, z.strictObject({ protocol: protocolSchema }))
|
|
43
|
+
.exactOptional(),
|
|
44
|
+
publisherStorage: z.custom(isPublisherStorage).exactOptional(),
|
|
45
|
+
connectorResources: connectorResourcesSchema.exactOptional(),
|
|
46
|
+
publisherResources: publisherResourcesSchema.exactOptional(),
|
|
47
|
+
controlUrl: z.string().min(1).max(1024).exactOptional(),
|
|
48
|
+
workload: z
|
|
49
|
+
.strictObject({
|
|
50
|
+
name: serviceNameSchema,
|
|
51
|
+
sourceFingerprint: z.string().regex(/^[0-9a-f]{64}$/),
|
|
52
|
+
assertion: z.custom((value) => typeof value === "function"),
|
|
53
|
+
})
|
|
54
|
+
.exactOptional(),
|
|
55
|
+
})
|
|
56
|
+
.refine((value) => value.workload === undefined
|
|
57
|
+
? value.services !== undefined || value.project !== undefined
|
|
58
|
+
: value.project !== undefined &&
|
|
59
|
+
value.token === undefined &&
|
|
60
|
+
value.services !== undefined &&
|
|
61
|
+
Object.keys(value.services).length > 0);
|
|
62
|
+
const metadataSchema = z.strictObject({
|
|
63
|
+
key: z.string().min(1).max(4096),
|
|
64
|
+
protocol: protocolSchema,
|
|
65
|
+
hostname: hostnameSchema,
|
|
66
|
+
});
|
|
67
|
+
function isDeployment(value) {
|
|
68
|
+
return (typeof value === "object" &&
|
|
69
|
+
value !== null &&
|
|
70
|
+
"connect" in value &&
|
|
71
|
+
typeof value.connect === "function" &&
|
|
72
|
+
"close" in value &&
|
|
73
|
+
typeof value.close === "function");
|
|
74
|
+
}
|
|
75
|
+
function isConnection(value) {
|
|
76
|
+
return (typeof value === "object" &&
|
|
77
|
+
value !== null &&
|
|
78
|
+
"metadata" in value &&
|
|
79
|
+
"open" in value &&
|
|
80
|
+
typeof value.open === "function" &&
|
|
81
|
+
"fetch" in value &&
|
|
82
|
+
typeof value.fetch === "function" &&
|
|
83
|
+
"close" in value &&
|
|
84
|
+
typeof value.close === "function");
|
|
85
|
+
}
|
|
86
|
+
function bridgeError(error) {
|
|
87
|
+
// arbitrary go/host errors can contain a credential or response body. neither
|
|
88
|
+
// their message nor their cause is part of the public binding error.
|
|
89
|
+
if (typeof error === "object" && error !== null && "code" in error) {
|
|
90
|
+
switch (error.code) {
|
|
91
|
+
case "sdk.authentication_required":
|
|
92
|
+
case "sdk.workload_assertion_unavailable":
|
|
93
|
+
case "sdk.configuration_invalid":
|
|
94
|
+
case "sdk.connection_unavailable":
|
|
95
|
+
case "sdk.client_capacity":
|
|
96
|
+
case "sdk.cleanup_failed":
|
|
97
|
+
case "sdk.wasm_invalid":
|
|
98
|
+
case "sdk.wasm_unavailable":
|
|
99
|
+
return new TnlError(error.code);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return new TnlError("sdk.connection_unavailable");
|
|
103
|
+
}
|
|
104
|
+
function throwFailures(results, ignored) {
|
|
105
|
+
const errors = [
|
|
106
|
+
...new Set(results.flatMap((result) => result.status === "rejected" && result.reason !== ignored ? [result.reason] : [])),
|
|
107
|
+
];
|
|
108
|
+
if (errors.length === 1)
|
|
109
|
+
throw errors[0];
|
|
110
|
+
if (errors.length > 1)
|
|
111
|
+
throw new AggregateError(errors, "deployment cleanup failed");
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* explicit portable credentials or a request-scoped workload assertion.
|
|
115
|
+
* no process environment, filesystem, installed owner, or local discovery.
|
|
116
|
+
*/
|
|
117
|
+
export function createStreamClient(options, loadRuntime, context = {}) {
|
|
118
|
+
const parsed = optionsSchema.safeParse(options);
|
|
119
|
+
if (!parsed.success)
|
|
120
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
121
|
+
const { project, token, publisherStorage, workload, controlUrl, connectorResources, publisherResources, } = parsed.data;
|
|
122
|
+
const protocols = new Map(Object.entries(parsed.data.services ?? {}).map(([name, service]) => [name, service.protocol]));
|
|
123
|
+
return Object.freeze({
|
|
124
|
+
deployment(name) {
|
|
125
|
+
if (name === "local")
|
|
126
|
+
throw new TnlError("sdk.capability_unsupported");
|
|
127
|
+
if ((!serviceNameSchema.safeParse(name).success && !/^dpl_[A-Za-z0-9]{22}$/.test(name)) ||
|
|
128
|
+
project === undefined ||
|
|
129
|
+
(token === undefined && workload === undefined))
|
|
130
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
131
|
+
const selectedProject = project;
|
|
132
|
+
const credential = token;
|
|
133
|
+
const closedError = new TnlError("sdk.client_closed");
|
|
134
|
+
const lifetime = new AbortController();
|
|
135
|
+
const externalAbort = () => lifetime.abort();
|
|
136
|
+
context.signal?.addEventListener("abort", externalAbort, { once: true });
|
|
137
|
+
if (context.signal?.aborted)
|
|
138
|
+
externalAbort();
|
|
139
|
+
const pending = new Set();
|
|
140
|
+
const publicationPending = new Set();
|
|
141
|
+
const factories = new Set();
|
|
142
|
+
const connections = new Set();
|
|
143
|
+
const invalidStreams = new Set();
|
|
144
|
+
const streams = new Set();
|
|
145
|
+
const publications = new Set();
|
|
146
|
+
const runtimes = new Map();
|
|
147
|
+
const startingRuntimes = new Map();
|
|
148
|
+
let backend;
|
|
149
|
+
let loading;
|
|
150
|
+
let storageHost = context.storageHost;
|
|
151
|
+
let ownedStorage;
|
|
152
|
+
let closed = false;
|
|
153
|
+
let closing;
|
|
154
|
+
let backendClosed = false;
|
|
155
|
+
function track(work) {
|
|
156
|
+
pending.add(work);
|
|
157
|
+
void work.then(() => pending.delete(work), () => pending.delete(work));
|
|
158
|
+
return work;
|
|
159
|
+
}
|
|
160
|
+
function validate(service) {
|
|
161
|
+
if (closed)
|
|
162
|
+
throw closedError;
|
|
163
|
+
const protocol = protocols.get(service);
|
|
164
|
+
if (protocol === undefined)
|
|
165
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
166
|
+
return protocol;
|
|
167
|
+
}
|
|
168
|
+
function ownedRuntime(kind) {
|
|
169
|
+
const existing = startingRuntimes.get(kind);
|
|
170
|
+
if (existing !== undefined)
|
|
171
|
+
return existing;
|
|
172
|
+
const starting = loadRuntime(kind).then((value) => {
|
|
173
|
+
runtimes.set(kind, value);
|
|
174
|
+
if (closed)
|
|
175
|
+
throw closedError;
|
|
176
|
+
return value;
|
|
177
|
+
});
|
|
178
|
+
startingRuntimes.set(kind, starting);
|
|
179
|
+
return starting;
|
|
180
|
+
}
|
|
181
|
+
function publicationStorage() {
|
|
182
|
+
if (storageHost === undefined) {
|
|
183
|
+
const storage = publisherStorage ?? (ownedStorage ??= createBrowserPublisherStorage());
|
|
184
|
+
storageHost = createPublisherStorageHost(storage);
|
|
185
|
+
}
|
|
186
|
+
return storageHost;
|
|
187
|
+
}
|
|
188
|
+
function selected() {
|
|
189
|
+
loading ??= (async () => {
|
|
190
|
+
const current = await ownedRuntime("connector");
|
|
191
|
+
if (typeof current.bridge.createDeployment !== "function")
|
|
192
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
193
|
+
const value = await current.bridge.createDeployment(
|
|
194
|
+
// registered credentials may include publish-only facts; workload
|
|
195
|
+
// exchange instead requests the exact set of selected names.
|
|
196
|
+
{
|
|
197
|
+
project: selectedProject,
|
|
198
|
+
...(connectorResources === undefined
|
|
199
|
+
? {}
|
|
200
|
+
: { resources: connectorResourceSelection(connectorResources) }),
|
|
201
|
+
name,
|
|
202
|
+
...(controlUrl === undefined ? {} : { controlUrl }),
|
|
203
|
+
...(credential === undefined ? {} : { credential }),
|
|
204
|
+
...(workload === undefined
|
|
205
|
+
? { services: [] }
|
|
206
|
+
: {
|
|
207
|
+
workload: {
|
|
208
|
+
name: workload.name,
|
|
209
|
+
sourceFingerprint: workload.sourceFingerprint,
|
|
210
|
+
},
|
|
211
|
+
services: [...protocols.keys()],
|
|
212
|
+
}),
|
|
213
|
+
}, {
|
|
214
|
+
fetch: globalThis.fetch.bind(globalThis),
|
|
215
|
+
WebSocket: globalThis.WebSocket,
|
|
216
|
+
...(workload === undefined
|
|
217
|
+
? {}
|
|
218
|
+
: {
|
|
219
|
+
assertion: async (signal) => workload.assertion(signal),
|
|
220
|
+
}),
|
|
221
|
+
}, lifetime.signal);
|
|
222
|
+
if (isClosable(value))
|
|
223
|
+
backend = value;
|
|
224
|
+
if (!isDeployment(value))
|
|
225
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
226
|
+
if (closed)
|
|
227
|
+
throw closedError;
|
|
228
|
+
return value;
|
|
229
|
+
})().catch((error) => {
|
|
230
|
+
if (error === closedError)
|
|
231
|
+
throw error;
|
|
232
|
+
if (error instanceof TnlError)
|
|
233
|
+
throw new TnlError(error.code);
|
|
234
|
+
if (error instanceof Error && error.name === "AbortError")
|
|
235
|
+
throw new DOMException("connector operation canceled", "AbortError");
|
|
236
|
+
throw bridgeError(error);
|
|
237
|
+
});
|
|
238
|
+
return loading;
|
|
239
|
+
}
|
|
240
|
+
function operationError(error, signal) {
|
|
241
|
+
if (error === closedError)
|
|
242
|
+
return closedError;
|
|
243
|
+
if (signal.aborted &&
|
|
244
|
+
(error === undefined || (error instanceof Error && error.name === "AbortError"))) {
|
|
245
|
+
if (closed)
|
|
246
|
+
return closedError;
|
|
247
|
+
return new DOMException("connector operation canceled", "AbortError");
|
|
248
|
+
}
|
|
249
|
+
// a concurrent stop must not erase an independent operation/cleanup
|
|
250
|
+
// failure. go marks cancellation with AbortError, not arbitrary text.
|
|
251
|
+
return bridgeError(error);
|
|
252
|
+
}
|
|
253
|
+
function settings(raw, metadata) {
|
|
254
|
+
const origin = `https://${metadata.hostname}`;
|
|
255
|
+
const open = (options) => {
|
|
256
|
+
if (closed)
|
|
257
|
+
return Promise.reject(closedError);
|
|
258
|
+
if (streams.size + invalidStreams.size + pending.size >= 64)
|
|
259
|
+
return Promise.reject(new TnlError("sdk.client_capacity"));
|
|
260
|
+
const signal = options?.signal === undefined
|
|
261
|
+
? lifetime.signal
|
|
262
|
+
: AbortSignal.any([lifetime.signal, options.signal]);
|
|
263
|
+
return track((async () => {
|
|
264
|
+
signal.throwIfAborted();
|
|
265
|
+
const value = await raw.open(signal);
|
|
266
|
+
if (!isWasmByteStream(value)) {
|
|
267
|
+
if (isClosable(value))
|
|
268
|
+
invalidStreams.add(value);
|
|
269
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
270
|
+
}
|
|
271
|
+
const stream = wasmStream(value, signal, () => streams.delete(stream));
|
|
272
|
+
streams.add(stream);
|
|
273
|
+
if (signal.aborted) {
|
|
274
|
+
await stream.close();
|
|
275
|
+
throw operationError(undefined, signal);
|
|
276
|
+
}
|
|
277
|
+
if (metadata.protocol !== "tcp" && metadata.protocol !== "mysql")
|
|
278
|
+
await stream.startTls();
|
|
279
|
+
return stream;
|
|
280
|
+
})().catch((error) => {
|
|
281
|
+
throw operationError(error, signal);
|
|
282
|
+
}));
|
|
283
|
+
};
|
|
284
|
+
const fetch = (input, init) => {
|
|
285
|
+
if (closed)
|
|
286
|
+
return Promise.reject(closedError);
|
|
287
|
+
if (pending.size >= 64)
|
|
288
|
+
return Promise.reject(new TnlError("sdk.client_capacity"));
|
|
289
|
+
return track((async () => {
|
|
290
|
+
let request;
|
|
291
|
+
try {
|
|
292
|
+
const requestOptions = {
|
|
293
|
+
...init,
|
|
294
|
+
...(init?.body instanceof ReadableStream ? { duplex: "half" } : {}),
|
|
295
|
+
};
|
|
296
|
+
request = new Request(input instanceof Request ? input : new URL(input.toString(), origin), requestOptions);
|
|
297
|
+
}
|
|
298
|
+
catch {
|
|
299
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
300
|
+
}
|
|
301
|
+
const url = new URL(request.url);
|
|
302
|
+
if (url.origin !== origin || url.username !== "" || url.password !== "") {
|
|
303
|
+
const rejected = new TnlError("sdk.configuration_invalid");
|
|
304
|
+
try {
|
|
305
|
+
await request.body?.cancel();
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
throw new AggregateError([rejected, new TnlError("sdk.cleanup_failed")], "request setup and cleanup failed");
|
|
309
|
+
}
|
|
310
|
+
throw rejected;
|
|
311
|
+
}
|
|
312
|
+
const signal = AbortSignal.any([lifetime.signal, request.signal]);
|
|
313
|
+
try {
|
|
314
|
+
signal.throwIfAborted();
|
|
315
|
+
const value = await raw.fetch(new Request(request, { signal, redirect: "manual" }), signal);
|
|
316
|
+
if (!(value instanceof Response))
|
|
317
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
318
|
+
return value;
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
throw operationError(error, signal);
|
|
322
|
+
}
|
|
323
|
+
})());
|
|
324
|
+
};
|
|
325
|
+
switch (metadata.protocol) {
|
|
326
|
+
case "http":
|
|
327
|
+
return Object.freeze({ url: origin, fetch, open });
|
|
328
|
+
case "kubernetes":
|
|
329
|
+
return Object.freeze({ server: origin, fetch, open });
|
|
330
|
+
default:
|
|
331
|
+
return Object.freeze({ open });
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function acquire(service) {
|
|
335
|
+
const protocol = validate(service);
|
|
336
|
+
if (pending.size >= 64)
|
|
337
|
+
throw new TnlError("sdk.client_capacity");
|
|
338
|
+
return track((async () => {
|
|
339
|
+
try {
|
|
340
|
+
const value = await (await selected()).connect(service, lifetime.signal);
|
|
341
|
+
if (isClosable(value))
|
|
342
|
+
connections.add(value);
|
|
343
|
+
if (!isConnection(value))
|
|
344
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
345
|
+
if (connections.size > 64) {
|
|
346
|
+
try {
|
|
347
|
+
await value.close();
|
|
348
|
+
connections.delete(value);
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
throw new TnlError("sdk.cleanup_failed");
|
|
352
|
+
}
|
|
353
|
+
throw new TnlError("sdk.client_capacity");
|
|
354
|
+
}
|
|
355
|
+
const metadata = metadataSchema.safeParse(value.metadata);
|
|
356
|
+
if (!metadata.success || metadata.data.protocol !== protocol)
|
|
357
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
358
|
+
if (closed)
|
|
359
|
+
throw closedError;
|
|
360
|
+
// metadata was validated against both the bridge schema and this
|
|
361
|
+
// exact declared name/protocol before the generic indexed result.
|
|
362
|
+
return {
|
|
363
|
+
key: metadata.data.key,
|
|
364
|
+
connection: settings(value, metadata.data),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
catch (error) {
|
|
368
|
+
throw operationError(error, lifetime.signal);
|
|
369
|
+
}
|
|
370
|
+
})());
|
|
371
|
+
}
|
|
372
|
+
return Object.freeze({
|
|
373
|
+
async connect(service) {
|
|
374
|
+
return (await acquire(service)).connection;
|
|
375
|
+
},
|
|
376
|
+
client(service, driver) {
|
|
377
|
+
validate(service);
|
|
378
|
+
if (factories.size >= 64)
|
|
379
|
+
throw new TnlError("sdk.client_capacity");
|
|
380
|
+
const factory = clientFactory(() => acquire(service), driver);
|
|
381
|
+
const close = factory.close.bind(factory);
|
|
382
|
+
factory.close = async () => {
|
|
383
|
+
await close();
|
|
384
|
+
factories.delete(factory);
|
|
385
|
+
};
|
|
386
|
+
factories.add(factory);
|
|
387
|
+
return factory;
|
|
388
|
+
},
|
|
389
|
+
async publish(first, second, third) {
|
|
390
|
+
if (closed)
|
|
391
|
+
throw closedError;
|
|
392
|
+
if (credential === undefined)
|
|
393
|
+
throw new TnlError("sdk.authentication_required");
|
|
394
|
+
if (publications.size + pending.size >= 64)
|
|
395
|
+
throw new TnlError("sdk.client_capacity");
|
|
396
|
+
const service = typeof first === "string" ? first : undefined;
|
|
397
|
+
const handler = typeof first === "string" ? second : first;
|
|
398
|
+
if (typeof handler !== "function" ||
|
|
399
|
+
(service !== undefined &&
|
|
400
|
+
(!serviceNameSchema.safeParse(service).success ||
|
|
401
|
+
(protocols.size > 0 && protocols.get(service) !== "http"))) ||
|
|
402
|
+
(service === undefined && (typeof second === "function" || third !== undefined)))
|
|
403
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
404
|
+
const chosen = service === undefined ? (typeof second === "function" ? undefined : second) : third;
|
|
405
|
+
const parsed = portablePublicationOptions(chosen ?? {}, service !== undefined);
|
|
406
|
+
const signal = parsed.signal === undefined
|
|
407
|
+
? lifetime.signal
|
|
408
|
+
: AbortSignal.any([lifetime.signal, parsed.signal]);
|
|
409
|
+
const work = (async () => {
|
|
410
|
+
if (signal.aborted)
|
|
411
|
+
throw operationError(undefined, signal);
|
|
412
|
+
const storage = publicationStorage();
|
|
413
|
+
const current = await ownedRuntime("publisher");
|
|
414
|
+
return publishPortable({
|
|
415
|
+
bridge: current.bridge,
|
|
416
|
+
selection: {
|
|
417
|
+
project: selectedProject,
|
|
418
|
+
deployment: name,
|
|
419
|
+
credential,
|
|
420
|
+
...(publisherResources === undefined
|
|
421
|
+
? {}
|
|
422
|
+
: { resources: publisherResourceSelection(publisherResources) }),
|
|
423
|
+
...(service === undefined ? { temporary: parsed.temporary } : { service }),
|
|
424
|
+
},
|
|
425
|
+
handler,
|
|
426
|
+
storage,
|
|
427
|
+
signal,
|
|
428
|
+
certificate: parsed.certificate,
|
|
429
|
+
retain: (publication) => publications.add(publication),
|
|
430
|
+
release: (publication) => publications.delete(publication),
|
|
431
|
+
});
|
|
432
|
+
})().catch((error) => {
|
|
433
|
+
if (closed && error instanceof Error && error.name === "AbortError")
|
|
434
|
+
throw closedError;
|
|
435
|
+
throw error;
|
|
436
|
+
});
|
|
437
|
+
publicationPending.add(work);
|
|
438
|
+
void work.then(() => publicationPending.delete(work), () => publicationPending.delete(work));
|
|
439
|
+
return track(work);
|
|
440
|
+
},
|
|
441
|
+
close() {
|
|
442
|
+
closed = true;
|
|
443
|
+
context.signal?.removeEventListener("abort", externalAbort);
|
|
444
|
+
const stoppingPublications = [...publications].map((publication) => publication.close());
|
|
445
|
+
lifetime.abort(closedError);
|
|
446
|
+
closing ??= (async () => {
|
|
447
|
+
const joining = [...pending];
|
|
448
|
+
const publicationWork = new Set(publicationPending);
|
|
449
|
+
// stop established streams immediately, including those held by a
|
|
450
|
+
// driver whose own close is still waiting on a blocked read.
|
|
451
|
+
const stopped = [...streams].map((stream) => stream.close());
|
|
452
|
+
const joined = await Promise.allSettled(joining);
|
|
453
|
+
const resourceResults = await Promise.allSettled([
|
|
454
|
+
...stopped,
|
|
455
|
+
...[...streams].map((stream) => stream.close()),
|
|
456
|
+
...[...factories].map(async (factory) => {
|
|
457
|
+
await factory.close();
|
|
458
|
+
factories.delete(factory);
|
|
459
|
+
}),
|
|
460
|
+
...[...connections].map(async (connection) => {
|
|
461
|
+
try {
|
|
462
|
+
await connection.close();
|
|
463
|
+
connections.delete(connection);
|
|
464
|
+
}
|
|
465
|
+
catch {
|
|
466
|
+
throw new TnlError("sdk.cleanup_failed");
|
|
467
|
+
}
|
|
468
|
+
}),
|
|
469
|
+
...[...invalidStreams].map(async (stream) => {
|
|
470
|
+
try {
|
|
471
|
+
await stream.close();
|
|
472
|
+
invalidStreams.delete(stream);
|
|
473
|
+
}
|
|
474
|
+
catch {
|
|
475
|
+
throw new TnlError("sdk.cleanup_failed");
|
|
476
|
+
}
|
|
477
|
+
}),
|
|
478
|
+
]);
|
|
479
|
+
const publicationResults = await Promise.allSettled([
|
|
480
|
+
...stoppingPublications,
|
|
481
|
+
...[...publications].map((publication) => publication.close()),
|
|
482
|
+
]);
|
|
483
|
+
for (const publication of publications)
|
|
484
|
+
if (publication.joined)
|
|
485
|
+
publications.delete(publication);
|
|
486
|
+
if (backend !== undefined && !backendClosed) {
|
|
487
|
+
try {
|
|
488
|
+
await backend.close();
|
|
489
|
+
backendClosed = true;
|
|
490
|
+
}
|
|
491
|
+
catch {
|
|
492
|
+
resourceResults.push({
|
|
493
|
+
status: "rejected",
|
|
494
|
+
reason: new TnlError("sdk.cleanup_failed"),
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
const results = [...joined, ...resourceResults, ...publicationResults];
|
|
499
|
+
const nonPublicationResults = [
|
|
500
|
+
...joined.filter((_, index) => {
|
|
501
|
+
const work = joining[index];
|
|
502
|
+
return work === undefined || !publicationWork.has(work);
|
|
503
|
+
}),
|
|
504
|
+
...resourceResults,
|
|
505
|
+
];
|
|
506
|
+
// failed connector/driver closes retain live callbacks for retry.
|
|
507
|
+
// publication failures can exit only after their joined result;
|
|
508
|
+
// every failure still remains in results for the caller.
|
|
509
|
+
const ownersClosed = nonPublicationResults.every((result) => result.status === "fulfilled" || result.reason === closedError);
|
|
510
|
+
if (ownersClosed &&
|
|
511
|
+
publications.size === 0 &&
|
|
512
|
+
backendClosed === (backend !== undefined)) {
|
|
513
|
+
results.push(...(await Promise.allSettled([...runtimes].map(async ([kind, runtime]) => {
|
|
514
|
+
try {
|
|
515
|
+
await runtime.bridge.close();
|
|
516
|
+
await runtime.exited;
|
|
517
|
+
runtimes.delete(kind);
|
|
518
|
+
}
|
|
519
|
+
catch {
|
|
520
|
+
throw new TnlError("sdk.cleanup_failed");
|
|
521
|
+
}
|
|
522
|
+
}))));
|
|
523
|
+
}
|
|
524
|
+
if (runtimes.size === 0 && ownedStorage !== undefined) {
|
|
525
|
+
try {
|
|
526
|
+
await ownedStorage.close();
|
|
527
|
+
ownedStorage = undefined;
|
|
528
|
+
}
|
|
529
|
+
catch {
|
|
530
|
+
results.push({
|
|
531
|
+
status: "rejected",
|
|
532
|
+
reason: new TnlError("sdk.publication_cleanup_failed"),
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
throwFailures(results, closedError);
|
|
537
|
+
})().finally(() => {
|
|
538
|
+
closing = undefined;
|
|
539
|
+
});
|
|
540
|
+
return closing;
|
|
541
|
+
},
|
|
542
|
+
});
|
|
543
|
+
},
|
|
544
|
+
});
|
|
545
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { WasmBridge } from "./wasm_abi.js";
|
|
2
|
+
interface GoRuntime {
|
|
3
|
+
argv: string[];
|
|
4
|
+
importObject: WebAssembly.Imports;
|
|
5
|
+
run(instance: WebAssembly.Instance): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
type GoConstructor = new () => GoRuntime;
|
|
8
|
+
export declare function compileWasm(response: Response): Promise<WebAssembly.Module>;
|
|
9
|
+
export interface WasmRuntime {
|
|
10
|
+
readonly bridge: WasmBridge;
|
|
11
|
+
readonly exited: Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare function startWasm(module: WebAssembly.Module, Go: GoConstructor): Promise<WasmRuntime>;
|
|
14
|
+
export declare function loadWasm(source: WebAssembly.Module | URL): Promise<WasmRuntime>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { TnlError } from "../../errors.js";
|
|
2
|
+
function isGoConstructor(value) {
|
|
3
|
+
return typeof value === "function";
|
|
4
|
+
}
|
|
5
|
+
function isBridge(value) {
|
|
6
|
+
return (typeof value === "object" &&
|
|
7
|
+
value !== null &&
|
|
8
|
+
(("createDeployment" in value && typeof value.createDeployment === "function") ||
|
|
9
|
+
("publish" in value && typeof value.publish === "function")) &&
|
|
10
|
+
"close" in value &&
|
|
11
|
+
typeof value.close === "function");
|
|
12
|
+
}
|
|
13
|
+
let runtime;
|
|
14
|
+
function goConstructor() {
|
|
15
|
+
runtime ??= import("../../wasm/wasm_exec.js")
|
|
16
|
+
.then(() => {
|
|
17
|
+
const value = Reflect.get(globalThis, "Go");
|
|
18
|
+
if (!isGoConstructor(value))
|
|
19
|
+
throw new TnlError("sdk.wasm_unavailable");
|
|
20
|
+
return value;
|
|
21
|
+
})
|
|
22
|
+
.catch(() => {
|
|
23
|
+
runtime = undefined;
|
|
24
|
+
throw new TnlError("sdk.wasm_unavailable");
|
|
25
|
+
});
|
|
26
|
+
return runtime;
|
|
27
|
+
}
|
|
28
|
+
// only immutable compiled code is shared. a deployment always receives a fresh
|
|
29
|
+
// go scheduler and private callback references, including in a worker isolate.
|
|
30
|
+
const compiled = new Map();
|
|
31
|
+
export async function compileWasm(response) {
|
|
32
|
+
// do not tee the large artifact for a fallback: an unread branch buffers the
|
|
33
|
+
// entire download. invalid wasm cannot be repaired by compiling it twice.
|
|
34
|
+
if (typeof WebAssembly.compileStreaming === "function" &&
|
|
35
|
+
response.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase() === "application/wasm")
|
|
36
|
+
return WebAssembly.compileStreaming(response);
|
|
37
|
+
return WebAssembly.compile(await response.arrayBuffer());
|
|
38
|
+
}
|
|
39
|
+
function browserModule(url) {
|
|
40
|
+
const key = url.href;
|
|
41
|
+
const cached = compiled.get(key);
|
|
42
|
+
if (cached !== undefined)
|
|
43
|
+
return cached;
|
|
44
|
+
const loading = (async () => {
|
|
45
|
+
const response = await fetch(url, {
|
|
46
|
+
credentials: "omit",
|
|
47
|
+
redirect: "error",
|
|
48
|
+
signal: AbortSignal.timeout(45_000),
|
|
49
|
+
});
|
|
50
|
+
if (!response.ok)
|
|
51
|
+
throw new TnlError("sdk.wasm_unavailable");
|
|
52
|
+
return compileWasm(response);
|
|
53
|
+
})().catch(() => {
|
|
54
|
+
compiled.delete(key);
|
|
55
|
+
throw new TnlError("sdk.wasm_unavailable");
|
|
56
|
+
});
|
|
57
|
+
compiled.set(key, loading);
|
|
58
|
+
return loading;
|
|
59
|
+
}
|
|
60
|
+
// go registers synchronously before its first blocking wait. never await
|
|
61
|
+
// between clearing, running, capturing, and restoring the registration slot.
|
|
62
|
+
export async function startWasm(module, Go) {
|
|
63
|
+
try {
|
|
64
|
+
const go = new Go();
|
|
65
|
+
go.argv = ["tnl-wasm"];
|
|
66
|
+
const instance = await WebAssembly.instantiate(module, go.importObject);
|
|
67
|
+
const previous = Reflect.get(globalThis, "tnlWasm");
|
|
68
|
+
let bridge;
|
|
69
|
+
let exited;
|
|
70
|
+
try {
|
|
71
|
+
Reflect.deleteProperty(globalThis, "tnlWasm");
|
|
72
|
+
exited = go.run(instance);
|
|
73
|
+
void exited.catch(() => { });
|
|
74
|
+
const registered = Reflect.get(globalThis, "tnlWasm");
|
|
75
|
+
if (!isBridge(registered))
|
|
76
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
77
|
+
bridge = registered;
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
if (previous === undefined)
|
|
81
|
+
Reflect.deleteProperty(globalThis, "tnlWasm");
|
|
82
|
+
else
|
|
83
|
+
Reflect.set(globalThis, "tnlWasm", previous);
|
|
84
|
+
}
|
|
85
|
+
return { bridge, exited };
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (error instanceof TnlError)
|
|
89
|
+
throw error;
|
|
90
|
+
throw new TnlError("sdk.wasm_unavailable");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export async function loadWasm(source) {
|
|
94
|
+
const [Go, selected] = await Promise.all([
|
|
95
|
+
goConstructor(),
|
|
96
|
+
source instanceof URL ? browserModule(source) : source,
|
|
97
|
+
]);
|
|
98
|
+
return startWasm(selected, Go);
|
|
99
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { TnlTLSStream } from "../../project.js";
|
|
2
|
+
import { type WasmByteStream } from "./wasm_abi.js";
|
|
3
|
+
export declare function isWasmByteStream(value: unknown): value is WasmByteStream;
|
|
4
|
+
export declare function wasmStream(raw: WasmByteStream, signal: AbortSignal, released: () => void): TnlTLSStream;
|