@hono/node-server 1.3.2 → 1.3.4

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/listener.js CHANGED
@@ -34,8 +34,71 @@ __export(listener_exports, {
34
34
  });
35
35
  module.exports = __toCommonJS(listener_exports);
36
36
 
37
- // src/globals.ts
38
- var import_node_crypto = __toESM(require("crypto"));
37
+ // src/request.ts
38
+ var import_node_stream = require("stream");
39
+ var newRequestFromIncoming = (method, url, incoming) => {
40
+ const headerRecord = [];
41
+ const len = incoming.rawHeaders.length;
42
+ for (let i = 0; i < len; i += 2) {
43
+ headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
44
+ }
45
+ const init = {
46
+ method,
47
+ headers: headerRecord
48
+ };
49
+ if (!(method === "GET" || method === "HEAD")) {
50
+ init.body = import_node_stream.Readable.toWeb(incoming);
51
+ init.duplex = "half";
52
+ }
53
+ return new Request(url, init);
54
+ };
55
+ var getRequestCache = Symbol("getRequestCache");
56
+ var requestCache = Symbol("requestCache");
57
+ var incomingKey = Symbol("incomingKey");
58
+ var requestPrototype = {
59
+ get method() {
60
+ return this[incomingKey].method || "GET";
61
+ },
62
+ get url() {
63
+ return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
64
+ },
65
+ [getRequestCache]() {
66
+ return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
67
+ }
68
+ };
69
+ [
70
+ "body",
71
+ "bodyUsed",
72
+ "cache",
73
+ "credentials",
74
+ "destination",
75
+ "headers",
76
+ "integrity",
77
+ "mode",
78
+ "redirect",
79
+ "referrer",
80
+ "referrerPolicy",
81
+ "signal"
82
+ ].forEach((k) => {
83
+ Object.defineProperty(requestPrototype, k, {
84
+ get() {
85
+ return this[getRequestCache]()[k];
86
+ }
87
+ });
88
+ });
89
+ ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
90
+ Object.defineProperty(requestPrototype, k, {
91
+ value: function() {
92
+ return this[getRequestCache]()[k]();
93
+ }
94
+ });
95
+ });
96
+ Object.setPrototypeOf(requestPrototype, global.Request.prototype);
97
+ var newRequest = (incoming) => {
98
+ const req = Object.create(requestPrototype);
99
+ req[incomingKey] = incoming;
100
+ return req;
101
+ };
39
102
 
40
103
  // src/utils.ts
