@hono/node-server 1.10.1 → 1.11.1

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.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { createAdaptorServer, serve } from './server.mjs';
2
2
  export { getRequestListener } from './listener.mjs';
3
+ export { RequestError } from './request.mjs';
3
4
  export { Http2Bindings, HttpBindings } from './types.mjs';
4
5
  import 'node:net';
5
6
  import 'node:http';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { createAdaptorServer, serve } from './server.js';
2
2
  export { getRequestListener } from './listener.js';
3
+ export { RequestError } from './request.js';
3
4
  export { Http2Bindings, HttpBindings } from './types.js';
4
5
  import 'node:net';
5
6
  import 'node:http';
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
+ RequestError: () => RequestError,
33
34
  createAdaptorServer: () => createAdaptorServer,
34
35
  getRequestListener: () => getRequestListener,
35
36
  serve: () => serve
@@ -42,13 +43,25 @@ var import_node_http = require("http");
42
43
  // src/request.ts
43
44
  var import_node_http2 = require("http2");
44
45
  var import_node_stream = require("stream");
46
+ var RequestError = class extends Error {
47
+ static name = "RequestError";
48
+ constructor(message, options) {
49
+ super(message, options);
50
+ }
51
+ };
52
+ var toRequestError = (e) => {
53
+ if (e instanceof RequestError) {
54
+ return e;
55
+ }
56
+ return new RequestError(e.message, { cause: e });
57
+ };
45
58
  var GlobalRequest = global.Request;
46
59
  var Request = class extends GlobalRequest {
47
60
  constructor(input, options) {
48
61
  if (typeof input === "object" && getRequestCache in input) {
49
62
  input = input[getRequestCache]();
50
63
  }
51
- if (options?.body instanceof ReadableStream) {
64
+ if (typeof options?.body?.getReader !== "undefined") {
52
65
  ;
53
66
  options.duplex ??= "half";
54
67
  }
@@ -131,12 +144,20 @@ var requestPrototype = {
131
144
  });
132
145
  });
133
146
  Object.setPrototypeOf(requestPrototype, Request.prototype);
134
- var newRequest = (incoming) => {
147
+ var newRequest = (incoming, defaultHostname) => {
135
148
  const req = Object.create(requestPrototype);
136
149
  req[incomingKey] = incoming;
137
- req[urlKey] = new URL(
138
- `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
139
- ).href;
150
+ const host = (incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
151
+ if (!host) {
152
+ throw new RequestError("Missing host header");
153
+ }
154
+ const url = new URL(
155
+ `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
156
+ );
157
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
158
+ throw new RequestError("Invalid host header");
159
+ }
160
+ req[urlKey] = url.href;
140
161
  return req;
141
162
  };
142
163
 
@@ -223,7 +244,7 @@ var Response2 = class _Response {
223
244
  } else {
224
245
  this.#init = init;
225
246
  }
226
- if (typeof body === "string" || body instanceof ReadableStream) {
247
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
227
248
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
228
249
  if (headers instanceof Headers) {
229
250
  headers = buildOutgoingHttpHeaders(headers);
@@ -299,6 +320,9 @@ global.fetch = (info, init) => {
299
320
  // src/listener.ts
300
321
  var regBuffer = /^no$/i;
301
322
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
323
+ var handleRequestError = () => new Response(null, {
324
+ status: 400
325
+ });
302
326
  var handleFetchError = (e) => new Response(null, {
303
327
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
304
328
  });
@@ -395,9 +419,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
395
419
  });
396
420
  }
