akamai-edge-delivery-polyfill 1.0.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.
@@ -0,0 +1,846 @@
1
+ var AkamaiPolyfill = (() => {
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
7
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
8
+ }) : x)(function(x) {
9
+ if (typeof require !== "undefined")
10
+ return require.apply(this, arguments);
11
+ throw Error('Dynamic require of "' + x + '" is not supported');
12
+ });
13
+ var __export = (target, all) => {
14
+ for (var name in all)
15
+ __defProp(target, name, { get: all[name], enumerable: true });
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
26
+
27
+ // src/global.ts
28
+ var global_exports = {};
29
+ __export(global_exports, {
30
+ Crypto: () => Crypto,
31
+ Headers: () => Headers,
32
+ KVNamespace: () => KVNamespace,
33
+ Request: () => Request,
34
+ Response: () => Response,
35
+ SubtleCrypto: () => SubtleCrypto,
36
+ default: () => global_default,
37
+ installGlobalPolyfills: () => installGlobalPolyfills,
38
+ toEdgeWorkerResponse: () => toEdgeWorkerResponse
39
+ });
40
+
41
+ // src/request.ts
42
+ var Request = class _Request {
43
+ constructor(input, init) {
44
+ this._bodyUsed = false;
45
+ if (typeof input === "string") {
46
+ this._url = input;
47
+ this._method = init?.method?.toUpperCase() || "GET";
48
+ this._headers = new Headers(init?.headers);
49
+ this._body = init?.body ? this.createReadableStream(init.body) : null;
50
+ } else if (input instanceof _Request) {
51
+ this._url = input.url;
52
+ this._method = input.method;
53
+ this._headers = new Headers(input.headers);
54
+ this._body = input._body;
55
+ this.ewRequest = input.ewRequest;
56
+ } else {
57
+ this.ewRequest = input;
58
+ this._url = this.buildUrl(input);
59
+ this._method = input.method?.toUpperCase() || "GET";
60
+ this._headers = this.convertHeaders(input);
61
+ this._body = this.createBodyStream(input);
62
+ }
63
+ }
64
+ buildUrl(ewRequest) {
65
+ const scheme = ewRequest.scheme || "https";
66
+ const host = ewRequest.host || ewRequest.getHeader("host") || "localhost";
67
+ const path = ewRequest.path || "/";
68
+ const query = ewRequest.query || "";
69
+ return `${scheme}://${host}${path}${query ? "?" + query : ""}`;
70
+ }
71
+ convertHeaders(ewRequest) {
72
+ const headers = new Headers();
73
+ if (ewRequest.getHeaders) {
74
+ const ewHeaders = ewRequest.getHeaders();
75
+ for (const [name, values] of Object.entries(ewHeaders)) {
76
+ if (Array.isArray(values)) {
77
+ values.forEach((value) => headers.append(name, value));
78
+ } else {
79
+ headers.set(name, values);
80
+ }
81
+ }
82
+ }
83
+ return headers;
84
+ }
85
+ createBodyStream(ewRequest) {
86
+ if (!ewRequest.body) {
87
+ return null;
88
+ }
89
+ return new ReadableStream({
90
+ start(controller) {
91
+ if (typeof ewRequest.body === "string") {
92
+ controller.enqueue(new TextEncoder().encode(ewRequest.body));
93
+ } else if (ewRequest.body instanceof Uint8Array) {
94
+ controller.enqueue(ewRequest.body);
95
+ }
96
+ controller.close();
97
+ }
98
+ });
99
+ }
100
+ createReadableStream(body) {
101
+ return new ReadableStream({
102
+ start(controller) {
103
+ if (typeof body === "string") {
104
+ controller.enqueue(new TextEncoder().encode(body));
105
+ } else if (body instanceof Uint8Array) {
106
+ controller.enqueue(body);
107
+ } else if (body instanceof ArrayBuffer) {
108
+ controller.enqueue(new Uint8Array(body));
109
+ }
110
+ controller.close();
111
+ }
112
+ });
113
+ }
114
+ get url() {
115
+ return this._url;
116
+ }
117
+ get method() {
118
+ return this._method;
119
+ }
120
+ get headers() {
121
+ return this._headers;
122
+ }
123
+ get bodyUsed() {
124
+ return this._bodyUsed;
125
+ }
126
+ async text() {
127
+ if (this._bodyUsed) {
128
+ throw new TypeError("Body has already been consumed");
129
+ }
130
+ this._bodyUsed = true;
131
+ if (!this._body) {
132
+ return "";
133
+ }
134
+ const reader = this._body.getReader();
135
+ const chunks = [];
136
+ while (true) {
137
+ const { done, value } = await reader.read();
138
+ if (done)
139
+ break;
140
+ chunks.push(value);
141
+ }
142
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
143
+ const result = new Uint8Array(totalLength);
144
+ let offset = 0;
145
+ for (const chunk of chunks) {
146
+ result.set(chunk, offset);
147
+ offset += chunk.length;
148
+ }
149
+ return new TextDecoder().decode(result);
150
+ }
151
+ async json() {
152
+ const text = await this.text();
153
+ return JSON.parse(text);
154
+ }
155
+ async arrayBuffer() {
156
+ if (this._bodyUsed) {
157
+ throw new TypeError("Body has already been consumed");
158
+ }
159
+ this._bodyUsed = true;
160
+ if (!this._body) {
161
+ return new ArrayBuffer(0);
162
+ }
163
+ const reader = this._body.getReader();
164
+ const chunks = [];
165
+ while (true) {
166
+ const { done, value } = await reader.read();
167
+ if (done)
168
+ break;
169
+ chunks.push(value);
170
+ }
171
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
172
+ const result = new Uint8Array(totalLength);
173
+ let offset = 0;
174
+ for (const chunk of chunks) {
175
+ result.set(chunk, offset);
176
+ offset += chunk.length;
177
+ }
178
+ return result.buffer;
179
+ }
180
+ clone() {
181
+ if (this._bodyUsed) {
182
+ throw new TypeError("Body has already been consumed");
183
+ }
184
+ const cloned = new _Request(this._url, {
185
+ method: this._method,
186
+ headers: this._headers
187
+ });
188
+ cloned.ewRequest = this.ewRequest;
189
+ return cloned;
190
+ }
191
+ };
192
+ var Headers = class _Headers {
193
+ constructor(init) {
194
+ this.headers = /* @__PURE__ */ new Map();
195
+ if (init) {
196
+ if (init instanceof _Headers) {
197
+ init.forEach((value, key) => {
198
+ this.append(key, value);
199
+ });
200
+ } else if (Array.isArray(init)) {
201
+ init.forEach(([key, value]) => {
202
+ this.append(key, value);
203
+ });
204
+ } else {
205
+ Object.entries(init).forEach(([key, value]) => {
206
+ this.append(key, value);
207
+ });
208
+ }
209
+ }
210
+ }
211
+ append(name, value) {
212
+ const normalizedName = name.toLowerCase();
213
+ const existing = this.headers.get(normalizedName) || [];
214
+ existing.push(value);
215
+ this.headers.set(normalizedName, existing);
216
+ }
217
+ delete(name) {
218
+ this.headers.delete(name.toLowerCase());
219
+ }
220
+ get(name) {
221
+ const values = this.headers.get(name.toLowerCase());
222
+ return values ? values[0] : null;
223
+ }
224
+ has(name) {
225
+ return this.headers.has(name.toLowerCase());
226
+ }
227
+ set(name, value) {
228
+ this.headers.set(name.toLowerCase(), [value]);
229
+ }
230
+ forEach(callback) {
231
+ this.headers.forEach((values, key) => {
232
+ values.forEach((value) => callback(value, key, this));
233
+ });
234
+ }
235
+ entries() {
236
+ const entries = [];
237
+ this.forEach((value, key) => {
238
+ entries.push([key, value]);
239
+ });
240
+ return entries[Symbol.iterator]();
241
+ }
242
+ keys() {
243
+ return this.headers.keys();
244
+ }
245
+ values() {
246
+ const values = [];
247
+ this.forEach((value) => {
248
+ values.push(value);
249
+ });
250
+ return values[Symbol.iterator]();
251
+ }
252
+ [Symbol.iterator]() {
253
+ return this.entries();
254
+ }
255
+ };
256
+
257
+ // src/response.ts
258
+ var Response = class _Response {
259
+ constructor(body, init) {
260
+ this._bodyUsed = false;
261
+ this._status = init?.status || 200;
262
+ this._statusText = init?.statusText || this.getStatusText(this._status);
263
+ this._headers = new Headers(init?.headers);
264
+ this._ok = this._status >= 200 && this._status < 300;
265
+ if (body !== null && body !== void 0) {
266
+ this._body = this.createReadableStream(body);
267
+ } else {
268
+ this._body = null;
269
+ }
270
+ }
271
+ getStatusText(status) {
272
+ const statusTexts = {
273
+ 200: "OK",
274
+ 201: "Created",
275
+ 204: "No Content",
276
+ 301: "Moved Permanently",
277
+ 302: "Found",
278
+ 304: "Not Modified",
279
+ 400: "Bad Request",
280
+ 401: "Unauthorized",
281
+ 403: "Forbidden",
282
+ 404: "Not Found",
283
+ 500: "Internal Server Error",
284
+ 502: "Bad Gateway",
285
+ 503: "Service Unavailable"
286
+ };
287
+ return statusTexts[status] || "";
288
+ }
289
+ createReadableStream(body) {
290
+ return new ReadableStream({
291
+ start(controller) {
292
+ if (typeof body === "string") {
293
+ controller.enqueue(new TextEncoder().encode(body));
294
+ } else if (body instanceof Uint8Array) {
295
+ controller.enqueue(body);
296
+ } else if (body instanceof ArrayBuffer) {
297
+ controller.enqueue(new Uint8Array(body));
298
+ } else if (body instanceof ReadableStream) {
299
+ let push = function() {
300
+ reader.read().then(({ done, value }) => {
301
+ if (done) {
302
+ controller.close();
303
+ return;
304
+ }
305
+ controller.enqueue(value);
306
+ push();
307
+ });
308
+ };
309
+ const reader = body.getReader();
310
+ push();
311
+ return;
312
+ }
313
+ controller.close();
314
+ }
315
+ });
316
+ }
317
+ get body() {
318
+ return this._body;
319
+ }
320
+ get bodyUsed() {
321
+ return this._bodyUsed;
322
+ }
323
+ get ok() {
324
+ return this._ok;
325
+ }
326
+ get status() {
327
+ return this._status;
328
+ }
329
+ get statusText() {
330
+ return this._statusText;
331
+ }
332
+ get headers() {
333
+ return this._headers;
334
+ }
335
+ async text() {
336
+ if (this._bodyUsed) {
337
+ throw new TypeError("Body has already been consumed");
338
+ }
339
+ this._bodyUsed = true;
340
+ if (!this._body) {
341
+ return "";
342
+ }
343
+ const reader = this._body.getReader();
344
+ const chunks = [];
345
+ while (true) {
346
+ const { done, value } = await reader.read();
347
+ if (done)
348
+ break;
349
+ chunks.push(value);
350
+ }
351
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
352
+ const result = new Uint8Array(totalLength);
353
+ let offset = 0;
354
+ for (const chunk of chunks) {
355
+ result.set(chunk, offset);
356
+ offset += chunk.length;
357
+ }
358
+ return new TextDecoder().decode(result);
359
+ }
360
+ async json() {
361
+ const text = await this.text();
362
+ return JSON.parse(text);
363
+ }
364
+ async arrayBuffer() {
365
+ if (this._bodyUsed) {
366
+ throw new TypeError("Body has already been consumed");
367
+ }
368
+ this._bodyUsed = true;
369
+ if (!this._body) {
370
+ return new ArrayBuffer(0);
371
+ }
372
+ const reader = this._body.getReader();
373
+ const chunks = [];
374
+ while (true) {
375
+ const { done, value } = await reader.read();
376
+ if (done)
377
+ break;
378
+ chunks.push(value);
379
+ }
380
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
381
+ const result = new Uint8Array(totalLength);
382
+ let offset = 0;
383
+ for (const chunk of chunks) {
384
+ result.set(chunk, offset);
385
+ offset += chunk.length;
386
+ }
387
+ return result.buffer;
388
+ }
389
+ clone() {
390
+ if (this._bodyUsed) {
391
+ throw new TypeError("Body has already been consumed");
392
+ }
393
+ return new _Response(this._body, {
394
+ status: this._status,
395
+ statusText: this._statusText,
396
+ headers: this._headers
397
+ });
398
+ }
399
+ static redirect(url, status) {
400
+ const redirectStatus = status || 302;
401
+ return new _Response(null, {
402
+ status: redirectStatus,
403
+ headers: {
404
+ Location: url
405
+ }
406
+ });
407
+ }
408
+ static json(data, init) {
409
+ const body = JSON.stringify(data);
410
+ const headers = new Headers(init?.headers);
411
+ if (!headers.has("content-type")) {
412
+ headers.set("content-type", "application/json");
413
+ }
414
+ return new _Response(body, {
415
+ ...init,
416
+ headers
417
+ });
418
+ }
419
+ };
420
+
421
+ // src/kv-namespace.ts
422
+ var KVNamespace = class {
423
+ constructor(namespace, group = "default") {
424
+ this.namespace = namespace;
425
+ this.group = group;
426
+ try {
427
+ const { EdgeKV } = __require("./edgekv.js");
428
+ this.edgeKV = new EdgeKV({ namespace: this.namespace, group: this.group });
429
+ } catch (e) {
430
+ console.warn("EdgeKV not available, KVNamespace will not function properly");
431
+ }
432
+ }
433
+ async get(key, optionsOrType) {
434
+ if (!this.edgeKV) {
435
+ throw new Error("EdgeKV not initialized");
436
+ }
437
+ try {
438
+ const response = await this.edgeKV.getText({ item: key });
439
+ if (response === null || response === void 0) {
440
+ return null;
441
+ }
442
+ let type = "text";
443
+ if (typeof optionsOrType === "string") {
444
+ type = optionsOrType;
445
+ } else if (optionsOrType && "type" in optionsOrType) {
446
+ type = optionsOrType.type || "text";
447
+ }
448
+ switch (type) {
449
+ case "json":
450
+ return JSON.parse(response);
451
+ case "arrayBuffer":
452
+ return new TextEncoder().encode(response).buffer;
453
+ case "stream":
454
+ return new ReadableStream({
455
+ start(controller) {
456
+ controller.enqueue(new TextEncoder().encode(response));
457
+ controller.close();
458
+ }
459
+ });
460
+ case "text":
461
+ default:
462
+ return response;
463
+ }
464
+ } catch (error) {
465
+ if (error.status === 404 || error.message?.includes("not found")) {
466
+ return null;
467
+ }
468
+ throw error;
469
+ }
470
+ }
471
+ async getWithMetadata(key, optionsOrType) {
472
+ const value = await this.get(key, optionsOrType);
473
+ return {
474
+ value,
475
+ metadata: null
476
+ };
477
+ }
478
+ /**
479
+ * Put a value into EdgeKV (maps to Cloudflare KV put)
480
+ */
481
+ async put(key, value, options) {
482
+ if (!this.edgeKV) {
483
+ throw new Error("EdgeKV not initialized");
484
+ }
485
+ let stringValue;
486
+ if (typeof value === "string") {
487
+ stringValue = value;
488
+ } else if (value instanceof ArrayBuffer) {
489
+ stringValue = new TextDecoder().decode(value);
490
+ } else if (value instanceof ReadableStream) {
491
+ const reader = value.getReader();
492
+ const chunks = [];
493
+ while (true) {
494
+ const { done, value: value2 } = await reader.read();
495
+ if (done)
496
+ break;
497
+ chunks.push(value2);
498
+ }
499
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
500
+ const result = new Uint8Array(totalLength);
501
+ let offset = 0;
502
+ for (const chunk of chunks) {
503
+ result.set(chunk, offset);
504
+ offset += chunk.length;
505
+ }
506
+ stringValue = new TextDecoder().decode(result);
507
+ } else {
508
+ throw new TypeError("Invalid value type");
509
+ }
510
+ await this.edgeKV.putText({ item: key, value: stringValue });
511
+ }
512
+ /**
513
+ * Delete a value from EdgeKV (maps to Cloudflare KV delete)
514
+ */
515
+ async delete(key) {
516
+ if (!this.edgeKV) {
517
+ throw new Error("EdgeKV not initialized");
518
+ }
519
+ try {
520
+ await this.edgeKV.delete({ item: key });
521
+ } catch (error) {
522
+ if (error.status === 404 || error.message?.includes("not found")) {
523
+ return;
524
+ }
525
+ throw error;
526
+ }
527
+ }
528
+ /**
529
+ * List keys in the namespace
530
+ * Note: EdgeKV doesn't support listing keys, so this is a limited implementation
531
+ */
532
+ async list(options) {
533
+ console.warn("KVNamespace.list() is not fully supported with Akamai EdgeKV");
534
+ return {
535
+ keys: [],
536
+ list_complete: true,
537
+ cursor: void 0
538
+ };
539
+ }
540
+ };
541
+
542
+ // src/crypto.ts
543
+ var Crypto = class {
544
+ /**
545
+ * Generate cryptographically strong random values
546
+ */
547
+ getRandomValues(array) {
548
+ if (typeof crypto !== "undefined" && crypto.getRandomValues) {
549
+ return crypto.getRandomValues(array);
550
+ }
551
+ console.warn("Using fallback random number generation - not cryptographically secure");
552
+ const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
553
+ for (let i = 0; i < bytes.length; i++) {
554
+ bytes[i] = Math.floor(Math.random() * 256);
555
+ }
556
+ return array;
557
+ }
558
+ /**
559
+ * Generate a random UUID
560
+ */
561
+ randomUUID() {
562
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
563
+ return crypto.randomUUID();
564
+ }
565
+ const bytes = new Uint8Array(16);
566
+ this.getRandomValues(bytes);
567
+ bytes[6] = bytes[6] & 15 | 64;
568
+ bytes[8] = bytes[8] & 63 | 128;
569
+ const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
570
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
571
+ }
572
+ /**
573
+ * SubtleCrypto API for cryptographic operations
574
+ */
575
+ get subtle() {
576
+ return new SubtleCrypto();
577
+ }
578
+ };
579
+ var SubtleCrypto = class {
580
+ /**
581
+ * Generate a digest (hash) of the data
582
+ */
583
+ async digest(algorithm, data) {
584
+ const algName = typeof algorithm === "string" ? algorithm : algorithm.name;
585
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.digest) {
586
+ return crypto.subtle.digest(algorithm, data);
587
+ }
588
+ throw new Error(`Digest algorithm ${algName} not supported in this environment`);
589
+ }
590
+ /**
591
+ * Sign data with a key
592
+ */
593
+ async sign(algorithm, key, data) {
594
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.sign) {
595
+ return crypto.subtle.sign(algorithm, key, data);
596
+ }
597
+ throw new Error("Sign operation not supported in this environment");
598
+ }
599
+ /**
600
+ * Verify a signature
601
+ */
602
+ async verify(algorithm, key, signature, data) {
603
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.verify) {
604
+ return crypto.subtle.verify(algorithm, key, signature, data);
605
+ }
606
+ throw new Error("Verify operation not supported in this environment");
607
+ }
608
+ /**
609
+ * Encrypt data
610
+ */
611
+ async encrypt(algorithm, key, data) {
612
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.encrypt) {
613
+ return crypto.subtle.encrypt(algorithm, key, data);
614
+ }
615
+ throw new Error("Encrypt operation not supported in this environment");
616
+ }
617
+ /**
618
+ * Decrypt data
619
+ */
620
+ async decrypt(algorithm, key, data) {
621
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.decrypt) {
622
+ return crypto.subtle.decrypt(algorithm, key, data);
623
+ }
624
+ throw new Error("Decrypt operation not supported in this environment");
625
+ }
626
+ /**
627
+ * Generate a key
628
+ */
629
+ async generateKey(algorithm, extractable, keyUsages) {
630
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.generateKey) {
631
+ return crypto.subtle.generateKey(algorithm, extractable, keyUsages);
632
+ }
633
+ throw new Error("GenerateKey operation not supported in this environment");
634
+ }
635
+ /**
636
+ * Derive bits from a key
637
+ */
638
+ async deriveBits(algorithm, baseKey, length) {
639
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.deriveBits) {
640
+ return crypto.subtle.deriveBits(algorithm, baseKey, length);
641
+ }
642
+ throw new Error("DeriveBits operation not supported in this environment");
643
+ }
644
+ /**
645
+ * Derive a key from another key
646
+ */
647
+ async deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages) {
648
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.deriveKey) {
649
+ return crypto.subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages);
650
+ }
651
+ throw new Error("DeriveKey operation not supported in this environment");
652
+ }
653
+ /**
654
+ * Import a key
655
+ */
656
+ async importKey(format, keyData, algorithm, extractable, keyUsages) {
657
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.importKey) {
658
+ return crypto.subtle.importKey(format, keyData, algorithm, extractable, keyUsages);
659
+ }
660
+ throw new Error("ImportKey operation not supported in this environment");
661
+ }
662
+ /**
663
+ * Export a key
664
+ */
665
+ async exportKey(format, key) {
666
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.exportKey) {
667
+ return crypto.subtle.exportKey(format, key);
668
+ }
669
+ throw new Error("ExportKey operation not supported in this environment");
670
+ }
671
+ /**
672
+ * Wrap a key
673
+ */
674
+ async wrapKey(format, key, wrappingKey, wrapAlgorithm) {
675
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.wrapKey) {
676
+ return crypto.subtle.wrapKey(format, key, wrappingKey, wrapAlgorithm);
677
+ }
678
+ throw new Error("WrapKey operation not supported in this environment");
679
+ }
680
+ /**
681
+ * Unwrap a key
682
+ */
683
+ async unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages) {
684
+ if (typeof crypto !== "undefined" && crypto.subtle && crypto.subtle.unwrapKey) {
685
+ return crypto.subtle.unwrapKey(
686
+ format,
687
+ wrappedKey,
688
+ unwrappingKey,
689
+ unwrapAlgorithm,
690
+ unwrappedKeyAlgorithm,
691
+ extractable,
692
+ keyUsages
693
+ );
694
+ }
695
+ throw new Error("UnwrapKey operation not supported in this environment");
696
+ }
697
+ };
698
+
699
+ // src/index.ts
700
+ async function toEdgeWorkerResponse(response) {
701
+ const body = await response.text();
702
+ const headers = {};
703
+ response.headers.forEach((value, key) => {
704
+ headers[key] = value;
705
+ });
706
+ return {
707
+ status: response.status,
708
+ headers,
709
+ body
710
+ };
711
+ }
712
+ function installGlobalPolyfills() {
713
+ const g = globalThis;
714
+ if (!g.Request) {
715
+ g.Request = Request;
716
+ }
717
+ if (!g.Response) {
718
+ g.Response = Response;
719
+ }
720
+ if (!g.Headers) {
721
+ g.Headers = Headers;
722
+ }
723
+ if (!g.crypto) {
724
+ g.crypto = new Crypto();
725
+ }
726
+ if (!g.TextEncoder) {
727
+ g.TextEncoder = class TextEncoder {
728
+ encode(input) {
729
+ const utf8 = [];
730
+ for (let i = 0; i < input.length; i++) {
731
+ let charCode = input.charCodeAt(i);
732
+ if (charCode < 128) {
733
+ utf8.push(charCode);
734
+ } else if (charCode < 2048) {
735
+ utf8.push(192 | charCode >> 6, 128 | charCode & 63);
736
+ } else if (charCode < 55296 || charCode >= 57344) {
737
+ utf8.push(224 | charCode >> 12, 128 | charCode >> 6 & 63, 128 | charCode & 63);
738
+ } else {
739
+ i++;
740
+ charCode = 65536 + ((charCode & 1023) << 10 | input.charCodeAt(i) & 1023);
741
+ utf8.push(
742
+ 240 | charCode >> 18,
743
+ 128 | charCode >> 12 & 63,
744
+ 128 | charCode >> 6 & 63,
745
+ 128 | charCode & 63
746
+ );
747
+ }
748
+ }
749
+ return new Uint8Array(utf8);
750
+ }
751
+ };
752
+ }
753
+ if (!g.TextDecoder) {
754
+ g.TextDecoder = class TextDecoder {
755
+ decode(input) {
756
+ const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
757
+ let result = "";
758
+ let i = 0;
759
+ while (i < bytes.length) {
760
+ const byte1 = bytes[i++];
761
+ if (byte1 < 128) {
762
+ result += String.fromCharCode(byte1);
763
+ } else if (byte1 < 224) {
764
+ const byte2 = bytes[i++];
765
+ result += String.fromCharCode((byte1 & 31) << 6 | byte2 & 63);
766
+ } else if (byte1 < 240) {
767
+ const byte2 = bytes[i++];
768
+ const byte3 = bytes[i++];
769
+ result += String.fromCharCode((byte1 & 15) << 12 | (byte2 & 63) << 6 | byte3 & 63);
770
+ } else {
771
+ const byte2 = bytes[i++];
772
+ const byte3 = bytes[i++];
773
+ const byte4 = bytes[i++];
774
+ let codePoint = (byte1 & 7) << 18 | (byte2 & 63) << 12 | (byte3 & 63) << 6 | byte4 & 63;
775
+ codePoint -= 65536;
776
+ result += String.fromCharCode(55296 + (codePoint >> 10), 56320 + (codePoint & 1023));
777
+ }
778
+ }
779
+ return result;
780
+ }
781
+ };
782
+ }
783
+ if (!g.ReadableStream) {
784
+ g.ReadableStream = class ReadableStream {
785
+ constructor(underlyingSource) {
786
+ this.started = false;
787
+ this.closed = false;
788
+ this.controller = {
789
+ enqueue: (chunk) => {
790
+ this.controller._queue = this.controller._queue || [];
791
+ this.controller._queue.push(chunk);
792
+ },
793
+ close: () => {
794
+ this.closed = true;
795
+ },
796
+ error: (e) => {
797
+ this.controller._error = e;
798
+ },
799
+ _queue: [],
800
+ _error: null
801
+ };
802
+ if (underlyingSource && underlyingSource.start) {
803
+ underlyingSource.start(this.controller);
804
+ this.started = true;
805
+ }
806
+ }
807
+ getReader() {
808
+ return {
809
+ read: async () => {
810
+ if (this.controller._error) {
811
+ throw this.controller._error;
812
+ }
813
+ if (this.controller._queue.length > 0) {
814
+ return { done: false, value: this.controller._queue.shift() };
815
+ }
816
+ if (this.closed) {
817
+ return { done: true, value: void 0 };
818
+ }
819
+ return { done: true, value: void 0 };
820
+ },
821
+ releaseLock: () => {
822
+ }
823
+ };
824
+ }
825
+ };
826
+ }
827
+ }
828
+
829
+ // src/global.ts
830
+ var global_default = {
831
+ Request,
832
+ Response,
833
+ Headers,
834
+ KVNamespace,
835
+ Crypto,
836
+ SubtleCrypto,
837
+ installGlobalPolyfills,
838
+ toEdgeWorkerResponse
839
+ };
840
+ return __toCommonJS(global_exports);
841
+ })();
842
+ // Auto-install polyfills when script is loaded
843
+ if (typeof AkamaiPolyfill !== 'undefined' && AkamaiPolyfill.installGlobalPolyfills) {
844
+ AkamaiPolyfill.installGlobalPolyfills();
845
+ }
846
+ //# sourceMappingURL=polyfill.global.js.map