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