@hono/node-server 1.7.0 → 1.8.1

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.
@@ -28,8 +28,9 @@ var import_fs = require("fs");
28
28
  // node_modules/hono/dist/utils/filepath.js
29
29
  var getFilePath = (options) => {
30
30
  let filename = options.filename;
31
- if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename))
31
+ if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) {
32
32
  return;
33
+ }
33
34
  let root = options.root || "";
34
35
  const defaultDocument = options.defaultDocument || "index.html";
35
36
  if (filename.endsWith("/")) {
@@ -46,34 +47,27 @@ var getFilePath = (options) => {
46
47
  };
47
48
 
48
49
  // node_modules/hono/dist/utils/mime.js
49
- var getMimeType = (filename) => {
50
+ var getMimeType = (filename, mimes = baseMimes) => {
50
51
  const regexp = /\.([a-zA-Z0-9]+?)$/;
51
52
  const match = filename.match(regexp);
52
- if (!match)
53
+ if (!match) {
53
54
  return;
55
+ }
54
56
  let mimeType = mimes[match[1]];
55
57
  if (mimeType && mimeType.startsWith("text") || mimeType === "application/json") {
56
58
  mimeType += "; charset=utf-8";
57
59
  }
58
60
  return mimeType;
59
61
  };
60
- var mimes = {
62
+ var baseMimes = {
61
63
  aac: "audio/aac",
62
- abw: "application/x-abiword",
63
- arc: "application/x-freearc",
64
64
  avi: "video/x-msvideo",
65
65
  avif: "image/avif",
66
66
  av1: "video/av1",
67
- azw: "application/vnd.amazon.ebook",
68
67
  bin: "application/octet-stream",
69
68
  bmp: "image/bmp",
70
- bz: "application/x-bzip",
71
- bz2: "application/x-bzip2",
72
- csh: "application/x-csh",
73
69
  css: "text/css",
74
70
  csv: "text/csv",
75
- doc: "application/msword",
76
- docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
77
71
  eot: "application/vnd.ms-fontobject",
78
72
  epub: "application/epub+zip",
79
73
  gif: "image/gif",
@@ -82,7 +76,6 @@ var mimes = {
82
76
  html: "text/html",
83
77
  ico: "image/x-icon",
84
78
  ics: "text/calendar",
85
- jar: "application/java-archive",
86
79
  jpeg: "image/jpeg",
87
80
  jpg: "image/jpeg",
88
81
  js: "text/javascript",
@@ -95,31 +88,20 @@ var mimes = {
95
88
  mp3: "audio/mpeg",
96
89
  mp4: "video/mp4",
97
90
  mpeg: "video/mpeg",
98
- mpkg: "application/vnd.apple.installer+xml",
99
- odp: "application/vnd.oasis.opendocument.presentation",
100
- ods: "application/vnd.oasis.opendocument.spreadsheet",
101
- odt: "application/vnd.oasis.opendocument.text",
102
91
  oga: "audio/ogg",
103
92
  ogv: "video/ogg",
104
93
  ogx: "application/ogg",
105
94
  opus: "audio/opus",
106
95
  otf: "font/otf",
107
96
  pdf: "application/pdf",
108
- php: "application/php",
109
97
  png: "image/png",
110
- ppt: "application/vnd.ms-powerpoint",
111
- pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
112
98
  rtf: "application/rtf",
113
- sh: "application/x-sh",
114
99
  svg: "image/svg+xml",
115
- swf: "application/x-shockwave-flash",
116
- tar: "application/x-tar",
117
100
  tif: "image/tiff",
118
101
  tiff: "image/tiff",
119
102
  ts: "video/mp2t",
120
103
  ttf: "font/ttf",
121
104
  txt: "text/plain",
122
- vsd: "application/vnd.visio",
123
105
  wasm: "application/wasm",
124
106
  webm: "video/webm",
125
107
  weba: "audio/webm",
@@ -127,14 +109,10 @@ var mimes = {
127
109
  woff: "font/woff",
128
110
  woff2: "font/woff2",
129
111
  xhtml: "application/xhtml+xml",
130
- xls: "application/vnd.ms-excel",
131
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
132
112
  xml: "application/xml",
133
- xul: "application/vnd.mozilla.xul+xml",
134
113
  zip: "application/zip",
135
114
  "3gp": "video/3gpp",
136
115
  "3g2": "video/3gpp2",
137
- "7z": "application/x-7z-compressed",
138
116
  gltf: "model/gltf+json",
139
117
  glb: "model/gltf-binary"
140
118
  };
@@ -4,8 +4,9 @@ import { createReadStream, existsSync, lstatSync } from "fs";
4
4
  // node_modules/hono/dist/utils/filepath.js
5
5
  var getFilePath = (options) => {
6
6
  let filename = options.filename;
7
- if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename))
7
+ if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) {
8
8
  return;
9
+ }
9
10
  let root = options.root || "";
10
11
  const defaultDocument = options.defaultDocument || "index.html";
11
12
  if (filename.endsWith("/")) {
@@ -22,34 +23,27 @@ var getFilePath = (options) => {
22
23
  };
23
24
 
24
25
  // node_modules/hono/dist/utils/mime.js
25
- var getMimeType = (filename) => {
26
+ var getMimeType = (filename, mimes = baseMimes) => {
26
27
  const regexp = /\.([a-zA-Z0-9]+?)$/;
27
28
  const match = filename.match(regexp);
28
- if (!match)
29
+ if (!match) {
29
30
  return;
31
+ }
30
32
  let mimeType = mimes[match[1]];
31
33
  if (mimeType && mimeType.startsWith("text") || mimeType === "application/json") {
32
34
  mimeType += "; charset=utf-8";
33
35
  }
34
36
  return mimeType;
35
37
  };
36
- var mimes = {
38
+ var baseMimes = {
37
39
  aac: "audio/aac",
38
- abw: "application/x-abiword",
39
- arc: "application/x-freearc",
40
40
  avi: "video/x-msvideo",
41
41
  avif: "image/avif",
42
42
  av1: "video/av1",
43
- azw: "application/vnd.amazon.ebook",
44
43
  bin: "application/octet-stream",
45
44
  bmp: "image/bmp",
46
- bz: "application/x-bzip",
47
- bz2: "application/x-bzip2",
48
- csh: "application/x-csh",
49
45
  css: "text/css",
50
46
  csv: "text/csv",
51
- doc: "application/msword",
52
- docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
53
47
  eot: "application/vnd.ms-fontobject",
54
48
  epub: "application/epub+zip",
55
49
  gif: "image/gif",
@@ -58,7 +52,6 @@ var mimes = {
58
52
  html: "text/html",
59
53
  ico: "image/x-icon",
60
54
  ics: "text/calendar",
61
- jar: "application/java-archive",
62
55
  jpeg: "image/jpeg",
63
56
  jpg: "image/jpeg",
64
57
  js: "text/javascript",
@@ -71,31 +64,20 @@ var mimes = {
71
64
  mp3: "audio/mpeg",
72
65
  mp4: "video/mp4",
73
66
  mpeg: "video/mpeg",
74
- mpkg: "application/vnd.apple.installer+xml",
75
- odp: "application/vnd.oasis.opendocument.presentation",
76
- ods: "application/vnd.oasis.opendocument.spreadsheet",
77
- odt: "application/vnd.oasis.opendocument.text",
78
67
  oga: "audio/ogg",
79
68
  ogv: "video/ogg",
80
69
  ogx: "application/ogg",
81
70
  opus: "audio/opus",
82
71
  otf: "font/otf",
83
72
  pdf: "application/pdf",
84
- php: "application/php",
85
73
  png: "image/png",
86
- ppt: "application/vnd.ms-powerpoint",
87
- pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
88
74
  rtf: "application/rtf",
89
- sh: "application/x-sh",
90
75
  svg: "image/svg+xml",
91
- swf: "application/x-shockwave-flash",
92
- tar: "application/x-tar",
93
76
  tif: "image/tiff",
94
77
  tiff: "image/tiff",
95
78
  ts: "video/mp2t",
96
79
  ttf: "font/ttf",
97
80
  txt: "text/plain",
98
- vsd: "application/vnd.visio",
99
81
  wasm: "application/wasm",
100
82
  webm: "video/webm",
101
83
  weba: "audio/webm",
@@ -103,14 +85,10 @@ var mimes = {
103
85
  woff: "font/woff",
104
86
  woff2: "font/woff2",
105
87
  xhtml: "application/xhtml+xml",
106
- xls: "application/vnd.ms-excel",
107
- xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
108
88
  xml: "application/xml",
109
- xul: "application/vnd.mozilla.xul+xml",
110
89
  zip: "application/zip",
111
90
  "3gp": "video/3gpp",
112
91
  "3g2": "video/3gpp2",
113
- "7z": "application/x-7z-compressed",
114
92
  gltf: "model/gltf+json",
115
93
  glb: "model/gltf-binary"
116
94
  };
package/dist/server.js CHANGED
@@ -55,7 +55,7 @@ var Request = class extends GlobalRequest {
55
55
  Object.defineProperty(global, "Request", {
56
56
  value: Request
57
57
  });
58
- var newRequestFromIncoming = (method, url, incoming) => {
58
+ var newRequestFromIncoming = (method, url, incoming, abortController) => {
59
59
  const headerRecord = [];
60
60
  const rawHeaders = incoming.rawHeaders;
61
61
  for (let i = 0; i < rawHeaders.length; i += 2) {
@@ -67,7 +67,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
67
67
  }
68
68
  const init = {
69
69
  method,
70
- headers: headerRecord
70
+ headers: headerRecord,
71
+ signal: abortController.signal
71
72
  };
72
73
  if (!(method === "GET" || method === "HEAD")) {
73
74
  init.body = import_node_stream.Readable.toWeb(incoming);
@@ -78,6 +79,8 @@ var getRequestCache = Symbol("getRequestCache");
78
79
  var requestCache = Symbol("requestCache");
79
80
  var incomingKey = Symbol("incomingKey");
80
81
  var urlKey = Symbol("urlKey");
82
+ var abortControllerKey = Symbol("abortControllerKey");
83
+ var getAbortController = Symbol("getAbortController");
81
84
  var requestPrototype = {
82
85
  get method() {
83
86
  return this[incomingKey].method || "GET";
@@ -85,11 +88,17 @@ var requestPrototype = {
85
88
  get url() {
86
89
  return this[urlKey];
87
90
  },
91
+ [getAbortController]() {
92
+ this[getRequestCache]();
93
+ return this[abortControllerKey];
94
+ },
88
95
  [getRequestCache]() {
96
+ this[abortControllerKey] ||= new AbortController();
89
97
  return this[requestCache] ||= newRequestFromIncoming(
90
98
  this.method,
91
99
  this[urlKey],
92
- this[incomingKey]
100
+ this[incomingKey],
101
+ this[abortControllerKey]
93
102
  );
94
103
  }
95
104
  };
@@ -105,7 +114,8 @@ var requestPrototype = {
105
114
  "redirect",
106
115
  "referrer",
107
116
  "referrerPolicy",
108
- "signal"
117
+ "signal",
118
+ "keepalive"
109
119
  ].forEach((k) => {
110
120
  Object.defineProperty(requestPrototype, k, {
111
121
  get() {
@@ -183,18 +193,19 @@ var buildOutgoingHttpHeaders = (headers) => {
183
193
  if (cookies.length > 0) {
184
194
  res["set-cookie"] = cookies;
185
195
  }
186
- res["content-type"] ??= "text/plain;charset=UTF-8";
196
+ res["content-type"] ??= "text/plain; charset=UTF-8";
187
197
  return res;
188
198
  };
189
199
 
190
200
  // src/response.ts
191
201
  var responseCache = Symbol("responseCache");
202
+ var getResponseCache = Symbol("getResponseCache");
192
203
  var cacheKey = Symbol("cache");
193
204
  var GlobalResponse = global.Response;
194
205
  var Response2 = class _Response {
195
206
  #body;
196
207
  #init;
197
- get cache() {
208
+ [getResponseCache]() {
198
209
  delete this[cacheKey];
199
210
  return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
200
211
  }
@@ -204,7 +215,7 @@ var Response2 = class _Response {
204
215
  const cachedGlobalResponse = init[responseCache];
205
216
  if (cachedGlobalResponse) {
206
217
  this.#init = cachedGlobalResponse;
207
- this.cache;
218
+ this[getResponseCache]();
208
219
  return;
209
220
  } else {
210
221
  this.#init = init.#init;
@@ -213,7 +224,7 @@ var Response2 = class _Response {
213
224
  this.#init = init;
214
225
  }
215
226
  if (typeof body === "string" || body instanceof ReadableStream) {
216
- let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
227
+ let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
217
228
  if (headers instanceof Headers) {
218
229
  headers = buildOutgoingHttpHeaders(headers);
219
230
  }
@@ -236,14 +247,14 @@ var Response2 = class _Response {
236
247
  ].forEach((k) => {
237
248
  Object.defineProperty(Response2.prototype, k, {
238
249
  get() {
239
- return this.cache[k];
250
+ return this[getResponseCache]()[k];
240
251
  }
241
252
  });
242
253
  });
243
254
  ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
244
255
  Object.defineProperty(Response2.prototype, k, {
245
256
  value: function() {
246
- return this.cache[k]();
257
+ return this[getResponseCache]()[k]();
247
258
  }
248
259
  });
249
260
  });
@@ -252,6 +263,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
252
263
  Object.defineProperty(global, "Response", {
253
264
  value: Response2
254
265
  });
266
+ var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
267
+ (k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
268
+ );
269
+ if (!stateKey) {
270
+ console.warn("Failed to find Response internal state key");
271
+ }
272
+ function getInternalBody(response) {
273
+ if (!stateKey) {
274
+ return;
275
+ }
276
+ if (response instanceof Response2) {
277
+ response = response[getResponseCache]();
278
+ }
279
+ const state = response[stateKey];
280
+ return state && state.body || void 0;
281
+ }
255
282
 
256
283
  // src/globals.ts
257
284
  var import_node_crypto = __toESM(require("crypto"));
@@ -317,36 +344,40 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
317
344
  res = await res.catch(handleFetchError);
318
345
  }
319
346
  }
320
- try {
321
- const isCached = cacheKey in res;
322
- if (isCached) {
323
- return responseViaCache(res, outgoing);
324
- }
325
- } catch (e) {
326
- return handleResponseError(e, outgoing);
347
+ if (cacheKey in res) {
348
+ return responseViaCache(res, outgoing);
327
349
  }
328
350
  const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
329
- if (res.body) {
330
- try {
331
- const {
332
- "transfer-encoding": transferEncoding,
333
- "content-encoding": contentEncoding,
334
- "content-length": contentLength,
335
- "x-accel-buffering": accelBuffering,
336
- "content-type": contentType
337
- } = resHeaderRecord;
338
- if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
339
- accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
340
- outgoing.writeHead(res.status, resHeaderRecord);
341
- await writeFromReadableStream(res.body, outgoing);
342
- } else {
343
- const buffer = await res.arrayBuffer();
344
- resHeaderRecord["content-length"] = buffer.byteLength;
345
- outgoing.writeHead(res.status, resHeaderRecord);
346
- outgoing.end(new Uint8Array(buffer));
347
- }
348
- } catch (e) {
349
- handleResponseError(e, outgoing);
351
+ const internalBody = getInternalBody(res);
352
+ if (internalBody) {
353
+ if (internalBody.length) {
354
+ resHeaderRecord["content-length"] = internalBody.length;
355
+ }
356
+ outgoing.writeHead(res.status, resHeaderRecord);
357
+ if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
358
+ outgoing.end(internalBody.source);
359
+ } else if (internalBody.source instanceof Blob) {
360
+ outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
361
+ } else {
362
+ await writeFromReadableStream(internalBody.stream, outgoing);
363
+ }
364
+ } else if (res.body) {
365
+ const {
366
+ "transfer-encoding": transferEncoding,
367
+ "content-encoding": contentEncoding,
368
+ "content-length": contentLength,
369
+ "x-accel-buffering": accelBuffering,
370
+ "content-type": contentType
371
+ } = resHeaderRecord;
372
+ if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
373
+ accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
374
+ outgoing.writeHead(res.status, resHeaderRecord);
375
+ await writeFromReadableStream(res.body, outgoing);
376
+ } else {
377
+ const buffer = await res.arrayBuffer();
378
+ resHeaderRecord["content-length"] = buffer.byteLength;
379
+ outgoing.writeHead(res.status, resHeaderRecord);
380
+ outgoing.end(new Uint8Array(buffer));
350
381
  }
351
382
  } else {
352
383
  outgoing.writeHead(res.status, resHeaderRecord);
@@ -357,6 +388,11 @@ var getRequestListener = (fetchCallback, options = {}) => {
357
388
  return async (incoming, outgoing) => {
358
389
  let res;
359
390
  const req = newRequest(incoming);
391
+ outgoing.on("close", () => {
392
+ if (incoming.destroyed) {
393
+ req[getAbortController]().abort();
394
+ }
395
+ });
360
396
  try {
361
397
  res = fetchCallback(req, { incoming, outgoing });
362
398
  if (cacheKey in res) {
@@ -376,7 +412,11 @@ var getRequestListener = (fetchCallback, options = {}) => {
376
412
  return handleResponseError(e, outgoing);
377
413
  }
378
414
  }
379
- return responseViaResponseObject(res, outgoing, options);
415
+ try {
416
+ return responseViaResponseObject(res, outgoing, options);
417
+ } catch (e) {
418
+ return handleResponseError(e, outgoing);
419
+ }
380
420
  };
381
421
  };
382
422
 
package/dist/server.mjs CHANGED
@@ -20,7 +20,7 @@ var Request = class extends GlobalRequest {
20
20
  Object.defineProperty(global, "Request", {
21
21
  value: Request
22
22
  });
23
- var newRequestFromIncoming = (method, url, incoming) => {
23
+ var newRequestFromIncoming = (method, url, incoming, abortController) => {
24
24
  const headerRecord = [];
25
25
  const rawHeaders = incoming.rawHeaders;
26
26
  for (let i = 0; i < rawHeaders.length; i += 2) {
@@ -32,7 +32,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
32
32
  }
33
33
  const init = {
34
34
  method,
35
- headers: headerRecord
35
+ headers: headerRecord,
36
+ signal: abortController.signal
36
37
  };
37
38
  if (!(method === "GET" || method === "HEAD")) {
38
39
  init.body = Readable.toWeb(incoming);
@@ -43,6 +44,8 @@ var getRequestCache = Symbol("getRequestCache");
43
44
  var requestCache = Symbol("requestCache");
44
45
  var incomingKey = Symbol("incomingKey");
45
46
  var urlKey = Symbol("urlKey");
47
+ var abortControllerKey = Symbol("abortControllerKey");
48
+ var getAbortController = Symbol("getAbortController");
46
49
  var requestPrototype = {
47
50
  get method() {
48
51
  return this[incomingKey].method || "GET";
@@ -50,11 +53,17 @@ var requestPrototype = {
50
53
  get url() {
51
54
  return this[urlKey];
52
55
  },
56
+ [getAbortController]() {
57
+ this[getRequestCache]();
58
+ return this[abortControllerKey];
59
+ },
53
60
  [getRequestCache]() {
61
+ this[abortControllerKey] ||= new AbortController();
54
62
  return this[requestCache] ||= newRequestFromIncoming(
55
63
  this.method,
56
64
  this[urlKey],
57
- this[incomingKey]
65
+ this[incomingKey],
66
+ this[abortControllerKey]
58
67
  );
59
68
  }
60
69
  };
@@ -70,7 +79,8 @@ var requestPrototype = {
70
79
  "redirect",
71
80
  "referrer",
72
81
  "referrerPolicy",
73
- "signal"
82
+ "signal",
83
+ "keepalive"
74
84
  ].forEach((k) => {
75
85
  Object.defineProperty(requestPrototype, k, {
76
86
  get() {
@@ -148,18 +158,19 @@ var buildOutgoingHttpHeaders = (headers) => {
148
158
  if (cookies.length > 0) {
149
159
  res["set-cookie"] = cookies;
150
160
  }
151
- res["content-type"] ??= "text/plain;charset=UTF-8";
161
+ res["content-type"] ??= "text/plain; charset=UTF-8";
152
162
  return res;
153
163
  };
154
164
 
155
165
  // src/response.ts
156
166
  var responseCache = Symbol("responseCache");
167
+ var getResponseCache = Symbol("getResponseCache");
157
168
  var cacheKey = Symbol("cache");
158
169
  var GlobalResponse = global.Response;
159
170
  var Response2 = class _Response {
160
171
  #body;
161
172
  #init;
162
- get cache() {
173
+ [getResponseCache]() {
163
174
  delete this[cacheKey];
164
175
  return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
165
176
  }
@@ -169,7 +180,7 @@ var Response2 = class _Response {
169
180
  const cachedGlobalResponse = init[responseCache];
170
181
  if (cachedGlobalResponse) {
171
182
  this.#init = cachedGlobalResponse;
172
- this.cache;
183
+ this[getResponseCache]();
173
184
  return;
174
185
  } else {
175
186
  this.#init = init.#init;
@@ -178,7 +189,7 @@ var Response2 = class _Response {
178
189
  this.#init = init;
179
190
  }
180
191
  if (typeof body === "string" || body instanceof ReadableStream) {
181
- let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
192
+ let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
182
193
  if (headers instanceof Headers) {
183
194
  headers = buildOutgoingHttpHeaders(headers);
184
195
  }
@@ -201,14 +212,14 @@ var Response2 = class _Response {
201
212
  ].forEach((k) => {
202
213
  Object.defineProperty(Response2.prototype, k, {
203
214
  get() {
204
- return this.cache[k];
215
+ return this[getResponseCache]()[k];
205
216
  }
206
217
  });
207
218
  });
208
219
  ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
209
220
  Object.defineProperty(Response2.prototype, k, {
210
221
  value: function() {
211
- return this.cache[k]();
222
+ return this[getResponseCache]()[k]();
212
223
  }
213
224
  });
214
225
  });
@@ -217,6 +228,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
217
228
  Object.defineProperty(global, "Response", {
218
229
  value: Response2
219
230
  });
231
+ var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
232
+ (k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
233
+ );
234
+ if (!stateKey) {
235
+ console.warn("Failed to find Response internal state key");
236
+ }
237
+ function getInternalBody(response) {
238
+ if (!stateKey) {
239
+ return;
240
+ }
241
+ if (response instanceof Response2) {
242
+ response = response[getResponseCache]();
243
+ }
244
+ const state = response[stateKey];
245
+ return state && state.body || void 0;
246
+ }
220
247
 
221
248
  // src/globals.ts
222
249
  import crypto from "crypto";
@@ -282,36 +309,40 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
282
309
  res = await res.catch(handleFetchError);
283
310
  }
284
311
  }
285
- try {
286
- const isCached = cacheKey in res;
287
- if (isCached) {
288
- return responseViaCache(res, outgoing);
289
- }
290
- } catch (e) {
291
- return handleResponseError(e, outgoing);
312
+ if (cacheKey in res) {
313
+ return responseViaCache(res, outgoing);
292
314
  }
293
315
  const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
294
- if (res.body) {
295
- try {
296
- const {
297
- "transfer-encoding": transferEncoding,
298
- "content-encoding": contentEncoding,
299
- "content-length": contentLength,
300
- "x-accel-buffering": accelBuffering,
301
- "content-type": contentType
302
- } = resHeaderRecord;
303
- if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
304
- accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
305
- outgoing.writeHead(res.status, resHeaderRecord);
306
- await writeFromReadableStream(res.body, outgoing);
307
- } else {
308
- const buffer = await res.arrayBuffer();
309
- resHeaderRecord["content-length"] = buffer.byteLength;
310
- outgoing.writeHead(res.status, resHeaderRecord);
311
- outgoing.end(new Uint8Array(buffer));
312
- }
313
- } catch (e) {
314
- handleResponseError(e, outgoing);
316
+ const internalBody = getInternalBody(res);
317
+ if (internalBody) {
318
+ if (internalBody.length) {
319
+ resHeaderRecord["content-length"] = internalBody.length;
320
+ }
321
+ outgoing.writeHead(res.status, resHeaderRecord);
322
+ if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
323
+ outgoing.end(internalBody.source);
324
+ } else if (internalBody.source instanceof Blob) {
325
+ outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
326
+ } else {
327
+ await writeFromReadableStream(internalBody.stream, outgoing);
328
+ }
329
+ } else if (res.body) {
330
+ const {
331
+ "transfer-encoding": transferEncoding,
332
+ "content-encoding": contentEncoding,
333
+ "content-length": contentLength,
334
+ "x-accel-buffering": accelBuffering,
335
+ "content-type": contentType
336
+ } = resHeaderRecord;
337
+ if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
338
+ accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
339
+ outgoing.writeHead(res.status, resHeaderRecord);
340
+ await writeFromReadableStream(res.body, outgoing);
341
+ } else {
342
+ const buffer = await res.arrayBuffer();
343
+ resHeaderRecord["content-length"] = buffer.byteLength;
344
+ outgoing.writeHead(res.status, resHeaderRecord);
345
+ outgoing.end(new Uint8Array(buffer));
315
346
  }
316
347
  } else {
317
348
  outgoing.writeHead(res.status, resHeaderRecord);
@@ -322,6 +353,11 @@ var getRequestListener = (fetchCallback, options = {}) => {
322
353
  return async (incoming, outgoing) => {
323
354
  let res;
324
355
  const req = newRequest(incoming);
356
+ outgoing.on("close", () => {
357
+ if (incoming.destroyed) {
358
+ req[getAbortController]().abort();
359
+ }
360
+ });
325
361
  try {
326
362
  res = fetchCallback(req, { incoming, outgoing });
327
363
  if (cacheKey in res) {
@@ -341,7 +377,11 @@ var getRequestListener = (fetchCallback, options = {}) => {
341
377
  return handleResponseError(e, outgoing);
342
378
  }
343
379
  }
344
- return responseViaResponseObject(res, outgoing, options);
380
+ try {
381
+ return responseViaResponseObject(res, outgoing, options);
382
+ } catch (e) {
383
+ return handleResponseError(e, outgoing);
384
+ }
345
385
  };
346
386
  };
347
387
 
package/dist/utils.js CHANGED
@@ -76,7 +76,7 @@ var buildOutgoingHttpHeaders = (headers) => {
76
76
  if (cookies.length > 0) {
77
77
  res["set-cookie"] = cookies;
78
78
  }
79
- res["content-type"] ??= "text/plain;charset=UTF-8";
79
+ res["content-type"] ??= "text/plain; charset=UTF-8";
80
80
  return res;
81
81
  };
82
82
  // Annotate the CommonJS export names for ESM import in node:
package/dist/utils.mjs CHANGED
@@ -51,7 +51,7 @@ var buildOutgoingHttpHeaders = (headers) => {
51
51
  if (cookies.length > 0) {
52
52
  res["set-cookie"] = cookies;
53
53
  }
54
- res["content-type"] ??= "text/plain;charset=UTF-8";
54
+ res["content-type"] ??= "text/plain; charset=UTF-8";
55
55
  return res;
56
56
  };
57
57
  export {