Repository navigation
Expand file tree
/
Copy pathedge.js
More file actions
1096 lines (1023 loc) · 40.3 KB
/
Copy pathedge.js
File metadata and controls
1096 lines (1023 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module edge
* @description Pure Web-Standards edge runtime bricks (`INV-LEGO-01`, `INV-LEGO-08`) for
* Cloudflare Workers, Pages Functions, Deno, Bun, and modern Web `fetch(request, env, ctx)` runtimes.
* Uses strictly standard Web globals (`Request`, `Response`, `Headers`, `URL`, `ReadableStream`,
* `TextEncoder`, `TextDecoder`, `AbortController`, `crypto`, `performance`) with zero host-specific imports.
*/
const DEFAULT_EDGE_MAX_BYTES = 1_048_576;
const ALLOW_METHODS = 'GET, POST, PUT, PATCH, DELETE, OPTIONS';
const ALLOW_HEADERS = 'Content-Type, Authorization, X-Request-ID';
const FORBIDDEN_PARAM_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
/**
* Check whether a string contains CR or LF characters.
*
* @param {string} value - Input string to inspect
* @returns {boolean}
*/
function hasCrLf(value) {
return /[\r\n]/.test(value);
}
/**
* Strip CR/LF characters and trim surrounding whitespace for safe header emission.
*
* @param {unknown} rawValue - Candidate header value
* @returns {string}
*/
function sanitizeSingleLineHeader(rawValue) {
return String(rawValue ?? '').replace(/[\r\n]+/g, '').trim();
}
/**
* Normalize a `Headers` instance or plain header record into a plain object without prototype hazards.
*
* @param {Headers|Record<string, string>|null|undefined} input - Header collection
* @returns {Record<string, string>}
*/
function normalizeHeadersRecord(input) {
if (!input || typeof input !== 'object') return {};
if (typeof Headers !== 'undefined' && input instanceof Headers) {
return Object.fromEntries(input.entries());
}
const out = {};
for (const [key, value] of Object.entries(input)) {
if (value !== undefined && value !== null && !FORBIDDEN_PARAM_KEYS.has(key)) {
out[key] = String(value);
}
}
return out;
}
/**
* Generate a correlation ID using standard Web Crypto `randomUUID` with a deterministic-shape fallback.
*
* @returns {string}
*/
function generateRequestId() {
if (typeof globalThis.crypto?.randomUUID === 'function') {
return globalThis.crypto.randomUUID();
}
return `req-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
}
/**
* Read a monotonic millisecond timestamp across edge and standard Web runtimes.
*
* @returns {number}
*/
function nowMs() {
return typeof globalThis.performance?.now === 'function'
? globalThis.performance.now()
: Date.now();
}
/**
* Build a deterministic `INV-LEGO-01` success envelope for edge handlers.
*
* @param {string} tool - Originating service or tool identifier
* @param {string} summary - Concise human-readable status summary
* @param {object} [data={}] - Response payload object
* @returns {{ ok: true, tool: string, summary: string, data: object }}
*/
export function edgeOkEnvelope(tool, summary, data = {}) {
return {
ok: true,
tool: String(tool || 'edge'),
summary: String(summary || 'OK'),
data: data ?? {},
};
}
/**
* Build a deterministic `INV-LEGO-01` error envelope for edge handlers.
*
* @param {string} code - Machine-readable error code
* @param {string} message - Human-readable error message
* @param {unknown} [details=null] - Optional structured diagnostic details
* @param {string} [tool='edge'] - Originating service or tool identifier
* @returns {{ ok: false, tool: string, summary: string, error: { code: string, message: string, details?: unknown } }}
*/
export function edgeErrorEnvelope(code, message, details = null, tool = 'edge') {
return {
ok: false,
tool: String(tool || 'edge'),
summary: String(message || code || 'Request failed'),
error: {
code: String(code || 'EDGE_ERROR'),
message: String(message || 'Request failed'),
...(details !== null && details !== undefined ? { details } : {}),
},
};
}
/**
* Normalize an origin string, returning `''` when blank, CR/LF-tainted, `'null'`, or `'false'`.
*
* @param {unknown} candidate - Raw origin string
* @returns {string}
*/
function normalizeSafeOrigin(candidate) {
if (typeof candidate !== 'string' || hasCrLf(candidate)) return '';
const trimmed = candidate.trim();
if (!trimmed) return '';
const lowered = trimmed.toLowerCase();
if (lowered === 'null' || lowered === 'false') return '';
return trimmed;
}
/**
* Compute Web-Standards CORS response headers while rejecting `Origin: null`, `Origin: false`, and CR/LF (`INV-LEGO-08`).
*
* @param {Request|string|null|undefined} requestOrOrigin - Incoming `Request` or raw `Origin` header string
* @param {string[]} [allowedOrigins=['*']] - Allowed origin list or `['*']`
* @returns {Record<string, string>}
*/
export function edgeCorsHeaders(requestOrOrigin, allowedOrigins = ['*']) {
const rawOrigin = typeof requestOrOrigin === 'string'
? requestOrOrigin
: requestOrOrigin?.headers?.get?.('origin') || '';
const rawList = Array.isArray(allowedOrigins)
? allowedOrigins
: typeof allowedOrigins === 'string'
? [allowedOrigins]
: [];
const normalizedAllowed = rawList.map(normalizeSafeOrigin).filter(Boolean);
if (normalizedAllowed.includes('*')) {
return {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': ALLOW_METHODS,
'Access-Control-Allow-Headers': ALLOW_HEADERS,
};
}
const cleanOrigin = normalizeSafeOrigin(rawOrigin);
if (cleanOrigin && normalizedAllowed.includes(cleanOrigin)) {
return {
Vary: 'Origin',
'Access-Control-Allow-Origin': cleanOrigin,
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Methods': ALLOW_METHODS,
'Access-Control-Allow-Headers': ALLOW_HEADERS,
};
}
return {
Vary: 'Origin',
};
}
/**
* Create a JSON `Response` with `nosniff`, optional CORS headers, and caller header overrides.
*
* @param {unknown} payload - JSON-serializable response payload
* @param {object} [options={}] - Response options
* @param {number} [options.status=200] - HTTP status code
* @param {Headers|Record<string, string>} [options.headers={}] - Explicit response headers
* @param {Headers|Record<string, string>} [options.corsHeaders={}] - Computed CORS headers
* @returns {Response}
*/
export function edgeJsonResponse(payload, { status = 200, headers = {}, corsHeaders = {} } = {}) {
return new Response(JSON.stringify(payload ?? null), {
status,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Content-Type-Options': 'nosniff',
...normalizeHeadersRecord(corsHeaders),
...normalizeHeadersRecord(headers),
},
});
}
/**
* Wrap an `INV-LEGO-01` success envelope in a Web `Response`.
*
* @param {string} tool - Originating service or tool name
* @param {string} summary - Human-readable summary
* @param {object} [data={}] - Response payload data
* @param {object} [options={}] - HTTP status and header options
* @returns {Response}
*/
export function edgeOkResponse(tool, summary, data = {}, options = {}) {
const opts = options || {};
return edgeJsonResponse(edgeOkEnvelope(tool, summary, data), {
status: opts.status ?? 200,
headers: opts.headers,
corsHeaders: opts.corsHeaders,
});
}
/**
* Wrap an `INV-LEGO-01` error envelope in a Web `Response`.
*
* @param {string} code - Machine-readable error code
* @param {string} message - Human-readable error message
* @param {object} [options={}] - Error response options
* @param {number} [options.status=400] - HTTP status code
* @param {unknown} [options.details=null] - Structured diagnostic metadata
* @param {string} [options.tool='edge'] - Originating service or tool name
* @param {Headers|Record<string, string>} [options.headers={}] - Additional headers
* @param {Headers|Record<string, string>} [options.corsHeaders={}] - Computed CORS headers
* @returns {Response}
*/
export function edgeErrorResponse(
code,
message,
{ status = 400, details = null, tool = 'edge', headers = {}, corsHeaders = {} } = {}
) {
return edgeJsonResponse(edgeErrorEnvelope(code, message, details, tool), {
status,
headers,
corsHeaders,
});
}
/**
* Convert an incoming stream chunk into a `Uint8Array` view without copying when already typed.
*
* @param {unknown} value - Stream chunk value
* @param {TextEncoder} encoder - Shared UTF-8 encoder fallback
* @returns {Uint8Array}
*/
function toUint8Chunk(value, encoder) {
if (value instanceof Uint8Array) return value;
if (ArrayBuffer.isView(value)) {
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
}
if (value instanceof ArrayBuffer) return new Uint8Array(value);
return encoder.encode(String(value));
}
/**
* Concatenate multiple `Uint8Array` chunks into a single contiguous buffer.
*
* @param {Uint8Array[]} chunks - Collected byte chunks
* @param {number} totalBytes - Total byte count across chunks
* @returns {Uint8Array}
*/
function concatUint8Chunks(chunks, totalBytes) {
if (chunks.length === 1) return chunks[0];
const combined = new Uint8Array(totalBytes);
let offset = 0;
for (const chunk of chunks) {
combined.set(chunk, offset);
offset += chunk.byteLength;
}
return combined;
}
/**
* Read and parse a bounded JSON body from a Web `Request`, enforcing `maxBytes` on both
* `Content-Length` and chunked `ReadableStream` payloads (`INV-LEGO-08`).
*
* @param {Request} request - Incoming Web `Request`
* @param {object} [options={}] - Reader options
* @param {number} [options.maxBytes=1048576] - Maximum allowed payload size in bytes
* @param {string} [options.tool='edge'] - Tool identifier for error envelopes
* @param {Record<string, string>} [options.corsHeaders={}] - CORS headers to preserve on `400`/`413` responses
* @returns {Promise<{ ok: true, data: any } | { ok: false, response: Response }>}
*/
export async function readBoundedEdgeJson(
request,
{ maxBytes = DEFAULT_EDGE_MAX_BYTES, tool = 'edge', corsHeaders = {} } = {}
) {
const rawLength = request?.headers?.get?.('content-length');
if (rawLength !== null && rawLength !== undefined && rawLength.trim() !== '') {
const declaredBytes = Number(rawLength);
if (Number.isFinite(declaredBytes) && declaredBytes > maxBytes) {
try {
await request.body?.cancel?.();
} catch {
// Ignore stream cancel errors on rejected oversized request
}
return {
ok: false,
response: edgeErrorResponse(
'PAYLOAD_TOO_LARGE',
`Request body exceeds ${maxBytes} bytes`,
{ status: 413, tool, corsHeaders }
),
};
}
}
if (!request?.body) {
return { ok: true, data: {} };
}
const reader = request.body.getReader();
const encoder = new TextEncoder();
const chunks = [];
let totalBytes = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value === undefined || value === null) continue;
const bytes = toUint8Chunk(value, encoder);
totalBytes += bytes.byteLength;
if (totalBytes > maxBytes) {
chunks.length = 0;
try {
await reader.cancel();
} catch {
// Ignore reader cancel errors
}
return {
ok: false,
response: edgeErrorResponse(
'PAYLOAD_TOO_LARGE',
`Request body exceeds ${maxBytes} bytes`,
{ status: 413, tool, corsHeaders }
),
};
}
if (bytes.byteLength > 0) {
chunks.push(bytes);
}
}
} finally {
try {
reader.releaseLock?.();
} catch {
// Lock may already be released after cancel
}
}
if (totalBytes === 0) {
return { ok: true, data: {} };
}
const decoder = new TextDecoder('utf-8', { fatal: false });
const rawText = decoder.decode(concatUint8Chunks(chunks, totalBytes));
if (rawText.trim() === '') {
return { ok: true, data: {} };
}
try {
const parsed = JSON.parse(rawText);
return { ok: true, data: parsed };
} catch {
return {
ok: false,
response: edgeErrorResponse('INVALID_JSON', 'Malformed JSON request body', {
status: 400,
tool,
corsHeaders,
}),
};
}
}
/**
* Format a Server-Sent Events frame safely across single-line JSON and multi-line strings.
*
* @param {string} event - SSE event name
* @param {unknown} data - Event payload
* @returns {string}
*/
function formatEdgeSseFrame(event, data) {
const safeEvent = String(event || 'message').replace(/[\r\n]+/g, ' ').trim() || 'message';
const dataBlock = typeof data === 'string'
? data
.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n')
.split('\n')
.map((line) => `data: ${line}`)
.join('\n')
: `data: ${JSON.stringify(data ?? null)}`;
return `event: ${safeEvent}\n${dataBlock}\n\n`;
}
/**
* Stream Server-Sent Events over a Web `ReadableStream` with automatic `AbortController`
* propagation on `request.signal` abort and `ReadableStream.cancel()` (`INV-LEGO-08`).
*
* @param {(sse: {
* signal: AbortSignal,
* send: (event: string, data: unknown) => boolean,
* sendToken: (delta: string) => boolean,
* done: (summary?: string, data?: object) => void,
* error: (code: string, message?: string, details?: unknown) => void
* }) => Promise<void>|void} producer - Async SSE producer callback
* @param {object} [options={}] - SSE stream configuration
* @param {Request|null} [options.request=null] - Incoming request for `signal` linkage
* @param {string} [options.tool='edge.sse'] - Tool identifier for terminal envelopes
* @param {Record<string, string>} [options.corsHeaders={}] - CORS headers to attach
* @param {number} [options.retryMs=3000] - Client reconnection `retry:` hint in milliseconds
* @returns {Response}
*/
export function edgeSseResponse(
producer,
{ request = null, tool = 'edge.sse', corsHeaders = {}, retryMs = 3000 } = {}
) {
const resolvedTool = String(tool || 'edge.sse');
const controller = new AbortController();
const encoder = new TextEncoder();
let streamController = null;
let streamClosed = false;
let terminalSent = false;
const onRequestAbort = () => {
if (!controller.signal.aborted) {
controller.abort(request?.signal?.reason);
}
};
if (request?.signal) {
if (request.signal.aborted) {
controller.abort(request.signal.reason);
} else {
request.signal.addEventListener('abort', onRequestAbort, { once: true });
}
}
const cleanupSignalListener = () => {
if (request?.signal) {
request.signal.removeEventListener('abort', onRequestAbort);
}
};
const enqueueRaw = (frameText) => {
if (streamClosed || !streamController) return false;
try {
streamController.enqueue(encoder.encode(frameText));
return true;
} catch {
streamClosed = true;
if (!controller.signal.aborted) {
controller.abort();
}
return false;
}
};
const closeStream = () => {
cleanupSignalListener();
if (streamClosed) return;
streamClosed = true;
try {
streamController?.close();
} catch {
// Stream controller already closed
}
};
const sse = {
signal: controller.signal,
send(event, data) {
return enqueueRaw(formatEdgeSseFrame(event, data));
},
sendToken(delta) {
return sse.send('token', { delta: String(delta ?? '') });
},
done(summary = 'Stream complete', data = {}) {
if (terminalSent) return;
terminalSent = true;
if (summary && typeof summary === 'object' && !Array.isArray(summary)) {
const envTool = String(summary.tool || resolvedTool);
const envSummary = String(summary.summary || 'Stream complete');
const envData = summary.data !== undefined ? summary.data : summary;
sse.send('done', edgeOkEnvelope(envTool, envSummary, envData));
} else {
sse.send('done', edgeOkEnvelope(resolvedTool, summary || 'Stream complete', data ?? {}));
}
closeStream();
},
error(code, message, details = null) {
if (terminalSent) return;
terminalSent = true;
if (code && typeof code === 'object') {
const errCode = String(code.code || 'STREAM_ERROR');
const errMsg = String(code.message || 'Stream failed');
const errDetails = code.details !== undefined ? code.details : null;
sse.send('error', edgeErrorEnvelope(errCode, errMsg, errDetails, resolvedTool));
} else {
const errCode = String(code || 'STREAM_ERROR');
const errMsg = String(message || code || 'Stream failed');
sse.send('error', edgeErrorEnvelope(errCode, errMsg, details, resolvedTool));
}
closeStream();
},
};
const stream = new ReadableStream({
start(ctrl) {
streamController = ctrl;
if (controller.signal.aborted) {
streamClosed = true;
cleanupSignalListener();
ctrl.close();
return;
}
if (Number.isFinite(retryMs) && retryMs > 0) {
enqueueRaw(`retry: ${Math.floor(retryMs)}\n\n`);
}
(async () => {
try {
if (typeof producer === 'function') {
await producer(sse);
}
if (!terminalSent && !controller.signal.aborted && !streamClosed) {
sse.done('Stream complete', {});
}
} catch (err) {
if (
!terminalSent &&
!controller.signal.aborted &&
!streamClosed &&
err?.name !== 'AbortError'
) {
sse.error(
err?.code || 'STREAM_ERROR',
err?.message || String(err || 'Stream failed'),
err?.details ?? null
);
}
} finally {
closeStream();
}
})();
},
cancel(reason) {
streamClosed = true;
cleanupSignalListener();
if (!controller.signal.aborted) {
controller.abort(reason);
}
},
});
return new Response(stream, {
status: 200,
headers: {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache, no-transform',
'X-Accel-Buffering': 'no',
'X-Content-Type-Options': 'nosniff',
...normalizeHeadersRecord(corsHeaders),
},
});
}
/**
* Create a JSON key-value store backed by a Cloudflare Workers `KVNamespace` or an in-memory `Map`
* fallback with deterministic TTL expiration and prototype-safe key handling (`INV-LEGO-08`).
*
* @param {object|null} [kvNamespace=null] - Optional Workers KV binding (`get`, `put`, `delete`, `list`)
* @param {object} [options={}] - Store configuration
* @param {string} [options.prefix=''] - Optional key namespace prefix
* @param {number|null} [options.defaultTtlSeconds=null] - Optional default TTL in seconds
* @returns {{
* get: (key: string) => Promise<any>,
* put: (key: string, value: unknown, options?: { ttlSeconds?: number|null, expirationTtl?: number|null }) => Promise<boolean>,
* del: (key: string) => Promise<boolean>,
* list: (options?: { limit?: number, cursor?: string }) => Promise<{ keys: Array<{ name: string, expiration?: number }>, list_complete: boolean, cursor?: string }>
* }}
*/
export function createEdgeKvJsonStore(kvNamespace = null, { prefix = '', defaultTtlSeconds = null } = {}) {
const keyPrefix = String(prefix ?? '');
const hasExternalKv = Boolean(
kvNamespace &&
typeof kvNamespace === 'object' &&
typeof kvNamespace.get === 'function' &&
typeof kvNamespace.put === 'function'
);
const memoryMap = new Map();
const toFullKey = (key) => `${keyPrefix}${String(key ?? '')}`;
const stripPrefix = (fullKey) =>
keyPrefix && fullKey.startsWith(keyPrefix) ? fullKey.slice(keyPrefix.length) : fullKey;
const purgeExpiredMemoryKeys = () => {
const now = Date.now();
for (const [k, entry] of memoryMap.entries()) {
if (entry.expiresAt !== null && now >= entry.expiresAt) {
memoryMap.delete(k);
}
}
};
return {
async get(key) {
const fullKey = toFullKey(key);
if (hasExternalKv) {
const raw = await kvNamespace.get(fullKey);
if (raw === null || raw === undefined) return null;
if (typeof raw !== 'string') return raw;
try {
return JSON.parse(raw);
} catch {
return null;
}
}
const entry = memoryMap.get(fullKey);
if (!entry) return null;
if (entry.expiresAt !== null && Date.now() >= entry.expiresAt) {
memoryMap.delete(fullKey);
return null;
}
try {
return JSON.parse(entry.serialized);
} catch {
return null;
}
},
async put(key, value, putOptions = {}) {
const opts = putOptions || {};
const rawTtl = Object.prototype.hasOwnProperty.call(opts, 'ttlSeconds')
? opts.ttlSeconds
: Object.prototype.hasOwnProperty.call(opts, 'expirationTtl')
? opts.expirationTtl
: defaultTtlSeconds;
const fullKey = toFullKey(key);
const serialized = JSON.stringify(value ?? null);
if (hasExternalKv) {
const numericTtl = rawTtl !== null && rawTtl !== undefined ? Number(rawTtl) : NaN;
if (Number.isFinite(numericTtl) && numericTtl > 0) {
await kvNamespace.put(fullKey, serialized, { expirationTtl: numericTtl });
} else {
await kvNamespace.put(fullKey, serialized);
}
return true;
}
let expiresAt = null;
if (rawTtl !== null && rawTtl !== undefined) {
const numericTtl = Number(rawTtl);
if (Number.isFinite(numericTtl) && numericTtl > 0) {
expiresAt = Date.now() + numericTtl * 1000;
}
}
memoryMap.set(fullKey, { serialized, expiresAt });
return true;
},
async del(key) {
const fullKey = toFullKey(key);
if (hasExternalKv) {
if (typeof kvNamespace.delete === 'function') {
await kvNamespace.delete(fullKey);
return true;
}
return false;
}
return memoryMap.delete(fullKey);
},
async list({ limit = 100, cursor = undefined } = {}) {
const pageLimit = Math.max(1, Math.floor(Number(limit) || 100));
if (hasExternalKv && typeof kvNamespace.list === 'function') {
const rawList = await kvNamespace.list({
...(keyPrefix ? { prefix: keyPrefix } : {}),
limit: pageLimit,
...(cursor !== undefined ? { cursor } : {}),
});
const keys = Array.isArray(rawList?.keys)
? rawList.keys.map((item) => ({
...item,
name: stripPrefix(String(item?.name ?? '')),
}))
: [];
return {
keys,
list_complete: Boolean(rawList?.list_complete ?? true),
...(rawList?.cursor ? { cursor: rawList.cursor } : {}),
};
}
purgeExpiredMemoryKeys();
const matching = [];
for (const [fullKey, entry] of memoryMap.entries()) {
if (!keyPrefix || fullKey.startsWith(keyPrefix)) {
matching.push([fullKey, entry]);
}
}
const startIndex = cursor !== undefined
? Math.max(0, Number.parseInt(String(cursor), 10) || 0)
: 0;
const page = matching.slice(startIndex, startIndex + pageLimit);
const nextIndex = startIndex + page.length;
const listComplete = nextIndex >= matching.length;
return {
keys: page.map(([fullKey, entry]) => ({
name: stripPrefix(fullKey),
...(entry.expiresAt !== null
? { expiration: Math.ceil(entry.expiresAt / 1000) }
: {}),
})),
list_complete: listComplete,
...(listComplete ? {} : { cursor: String(nextIndex) }),
};
},
};
}
/**
* Split a pathname into non-empty raw segments without decoding percent escapes.
*
* @param {string} pathname - URL pathname
* @returns {string[]}
*/
function splitRawPathSegments(pathname) {
if (!pathname || pathname === '/') return [];
return pathname.split('/').filter((seg) => seg.length > 0);
}
/**
* Decode raw URL path segments using `decodeURIComponent`, returning `{ ok: false }` on malformed `%ZZ`.
*
* @param {string} pathname - Raw URL pathname
* @returns {{ ok: true, segments: string[] } | { ok: false }}
*/
function decodeUrlPathSegments(pathname) {
const rawSegments = splitRawPathSegments(pathname);
const decoded = [];
for (const rawSeg of rawSegments) {
try {
decoded.push(decodeURIComponent(rawSeg));
} catch {
return { ok: false };
}
}
return { ok: true, segments: decoded };
}
/**
* Compile route map entries (`"GET /api/items"`, `"POST /api/items/:id"`, `"ALL /api/*"`) safely.
*
* @param {Record<string, Function|object>} routes - Raw route table
* @returns {Array<{ method: string, patternSegments: string[], handler: Function, rawBody: boolean, parseJson: boolean, maxBodyBytes?: number }>}
*/
function compileEdgeRoutes(routes) {
if (!routes || typeof routes !== 'object') return [];
const compiled = [];
for (const [routeKey, routeSpec] of Object.entries(routes)) {
if (FORBIDDEN_PARAM_KEYS.has(routeKey)) continue;
const handler = typeof routeSpec === 'function' ? routeSpec : routeSpec?.handler;
if (typeof handler !== 'function') continue;
const trimmedKey = String(routeKey).trim();
const spaceIdx = trimmedKey.indexOf(' ');
const rawMethod = spaceIdx === -1 ? 'ALL' : trimmedKey.slice(0, spaceIdx).trim().toUpperCase();
const rawPattern = spaceIdx === -1 ? trimmedKey : trimmedKey.slice(spaceIdx + 1).trim();
const method = rawMethod === '*' || !rawMethod ? 'ALL' : rawMethod;
compiled.push({
method,
rawPattern: rawPattern || '/',
patternSegments: splitRawPathSegments(rawPattern || '/'),
handler,
rawBody: Boolean(routeSpec?.rawBody || handler?.rawBody),
parseJson: routeSpec?.parseJson !== false && handler?.parseJson !== false,
maxBodyBytes: routeSpec?.maxBodyBytes ?? handler?.maxBodyBytes,
});
}
return compiled;
}
/**
* Match a request method and decoded path segments against compiled routes.
*
* @param {ReturnType<typeof compileEdgeRoutes>} compiledRoutes - Compiled route list
* @param {string} method - Uppercase HTTP method
* @param {string[]} decodedSegments - Decoded path segments
* @returns {{ route: object, params: Record<string, string> } | null}
*/
function matchCompiledRoute(compiledRoutes, method, decodedSegments) {
for (const route of compiledRoutes) {
const methodMatches =
route.method === 'ALL' ||
route.method === method ||
(method === 'HEAD' && route.method === 'GET');
if (!methodMatches) continue;
const pat = route.patternSegments;
const params = {};
let matched = true;
let hasWildcard = false;
for (let i = 0; i < pat.length; i += 1) {
const token = pat[i];
if (token === '*') {
hasWildcard = true;
params['*'] = decodedSegments.slice(i).join('/');
break;
}
if (i >= decodedSegments.length) {
matched = false;
break;
}
if (token.startsWith(':') && token.length > 1) {
const paramName = token.slice(1);
if (!FORBIDDEN_PARAM_KEYS.has(paramName)) {
params[paramName] = decodedSegments[i];
}
continue;
}
if (token !== decodedSegments[i]) {
matched = false;
break;
}
}
if (matched && (hasWildcard || pat.length === decodedSegments.length)) {
return { route, params };
}
}
return null;
}
/**
* Attach `X-Request-ID`, `X-Response-Time-Ms`, `X-Content-Type-Options: nosniff`, and `corsHeaders`
* safely even when an upstream `Response` carries immutable headers.
*
* @param {Response} response - Outgoing Web `Response`
* @param {object} meta - Request correlation and timing metadata
* @returns {Response}
*/
function finalizeEdgeResponse(response, { requestId, startMs, corsHeaders }) {
const elapsedMs = Math.max(0, nowMs() - startMs).toFixed(2);
const merged = new Headers(response.headers);
for (const [key, value] of Object.entries(corsHeaders)) {
if (!merged.has(key)) {
merged.set(key, value);
}
}
if (!merged.has('X-Content-Type-Options')) {
merged.set('X-Content-Type-Options', 'nosniff');
}
merged.set('X-Request-ID', requestId);
merged.set('X-Response-Time-Ms', elapsedMs);
try {
for (const [key, value] of merged.entries()) {
response.headers.set(key, value);
}
return response;
} catch {
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: merged,
});
}
}
/**
* Execute the built-in `/health` and `/doctor` edge probe and return a `200` or `503` `Response`.
*
* @param {object} cfg - Health check parameters
* @returns {Promise<Response>}
*/
async function evaluateEdgeHealth({ serviceName, version, healthCheck, request, env, ctx, corsHeaders }) {
let extra = null;
if (typeof healthCheck === 'function') {
try {
const result = await healthCheck({ request, env, ctx });
if (result === false || (result && typeof result === 'object' && result.ok === false)) {
const msg =
result?.message ||
result?.summary ||
result?.error?.message ||
'Service unhealthy';
const details = result?.details ?? result?.error?.details ?? null;
return edgeErrorResponse('UNHEALTHY', msg, {
status: 503,
details,
tool: serviceName,
corsHeaders,
});
}
if (result && typeof result === 'object' && !Array.isArray(result)) {
extra = result;
}
} catch (err) {
return edgeErrorResponse('UNHEALTHY', err?.message || 'Health check failed', {
status: 503,
tool: serviceName,
corsHeaders,
});
}
}
return edgeOkResponse(
serviceName,
'Service healthy',
{
service: serviceName,
version,
...(extra && typeof extra === 'object' ? extra : {}),
},
{ corsHeaders }
);
}
/**
* Create a pure Web-Standards edge router returning `{ async fetch(request, env, ctx) }`
* for Cloudflare Workers, Pages Functions, Deno, Bun, and modern Fetch runtimes (`INV-LEGO-08`).
*
* @param {object} [options={}] - Router configuration
* @param {string} [options.serviceName='krusch-edge'] - Service/tool identifier in envelopes
* @param {string} [options.version='1.0.0'] - Semantic version reported on `/health` and `/doctor`
* @param {string[]} [options.corsOrigins=['*']] - Allowed CORS origins
* @param {number} [options.maxBodyBytes=1048576] - Maximum JSON request body size in bytes
* @param {Function|null} [options.healthCheck=null] - Optional async health check `({ request, env, ctx })`
* @param {Record<string, Function|object>} [options.routes={}] - Route table keyed by `"METHOD /path"`
* @param {string|null} [options.assetsBinding='ASSETS'] - Optional `env[assetsBinding]` static fetcher binding
* @returns {{ fetch: (request: Request, env?: Record<string, any>, ctx?: any) => Promise<Response> }}
*/
export function createEdgeRouter({
serviceName = 'krusch-edge',
version = '1.0.0',
corsOrigins = ['*'],
maxBodyBytes = DEFAULT_EDGE_MAX_BYTES,
healthCheck = null,
routes = {},
assetsBinding = 'ASSETS',
} = {}) {
const compiledRoutes = compileEdgeRoutes(routes);
return {
async fetch(request, env = {}, ctx = {}) {
const startMs = nowMs();
const corsHeaders = edgeCorsHeaders(request, corsOrigins);
const incomingId = sanitizeSingleLineHeader(request?.headers?.get?.('x-request-id'));
const requestId = incomingId || generateRequestId();
const meta = { requestId, startMs, corsHeaders };
try {
const method = String(request?.method || 'GET').toUpperCase();
if (method === 'OPTIONS') {
return finalizeEdgeResponse(new Response(null, { status: 204, headers: corsHeaders }), meta);
}
let url;
try {
url = new URL(request?.url || '/', 'http://localhost');
} catch {
return finalizeEdgeResponse(
edgeErrorResponse('INVALID_PATH', 'Invalid request URL', {
status: 400,
tool: serviceName,
corsHeaders,
}),
meta
);
}
const decodedPath = decodeUrlPathSegments(url.pathname);
if (!decodedPath.ok) {
return finalizeEdgeResponse(
edgeErrorResponse('INVALID_PATH', 'Malformed URL path encoding', {
status: 400,
tool: serviceName,
corsHeaders,
}),
meta
);
}
if (
(method === 'GET' || method === 'HEAD') &&
(url.pathname === '/health' || url.pathname === '/doctor')
) {
const healthRes = await evaluateEdgeHealth({
serviceName,
version,
healthCheck,
request,
env,
ctx,