@hono/node-server 1.10.0 → 1.11.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/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,6 +43,18 @@ 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) {
@@ -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
 
@@ -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,14 +419,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
395
419
  });
396
420
  }
397
421
  return async (incoming, outgoing) => {
398
- let res;
399
- const req = newRequest(incoming);
400
- outgoing.on("close", () => {
401
- if (incoming.destroyed) {
402
- req[getAbortController]().abort();
403
- }
404
- });
422
+ let res, req;
405
423
  try {
424
+ req = newRequest(incoming, options.hostname);
425
+ outgoing.on("close", () => {
426
+ if (incoming.destroyed) {
427
+ req[getAbortController]().abort();
428
+ }
429
+ });
406
430
  res = fetchCallback(req, { incoming, outgoing });
407
431
  if (cacheKey in res) {
408
432
  return responseViaCache(res, outgoing);
@@ -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,6 +4,18 @@ 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) {
@@ -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
 
@@ -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,14 +380,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
357
380
  });
358
381
  }
359
382
  return async (incoming, outgoing) => {
360
- let res;
361
- const req = newRequest(incoming);
362
- outgoing.on("close", () => {
363
- if (incoming.destroyed) {
364
- req[getAbortController]().abort();
365
- }
366
- });
383
+ let res, req;
367
384
  try {
385
+ req = newRequest(incoming, options.hostname);
386
+ outgoing.on("close", () => {
387
+ if (incoming.destroyed) {
388
+ req[getAbortController]().abort();
389
+ }
390
+ });
368
391
  res = fetchCallback(req, { incoming, outgoing });
369
392
  if (cacheKey in res) {
370
393
  return responseViaCache(res, outgoing);
@@ -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,6 +37,18 @@ 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) {
@@ -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
 
@@ -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,14 +413,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
390
413
  });
391
414
  }
392
415
  return async (incoming, outgoing) => {
393
- let res;
394
- const req = newRequest(incoming);
395
- outgoing.on("close", () => {
396
- if (incoming.destroyed) {
397
- req[getAbortController]().abort();
398
- }
399
- });
416
+ let res, req;
400
417
  try {
418
+ req = newRequest(incoming, options.hostname);
419
+ outgoing.on("close", () => {
420
+ if (incoming.destroyed) {
421
+ req[getAbortController]().abort();
422
+ }
423
+ });
401
424
  res = fetchCallback(req, { incoming, outgoing });
402
425
  if (cacheKey in res) {
403
426
  return responseViaCache(res, outgoing);
@@ -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,6 +1,18 @@
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) {
@@ -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
 
@@ -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,14 +377,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
354
377
  });
355
378
  }
356
379
  return async (incoming, outgoing) => {
357
- let res;
358
- const req = newRequest(incoming);
359
- outgoing.on("close", () => {
360
- if (incoming.destroyed) {
361
- req[getAbortController]().abort();
362
- }
363
- });
380
+ let res, req;
364
381
  try {
382
+ req = newRequest(incoming, options.hostname);
383
+ outgoing.on("close", () => {
384
+ if (incoming.destroyed) {
385
+ req[getAbortController]().abort();
386
+ }
387
+ });
365
388
  res = fetchCallback(req, { incoming, outgoing });
366
389
  if (cacheKey in res) {
367
390
  return responseViaCache(res, outgoing);
@@ -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,12 +22,26 @@ 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) {
@@ -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,6 +1,18 @@
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) {
@@ -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/server.js CHANGED
@@ -39,6 +39,18 @@ 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) {
@@ -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
 
@@ -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,14 +415,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
392
415
  });
393
416
  }
394
417
  return async (incoming, outgoing) => {
395
- let res;
396
- const req = newRequest(incoming);
397
- outgoing.on("close", () => {
398
- if (incoming.destroyed) {
399
- req[getAbortController]().abort();
400
- }
401
- });
418
+ let res, req;
402
419
  try {
420
+ req = newRequest(incoming, options.hostname);
421
+ outgoing.on("close", () => {
422
+ if (incoming.destroyed) {
423
+ req[getAbortController]().abort();
424
+ }
425
+ });
403
426
  res = fetchCallback(req, { incoming, outgoing });
404
427
  if (cacheKey in res) {
405
428
  return responseViaCache(res, outgoing);
@@ -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,6 +4,18 @@ 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) {
@@ -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
 
@@ -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,14 +380,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
357
380
  });
358
381
  }
359
382
  return async (incoming, outgoing) => {
360
- let res;
361
- const req = newRequest(incoming);
362
- outgoing.on("close", () => {
363
- if (incoming.destroyed) {
364
- req[getAbortController]().abort();
365
- }
366
- });
383
+ let res, req;
367
384
  try {
385
+ req = newRequest(incoming, options.hostname);
386
+ outgoing.on("close", () => {
387
+ if (incoming.destroyed) {
388
+ req[getAbortController]().abort();
389
+ }
390
+ });
368
391
  res = fetchCallback(req, { incoming, outgoing });
369
392
  if (cacheKey in res) {
370
393
  return responseViaCache(res, outgoing);
@@ -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,6 +37,18 @@ 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) {
@@ -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
 
@@ -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,14 +413,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
390
413
  });
391
414
  }
392
415
  return async (incoming, outgoing) => {
393
- let res;
394
- const req = newRequest(incoming);
395
- outgoing.on("close", () => {
396
- if (incoming.destroyed) {
397
- req[getAbortController]().abort();
398
- }
399
- });
416
+ let res, req;
400
417
  try {
418
+ req = newRequest(incoming, options.hostname);
419
+ outgoing.on("close", () => {
420
+ if (incoming.destroyed) {
421
+ req[getAbortController]().abort();
422
+ }
423
+ });
401
424
  res = fetchCallback(req, { incoming, outgoing });
402
425
  if (cacheKey in res) {
403
426
  return responseViaCache(res, outgoing);
@@ -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,6 +1,18 @@
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) {
@@ -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
 
@@ -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,14 +377,14 @@ var getRequestListener = (fetchCallback, options = {}) => {
354
377
  });
355
378
  }
356
379
  return async (incoming, outgoing) => {
357
- let res;
358
- const req = newRequest(incoming);
359
- outgoing.on("close", () => {
360
- if (incoming.destroyed) {
361
- req[getAbortController]().abort();
362
- }
363
- });
380
+ let res, req;
364
381
  try {
382
+ req = newRequest(incoming, options.hostname);
383
+ outgoing.on("close", () => {
384
+ if (incoming.destroyed) {
385
+ req[getAbortController]().abort();
386
+ }
387
+ });
365
388
  res = fetchCallback(req, { incoming, outgoing });
366
389
  if (cacheKey in res) {
367
390
  return responseViaCache(res, outgoing);
@@ -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.0",
3
+ "version": "1.11.0",
4
4
  "description": "Node.js Adapter for Hono",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",