@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 CHANGED
@@ -190,6 +190,17 @@ var requestPrototype = {
190
190
  }
191
191
  });
192
192
  });
193
+ Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), {
194
+ value: function(depth, options, inspectFn) {
195
+ const props = {
196
+ method: this.method,
197
+ url: this.url,
198
+ headers: this.headers,
199
+ nativeRequest: this[requestCache]
200
+ };
201
+ return `Request (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
202
+ }
203
+ });
193
204
  Object.setPrototypeOf(requestPrototype, Request.prototype);
194
205
  var newRequest = (incoming, defaultHostname) => {
195
206
  const req = Object.create(requestPrototype);
@@ -258,15 +269,17 @@ var Response2 = class _Response {
258
269
  this.#init = init;
259
270
  }
260
271
  if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
261
- headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
262
- this[cacheKey] = [init?.status || 200, body, headers];
272
+ ;
273
+ this[cacheKey] = [init?.status || 200, body, headers || init?.headers];
263
274
  }
264
275
  }
265
276
  get headers() {
266
277
  const cache = this[cacheKey];
267
278
  if (cache) {
268
279
  if (!(cache[2] instanceof Headers)) {
269
- cache[2] = new Headers(cache[2]);
280
+ cache[2] = new Headers(
281
+ cache[2] || { "content-type": "text/plain; charset=UTF-8" }
282
+ );
270
283
  }
271
284
  return cache[2];
272
285
  }
@@ -294,6 +307,17 @@ var Response2 = class _Response {
294
307
  }
295
308
  });
296
309
  });
310
+ Object.defineProperty(Response2.prototype, Symbol.for("nodejs.util.inspect.custom"), {
311
+ value: function(depth, options, inspectFn) {
312
+ const props = {
313
+ status: this.status,
314
+ headers: this.headers,
315
+ ok: this.ok,
316
+ nativeResponse: this[responseCache]
317
+ };
318
+ return `Response (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
319
+ }
320
+ });
297
321
  Object.setPrototypeOf(Response2, GlobalResponse);
298
322
  Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
299
323
 
@@ -374,6 +398,50 @@ if (typeof global.crypto === "undefined") {
374
398
 
375
399
  // src/listener.ts
376
400
  var outgoingEnded = Symbol("outgoingEnded");
401
+ var incomingDraining = Symbol("incomingDraining");
402
+ var DRAIN_TIMEOUT_MS = 500;
403
+ var MAX_DRAIN_BYTES = 64 * 1024 * 1024;
404
+ var drainIncoming = (incoming) => {
405
+ const incomingWithDrainState = incoming;
406
+ if (incoming.destroyed || incomingWithDrainState[incomingDraining]) {
407
+ return;
408
+ }
409
+ incomingWithDrainState[incomingDraining] = true;
410
+ if (incoming instanceof import_node_http22.Http2ServerRequest) {
411
+ try {
412
+ ;
413
+ incoming.stream?.close?.(import_node_http22.constants.NGHTTP2_NO_ERROR);
414
+ } catch {
415
+ }
416
+ return;
417
+ }
418
+ let bytesRead = 0;
419
+ const cleanup = () => {
420
+ clearTimeout(timer);
421
+ incoming.off("data", onData);
422
+ incoming.off("end", cleanup);
423
+ incoming.off("error", cleanup);
424
+ };
425
+ const forceClose = () => {
426
+ cleanup();
427
+ const socket = incoming.socket;
428
+ if (socket && !socket.destroyed) {
429
+ socket.destroySoon();
430
+ }
431
+ };
432
+ const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
433
+ timer.unref?.();
434
+ const onData = (chunk) => {
435
+ bytesRead += chunk.length;
436
+ if (bytesRead > MAX_DRAIN_BYTES) {
437
+ forceClose();
438
+ }
439
+ };
440
+ incoming.on("data", onData);
441
+ incoming.on("end", cleanup);
442
+ incoming.on("error", cleanup);
443
+ incoming.resume();
444
+ };
377
445
  var handleRequestError = () => new Response(null, {
378
446
  status: 400
379
447
  });
@@ -400,15 +468,32 @@ var flushHeaders = (outgoing) => {
400
468
  };
401
469
  var responseViaCache = async (res, outgoing) => {
402
470
  let [status, body, header] = res[cacheKey];
403
- if (header instanceof Headers) {
471
+ let hasContentLength = false;
472
+ if (!header) {
473
+ header = { "content-type": "text/plain; charset=UTF-8" };
474
+ } else if (header instanceof Headers) {
475
+ hasContentLength = header.has("content-length");
404
476
  header = buildOutgoingHttpHeaders(header);
477
+ } else if (Array.isArray(header)) {
478
+ const headerObj = new Headers(header);
479
+ hasContentLength = headerObj.has("content-length");
480
+ header = buildOutgoingHttpHeaders(headerObj);
481
+ } else {
482
+ for (const key in header) {
483
+ if (key.length === 14 && key.toLowerCase() === "content-length") {
484
+ hasContentLength = true;
485
+ break;
486
+ }
487
+ }
405
488
  }
406
- if (typeof body === "string") {
407
- header["Content-Length"] = Buffer.byteLength(body);
408
- } else if (body instanceof Uint8Array) {
409
- header["Content-Length"] = body.byteLength;
410
- } else if (body instanceof Blob) {
411
- header["Content-Length"] = body.size;
489
+ if (!hasContentLength) {
490
+ if (typeof body === "string") {
491
+ header["Content-Length"] = Buffer.byteLength(body);
492
+ } else if (body instanceof Uint8Array) {
493
+ header["Content-Length"] = body.byteLength;
494
+ } else if (body instanceof Blob) {
495
+ header["Content-Length"] = body.size;
496
+ }
412
497
  }
413
498
  outgoing.writeHead(status, header);
414
499
  if (typeof body === "string" || body instanceof Uint8Array) {
@@ -528,14 +613,18 @@ var getRequestListener = (fetchCallback, options = {}) => {
528
613
  setTimeout(() => {
529
614
  if (!incomingEnded) {
530
615
  setTimeout(() => {
531
- incoming.destroy();
532
- outgoing.destroy();
616
+ drainIncoming(incoming);
533
617
  });
534
618
  }
535
619
  });
536
620
  }
537
621
  };
538
622
  }
623
+ outgoing.on("finish", () => {
624
+ if (!incomingEnded) {
625
+ drainIncoming(incoming);
626
+ }
627
+ });
539
628
  }
540
629
  outgoing.on("close", () => {
541
630
  const abortController = req[abortControllerKey];
@@ -550,7 +639,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
550
639
  setTimeout(() => {
551
640
  if (!incomingEnded) {
552
641
  setTimeout(() => {
553
- incoming.destroy();
642
+ drainIncoming(incoming);
554
643
  });
555
644
  }
556
645
  });
package/dist/index.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
- headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
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(cache[2]);
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
- if (header instanceof Headers) {
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 (typeof body === "string") {
368
- header["Content-Length"] = Buffer.byteLength(body);
369
- } else if (body instanceof Uint8Array) {
370
- header["Content-Length"] = body.byteLength;
371
- } else if (body instanceof Blob) {
372
- header["Content-Length"] = body.size;
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.destroy();
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.destroy();
603
+ drainIncoming(incoming);
515
604
  });
516
605
  }
517
606
  });
package/dist/listener.js CHANGED
@@ -182,6 +182,17 @@ var requestPrototype = {
182
182
  }
183
183
  });
