@hono/node-server 1.3.3 → 1.3.5

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