41
104
  function writeFromReadableStream(stream, writable) {
@@ -95,31 +158,35 @@ var buildOutgoingHttpHeaders = (headers) => {
95
158
 
96
159
  // src/response.ts
97
160
  var responseCache = Symbol("responseCache");
98
- var newGlobalResponseKey = Symbol("newGlobalResponse");
99
161
  var cacheKey = Symbol("cache");
100
162
  var GlobalResponse = global.Response;
101
163
  var Response2 = class _Response {
102
164
  #body;
103
165
  #init;
104
- [newGlobalResponseKey]() {
105
- return new GlobalResponse(
106
- this.#body,
107
- this.#init instanceof _Response ? this.#init[newGlobalResponseKey]() : this.#init
108
- );
109
- }
110
- // @ts-ignore
111
166
  get cache() {
112
167
  delete this[cacheKey];
113
- return this[responseCache] ||= this[newGlobalResponseKey]();
168
+ return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
114
169
  }
115
170
  constructor(body, init) {
116
171
  this.#body = body;
117
- this.#init = init;
172
+ if (init instanceof _Response) {
173
+ const cachedGlobalResponse = init[responseCache];
174
+ if (cachedGlobalResponse) {
175
+ this.#init = cachedGlobalResponse;
176
+ this.cache;
177
+ return;
178
+ } else {
179
+ this.#init = init.#init;
180
+ }
181
+ } else {
182
+ this.#init = init;
183
+ }
118
184
  if (typeof body === "string" || body instanceof ReadableStream) {
119
185
  let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
120
186
  if (headers instanceof Headers) {
121
187
  headers = buildOutgoingHttpHeaders(headers);
122
188
  }
189
+ ;
123
190
  this[cacheKey] = [init?.status || 200, body, headers];
124
191
  }
125
192
  }
@@ -156,6 +223,7 @@ Object.defineProperty(global, "Response", {
156
223
  });
157
224
 
158
225
  // src/globals.ts
226
+ var import_node_crypto = __toESM(require("crypto"));
159
227
  Object.defineProperty(global, "Response", {
160
228
  value: Response2
161
229
  });
@@ -173,72 +241,6 @@ global.fetch = (info, init) => {
173
241
  return webFetch(info, init);
174
242
  };
175
243
 
176
- // src/request.ts
177
- var import_node_stream = require("stream");
178
- var newRequestFromIncoming = (method, url, incoming) => {
179
- const headerRecord = [];
180
- const len = incoming.rawHeaders.length;
181
- for (let i = 0; i < len; i += 2) {
182
- headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
183
- }
184
- const init = {
185
- method,
186
- headers: headerRecord
187
- };
188
- if (!(method === "GET" || method === "HEAD")) {
189
- init.body = import_node_stream.Readable.toWeb(incoming);
190
- init.duplex = "half";
191
- }
192
- return new Request(url, init);
193
- };
194
- var getRequestCache = Symbol("getRequestCache");
195
- var requestCache = Symbol("requestCache");
196
- var incomingKey = Symbol("incomingKey");
197
- var requestPrototype = {
198
- get method() {
199
- return this[incomingKey].method || "GET";
200
- },
201
- get url() {
202
- return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
203
- },
204
- [getRequestCache]() {
205
- return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
206
- }
207
- };
208
- [
209
- "body",
210
- "bodyUsed",
211
- "cache",
212
- "credentials",
213
- "destination",
214
- "headers",
215
- "integrity",
216
- "mode",
217
- "redirect",
218
- "referrer",
219
- "referrerPolicy",
220
- "signal"
221
- ].forEach((k) => {
222
- Object.defineProperty(requestPrototype, k, {
223
- get() {
224
- return this[getRequestCache]()[k];
225
- }
226
- });
227
- });
228
- ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
229
- Object.defineProperty(requestPrototype, k, {
230
- value: function() {
231
- return this[getRequestCache]()[k]();
232
- }
233
- });
234
- });
235
- Object.setPrototypeOf(requestPrototype, global.Request.prototype);
236
- var newRequest = (incoming) => {
237
- const req = Object.create(requestPrototype);
238
- req[incomingKey] = incoming;
239
- return req;
240
- };
241
-
242
244
  // src/listener.ts
243
245
  var regBuffer = /^no$/i;
244
246
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
@@ -251,6 +253,9 @@ var handleResponseError = (e, outgoing) => {
251
253
  console.info("The user aborted a request.");
252
254
  } else {
253
255
  console.error(e);
256
+ if (!outgoing.headersSent)
257
+ outgoing.writeHead(500, { "Content-Type": "text/plain" });
258
+ outgoing.end(`Error: ${err.message}`);
254
259
  outgoing.destroy(err);
255
260
  }
256
261
  };