184
184
  });
185
+ Object.defineProperty(requestPrototype, Symbol.for("nodejs.util.inspect.custom"), {
186
+ value: function(depth, options, inspectFn) {
187
+ const props = {
188
+ method: this.method,
189
+ url: this.url,
190
+ headers: this.headers,
191
+ nativeRequest: this[requestCache]
192
+ };
193
+ return `Request (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
194
+ }
195
+ });
185
196
  Object.setPrototypeOf(requestPrototype, Request.prototype);
186
197
  var newRequest = (incoming, defaultHostname) => {
187
198
  const req = Object.create(requestPrototype);
@@ -250,15 +261,17 @@ var Response2 = class _Response {
250
261
  this.#init = init;
251
262
  }
252
263
  if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
253
- headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
254
- this[cacheKey] = [init?.status || 200, body, headers];
264
+ ;
265
+ this[cacheKey] = [init?.status || 200, body, headers || init?.headers];
255
266
  }
256
267
  }
257
268
  get headers() {
258
269
  const cache = this[cacheKey];
259
270
  if (cache) {
260
271
  if (!(cache[2] instanceof Headers)) {
261
- cache[2] = new Headers(cache[2]);
272
+ cache[2] = new Headers(
273
+ cache[2] || { "content-type": "text/plain; charset=UTF-8" }
274
+ );
262
275
  }
263
276
  return cache[2];
264
277
  }
@@ -286,6 +299,17 @@ var Response2 = class _Response {
286
299
  }
287
300
  });
288
301
  });
302
+ Object.defineProperty(Response2.prototype, Symbol.for("nodejs.util.inspect.custom"), {
303
+ value: function(depth, options, inspectFn) {
304
+ const props = {
305
+ status: this.status,
306
+ headers: this.headers,
307
+ ok: this.ok,
308
+ nativeResponse: this[responseCache]
309
+ };
310
+ return `Response (lightweight) ${inspectFn(props, { ...options, depth: depth == null ? null : depth - 1 })}`;
311
+ }
312
+ });
289
313
  Object.setPrototypeOf(Response2, GlobalResponse);
290
314
  Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
291
315
 
@@ -366,6 +390,50 @@ if (typeof global.crypto === "undefined") {
366
390
 
367
391
  // src/listener.ts
368
392
  var outgoingEnded = Symbol("outgoingEnded");
393
+ var incomingDraining = Symbol("incomingDraining");
394
+ var DRAIN_TIMEOUT_MS = 500;
395
+ var MAX_DRAIN_BYTES = 64 * 1024 * 1024;
396
+ var drainIncoming = (incoming) => {
397
+ const incomingWithDrainState = incoming;
398
+ if (incoming.destroyed || incomingWithDrainState[incomingDraining]) {
399
+ return;
400
+ }
401
+ incomingWithDrainState[incomingDraining] = true;
402
+ if (incoming instanceof import_node_http22.Http2ServerRequest) {
403
+ try {
404
+ ;
405
+ incoming.stream?.close?.(import_node_http22.constants.NGHTTP2_NO_ERROR);
406
+ } catch {
407
+ }
408
+ return;
409
+ }
410
+ let bytesRead = 0;
411
+ const cleanup = () => {
412
+ clearTimeout(timer);
413
+ incoming.off("data", onData);
414
+ incoming.off("end", cleanup);
415
+ incoming.off("error", cleanup);
416
+ };
417
+ const forceClose = () => {
418
+ cleanup();
419
+ const socket = incoming.socket;
420
+ if (socket && !socket.destroyed) {
421
+ socket.destroySoon();
422
+ }
423
+ };
424
+ const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
425
+ timer.unref?.();
426
+ const onData = (chunk) => {
427
+ bytesRead += chunk.length;
428
+ if (bytesRead > MAX_DRAIN_BYTES) {
429
+ forceClose();
430
+ }
431
+ };
432
+ incoming.on("data", onData);
433
+ incoming.on("end", cleanup);
434
+ incoming.on("error", cleanup);
435
+ incoming.resume();
436
+ };
369
437
  var handleRequestError = () => new Response(null, {
370
438
  status: 400
371
439
  });
@@ -392,15 +460,32 @@ var flushHeaders = (outgoing) => {
392
460
  };
393
461
  var responseViaCache = async (res, outgoing) => {
394
462
  let [status, body, header] = res[cacheKey];
395
- if (header instanceof Headers) {
463
+ let hasContentLength = false;
464
+ if (!header) {
465
+ header = { "content-type": "text/plain; charset=UTF-8" };
466
+ } else if (header instanceof Headers) {
467
+ hasContentLength = header.has("content-length");
396
468
  header = buildOutgoingHttpHeaders(header);
469
+ } else if (Array.isArray(header)) {
470
+ const headerObj = new Headers(header);
471
+ hasContentLength = headerObj.has("content-length");
472
+ header = buildOutgoingHttpHeaders(headerObj);
473
+ } else {
474
+ for (const key in header) {
475
+ if (key.length === 14 && key.toLowerCase() === "content-length") {
476
+ hasContentLength = true;
477
+ break;
478
+ }
479
+ }
397
480
  }
398
- if (typeof body === "string") {
399
- header["Content-Length"] = Buffer.byteLength(body);
400
- } else if (body instanceof Uint8Array) {
401
- header["Content-Length"] = body.byteLength;
402
- } else if (body instanceof Blob) {
403
- header["Content-Length"] = body.size;
481
+ if (!hasContentLength) {
482
+ if (typeof body === "string") {
483
+ header["Content-Length"] = Buffer.byteLength(body);
484
+ } else if (body instanceof Uint8Array) {
485
+ header["Content-Length"] = body.byteLength;
486
+ } else if (body instanceof Blob) {
487
+ header["Content-Length"] = body.size;
488
+ }
404
489
  }
405
490
  outgoing.writeHead(status, header);
406
491
  if (typeof body === "string" || body instanceof Uint8Array) {
@@ -520,14 +605,18 @@ var getRequestListener = (fetchCallback, options = {}) => {
520
605
  setTimeout(() => {
521
606
  if (!incomingEnded) {
522
607
  setTimeout(() => {
523
- incoming.destroy();
524
- outgoing.destroy();
608
+ drainIncoming(incoming);
525
609
  });
526
610
  }
527
611
  });
528
612
  }
529
613
  };
530
614
  }
615
+ outgoing.on("finish", () => {
616
+ if (!incomingEnded) {
617
+ drainIncoming(incoming);
618
+ }
619
+ });
531
620
  }
532
621
  outgoing.on("close", () => {
533
622
  const abortController = req[abortControllerKey];
@@ -542,7 +631,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
542
631
  setTimeout(() => {
543
632
  if (!incomingEnded) {
544
633
  setTimeout(() => {
545
- incoming.destroy();
634
+ drainIncoming(incoming);
546
635
  });
547
636
  }
548
637
  });