@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/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) {
@@ -97,31 +160,35 @@ var buildOutgoingHttpHeaders = (headers) => {
97
160
 
98
161
  // src/response.ts
99
162
  var responseCache = Symbol("responseCache");
100
- var newGlobalResponseKey = Symbol("newGlobalResponse");
101
163
  var cacheKey = Symbol("cache");
102
164
  var GlobalResponse = global.Response;
103
165
  var Response2 = class _Response {
104
166
  #body;
105
167
  #init;
106
- [newGlobalResponseKey]() {
107
- return new GlobalResponse(
108
- this.#body,
109
- this.#init instanceof _Response ? this.#init[newGlobalResponseKey]() : this.#init
110
- );
111
- }
112
- // @ts-ignore
113
168
  get cache() {
114
169
  delete this[cacheKey];
115
- return this[responseCache] ||= this[newGlobalResponseKey]();
170
+ return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
116
171
  }
117
172
  constructor(body, init) {
118
173
  this.#body = body;
119
- this.#init = init;
174
+ if (init instanceof _Response) {
175
+ const cachedGlobalResponse = init[responseCache];
176
+ if (cachedGlobalResponse) {
177
+ this.#init = cachedGlobalResponse;
178
+ this.cache;
179
+ return;
180
+ } else {
181
+ this.#init = init.#init;
182
+ }
183
+ } else {
184
+ this.#init = init;
185
+ }
120
186
  if (typeof body === "string" || body instanceof ReadableStream) {
121
187
  let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
122
188
  if (headers instanceof Headers) {
123
189
  headers = buildOutgoingHttpHeaders(headers);
124
190
  }
191
+ ;
125
192
  this[cacheKey] = [init?.status || 200, body, headers];
126
193
  }
127
194
  }
@@ -158,6 +225,7 @@ Object.defineProperty(global, "Response", {
158
225
  });
159
226
 
160
227
  // src/globals.ts
228
+ var import_node_crypto = __toESM(require("crypto"));
161
229
  Object.defineProperty(global, "Response", {
162
230
  value: Response2
163
231
  });
@@ -175,72 +243,6 @@ global.fetch = (info, init) => {
175
243
  return webFetch(info, init);
176
244
  };
177
245
 
178
- // src/request.ts
179
- var import_node_stream = require("stream");
180
- var newRequestFromIncoming = (method, url, incoming) => {
181
- const headerRecord = [];
182
- const len = incoming.rawHeaders.length;
183
- for (let i = 0; i < len; i += 2) {
184
- headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
185
- }
186
- const init = {
187
- method,
188
- headers: headerRecord
189
- };
190
- if (!(method === "GET" || method === "HEAD")) {
191
- init.body = import_node_stream.Readable.toWeb(incoming);
192
- init.duplex = "half";
193
- }
194
- return new Request(url, init);
195
- };
196
- var getRequestCache = Symbol("getRequestCache");
197
- var requestCache = Symbol("requestCache");
198
- var incomingKey = Symbol("incomingKey");
199
- var requestPrototype = {
200
- get method() {
201
- return this[incomingKey].method || "GET";
202
- },
203
- get url() {
204
- return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
205
- },
206
- [getRequestCache]() {
207
- return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
208
- }
209
- };
210
- [
211
- "body",
212
- "bodyUsed",
213
- "cache",
214
- "credentials",
215
- "destination",
216
- "headers",
217
- "integrity",
218
- "mode",
219
- "redirect",
220
- "referrer",
221
- "referrerPolicy",
222
- "signal"
223
- ].forEach((k) => {
224
- Object.defineProperty(requestPrototype, k, {
225
- get() {
226
- return this[getRequestCache]()[k];
227
- }
228
- });
229
- });
230
- ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
231
- Object.defineProperty(requestPrototype, k, {
232
- value: function() {
233
- return this[getRequestCache]()[k]();
234
- }
235
- });
236
- });
237
- Object.setPrototypeOf(requestPrototype, global.Request.prototype);
238
- var newRequest = (incoming) => {
239
- const req = Object.create(requestPrototype);
240
- req[incomingKey] = incoming;
241
- return req;
242
- };
243
-
244
246
  // src/listener.ts
245
247
  var regBuffer = /^no$/i;
246
248
  var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
@@ -253,6 +255,9 @@ var handleResponseError = (e, outgoing) => {
253
255
  console.info("The user aborted a request.");
254
256
  } else {
255
257
  console.error(e);
258
+ if (!outgoing.headersSent)
259
+ outgoing.writeHead(500, { "Content-Type": "text/plain" });
260
+ outgoing.end(`Error: ${err.message}`);
256
261
  outgoing.destroy(err);
257
262
  }
258
263
  };
@@ -273,6 +278,13 @@ var responseViaResponseObject = async (res, outgoing) => {
273
278
  if (res instanceof Promise) {
274
279
  res = await res.catch(handleFetchError);
275
280
  }
281
+ if (!(res instanceof Response)) {
282
+ return handleResponseError(
283
+ // @ts-expect-error the object must have `toString()`
284
+ new Error(`The response is not an instance of Response, but ${res.toString()}`),
285
+ outgoing
286
+ );
287
+ }
276
288
  if (cacheKey in res) {
277
289
  try {
278
290
  return responseViaCache(res, outgoing);
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) {
@@ -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);
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;
package/dist/vercel.js CHANGED
@@ -34,8 +34,71 @@ __export(vercel_exports, {
34
34
  });
35
35
  module.exports = __toCommonJS(vercel_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);