@hono/node-server 1.2.3 → 1.3.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.
- package/dist/globals.js +78 -0
- package/dist/globals.mjs +78 -0
- package/dist/index.js +221 -73
- package/dist/index.mjs +221 -73
- package/dist/listener.d.mts +1 -1
- package/dist/listener.d.ts +1 -1
- package/dist/listener.js +221 -71
- package/dist/listener.mjs +221 -73
- package/dist/request.d.mts +6 -0
- package/dist/request.d.ts +6 -0
- package/dist/request.js +93 -0
- package/dist/request.mjs +68 -0
- package/dist/response.d.mts +15 -0
- package/dist/response.d.ts +15 -0
- package/dist/response.js +106 -0
- package/dist/response.mjs +77 -0
- package/dist/serve-static.d.mts +1 -1
- package/dist/serve-static.d.ts +1 -1
- package/dist/server.js +221 -73
- package/dist/server.mjs +221 -73
- package/dist/types.d.mts +9 -9
- package/dist/types.d.ts +9 -9
- package/dist/utils.d.mts +3 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +18 -0
- package/dist/utils.mjs +17 -0
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +221 -73
- package/dist/vercel.mjs +221 -73
- package/package.json +3 -4
package/dist/index.mjs
CHANGED
|
@@ -1,24 +1,8 @@
|
|
|
1
1
|
// src/server.ts
|
|
2
2
|
import { createServer as createServerHTTP } from "http";
|
|
3
3
|
|
|
4
|
-
// src/listener.ts
|
|
5
|
-
import { Readable } from "stream";
|
|
6
|
-
|
|
7
4
|
// src/globals.ts
|
|
8
5
|
import crypto from "crypto";
|
|
9
|
-
var webFetch = global.fetch;
|
|
10
|
-
if (typeof global.crypto === "undefined") {
|
|
11
|
-
global.crypto = crypto;
|
|
12
|
-
}
|
|
13
|
-
global.fetch = (info, init) => {
|
|
14
|
-
init = {
|
|
15
|
-
// Disable compression handling so people can return the result of a fetch
|
|
16
|
-
// directly in the loader without messing with the Content-Encoding header.
|
|
17
|
-
compress: false,
|
|
18
|
-
...init
|
|
19
|
-
};
|
|
20
|
-
return webFetch(info, init);
|
|
21
|
-
};
|
|
22
6
|
|
|
23
7
|
// src/utils.ts
|
|
24
8
|
function writeFromReadableStream(stream, writable) {
|
|
@@ -59,75 +43,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
59
43
|
}
|
|
60
44
|
}
|
|
61
45
|
}
|
|
46
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
47
|
+
const res = {};
|
|
48
|
+
const cookies = [];
|
|
49
|
+
for (const [k, v] of headers) {
|
|
50
|
+
if (k === "set-cookie") {
|
|
51
|
+
cookies.push(v);
|
|
52
|
+
} else {
|
|
53
|
+
res[k] = v;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (cookies.length > 0) {
|
|
57
|
+
res["set-cookie"] = cookies;
|
|
58
|
+
}
|
|
59
|
+
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
60
|
+
return res;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/response.ts
|
|
64
|
+
var responseCache = Symbol("responseCache");
|
|
65
|
+
var cacheKey = Symbol("cache");
|
|
66
|
+
var globalResponse = global.Response;
|
|
67
|
+
var Response2 = class {
|
|
68
|
+
#body;
|
|
69
|
+
#init;
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
get cache() {
|
|
72
|
+
delete this[cacheKey];
|
|
73
|
+
return this[responseCache] ||= new globalResponse(this.#body, this.#init);
|
|
74
|
+
}
|
|
75
|
+
constructor(body, init) {
|
|
76
|
+
this.#body = body;
|
|
77
|
+
this.#init = init;
|
|
78
|
+
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
79
|
+
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
80
|
+
if (headers instanceof Headers) {
|
|
81
|
+
headers = buildOutgoingHttpHeaders(headers);
|
|
82
|
+
}
|
|
83
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
[
|
|
88
|
+
"body",
|
|
89
|
+
"bodyUsed",
|
|
90
|
+
"headers",
|
|
91
|
+
"ok",
|
|
92
|
+
"redirected",
|
|
93
|
+
"status",
|
|
94
|
+
"statusText",
|
|
95
|
+
"trailers",
|
|
96
|
+
"type",
|
|
97
|
+
"url"
|
|
98
|
+
].forEach((k) => {
|
|
99
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
100
|
+
get() {
|
|
101
|
+
return this.cache[k];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
106
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
107
|
+
value: function() {
|
|
108
|
+
return this.cache[k]();
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
Object.setPrototypeOf(Response2, globalResponse);
|
|
113
|
+
Object.setPrototypeOf(Response2.prototype, globalResponse.prototype);
|
|
114
|
+
Object.defineProperty(global, "Response", {
|
|
115
|
+
value: Response2
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// src/globals.ts
|
|
119
|
+
Object.defineProperty(global, "Response", {
|
|
120
|
+
value: Response2
|
|
121
|
+
});
|
|
122
|
+
var webFetch = global.fetch;
|
|
123
|
+
if (typeof global.crypto === "undefined") {
|
|
124
|
+
global.crypto = crypto;
|
|
125
|
+
}
|
|
126
|
+
global.fetch = (info, init) => {
|
|
127
|
+
init = {
|
|
128
|
+
// Disable compression handling so people can return the result of a fetch
|
|
129
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
130
|
+
compress: false,
|
|
131
|
+
...init
|
|
132
|
+
};
|
|
133
|
+
return webFetch(info, init);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/request.ts
|
|
137
|
+
import { Readable } from "stream";
|
|
138
|
+
var newRequestFromIncoming = (method, url, incoming) => {
|
|
139
|
+
const headerRecord = [];
|
|
140
|
+
const len = incoming.rawHeaders.length;
|
|
141
|
+
for (let i = 0; i < len; i += 2) {
|
|
142
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
143
|
+
}
|
|
144
|
+
const init = {
|
|
145
|
+
method,
|
|
146
|
+
headers: headerRecord
|
|
147
|
+
};
|
|
148
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
149
|
+
init.body = Readable.toWeb(incoming);
|
|
150
|
+
init.duplex = "half";
|
|
151
|
+
}
|
|
152
|
+
return new Request(url, init);
|
|
153
|
+
};
|
|
154
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
155
|
+
var requestCache = Symbol("requestCache");
|
|
156
|
+
var incomingKey = Symbol("incomingKey");
|
|
157
|
+
var requestPrototype = {
|
|
158
|
+
get method() {
|
|
159
|
+
return this[incomingKey].method || "GET";
|
|
160
|
+
},
|
|
161
|
+
get url() {
|
|
162
|
+
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
163
|
+
},
|
|
164
|
+
[getRequestCache]() {
|
|
165
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
[
|
|
169
|
+
"body",
|
|
170
|
+
"bodyUsed",
|
|
171
|
+
"cache",
|
|
172
|
+
"credentials",
|
|
173
|
+
"destination",
|
|
174
|
+
"headers",
|
|
175
|
+
"integrity",
|
|
176
|
+
"mode",
|
|
177
|
+
"redirect",
|
|
178
|
+
"referrer",
|
|
179
|
+
"referrerPolicy",
|
|
180
|
+
"signal"
|
|
181
|
+
].forEach((k) => {
|
|
182
|
+
Object.defineProperty(requestPrototype, k, {
|
|
183
|
+
get() {
|
|
184
|
+
return this[getRequestCache]()[k];
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
189
|
+
Object.defineProperty(requestPrototype, k, {
|
|
190
|
+
value: function() {
|
|
191
|
+
return this[getRequestCache]()[k]();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
196
|
+
var newRequest = (incoming) => {
|
|
197
|
+
const req = Object.create(requestPrototype);
|
|
198
|
+
req[incomingKey] = incoming;
|
|
199
|
+
return req;
|
|
200
|
+
};
|
|
62
201
|
|
|
63
202
|
// src/listener.ts
|
|
64
203
|
var regBuffer = /^no$/i;
|
|
65
204
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
66
|
-
var
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
205
|
+
var handleFetchError = (e) => new Response(null, {
|
|
206
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
207
|
+
});
|
|
208
|
+
var handleResponseError = (e, outgoing) => {
|
|
209
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
210
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
211
|
+
console.info("The user aborted a request.");
|
|
212
|
+
} else {
|
|
213
|
+
console.error(e);
|
|
214
|
+
outgoing.destroy(err);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var responseViaCache = (res, outgoing) => {
|
|
218
|
+
const [status, body, header] = res[cacheKey];
|
|
219
|
+
if (typeof body === "string") {
|
|
220
|
+
header["Content-Length"] = Buffer.byteLength(body);
|
|
221
|
+
outgoing.writeHead(status, header);
|
|
222
|
+
outgoing.end(body);
|
|
223
|
+
} else {
|
|
224
|
+
outgoing.writeHead(status, header);
|
|
225
|
+
return writeFromReadableStream(body, outgoing)?.catch(
|
|
226
|
+
(e) => handleResponseError(e, outgoing)
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
var responseViaResponseObject = async (res, outgoing) => {
|
|
231
|
+
if (res instanceof Promise) {
|
|
232
|
+
res = await res.catch(handleFetchError);
|
|
233
|
+
}
|
|
234
|
+
if (cacheKey in res) {
|
|
84
235
|
try {
|
|
85
|
-
|
|
236
|
+
return responseViaCache(res, outgoing);
|
|
86
237
|
} catch (e) {
|
|
87
|
-
|
|
88
|
-
if (e instanceof Error) {
|
|
89
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
90
|
-
res = new Response(null, { status: 504 });
|
|
91
|
-
}
|
|
92
|
-
}
|
|
238
|
+
return handleResponseError(e, outgoing);
|
|
93
239
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
240
|
+
}
|
|
241
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
242
|
+
if (res.body) {
|
|
243
|
+
try {
|
|
244
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
245
|
+
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
246
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
247
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
99
248
|
} else {
|
|
100
|
-
|
|
249
|
+
const buffer = await res.arrayBuffer();
|
|
250
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
251
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
252
|
+
outgoing.end(new Uint8Array(buffer));
|
|
101
253
|
}
|
|
254
|
+
} catch (e) {
|
|
255
|
+
handleResponseError(e, outgoing);
|
|
102
256
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
} else {
|
|
123
|
-
console.error(e);
|
|
124
|
-
outgoing.destroy(err);
|
|
125
|
-
}
|
|
257
|
+
} else {
|
|
258
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
259
|
+
outgoing.end();
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
var getRequestListener = (fetchCallback) => {
|
|
263
|
+
return (incoming, outgoing) => {
|
|
264
|
+
let res;
|
|
265
|
+
const req = newRequest(incoming);
|
|
266
|
+
try {
|
|
267
|
+
res = fetchCallback(req);
|
|
268
|
+
if (cacheKey in res) {
|
|
269
|
+
return responseViaCache(res, outgoing);
|
|
270
|
+
}
|
|
271
|
+
} catch (e) {
|
|
272
|
+
if (!res) {
|
|
273
|
+
res = handleFetchError(e);
|
|
274
|
+
} else {
|
|
275
|
+
return handleResponseError(e, outgoing);
|
|
126
276
|
}
|
|
127
|
-
} else {
|
|
128
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
129
|
-
outgoing.end();
|
|
130
277
|
}
|
|
278
|
+
return responseViaResponseObject(res, outgoing);
|
|
131
279
|
};
|
|
132
280
|
};
|
|
133
281
|
|
package/dist/listener.d.mts
CHANGED
|
@@ -3,6 +3,6 @@ import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
|
3
3
|
import { FetchCallback } from './types.mjs';
|
|
4
4
|
import 'node:https';
|
|
5
5
|
|
|
6
|
-
declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
|
|
6
|
+
declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => void | Promise<void>;
|
|
7
7
|
|
|
8
8
|
export { getRequestListener };
|
package/dist/listener.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
|
3
3
|
import { FetchCallback } from './types.js';
|
|
4
4
|
import 'node:https';
|
|
5
5
|
|
|
6
|
-
declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
|
|
6
|
+
declare const getRequestListener: (fetchCallback: FetchCallback) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => void | Promise<void>;
|
|
7
7
|
|
|
8
8
|
export { getRequestListener };
|
package/dist/listener.js
CHANGED
|
@@ -33,23 +33,9 @@ __export(listener_exports, {
|
|
|
33
33
|
getRequestListener: () => getRequestListener
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(listener_exports);
|
|
36
|
-
var import_node_stream = require("stream");
|
|
37
36
|
|
|
38
37
|
// src/globals.ts
|
|
39
38
|
var import_node_crypto = __toESM(require("crypto"));
|
|
40
|
-
var webFetch = global.fetch;
|
|
41
|
-
if (typeof global.crypto === "undefined") {
|
|
42
|
-
global.crypto = import_node_crypto.default;
|
|
43
|
-
}
|
|
44
|
-
global.fetch = (info, init) => {
|
|
45
|
-
init = {
|
|
46
|
-
// Disable compression handling so people can return the result of a fetch
|
|
47
|
-
// directly in the loader without messing with the Content-Encoding header.
|
|
48
|
-
compress: false,
|
|
49
|
-
...init
|
|
50
|
-
};
|
|
51
|
-
return webFetch(info, init);
|
|
52
|
-
};
|
|
53
39
|
|
|
54
40
|
// src/utils.ts
|
|
55
41
|
function writeFromReadableStream(stream, writable) {
|
|
@@ -90,75 +76,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
90
76
|
}
|
|
91
77
|
}
|
|
92
78
|
}
|
|
79
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
80
|
+
const res = {};
|
|
81
|
+
const cookies = [];
|
|
82
|
+
for (const [k, v] of headers) {
|
|
83
|
+
if (k === "set-cookie") {
|
|
84
|
+
cookies.push(v);
|
|
85
|
+
} else {
|
|
86
|
+
res[k] = v;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (cookies.length > 0) {
|
|
90
|
+
res["set-cookie"] = cookies;
|
|
91
|
+
}
|
|
92
|
+
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
93
|
+
return res;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/response.ts
|
|
97
|
+
var responseCache = Symbol("responseCache");
|
|
98
|
+
var cacheKey = Symbol("cache");
|
|
99
|
+
var globalResponse = global.Response;
|
|
100
|
+
var Response2 = class {
|
|
101
|
+
#body;
|
|
102
|
+
#init;
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
get cache() {
|
|
105
|
+
delete this[cacheKey];
|
|
106
|
+
return this[responseCache] ||= new globalResponse(this.#body, this.#init);
|
|
107
|
+
}
|
|
108
|
+
constructor(body, init) {
|
|
109
|
+
this.#body = body;
|
|
110
|
+
this.#init = init;
|
|
111
|
+
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
112
|
+
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
113
|
+
if (headers instanceof Headers) {
|
|
114
|
+
headers = buildOutgoingHttpHeaders(headers);
|
|
115
|
+
}
|
|
116
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
[
|
|
121
|
+
"body",
|
|
122
|
+
"bodyUsed",
|
|
123
|
+
"headers",
|
|
124
|
+
"ok",
|
|
125
|
+
"redirected",
|
|
126
|
+
"status",
|
|
127
|
+
"statusText",
|
|
128
|
+
"trailers",
|
|
129
|
+
"type",
|
|
130
|
+
"url"
|
|
131
|
+
].forEach((k) => {
|
|
132
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
133
|
+
get() {
|
|
134
|
+
return this.cache[k];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
139
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
140
|
+
value: function() {
|
|
141
|
+
return this.cache[k]();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
Object.setPrototypeOf(Response2, globalResponse);
|
|
146
|
+
Object.setPrototypeOf(Response2.prototype, globalResponse.prototype);
|
|
147
|
+
Object.defineProperty(global, "Response", {
|
|
148
|
+
value: Response2
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// src/globals.ts
|
|
152
|
+
Object.defineProperty(global, "Response", {
|
|
153
|
+
value: Response2
|
|
154
|
+
});
|
|
155
|
+
var webFetch = global.fetch;
|
|
156
|
+
if (typeof global.crypto === "undefined") {
|
|
157
|
+
global.crypto = import_node_crypto.default;
|
|
158
|
+
}
|
|
159
|
+
global.fetch = (info, init) => {
|
|
160
|
+
init = {
|
|
161
|
+
// Disable compression handling so people can return the result of a fetch
|
|
162
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
163
|
+
compress: false,
|
|
164
|
+
...init
|
|
165
|
+
};
|
|
166
|
+
return webFetch(info, init);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/request.ts
|
|
170
|
+
var import_node_stream = require("stream");
|
|
171
|
+
var newRequestFromIncoming = (method, url, incoming) => {
|
|
172
|
+
const headerRecord = [];
|
|
173
|
+
const len = incoming.rawHeaders.length;
|
|
174
|
+
for (let i = 0; i < len; i += 2) {
|
|
175
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
176
|
+
}
|
|
177
|
+
const init = {
|
|
178
|
+
method,
|
|
179
|
+
headers: headerRecord
|
|
180
|
+
};
|
|
181
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
182
|
+
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
183
|
+
init.duplex = "half";
|
|
184
|
+
}
|
|
185
|
+
return new Request(url, init);
|
|
186
|
+
};
|
|
187
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
188
|
+
var requestCache = Symbol("requestCache");
|
|
189
|
+
var incomingKey = Symbol("incomingKey");
|
|
190
|
+
var requestPrototype = {
|
|
191
|
+
get method() {
|
|
192
|
+
return this[incomingKey].method || "GET";
|
|
193
|
+
},
|
|
194
|
+
get url() {
|
|
195
|
+
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
196
|
+
},
|
|
197
|
+
[getRequestCache]() {
|
|
198
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
[
|
|
202
|
+
"body",
|
|
203
|
+
"bodyUsed",
|
|
204
|
+
"cache",
|
|
205
|
+
"credentials",
|
|
206
|
+
"destination",
|
|
207
|
+
"headers",
|
|
208
|
+
"integrity",
|
|
209
|
+
"mode",
|
|
210
|
+
"redirect",
|
|
211
|
+
"referrer",
|
|
212
|
+
"referrerPolicy",
|
|
213
|
+
"signal"
|
|
214
|
+
].forEach((k) => {
|
|
215
|
+
Object.defineProperty(requestPrototype, k, {
|
|
216
|
+
get() {
|
|
217
|
+
return this[getRequestCache]()[k];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
222
|
+
Object.defineProperty(requestPrototype, k, {
|
|
223
|
+
value: function() {
|
|
224
|
+
return this[getRequestCache]()[k]();
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
229
|
+
var newRequest = (incoming) => {
|
|
230
|
+
const req = Object.create(requestPrototype);
|
|
231
|
+
req[incomingKey] = incoming;
|
|
232
|
+
return req;
|
|
233
|
+
};
|
|
93
234
|
|
|
94
235
|
// src/listener.ts
|
|
95
236
|
var regBuffer = /^no$/i;
|
|
96
237
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
97
|
-
var
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
238
|
+
var handleFetchError = (e) => new Response(null, {
|
|
239
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
240
|
+
});
|
|
241
|
+
var handleResponseError = (e, outgoing) => {
|
|
242
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
243
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
244
|
+
console.info("The user aborted a request.");
|
|
245
|
+
} else {
|
|
246
|
+
console.error(e);
|
|
247
|
+
outgoing.destroy(err);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
var responseViaCache = (res, outgoing) => {
|
|
251
|
+
const [status, body, header] = res[cacheKey];
|
|
252
|
+
if (typeof body === "string") {
|
|
253
|
+
header["Content-Length"] = Buffer.byteLength(body);
|
|
254
|
+
outgoing.writeHead(status, header);
|
|
255
|
+
outgoing.end(body);
|
|
256
|
+
} else {
|
|
257
|
+
outgoing.writeHead(status, header);
|
|
258
|
+
return writeFromReadableStream(body, outgoing)?.catch(
|
|
259
|
+
(e) => handleResponseError(e, outgoing)
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
var responseViaResponseObject = async (res, outgoing) => {
|
|
264
|
+
if (res instanceof Promise) {
|
|
265
|
+
res = await res.catch(handleFetchError);
|
|
266
|
+
}
|
|
267
|
+
if (cacheKey in res) {
|
|
115
268
|
try {
|
|
116
|
-
|
|
269
|
+
return responseViaCache(res, outgoing);
|
|
117
270
|
} catch (e) {
|
|
118
|
-
|
|
119
|
-
if (e instanceof Error) {
|
|
120
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
121
|
-
res = new Response(null, { status: 504 });
|
|
122
|
-
}
|
|
123
|
-
}
|
|
271
|
+
return handleResponseError(e, outgoing);
|
|
124
272
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
273
|
+
}
|
|
274
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
275
|
+
if (res.body) {
|
|
276
|
+
try {
|
|
277
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
278
|
+
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
279
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
280
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
130
281
|
} else {
|
|
131
|
-
|
|
282
|
+
const buffer = await res.arrayBuffer();
|
|
283
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
284
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
285
|
+
outgoing.end(new Uint8Array(buffer));
|
|
132
286
|
}
|
|
287
|
+
} catch (e) {
|
|
288
|
+
handleResponseError(e, outgoing);
|
|
133
289
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
} else {
|
|
154
|
-
console.error(e);
|
|
155
|
-
outgoing.destroy(err);
|
|
156
|
-
}
|
|
290
|
+
} else {
|
|
291
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
292
|
+
outgoing.end();
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
var getRequestListener = (fetchCallback) => {
|
|
296
|
+
return (incoming, outgoing) => {
|
|
297
|
+
let res;
|
|
298
|
+
const req = newRequest(incoming);
|
|
299
|
+
try {
|
|
300
|
+
res = fetchCallback(req);
|
|
301
|
+
if (cacheKey in res) {
|
|
302
|
+
return responseViaCache(res, outgoing);
|
|
303
|
+
}
|
|
304
|
+
} catch (e) {
|
|
305
|
+
if (!res) {
|
|
306
|
+
res = handleFetchError(e);
|
|
307
|
+
} else {
|
|
308
|
+
return handleResponseError(e, outgoing);
|
|
157
309
|
}
|
|
158
|
-
} else {
|
|
159
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
160
|
-
outgoing.end();
|
|
161
310
|
}
|
|
311
|
+
return responseViaResponseObject(res, outgoing);
|
|
162
312
|
};
|
|
163
313
|
};
|
|
164
314
|
// Annotate the CommonJS export names for ESM import in node:
|