@hono/node-server 1.2.0 → 1.2.2

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,7 +1,66 @@
1
1
  // src/listener.ts
2
- import { Readable } from "node:stream";
3
- import { pipeline } from "node:stream/promises";
4
- import "./globals.mjs";
2
+ import { Readable } from "stream";
3
+
4
+ // src/globals.ts
5
+ import crypto from "crypto";
6
+ var webFetch = global.fetch;
7
+ if (typeof global.crypto === "undefined") {
8
+ global.crypto = crypto;
9
+ }
10
+ global.fetch = (info, init) => {
11
+ init = {
12
+ // Disable compression handling so people can return the result of a fetch
13
+ // directly in the loader without messing with the Content-Encoding header.
14
+ compress: false,
15
+ ...init
16
+ };
17
+ return webFetch(info, init);
18
+ };
19
+
20
+ // src/utils.ts
21
+ function writeFromReadableStream(stream, writable) {
22
+ if (stream.locked) {
23
+ throw new TypeError("ReadableStream is locked.");
24
+ }
25
+ const reader = stream.getReader();
26
+ if (writable.destroyed) {
27
+ reader.cancel();
28
+ return;
29
+ }
30
+ writable.on("drain", onDrain);
31
+ writable.on("close", cancel);
32
+ writable.on("error", cancel);
33
+ reader.read().then(flow, cancel);
34
+ return reader.closed.finally(() => {
35
+ writable.off("close", cancel);
36
+ writable.off("error", cancel);
37
+ writable.off("drain", onDrain);
38
+ });
39
+ function cancel(error) {
40
+ reader.cancel(error).catch(() => {
41
+ });
42
+ if (error)
43
+ writable.destroy(error);
44
+ }
45
+ function onDrain() {
46
+ reader.read().then(flow, cancel);
47
+ }
48
+ function flow({ done, value }) {
49
+ try {
50
+ if (done) {
51
+ writable.end();
52
+ } else if (writable.write(value)) {
53
+ return reader.read().then(flow, cancel);
54
+ }
55
+ } catch (e) {
56
+ cancel(e);
57
+ }
58
+ }
59
+ }
60
+
61
+ // src/listener.ts
62
+ var regBuffer = /^no$/i;
63
+ var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
5
64
  var getRequestListener = (fetchCallback) => {
6
65
  return async (incoming, outgoing) => {
7
66
  const method = incoming.method || "GET";
@@ -21,7 +80,7 @@ var getRequestListener = (fetchCallback) => {
21
80
  }
22
81
  let res;
23
82
  try {
24
- res = await fetchCallback(new Request(url.toString(), init));
83
+ res = await fetchCallback(new Request(url, init));
25
84
  } catch (e) {
26
85
  res = new Response(null, { status: 500 });
27
86
  if (e instanceof Error) {
@@ -30,27 +89,29 @@ var getRequestListener = (fetchCallback) => {
30
89
  }
31
90
  }
32
91
  }
33
- const contentType = res.headers.get("content-type") || "";
34
- const buffering = res.headers.get("x-accel-buffering") || "";
35
- const contentEncoding = res.headers.get("content-encoding");
36
- const contentLength = res.headers.get("content-length");
37
- const transferEncoding = res.headers.get("transfer-encoding");
92
+ const resHeaderRecord = {};
93
+ const cookies = [];
38
94
  for (const [k, v] of res.headers) {
39
95
  if (k === "set-cookie") {
40
- outgoing.setHeader(k, res.headers.getSetCookie(k));
96
+ cookies.push(v);
41
97
  } else {
42
- outgoing.setHeader(k, v);
98
+ resHeaderRecord[k] = v;
43
99
  }
44
100
  }
45
- outgoing.statusCode = res.status;
101
+ if (cookies.length > 0) {
102
+ resHeaderRecord["set-cookie"] = cookies;
103
+ }
46
104
  if (res.body) {
47
105
  try {
48
- if (contentEncoding || transferEncoding || contentLength || /^no$/i.test(buffering) || !/^(application\/json\b|text\/(?!event-stream\b))/i.test(contentType)) {
49
- await pipeline(Readable.fromWeb(res.body), outgoing);
106
+ if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
107
+ resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
108
+ outgoing.writeHead(res.status, resHeaderRecord);
109
+ await writeFromReadableStream(res.body, outgoing);
50
110
  } else {
51
- const text = await res.text();
52
- outgoing.setHeader("Content-Length", Buffer.byteLength(text));
53
- outgoing.end(text);
111
+ const buffer = await res.arrayBuffer();
112
+ resHeaderRecord["content-length"] = buffer.byteLength;
113
+ outgoing.writeHead(res.status, resHeaderRecord);
114
+ outgoing.end(new Uint8Array(buffer));
54
115
  }
55
116
  } catch (e) {
56
117
  const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
@@ -62,6 +123,7 @@ var getRequestListener = (fetchCallback) => {
62
123
  }
63
124
  }
64
125
  } else {
126
+ outgoing.writeHead(res.status, resHeaderRecord);
65
127
  outgoing.end();
66
128
  }
67
129
  };
@@ -0,0 +1,14 @@
1
+ import { MiddlewareHandler } from 'hono';
2
+
3
+ declare type ServeStaticOptions = {
4
+ /**
5
+ * Root path, relative to current working directory. (absolute paths are not supported)
6
+ */
7
+ root?: string;
8
+ path?: string;
9
+ index?: string;
10
+ rewriteRequestPath?: (path: string) => string;
11
+ };
12
+ declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
13
+
14
+ export { ServeStaticOptions, serveStatic };
@@ -1,5 +1,6 @@
1
- import type { MiddlewareHandler } from 'hono';
2
- export declare type ServeStaticOptions = {
1
+ import { MiddlewareHandler } from 'hono';
2
+
3
+ declare type ServeStaticOptions = {
3
4
  /**
4
5
  * Root path, relative to current working directory. (absolute paths are not supported)
5
6
  */
@@ -8,4 +9,6 @@ export declare type ServeStaticOptions = {
8
9
  index?: string;
9
10
  rewriteRequestPath?: (path: string) => string;
10
11
  };
11
- export declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
12
+ declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler;
13
+
14
+ export { ServeStaticOptions, serveStatic };
@@ -16,15 +16,131 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/serve-static.ts
19
21
  var serve_static_exports = {};
20
22
  __export(serve_static_exports, {
21
23
  serveStatic: () => serveStatic
22
24
  });
23
25
  module.exports = __toCommonJS(serve_static_exports);
24
26
  var import_fs = require("fs");
25
- var import_filepath = require("hono/utils/filepath");
26
- var import_mime = require("hono/utils/mime");
27
- const createStreamBody = (stream) => {
27
+
28
+ // node_modules/hono/dist/utils/filepath.js
29
+ var getFilePath = (options) => {
30
+ let filename = options.filename;
31
+ if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename))
32
+ return;
33
+ let root = options.root || "";
34
+ const defaultDocument = options.defaultDocument || "index.html";
35
+ if (filename.endsWith("/")) {
36
+ filename = filename.concat(defaultDocument);
37
+ } else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {
38
+ filename = filename.concat("/" + defaultDocument);
39
+ }
40
+ filename = filename.replace(/^\.?[\/\\]/, "");
41
+ filename = filename.replace(/\\/, "/");
42
+ root = root.replace(/\/$/, "");
43
+ let path = root ? root + "/" + filename : filename;
44
+ path = path.replace(/^\.?\//, "");
45
+ return path;
46
+ };
47
+
48
+ // node_modules/hono/dist/utils/mime.js
49
+ var getMimeType = (filename) => {
50
+ const regexp = /\.([a-zA-Z0-9]+?)$/;
51
+ const match = filename.match(regexp);
52
+ if (!match)
53
+ return;
54
+ let mimeType = mimes[match[1]];
55
+ if (mimeType && mimeType.startsWith("text") || mimeType === "application/json") {
56
+ mimeType += "; charset=utf-8";
57
+ }
58
+ return mimeType;
59
+ };
60
+ var mimes = {
61
+ aac: "audio/aac",
62
+ abw: "application/x-abiword",
63
+ arc: "application/x-freearc",
64
+ avi: "video/x-msvideo",
65
+ avif: "image/avif",
66
+ av1: "video/av1",
67
+ azw: "application/vnd.amazon.ebook",
68
+ bin: "application/octet-stream",
69
+ bmp: "image/bmp",
70
+ bz: "application/x-bzip",
71
+ bz2: "application/x-bzip2",
72
+ csh: "application/x-csh",
73
+ css: "text/css",
74
+ csv: "text/csv",
75
+ doc: "application/msword",
76
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
77
+ eot: "application/vnd.ms-fontobject",
78
+ epub: "application/epub+zip",
79
+ gif: "image/gif",
80
+ gz: "application/gzip",
81
+ htm: "text/html",
82
+ html: "text/html",
83
+ ico: "image/x-icon",
84
+ ics: "text/calendar",
85
+ jar: "application/java-archive",
86
+ jpeg: "image/jpeg",
87
+ jpg: "image/jpeg",
88
+ js: "text/javascript",
89
+ json: "application/json",
90
+ jsonld: "application/ld+json",
91
+ map: "application/json",
92
+ mid: "audio/x-midi",
93
+ midi: "audio/x-midi",
94
+ mjs: "text/javascript",
95
+ mp3: "audio/mpeg",
96
+ mp4: "video/mp4",
97
+ 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
+ oga: "audio/ogg",
103
+ ogv: "video/ogg",
104
+ ogx: "application/ogg",
105
+ opus: "audio/opus",
106
+ otf: "font/otf",
107
+ pdf: "application/pdf",
108
+ php: "application/php",
109
+ png: "image/png",
110
+ ppt: "application/vnd.ms-powerpoint",
111
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
112
+ rtf: "application/rtf",
113
+ sh: "application/x-sh",
114
+ svg: "image/svg+xml",
115
+ swf: "application/x-shockwave-flash",
116
+ tar: "application/x-tar",
117
+ tif: "image/tiff",
118
+ tiff: "image/tiff",
119
+ ts: "video/mp2t",
120
+ ttf: "font/ttf",
121
+ txt: "text/plain",
122
+ vsd: "application/vnd.visio",
123
+ wasm: "application/wasm",
124
+ webm: "video/webm",
125
+ weba: "audio/webm",
126
+ webp: "image/webp",
127
+ woff: "font/woff",
128
+ woff2: "font/woff2",
129
+ xhtml: "application/xhtml+xml",
130
+ xls: "application/vnd.ms-excel",
131
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
132
+ xml: "application/xml",
133
+ xul: "application/vnd.mozilla.xul+xml",
134
+ zip: "application/zip",
135
+ "3gp": "video/3gpp",
136
+ "3g2": "video/3gpp2",
137
+ "7z": "application/x-7z-compressed",
138
+ gltf: "model/gltf+json",
139
+ glb: "model/gltf-binary"
140
+ };
141
+
142
+ // src/serve-static.ts
143
+ var createStreamBody = (stream) => {
28
144
  const body = new ReadableStream({
29
145
  start(controller) {
30
146
  stream.on("data", (chunk) => {
@@ -40,13 +156,13 @@ const createStreamBody = (stream) => {
40
156
  });
41
157
  return body;
42
158
  };
43
- const serveStatic = (options = { root: "" }) => {
159
+ var serveStatic = (options = { root: "" }) => {
44
160
  return async (c, next) => {
45
161
  if (c.finalized)
46
162
  return next();
47
163
  const url = new URL(c.req.url);
48
164
  const filename = options.path ?? decodeURIComponent(url.pathname);
49
- let path = (0, import_filepath.getFilePath)({
165
+ let path = getFilePath({
50
166
  filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
51
167
  root: options.root,
52
168
  defaultDocument: options.index ?? "index.html"
@@ -57,7 +173,7 @@ const serveStatic = (options = { root: "" }) => {
57
173
  if (!(0, import_fs.existsSync)(path)) {
58
174
  return next();
59
175
  }
60
- const mimeType = (0, import_mime.getMimeType)(path);
176
+ const mimeType = getMimeType(path);
61
177
  if (mimeType) {
62
178
  c.header("Content-Type", mimeType);
63
179
  }
@@ -1,7 +1,121 @@
1
1
  // src/serve-static.ts
2
2
  import { createReadStream, existsSync, lstatSync } from "fs";
3
- import { getFilePath } from "hono/utils/filepath";
4
- import { getMimeType } from "hono/utils/mime";
3
+
4
+ // node_modules/hono/dist/utils/filepath.js
5
+ var getFilePath = (options) => {
6
+ let filename = options.filename;
7
+ if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename))
8
+ return;
9
+ let root = options.root || "";
10
+ const defaultDocument = options.defaultDocument || "index.html";
11
+ if (filename.endsWith("/")) {
12
+ filename = filename.concat(defaultDocument);
13
+ } else if (!filename.match(/\.[a-zA-Z0-9]+$/)) {
14
+ filename = filename.concat("/" + defaultDocument);
15
+ }
16
+ filename = filename.replace(/^\.?[\/\\]/, "");
17
+ filename = filename.replace(/\\/, "/");
18
+ root = root.replace(/\/$/, "");
19
+ let path = root ? root + "/" + filename : filename;
20
+ path = path.replace(/^\.?\//, "");
21
+ return path;
22
+ };
23
+
24
+ // node_modules/hono/dist/utils/mime.js
25
+ var getMimeType = (filename) => {
26
+ const regexp = /\.([a-zA-Z0-9]+?)$/;
27
+ const match = filename.match(regexp);
28
+ if (!match)
29
+ return;
30
+ let mimeType = mimes[match[1]];
31
+ if (mimeType && mimeType.startsWith("text") || mimeType === "application/json") {
32
+ mimeType += "; charset=utf-8";
33
+ }
34
+ return mimeType;
35
+ };
36
+ var mimes = {
37
+ aac: "audio/aac",
38
+ abw: "application/x-abiword",
39
+ arc: "application/x-freearc",
40
+ avi: "video/x-msvideo",
41
+ avif: "image/avif",
42
+ av1: "video/av1",
43
+ azw: "application/vnd.amazon.ebook",
44
+ bin: "application/octet-stream",
45
+ bmp: "image/bmp",
46
+ bz: "application/x-bzip",
47
+ bz2: "application/x-bzip2",
48
+ csh: "application/x-csh",
49
+ css: "text/css",
50
+ csv: "text/csv",
51
+ doc: "application/msword",
52
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
53
+ eot: "application/vnd.ms-fontobject",
54
+ epub: "application/epub+zip",
55
+ gif: "image/gif",
56
+ gz: "application/gzip",
57
+ htm: "text/html",
58
+ html: "text/html",
59
+ ico: "image/x-icon",
60
+ ics: "text/calendar",
61
+ jar: "application/java-archive",
62
+ jpeg: "image/jpeg",
63
+ jpg: "image/jpeg",
64
+ js: "text/javascript",
65
+ json: "application/json",
66
+ jsonld: "application/ld+json",
67
+ map: "application/json",
68
+ mid: "audio/x-midi",
69
+ midi: "audio/x-midi",
70
+ mjs: "text/javascript",
71
+ mp3: "audio/mpeg",
72
+ mp4: "video/mp4",
73
+ 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
+ oga: "audio/ogg",
79
+ ogv: "video/ogg",
80
+ ogx: "application/ogg",
81
+ opus: "audio/opus",
82
+ otf: "font/otf",
83
+ pdf: "application/pdf",
84
+ php: "application/php",
85
+ png: "image/png",
86
+ ppt: "application/vnd.ms-powerpoint",
87
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
88
+ rtf: "application/rtf",
89
+ sh: "application/x-sh",
90
+ svg: "image/svg+xml",
91
+ swf: "application/x-shockwave-flash",
92
+ tar: "application/x-tar",
93
+ tif: "image/tiff",
94
+ tiff: "image/tiff",
95
+ ts: "video/mp2t",
96
+ ttf: "font/ttf",
97
+ txt: "text/plain",
98
+ vsd: "application/vnd.visio",
99
+ wasm: "application/wasm",
100
+ webm: "video/webm",
101
+ weba: "audio/webm",
102
+ webp: "image/webp",
103
+ woff: "font/woff",
104
+ woff2: "font/woff2",
105
+ xhtml: "application/xhtml+xml",
106
+ xls: "application/vnd.ms-excel",
107
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
108
+ xml: "application/xml",
109
+ xul: "application/vnd.mozilla.xul+xml",
110
+ zip: "application/zip",
111
+ "3gp": "video/3gpp",
112
+ "3g2": "video/3gpp2",
113
+ "7z": "application/x-7z-compressed",
114
+ gltf: "model/gltf+json",
115
+ glb: "model/gltf-binary"
116
+ };
117
+
118
+ // src/serve-static.ts
5
119
  var createStreamBody = (stream) => {
6
120
  const body = new ReadableStream({
7
121
  start(controller) {
@@ -0,0 +1,10 @@
1
+ import { AddressInfo } from 'node:net';
2
+ import { Options, ServerType } from './types.mjs';
3
+ import 'node:http';
4
+ import 'node:https';
5
+ import 'node:http2';
6
+
7
+ declare const createAdaptorServer: (options: Options) => ServerType;
8
+ declare const serve: (options: Options, listeningListener?: ((info: AddressInfo) => void) | undefined) => ServerType;
9
+
10
+ export { createAdaptorServer, serve };
package/dist/server.d.ts CHANGED
@@ -1,4 +1,10 @@
1
- import type { AddressInfo } from 'node:net';
2
- import type { Options, ServerType } from './types';
3
- export declare const createAdaptorServer: (options: Options) => ServerType;
4
- export declare const serve: (options: Options, listeningListener?: ((info: AddressInfo) => void) | undefined) => ServerType;
1
+ import { AddressInfo } from 'node:net';
2
+ import { Options, ServerType } from './types.js';
3
+ import 'node:http';
4
+ import 'node:https';
5
+ import 'node:http2';
6
+
7
+ declare const createAdaptorServer: (options: Options) => ServerType;
8
+ declare const serve: (options: Options, listeningListener?: ((info: AddressInfo) => void) | undefined) => ServerType;
9
+
10
+ export { createAdaptorServer, serve };
package/dist/server.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,23 +17,165 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/server.ts
19
31
  var server_exports = {};
20
32
  __export(server_exports, {
21
33
  createAdaptorServer: () => createAdaptorServer,
22
34
  serve: () => serve
23
35
  });
24
36
  module.exports = __toCommonJS(server_exports);
25
- var import_node_http = require("node:http");
26
- var import_listener = require("./listener");
27
- const createAdaptorServer = (options) => {
37
+ var import_node_http = require("http");
38
+
39
+ // src/listener.ts
40
+ var import_node_stream = require("stream");
41
+
42
+ // src/globals.ts
43
+ var import_node_crypto = __toESM(require("crypto"));
44
+ var webFetch = global.fetch;
45
+ if (typeof global.crypto === "undefined") {
46
+ global.crypto = import_node_crypto.default;
47
+ }
48
+ global.fetch = (info, init) => {
49
+ init = {
50
+ // Disable compression handling so people can return the result of a fetch
51
+ // directly in the loader without messing with the Content-Encoding header.
52
+ compress: false,
53
+ ...init
54
+ };
55
+ return webFetch(info, init);
56
+ };
57
+
58
+ // src/utils.ts
59
+ function writeFromReadableStream(stream, writable) {
60
+ if (stream.locked) {
61
+ throw new TypeError("ReadableStream is locked.");
62
+ }
63
+ const reader = stream.getReader();
64
+ if (writable.destroyed) {
65
+ reader.cancel();
66
+ return;
67
+ }
68
+ writable.on("drain", onDrain);
69
+ writable.on("close", cancel);
70
+ writable.on("error", cancel);
71
+ reader.read().then(flow, cancel);
72
+ return reader.closed.finally(() => {
73
+ writable.off("close", cancel);
74
+ writable.off("error", cancel);
75
+ writable.off("drain", onDrain);
76
+ });
77
+ function cancel(error) {
78
+ reader.cancel(error).catch(() => {
79
+ });
80
+ if (error)
81
+ writable.destroy(error);
82
+ }
83
+ function onDrain() {
84
+ reader.read().then(flow, cancel);
85
+ }
86
+ function flow({ done, value }) {
87
+ try {
88
+ if (done) {
89
+ writable.end();
90
+ } else if (writable.write(value)) {
91
+ return reader.read().then(flow, cancel);
92
+ }
93
+ } catch (e) {
94
+ cancel(e);
95
+ }
96
+ }
97
+ }
98
+
99
+ // src/listener.ts
100
+ var regBuffer = /^no$/i;
101
+ var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
102
+ var getRequestListener = (fetchCallback) => {
103
+ return async (incoming, outgoing) => {
104
+ const method = incoming.method || "GET";
105
+ const url = `http://${incoming.headers.host}${incoming.url}`;
106
+ const headerRecord = [];
107
+ const len = incoming.rawHeaders.length;
108
+ for (let i = 0; i < len; i += 2) {
109
+ headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
110
+ }
111
+ const init = {
112
+ method,
113
+ headers: headerRecord
114
+ };
115
+ if (!(method === "GET" || method === "HEAD")) {
116
+ init.body = import_node_stream.Readable.toWeb(incoming);
117
+ init.duplex = "half";
118
+ }
119
+ let res;
120
+ try {
121
+ res = await fetchCallback(new Request(url, init));
122
+ } catch (e) {
123
+ res = new Response(null, { status: 500 });
124
+ if (e instanceof Error) {
125
+ if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
126
+ res = new Response(null, { status: 504 });
127
+ }
128
+ }
129
+ }
130
+ const resHeaderRecord = {};
131
+ const cookies = [];
132
+ for (const [k, v] of res.headers) {
133
+ if (k === "set-cookie") {
134
+ cookies.push(v);
135
+ } else {
136
+ resHeaderRecord[k] = v;
137
+ }
138
+ }
139
+ if (cookies.length > 0) {
140
+ resHeaderRecord["set-cookie"] = cookies;
141
+ }
142
+ if (res.body) {
143
+ try {
144
+ if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
145
+ resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
146
+ outgoing.writeHead(res.status, resHeaderRecord);
147
+ await writeFromReadableStream(res.body, outgoing);
148
+ } else {
149
+ const buffer = await res.arrayBuffer();
150
+ resHeaderRecord["content-length"] = buffer.byteLength;
151
+ outgoing.writeHead(res.status, resHeaderRecord);
152
+ outgoing.end(new Uint8Array(buffer));
153
+ }
154
+ } catch (e) {
155
+ const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
156
+ if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
157
+ console.info("The user aborted a request.");
158
+ } else {
159
+ console.error(e);
160
+ outgoing.destroy(err);
161
+ }
162
+ }
163
+ } else {
164
+ outgoing.writeHead(res.status, resHeaderRecord);
165
+ outgoing.end();
166
+ }
167
+ };
168
+ };
169
+
170
+ // src/server.ts
171
+ var createAdaptorServer = (options) => {
28
172
  const fetchCallback = options.fetch;
29
- const requestListener = (0, import_listener.getRequestListener)(fetchCallback);
173
+ const requestListener = getRequestListener(fetchCallback);
30
174
  const createServer = options.createServer || import_node_http.createServer;
31
175
  const server = createServer(options.serverOptions || {}, requestListener);
32
176
  return server;
33
177
  };
34
- const serve = (options, listeningListener) => {
178
+ var serve = (options, listeningListener) => {
35
179
  const server = createAdaptorServer(options);
36
180
  server.listen(options?.port ?? 3e3, options.hostname ?? "0.0.0.0", () => {
37
181
  const serverInfo = server.address();