397
421
  return async (incoming, outgoing) => {
398
- let res;
422
+ let res, req;
399
423
  try {
400
- const req = newRequest(incoming);
424
+ req = newRequest(incoming, options.hostname);
401
425
  outgoing.on("close", () => {
402
426
  if (incoming.destroyed) {
403
427
  req[getAbortController]().abort();
@@ -410,10 +434,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
410
434
  } catch (e) {
411
435
  if (!res) {
412
436
  if (options.errorHandler) {
413
- res = await options.errorHandler(e);
437
+ res = await options.errorHandler(req ? e : toRequestError(e));
414
438
  if (!res) {
415
439
  return;
416
440
  }
441
+ } else if (!req) {
442
+ res = handleRequestError();
417
443
  } else {
418
444
  res = handleFetchError(e);
419
445
  }
@@ -433,6 +459,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
433
459
  var createAdaptorServer = (options) => {
434
460
  const fetchCallback = options.fetch;
435
461
  const requestListener = getRequestListener(fetchCallback, {
462
+ hostname: options.hostname,
436
463
  overrideGlobalObjects: options.overrideGlobalObjects
437
464
  });
438
465
  const createServer = options.createServer || import_node_http.createServer;
@@ -449,6 +476,7 @@ var serve = (options, listeningListener) => {
449
476
  };
450
477
  // Annotate the CommonJS export names for ESM import in node:
451
478
  0 && (module.exports = {
479
+ RequestError,
452
480
  createAdaptorServer,
453
481
  getRequestListener,
454
482
  serve
package/dist/index.mjs CHANGED
@@ -4,13 +4,25 @@ import { createServer as createServerHTTP } from "http";
4
4
  // src/request.ts
5
5
  import { Http2ServerRequest } from "http2";
6
6
  import { Readable } from "stream";
7
+ var RequestError = class extends Error {
8
+ static name = "RequestError";
9
+ constructor(message, options) {
10
+ super(message, options);
11
+ }
12
+ };
13
+ var toRequestError = (e) => {
14
+ if (e instanceof RequestError) {
15
+ return e;
16
+ }
17
+ return new RequestError(e.message, { cause: e });
18
+ };
7
19
  var GlobalRequest = global.Request;
8
20
  var Request = class extends GlobalRequest {
9
21
  constructor(input, options) {
10
22
  if (typeof input === "object" && getRequestCache in input) {
11
23
  input = input[getRequestCache]();
12
24
  }
13
- if (options?.body instanceof ReadableStream) {
25
+ if (typeof options?.body?.getReader !== "undefined") {
14
26
  ;
15
27
  options.duplex ??= "half";
16
28
  }
@@ -93,12 +105,20 @@ var requestPrototype = {
93
105
  });
94
106
  });
95
107
  Object.setPrototypeOf(requestPrototype, Request.prototype);
96
- var newRequest = (incoming) => {
108
+ var newRequest = (incoming, defaultHostname) => {
97
109
  const req = Object.create(requestPrototype);
98
110
  req[incomingKey] = incoming;
99
- req[urlKey] = new URL(
100
- `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
101
- ).href;
111
+ const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
112
+ if (!host) {
113
+ throw new RequestError("Missing host header");
114
+ }
115
+ const url = new URL(
116
+ `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
117
+ );
118
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
119
+ throw new RequestError("Invalid host header");
120
+ }
121
+ req[urlKey] = url.href;
102
122
  return req;
103
123
  };
104
124
 
@@ -185,7 +205,7 @@ var Response2 = class _Response {
185
205
  } else {
186
206
  this.#init = init;
187
207
  }
188
- if (typeof body === "string" || body instanceof ReadableStream) {
208
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
189
209
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
190
210
  if (headers instanceof Headers) {
191
211
  headers = buildOutgoingHttpHeaders(headers);
@@ -261,6 +281,9 @@ global.fetch = (info, init) => {
261
281
  // src/listener.ts
262
282
  var regBuffer = /^no$/i;
263
283
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
284
+ var handleRequestError = () => new Response(null, {
285
+ status: 400
286
+ });
264
287
  var handleFetchError = (e) => new Response(null, {
265
288
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
266
289
  });
@@ -357,9 +380,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
357
380
  });
358
381
  }
359
382
  return async (incoming, outgoing) => {
360
- let res;
383
+ let res, req;
361
384
  try {
362
- const req = newRequest(incoming);
385
+ req = newRequest(incoming, options.hostname);
363
386
  outgoing.on("close", () => {
364
387
  if (incoming.destroyed) {
365
388
  req[getAbortController]().abort();
@@ -372,10 +395,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
372
395
  } catch (e) {
373
396
  if (!res) {
374
397
  if (options.errorHandler) {
375
- res = await options.errorHandler(e);
398
+ res = await options.errorHandler(req ? e : toRequestError(e));
376
399
  if (!res) {
377
400
  return;
378
401
  }
402
+ } else if (!req) {
403
+ res = handleRequestError();
379
404
  } else {
380
405
  res = handleFetchError(e);
381
406
  }
@@ -395,6 +420,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
395
420
  var createAdaptorServer = (options) => {
396
421
  const fetchCallback = options.fetch;
397
422
  const requestListener = getRequestListener(fetchCallback, {
423
+ hostname: options.hostname,
398
424
  overrideGlobalObjects: options.overrideGlobalObjects
399
425
  });
400
426
  const createServer = options.createServer || createServerHTTP;
@@ -410,6 +436,7 @@ var serve = (options, listeningListener) => {
410
436
  return server;
411
437
  };
412
438
  export {
439
+ RequestError,
413
440
  createAdaptorServer,
414
441
  getRequestListener,
415
442
  serve
@@ -4,6 +4,7 @@ import { FetchCallback, CustomErrorHandler } from './types.mjs';
4
4
  import 'node:https';
5
5
 
6
6
  declare const getRequestListener: (fetchCallback: FetchCallback, options?: {
7
+ hostname?: string;
7
8
  errorHandler?: CustomErrorHandler;
8
9
  overrideGlobalObjects?: boolean;
9
10
  }) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
@@ -4,6 +4,7 @@ import { FetchCallback, CustomErrorHandler } from './types.js';
4
4
  import 'node:https';
5
5
 
6
6
  declare const getRequestListener: (fetchCallback: FetchCallback, options?: {
7
+ hostname?: string;
7
8
  errorHandler?: CustomErrorHandler;
8
9
  overrideGlobalObjects?: boolean;
9
10
  }) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
package/dist/listener.js CHANGED
@@ -37,13 +37,25 @@ module.exports = __toCommonJS(listener_exports);
37
37
  // src/request.ts
38
38
  var import_node_http2 = require("http2");
39
39
  var import_node_stream = require("stream");
40
+ var RequestError = class extends Error {
41
+ static name = "RequestError";
42
+ constructor(message, options) {
43
+ super(message, options);
44
+ }
45
+ };
46
+ var toRequestError = (e) => {
47
+ if (e instanceof RequestError) {
48
+ return e;
49
+ }
50
+ return new RequestError(e.message, { cause: e });
51
+ };
40
52
  var GlobalRequest = global.Request;
41
53
  var Request = class extends GlobalRequest {
42
54
  constructor(input, options) {
43
55
  if (typeof input === "object" && getRequestCache in input) {
44
56
  input = input[getRequestCache]();
45
57
  }
46
- if (options?.body instanceof ReadableStream) {
58
+ if (typeof options?.body?.getReader !== "undefined") {
47
59
  ;
48
60
  options.duplex ??= "half";
49
61
  }
@@ -126,12 +138,20 @@ var requestPrototype = {
126
138
  });
127
139
  });
128
140
  Object.setPrototypeOf(requestPrototype, Request.prototype);
129
- var newRequest = (incoming) => {
141
+ var newRequest = (incoming, defaultHostname) => {
130
142
  const req = Object.create(requestPrototype);
131
143
  req[incomingKey] = incoming;
132
- req[urlKey] = new URL(
133
- `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
134
- ).href;
144
+ const host = (incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
145
+ if (!host) {
146
+ throw new RequestError("Missing host header");
147
+ }
148
+ const url = new URL(
149
+ `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
150
+ );
151
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
152
+ throw new RequestError("Invalid host header");
153
+ }
154
+ req[urlKey] = url.href;
135
155
  return req;
136
156
  };
137
157
 
@@ -218,7 +238,7 @@ var Response2 = class _Response {
218
238
  } else {
219
239
  this.#init = init;
220
240
  }
221
- if (typeof body === "string" || body instanceof ReadableStream) {
241
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
222
242
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
223
243
  if (headers instanceof Headers) {
224
244
  headers = buildOutgoingHttpHeaders(headers);
@@ -294,6 +314,9 @@ global.fetch = (info, init) => {
294
314
  // src/listener.ts
295
315
  var regBuffer = /^no$/i;
296
316
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
317
+ var handleRequestError = () => new Response(null, {
318
+ status: 400
319
+ });
297
320
  var handleFetchError = (e) => new Response(null, {
298
321
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
299
322
  });
@@ -390,9 +413,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
390
413
  });
391
414
  }
392
415
  return async (incoming, outgoing) => {
393
- let res;
416
+ let res, req;
394
417
  try {
395
- const req = newRequest(incoming);
418
+ req = newRequest(incoming, options.hostname);
396
419
  outgoing.on("close", () => {
397
420
  if (incoming.destroyed) {
398
421
  req[getAbortController]().abort();
@@ -405,10 +428,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
405
428
  } catch (e) {
406
429
  if (!res) {
407
430
  if (options.errorHandler) {
408
- res = await options.errorHandler(e);
431
+ res = await options.errorHandler(req ? e : toRequestError(e));
409
432
  if (!res) {
410
433
  return;
411
434
  }
435
+ } else if (!req) {
436
+ res = handleRequestError();
412
437
  } else {
413
438
  res = handleFetchError(e);
414
439
  }
package/dist/listener.mjs CHANGED
@@ -1,13 +1,25 @@
1
1
  // src/request.ts
2
2
  import { Http2ServerRequest } from "http2";
3
3
  import { Readable } from "stream";
4
+ var RequestError = class extends Error {
5
+ static name = "RequestError";
6
+ constructor(message, options) {
7
+ super(message, options);
8
+ }
9
+ };
10
+ var toRequestError = (e) => {
11
+ if (e instanceof RequestError) {
12
+ return e;
13
+ }
14
+ return new RequestError(e.message, { cause: e });
15
+ };
4
16
  var GlobalRequest = global.Request;
5
17
  var Request = class extends GlobalRequest {
6
18
  constructor(input, options) {
7
19
  if (typeof input === "object" && getRequestCache in input) {
8
20
  input = input[getRequestCache]();
9
21
  }
10
- if (options?.body instanceof ReadableStream) {
22
+ if (typeof options?.body?.getReader !== "undefined") {
11
23
  ;
12
24
  options.duplex ??= "half";
13
25
  }
@@ -90,12 +102,20 @@ var requestPrototype = {
90
102
  });
91
103
  });
92
104
  Object.setPrototypeOf(requestPrototype, Request.prototype);
93
- var newRequest = (incoming) => {
105
+ var newRequest = (incoming, defaultHostname) => {
94
106
  const req = Object.create(requestPrototype);
95
107
  req[incomingKey] = incoming;
96
- req[urlKey] = new URL(
97
- `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
98
- ).href;
108
+ const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
109
+ if (!host) {
110
+ throw new RequestError("Missing host header");
111
+ }
112
+ const url = new URL(
113
+ `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
114
+ );
115
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
116
+ throw new RequestError("Invalid host header");
117
+ }
118
+ req[urlKey] = url.href;
99
119
  return req;
100
120
  };
101
121
 
@@ -182,7 +202,7 @@ var Response2 = class _Response {
182
202
  } else {
183
203
  this.#init = init;
184
204
  }
185
- if (typeof body === "string" || body instanceof ReadableStream) {
205
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
186
206
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
187
207
  if (headers instanceof Headers) {
188
208
  headers = buildOutgoingHttpHeaders(headers);
@@ -258,6 +278,9 @@ global.fetch = (info, init) => {
258
278
  // src/listener.ts
259
279
  var regBuffer = /^no$/i;
260
280
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
281
+ var handleRequestError = () => new Response(null, {
282
+ status: 400
283
+ });
261
284
  var handleFetchError = (e) => new Response(null, {
262
285
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
263
286
  });
@@ -354,9 +377,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
354
377
  });
355
378
  }
356
379
  return async (incoming, outgoing) => {
357
- let res;
380
+ let res, req;
358
381
  try {
359
- const req = newRequest(incoming);
382
+ req = newRequest(incoming, options.hostname);
360
383
  outgoing.on("close", () => {
361
384
  if (incoming.destroyed) {
362
385
  req[getAbortController]().abort();
@@ -369,10 +392,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
369
392
  } catch (e) {
370
393
  if (!res) {
371
394
  if (options.errorHandler) {
372
- res = await options.errorHandler(e);
395
+ res = await options.errorHandler(req ? e : toRequestError(e));
373
396
  if (!res) {
374
397
  return;
375
398
  }
399
+ } else if (!req) {
400
+ res = handleRequestError();
376
401
  } else {
377
402
  res = handleFetchError(e);
378
403
  }
@@ -1,6 +1,13 @@
1
1
  import { IncomingMessage } from 'node:http';
2
2
  import { Http2ServerRequest } from 'node:http2';
3
3
 
4
+ declare class RequestError extends Error {
5
+ static name: string;
6
+ constructor(message: string, options?: {
7
+ cause?: unknown;
8
+ });
9
+ }
10
+ declare const toRequestError: (e: unknown) => RequestError;
4
11
  declare const GlobalRequest: {
5
12
  new (input: RequestInfo | URL, init?: RequestInit | undefined): globalThis.Request;
6
13
  prototype: globalThis.Request;
@@ -9,6 +16,6 @@ declare class Request extends GlobalRequest {
9
16
  constructor(input: string | Request, options?: RequestInit);
10
17
  }
11
18
  declare const getAbortController: unique symbol;
12
- declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest) => any;
19
+ declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest, defaultHostname?: string) => any;
13
20
 
14
- export { GlobalRequest, Request, getAbortController, newRequest };
21
+ export { GlobalRequest, Request, RequestError, getAbortController, newRequest, toRequestError };
package/dist/request.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import { IncomingMessage } from 'node:http';
2
2
  import { Http2ServerRequest } from 'node:http2';
3
3
 
4
+ declare class RequestError extends Error {
5
+ static name: string;
6
+ constructor(message: string, options?: {
7
+ cause?: unknown;
8
+ });
9
+ }
10
+ declare const toRequestError: (e: unknown) => RequestError;
4
11
  declare const GlobalRequest: {
5
12
  new (input: RequestInfo | URL, init?: RequestInit | undefined): globalThis.Request;
6
13
  prototype: globalThis.Request;
@@ -9,6 +16,6 @@ declare class Request extends GlobalRequest {
9
16
  constructor(input: string | Request, options?: RequestInit);
10
17
  }
11
18
  declare const getAbortController: unique symbol;
12
- declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest) => any;
19
+ declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest, defaultHostname?: string) => any;
13
20
 
14
- export { GlobalRequest, Request, getAbortController, newRequest };
21
+ export { GlobalRequest, Request, RequestError, getAbortController, newRequest, toRequestError };
package/dist/request.js CHANGED
@@ -22,19 +22,33 @@ var request_exports = {};
22
22
  __export(request_exports, {
23
23
  GlobalRequest: () => GlobalRequest,
24
24
  Request: () => Request,
25
+ RequestError: () => RequestError,
25
26
  getAbortController: () => getAbortController,
26
- newRequest: () => newRequest
27
+ newRequest: () => newRequest,
28
+ toRequestError: () => toRequestError
27
29
  });
28
30
  module.exports = __toCommonJS(request_exports);
29
31
  var import_node_http2 = require("http2");
30
32
  var import_node_stream = require("stream");
33
+ var RequestError = class extends Error {
34
+ static name = "RequestError";
35
+ constructor(message, options) {
36
+ super(message, options);
37
+ }
38
+ };
39
+ var toRequestError = (e) => {
40
+ if (e instanceof RequestError) {
41
+ return e;
42
+ }
43
+ return new RequestError(e.message, { cause: e });
44
+ };
31
45
  var GlobalRequest = global.Request;
32
46
  var Request = class extends GlobalRequest {
33
47
  constructor(input, options) {
34
48
  if (typeof input === "object" && getRequestCache in input) {
35
49
  input = input[getRequestCache]();
36
50
  }
37
- if (options?.body instanceof ReadableStream) {
51
+ if (typeof options?.body?.getReader !== "undefined") {
38
52
  ;
39
53
  options.duplex ??= "half";
40
54
  }
@@ -117,18 +131,28 @@ var requestPrototype = {
117
131
  });
118
132
  });
119
133
  Object.setPrototypeOf(requestPrototype, Request.prototype);
120
- var newRequest = (incoming) => {
134
+ var newRequest = (incoming, defaultHostname) => {
121
135
  const req = Object.create(requestPrototype);
122
136
  req[incomingKey] = incoming;
123
- req[urlKey] = new URL(
124
- `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
125
- ).href;
137
+ const host = (incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
138
+ if (!host) {
139
+ throw new RequestError("Missing host header");
140
+ }
141
+ const url = new URL(
142
+ `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
143
+ );
144
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
145
+ throw new RequestError("Invalid host header");
146
+ }
147
+ req[urlKey] = url.href;
126
148
  return req;
127
149
  };
128
150
  // Annotate the CommonJS export names for ESM import in node:
129
151
  0 && (module.exports = {
130
152
  GlobalRequest,
131
153
  Request,
154
+ RequestError,
132
155
  getAbortController,
133
- newRequest
156
+ newRequest,
157
+ toRequestError
134
158
  });
package/dist/request.mjs CHANGED
@@ -1,13 +1,25 @@
1
1
  // src/request.ts
2
2
  import { Http2ServerRequest } from "http2";
3
3
  import { Readable } from "stream";
4
+ var RequestError = class extends Error {
5
+ static name = "RequestError";
6
+ constructor(message, options) {
7
+ super(message, options);
8
+ }
9
+ };
10
+ var toRequestError = (e) => {
11
+ if (e instanceof RequestError) {
12
+ return e;
13
+ }
14
+ return new RequestError(e.message, { cause: e });
15
+ };
4
16
  var GlobalRequest = global.Request;
5
17
  var Request = class extends GlobalRequest {
6
18
  constructor(input, options) {
7
19
  if (typeof input === "object" && getRequestCache in input) {
8
20
  input = input[getRequestCache]();
9
21
  }
10
- if (options?.body instanceof ReadableStream) {
22
+ if (typeof options?.body?.getReader !== "undefined") {
11
23
  ;
12
24
  options.duplex ??= "half";
13
25
  }
@@ -90,17 +102,27 @@ var requestPrototype = {
90
102
  });
91
103
  });
92
104
  Object.setPrototypeOf(requestPrototype, Request.prototype);
93
- var newRequest = (incoming) => {
105
+ var newRequest = (incoming, defaultHostname) => {
94
106
  const req = Object.create(requestPrototype);
95
107
  req[incomingKey] = incoming;
96
- req[urlKey] = new URL(
97
- `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
98
- ).href;
108
+ const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
109
+ if (!host) {
110
+ throw new RequestError("Missing host header");
111
+ }
112
+ const url = new URL(
113
+ `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
114
+ );
115
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
116
+ throw new RequestError("Invalid host header");
117
+ }
118
+ req[urlKey] = url.href;
99
119
  return req;
100
120
  };
101
121
  export {
102
122
  GlobalRequest,
103
123
  Request,
124
+ RequestError,
104
125
  getAbortController,
105
- newRequest
126
+ newRequest,
127
+ toRequestError
106
128
  };
package/dist/response.js CHANGED
@@ -71,7 +71,7 @@ var Response = class _Response {
71
71
  } else {
72
72
  this.#init = init;
73
73
  }
74
- if (typeof body === "string" || body instanceof ReadableStream) {
74
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
75
75
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
76
76
  if (headers instanceof Headers) {
77
77
  headers = buildOutgoingHttpHeaders(headers);
package/dist/response.mjs CHANGED
@@ -42,7 +42,7 @@ var Response = class _Response {
42
42
  } else {
43
43
  this.#init = init;
44
44
  }
45
- if (typeof body === "string" || body instanceof ReadableStream) {
45
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
46
46
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
47
47
  if (headers instanceof Headers) {
48
48
  headers = buildOutgoingHttpHeaders(headers);
@@ -139,8 +139,7 @@ var serveStatic = (options = { root: "" }) => {
139
139
  if (c.finalized) {
140
140
  return next();
141
141
  }
142
- const url = new URL(c.req.url);
143
- const filename = options.path ?? decodeURIComponent(url.pathname);
142
+ const filename = options.path ?? decodeURIComponent(c.req.path);
144
143
  let path = getFilePath({
145
144
  filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
146
145
  root: options.root,
@@ -115,8 +115,7 @@ var serveStatic = (options = { root: "" }) => {
115
115
  if (c.finalized) {
116
116
  return next();
117
117
  }
118
- const url = new URL(c.req.url);
119
- const filename = options.path ?? decodeURIComponent(url.pathname);
118
+ const filename = options.path ?? decodeURIComponent(c.req.path);
120
119
  let path = getFilePath({
121
120
  filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
122
121
  root: options.root,
package/dist/server.js CHANGED
@@ -39,13 +39,25 @@ var import_node_http = require("http");
39
39
  // src/request.ts
40
40
  var import_node_http2 = require("http2");
41
41
  var import_node_stream = require("stream");
42
+ var RequestError = class extends Error {
43
+ static name = "RequestError";
44
+ constructor(message, options) {
45
+ super(message, options);
46
+ }
47
+ };
48
+ var toRequestError = (e) => {
49
+ if (e instanceof RequestError) {
50
+ return e;
51
+ }
52
+ return new RequestError(e.message, { cause: e });
53
+ };
42
54
  var GlobalRequest = global.Request;
43
55
  var Request = class extends GlobalRequest {
44
56
  constructor(input, options) {
45
57
  if (typeof input === "object" && getRequestCache in input) {
46
58
  input = input[getRequestCache]();
47
59
  }
48
- if (options?.body instanceof ReadableStream) {
60
+ if (typeof options?.body?.getReader !== "undefined") {
49
61
  ;
50
62
  options.duplex ??= "half";
51
63
  }
@@ -128,12 +140,20 @@ var requestPrototype = {
128
140
  });
129
141
  });
130
142
  Object.setPrototypeOf(requestPrototype, Request.prototype);
131
- var newRequest = (incoming) => {
143
+ var newRequest = (incoming, defaultHostname) => {
132
144
  const req = Object.create(requestPrototype);
133
145
  req[incomingKey] = incoming;
134
- req[urlKey] = new URL(
135
- `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
136
- ).href;
146
+ const host = (incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
147
+ if (!host) {
148
+ throw new RequestError("Missing host header");
149
+ }
150
+ const url = new URL(
151
+ `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
152
+ );
153
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
154
+ throw new RequestError("Invalid host header");
155
+ }
156
+ req[urlKey] = url.href;
137
157
  return req;
138
158
  };
139
159
 
@@ -220,7 +240,7 @@ var Response2 = class _Response {
220
240
  } else {
221
241
  this.#init = init;
222
242
  }
223
- if (typeof body === "string" || body instanceof ReadableStream) {
243
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
224
244
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
225
245
  if (headers instanceof Headers) {
226
246
  headers = buildOutgoingHttpHeaders(headers);
@@ -296,6 +316,9 @@ global.fetch = (info, init) => {
296
316
  // src/listener.ts
297
317
  var regBuffer = /^no$/i;
298
318
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
319
+ var handleRequestError = () => new Response(null, {
320
+ status: 400
321
+ });
299
322
  var handleFetchError = (e) => new Response(null, {
300
323
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
301
324
  });
@@ -392,9 +415,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
392
415
  });
393
416
  }
394
417
  return async (incoming, outgoing) => {
395
- let res;
418
+ let res, req;
396
419
  try {
397
- const req = newRequest(incoming);
420
+ req = newRequest(incoming, options.hostname);
398
421
  outgoing.on("close", () => {
399
422
  if (incoming.destroyed) {
400
423
  req[getAbortController]().abort();
@@ -407,10 +430,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
407
430
  } catch (e) {
408
431
  if (!res) {
409
432
  if (options.errorHandler) {
410
- res = await options.errorHandler(e);
433
+ res = await options.errorHandler(req ? e : toRequestError(e));
411
434
  if (!res) {
412
435
  return;
413
436
  }
437
+ } else if (!req) {
438
+ res = handleRequestError();
414
439
  } else {
415
440
  res = handleFetchError(e);
416
441
  }
@@ -430,6 +455,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
430
455
  var createAdaptorServer = (options) => {
431
456
  const fetchCallback = options.fetch;
432
457
  const requestListener = getRequestListener(fetchCallback, {
458
+ hostname: options.hostname,
433
459
  overrideGlobalObjects: options.overrideGlobalObjects
434
460
  });
435
461
  const createServer = options.createServer || import_node_http.createServer;
package/dist/server.mjs CHANGED
@@ -4,13 +4,25 @@ import { createServer as createServerHTTP } from "http";
4
4
  // src/request.ts
5
5
  import { Http2ServerRequest } from "http2";
6
6
  import { Readable } from "stream";
7
+ var RequestError = class extends Error {
8
+ static name = "RequestError";
9
+ constructor(message, options) {
10
+ super(message, options);
11
+ }
12
+ };
13
+ var toRequestError = (e) => {
14
+ if (e instanceof RequestError) {
15
+ return e;
16
+ }
17
+ return new RequestError(e.message, { cause: e });
18
+ };
7
19
  var GlobalRequest = global.Request;
8
20
  var Request = class extends GlobalRequest {
9
21
  constructor(input, options) {
10
22
  if (typeof input === "object" && getRequestCache in input) {
11
23
  input = input[getRequestCache]();
12
24
  }
13
- if (options?.body instanceof ReadableStream) {
25
+ if (typeof options?.body?.getReader !== "undefined") {
14
26
  ;
15
27
  options.duplex ??= "half";
16
28
  }
@@ -93,12 +105,20 @@ var requestPrototype = {
93
105
  });
94
106
  });
95
107
  Object.setPrototypeOf(requestPrototype, Request.prototype);
96
- var newRequest = (incoming) => {
108
+ var newRequest = (incoming, defaultHostname) => {
97
109
  const req = Object.create(requestPrototype);
98
110
  req[incomingKey] = incoming;
99
- req[urlKey] = new URL(
100
- `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
101
- ).href;
111
+ const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
112
+ if (!host) {
113
+ throw new RequestError("Missing host header");
114
+ }
115
+ const url = new URL(
116
+ `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
117
+ );
118
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
119
+ throw new RequestError("Invalid host header");
120
+ }
121
+ req[urlKey] = url.href;
102
122
  return req;
103
123
  };
104
124
 
@@ -185,7 +205,7 @@ var Response2 = class _Response {
185
205
  } else {
186
206
  this.#init = init;
187
207
  }
188
- if (typeof body === "string" || body instanceof ReadableStream) {
208
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
189
209
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
190
210
  if (headers instanceof Headers) {
191
211
  headers = buildOutgoingHttpHeaders(headers);
@@ -261,6 +281,9 @@ global.fetch = (info, init) => {
261
281
  // src/listener.ts
262
282
  var regBuffer = /^no$/i;
263
283
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
284
+ var handleRequestError = () => new Response(null, {
285
+ status: 400
286
+ });
264
287
  var handleFetchError = (e) => new Response(null, {
265
288
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
266
289
  });
@@ -357,9 +380,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
357
380
  });
358
381
  }
359
382
  return async (incoming, outgoing) => {
360
- let res;
383
+ let res, req;
361
384
  try {
362
- const req = newRequest(incoming);
385
+ req = newRequest(incoming, options.hostname);
363
386
  outgoing.on("close", () => {
364
387
  if (incoming.destroyed) {
365
388
  req[getAbortController]().abort();
@@ -372,10 +395,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
372
395
  } catch (e) {
373
396
  if (!res) {
374
397
  if (options.errorHandler) {
375
- res = await options.errorHandler(e);
398
+ res = await options.errorHandler(req ? e : toRequestError(e));
376
399
  if (!res) {
377
400
  return;
378
401
  }
402
+ } else if (!req) {
403
+ res = handleRequestError();
379
404
  } else {
380
405
  res = handleFetchError(e);
381
406
  }
@@ -395,6 +420,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
395
420
  var createAdaptorServer = (options) => {
396
421
  const fetchCallback = options.fetch;
397
422
  const requestListener = getRequestListener(fetchCallback, {
423
+ hostname: options.hostname,
398
424
  overrideGlobalObjects: options.overrideGlobalObjects
399
425
  });
400
426
  const createServer = options.createServer || createServerHTTP;
package/dist/vercel.js CHANGED
@@ -37,13 +37,25 @@ module.exports = __toCommonJS(vercel_exports);
37
37
  // src/request.ts
38
38
  var import_node_http2 = require("http2");
39
39
  var import_node_stream = require("stream");
40
+ var RequestError = class extends Error {
41
+ static name = "RequestError";
42
+ constructor(message, options) {
43
+ super(message, options);
44
+ }
45
+ };
46
+ var toRequestError = (e) => {
47
+ if (e instanceof RequestError) {
48
+ return e;
49
+ }
50
+ return new RequestError(e.message, { cause: e });
51
+ };
40
52
  var GlobalRequest = global.Request;
41
53
  var Request = class extends GlobalRequest {
42
54
  constructor(input, options) {
43
55
  if (typeof input === "object" && getRequestCache in input) {
44
56
  input = input[getRequestCache]();
45
57
  }
46
- if (options?.body instanceof ReadableStream) {
58
+ if (typeof options?.body?.getReader !== "undefined") {
47
59
  ;
48
60
  options.duplex ??= "half";
49
61
  }
@@ -126,12 +138,20 @@ var requestPrototype = {
126
138
  });
127
139
  });
128
140
  Object.setPrototypeOf(requestPrototype, Request.prototype);
129
- var newRequest = (incoming) => {
141
+ var newRequest = (incoming, defaultHostname) => {
130
142
  const req = Object.create(requestPrototype);
131
143
  req[incomingKey] = incoming;
132
- req[urlKey] = new URL(
133
- `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
134
- ).href;
144
+ const host = (incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
145
+ if (!host) {
146
+ throw new RequestError("Missing host header");
147
+ }
148
+ const url = new URL(
149
+ `${incoming instanceof import_node_http2.Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
150
+ );
151
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
152
+ throw new RequestError("Invalid host header");
153
+ }
154
+ req[urlKey] = url.href;
135
155
  return req;
136
156
  };
137
157
 
@@ -218,7 +238,7 @@ var Response2 = class _Response {
218
238
  } else {
219
239
  this.#init = init;
220
240
  }
221
- if (typeof body === "string" || body instanceof ReadableStream) {
241
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
222
242
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
223
243
  if (headers instanceof Headers) {
224
244
  headers = buildOutgoingHttpHeaders(headers);
@@ -294,6 +314,9 @@ global.fetch = (info, init) => {
294
314
  // src/listener.ts
295
315
  var regBuffer = /^no$/i;
296
316
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
317
+ var handleRequestError = () => new Response(null, {
318
+ status: 400
319
+ });
297
320
  var handleFetchError = (e) => new Response(null, {
298
321
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
299
322
  });
@@ -390,9 +413,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
390
413
  });
391
414
  }
392
415
  return async (incoming, outgoing) => {
393
- let res;
416
+ let res, req;
394
417
  try {
395
- const req = newRequest(incoming);
418
+ req = newRequest(incoming, options.hostname);
396
419
  outgoing.on("close", () => {
397
420
  if (incoming.destroyed) {
398
421
  req[getAbortController]().abort();
@@ -405,10 +428,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
405
428
  } catch (e) {
406
429
  if (!res) {
407
430
  if (options.errorHandler) {
408
- res = await options.errorHandler(e);
431
+ res = await options.errorHandler(req ? e : toRequestError(e));
409
432
  if (!res) {
410
433
  return;
411
434
  }
435
+ } else if (!req) {
436
+ res = handleRequestError();
412
437
  } else {
413
438
  res = handleFetchError(e);
414
439
  }
package/dist/vercel.mjs CHANGED
@@ -1,13 +1,25 @@
1
1
  // src/request.ts
2
2
  import { Http2ServerRequest } from "http2";
3
3
  import { Readable } from "stream";
4
+ var RequestError = class extends Error {
5
+ static name = "RequestError";
6
+ constructor(message, options) {
7
+ super(message, options);
8
+ }
9
+ };
10
+ var toRequestError = (e) => {
11
+ if (e instanceof RequestError) {
12
+ return e;
13
+ }
14
+ return new RequestError(e.message, { cause: e });
15
+ };
4
16
  var GlobalRequest = global.Request;
5
17
  var Request = class extends GlobalRequest {
6
18
  constructor(input, options) {
7
19
  if (typeof input === "object" && getRequestCache in input) {
8
20
  input = input[getRequestCache]();
9
21
  }
10
- if (options?.body instanceof ReadableStream) {
22
+ if (typeof options?.body?.getReader !== "undefined") {
11
23
  ;
12
24
  options.duplex ??= "half";
13
25
  }
@@ -90,12 +102,20 @@ var requestPrototype = {
90
102
  });
91
103
  });
92
104
  Object.setPrototypeOf(requestPrototype, Request.prototype);
93
- var newRequest = (incoming) => {
105
+ var newRequest = (incoming, defaultHostname) => {
94
106
  const req = Object.create(requestPrototype);
95
107
  req[incomingKey] = incoming;
96
- req[urlKey] = new URL(
97
- `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
98
- ).href;
108
+ const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
109
+ if (!host) {
110
+ throw new RequestError("Missing host header");
111
+ }
112
+ const url = new URL(
113
+ `${incoming instanceof Http2ServerRequest || incoming.socket && incoming.socket.encrypted ? "https" : "http"}://${host}${incoming.url}`
114
+ );
115
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
116
+ throw new RequestError("Invalid host header");
117
+ }
118
+ req[urlKey] = url.href;
99
119
  return req;
100
120
  };
101
121
 
@@ -182,7 +202,7 @@ var Response2 = class _Response {
182
202
  } else {
183
203
  this.#init = init;
184
204
  }
185
- if (typeof body === "string" || body instanceof ReadableStream) {
205
+ if (typeof body === "string" || typeof body?.getReader !== "undefined") {
186
206
  let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
187
207
  if (headers instanceof Headers) {
188
208
  headers = buildOutgoingHttpHeaders(headers);
@@ -258,6 +278,9 @@ global.fetch = (info, init) => {
258
278
  // src/listener.ts
259
279
  var regBuffer = /^no$/i;
260
280
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
281
+ var handleRequestError = () => new Response(null, {
282
+ status: 400
283
+ });
261
284
  var handleFetchError = (e) => new Response(null, {
262
285
  status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
263
286
  });
@@ -354,9 +377,9 @@ var getRequestListener = (fetchCallback, options = {}) => {
354
377
  });
355
378
  }
356
379
  return async (incoming, outgoing) => {
357
- let res;
380
+ let res, req;
358
381
  try {
359
- const req = newRequest(incoming);
382
+ req = newRequest(incoming, options.hostname);
360
383
  outgoing.on("close", () => {
361
384
  if (incoming.destroyed) {
362
385
  req[getAbortController]().abort();
@@ -369,10 +392,12 @@ var getRequestListener = (fetchCallback, options = {}) => {
369
392
  } catch (e) {
370
393
  if (!res) {
371
394
  if (options.errorHandler) {
372
- res = await options.errorHandler(e);
395
+ res = await options.errorHandler(req ? e : toRequestError(e));
373
396
  if (!res) {
374
397
  return;
375
398
  }
399
+ } else if (!req) {
400
+ res = handleRequestError();
376
401
  } else {
377
402
  res = handleFetchError(e);
378
403
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/node-server",
3
- "version": "1.10.1",
3
+ "version": "1.11.1",
4
4
  "description": "Node.js Adapter for Hono",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",