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