@hono/node-server 1.19.10 → 1.19.14
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/dist/index.js +102 -13
- package/dist/index.mjs +103 -14
- package/dist/listener.js +102 -13
- package/dist/listener.mjs +103 -14
- package/dist/request.js +11 -0
- package/dist/request.mjs +11 -0
- package/dist/response.d.mts +1 -1
- package/dist/response.d.ts +1 -1
- package/dist/response.js +16 -3
- package/dist/response.mjs +16 -3
- package/dist/serve-static.js +1 -1
- package/dist/serve-static.mjs +1 -1
- package/dist/server.js +102 -13
- package/dist/server.mjs +103 -14
- package/dist/vercel.js +102 -13
- package/dist/vercel.mjs +103 -14
- package/package.json +1 -1
package/dist/server.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { createServer as createServerHTTP } from "http";
|
|
3
3
|
|
|
4
4
|
// src/listener.ts
|
|
5
|
-
import { Http2ServerRequest as Http2ServerRequest2 } from "http2";
|
|
5
|
+
import { Http2ServerRequest as Http2ServerRequest2, constants as h2constants } from "http2";
|
|
6
6
|
|
|
7
7
|
// src/request.ts
|
|
8
8
|
import { Http2ServerRequest } from "http2";
|
|
@@ -151,6 +151,17 @@ var requestPrototype = {
|
|
|
151
151
|
}
|
|
152
152
|
});
|
|
153
153
|
});
|
|
154
|
+
Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), {
|
|
155
|
+
value: function(depth, options, inspectFn) {
|
|
156
|
+
const props = {
|
|
157
|
+
method: this.method,
|
|
158
|
+
url: this.url,
|
|
159
|
+
headers: this.headers,
|
|
160
|
+
nativeRequest: this[requestCache]
|
|
161
|
+
};
|
|
162
|
+
return `Request (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
|
163
|
+
}
|
|
164
|
+
});
|
|
154
165
|
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
155
166
|
var newRequest = (incoming, defaultHostname) => {
|
|
156
167
|
const req = Object.create(requestPrototype);
|
|
@@ -219,15 +230,17 @@ var Response2 = class _Response {
|
|
|
219
230
|
this.#init = init;
|
|
220
231
|
}
|
|
221
232
|
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
222
|
-
|
|
223
|
-
this[cacheKey] = [init?.status || 200, body, headers];
|
|
233
|
+
;
|
|
234
|
+
this[cacheKey] = [init?.status || 200, body, headers || init?.headers];
|
|
224
235
|
}
|
|
225
236
|
}
|
|
226
237
|
get headers() {
|
|
227
238
|
const cache = this[cacheKey];
|
|
228
239
|
if (cache) {
|
|
229
240
|
if (!(cache[2] instanceof Headers)) {
|
|
230
|
-
cache[2] = new Headers(
|
|
241
|
+
cache[2] = new Headers(
|
|
242
|
+
cache[2] || { "content-type": "text/plain; charset=UTF-8" }
|
|
243
|
+
);
|
|
231
244
|
}
|
|
232
245
|
return cache[2];
|
|
233
246
|
}
|
|
@@ -255,6 +268,17 @@ var Response2 = class _Response {
|
|
|
255
268
|
}
|
|
256
269
|
});
|
|
257
270
|
});
|
|
271
|
+
Object.defineProperty(Response2.prototype, Symbol.for("nodejs.util.inspect.custom"), {
|
|
272
|
+
value: function(depth, options, inspectFn) {
|
|
273
|
+
const props = {
|
|
274
|
+
status: this.status,
|
|
275
|
+
headers: this.headers,
|
|
276
|
+
ok: this.ok,
|
|
277
|
+
nativeResponse: this[responseCache]
|
|
278
|
+
};
|
|
279
|
+
return `Response (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
258
282
|
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
259
283
|
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
260
284
|
|
|
@@ -335,6 +359,50 @@ if (typeof global.crypto === "undefined") {
|
|
|
335
359
|
|
|
336
360
|
// src/listener.ts
|
|
337
361
|
var outgoingEnded = Symbol("outgoingEnded");
|
|
362
|
+
var incomingDraining = Symbol("incomingDraining");
|
|
363
|
+
var DRAIN_TIMEOUT_MS = 500;
|
|
364
|
+
var MAX_DRAIN_BYTES = 64 * 1024 * 1024;
|
|
365
|
+
var drainIncoming = (incoming) => {
|
|
366
|
+
const incomingWithDrainState = incoming;
|
|
367
|
+
if (incoming.destroyed || incomingWithDrainState[incomingDraining]) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
incomingWithDrainState[incomingDraining] = true;
|
|
371
|
+
if (incoming instanceof Http2ServerRequest2) {
|
|
372
|
+
try {
|
|
373
|
+
;
|
|
374
|
+
incoming.stream?.close?.(h2constants.NGHTTP2_NO_ERROR);
|
|
375
|
+
} catch {
|
|
376
|
+
}
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
let bytesRead = 0;
|
|
380
|
+
const cleanup = () => {
|
|
381
|
+
clearTimeout(timer);
|
|
382
|
+
incoming.off("data", onData);
|
|
383
|
+
incoming.off("end", cleanup);
|
|
384
|
+
incoming.off("error", cleanup);
|
|
385
|
+
};
|
|
386
|
+
const forceClose = () => {
|
|
387
|
+
cleanup();
|
|
388
|
+
const socket = incoming.socket;
|
|
389
|
+
if (socket && !socket.destroyed) {
|
|
390
|
+
socket.destroySoon();
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
|
|
394
|
+
timer.unref?.();
|
|
395
|
+
const onData = (chunk) => {
|
|
396
|
+
bytesRead += chunk.length;
|
|
397
|
+
if (bytesRead > MAX_DRAIN_BYTES) {
|
|
398
|
+
forceClose();
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
incoming.on("data", onData);
|
|
402
|
+
incoming.on("end", cleanup);
|
|
403
|
+
incoming.on("error", cleanup);
|
|
404
|
+
incoming.resume();
|
|
405
|
+
};
|
|
338
406
|
var handleRequestError = () => new Response(null, {
|
|
339
407
|
status: 400
|
|
340
408
|
});
|
|
@@ -361,15 +429,32 @@ var flushHeaders = (outgoing) => {
|
|
|
361
429
|
};
|
|
362
430
|
var responseViaCache = async (res, outgoing) => {
|
|
363
431
|
let [status, body, header] = res[cacheKey];
|
|
364
|
-
|
|
432
|
+
let hasContentLength = false;
|
|
433
|
+
if (!header) {
|
|
434
|
+
header = { "content-type": "text/plain; charset=UTF-8" };
|
|
435
|
+
} else if (header instanceof Headers) {
|
|
436
|
+
hasContentLength = header.has("content-length");
|
|
365
437
|
header = buildOutgoingHttpHeaders(header);
|
|
438
|
+
} else if (Array.isArray(header)) {
|
|
439
|
+
const headerObj = new Headers(header);
|
|
440
|
+
hasContentLength = headerObj.has("content-length");
|
|
441
|
+
header = buildOutgoingHttpHeaders(headerObj);
|
|
442
|
+
} else {
|
|
443
|
+
for (const key in header) {
|
|
444
|
+
if (key.length === 14 && key.toLowerCase() === "content-length") {
|
|
445
|
+
hasContentLength = true;
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
366
449
|
}
|
|
367
|
-
if (
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
450
|
+
if (!hasContentLength) {
|
|
451
|
+
if (typeof body === "string") {
|
|
452
|
+
header["Content-Length"] = Buffer.byteLength(body);
|
|
453
|
+
} else if (body instanceof Uint8Array) {
|
|
454
|
+
header["Content-Length"] = body.byteLength;
|
|
455
|
+
} else if (body instanceof Blob) {
|
|
456
|
+
header["Content-Length"] = body.size;
|
|
457
|
+
}
|
|
373
458
|
}
|
|
374
459
|
outgoing.writeHead(status, header);
|
|
375
460
|
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
@@ -489,14 +574,18 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
|
|
489
574
|
setTimeout(() => {
|
|
490
575
|
if (!incomingEnded) {
|
|
491
576
|
setTimeout(() => {
|
|
492
|
-
incoming
|
|
493
|
-
outgoing.destroy();
|
|
577
|
+
drainIncoming(incoming);
|
|
494
578
|
});
|
|
495
579
|
}
|
|
496
580
|
});
|
|
497
581
|
}
|
|
498
582
|
};
|
|
499
583
|
}
|
|
584
|
+
outgoing.on("finish", () => {
|
|
585
|
+
if (!incomingEnded) {
|
|
586
|
+
drainIncoming(incoming);
|
|
587
|
+
}
|
|
588
|
+
});
|
|
500
589
|
}
|
|
501
590
|
outgoing.on("close", () => {
|
|
502
591
|
const abortController = req[abortControllerKey];
|
|
@@ -511,7 +600,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
|
|
511
600
|
setTimeout(() => {
|
|
512
601
|
if (!incomingEnded) {
|
|
513
602
|
setTimeout(() => {
|
|
514
|
-
incoming
|
|
603
|
+
drainIncoming(incoming);
|
|
515
604
|
});
|
|
516
605
|
}
|
|
517
606
|
});
|
package/dist/vercel.js
CHANGED
|
@@ -184,6 +184,17 @@ var requestPrototype = {
|
|
|
184
184
|
}
|
|
185
185
|
});
|
|
186
186
|
});
|
|
187
|
+
Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), {
|
|
188
|
+
value: function(depth, options, inspectFn) {
|
|
189
|
+
const props = {
|
|
190
|
+
method: this.method,
|
|
191
|
+
url: this.url,
|
|
192
|
+
headers: this.headers,
|
|
193
|
+
nativeRequest: this[requestCache]
|
|
194
|
+
};
|
|
195
|
+
return `Request (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
187
198
|
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
188
199
|
var newRequest = (incoming, defaultHostname) => {
|
|
189
200
|
const req = Object.create(requestPrototype);
|
|
@@ -252,15 +263,17 @@ var Response2 = class _Response {
|
|
|
252
263
|
this.#init = init;
|
|
253
264
|
}
|
|
254
265
|
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
255
|
-
|
|
256
|
-
this[cacheKey] = [init?.status || 200, body, headers];
|
|
266
|
+
;
|
|
267
|
+
this[cacheKey] = [init?.status || 200, body, headers || init?.headers];
|
|
257
268
|
}
|
|
258
269
|
}
|
|
259
270
|
get headers() {
|
|
260
271
|
const cache = this[cacheKey];
|
|
261
272
|
if (cache) {
|
|
262
273
|
if (!(cache[2] instanceof Headers)) {
|
|
263
|
-
cache[2] = new Headers(
|
|
274
|
+
cache[2] = new Headers(
|
|
275
|
+
cache[2] || { "content-type": "text/plain; charset=UTF-8" }
|
|
276
|
+
);
|
|
264
277
|
}
|
|
265
278
|
return cache[2];
|
|
266
279
|
}
|
|
@@ -288,6 +301,17 @@ var Response2 = class _Response {
|
|
|
288
301
|
}
|
|
289
302
|
});
|
|
290
303
|
});
|
|
304
|
+
Object.defineProperty(Response2.prototype, Symbol.for("nodejs.util.inspect.custom"), {
|
|
305
|
+
value: function(depth, options, inspectFn) {
|
|
306
|
+
const props = {
|
|
307
|
+
status: this.status,
|
|
308
|
+
headers: this.headers,
|
|
309
|
+
ok: this.ok,
|
|
310
|
+
nativeResponse: this[responseCache]
|
|
311
|
+
};
|
|
312
|
+
return `Response (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
|
313
|
+
}
|
|
314
|
+
});
|
|
291
315
|
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
292
316
|
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
293
317
|
|
|
@@ -368,6 +392,50 @@ if (typeof global.crypto === "undefined") {
|
|
|
368
392
|
|
|
369
393
|
// src/listener.ts
|
|
370
394
|
var outgoingEnded = Symbol("outgoingEnded");
|
|
395
|
+
var incomingDraining = Symbol("incomingDraining");
|
|
396
|
+
var DRAIN_TIMEOUT_MS = 500;
|
|
397
|
+
var MAX_DRAIN_BYTES = 64 * 1024 * 1024;
|
|
398
|
+
var drainIncoming = (incoming) => {
|
|
399
|
+
const incomingWithDrainState = incoming;
|
|
400
|
+
if (incoming.destroyed || incomingWithDrainState[incomingDraining]) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
incomingWithDrainState[incomingDraining] = true;
|
|
404
|
+
if (incoming instanceof import_node_http22.Http2ServerRequest) {
|
|
405
|
+
try {
|
|
406
|
+
;
|
|
407
|
+
incoming.stream?.close?.(import_node_http22.constants.NGHTTP2_NO_ERROR);
|
|
408
|
+
} catch {
|
|
409
|
+
}
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
let bytesRead = 0;
|
|
413
|
+
const cleanup = () => {
|
|
414
|
+
clearTimeout(timer);
|
|
415
|
+
incoming.off("data", onData);
|
|
416
|
+
incoming.off("end", cleanup);
|
|
417
|
+
incoming.off("error", cleanup);
|
|
418
|
+
};
|
|
419
|
+
const forceClose = () => {
|
|
420
|
+
cleanup();
|
|
421
|
+
const socket = incoming.socket;
|
|
422
|
+
if (socket && !socket.destroyed) {
|
|
423
|
+
socket.destroySoon();
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
|
|
427
|
+
timer.unref?.();
|
|
428
|
+
const onData = (chunk) => {
|
|
429
|
+
bytesRead += chunk.length;
|
|
430
|
+
if (bytesRead > MAX_DRAIN_BYTES) {
|
|
431
|
+
forceClose();
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
incoming.on("data", onData);
|
|
435
|
+
incoming.on("end", cleanup);
|
|
436
|
+
incoming.on("error", cleanup);
|
|
437
|
+
incoming.resume();
|
|
438
|
+
};
|
|
371
439
|
var handleRequestError = () => new Response(null, {
|
|
372
440
|
status: 400
|
|
373
441
|
});
|
|
@@ -394,15 +462,32 @@ var flushHeaders = (outgoing) => {
|
|
|
394
462
|
};
|
|
395
463
|
var responseViaCache = async (res, outgoing) => {
|
|
396
464
|
let [status, body, header] = res[cacheKey];
|
|
397
|
-
|
|
465
|
+
let hasContentLength = false;
|
|
466
|
+
if (!header) {
|
|
467
|
+
header = { "content-type": "text/plain; charset=UTF-8" };
|
|
468
|
+
} else if (header instanceof Headers) {
|
|
469
|
+
hasContentLength = header.has("content-length");
|
|
398
470
|
header = buildOutgoingHttpHeaders(header);
|
|
471
|
+
} else if (Array.isArray(header)) {
|
|
472
|
+
const headerObj = new Headers(header);
|
|
473
|
+
hasContentLength = headerObj.has("content-length");
|
|
474
|
+
header = buildOutgoingHttpHeaders(headerObj);
|
|
475
|
+
} else {
|
|
476
|
+
for (const key in header) {
|
|
477
|
+
if (key.length === 14 && key.toLowerCase() === "content-length") {
|
|
478
|
+
hasContentLength = true;
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
399
482
|
}
|
|
400
|
-
if (
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
483
|
+
if (!hasContentLength) {
|
|
484
|
+
if (typeof body === "string") {
|
|
485
|
+
header["Content-Length"] = Buffer.byteLength(body);
|
|
486
|
+
} else if (body instanceof Uint8Array) {
|
|
487
|
+
header["Content-Length"] = body.byteLength;
|
|
488
|
+
} else if (body instanceof Blob) {
|
|
489
|
+
header["Content-Length"] = body.size;
|
|
490
|
+
}
|
|
406
491
|
}
|
|
407
492
|
outgoing.writeHead(status, header);
|
|
408
493
|
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
@@ -522,14 +607,18 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
|
|
522
607
|
setTimeout(() => {
|
|
523
608
|
if (!incomingEnded) {
|
|
524
609
|
setTimeout(() => {
|
|
525
|
-
incoming
|
|
526
|
-
outgoing.destroy();
|
|
610
|
+
drainIncoming(incoming);
|
|
527
611
|
});
|
|
528
612
|
}
|
|
529
613
|
});
|
|
530
614
|
}
|
|
531
615
|
};
|
|
532
616
|
}
|
|
617
|
+
outgoing.on("finish", () => {
|
|
618
|
+
if (!incomingEnded) {
|
|
619
|
+
drainIncoming(incoming);
|
|
620
|
+
}
|
|
621
|
+
});
|
|
533
622
|
}
|
|
534
623
|
outgoing.on("close", () => {
|
|
535
624
|
const abortController = req[abortControllerKey];
|
|
@@ -544,7 +633,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
|
|
544
633
|
setTimeout(() => {
|
|
545
634
|
if (!incomingEnded) {
|
|
546
635
|
setTimeout(() => {
|
|
547
|
-
incoming
|
|
636
|
+
drainIncoming(incoming);
|
|
548
637
|
});
|
|
549
638
|
}
|
|
550
639
|
});
|
package/dist/vercel.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/listener.ts
|
|
2
|
-
import { Http2ServerRequest as Http2ServerRequest2 } from "http2";
|
|
2
|
+
import { Http2ServerRequest as Http2ServerRequest2, constants as h2constants } from "http2";
|
|
3
3
|
|
|
4
4
|
// src/request.ts
|
|
5
5
|
import { Http2ServerRequest } from "http2";
|
|
@@ -148,6 +148,17 @@ var requestPrototype = {
|
|
|
148
148
|
}
|
|
149
149
|
});
|
|
150
150
|
});
|
|
151
|
+
Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), {
|
|
152
|
+
value: function(depth, options, inspectFn) {
|
|
153
|
+
const props = {
|
|
154
|
+
method: this.method,
|
|
155
|
+
url: this.url,
|
|
156
|
+
headers: this.headers,
|
|
157
|
+
nativeRequest: this[requestCache]
|
|
158
|
+
};
|
|
159
|
+
return `Request (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
151
162
|
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
152
163
|
var newRequest = (incoming, defaultHostname) => {
|
|
153
164
|
const req = Object.create(requestPrototype);
|
|
@@ -216,15 +227,17 @@ var Response2 = class _Response {
|
|
|
216
227
|
this.#init = init;
|
|
217
228
|
}
|
|
218
229
|
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
219
|
-
|
|
220
|
-
this[cacheKey] = [init?.status || 200, body, headers];
|
|
230
|
+
;
|
|
231
|
+
this[cacheKey] = [init?.status || 200, body, headers || init?.headers];
|
|
221
232
|
}
|
|
222
233
|
}
|
|
223
234
|
get headers() {
|
|
224
235
|
const cache = this[cacheKey];
|
|
225
236
|
if (cache) {
|
|
226
237
|
if (!(cache[2] instanceof Headers)) {
|
|
227
|
-
cache[2] = new Headers(
|
|
238
|
+
cache[2] = new Headers(
|
|
239
|
+
cache[2] || { "content-type": "text/plain; charset=UTF-8" }
|
|
240
|
+
);
|
|
228
241
|
}
|
|
229
242
|
return cache[2];
|
|
230
243
|
}
|
|
@@ -252,6 +265,17 @@ var Response2 = class _Response {
|
|
|
252
265
|
}
|
|
253
266
|
});
|
|
254
267
|
});
|
|
268
|
+
Object.defineProperty(Response2.prototype, Symbol.for("nodejs.util.inspect.custom"), {
|
|
269
|
+
value: function(depth, options, inspectFn) {
|
|
270
|
+
const props = {
|
|
271
|
+
status: this.status,
|
|
272
|
+
headers: this.headers,
|
|
273
|
+
ok: this.ok,
|
|
274
|
+
nativeResponse: this[responseCache]
|
|
275
|
+
};
|
|
276
|
+
return `Response (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
255
279
|
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
256
280
|
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
257
281
|
|
|
@@ -332,6 +356,50 @@ if (typeof global.crypto === "undefined") {
|
|
|
332
356
|
|
|
333
357
|
// src/listener.ts
|
|
334
358
|
var outgoingEnded = Symbol("outgoingEnded");
|
|
359
|
+
var incomingDraining = Symbol("incomingDraining");
|
|
360
|
+
var DRAIN_TIMEOUT_MS = 500;
|
|
361
|
+
var MAX_DRAIN_BYTES = 64 * 1024 * 1024;
|
|
362
|
+
var drainIncoming = (incoming) => {
|
|
363
|
+
const incomingWithDrainState = incoming;
|
|
364
|
+
if (incoming.destroyed || incomingWithDrainState[incomingDraining]) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
incomingWithDrainState[incomingDraining] = true;
|
|
368
|
+
if (incoming instanceof Http2ServerRequest2) {
|
|
369
|
+
try {
|
|
370
|
+
;
|
|
371
|
+
incoming.stream?.close?.(h2constants.NGHTTP2_NO_ERROR);
|
|
372
|
+
} catch {
|
|
373
|
+
}
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
let bytesRead = 0;
|
|
377
|
+
const cleanup = () => {
|
|
378
|
+
clearTimeout(timer);
|
|
379
|
+
incoming.off("data", onData);
|
|
380
|
+
incoming.off("end", cleanup);
|
|
381
|
+
incoming.off("error", cleanup);
|
|
382
|
+
};
|
|
383
|
+
const forceClose = () => {
|
|
384
|
+
cleanup();
|
|
385
|
+
const socket = incoming.socket;
|
|
386
|
+
if (socket && !socket.destroyed) {
|
|
387
|
+
socket.destroySoon();
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
|
|
391
|
+
timer.unref?.();
|
|
392
|
+
const onData = (chunk) => {
|
|
393
|
+
bytesRead += chunk.length;
|
|
394
|
+
if (bytesRead > MAX_DRAIN_BYTES) {
|
|
395
|
+
forceClose();
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
incoming.on("data", onData);
|
|
399
|
+
incoming.on("end", cleanup);
|
|
400
|
+
incoming.on("error", cleanup);
|
|
401
|
+
incoming.resume();
|
|
402
|
+
};
|
|
335
403
|
var handleRequestError = () => new Response(null, {
|
|
336
404
|
status: 400
|
|
337
405
|
});
|
|
@@ -358,15 +426,32 @@ var flushHeaders = (outgoing) => {
|
|
|
358
426
|
};
|
|
359
427
|
var responseViaCache = async (res, outgoing) => {
|
|
360
428
|
let [status, body, header] = res[cacheKey];
|
|
361
|
-
|
|
429
|
+
let hasContentLength = false;
|
|
430
|
+
if (!header) {
|
|
431
|
+
header = { "content-type": "text/plain; charset=UTF-8" };
|
|
432
|
+
} else if (header instanceof Headers) {
|
|
433
|
+
hasContentLength = header.has("content-length");
|
|
362
434
|
header = buildOutgoingHttpHeaders(header);
|
|
435
|
+
} else if (Array.isArray(header)) {
|
|
436
|
+
const headerObj = new Headers(header);
|
|
437
|
+
hasContentLength = headerObj.has("content-length");
|
|
438
|
+
header = buildOutgoingHttpHeaders(headerObj);
|
|
439
|
+
} else {
|
|
440
|
+
for (const key in header) {
|
|
441
|
+
if (key.length === 14 && key.toLowerCase() === "content-length") {
|
|
442
|
+
hasContentLength = true;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
363
446
|
}
|
|
364
|
-
if (
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
447
|
+
if (!hasContentLength) {
|
|
448
|
+
if (typeof body === "string") {
|
|
449
|
+
header["Content-Length"] = Buffer.byteLength(body);
|
|
450
|
+
} else if (body instanceof Uint8Array) {
|
|
451
|
+
header["Content-Length"] = body.byteLength;
|
|
452
|
+
} else if (body instanceof Blob) {
|
|
453
|
+
header["Content-Length"] = body.size;
|
|
454
|
+
}
|
|
370
455
|
}
|
|
371
456
|
outgoing.writeHead(status, header);
|
|
372
457
|
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
@@ -486,14 +571,18 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
|
|
486
571
|
setTimeout(() => {
|
|
487
572
|
if (!incomingEnded) {
|
|
488
573
|
setTimeout(() => {
|
|
489
|
-
incoming
|
|
490
|
-
outgoing.destroy();
|
|
574
|
+
drainIncoming(incoming);
|
|
491
575
|
});
|
|
492
576
|
}
|
|
493
577
|
});
|
|
494
578
|
}
|
|
495
579
|
};
|
|
496
580
|
}
|
|
581
|
+
outgoing.on("finish", () => {
|
|
582
|
+
if (!incomingEnded) {
|
|
583
|
+
drainIncoming(incoming);
|
|
584
|
+
}
|
|
585
|
+
});
|
|
497
586
|
}
|
|
498
587
|
outgoing.on("close", () => {
|
|
499
588
|
const abortController = req[abortControllerKey];
|
|
@@ -508,7 +597,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
|
|
|
508
597
|
setTimeout(() => {
|
|
509
598
|
if (!incomingEnded) {
|
|
510
599
|
setTimeout(() => {
|
|
511
|
-
incoming
|
|
600
|
+
drainIncoming(incoming);
|
|
512
601
|
});
|
|
513
602
|
}
|
|
514
603
|
});
|