@moinulmoin/eden-runtime-cloudflare 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 +201 -0
- package/NOTICE +42 -0
- package/dist/artifact-runtime.d.ts +27 -0
- package/dist/artifact-runtime.js +14 -0
- package/dist/eden-eve-host-worker.mjs +1960 -0
- package/dist/eve-host-runtime.d.ts +46 -0
- package/dist/eve-host-runtime.js +127 -0
- package/dist/eve-host.d.ts +166 -0
- package/dist/eve-host.js +495 -0
- package/dist/http-host.d.ts +6 -0
- package/dist/http-host.js +419 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +7 -0
- package/dist/model-adapter-internal.d.ts +12 -0
- package/dist/model-adapter-internal.js +172 -0
- package/dist/model-adapter.d.ts +9 -0
- package/dist/model-adapter.js +56 -0
- package/dist/model-contracts.d.ts +92 -0
- package/dist/model-contracts.js +2 -0
- package/dist/model-normalizers.d.ts +7 -0
- package/dist/model-normalizers.js +453 -0
- package/dist/session-checkpoint.d.ts +48 -0
- package/dist/session-checkpoint.js +300 -0
- package/dist/session-identity.d.ts +9 -0
- package/dist/session-identity.js +45 -0
- package/dist/session-jobs.d.ts +69 -0
- package/dist/session-jobs.js +427 -0
- package/dist/session-journal.d.ts +154 -0
- package/dist/session-journal.js +391 -0
- package/dist/session-schema.d.ts +21 -0
- package/dist/session-schema.js +247 -0
- package/dist/session-state.d.ts +165 -0
- package/dist/session-state.js +168 -0
- package/dist/session.d.ts +40 -0
- package/dist/session.js +792 -0
- package/dist/test-execution.d.ts +8 -0
- package/dist/test-execution.js +12 -0
- package/dist/test-worker.d.ts +12 -0
- package/dist/test-worker.js +214 -0
- package/dist/tool-harness.d.ts +31 -0
- package/dist/tool-harness.js +226 -0
- package/dist/turn-runner-storage.d.ts +28 -0
- package/dist/turn-runner-storage.js +583 -0
- package/dist/turn-runner-types.d.ts +112 -0
- package/dist/turn-runner-types.js +21 -0
- package/dist/turn-runner.d.ts +6 -0
- package/dist/turn-runner.js +370 -0
- package/package.json +51 -0
package/dist/eve-host.js
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
export const EVE_HOST_DEFAULTS = {
|
|
2
|
+
compatibilityDate: "2026-04-01",
|
|
3
|
+
healthPath: "/eve/v1/health",
|
|
4
|
+
internalPort: 8080,
|
|
5
|
+
maxInstances: 1,
|
|
6
|
+
startCommand: [
|
|
7
|
+
"./node_modules/.bin/eve",
|
|
8
|
+
"start",
|
|
9
|
+
"--host",
|
|
10
|
+
"0.0.0.0",
|
|
11
|
+
"--port",
|
|
12
|
+
"8080",
|
|
13
|
+
],
|
|
14
|
+
sleepAfter: "24h",
|
|
15
|
+
};
|
|
16
|
+
export const EVE_HOST_OWNED_HEADERS = [
|
|
17
|
+
"forwarded",
|
|
18
|
+
"x-forwarded-for",
|
|
19
|
+
"x-forwarded-host",
|
|
20
|
+
"x-forwarded-port",
|
|
21
|
+
"x-forwarded-proto",
|
|
22
|
+
"x-forwarded-server",
|
|
23
|
+
"x-forwarded-scheme",
|
|
24
|
+
"x-real-ip",
|
|
25
|
+
"cf-connecting-ip",
|
|
26
|
+
"cf-visitor",
|
|
27
|
+
"true-client-ip",
|
|
28
|
+
"x-original-host",
|
|
29
|
+
"x-original-proto",
|
|
30
|
+
"x-envoy-original-host",
|
|
31
|
+
"x-envoy-original-proto",
|
|
32
|
+
"x-proxy-host",
|
|
33
|
+
"x-host",
|
|
34
|
+
"x-eve-public-origin",
|
|
35
|
+
"x-workflow-local-base-url",
|
|
36
|
+
"x-eden-eve-callback-base",
|
|
37
|
+
"x-eden-eve-deployment-id",
|
|
38
|
+
"x-eden-eve-generation-id",
|
|
39
|
+
"x-eden-eve-public-origin",
|
|
40
|
+
"x-eden-eve-correlation-id",
|
|
41
|
+
"x-eden-eve-container-name",
|
|
42
|
+
"x-eden-eve-container-id",
|
|
43
|
+
"x-eden-eve-runtime-revision",
|
|
44
|
+
"x-eden-deployment-id",
|
|
45
|
+
"x-eden-generation-id",
|
|
46
|
+
"x-eden-container-name",
|
|
47
|
+
"x-eden-container-id",
|
|
48
|
+
"cf-container-target-port",
|
|
49
|
+
];
|
|
50
|
+
const EVE_HOST_OWNED_HEADER_SET = new Set(EVE_HOST_OWNED_HEADERS);
|
|
51
|
+
export const WORKER_NAME_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/u;
|
|
52
|
+
const SUBDOMAIN_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/u;
|
|
53
|
+
export const IDENTIFIER_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
|
54
|
+
const INSTANCE_NAME_PATTERN = /^[a-z0-9](?:[a-z0-9._-]{0,125}[a-z0-9])?$/u;
|
|
55
|
+
const SHA256_IMAGE_PATTERN = /^(?:[a-z0-9.-]+\/)?[a-z0-9._/-]+@sha256:[0-9a-f]{64}$/u;
|
|
56
|
+
export class EveHostError extends Error {
|
|
57
|
+
code;
|
|
58
|
+
constructor(code, message) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = "EveHostError";
|
|
61
|
+
this.code = code;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function resolveStableWorkersDevOrigin(request) {
|
|
65
|
+
if (!WORKER_NAME_PATTERN.test(request.workerName)) {
|
|
66
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The exact Worker name is not valid for a stable workers.dev origin.");
|
|
67
|
+
}
|
|
68
|
+
if (!SUBDOMAIN_PATTERN.test(request.workersDevSubdomain) ||
|
|
69
|
+
request.workersDevSubdomain.includes(".")) {
|
|
70
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The authenticated workers.dev account subdomain is unavailable or invalid.");
|
|
71
|
+
}
|
|
72
|
+
return `https://${request.workerName}.${request.workersDevSubdomain}.workers.dev`;
|
|
73
|
+
}
|
|
74
|
+
export function assertNonEmpty(value, subject) {
|
|
75
|
+
if (value.length === 0) {
|
|
76
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", `The ${subject} must be non-empty.`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export function assertStableOrigin(value, workerName) {
|
|
80
|
+
let origin;
|
|
81
|
+
try {
|
|
82
|
+
origin = new URL(value);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The stable workers.dev origin is not a valid absolute URL.");
|
|
86
|
+
}
|
|
87
|
+
if (origin.protocol !== "https:" ||
|
|
88
|
+
origin.port.length !== 0 ||
|
|
89
|
+
origin.pathname !== "/" ||
|
|
90
|
+
origin.search.length !== 0 ||
|
|
91
|
+
origin.hash.length !== 0 ||
|
|
92
|
+
!origin.hostname.endsWith(".workers.dev")) {
|
|
93
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The stable origin must be an HTTPS provider-assigned workers.dev origin.");
|
|
94
|
+
}
|
|
95
|
+
if (workerName !== undefined &&
|
|
96
|
+
!origin.hostname.startsWith(`${workerName}.`)) {
|
|
97
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The stable workers.dev origin does not belong to the exact Worker target.");
|
|
98
|
+
}
|
|
99
|
+
const labels = origin.hostname.split(".");
|
|
100
|
+
if (labels.length !== 4 ||
|
|
101
|
+
labels[labels.length - 2] !== "workers" ||
|
|
102
|
+
labels[labels.length - 1] !== "dev" ||
|
|
103
|
+
!SUBDOMAIN_PATTERN.test(labels[1] ?? "") ||
|
|
104
|
+
(workerName !== undefined && labels[0] !== workerName)) {
|
|
105
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The stable origin must be the exact provider-assigned Worker workers.dev hostname.");
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function assertContainerImage(value) {
|
|
109
|
+
if (!SHA256_IMAGE_PATTERN.test(value)) {
|
|
110
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container image must be an immutable sha256 digest reference.");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export function createEveHostConfig(request) {
|
|
114
|
+
if (!WORKER_NAME_PATTERN.test(request.workerName)) {
|
|
115
|
+
throw new EveHostError("HOST_ORIGIN_UNAVAILABLE", "The exact Worker name is not valid.");
|
|
116
|
+
}
|
|
117
|
+
if (!WORKER_NAME_PATTERN.test(request.containerApplicationName) ||
|
|
118
|
+
!IDENTIFIER_PATTERN.test(request.containerClassName)) {
|
|
119
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container application and class names must be stable safe identifiers.");
|
|
120
|
+
}
|
|
121
|
+
if (!IDENTIFIER_PATTERN.test(request.containerBindingName)) {
|
|
122
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container binding name must be a valid Worker environment identifier.");
|
|
123
|
+
}
|
|
124
|
+
if (!INSTANCE_NAME_PATTERN.test(request.stableContainerInstanceName)) {
|
|
125
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The stable Container instance name must be a bounded logical identifier.");
|
|
126
|
+
}
|
|
127
|
+
assertNonEmpty(request.deploymentId, "deployment identity");
|
|
128
|
+
assertNonEmpty(request.generationId, "generation identity");
|
|
129
|
+
assertStableOrigin(request.stableWorkersDevOrigin, request.workerName);
|
|
130
|
+
assertContainerImage(request.containerImage);
|
|
131
|
+
for (const name of request.runtimeVariableNames ?? []) {
|
|
132
|
+
if (!IDENTIFIER_PATTERN.test(name) || RESERVED_EVE_HOST_VARIABLES.has(name)) {
|
|
133
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", `The protected runtime variable name ${name} is invalid or reserved.`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const container = {
|
|
137
|
+
name: request.containerApplicationName,
|
|
138
|
+
class_name: request.containerClassName,
|
|
139
|
+
image: request.containerImage,
|
|
140
|
+
...(request.containerImageBuildContext === undefined
|
|
141
|
+
? {}
|
|
142
|
+
: { image_build_context: request.containerImageBuildContext }),
|
|
143
|
+
max_instances: EVE_HOST_DEFAULTS.maxInstances,
|
|
144
|
+
};
|
|
145
|
+
return {
|
|
146
|
+
worker: {
|
|
147
|
+
...(request.accountId === undefined
|
|
148
|
+
? {}
|
|
149
|
+
: { account_id: request.accountId }),
|
|
150
|
+
name: request.workerName,
|
|
151
|
+
main: "worker.ts",
|
|
152
|
+
compatibility_date: EVE_HOST_DEFAULTS.compatibilityDate,
|
|
153
|
+
workers_dev: true,
|
|
154
|
+
vars: {
|
|
155
|
+
EVE_PUBLIC_ORIGIN: request.stableWorkersDevOrigin,
|
|
156
|
+
EVE_CONTAINER_INSTANCE_NAME: request.stableContainerInstanceName,
|
|
157
|
+
EDEN_EVE_DEPLOYMENT_ID: request.deploymentId,
|
|
158
|
+
EDEN_EVE_GENERATION_ID: request.generationId,
|
|
159
|
+
EVE_RUNTIME_VARIABLE_NAMES: request.runtimeVariableNames ?? [],
|
|
160
|
+
...(request.runtimeRevisionHandle === undefined
|
|
161
|
+
? {}
|
|
162
|
+
: { EDEN_EVE_RUNTIME_REVISION: request.runtimeRevisionHandle }),
|
|
163
|
+
},
|
|
164
|
+
containers: [container],
|
|
165
|
+
durable_objects: {
|
|
166
|
+
bindings: [
|
|
167
|
+
{
|
|
168
|
+
name: request.containerBindingName,
|
|
169
|
+
class_name: request.containerClassName,
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
},
|
|
173
|
+
migrations: [
|
|
174
|
+
{
|
|
175
|
+
tag: "v1",
|
|
176
|
+
new_sqlite_classes: [request.containerClassName],
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
},
|
|
180
|
+
container: {
|
|
181
|
+
applicationName: request.containerApplicationName,
|
|
182
|
+
className: request.containerClassName,
|
|
183
|
+
instanceName: request.stableContainerInstanceName,
|
|
184
|
+
bindingName: request.containerBindingName,
|
|
185
|
+
port: EVE_HOST_DEFAULTS.internalPort,
|
|
186
|
+
publicOrigin: request.stableWorkersDevOrigin,
|
|
187
|
+
deploymentId: request.deploymentId,
|
|
188
|
+
generationId: request.generationId,
|
|
189
|
+
...(request.runtimeRevisionHandle === undefined
|
|
190
|
+
? {}
|
|
191
|
+
: { runtimeRevisionHandle: request.runtimeRevisionHandle }),
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
export function generateEveHostWorkerSource(request) {
|
|
196
|
+
const moduleSpecifier = "./eden-eve-host-worker.mjs";
|
|
197
|
+
const workerOptions = {
|
|
198
|
+
publicOrigin: request.config.container.publicOrigin,
|
|
199
|
+
workerName: request.config.worker.name,
|
|
200
|
+
containerBindingName: request.config.container.bindingName,
|
|
201
|
+
deploymentId: request.config.container.deploymentId,
|
|
202
|
+
generationId: request.config.container.generationId,
|
|
203
|
+
stableContainerInstanceName: request.config.container.instanceName,
|
|
204
|
+
...(request.config.container.runtimeRevisionHandle === undefined
|
|
205
|
+
? {}
|
|
206
|
+
: {
|
|
207
|
+
runtimeRevisionHandle: request.config.container.runtimeRevisionHandle,
|
|
208
|
+
}),
|
|
209
|
+
};
|
|
210
|
+
return [
|
|
211
|
+
`import { EveHostContainer, createEveHostWorker } from ${JSON.stringify(moduleSpecifier)};`,
|
|
212
|
+
request.config.container.className === "EveHostContainer"
|
|
213
|
+
? "export { EveHostContainer };"
|
|
214
|
+
: `export { EveHostContainer as ${request.config.container.className} };`,
|
|
215
|
+
"",
|
|
216
|
+
`export default createEveHostWorker(${JSON.stringify(workerOptions)});`,
|
|
217
|
+
"",
|
|
218
|
+
].join("\n");
|
|
219
|
+
}
|
|
220
|
+
function originHost(origin) {
|
|
221
|
+
const parsed = new URL(origin);
|
|
222
|
+
return parsed.host;
|
|
223
|
+
}
|
|
224
|
+
function isBodylessMethod(method) {
|
|
225
|
+
return method === "GET" || method === "HEAD";
|
|
226
|
+
}
|
|
227
|
+
function isHostOwnedHeader(name) {
|
|
228
|
+
const lowerName = name.toLowerCase();
|
|
229
|
+
return (EVE_HOST_OWNED_HEADER_SET.has(lowerName) ||
|
|
230
|
+
lowerName.startsWith("x-forwarded-") ||
|
|
231
|
+
lowerName.startsWith("x-eden-") ||
|
|
232
|
+
lowerName.startsWith("cf-"));
|
|
233
|
+
}
|
|
234
|
+
export function createTrustedEveRequest(request, metadata) {
|
|
235
|
+
assertStableOrigin(metadata.publicOrigin);
|
|
236
|
+
assertNonEmpty(metadata.deploymentId, "deployment identity");
|
|
237
|
+
assertNonEmpty(metadata.generationId, "generation identity");
|
|
238
|
+
assertNonEmpty(metadata.correlationId, "correlation identity");
|
|
239
|
+
const headers = new Headers(request.headers);
|
|
240
|
+
const headersToStrip = [];
|
|
241
|
+
headers.forEach((_value, header) => {
|
|
242
|
+
if (isHostOwnedHeader(header)) {
|
|
243
|
+
headersToStrip.push(header);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
for (const header of headersToStrip)
|
|
247
|
+
headers.delete(header);
|
|
248
|
+
const host = originHost(metadata.publicOrigin);
|
|
249
|
+
const targetOrigin = new URL(metadata.publicOrigin);
|
|
250
|
+
const targetUrl = new URL(request.url);
|
|
251
|
+
targetUrl.protocol = targetOrigin.protocol;
|
|
252
|
+
targetUrl.hostname = targetOrigin.hostname;
|
|
253
|
+
targetUrl.port = targetOrigin.port;
|
|
254
|
+
const edgeUrl = new URL(request.url);
|
|
255
|
+
const edgeProtocol = edgeUrl.protocol === "http:" ? "http" : "https";
|
|
256
|
+
const publicPort = targetOrigin.port.length > 0
|
|
257
|
+
? targetOrigin.port
|
|
258
|
+
: targetOrigin.protocol === "https:"
|
|
259
|
+
? "443"
|
|
260
|
+
: "80";
|
|
261
|
+
headers.set("forwarded", `proto=${edgeProtocol};host=${host}`);
|
|
262
|
+
headers.set("host", host);
|
|
263
|
+
headers.set("x-forwarded-host", host);
|
|
264
|
+
headers.set("x-forwarded-port", publicPort);
|
|
265
|
+
headers.set("x-forwarded-proto", edgeProtocol);
|
|
266
|
+
headers.set("x-eden-eve-public-origin", metadata.publicOrigin);
|
|
267
|
+
headers.set("x-eden-eve-deployment-id", metadata.deploymentId);
|
|
268
|
+
headers.set("x-eden-eve-generation-id", metadata.generationId);
|
|
269
|
+
headers.set("x-eden-eve-correlation-id", metadata.correlationId);
|
|
270
|
+
if (metadata.runtimeRevisionHandle !== undefined) {
|
|
271
|
+
headers.set("x-eden-eve-runtime-revision", metadata.runtimeRevisionHandle);
|
|
272
|
+
}
|
|
273
|
+
const init = {
|
|
274
|
+
method: request.method,
|
|
275
|
+
headers,
|
|
276
|
+
redirect: request.redirect,
|
|
277
|
+
signal: request.signal,
|
|
278
|
+
...(isBodylessMethod(request.method) || request.body === null
|
|
279
|
+
? {}
|
|
280
|
+
: { body: request.body, duplex: "half" }),
|
|
281
|
+
};
|
|
282
|
+
return new Request(targetUrl, init);
|
|
283
|
+
}
|
|
284
|
+
function isWebSocketUpgrade(request) {
|
|
285
|
+
return request.headers.get("upgrade")?.toLowerCase() === "websocket";
|
|
286
|
+
}
|
|
287
|
+
function throwIfAborted(signal) {
|
|
288
|
+
if (signal.aborted) {
|
|
289
|
+
throw new EveHostError("HOST_REQUEST_ABORTED", "The client request was cancelled before Eve became ready.");
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
export function createEveHostProxy(options) {
|
|
293
|
+
return async (request) => {
|
|
294
|
+
throwIfAborted(request.signal);
|
|
295
|
+
await options.ensureReady(request.signal);
|
|
296
|
+
throwIfAborted(request.signal);
|
|
297
|
+
const forwarded = createTrustedEveRequest(request, options);
|
|
298
|
+
return isWebSocketUpgrade(request)
|
|
299
|
+
? options.transport.fetch(forwarded)
|
|
300
|
+
: options.transport.containerFetch(forwarded);
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
export function createEveHostLifecycleObserver(now = Date.now) {
|
|
304
|
+
const events = [];
|
|
305
|
+
return {
|
|
306
|
+
events,
|
|
307
|
+
record(type, safeStatus) {
|
|
308
|
+
const event = {
|
|
309
|
+
type,
|
|
310
|
+
at: now(),
|
|
311
|
+
...(safeStatus === undefined ? {} : { safeStatus }),
|
|
312
|
+
};
|
|
313
|
+
events.push(event);
|
|
314
|
+
return event;
|
|
315
|
+
},
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function responseBodyIsReady(value) {
|
|
319
|
+
if (typeof value !== "object" || value === null)
|
|
320
|
+
return false;
|
|
321
|
+
const record = value;
|
|
322
|
+
return (record.status === "ready" ||
|
|
323
|
+
record.state === "ready" ||
|
|
324
|
+
record.health === "ready" ||
|
|
325
|
+
record.ready === true ||
|
|
326
|
+
record.healthy === true);
|
|
327
|
+
}
|
|
328
|
+
const RESERVED_EVE_HOST_VARIABLES = new Set([
|
|
329
|
+
"HOST",
|
|
330
|
+
"NITRO_HOST",
|
|
331
|
+
"PORT",
|
|
332
|
+
"NITRO_PORT",
|
|
333
|
+
"NODE_ENV",
|
|
334
|
+
"WORKFLOW_LOCAL_BASE_URL",
|
|
335
|
+
"EDEN_EVE_DEPLOYMENT_ID",
|
|
336
|
+
"EDEN_EVE_GENERATION_ID",
|
|
337
|
+
"EDEN_EVE_RUNTIME_REVISION",
|
|
338
|
+
]);
|
|
339
|
+
export function readProtectedRuntimeVariables(env) {
|
|
340
|
+
const names = env.EVE_RUNTIME_VARIABLE_NAMES ?? [];
|
|
341
|
+
const values = {};
|
|
342
|
+
for (const name of names) {
|
|
343
|
+
if (!IDENTIFIER_PATTERN.test(name) || RESERVED_EVE_HOST_VARIABLES.has(name)) {
|
|
344
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", `The protected runtime contract contains an invalid or reserved variable name ${name}.`);
|
|
345
|
+
}
|
|
346
|
+
const value = env[name];
|
|
347
|
+
if (typeof value !== "string") {
|
|
348
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", `The protected runtime contract is missing variable ${name}.`);
|
|
349
|
+
}
|
|
350
|
+
values[name] = value;
|
|
351
|
+
}
|
|
352
|
+
return values;
|
|
353
|
+
}
|
|
354
|
+
function raceWithAbort(promise, signal) {
|
|
355
|
+
if (signal.aborted) {
|
|
356
|
+
return Promise.reject(new EveHostError("HOST_REQUEST_ABORTED", "The client request was cancelled before Eve became ready."));
|
|
357
|
+
}
|
|
358
|
+
return new Promise((resolve, reject) => {
|
|
359
|
+
const onAbort = () => {
|
|
360
|
+
reject(new EveHostError("HOST_REQUEST_ABORTED", "The client request was cancelled before Eve became ready."));
|
|
361
|
+
};
|
|
362
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
363
|
+
void promise.then((value) => {
|
|
364
|
+
signal.removeEventListener("abort", onAbort);
|
|
365
|
+
resolve(value);
|
|
366
|
+
}, (error) => {
|
|
367
|
+
signal.removeEventListener("abort", onAbort);
|
|
368
|
+
reject(error);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
export function createEveReadinessGate(options) {
|
|
373
|
+
const port = options.port ?? EVE_HOST_DEFAULTS.internalPort;
|
|
374
|
+
const healthPath = options.healthPath ?? EVE_HOST_DEFAULTS.healthPath;
|
|
375
|
+
const instanceGetTimeoutMs = options.instanceGetTimeoutMs ?? 120_000;
|
|
376
|
+
const portReadyTimeoutMs = options.portReadyTimeoutMs ?? 30_000;
|
|
377
|
+
const healthTimeoutMs = options.healthTimeoutMs ?? 10_000;
|
|
378
|
+
const waitIntervalMs = options.waitIntervalMs ?? 300;
|
|
379
|
+
let evidence;
|
|
380
|
+
let inFlight;
|
|
381
|
+
let inFlightController;
|
|
382
|
+
let generation = 0;
|
|
383
|
+
const run = async (signal) => {
|
|
384
|
+
try {
|
|
385
|
+
await options.startAndWaitForPorts({
|
|
386
|
+
ports: port,
|
|
387
|
+
cancellationOptions: {
|
|
388
|
+
abort: signal,
|
|
389
|
+
instanceGetTimeoutMS: instanceGetTimeoutMs,
|
|
390
|
+
portReadyTimeoutMS: portReadyTimeoutMs,
|
|
391
|
+
waitInterval: waitIntervalMs,
|
|
392
|
+
},
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
catch {
|
|
396
|
+
if (signal.aborted) {
|
|
397
|
+
throw new EveHostError("HOST_REQUEST_ABORTED", "The client request was cancelled before Eve became ready.");
|
|
398
|
+
}
|
|
399
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Eve Container did not reach its internal port readiness deadline.");
|
|
400
|
+
}
|
|
401
|
+
const healthController = new AbortController();
|
|
402
|
+
let rejectHealthTimeout;
|
|
403
|
+
const abortHealth = () => {
|
|
404
|
+
healthController.abort();
|
|
405
|
+
rejectHealthTimeout?.(new EveHostError("HOST_REQUEST_ABORTED", "The client request was cancelled before Eve became ready."));
|
|
406
|
+
};
|
|
407
|
+
signal.addEventListener("abort", abortHealth, { once: true });
|
|
408
|
+
const healthTimeoutResult = new Promise((_resolve, reject) => {
|
|
409
|
+
rejectHealthTimeout = reject;
|
|
410
|
+
});
|
|
411
|
+
const healthTimeout = setTimeout(() => {
|
|
412
|
+
healthController.abort();
|
|
413
|
+
rejectHealthTimeout?.(new EveHostError("HOST_READINESS_UNPROVEN", "The bounded Eve health probe timed out."));
|
|
414
|
+
}, healthTimeoutMs);
|
|
415
|
+
let response;
|
|
416
|
+
try {
|
|
417
|
+
const healthRequest = new Request(`http://localhost${healthPath}`, {
|
|
418
|
+
method: "GET",
|
|
419
|
+
signal: healthController.signal,
|
|
420
|
+
});
|
|
421
|
+
const healthResult = options.healthFetch(healthRequest);
|
|
422
|
+
response = await Promise.race([healthResult, healthTimeoutResult]);
|
|
423
|
+
}
|
|
424
|
+
catch {
|
|
425
|
+
if (signal.aborted) {
|
|
426
|
+
throw new EveHostError("HOST_REQUEST_ABORTED", "The client request was cancelled before Eve became ready.");
|
|
427
|
+
}
|
|
428
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The bounded Eve health probe did not complete.");
|
|
429
|
+
}
|
|
430
|
+
finally {
|
|
431
|
+
clearTimeout(healthTimeout);
|
|
432
|
+
signal.removeEventListener("abort", abortHealth);
|
|
433
|
+
}
|
|
434
|
+
if (response.status !== 200) {
|
|
435
|
+
await response.arrayBuffer().catch(() => undefined);
|
|
436
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Eve health route did not return the expected successful status.");
|
|
437
|
+
}
|
|
438
|
+
if (!(response.headers.get("content-type") ?? "")
|
|
439
|
+
.toLowerCase()
|
|
440
|
+
.startsWith("application/json")) {
|
|
441
|
+
await response.arrayBuffer().catch(() => undefined);
|
|
442
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Eve health route did not return the expected JSON contract.");
|
|
443
|
+
}
|
|
444
|
+
let body;
|
|
445
|
+
try {
|
|
446
|
+
body = await response.json();
|
|
447
|
+
}
|
|
448
|
+
catch {
|
|
449
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Eve health route did not return a valid ready response.");
|
|
450
|
+
}
|
|
451
|
+
if (!responseBodyIsReady(body)) {
|
|
452
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Eve health route is reachable but has not reported ready.");
|
|
453
|
+
}
|
|
454
|
+
return {
|
|
455
|
+
healthPath,
|
|
456
|
+
healthStatus: "ready",
|
|
457
|
+
healthVerified: true,
|
|
458
|
+
port,
|
|
459
|
+
checkedAt: new Date().toISOString(),
|
|
460
|
+
};
|
|
461
|
+
};
|
|
462
|
+
const gate = ((signal) => {
|
|
463
|
+
throwIfAborted(signal);
|
|
464
|
+
if (evidence !== undefined) {
|
|
465
|
+
return Promise.resolve(evidence);
|
|
466
|
+
}
|
|
467
|
+
if (inFlight === undefined) {
|
|
468
|
+
const runGeneration = generation;
|
|
469
|
+
inFlightController = new AbortController();
|
|
470
|
+
inFlight = run(inFlightController.signal)
|
|
471
|
+
.then((result) => {
|
|
472
|
+
if (runGeneration === generation) {
|
|
473
|
+
evidence = result;
|
|
474
|
+
}
|
|
475
|
+
return result;
|
|
476
|
+
})
|
|
477
|
+
.finally(() => {
|
|
478
|
+
if (runGeneration === generation) {
|
|
479
|
+
inFlight = undefined;
|
|
480
|
+
inFlightController = undefined;
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
return raceWithAbort(inFlight, signal);
|
|
485
|
+
});
|
|
486
|
+
gate.reset = () => {
|
|
487
|
+
generation += 1;
|
|
488
|
+
evidence = undefined;
|
|
489
|
+
inFlightController?.abort();
|
|
490
|
+
inFlightController = undefined;
|
|
491
|
+
inFlight = undefined;
|
|
492
|
+
};
|
|
493
|
+
return gate;
|
|
494
|
+
}
|
|
495
|
+
//# sourceMappingURL=eve-host.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export interface EdenWorkerEnvironment {
|
|
2
|
+
readonly EDEN_BEARER_SECRET?: string;
|
|
3
|
+
readonly EDEN_SESSIONS?: DurableObjectNamespace;
|
|
4
|
+
}
|
|
5
|
+
export declare function handleEdenRequest(request: Request, env: EdenWorkerEnvironment): Promise<Response>;
|
|
6
|
+
//# sourceMappingURL=http-host.d.ts.map
|