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