@hono/node-server 1.6.0 → 1.8.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/README.md +34 -0
- package/dist/index.js +102 -42
- package/dist/index.mjs +102 -42
- package/dist/listener.d.mts +4 -2
- package/dist/listener.d.ts +4 -2
- package/dist/listener.js +102 -42
- package/dist/listener.mjs +102 -42
- package/dist/request.d.mts +2 -1
- package/dist/request.d.ts +2 -1
- package/dist/request.js +14 -3
- package/dist/request.mjs +13 -3
- package/dist/response.d.mts +9 -2
- package/dist/response.d.ts +9 -2
- package/dist/response.js +27 -8
- package/dist/response.mjs +25 -7
- package/dist/serve-static.js +6 -28
- package/dist/serve-static.mjs +6 -28
- package/dist/server.js +102 -42
- package/dist/server.mjs +102 -42
- package/dist/types.d.mts +2 -1
- package/dist/types.d.ts +2 -1
- package/dist/utils.js +1 -1
- package/dist/utils.mjs +1 -1
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +102 -42
- package/dist/vercel.mjs +102 -42
- package/package.json +2 -2
package/dist/listener.js
CHANGED
|
@@ -53,7 +53,7 @@ var Request = class extends GlobalRequest {
|
|
|
53
53
|
Object.defineProperty(global, "Request", {
|
|
54
54
|
value: Request
|
|
55
55
|
});
|
|
56
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
56
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
57
57
|
const headerRecord = [];
|
|
58
58
|
const rawHeaders = incoming.rawHeaders;
|
|
59
59
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -65,7 +65,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
65
65
|
}
|
|
66
66
|
const init = {
|
|
67
67
|
method,
|
|
68
|
-
headers: headerRecord
|
|
68
|
+
headers: headerRecord,
|
|
69
|
+
signal: abortController.signal
|
|
69
70
|
};
|
|
70
71
|
if (!(method === "GET" || method === "HEAD")) {
|
|
71
72
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
@@ -76,6 +77,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
76
77
|
var requestCache = Symbol("requestCache");
|
|
77
78
|
var incomingKey = Symbol("incomingKey");
|
|
78
79
|
var urlKey = Symbol("urlKey");
|
|
80
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
81
|
+
var getAbortController = Symbol("getAbortController");
|
|
79
82
|
var requestPrototype = {
|
|
80
83
|
get method() {
|
|
81
84
|
return this[incomingKey].method || "GET";
|
|
@@ -83,11 +86,17 @@ var requestPrototype = {
|
|
|
83
86
|
get url() {
|
|
84
87
|
return this[urlKey];
|
|
85
88
|
},
|
|
89
|
+
[getAbortController]() {
|
|
90
|
+
this[getRequestCache]();
|
|
91
|
+
return this[abortControllerKey];
|
|
92
|
+
},
|
|
86
93
|
[getRequestCache]() {
|
|
94
|
+
this[abortControllerKey] ||= new AbortController();
|
|
87
95
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
88
96
|
this.method,
|
|
89
97
|
this[urlKey],
|
|
90
|
-
this[incomingKey]
|
|
98
|
+
this[incomingKey],
|
|
99
|
+
this[abortControllerKey]
|
|
91
100
|
);
|
|
92
101
|
}
|
|
93
102
|
};
|
|
@@ -181,18 +190,19 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
181
190
|
if (cookies.length > 0) {
|
|
182
191
|
res["set-cookie"] = cookies;
|
|
183
192
|
}
|
|
184
|
-
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
193
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
185
194
|
return res;
|
|
186
195
|
};
|
|
187
196
|
|
|
188
197
|
// src/response.ts
|
|
189
198
|
var responseCache = Symbol("responseCache");
|
|
199
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
190
200
|
var cacheKey = Symbol("cache");
|
|
191
201
|
var GlobalResponse = global.Response;
|
|
192
202
|
var Response2 = class _Response {
|
|
193
203
|
#body;
|
|
194
204
|
#init;
|
|
195
|
-
|
|
205
|
+
[getResponseCache]() {
|
|
196
206
|
delete this[cacheKey];
|
|
197
207
|
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
198
208
|
}
|
|
@@ -202,7 +212,7 @@ var Response2 = class _Response {
|
|
|
202
212
|
const cachedGlobalResponse = init[responseCache];
|
|
203
213
|
if (cachedGlobalResponse) {
|
|
204
214
|
this.#init = cachedGlobalResponse;
|
|
205
|
-
this
|
|
215
|
+
this[getResponseCache]();
|
|
206
216
|
return;
|
|
207
217
|
} else {
|
|
208
218
|
this.#init = init.#init;
|
|
@@ -211,7 +221,7 @@ var Response2 = class _Response {
|
|
|
211
221
|
this.#init = init;
|
|
212
222
|
}
|
|
213
223
|
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
214
|
-
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
224
|
+
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
215
225
|
if (headers instanceof Headers) {
|
|
216
226
|
headers = buildOutgoingHttpHeaders(headers);
|
|
217
227
|
}
|
|
@@ -234,14 +244,14 @@ var Response2 = class _Response {
|
|
|
234
244
|
].forEach((k) => {
|
|
235
245
|
Object.defineProperty(Response2.prototype, k, {
|
|
236
246
|
get() {
|
|
237
|
-
return this
|
|
247
|
+
return this[getResponseCache]()[k];
|
|
238
248
|
}
|
|
239
249
|
});
|
|
240
250
|
});
|
|
241
251
|
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
242
252
|
Object.defineProperty(Response2.prototype, k, {
|
|
243
253
|
value: function() {
|
|
244
|
-
return this
|
|
254
|
+
return this[getResponseCache]()[k]();
|
|
245
255
|
}
|
|
246
256
|
});
|
|
247
257
|
});
|
|
@@ -250,6 +260,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
|
250
260
|
Object.defineProperty(global, "Response", {
|
|
251
261
|
value: Response2
|
|
252
262
|
});
|
|
263
|
+
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
264
|
+
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
265
|
+
);
|
|
266
|
+
if (!stateKey) {
|
|
267
|
+
console.warn("Failed to find Response internal state key");
|
|
268
|
+
}
|
|
269
|
+
function getInternalBody(response) {
|
|
270
|
+
if (!stateKey) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (response instanceof Response2) {
|
|
274
|
+
response = response[getResponseCache]();
|
|
275
|
+
}
|
|
276
|
+
const state = response[stateKey];
|
|
277
|
+
return state && state.body || void 0;
|
|
278
|
+
}
|
|
253
279
|
|
|
254
280
|
// src/globals.ts
|
|
255
281
|
var import_node_crypto = __toESM(require("crypto"));
|
|
@@ -299,48 +325,71 @@ var responseViaCache = (res, outgoing) => {
|
|
|
299
325
|
);
|
|
300
326
|
}
|
|
301
327
|
};
|
|
302
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
328
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
329
|
+
if (res instanceof Promise) {
|
|
330
|
+
if (options.errorHandler) {
|
|
331
|
+
try {
|
|
332
|
+
res = await res;
|
|
333
|
+
} catch (err) {
|
|
334
|
+
const errRes = await options.errorHandler(err);
|
|
335
|
+
if (!errRes) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
res = errRes;
|
|
339
|
+
}
|
|
340
|
+
} else {
|
|
341
|
+
res = await res.catch(handleFetchError);
|
|
308
342
|
}
|
|
309
|
-
}
|
|
310
|
-
|
|
343
|
+
}
|
|
344
|
+
if (cacheKey in res) {
|
|
345
|
+
return responseViaCache(res, outgoing);
|
|
311
346
|
}
|
|
312
347
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
348
|
+
const internalBody = getInternalBody(res);
|
|
349
|
+
if (internalBody) {
|
|
350
|
+
if (internalBody.length) {
|
|
351
|
+
resHeaderRecord["content-length"] = internalBody.length;
|
|
352
|
+
}
|
|
353
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
354
|
+
if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
|
|
355
|
+
outgoing.end(internalBody.source);
|
|
356
|
+
} else if (internalBody.source instanceof Blob) {
|
|
357
|
+
outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
|
|
358
|
+
} else {
|
|
359
|
+
await writeFromReadableStream(internalBody.stream, outgoing);
|
|
360
|
+
}
|
|
361
|
+
} else if (res.body) {
|
|
362
|
+
const {
|
|
363
|
+
"transfer-encoding": transferEncoding,
|
|
364
|
+
"content-encoding": contentEncoding,
|
|
365
|
+
"content-length": contentLength,
|
|
366
|
+
"x-accel-buffering": accelBuffering,
|
|
367
|
+
"content-type": contentType
|
|
368
|
+
} = resHeaderRecord;
|
|
369
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
370
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
371
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
372
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
373
|
+
} else {
|
|
374
|
+
const buffer = await res.arrayBuffer();
|
|
375
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
376
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
377
|
+
outgoing.end(new Uint8Array(buffer));
|
|
334
378
|
}
|
|
335
379
|
} else {
|
|
336
380
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
337
381
|
outgoing.end();
|
|
338
382
|
}
|
|
339
383
|
};
|
|
340
|
-
var getRequestListener = (fetchCallback) => {
|
|
341
|
-
return (incoming, outgoing) => {
|
|
384
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
385
|
+
return async (incoming, outgoing) => {
|
|
342
386
|
let res;
|
|
343
387
|
const req = newRequest(incoming);
|
|
388
|
+
outgoing.on("close", () => {
|
|
389
|
+
if (incoming.destroyed) {
|
|
390
|
+
req[getAbortController]().abort();
|
|
391
|
+
}
|
|
392
|
+
});
|
|
344
393
|
try {
|
|
345
394
|
res = fetchCallback(req, { incoming, outgoing });
|
|
346
395
|
if (cacheKey in res) {
|
|
@@ -348,12 +397,23 @@ var getRequestListener = (fetchCallback) => {
|
|
|
348
397
|
}
|
|
349
398
|
} catch (e) {
|
|
350
399
|
if (!res) {
|
|
351
|
-
|
|
400
|
+
if (options.errorHandler) {
|
|
401
|
+
res = await options.errorHandler(e);
|
|
402
|
+
if (!res) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
} else {
|
|
406
|
+
res = handleFetchError(e);
|
|
407
|
+
}
|
|
352
408
|
} else {
|
|
353
409
|
return handleResponseError(e, outgoing);
|
|
354
410
|
}
|
|
355
411
|
}
|
|
356
|
-
|
|
412
|
+
try {
|
|
413
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
414
|
+
} catch (e) {
|
|
415
|
+
return handleResponseError(e, outgoing);
|
|
416
|
+
}
|
|
357
417
|
};
|
|
358
418
|
};
|
|
359
419
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/listener.mjs
CHANGED
|
@@ -17,7 +17,7 @@ var Request = class extends GlobalRequest {
|
|
|
17
17
|
Object.defineProperty(global, "Request", {
|
|
18
18
|
value: Request
|
|
19
19
|
});
|
|
20
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
20
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
21
21
|
const headerRecord = [];
|
|
22
22
|
const rawHeaders = incoming.rawHeaders;
|
|
23
23
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -29,7 +29,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
29
29
|
}
|
|
30
30
|
const init = {
|
|
31
31
|
method,
|
|
32
|
-
headers: headerRecord
|
|
32
|
+
headers: headerRecord,
|
|
33
|
+
signal: abortController.signal
|
|
33
34
|
};
|
|
34
35
|
if (!(method === "GET" || method === "HEAD")) {
|
|
35
36
|
init.body = Readable.toWeb(incoming);
|
|
@@ -40,6 +41,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
40
41
|
var requestCache = Symbol("requestCache");
|
|
41
42
|
var incomingKey = Symbol("incomingKey");
|
|
42
43
|
var urlKey = Symbol("urlKey");
|
|
44
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
45
|
+
var getAbortController = Symbol("getAbortController");
|
|
43
46
|
var requestPrototype = {
|
|
44
47
|
get method() {
|
|
45
48
|
return this[incomingKey].method || "GET";
|
|
@@ -47,11 +50,17 @@ var requestPrototype = {
|
|
|
47
50
|
get url() {
|
|
48
51
|
return this[urlKey];
|
|
49
52
|
},
|
|
53
|
+
[getAbortController]() {
|
|
54
|
+
this[getRequestCache]();
|
|
55
|
+
return this[abortControllerKey];
|
|
56
|
+
},
|
|
50
57
|
[getRequestCache]() {
|
|
58
|
+
this[abortControllerKey] ||= new AbortController();
|
|
51
59
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
52
60
|
this.method,
|
|
53
61
|
this[urlKey],
|
|
54
|
-
this[incomingKey]
|
|
62
|
+
this[incomingKey],
|
|
63
|
+
this[abortControllerKey]
|
|
55
64
|
);
|
|
56
65
|
}
|
|
57
66
|
};
|
|
@@ -145,18 +154,19 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
145
154
|
if (cookies.length > 0) {
|
|
146
155
|
res["set-cookie"] = cookies;
|
|
147
156
|
}
|
|
148
|
-
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
157
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
149
158
|
return res;
|
|
150
159
|
};
|
|
151
160
|
|
|
152
161
|
// src/response.ts
|
|
153
162
|
var responseCache = Symbol("responseCache");
|
|
163
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
154
164
|
var cacheKey = Symbol("cache");
|
|
155
165
|
var GlobalResponse = global.Response;
|
|
156
166
|
var Response2 = class _Response {
|
|
157
167
|
#body;
|
|
158
168
|
#init;
|
|
159
|
-
|
|
169
|
+
[getResponseCache]() {
|
|
160
170
|
delete this[cacheKey];
|
|
161
171
|
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
162
172
|
}
|
|
@@ -166,7 +176,7 @@ var Response2 = class _Response {
|
|
|
166
176
|
const cachedGlobalResponse = init[responseCache];
|
|
167
177
|
if (cachedGlobalResponse) {
|
|
168
178
|
this.#init = cachedGlobalResponse;
|
|
169
|
-
this
|
|
179
|
+
this[getResponseCache]();
|
|
170
180
|
return;
|
|
171
181
|
} else {
|
|
172
182
|
this.#init = init.#init;
|
|
@@ -175,7 +185,7 @@ var Response2 = class _Response {
|
|
|
175
185
|
this.#init = init;
|
|
176
186
|
}
|
|
177
187
|
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
178
|
-
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
188
|
+
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
179
189
|
if (headers instanceof Headers) {
|
|
180
190
|
headers = buildOutgoingHttpHeaders(headers);
|
|
181
191
|
}
|
|
@@ -198,14 +208,14 @@ var Response2 = class _Response {
|
|
|
198
208
|
].forEach((k) => {
|
|
199
209
|
Object.defineProperty(Response2.prototype, k, {
|
|
200
210
|
get() {
|
|
201
|
-
return this
|
|
211
|
+
return this[getResponseCache]()[k];
|
|
202
212
|
}
|
|
203
213
|
});
|
|
204
214
|
});
|
|
205
215
|
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
206
216
|
Object.defineProperty(Response2.prototype, k, {
|
|
207
217
|
value: function() {
|
|
208
|
-
return this
|
|
218
|
+
return this[getResponseCache]()[k]();
|
|
209
219
|
}
|
|
210
220
|
});
|
|
211
221
|
});
|
|
@@ -214,6 +224,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
|
214
224
|
Object.defineProperty(global, "Response", {
|
|
215
225
|
value: Response2
|
|
216
226
|
});
|
|
227
|
+
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
228
|
+
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
229
|
+
);
|
|
230
|
+
if (!stateKey) {
|
|
231
|
+
console.warn("Failed to find Response internal state key");
|
|
232
|
+
}
|
|
233
|
+
function getInternalBody(response) {
|
|
234
|
+
if (!stateKey) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (response instanceof Response2) {
|
|
238
|
+
response = response[getResponseCache]();
|
|
239
|
+
}
|
|
240
|
+
const state = response[stateKey];
|
|
241
|
+
return state && state.body || void 0;
|
|
242
|
+
}
|
|
217
243
|
|
|
218
244
|
// src/globals.ts
|
|
219
245
|
import crypto from "crypto";
|
|
@@ -263,48 +289,71 @@ var responseViaCache = (res, outgoing) => {
|
|
|
263
289
|
);
|
|
264
290
|
}
|
|
265
291
|
};
|
|
266
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
292
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
293
|
+
if (res instanceof Promise) {
|
|
294
|
+
if (options.errorHandler) {
|
|
295
|
+
try {
|
|
296
|
+
res = await res;
|
|
297
|
+
} catch (err) {
|
|
298
|
+
const errRes = await options.errorHandler(err);
|
|
299
|
+
if (!errRes) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
res = errRes;
|
|
303
|
+
}
|
|
304
|
+
} else {
|
|
305
|
+
res = await res.catch(handleFetchError);
|
|
272
306
|
}
|
|
273
|
-
}
|
|
274
|
-
|
|
307
|
+
}
|
|
308
|
+
if (cacheKey in res) {
|
|
309
|
+
return responseViaCache(res, outgoing);
|
|
275
310
|
}
|
|
276
311
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
312
|
+
const internalBody = getInternalBody(res);
|
|
313
|
+
if (internalBody) {
|
|
314
|
+
if (internalBody.length) {
|
|
315
|
+
resHeaderRecord["content-length"] = internalBody.length;
|
|
316
|
+
}
|
|
317
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
318
|
+
if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
|
|
319
|
+
outgoing.end(internalBody.source);
|
|
320
|
+
} else if (internalBody.source instanceof Blob) {
|
|
321
|
+
outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
|
|
322
|
+
} else {
|
|
323
|
+
await writeFromReadableStream(internalBody.stream, outgoing);
|
|
324
|
+
}
|
|
325
|
+
} else if (res.body) {
|
|
326
|
+
const {
|
|
327
|
+
"transfer-encoding": transferEncoding,
|
|
328
|
+
"content-encoding": contentEncoding,
|
|
329
|
+
"content-length": contentLength,
|
|
330
|
+
"x-accel-buffering": accelBuffering,
|
|
331
|
+
"content-type": contentType
|
|
332
|
+
} = resHeaderRecord;
|
|
333
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
334
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
335
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
336
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
337
|
+
} else {
|
|
338
|
+
const buffer = await res.arrayBuffer();
|
|
339
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
340
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
341
|
+
outgoing.end(new Uint8Array(buffer));
|
|
298
342
|
}
|
|
299
343
|
} else {
|
|
300
344
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
301
345
|
outgoing.end();
|
|
302
346
|
}
|
|
303
347
|
};
|
|
304
|
-
var getRequestListener = (fetchCallback) => {
|
|
305
|
-
return (incoming, outgoing) => {
|
|
348
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
349
|
+
return async (incoming, outgoing) => {
|
|
306
350
|
let res;
|
|
307
351
|
const req = newRequest(incoming);
|
|
352
|
+
outgoing.on("close", () => {
|
|
353
|
+
if (incoming.destroyed) {
|
|
354
|
+
req[getAbortController]().abort();
|
|
355
|
+
}
|
|
356
|
+
});
|
|
308
357
|
try {
|
|
309
358
|
res = fetchCallback(req, { incoming, outgoing });
|
|
310
359
|
if (cacheKey in res) {
|
|
@@ -312,12 +361,23 @@ var getRequestListener = (fetchCallback) => {
|
|
|
312
361
|
}
|
|
313
362
|
} catch (e) {
|
|
314
363
|
if (!res) {
|
|
315
|
-
|
|
364
|
+
if (options.errorHandler) {
|
|
365
|
+
res = await options.errorHandler(e);
|
|
366
|
+
if (!res) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
370
|
+
res = handleFetchError(e);
|
|
371
|
+
}
|
|
316
372
|
} else {
|
|
317
373
|
return handleResponseError(e, outgoing);
|
|
318
374
|
}
|
|
319
375
|
}
|
|
320
|
-
|
|
376
|
+
try {
|
|
377
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
378
|
+
} catch (e) {
|
|
379
|
+
return handleResponseError(e, outgoing);
|
|
380
|
+
}
|
|
321
381
|
};
|
|
322
382
|
};
|
|
323
383
|
export {
|
package/dist/request.d.mts
CHANGED
|
@@ -8,6 +8,7 @@ declare const GlobalRequest: {
|
|
|
8
8
|
declare class Request extends GlobalRequest {
|
|
9
9
|
constructor(input: string | Request, options?: RequestInit);
|
|
10
10
|
}
|
|
11
|
+
declare const getAbortController: unique symbol;
|
|
11
12
|
declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest) => any;
|
|
12
13
|
|
|
13
|
-
export { GlobalRequest, Request, newRequest };
|
|
14
|
+
export { GlobalRequest, Request, getAbortController, newRequest };
|
package/dist/request.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ declare const GlobalRequest: {
|
|
|
8
8
|
declare class Request extends GlobalRequest {
|
|
9
9
|
constructor(input: string | Request, options?: RequestInit);
|
|
10
10
|
}
|
|
11
|
+
declare const getAbortController: unique symbol;
|
|
11
12
|
declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest) => any;
|
|
12
13
|
|
|
13
|
-
export { GlobalRequest, Request, newRequest };
|
|
14
|
+
export { GlobalRequest, Request, getAbortController, newRequest };
|
package/dist/request.js
CHANGED
|
@@ -22,6 +22,7 @@ var request_exports = {};
|
|
|
22
22
|
__export(request_exports, {
|
|
23
23
|
GlobalRequest: () => GlobalRequest,
|
|
24
24
|
Request: () => Request,
|
|
25
|
+
getAbortController: () => getAbortController,
|
|
25
26
|
newRequest: () => newRequest
|
|
26
27
|
});
|
|
27
28
|
module.exports = __toCommonJS(request_exports);
|
|
@@ -43,7 +44,7 @@ var Request = class extends GlobalRequest {
|
|
|
43
44
|
Object.defineProperty(global, "Request", {
|
|
44
45
|
value: Request
|
|
45
46
|
});
|
|
46
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
47
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
47
48
|
const headerRecord = [];
|
|
48
49
|
const rawHeaders = incoming.rawHeaders;
|
|
49
50
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -55,7 +56,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
55
56
|
}
|
|
56
57
|
const init = {
|
|
57
58
|
method,
|
|
58
|
-
headers: headerRecord
|
|
59
|
+
headers: headerRecord,
|
|
60
|
+
signal: abortController.signal
|
|
59
61
|
};
|
|
60
62
|
if (!(method === "GET" || method === "HEAD")) {
|
|
61
63
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
@@ -66,6 +68,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
66
68
|
var requestCache = Symbol("requestCache");
|
|
67
69
|
var incomingKey = Symbol("incomingKey");
|
|
68
70
|
var urlKey = Symbol("urlKey");
|
|
71
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
72
|
+
var getAbortController = Symbol("getAbortController");
|
|
69
73
|
var requestPrototype = {
|
|
70
74
|
get method() {
|
|
71
75
|
return this[incomingKey].method || "GET";
|
|
@@ -73,11 +77,17 @@ var requestPrototype = {
|
|
|
73
77
|
get url() {
|
|
74
78
|
return this[urlKey];
|
|
75
79
|
},
|
|
80
|
+
[getAbortController]() {
|
|
81
|
+
this[getRequestCache]();
|
|
82
|
+
return this[abortControllerKey];
|
|
83
|
+
},
|
|
76
84
|
[getRequestCache]() {
|
|
85
|
+
this[abortControllerKey] ||= new AbortController();
|
|
77
86
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
78
87
|
this.method,
|
|
79
88
|
this[urlKey],
|
|
80
|
-
this[incomingKey]
|
|
89
|
+
this[incomingKey],
|
|
90
|
+
this[abortControllerKey]
|
|
81
91
|
);
|
|
82
92
|
}
|
|
83
93
|
};
|
|
@@ -121,5 +131,6 @@ var newRequest = (incoming) => {
|
|
|
121
131
|
0 && (module.exports = {
|
|
122
132
|
GlobalRequest,
|
|
123
133
|
Request,
|
|
134
|
+
getAbortController,
|
|
124
135
|
newRequest
|
|
125
136
|
});
|
package/dist/request.mjs
CHANGED
|
@@ -17,7 +17,7 @@ var Request = class extends GlobalRequest {
|
|
|
17
17
|
Object.defineProperty(global, "Request", {
|
|
18
18
|
value: Request
|
|
19
19
|
});
|
|
20
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
20
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
21
21
|
const headerRecord = [];
|
|
22
22
|
const rawHeaders = incoming.rawHeaders;
|
|
23
23
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -29,7 +29,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
29
29
|
}
|
|
30
30
|
const init = {
|
|
31
31
|
method,
|
|
32
|
-
headers: headerRecord
|
|
32
|
+
headers: headerRecord,
|
|
33
|
+
signal: abortController.signal
|
|
33
34
|
};
|
|
34
35
|
if (!(method === "GET" || method === "HEAD")) {
|
|
35
36
|
init.body = Readable.toWeb(incoming);
|
|
@@ -40,6 +41,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
40
41
|
var requestCache = Symbol("requestCache");
|
|
41
42
|
var incomingKey = Symbol("incomingKey");
|
|
42
43
|
var urlKey = Symbol("urlKey");
|
|
44
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
45
|
+
var getAbortController = Symbol("getAbortController");
|
|
43
46
|
var requestPrototype = {
|
|
44
47
|
get method() {
|
|
45
48
|
return this[incomingKey].method || "GET";
|
|
@@ -47,11 +50,17 @@ var requestPrototype = {
|
|
|
47
50
|
get url() {
|
|
48
51
|
return this[urlKey];
|
|
49
52
|
},
|
|
53
|
+
[getAbortController]() {
|
|
54
|
+
this[getRequestCache]();
|
|
55
|
+
return this[abortControllerKey];
|
|
56
|
+
},
|
|
50
57
|
[getRequestCache]() {
|
|
58
|
+
this[abortControllerKey] ||= new AbortController();
|
|
51
59
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
52
60
|
this.method,
|
|
53
61
|
this[urlKey],
|
|
54
|
-
this[incomingKey]
|
|
62
|
+
this[incomingKey],
|
|
63
|
+
this[abortControllerKey]
|
|
55
64
|
);
|
|
56
65
|
}
|
|
57
66
|
};
|
|
@@ -94,5 +103,6 @@ var newRequest = (incoming) => {
|
|
|
94
103
|
export {
|
|
95
104
|
GlobalRequest,
|
|
96
105
|
Request,
|
|
106
|
+
getAbortController,
|
|
97
107
|
newRequest
|
|
98
108
|
};
|
package/dist/response.d.mts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
interface InternalBody {
|
|
2
|
+
source: string | Uint8Array | FormData | Blob | null;
|
|
3
|
+
stream: ReadableStream;
|
|
4
|
+
length: number | null;
|
|
5
|
+
}
|
|
6
|
+
declare const getResponseCache: unique symbol;
|
|
1
7
|
declare const cacheKey: unique symbol;
|
|
2
8
|
declare const GlobalResponse: {
|
|
3
9
|
new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): globalThis.Response;
|
|
@@ -8,8 +14,9 @@ declare const GlobalResponse: {
|
|
|
8
14
|
};
|
|
9
15
|
declare class Response {
|
|
10
16
|
#private;
|
|
11
|
-
|
|
17
|
+
[getResponseCache](): typeof GlobalResponse;
|
|
12
18
|
constructor(body?: BodyInit | null, init?: ResponseInit);
|
|
13
19
|
}
|
|
20
|
+
declare function getInternalBody(response: Response | typeof GlobalResponse): InternalBody | undefined;
|
|
14
21
|
|
|
15
|
-
export { GlobalResponse, Response, cacheKey };
|
|
22
|
+
export { GlobalResponse, Response, cacheKey, getInternalBody };
|
package/dist/response.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
interface InternalBody {
|
|
2
|
+
source: string | Uint8Array | FormData | Blob | null;
|
|
3
|
+
stream: ReadableStream;
|
|
4
|
+
length: number | null;
|
|
5
|
+
}
|
|
6
|
+
declare const getResponseCache: unique symbol;
|
|
1
7
|
declare const cacheKey: unique symbol;
|
|
2
8
|
declare const GlobalResponse: {
|
|
3
9
|
new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): globalThis.Response;
|
|
@@ -8,8 +14,9 @@ declare const GlobalResponse: {
|
|
|
8
14
|
};
|
|
9
15
|
declare class Response {
|
|
10
16
|
#private;
|
|
11
|
-
|
|
17
|
+
[getResponseCache](): typeof GlobalResponse;
|
|
12
18
|
constructor(body?: BodyInit | null, init?: ResponseInit);
|
|
13
19
|
}
|
|
20
|
+
declare function getInternalBody(response: Response | typeof GlobalResponse): InternalBody | undefined;
|
|
14
21
|
|
|
15
|
-
export { GlobalResponse, Response, cacheKey };
|
|
22
|
+
export { GlobalResponse, Response, cacheKey, getInternalBody };
|