@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,304 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { TnlError } from "../../errors.js";
|
|
3
|
+
import { preserveRequestHeaders } from "./request.js";
|
|
4
|
+
import { httpsOriginSchema } from "../values.js";
|
|
5
|
+
import { publicationRunSchema, publisherURLSchema } from "../publication_run.js";
|
|
6
|
+
import { exportedCertificateSchema, resolveServiceCertificate, serviceCertificateSourceSchema, } from "../publication_certificate.js";
|
|
7
|
+
const optionsSchema = z.strictObject({
|
|
8
|
+
targetHostname: z.string().min(1).exactOptional(),
|
|
9
|
+
telemetry: z.boolean().exactOptional(),
|
|
10
|
+
signal: z.instanceof(AbortSignal).exactOptional(),
|
|
11
|
+
certificate: serviceCertificateSourceSchema.exactOptional(),
|
|
12
|
+
requirePostQuantum: z.boolean().exactOptional(),
|
|
13
|
+
public: z.boolean().exactOptional(),
|
|
14
|
+
access: z
|
|
15
|
+
.union([
|
|
16
|
+
z.enum(["restricted", "anonymous"]),
|
|
17
|
+
z.strictObject({
|
|
18
|
+
mode: z.enum(["restricted", "anonymous"]).exactOptional(),
|
|
19
|
+
ip: z.array(z.string().min(1).max(64)).max(32).exactOptional(),
|
|
20
|
+
}),
|
|
21
|
+
])
|
|
22
|
+
.exactOptional(),
|
|
23
|
+
limits: z
|
|
24
|
+
.strictObject({
|
|
25
|
+
requests: z.number().int().positive().exactOptional(),
|
|
26
|
+
concurrency: z.number().int().positive().exactOptional(),
|
|
27
|
+
rate: z
|
|
28
|
+
.strictObject({
|
|
29
|
+
requests: z.number().int().positive(),
|
|
30
|
+
per: z.custom((value) => typeof value === "string" &&
|
|
31
|
+
/^(?:[0-9]+(?:\.[0-9]+)?(?:ns|us|µs|ms|s|m|h)){1,4}$/.test(value)),
|
|
32
|
+
})
|
|
33
|
+
.exactOptional(),
|
|
34
|
+
})
|
|
35
|
+
.exactOptional(),
|
|
36
|
+
});
|
|
37
|
+
export function portablePublicationOptions(options, named) {
|
|
38
|
+
const parsed = optionsSchema.safeParse(options);
|
|
39
|
+
if (!parsed.success ||
|
|
40
|
+
(named &&
|
|
41
|
+
Object.keys(options).some((key) => !["targetHostname", "telemetry", "signal", "certificate"].includes(key))))
|
|
42
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
43
|
+
// there is no listener hostname or native telemetry process in this host.
|
|
44
|
+
if (parsed.data.targetHostname !== undefined || parsed.data.telemetry === true)
|
|
45
|
+
throw new TnlError("sdk.capability_unsupported");
|
|
46
|
+
const { access, limits, requirePostQuantum, signal } = parsed.data;
|
|
47
|
+
return {
|
|
48
|
+
signal,
|
|
49
|
+
certificate: parsed.data.certificate,
|
|
50
|
+
temporary: {
|
|
51
|
+
...(parsed.data.public === undefined ? {} : { public: parsed.data.public }),
|
|
52
|
+
...(requirePostQuantum === undefined ? {} : { require_post_quantum: requirePostQuantum }),
|
|
53
|
+
...(access === undefined
|
|
54
|
+
? {}
|
|
55
|
+
: {
|
|
56
|
+
access_mode: typeof access === "string" ? access : (access.mode ?? "restricted"),
|
|
57
|
+
...(typeof access === "string" || access.ip === undefined ? {} : { ip: access.ip }),
|
|
58
|
+
}),
|
|
59
|
+
...(limits === undefined
|
|
60
|
+
? {}
|
|
61
|
+
: {
|
|
62
|
+
limits: {
|
|
63
|
+
...(limits.requests === undefined ? {} : { total: limits.requests }),
|
|
64
|
+
...(limits.concurrency === undefined ? {} : { concurrency: limits.concurrency }),
|
|
65
|
+
...(limits.rate === undefined
|
|
66
|
+
? {}
|
|
67
|
+
: { rate: { count: limits.rate.requests, per: limits.rate.per } }),
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function isPromise(value) {
|
|
74
|
+
return (typeof value === "object" &&
|
|
75
|
+
value !== null &&
|
|
76
|
+
"then" in value &&
|
|
77
|
+
typeof value.then === "function");
|
|
78
|
+
}
|
|
79
|
+
function isPublication(value) {
|
|
80
|
+
return (typeof value === "object" &&
|
|
81
|
+
value !== null &&
|
|
82
|
+
"ready" in value &&
|
|
83
|
+
isPromise(value.ready) &&
|
|
84
|
+
"closed" in value &&
|
|
85
|
+
isPromise(value.closed) &&
|
|
86
|
+
"close" in value &&
|
|
87
|
+
typeof value.close === "function");
|
|
88
|
+
}
|
|
89
|
+
export function publicationError(cause, fallback = "sdk.publication_failed") {
|
|
90
|
+
if (cause instanceof Error && cause.name === "AbortError")
|
|
91
|
+
return new DOMException("publication canceled", "AbortError");
|
|
92
|
+
if (typeof cause === "object" && cause !== null && "code" in cause) {
|
|
93
|
+
switch (cause.code) {
|
|
94
|
+
case "sdk.authentication_required":
|
|
95
|
+
case "sdk.configuration_invalid":
|
|
96
|
+
case "sdk.client_capacity":
|
|
97
|
+
case "sdk.wasm_invalid":
|
|
98
|
+
case "sdk.wasm_unavailable":
|
|
99
|
+
case "sdk.publication_failed":
|
|
100
|
+
case "sdk.certificate_invalid":
|
|
101
|
+
case "sdk.certificate_unavailable":
|
|
102
|
+
case "sdk.publication_cleanup_failed":
|
|
103
|
+
return new TnlError(cause.code);
|
|
104
|
+
case "sdk.cleanup_failed":
|
|
105
|
+
return new TnlError("sdk.publication_cleanup_failed");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return new TnlError(fallback);
|
|
109
|
+
}
|
|
110
|
+
function failures(results) {
|
|
111
|
+
return [
|
|
112
|
+
...new Set(results.flatMap((result) => (result.status === "rejected" ? [result.reason] : []))),
|
|
113
|
+
];
|
|
114
|
+
}
|
|
115
|
+
function throwFailures(errors) {
|
|
116
|
+
if (errors.length === 1)
|
|
117
|
+
throw errors[0];
|
|
118
|
+
if (errors.length > 1)
|
|
119
|
+
throw new AggregateError(errors, "publication operation and cleanup failed");
|
|
120
|
+
}
|
|
121
|
+
export async function publishPortable(options) {
|
|
122
|
+
const lifetime = new AbortController();
|
|
123
|
+
const signal = AbortSignal.any([options.signal, lifetime.signal]);
|
|
124
|
+
const certificateSource = options.certificate;
|
|
125
|
+
if (signal.aborted)
|
|
126
|
+
throw new DOMException("publication canceled", "AbortError");
|
|
127
|
+
if (typeof options.bridge.publish !== "function")
|
|
128
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
129
|
+
let raw;
|
|
130
|
+
try {
|
|
131
|
+
raw = await options.bridge.publish(JSON.stringify(options.selection), {
|
|
132
|
+
storage: options.storage,
|
|
133
|
+
controlFetch: globalThis.fetch.bind(globalThis),
|
|
134
|
+
signal,
|
|
135
|
+
...(certificateSource === undefined
|
|
136
|
+
? {}
|
|
137
|
+
: {
|
|
138
|
+
certificate: async (signal) => resolveServiceCertificate(certificateSource, AbortSignal.any([signal, lifetime.signal, options.signal])),
|
|
139
|
+
}),
|
|
140
|
+
async fetch(request) {
|
|
141
|
+
if (signal.aborted)
|
|
142
|
+
throw new DOMException("publication canceled", "AbortError");
|
|
143
|
+
try {
|
|
144
|
+
if (!(request instanceof Request))
|
|
145
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
146
|
+
const result = await options.handler(preserveRequestHeaders(request));
|
|
147
|
+
if (!(result instanceof Response))
|
|
148
|
+
throw new TnlError("sdk.publication_failed");
|
|
149
|
+
// keep the actual Response, including headers attached by response().
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
throw new TnlError("sdk.publication_failed");
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
throw publicationError(error);
|
|
160
|
+
}
|
|
161
|
+
if (!isPublication(raw)) {
|
|
162
|
+
// even malformed handles may have acquired a run. never silently drop a
|
|
163
|
+
// usable cleanup capability because another field failed validation.
|
|
164
|
+
const invalid = new TnlError("sdk.wasm_invalid");
|
|
165
|
+
if (typeof raw === "object" &&
|
|
166
|
+
raw !== null &&
|
|
167
|
+
"close" in raw &&
|
|
168
|
+
typeof raw.close === "function") {
|
|
169
|
+
try {
|
|
170
|
+
await raw.close();
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
throw new AggregateError([invalid, new TnlError("sdk.publication_cleanup_failed")], "publication setup and cleanup failed");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
throw invalid;
|
|
177
|
+
}
|
|
178
|
+
let joined = false;
|
|
179
|
+
let stopped = false;
|
|
180
|
+
let closing;
|
|
181
|
+
const completion = Promise.resolve(raw.closed).then(() => {
|
|
182
|
+
joined = true;
|
|
183
|
+
signal.removeEventListener("abort", aborted);
|
|
184
|
+
lifetime.abort();
|
|
185
|
+
}, (error) => {
|
|
186
|
+
joined = true;
|
|
187
|
+
signal.removeEventListener("abort", aborted);
|
|
188
|
+
lifetime.abort();
|
|
189
|
+
throw publicationError(error);
|
|
190
|
+
});
|
|
191
|
+
void completion.catch(() => { });
|
|
192
|
+
const base = raw;
|
|
193
|
+
let run;
|
|
194
|
+
let publisherURL;
|
|
195
|
+
function close() {
|
|
196
|
+
signal.removeEventListener("abort", aborted);
|
|
197
|
+
// fence the application callback before asking go to stop. a saved host
|
|
198
|
+
// function cannot start another handler after this publication is closed.
|
|
199
|
+
lifetime.abort();
|
|
200
|
+
if (closing !== undefined)
|
|
201
|
+
return closing;
|
|
202
|
+
closing = (async () => {
|
|
203
|
+
const results = await Promise.allSettled([
|
|
204
|
+
Promise.resolve()
|
|
205
|
+
.then(() => base.close())
|
|
206
|
+
.catch((error) => {
|
|
207
|
+
throw publicationError(error, "sdk.publication_cleanup_failed");
|
|
208
|
+
}),
|
|
209
|
+
completion,
|
|
210
|
+
]);
|
|
211
|
+
stopped = joined;
|
|
212
|
+
const errors = failures(results);
|
|
213
|
+
if (errors.length === 0)
|
|
214
|
+
options.release(publication);
|
|
215
|
+
throwFailures(errors);
|
|
216
|
+
})().finally(() => {
|
|
217
|
+
if (!stopped)
|
|
218
|
+
closing = undefined;
|
|
219
|
+
});
|
|
220
|
+
return closing;
|
|
221
|
+
}
|
|
222
|
+
function aborted() {
|
|
223
|
+
void close().catch(() => { });
|
|
224
|
+
}
|
|
225
|
+
const publicEndpoint = httpsOriginSchema.safeParse(raw.url);
|
|
226
|
+
const expectedPublic = options.selection.service === undefined
|
|
227
|
+
? "temporary" in options.selection
|
|
228
|
+
? (options.selection.temporary?.public ?? true)
|
|
229
|
+
: true
|
|
230
|
+
: undefined;
|
|
231
|
+
const validEndpoint = (expectedPublic === undefined || raw.public === expectedPublic) &&
|
|
232
|
+
((raw.public === true && publicEndpoint.success) || (raw.public === false && !("url" in raw)));
|
|
233
|
+
const publication = {
|
|
234
|
+
...(raw.public === true && publicEndpoint.success ? { url: publicEndpoint.data } : {}),
|
|
235
|
+
get joined() {
|
|
236
|
+
return joined;
|
|
237
|
+
},
|
|
238
|
+
get run() {
|
|
239
|
+
return run;
|
|
240
|
+
},
|
|
241
|
+
async exportCertificate() {
|
|
242
|
+
if (signal.aborted || joined)
|
|
243
|
+
throw new TnlError("sdk.certificate_unavailable");
|
|
244
|
+
if (typeof base.exportCertificate !== "function")
|
|
245
|
+
throw new TnlError("sdk.capability_unsupported");
|
|
246
|
+
try {
|
|
247
|
+
const value = exportedCertificateSchema.parse(await base.exportCertificate());
|
|
248
|
+
if (signal.aborted || joined)
|
|
249
|
+
throw new TnlError("sdk.certificate_unavailable");
|
|
250
|
+
return value;
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
// never surface a bridge or schema error containing private material.
|
|
254
|
+
throw new TnlError("sdk.certificate_unavailable");
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
close,
|
|
258
|
+
wait: () => completion,
|
|
259
|
+
[Symbol.asyncDispose]: close,
|
|
260
|
+
};
|
|
261
|
+
if (publication.url !== undefined)
|
|
262
|
+
Object.defineProperty(publication, "publisherURL", {
|
|
263
|
+
get: () => publisherURL,
|
|
264
|
+
enumerable: true,
|
|
265
|
+
});
|
|
266
|
+
Object.freeze(publication);
|
|
267
|
+
options.retain(publication);
|
|
268
|
+
signal.addEventListener("abort", aborted, { once: true });
|
|
269
|
+
if (signal.aborted)
|
|
270
|
+
aborted();
|
|
271
|
+
try {
|
|
272
|
+
if (!validEndpoint)
|
|
273
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
274
|
+
await raw.ready;
|
|
275
|
+
if (signal.aborted)
|
|
276
|
+
throw new DOMException("publication canceled", "AbortError");
|
|
277
|
+
const reference = publicationRunSchema.safeParse(raw.run);
|
|
278
|
+
if (!reference.success)
|
|
279
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
280
|
+
run = Object.freeze(reference.data);
|
|
281
|
+
if (raw.publisherURL !== undefined) {
|
|
282
|
+
const parsed = publisherURLSchema.safeParse(raw.publisherURL);
|
|
283
|
+
if (!parsed.success || publication.url === undefined || parsed.data === publication.url)
|
|
284
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
285
|
+
publisherURL = parsed.data;
|
|
286
|
+
}
|
|
287
|
+
return publication;
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
const canceled = signal.aborted;
|
|
291
|
+
const cleanup = await Promise.allSettled([close()]);
|
|
292
|
+
const cleanupErrors = failures(cleanup);
|
|
293
|
+
// go's ready promise may report "stopped before readiness" after a clean
|
|
294
|
+
// stop. successful close plus closed is the joined lifecycle result; only
|
|
295
|
+
// that result can distinguish cancellation from an independent failure.
|
|
296
|
+
const original = canceled &&
|
|
297
|
+
cleanupErrors.length === 0 &&
|
|
298
|
+
!(error instanceof TnlError && error.code === "sdk.wasm_invalid")
|
|
299
|
+
? new DOMException("publication canceled", "AbortError")
|
|
300
|
+
: publicationError(error);
|
|
301
|
+
throwFailures([original, ...cleanupErrors]);
|
|
302
|
+
throw original;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function preserveRequestHeaders(request: Request): Request;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const wrappedRequests = new WeakSet();
|
|
2
|
+
// go supplies a genuine Request with raw headers attached: browser Request
|
|
3
|
+
// construction otherwise filters Cookie. native clone tees the body but copies
|
|
4
|
+
// only internal filtered headers, so carry the raw headers onto every clone.
|
|
5
|
+
// this cannot recover headers already lost by an ordinary Request constructor.
|
|
6
|
+
export function preserveRequestHeaders(request) {
|
|
7
|
+
if (wrappedRequests.has(request))
|
|
8
|
+
return request;
|
|
9
|
+
const headers = request.headers;
|
|
10
|
+
const nativeClone = request.clone;
|
|
11
|
+
Object.defineProperty(request, "clone", {
|
|
12
|
+
value: () => {
|
|
13
|
+
const copy = nativeClone.call(request);
|
|
14
|
+
Object.defineProperty(copy, "headers", { value: new Headers(headers) });
|
|
15
|
+
return preserveRequestHeaders(copy);
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
wrappedRequests.add(request);
|
|
19
|
+
return request;
|
|
20
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type PublisherStorage } from "./storage.js";
|
|
2
|
+
export interface BrowserPublisherStorageOptions {
|
|
3
|
+
readonly database?: string;
|
|
4
|
+
readonly indexedDB?: IDBFactory;
|
|
5
|
+
readonly locks?: LockManager;
|
|
6
|
+
}
|
|
7
|
+
export declare function createBrowserPublisherStorage(options?: BrowserPublisherStorageOptions): PublisherStorage & {
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { PublisherStorageError, storageBytes, storageKey, throwIfStorageAborted, } from "./storage.js";
|
|
2
|
+
// indexeddb provides atomic record replacement; web locks cover the longer
|
|
3
|
+
// read/prepare/network/write sequence across tabs and workers on the same origin.
|
|
4
|
+
// this storage is private to the origin, not a defense against same-origin code.
|
|
5
|
+
export function createBrowserPublisherStorage(options = {}) {
|
|
6
|
+
const factory = options.indexedDB ?? globalThis.indexedDB;
|
|
7
|
+
const locks = options.locks ?? globalThis.navigator?.locks;
|
|
8
|
+
if (factory === undefined || locks === undefined || typeof locks.request !== "function")
|
|
9
|
+
throw new PublisherStorageError();
|
|
10
|
+
const databaseName = storageKey(options.database ?? "tnl-publisher-v1");
|
|
11
|
+
let opening;
|
|
12
|
+
let closed = false;
|
|
13
|
+
const database = () => {
|
|
14
|
+
if (closed)
|
|
15
|
+
return Promise.reject(new PublisherStorageError());
|
|
16
|
+
opening ??= new Promise((resolve, reject) => {
|
|
17
|
+
let request;
|
|
18
|
+
try {
|
|
19
|
+
request = factory.open(databaseName, 1);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
reject(new PublisherStorageError());
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
let failed = false;
|
|
26
|
+
const fail = () => {
|
|
27
|
+
failed = true;
|
|
28
|
+
reject(new PublisherStorageError());
|
|
29
|
+
};
|
|
30
|
+
request.onblocked = fail;
|
|
31
|
+
request.onerror = fail;
|
|
32
|
+
request.onupgradeneeded = () => {
|
|
33
|
+
if (!request.result.objectStoreNames.contains("records"))
|
|
34
|
+
request.result.createObjectStore("records");
|
|
35
|
+
};
|
|
36
|
+
request.onsuccess = () => {
|
|
37
|
+
const db = request.result;
|
|
38
|
+
if (failed || closed) {
|
|
39
|
+
db.close();
|
|
40
|
+
fail();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
db.onversionchange = () => {
|
|
44
|
+
closed = true;
|
|
45
|
+
db.close();
|
|
46
|
+
};
|
|
47
|
+
resolve(db);
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
return opening;
|
|
51
|
+
};
|
|
52
|
+
const transaction = async (mode, operation) => {
|
|
53
|
+
const db = await database();
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
try {
|
|
56
|
+
const tx = db.transaction("records", mode, mode === "readwrite" ? { durability: "strict" } : {});
|
|
57
|
+
const request = operation(tx.objectStore("records"));
|
|
58
|
+
// request success alone is not a commit: quota and i/o failures can still
|
|
59
|
+
// abort the transaction after the put request has succeeded.
|
|
60
|
+
tx.oncomplete = () => resolve(request.result);
|
|
61
|
+
tx.onabort = () => reject(new PublisherStorageError());
|
|
62
|
+
tx.onerror = () => reject(new PublisherStorageError());
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
reject(new PublisherStorageError());
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
return Object.freeze({
|
|
70
|
+
async get(key) {
|
|
71
|
+
const value = await transaction("readonly", (store) => store.get(storageKey(key)));
|
|
72
|
+
return value === undefined ? undefined : storageBytes(value);
|
|
73
|
+
},
|
|
74
|
+
async put(key, value) {
|
|
75
|
+
const record = storageBytes(value);
|
|
76
|
+
await transaction("readwrite", (store) => store.put(record, storageKey(key)));
|
|
77
|
+
},
|
|
78
|
+
async delete(key) {
|
|
79
|
+
await transaction("readwrite", (store) => store.delete(storageKey(key)));
|
|
80
|
+
},
|
|
81
|
+
async sync(key) {
|
|
82
|
+
storageKey(key);
|
|
83
|
+
// put resolves only after a strict-durability transaction commits.
|
|
84
|
+
await database();
|
|
85
|
+
},
|
|
86
|
+
async withLock(name, operation, signal) {
|
|
87
|
+
throwIfStorageAborted(signal);
|
|
88
|
+
let entered = false;
|
|
89
|
+
try {
|
|
90
|
+
return await locks.request(`tnl-publisher:${JSON.stringify([databaseName, storageKey(name)])}`, { mode: "exclusive", ...(signal === undefined ? {} : { signal }) }, async () => {
|
|
91
|
+
entered = true;
|
|
92
|
+
throwIfStorageAborted(signal);
|
|
93
|
+
return operation();
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (entered)
|
|
98
|
+
throw error;
|
|
99
|
+
throwIfStorageAborted(signal);
|
|
100
|
+
throw new PublisherStorageError();
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
async close() {
|
|
104
|
+
closed = true;
|
|
105
|
+
if (opening !== undefined) {
|
|
106
|
+
try {
|
|
107
|
+
(await opening).close();
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// opening already reported the storage failure.
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type PublisherStorage } from "./storage.js";
|
|
2
|
+
export type PublisherSQLiteValue = string | number | null | ArrayBuffer;
|
|
3
|
+
export interface PublisherSQLiteStorage {
|
|
4
|
+
readonly sql: {
|
|
5
|
+
exec<T extends Record<string, PublisherSQLiteValue>>(query: string, ...bindings: PublisherSQLiteValue[]): {
|
|
6
|
+
toArray(): T[];
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
sync(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export declare function createDurableObjectPublisherStorage(storage: PublisherSQLiteStorage, namespace?: string): PublisherStorage;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { PublisherStorageError, storageBytes, storageKey, throwIfStorageAborted, } from "./storage.js";
|
|
2
|
+
const lockQueues = new WeakMap();
|
|
3
|
+
export function createDurableObjectPublisherStorage(storage, namespace = "default") {
|
|
4
|
+
const selectedNamespace = storageKey(namespace);
|
|
5
|
+
const prefix = `${selectedNamespace.length}:${selectedNamespace}:`;
|
|
6
|
+
if (storage?.sql === undefined ||
|
|
7
|
+
typeof storage.sql.exec !== "function" ||
|
|
8
|
+
typeof storage.sync !== "function")
|
|
9
|
+
throw new PublisherStorageError();
|
|
10
|
+
try {
|
|
11
|
+
storage.sql.exec("CREATE TABLE IF NOT EXISTS tnl_publisher_records_v1 (key TEXT PRIMARY KEY, value BLOB NOT NULL)");
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
throw new PublisherStorageError();
|
|
15
|
+
}
|
|
16
|
+
let locks = lockQueues.get(storage.sql);
|
|
17
|
+
if (locks === undefined) {
|
|
18
|
+
locks = new Map();
|
|
19
|
+
lockQueues.set(storage.sql, locks);
|
|
20
|
+
}
|
|
21
|
+
const queues = locks;
|
|
22
|
+
return Object.freeze({
|
|
23
|
+
async get(key) {
|
|
24
|
+
let rows;
|
|
25
|
+
try {
|
|
26
|
+
rows = storage.sql
|
|
27
|
+
.exec("SELECT value FROM tnl_publisher_records_v1 WHERE key = ?", prefix + storageKey(key))
|
|
28
|
+
.toArray();
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
throw new PublisherStorageError();
|
|
32
|
+
}
|
|
33
|
+
if (rows.length === 0)
|
|
34
|
+
return undefined;
|
|
35
|
+
const value = rows[0]?.value;
|
|
36
|
+
if (!(value instanceof ArrayBuffer))
|
|
37
|
+
throw new PublisherStorageError("sdk.publisher_storage_invalid");
|
|
38
|
+
return storageBytes(new Uint8Array(value));
|
|
39
|
+
},
|
|
40
|
+
async put(key, value) {
|
|
41
|
+
const record = storageBytes(value);
|
|
42
|
+
try {
|
|
43
|
+
// one sqlite statement replaces the whole record atomically.
|
|
44
|
+
storage.sql.exec("INSERT INTO tnl_publisher_records_v1 (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value", prefix + storageKey(key), record.buffer);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
throw new PublisherStorageError();
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
async delete(key) {
|
|
51
|
+
try {
|
|
52
|
+
storage.sql.exec("DELETE FROM tnl_publisher_records_v1 WHERE key = ?", prefix + storageKey(key));
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
throw new PublisherStorageError();
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
async sync(key) {
|
|
59
|
+
storageKey(key);
|
|
60
|
+
try {
|
|
61
|
+
await storage.sync();
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
throw new PublisherStorageError();
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
async withLock(name, operation, signal) {
|
|
68
|
+
throwIfStorageAborted(signal);
|
|
69
|
+
const key = prefix + storageKey(name);
|
|
70
|
+
const preceding = queues.get(key) ?? Promise.resolve();
|
|
71
|
+
let release;
|
|
72
|
+
const held = new Promise((resolve) => {
|
|
73
|
+
release = resolve;
|
|
74
|
+
});
|
|
75
|
+
const queued = preceding.then(() => held);
|
|
76
|
+
queues.set(key, queued);
|
|
77
|
+
// a durable object has one live isolate. this queue covers await
|
|
78
|
+
// interleaving within it; sqlite owns durability across isolate replacement.
|
|
79
|
+
// never hold a transaction open while a certificate request is on the network.
|
|
80
|
+
try {
|
|
81
|
+
await waitForLock(preceding, signal);
|
|
82
|
+
throwIfStorageAborted(signal);
|
|
83
|
+
return await operation();
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
release();
|
|
87
|
+
void queued.then(() => {
|
|
88
|
+
if (queues.get(key) === queued)
|
|
89
|
+
queues.delete(key);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async function waitForLock(preceding, signal) {
|
|
96
|
+
if (signal === undefined)
|
|
97
|
+
return preceding;
|
|
98
|
+
throwIfStorageAborted(signal);
|
|
99
|
+
let abort;
|
|
100
|
+
const canceled = new Promise((_resolve, reject) => {
|
|
101
|
+
abort = () => reject(new DOMException("publisher storage lock was aborted", "AbortError"));
|
|
102
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
103
|
+
});
|
|
104
|
+
try {
|
|
105
|
+
await Promise.race([preceding, canceled]);
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
signal.removeEventListener("abort", abort);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type PublisherStorage } from "./storage.js";
|
|
2
|
+
export interface PublisherStorageHost {
|
|
3
|
+
read(scopeJSON: string, key: string, limit: number, signal: AbortSignal): Promise<Uint8Array | null>;
|
|
4
|
+
write(scopeJSON: string, key: string, bytes: Uint8Array, signal: AbortSignal): Promise<void>;
|
|
5
|
+
sync(scopeJSON: string, key: string, signal: AbortSignal): Promise<void>;
|
|
6
|
+
lock(scopeJSON: string, key: string, signal: AbortSignal): Promise<() => Promise<void>>;
|
|
7
|
+
}
|
|
8
|
+
export declare function createPublisherStorageHost(storage: PublisherStorage): PublisherStorageHost;
|