@@ -271,6 +276,13 @@ var responseViaResponseObject = async (res, outgoing) => {
271
276
  if (res instanceof Promise) {
272
277
  res = await res.catch(handleFetchError);
273
278
  }
279
+ if (!(res instanceof Response)) {
280
+ return handleResponseError(
281
+ // @ts-expect-error the object must have `toString()`
282
+ new Error(`The response is not an instance of Response, but ${res.toString()}`),
283
+ outgoing
284
+ );
285
+ }
274
286
  if (cacheKey in res) {
275
287
  try {
276
288
  return responseViaCache(res, outgoing);
package/dist/listener.mjs CHANGED
@@ -1,5 +1,68 @@
1
- // src/globals.ts
2
- import crypto from "crypto";
1
+ // src/request.ts
2
+ import { Readable } from "stream";
3
+ var newRequestFromIncoming = (method, url, incoming) => {
4
+ const headerRecord = [];
5
+ const len = incoming.rawHeaders.length;
6
+ for (let i = 0; i < len; i += 2) {
7
+ headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
8
+ }
9
+ const init = {
10
+ method,
11
+ headers: headerRecord
12
+ };
13
+ if (!(method === "GET" || method === "HEAD")) {
14
+ init.body = Readable.toWeb(incoming);
15
+ init.duplex = "half";
16
+ }
17
+ return new Request(url, init);
18
+ };
19
+ var getRequestCache = Symbol("getRequestCache");
20
+ var requestCache = Symbol("requestCache");
21
+ var incomingKey = Symbol("incomingKey");
22
+ var requestPrototype = {
23
+ get method() {
24
+ return this[incomingKey].method || "GET";
25
+ },
26
+ get url() {
27
+ return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
28
+ },
29
+ [getRequestCache]() {
30
+ return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
31
+ }
32
+ };
33
+ [
34
+ "body",
35
+ "bodyUsed",
36
+ "cache",
37
+ "credentials",
38
+ "destination",
39
+ "headers",
40
+ "integrity",
41
+ "mode",
42
+ "redirect",
43
+ "referrer",
44
+ "referrerPolicy",
45
+ "signal"
46
+ ].forEach((k) => {
47
+ Object.defineProperty(requestPrototype, k, {
48
+ get() {
49
+ return this[getRequestCache]()[k];
50
+ }
51
+ });
52
+ });
53
+ ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
54
+ Object.defineProperty(requestPrototype, k, {
55
+ value: function() {
56
+ return this[getRequestCache]()[k]();
57
+ }
58
+ });
59
+ });
60
+ Object.setPrototypeOf(requestPrototype, global.Request.prototype);
61
+ var newRequest = (incoming) => {
62
+ const req = Object.create(requestPrototype);
63
+ req[incomingKey] = incoming;
64
+ return req;
65
+ };
3
66
 
4
67
  // src/utils.ts
5
68
  function writeFromReadableStream(stream, writable) {
@@ -59,31 +122,35 @@ var buildOutgoingHttpHeaders = (headers) => {
59
122
 
60
123
  // src/response.ts
61
124
  var responseCache = Symbol("responseCache");
62
- var newGlobalResponseKey = Symbol("newGlobalResponse");
63
125
  var cacheKey = Symbol("cache");
64
126
  var GlobalResponse = global.Response;
65
127
  var Response2 = class _Response {
66
128
  #body;
67
129
  #init;
68
- [newGlobalResponseKey]() {
69
- return new GlobalResponse(
70
- this.#body,
71
- this.#init instanceof _Response ? this.#init[newGlobalResponseKey]() : this.#init
72
- );
73
- }
74
- // @ts-ignore
75
130
  get cache() {
76
131
  delete this[cacheKey];
77
- return this[responseCache] ||= this[newGlobalResponseKey]();
132
+ return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
78
133
  }
79
134
  constructor(body, init) {
80
135
  this.#body = body;
81
- this.#init = init;
136
+ if (init instanceof _Response) {
137
+ const cachedGlobalResponse = init[responseCache];
138
+ if (cachedGlobalResponse) {
139
+ this.#init = cachedGlobalResponse;
140
+ this.cache;
141
+ return;
142
+ } else {
143
+ this.#init = init.#init;
144
+ }
145
+ } else {
146
+ this.#init = init;
147
+ }
82
148
  if (typeof body === "string" || body instanceof ReadableStream) {
83
149
  let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
84
150
  if (headers instanceof Headers) {
85
151
  headers = buildOutgoingHttpHeaders(headers);
86
152
  }
153
+ ;
87
154
  this[cacheKey] = [init?.status || 200, body, headers];
88
155
  }
89
156
  }
@@ -120,6 +187,7 @@ Object.defineProperty(global, "Response", {
120
187
  });
121
188
 
122
189
  // src/globals.ts
190
+ import crypto from "crypto";
123
191
  Object.defineProperty(global, "Response", {
124
192
  value: Response2
125
193
  });
@@ -137,72 +205,6 @@ global.fetch = (info, init) => {
137
205
  return webFetch(info, init);
138
206
  };
139
207
 
140
- // src/request.ts
141
- import { Readable } from "stream";
142
- var newRequestFromIncoming = (method, url, incoming) => {
143
- const headerRecord = [];
144
- const len = incoming.rawHeaders.length;
145
- for (let i = 0; i < len; i += 2) {
146
- headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
147
- }
148
- const init = {
149
- method,
150
- headers: headerRecord
151
- };
152
- if (!(method === "GET" || method === "HEAD")) {
153
- init.body = Readable.toWeb(incoming);
154
- init.duplex = "half";
155
- }
156
- return new Request(url, init);
157
- };
158
- var getRequestCache = Symbol("getRequestCache");
159
- var requestCache = Symbol("requestCache");
160
- var incomingKey = Symbol("incomingKey");
161
- var requestPrototype = {
162
- get method() {
163
- return this[incomingKey].method || "GET";
164
- },
165
- get url() {
166
- return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
167
- },
168
- [getRequestCache]() {
169
- return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
170
- }
171
- };
172
- [
173
- "body",
174
- "bodyUsed",
175
- "cache",
176
- "credentials",
177
- "destination",
178
- "headers",
179
- "integrity",
180
- "mode",
181
- "redirect",
182
- "referrer",
183
- "referrerPolicy",
184
- "signal"
185
- ].forEach((k) => {
186
- Object.defineProperty(requestPrototype, k, {
187
- get() {
188
- return this[getRequestCache]()[k];
189
- }
190
- });
191
- });
192
- ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
193
- Object.defineProperty(requestPrototype, k, {
194
- value: function() {
195
- return this[getRequestCache]()[k]();
196
- }
197
- });
198
- });
199
- Object.setPrototypeOf(requestPrototype, global.Request.prototype);
200
- var newRequest = (incoming) => {
201
- const req = Object.create(requestPrototype);
202
- req[incomingKey] = incoming;
203
- return req;
204
- };
205
-
206
208
  // src/listener.ts
207
209
  var regBuffer = /^no$/i;
208
210
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
@@ -215,6 +217,9 @@ var handleResponseError = (e, outgoing) => {
215
217
  console.info("The user aborted a request.");
216
218
  } else {
217
219
  console.error(e);
220
+ if (!outgoing.headersSent)
221
+ outgoing.writeHead(500, { "Content-Type": "text/plain" });
222
+ outgoing.end(`Error: ${err.message}`);
218
223
  outgoing.destroy(err);
219
224
  }
220
225
  };
@@ -235,6 +240,13 @@ var responseViaResponseObject = async (res, outgoing) => {
235
240
  if (res instanceof Promise) {
236
241
  res = await res.catch(handleFetchError);
237
242
  }
243
+ if (!(res instanceof Response)) {
244
+ return handleResponseError(
245
+ // @ts-expect-error the object must have `toString()`
246
+ new Error(`The response is not an instance of Response, but ${res.toString()}`),
247
+ outgoing
248
+ );
249
+ }
238
250
  if (cacheKey in res) {
239
251
  try {
240
252
  return responseViaCache(res, outgoing);
@@ -1,4 +1,3 @@
1
- declare const newGlobalResponseKey: unique symbol;
2
1
  declare const cacheKey: unique symbol;
3
2
  declare const GlobalResponse: {
4
3
  new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): globalThis.Response;
@@ -9,7 +8,6 @@ declare const GlobalResponse: {
9
8
  };
10
9
  declare class Response {
11
10
  #private;
12
- [newGlobalResponseKey](): typeof GlobalResponse;
13
11
  private get cache();
14
12
  constructor(body?: BodyInit | null, init?: ResponseInit);
15
13
  }
@@ -1,4 +1,3 @@
1
- declare const newGlobalResponseKey: unique symbol;
2
1
  declare const cacheKey: unique symbol;
3
2
  declare const GlobalResponse: {
4
3
  new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): globalThis.Response;
@@ -9,7 +8,6 @@ declare const GlobalResponse: {
9
8
  };
10
9
  declare class Response {
11
10
  #private;
12
- [newGlobalResponseKey](): typeof GlobalResponse;
13
11
  private get cache();
14
12
  constructor(body?: BodyInit | null, init?: ResponseInit);
15
13
  }
package/dist/response.js CHANGED
@@ -46,31 +46,35 @@ var buildOutgoingHttpHeaders = (headers) => {
46
46
 
47
47
  // src/response.ts
48
48
  var responseCache = Symbol("responseCache");
49
- var newGlobalResponseKey = Symbol("newGlobalResponse");
50
49
  var cacheKey = Symbol("cache");
51
50
  var GlobalResponse = global.Response;
52
51
  var Response = class _Response {
53
52
  #body;
54
53
  #init;
55
- [newGlobalResponseKey]() {
56
- return new GlobalResponse(
57
- this.#body,
58
- this.#init instanceof _Response ? this.#init[newGlobalResponseKey]() : this.#init
59
- );
60
- }
61
- // @ts-ignore
62
54
  get cache() {
63
55
  delete this[cacheKey];
64
- return this[responseCache] ||= this[newGlobalResponseKey]();
56
+ return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
65
57
  }
66
58
  constructor(body, init) {
67
59
  this.#body = body;
68
- this.#init = init;
60
+ if (init instanceof _Response) {
61
+ const cachedGlobalResponse = init[responseCache];
62
+ if (cachedGlobalResponse) {
63
+ this.#init = cachedGlobalResponse;
64
+ this.cache;
65
+ return;
66
+ } else {
67
+ this.#init = init.#init;
68
+ }
69
+ } else {
70
+ this.#init = init;
71
+ }
69
72
  if (typeof body === "string" || body instanceof ReadableStream) {
70
73
  let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
71
74
  if (headers instanceof Headers) {
72
75
  headers = buildOutgoingHttpHeaders(headers);
73
76
  }
77
+ ;
74
78
  this[cacheKey] = [init?.status || 200, body, headers];
75
79
  }
76
80
  }
package/dist/response.mjs CHANGED
@@ -18,31 +18,35 @@ var buildOutgoingHttpHeaders = (headers) => {
18
18
 
19
19
  // src/response.ts
20
20
  var responseCache = Symbol("responseCache");
21
- var newGlobalResponseKey = Symbol("newGlobalResponse");
22
21
  var cacheKey = Symbol("cache");
23
22
  var GlobalResponse = global.Response;
24
23
  var Response = class _Response {
25
24
  #body;
26
25
  #init;
27
- [newGlobalResponseKey]() {
28
- return new GlobalResponse(
29
- this.#body,
30
- this.#init instanceof _Response ? this.#init[newGlobalResponseKey]() : this.#init
31
- );
32
- }
33
- // @ts-ignore
34
26
  get cache() {
35
27
  delete this[cacheKey];
36
- return this[responseCache] ||= this[newGlobalResponseKey]();
28
+ return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
37
29
  }
38
30
  constructor(body, init) {
39
31
  this.#body = body;
40
- this.#init = init;
32
+ if (init instanceof _Response) {
33
+ const cachedGlobalResponse = init[responseCache];
34
+ if (cachedGlobalResponse) {
35
+ this.#init = cachedGlobalResponse;
36
+ this.cache;
37
+ return;
38
+ } else {
39
+ this.#init = init.#init;
40
+ }
41
+ } else {
42
+ this.#init = init;
43
+ }
41
44
  if (typeof body === "string" || body instanceof ReadableStream) {
42
45
  let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
43
46
  if (headers instanceof Headers) {
44
47
  headers = buildOutgoingHttpHeaders(headers);
45
48
  }
49
+ ;
46
50
  this[cacheKey] = [init?.status || 200, body, headers];
47
51
  }
48
52
  }
package/dist/server.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { AddressInfo } from 'node:net';
2
2
  import { Options, ServerType } from './types.mjs';
3
3
  import 'node:http';
4
- import 'node:https';
5
4
  import 'node:http2';
5
+ import 'node:https';
6
6
 
7
7
  declare const createAdaptorServer: (options: Options) => ServerType;
8
8
  declare const serve: (options: Options, listeningListener?: ((info: AddressInfo) => void) | undefined) => ServerType;
package/dist/server.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { AddressInfo } from 'node:net';
2
2
  import { Options, ServerType } from './types.js';
3
3
  import 'node:http';
4
- import 'node:https';
5
4
  import 'node:http2';
5
+ import 'node:https';
6
6
 
7
7
  declare const createAdaptorServer: (options: Options) => ServerType;
8
8
  declare const serve: (options: Options, listeningListener?: ((info: AddressInfo) => void) | undefined) => ServerType;