@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,177 @@
|
|
|
1
|
+
import { TnlError } from "../../errors.js";
|
|
2
|
+
import { WASM_CHUNK_BYTES } from "./wasm_abi.js";
|
|
3
|
+
export function isWasmByteStream(value) {
|
|
4
|
+
return (typeof value === "object" &&
|
|
5
|
+
value !== null &&
|
|
6
|
+
"read" in value &&
|
|
7
|
+
typeof value.read === "function" &&
|
|
8
|
+
"write" in value &&
|
|
9
|
+
typeof value.write === "function" &&
|
|
10
|
+
"closeWrite" in value &&
|
|
11
|
+
typeof value.closeWrite === "function" &&
|
|
12
|
+
"startTls" in value &&
|
|
13
|
+
typeof value.startTls === "function" &&
|
|
14
|
+
"close" in value &&
|
|
15
|
+
typeof value.close === "function");
|
|
16
|
+
}
|
|
17
|
+
// web streams serialize reads and writes. zero read-ahead avoids consuming a
|
|
18
|
+
// mysql greeting or tls byte before its driver explicitly requests it.
|
|
19
|
+
export function wasmStream(raw, signal, released) {
|
|
20
|
+
const pending = new Set();
|
|
21
|
+
let stopped = false;
|
|
22
|
+
let upgrading = false;
|
|
23
|
+
let readEnded = false;
|
|
24
|
+
let writeEnded = false;
|
|
25
|
+
let closed = false;
|
|
26
|
+
let closing;
|
|
27
|
+
let reading;
|
|
28
|
+
let writing;
|
|
29
|
+
const failure = () => new TnlError("sdk.connection_unavailable");
|
|
30
|
+
function track(work) {
|
|
31
|
+
pending.add(work);
|
|
32
|
+
void work.then(() => pending.delete(work), () => pending.delete(work));
|
|
33
|
+
return work;
|
|
34
|
+
}
|
|
35
|
+
function close() {
|
|
36
|
+
stopped = true;
|
|
37
|
+
signal.removeEventListener("abort", aborted);
|
|
38
|
+
reading?.error(failure());
|
|
39
|
+
writing?.error(failure());
|
|
40
|
+
if (closed)
|
|
41
|
+
return Promise.resolve();
|
|
42
|
+
closing ??= (async () => {
|
|
43
|
+
// raw close must interrupt blocked I/O; joining before it would deadlock.
|
|
44
|
+
try {
|
|
45
|
+
await raw.close();
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new TnlError("sdk.cleanup_failed");
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
await Promise.allSettled(pending);
|
|
52
|
+
}
|
|
53
|
+
closed = true;
|
|
54
|
+
released();
|
|
55
|
+
})().finally(() => {
|
|
56
|
+
closing = undefined;
|
|
57
|
+
});
|
|
58
|
+
return closing;
|
|
59
|
+
}
|
|
60
|
+
function aborted() {
|
|
61
|
+
void close().catch(() => { });
|
|
62
|
+
}
|
|
63
|
+
const readable = new ReadableStream({
|
|
64
|
+
start(controller) {
|
|
65
|
+
reading = controller;
|
|
66
|
+
},
|
|
67
|
+
async pull(controller) {
|
|
68
|
+
if (stopped)
|
|
69
|
+
throw failure();
|
|
70
|
+
if (upgrading)
|
|
71
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
72
|
+
try {
|
|
73
|
+
const bytes = await track(raw.read(WASM_CHUNK_BYTES));
|
|
74
|
+
if (stopped)
|
|
75
|
+
return;
|
|
76
|
+
if (bytes === null) {
|
|
77
|
+
readEnded = true;
|
|
78
|
+
controller.close();
|
|
79
|
+
reading = undefined;
|
|
80
|
+
if (writeEnded)
|
|
81
|
+
await close();
|
|
82
|
+
}
|
|
83
|
+
else if (bytes instanceof Uint8Array &&
|
|
84
|
+
bytes.byteLength > 0 &&
|
|
85
|
+
bytes.byteLength <= WASM_CHUNK_BYTES) {
|
|
86
|
+
// go may reuse its crossing buffer after this call returns.
|
|
87
|
+
controller.enqueue(bytes.slice());
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new TnlError("sdk.wasm_invalid");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
const safe = error instanceof TnlError && error.code === "sdk.wasm_invalid" ? error : failure();
|
|
95
|
+
controller.error(safe);
|
|
96
|
+
reading = undefined;
|
|
97
|
+
void close().catch(() => { });
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
cancel: close,
|
|
101
|
+
}, { highWaterMark: 0 });
|
|
102
|
+
const writable = new WritableStream({
|
|
103
|
+
start(controller) {
|
|
104
|
+
writing = controller;
|
|
105
|
+
},
|
|
106
|
+
async write(bytes) {
|
|
107
|
+
if (stopped)
|
|
108
|
+
throw failure();
|
|
109
|
+
if (upgrading)
|
|
110
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
111
|
+
if (!(bytes instanceof Uint8Array))
|
|
112
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
113
|
+
try {
|
|
114
|
+
// no unbounded queue or full-payload copy. each write is acknowledged
|
|
115
|
+
// before the next 64 kib slice is handed to go.
|
|
116
|
+
for (let offset = 0; offset < bytes.byteLength; offset += WASM_CHUNK_BYTES) {
|
|
117
|
+
if (stopped)
|
|
118
|
+
throw failure();
|
|
119
|
+
await track(raw.write(bytes.subarray(offset, offset + WASM_CHUNK_BYTES)));
|
|
120
|
+
}
|
|
121
|
+
if (stopped)
|
|
122
|
+
throw failure();
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
void close().catch(() => { });
|
|
126
|
+
throw failure();
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
async close() {
|
|
130
|
+
if (stopped)
|
|
131
|
+
throw failure();
|
|
132
|
+
if (upgrading)
|
|
133
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
134
|
+
try {
|
|
135
|
+
await track(raw.closeWrite());
|
|
136
|
+
writeEnded = true;
|
|
137
|
+
writing = undefined;
|
|
138
|
+
if (readEnded)
|
|
139
|
+
await close();
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
void close().catch(() => { });
|
|
143
|
+
throw error instanceof TnlError && error.code === "sdk.cleanup_failed"
|
|
144
|
+
? error
|
|
145
|
+
: failure();
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
abort: close,
|
|
149
|
+
}, { highWaterMark: 1 });
|
|
150
|
+
signal.addEventListener("abort", aborted, { once: true });
|
|
151
|
+
if (signal.aborted)
|
|
152
|
+
aborted();
|
|
153
|
+
return Object.freeze({
|
|
154
|
+
readable,
|
|
155
|
+
writable,
|
|
156
|
+
close,
|
|
157
|
+
async startTls() {
|
|
158
|
+
if (stopped)
|
|
159
|
+
throw failure();
|
|
160
|
+
if (pending.size !== 0)
|
|
161
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
162
|
+
upgrading = true;
|
|
163
|
+
try {
|
|
164
|
+
await track(raw.startTls());
|
|
165
|
+
if (stopped)
|
|
166
|
+
throw failure();
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
void close().catch(() => { });
|
|
170
|
+
throw failure();
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
upgrading = false;
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
});
|
|
177
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
export interface WorkloadRequest {
|
|
3
|
+
selection?: {
|
|
4
|
+
project: string;
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
name?: string;
|
|
8
|
+
callback?: boolean;
|
|
9
|
+
token_file?: string;
|
|
10
|
+
runtime: {
|
|
11
|
+
workload?: string;
|
|
12
|
+
vercel?: string;
|
|
13
|
+
fly_app_name?: string;
|
|
14
|
+
github_actions?: string;
|
|
15
|
+
cloud_run_service?: string;
|
|
16
|
+
kubernetes_service_host?: string;
|
|
17
|
+
gitlab_ci?: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export interface SelectionOptions {
|
|
21
|
+
deployment?: {
|
|
22
|
+
project: string;
|
|
23
|
+
name: string;
|
|
24
|
+
token: string;
|
|
25
|
+
services?: string[];
|
|
26
|
+
};
|
|
27
|
+
requested_at?: string;
|
|
28
|
+
assertion?: string;
|
|
29
|
+
sdk_workload?: WorkloadRequest;
|
|
30
|
+
sdk_workload_policy?: string;
|
|
31
|
+
sdk_environment?: {
|
|
32
|
+
github_actions: string;
|
|
33
|
+
github_request_url: string;
|
|
34
|
+
github_request_token: string;
|
|
35
|
+
vercel_token_file: string;
|
|
36
|
+
vercel_token: string;
|
|
37
|
+
token_env?: string;
|
|
38
|
+
token_value?: string;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export declare const workloadSelectionSchema: z.ZodObject<{
|
|
42
|
+
schema: z.ZodLiteral<1>;
|
|
43
|
+
name: z.ZodString;
|
|
44
|
+
directory: z.ZodString;
|
|
45
|
+
manifest: z.ZodString;
|
|
46
|
+
policy: z.ZodString;
|
|
47
|
+
source: z.ZodEnum<{
|
|
48
|
+
callback: "callback";
|
|
49
|
+
native: "native";
|
|
50
|
+
vercel: "vercel";
|
|
51
|
+
}>;
|
|
52
|
+
token_env: z.ZodOptional<z.ZodString>;
|
|
53
|
+
}, z.core.$strict>;
|
|
54
|
+
export type WorkloadSelection = z.output<typeof workloadSelectionSchema>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { digestSchema, pathTextSchema, serviceNameSchema } from "../../values.js";
|
|
3
|
+
export const workloadSelectionSchema = z.strictObject({
|
|
4
|
+
schema: z.literal(1),
|
|
5
|
+
name: serviceNameSchema,
|
|
6
|
+
directory: pathTextSchema,
|
|
7
|
+
manifest: pathTextSchema,
|
|
8
|
+
policy: digestSchema,
|
|
9
|
+
source: z.enum(["callback", "vercel", "native"]),
|
|
10
|
+
token_env: z
|
|
11
|
+
.string()
|
|
12
|
+
.regex(/^[A-Za-z_][A-Za-z0-9_]{0,127}$/)
|
|
13
|
+
.optional(),
|
|
14
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import type { WorkloadOptions } from "../../../workload.js";
|
|
3
|
+
import { type WorkloadRequest, type WorkloadSelection, type SelectionOptions } from "./contract.js";
|
|
4
|
+
declare const workloadOptionsSchema: z.ZodReadonly<z.ZodUnion<readonly [z.ZodObject<{
|
|
5
|
+
name: z.ZodString;
|
|
6
|
+
token: z.ZodCustom<() => Promise<string>, () => Promise<string>>;
|
|
7
|
+
tokenFile: z.ZodOptional<z.ZodUndefined>;
|
|
8
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
9
|
+
name: z.ZodString;
|
|
10
|
+
tokenFile: z.ZodString;
|
|
11
|
+
token: z.ZodOptional<z.ZodUndefined>;
|
|
12
|
+
}, z.core.$strict>]>>;
|
|
13
|
+
export type WorkloadConfiguration = z.output<typeof workloadOptionsSchema>;
|
|
14
|
+
export interface WorkloadSource {
|
|
15
|
+
readonly configuration: WorkloadConfiguration | undefined;
|
|
16
|
+
readonly environment: Readonly<NodeJS.ProcessEnv>;
|
|
17
|
+
}
|
|
18
|
+
export declare function captureWorkload(key?: string): WorkloadSource;
|
|
19
|
+
/** register a fixed request-scoped source, never a mutable current token. */
|
|
20
|
+
export declare function configureWorkload(options: WorkloadOptions): void;
|
|
21
|
+
export declare function usesWorkload(key?: string, source?: WorkloadSource): boolean;
|
|
22
|
+
export declare function selectWorkload(select: (request: WorkloadRequest) => Promise<unknown>, key?: string, source?: WorkloadSource, expected?: WorkloadRequest["selection"]): Promise<SelectionOptions>;
|
|
23
|
+
export declare function acquireAssertion(selection: Pick<WorkloadSelection, "name" | "source">, signal: AbortSignal, key?: string, source?: WorkloadSource): Promise<string | undefined>;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { privateTnlError, TnlError } from "../../../errors.js";
|
|
3
|
+
import { parseValue } from "../../validation.js";
|
|
4
|
+
import { pathTextSchema, serviceNameSchema } from "../../values.js";
|
|
5
|
+
import { cancellable } from "../cancellation.js";
|
|
6
|
+
import { context, contextKey } from "../context.js";
|
|
7
|
+
import { workloadSelectionSchema, } from "./contract.js";
|
|
8
|
+
const workloadOptionsSchema = z
|
|
9
|
+
.union([
|
|
10
|
+
z.strictObject({
|
|
11
|
+
name: serviceNameSchema,
|
|
12
|
+
token: z.custom((value) => typeof value === "function"),
|
|
13
|
+
tokenFile: z.undefined().optional(),
|
|
14
|
+
}),
|
|
15
|
+
z.strictObject({
|
|
16
|
+
name: serviceNameSchema,
|
|
17
|
+
tokenFile: pathTextSchema,
|
|
18
|
+
token: z.undefined().optional(),
|
|
19
|
+
}),
|
|
20
|
+
])
|
|
21
|
+
.readonly();
|
|
22
|
+
export function captureWorkload(key = contextKey()) {
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
configuration: context.workloads.get(key),
|
|
25
|
+
environment: Object.freeze({ ...process.env }),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** register a fixed request-scoped source, never a mutable current token. */
|
|
29
|
+
export function configureWorkload(options) {
|
|
30
|
+
const selected = parseValue(workloadOptionsSchema, options, "sdk.workload_required");
|
|
31
|
+
const key = contextKey();
|
|
32
|
+
const configuration = context.workloads.get(key);
|
|
33
|
+
if (configuration !== undefined &&
|
|
34
|
+
(configuration.name !== selected.name ||
|
|
35
|
+
configuration.token !== selected.token ||
|
|
36
|
+
configuration.tokenFile !== selected.tokenFile))
|
|
37
|
+
throw new TnlError("sdk.workload_required");
|
|
38
|
+
if (configuration === undefined && context.workloads.size >= 32)
|
|
39
|
+
throw new TnlError("sdk.workload_required");
|
|
40
|
+
context.workloads.set(key, selected);
|
|
41
|
+
}
|
|
42
|
+
export function usesWorkload(key = contextKey(), source = captureWorkload(key)) {
|
|
43
|
+
const environment = source.environment;
|
|
44
|
+
return (environment.TNL_CONNECTION_HANDLE === undefined &&
|
|
45
|
+
(environment.TNL_WORKLOAD !== undefined ||
|
|
46
|
+
source.configuration !== undefined ||
|
|
47
|
+
environment.VERCEL === "1" ||
|
|
48
|
+
Boolean(environment.FLY_APP_NAME) ||
|
|
49
|
+
Boolean(environment.K_SERVICE) ||
|
|
50
|
+
Boolean(environment.KUBERNETES_SERVICE_HOST) ||
|
|
51
|
+
environment.GITLAB_CI === "true" ||
|
|
52
|
+
environment.GITHUB_ACTIONS === "true"));
|
|
53
|
+
}
|
|
54
|
+
// only native reads the manifest and chooses acquisition. callbacks and Vercel's
|
|
55
|
+
// request context remain in this process; provider environment travels privately.
|
|
56
|
+
export async function selectWorkload(select, key = contextKey(), source = captureWorkload(key), expected) {
|
|
57
|
+
const { configuration, environment } = source;
|
|
58
|
+
const request = {
|
|
59
|
+
...(expected === undefined ? {} : { selection: expected }),
|
|
60
|
+
...(configuration === undefined
|
|
61
|
+
? {}
|
|
62
|
+
: {
|
|
63
|
+
name: configuration.name,
|
|
64
|
+
callback: configuration.token !== undefined,
|
|
65
|
+
...(configuration.tokenFile === undefined ? {} : { token_file: configuration.tokenFile }),
|
|
66
|
+
}),
|
|
67
|
+
runtime: {
|
|
68
|
+
...(environment.TNL_WORKLOAD === undefined ? {} : { workload: environment.TNL_WORKLOAD }),
|
|
69
|
+
vercel: environment.VERCEL ?? "",
|
|
70
|
+
fly_app_name: environment.FLY_APP_NAME ?? "",
|
|
71
|
+
github_actions: environment.GITHUB_ACTIONS ?? "",
|
|
72
|
+
cloud_run_service: environment.K_SERVICE ?? "",
|
|
73
|
+
kubernetes_service_host: environment.KUBERNETES_SERVICE_HOST ?? "",
|
|
74
|
+
gitlab_ci: environment.GITLAB_CI ?? "",
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
const selection = parseValue(workloadSelectionSchema, await select(request), "sdk.response_invalid");
|
|
78
|
+
const assertion = await acquireAssertion(selection, AbortSignal.timeout(10_000), key, source);
|
|
79
|
+
return {
|
|
80
|
+
sdk_workload: request,
|
|
81
|
+
sdk_workload_policy: selection.policy,
|
|
82
|
+
sdk_environment: {
|
|
83
|
+
github_actions: environment.GITHUB_ACTIONS ?? "",
|
|
84
|
+
github_request_url: environment.ACTIONS_ID_TOKEN_REQUEST_URL ?? "",
|
|
85
|
+
github_request_token: process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN ?? "",
|
|
86
|
+
vercel_token_file: environment.VERCEL_OIDC_TOKEN_FILE ?? "",
|
|
87
|
+
vercel_token: process.env.VERCEL_OIDC_TOKEN ?? "",
|
|
88
|
+
...(selection.token_env === undefined
|
|
89
|
+
? {}
|
|
90
|
+
: { token_env: selection.token_env, token_value: process.env[selection.token_env] ?? "" }),
|
|
91
|
+
},
|
|
92
|
+
...(assertion === undefined ? {} : { assertion }),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function boundedAssertion(raw) {
|
|
96
|
+
if (typeof raw !== "string")
|
|
97
|
+
throw new TnlError("sdk.workload_assertion_unavailable");
|
|
98
|
+
const token = raw.trim();
|
|
99
|
+
if (!token || Buffer.byteLength(token) > 32768 || token.includes("\0") || /\s/.test(token))
|
|
100
|
+
throw new TnlError("sdk.workload_assertion_unavailable");
|
|
101
|
+
return token;
|
|
102
|
+
}
|
|
103
|
+
export async function acquireAssertion(selection, signal, key = contextKey(), source) {
|
|
104
|
+
const configuration = source === undefined ? context.workloads.get(key) : source.configuration;
|
|
105
|
+
const lifetime = AbortSignal.any([signal, AbortSignal.timeout(10_000)]);
|
|
106
|
+
try {
|
|
107
|
+
return await cancellable(lifetime, async () => {
|
|
108
|
+
if (selection.source === "native")
|
|
109
|
+
return undefined;
|
|
110
|
+
if (selection.source === "callback") {
|
|
111
|
+
if (configuration?.name !== selection.name || configuration.token === undefined)
|
|
112
|
+
throw new TnlError("sdk.workload_assertion_unavailable");
|
|
113
|
+
return boundedAssertion(await configuration.token());
|
|
114
|
+
}
|
|
115
|
+
const { getContext, getVercelOidcToken } = await import("@vercel/oidc");
|
|
116
|
+
const current = getContext().headers?.["x-vercel-oidc-token"];
|
|
117
|
+
if (current === undefined)
|
|
118
|
+
return undefined;
|
|
119
|
+
boundedAssertion(current);
|
|
120
|
+
// disable development refresh: control decides validity, and missing tokens
|
|
121
|
+
// must never cause CLI login, project discovery, or global token writes.
|
|
122
|
+
return boundedAssertion(await getVercelOidcToken({ expirationBufferMs: Number.NEGATIVE_INFINITY }));
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (cause) {
|
|
126
|
+
if (lifetime.aborted)
|
|
127
|
+
throw lifetime.reason;
|
|
128
|
+
throw privateTnlError("sdk.workload_assertion_unavailable", cause);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
export type PublisherEnrollment = string | ((signal: AbortSignal) => string | Promise<string>);
|
|
3
|
+
export declare const enrollmentSourceSchema: z.ZodUnion<readonly [z.ZodString, z.ZodCustom<(signal: AbortSignal) => string | Promise<string>, (signal: AbortSignal) => string | Promise<string>>]>;
|
|
4
|
+
export declare function bindEnrollment(source: PublisherEnrollment, lifetime: AbortSignal): () => Promise<string>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { TnlError } from "../errors.js";
|
|
3
|
+
export const enrollmentSourceSchema = z.union([
|
|
4
|
+
z.string().min(1).max(256),
|
|
5
|
+
z.custom((value) => typeof value === "function"),
|
|
6
|
+
]);
|
|
7
|
+
export function bindEnrollment(source, lifetime) {
|
|
8
|
+
return async () => {
|
|
9
|
+
const signal = AbortSignal.any([lifetime, AbortSignal.timeout(15_000)]);
|
|
10
|
+
signal.throwIfAborted();
|
|
11
|
+
if (typeof source === "string")
|
|
12
|
+
return source;
|
|
13
|
+
let listener;
|
|
14
|
+
try {
|
|
15
|
+
const aborted = new Promise((_, reject) => {
|
|
16
|
+
listener = () => reject(signal.reason);
|
|
17
|
+
signal.addEventListener("abort", listener, { once: true });
|
|
18
|
+
});
|
|
19
|
+
const value = await Promise.race([
|
|
20
|
+
aborted,
|
|
21
|
+
Promise.resolve().then(() => {
|
|
22
|
+
signal.throwIfAborted();
|
|
23
|
+
return source(signal);
|
|
24
|
+
}),
|
|
25
|
+
]);
|
|
26
|
+
if (typeof value !== "string" || value.length === 0 || value.length > 256)
|
|
27
|
+
throw new TnlError("sdk.configuration_invalid");
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
signal.removeEventListener("abort", listener);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// generated from contracts/control/public/v1 with @hey-api/openapi-ts; do not edit.
|
|
2
|
+
import { createSseClient } from "../core/serverSentEvents.gen.js";
|
|
3
|
+
import { getValidRequestBody } from "../core/utils.gen.js";
|
|
4
|
+
import { buildUrl, createConfig, createInterceptors, getParseAs, mergeConfigs, mergeHeaders, setAuthParams, } from "./utils.gen.js";
|
|
5
|
+
export const createClient = (config = {}) => {
|
|
6
|
+
let _config = mergeConfigs(createConfig(), config);
|
|
7
|
+
const getConfig = () => ({ ..._config });
|
|
8
|
+
const setConfig = (config) => {
|
|
9
|
+
_config = mergeConfigs(_config, config);
|
|
10
|
+
return getConfig();
|
|
11
|
+
};
|
|
12
|
+
const interceptors = createInterceptors();
|
|
13
|
+
const beforeRequest = async (options) => {
|
|
14
|
+
const opts = {
|
|
15
|
+
..._config,
|
|
16
|
+
...options,
|
|
17
|
+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
18
|
+
headers: mergeHeaders(_config.headers, options.headers),
|
|
19
|
+
serializedBody: undefined,
|
|
20
|
+
};
|
|
21
|
+
if (opts.security) {
|
|
22
|
+
await setAuthParams(opts);
|
|
23
|
+
}
|
|
24
|
+
if (opts.requestValidator) {
|
|
25
|
+
await opts.requestValidator(opts);
|
|
26
|
+
}
|
|
27
|
+
if (opts.body !== undefined && opts.bodySerializer) {
|
|
28
|
+
opts.serializedBody = opts.bodySerializer(opts.body);
|
|
29
|
+
}
|
|
30
|
+
// remove Content-Type header if body is empty to avoid sending invalid requests
|
|
31
|
+
if (opts.body === undefined || opts.serializedBody === "") {
|
|
32
|
+
opts.headers.delete("Content-Type");
|
|
33
|
+
}
|
|
34
|
+
const resolvedOpts = opts;
|
|
35
|
+
const url = buildUrl(resolvedOpts);
|
|
36
|
+
return { opts: resolvedOpts, url };
|
|
37
|
+
};
|
|
38
|
+
const request = async (options) => {
|
|
39
|
+
const throwOnError = options.throwOnError ?? _config.throwOnError;
|
|
40
|
+
const responseStyle = options.responseStyle ?? _config.responseStyle;
|
|
41
|
+
let request;
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
const { opts, url } = await beforeRequest(options);
|
|
45
|
+
const requestInit = {
|
|
46
|
+
redirect: "follow",
|
|
47
|
+
...opts,
|
|
48
|
+
body: getValidRequestBody(opts),
|
|
49
|
+
};
|
|
50
|
+
request = new Request(url, requestInit);
|
|
51
|
+
for (const fn of interceptors.request.fns) {
|
|
52
|
+
if (fn) {
|
|
53
|
+
request = await fn(request, opts);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
57
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
58
|
+
const _fetch = opts.fetch;
|
|
59
|
+
response = await _fetch(request);
|
|
60
|
+
for (const fn of interceptors.response.fns) {
|
|
61
|
+
if (fn) {
|
|
62
|
+
response = await fn(response, request, opts);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const result = {
|
|
66
|
+
request,
|
|
67
|
+
response,
|
|
68
|
+
};
|
|
69
|
+
if (response.ok) {
|
|
70
|
+
const parseAs = (opts.parseAs === "auto"
|
|
71
|
+
? getParseAs(response.headers.get("Content-Type"))
|
|
72
|
+
: opts.parseAs) ?? "json";
|
|
73
|
+
if (response.status === 204 || response.headers.get("Content-Length") === "0") {
|
|
74
|
+
let emptyData;
|
|
75
|
+
switch (parseAs) {
|
|
76
|
+
case "arrayBuffer":
|
|
77
|
+
case "blob":
|
|
78
|
+
case "text":
|
|
79
|
+
emptyData = await response[parseAs]();
|
|
80
|
+
break;
|
|
81
|
+
case "formData":
|
|
82
|
+
emptyData = new FormData();
|
|
83
|
+
break;
|
|
84
|
+
case "stream":
|
|
85
|
+
emptyData = response.body;
|
|
86
|
+
break;
|
|
87
|
+
case "json":
|
|
88
|
+
default:
|
|
89
|
+
emptyData = {};
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
return opts.responseStyle === "data"
|
|
93
|
+
? emptyData
|
|
94
|
+
: {
|
|
95
|
+
data: emptyData,
|
|
96
|
+
...result,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
let data;
|
|
100
|
+
switch (parseAs) {
|
|
101
|
+
case "arrayBuffer":
|
|
102
|
+
case "blob":
|
|
103
|
+
case "formData":
|
|
104
|
+
case "text":
|
|
105
|
+
data = await response[parseAs]();
|
|
106
|
+
break;
|
|
107
|
+
case "json": {
|
|
108
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
109
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
110
|
+
const text = await response.text();
|
|
111
|
+
data = text ? JSON.parse(text) : {};
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "stream":
|
|
115
|
+
return opts.responseStyle === "data"
|
|
116
|
+
? response.body
|
|
117
|
+
: {
|
|
118
|
+
data: response.body,
|
|
119
|
+
...result,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (parseAs === "json") {
|
|
123
|
+
if (opts.responseValidator) {
|
|
124
|
+
await opts.responseValidator(data);
|
|
125
|
+
}
|
|
126
|
+
if (opts.responseTransformer) {
|
|
127
|
+
data = await opts.responseTransformer(data);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return opts.responseStyle === "data"
|
|
131
|
+
? data
|
|
132
|
+
: {
|
|
133
|
+
data,
|
|
134
|
+
...result,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
const textError = await response.text();
|
|
138
|
+
let jsonError;
|
|
139
|
+
try {
|
|
140
|
+
jsonError = JSON.parse(textError);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// noop
|
|
144
|
+
}
|
|
145
|
+
throw jsonError ?? textError;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
let finalError = error;
|
|
149
|
+
for (const fn of interceptors.error.fns) {
|
|
150
|
+
if (fn) {
|
|
151
|
+
finalError = await fn(finalError, response, request, options);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
finalError = finalError || {};
|
|
155
|
+
if (throwOnError) {
|
|
156
|
+
throw finalError;
|
|
157
|
+
}
|
|
158
|
+
// TODO: we probably want to return error and improve types
|
|
159
|
+
return responseStyle === "data"
|
|
160
|
+
? undefined
|
|
161
|
+
: {
|
|
162
|
+
error: finalError,
|
|
163
|
+
request,
|
|
164
|
+
response,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const makeMethodFn = (method) => (options) => request({ ...options, method });
|
|
169
|
+
const makeSseFn = (method) => async (options) => {
|
|
170
|
+
const { opts, url } = await beforeRequest(options);
|
|
171
|
+
return createSseClient({
|
|
172
|
+
...opts,
|
|
173
|
+
body: opts.body,
|
|
174
|
+
method,
|
|
175
|
+
onRequest: async (url, init) => {
|
|
176
|
+
let request = new Request(url, init);
|
|
177
|
+
for (const fn of interceptors.request.fns) {
|
|
178
|
+
if (fn) {
|
|
179
|
+
request = await fn(request, opts);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return request;
|
|
183
|
+
},
|
|
184
|
+
serializedBody: getValidRequestBody(opts),
|
|
185
|
+
url,
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
const _buildUrl = (options) => buildUrl({ ..._config, ...options });
|
|
189
|
+
return {
|
|
190
|
+
buildUrl: _buildUrl,
|
|
191
|
+
connect: makeMethodFn("CONNECT"),
|
|
192
|
+
delete: makeMethodFn("DELETE"),
|
|
193
|
+
get: makeMethodFn("GET"),
|
|
194
|
+
getConfig,
|
|
195
|
+
head: makeMethodFn("HEAD"),
|
|
196
|
+
interceptors,
|
|
197
|
+
options: makeMethodFn("OPTIONS"),
|
|
198
|
+
patch: makeMethodFn("PATCH"),
|
|
199
|
+
post: makeMethodFn("POST"),
|
|
200
|
+
put: makeMethodFn("PUT"),
|
|
201
|
+
request,
|
|
202
|
+
setConfig,
|
|
203
|
+
sse: {
|
|
204
|
+
connect: makeSseFn("CONNECT"),
|
|
205
|
+
delete: makeSseFn("DELETE"),
|
|
206
|
+
get: makeSseFn("GET"),
|
|
207
|
+
head: makeSseFn("HEAD"),
|
|
208
|
+
options: makeSseFn("OPTIONS"),
|
|
209
|
+
patch: makeSseFn("PATCH"),
|
|
210
|
+
post: makeSseFn("POST"),
|
|
211
|
+
put: makeSseFn("PUT"),
|
|
212
|
+
trace: makeSseFn("TRACE"),
|
|
213
|
+
},
|
|
214
|
+
trace: makeMethodFn("TRACE"),
|
|
215
|
+
};
|
|
216
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type { Auth } from "../core/auth.gen.js";
|
|
2
|
+
export type { QuerySerializerOptions } from "../core/bodySerializer.gen.js";
|
|
3
|
+
export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer, } from "../core/bodySerializer.gen.js";
|
|
4
|
+
export { buildClientParams } from "../core/params.gen.js";
|
|
5
|
+
export { serializeQueryKeyValue } from "../core/queryKeySerializer.gen.js";
|
|
6
|
+
export type { ServerSentEventsResult } from "../core/serverSentEvents.gen.js";
|
|
7
|
+
export type { ClientMeta } from "../core/types.gen.js";
|
|
8
|
+
export { createClient } from "./client.gen.js";
|
|
9
|
+
export type { Client, ClientOptions, Config, CreateClientConfig, Options, RequestOptions, RequestResult, ResolvedRequestOptions, ResponseStyle, TDataShape, } from "./types.gen.js";
|
|
10
|
+
export { createConfig, mergeHeaders } from "./utils.gen.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// generated from contracts/control/public/v1 with @hey-api/openapi-ts; do not edit.
|
|
2
|
+
export { formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer, } from "../core/bodySerializer.gen.js";
|
|
3
|
+
export { buildClientParams } from "../core/params.gen.js";
|
|
4
|
+
export { serializeQueryKeyValue } from "../core/queryKeySerializer.gen.js";
|
|
5
|
+
export { createClient } from "./client.gen.js";
|
|
6
|
+
export { createConfig, mergeHeaders } from "./utils.gen.js";
|