@hono/node-server 1.19.10 → 2.0.0-rc.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/README.md +1 -7
- package/dist/conninfo.d.mts +4 -3
- package/dist/conninfo.d.ts +4 -3
- package/dist/conninfo.js +20 -40
- package/dist/conninfo.mjs +19 -16
- package/dist/constants-B7DBcQew.js +11 -0
- package/dist/constants-DEKKqoym.mjs +5 -0
- package/dist/index.d.mts +62 -8
- package/dist/index.d.ts +62 -8
- package/dist/index.js +28 -613
- package/dist/index.mjs +24 -573
- package/dist/listener-Brd4yZ5d.js +726 -0
- package/dist/listener-RIBxK9_x.mjs +715 -0
- package/dist/serve-static.d.mts +14 -13
- package/dist/serve-static.d.ts +14 -13
- package/dist/serve-static.js +128 -170
- package/dist/serve-static.mjs +127 -145
- package/dist/utils/response.d.mts +3 -2
- package/dist/utils/response.d.ts +3 -2
- package/dist/utils/response.js +6 -35
- package/dist/utils/response.mjs +6 -9
- package/dist/vercel.d.mts +6 -5
- package/dist/vercel.d.ts +6 -5
- package/dist/vercel.js +7 -585
- package/dist/vercel.mjs +6 -548
- package/package.json +11 -13
- package/dist/globals.d.mts +0 -2
- package/dist/globals.d.ts +0 -2
- package/dist/globals.js +0 -29
- package/dist/globals.mjs +0 -5
- package/dist/listener.d.mts +0 -13
- package/dist/listener.d.ts +0 -13
- package/dist/listener.js +0 -581
- package/dist/listener.mjs +0 -546
- package/dist/request.d.mts +0 -25
- package/dist/request.d.ts +0 -25
- package/dist/request.js +0 -227
- package/dist/request.mjs +0 -195
- package/dist/response.d.mts +0 -26
- package/dist/response.d.ts +0 -26
- package/dist/response.js +0 -99
- package/dist/response.mjs +0 -72
- package/dist/server.d.mts +0 -10
- package/dist/server.d.ts +0 -10
- package/dist/server.js +0 -607
- package/dist/server.mjs +0 -571
- package/dist/types.d.mts +0 -44
- package/dist/types.d.ts +0 -44
- package/dist/types.js +0 -18
- package/dist/types.mjs +0 -0
- package/dist/utils/response/constants.d.mts +0 -3
- package/dist/utils/response/constants.d.ts +0 -3
- package/dist/utils/response/constants.js +0 -30
- package/dist/utils/response/constants.mjs +0 -5
- package/dist/utils.d.mts +0 -9
- package/dist/utils.d.ts +0 -9
- package/dist/utils.js +0 -99
- package/dist/utils.mjs +0 -71
package/dist/listener.js
DELETED
|
@@ -1,581 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
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
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/listener.ts
|
|
31
|
-
var listener_exports = {};
|
|
32
|
-
__export(listener_exports, {
|
|
33
|
-
getRequestListener: () => getRequestListener
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(listener_exports);
|
|
36
|
-
var import_node_http22 = require("http2");
|
|
37
|
-
|
|
38
|
-
// src/request.ts
|
|
39
|
-
var import_node_http2 = require("http2");
|
|
40
|
-
var import_node_stream = require("stream");
|
|
41
|
-
var RequestError = class extends Error {
|
|
42
|
-
constructor(message, options) {
|
|
43
|
-
super(message, options);
|
|
44
|
-
this.name = "RequestError";
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
var toRequestError = (e) => {
|
|
48
|
-
if (e instanceof RequestError) {
|
|
49
|
-
return e;
|
|
50
|
-
}
|
|
51
|
-
return new RequestError(e.message, { cause: e });
|
|
52
|
-
};
|
|
53
|
-
var GlobalRequest = global.Request;
|
|
54
|
-
var Request = class extends GlobalRequest {
|
|
55
|
-
constructor(input, options) {
|
|
56
|
-
if (typeof input === "object" && getRequestCache in input) {
|
|
57
|
-
input = input[getRequestCache]();
|
|
58
|
-
}
|
|
59
|
-
if (typeof options?.body?.getReader !== "undefined") {
|
|
60
|
-
;
|
|
61
|
-
options.duplex ??= "half";
|
|
62
|
-
}
|
|
63
|
-
super(input, options);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
var newHeadersFromIncoming = (incoming) => {
|
|
67
|
-
const headerRecord = [];
|
|
68
|
-
const rawHeaders = incoming.rawHeaders;
|
|
69
|
-
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
70
|
-
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
71
|
-
if (key.charCodeAt(0) !== /*:*/
|
|
72
|
-
58) {
|
|
73
|
-
headerRecord.push([key, value]);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return new Headers(headerRecord);
|
|
77
|
-
};
|
|
78
|
-
var wrapBodyStream = Symbol("wrapBodyStream");
|
|
79
|
-
var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
|
|
80
|
-
const init = {
|
|
81
|
-
method,
|
|
82
|
-
headers,
|
|
83
|
-
signal: abortController.signal
|
|
84
|
-
};
|
|
85
|
-
if (method === "TRACE") {
|
|
86
|
-
init.method = "GET";
|
|
87
|
-
const req = new Request(url, init);
|
|
88
|
-
Object.defineProperty(req, "method", {
|
|
89
|
-
get() {
|
|
90
|
-
return "TRACE";
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
return req;
|
|
94
|
-
}
|
|
95
|
-
if (!(method === "GET" || method === "HEAD")) {
|
|
96
|
-
if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) {
|
|
97
|
-
init.body = new ReadableStream({
|
|
98
|
-
start(controller) {
|
|
99
|
-
controller.enqueue(incoming.rawBody);
|
|
100
|
-
controller.close();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
} else if (incoming[wrapBodyStream]) {
|
|
104
|
-
let reader;
|
|
105
|
-
init.body = new ReadableStream({
|
|
106
|
-
async pull(controller) {
|
|
107
|
-
try {
|
|
108
|
-
reader ||= import_node_stream.Readable.toWeb(incoming).getReader();
|
|
109
|
-
const { done, value } = await reader.read();
|
|
110
|
-
if (done) {
|
|
111
|
-
controller.close();
|
|
112
|
-
} else {
|
|
113
|
-
controller.enqueue(value);
|
|
114
|
-
}
|
|
115
|
-
} catch (error) {
|
|
116
|
-
controller.error(error);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
} else {
|
|
121
|
-
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return new Request(url, init);
|
|
125
|
-
};
|
|
126
|
-
var getRequestCache = Symbol("getRequestCache");
|
|
127
|
-
var requestCache = Symbol("requestCache");
|
|
128
|
-
var incomingKey = Symbol("incomingKey");
|
|
129
|
-
var urlKey = Symbol("urlKey");
|
|
130
|
-
var headersKey = Symbol("headersKey");
|
|
131
|
-
var abortControllerKey = Symbol("abortControllerKey");
|
|
132
|
-
var getAbortController = Symbol("getAbortController");
|
|
133
|
-
var requestPrototype = {
|
|
134
|
-
get method() {
|
|
135
|
-
return this[incomingKey].method || "GET";
|
|
136
|
-
},
|
|
137
|
-
get url() {
|
|
138
|
-
return this[urlKey];
|
|
139
|
-
},
|
|
140
|
-
get headers() {
|
|
141
|
-
return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
|
|
142
|
-
},
|
|
143
|
-
[getAbortController]() {
|
|
144
|
-
this[getRequestCache]();
|
|
145
|
-
return this[abortControllerKey];
|
|
146
|
-
},
|
|
147
|
-
[getRequestCache]() {
|
|
148
|
-
this[abortControllerKey] ||= new AbortController();
|
|
149
|
-
return this[requestCache] ||= newRequestFromIncoming(
|
|
150
|
-
this.method,
|
|
151
|
-
this[urlKey],
|
|
152
|
-
this.headers,
|
|
153
|
-
this[incomingKey],
|
|
154
|
-
this[abortControllerKey]
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
[
|
|
159
|
-
"body",
|
|
160
|
-
"bodyUsed",
|
|
161
|
-
"cache",
|
|
162
|
-
"credentials",
|
|
163
|
-
"destination",
|
|
164
|
-
"integrity",
|
|
165
|
-
"mode",
|
|
166
|
-
"redirect",
|
|
167
|
-
"referrer",
|
|
168
|
-
"referrerPolicy",
|
|
169
|
-
"signal",
|
|
170
|
-
"keepalive"
|
|
171
|
-
].forEach((k) => {
|
|
172
|
-
Object.defineProperty(requestPrototype, k, {
|
|
173
|
-
get() {
|
|
174
|
-
return this[getRequestCache]()[k];
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
179
|
-
Object.defineProperty(requestPrototype, k, {
|
|
180
|
-
value: function() {
|
|
181
|
-
return this[getRequestCache]()[k]();
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
186
|
-
var newRequest = (incoming, defaultHostname) => {
|
|
187
|
-
const req = Object.create(requestPrototype);
|
|
188
|
-
req[incomingKey] = incoming;
|
|
189
|
-
const incomingUrl = incoming.url || "";
|
|
190
|
-
if (incomingUrl[0] !== "/" && // short-circuit for performance. most requests are relative URL.
|
|
191
|
-
(incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) {
|
|
192
|
-
if (incoming instanceof import_node_http2.Http2ServerRequest) {
|
|
193
|
-
throw new RequestError("Absolute URL for :path is not allowed in HTTP/2");
|
|
194
|
-
}
|
|
195
|
-
try {
|
|
196
|
-
const url2 = new URL(incomingUrl);
|
|
197
|
-
req[urlKey] = url2.href;
|
|
198
|
-
} catch (e) {
|
|
199
|
-
throw new RequestError("Invalid absolute URL", { cause: e });
|
|
200
|
-
}
|
|
201
|
-
return req;
|
|
202
|
-
}
|
|
203
|
-
const host = (incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
|
|
204
|
-
if (!host) {
|
|
205
|
-
throw new RequestError("Missing host header");
|
|
206
|
-
}
|
|
207
|
-
let scheme;
|
|
208
|
-
if (incoming instanceof import_node_http2.Http2ServerRequest) {
|
|
209
|
-
scheme = incoming.scheme;
|
|
210
|
-
if (!(scheme === "http" || scheme === "https")) {
|
|
211
|
-
throw new RequestError("Unsupported scheme");
|
|
212
|
-
}
|
|
213
|
-
} else {
|
|
214
|
-
scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http";
|
|
215
|
-
}
|
|
216
|
-
const url = new URL(`${scheme}://${host}${incomingUrl}`);
|
|
217
|
-
if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
|
|
218
|
-
throw new RequestError("Invalid host header");
|
|
219
|
-
}
|
|
220
|
-
req[urlKey] = url.href;
|
|
221
|
-
return req;
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// src/response.ts
|
|
225
|
-
var responseCache = Symbol("responseCache");
|
|
226
|
-
var getResponseCache = Symbol("getResponseCache");
|
|
227
|
-
var cacheKey = Symbol("cache");
|
|
228
|
-
var GlobalResponse = global.Response;
|
|
229
|
-
var Response2 = class _Response {
|
|
230
|
-
#body;
|
|
231
|
-
#init;
|
|
232
|
-
[getResponseCache]() {
|
|
233
|
-
delete this[cacheKey];
|
|
234
|
-
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
235
|
-
}
|
|
236
|
-
constructor(body, init) {
|
|
237
|
-
let headers;
|
|
238
|
-
this.#body = body;
|
|
239
|
-
if (init instanceof _Response) {
|
|
240
|
-
const cachedGlobalResponse = init[responseCache];
|
|
241
|
-
if (cachedGlobalResponse) {
|
|
242
|
-
this.#init = cachedGlobalResponse;
|
|
243
|
-
this[getResponseCache]();
|
|
244
|
-
return;
|
|
245
|
-
} else {
|
|
246
|
-
this.#init = init.#init;
|
|
247
|
-
headers = new Headers(init.#init.headers);
|
|
248
|
-
}
|
|
249
|
-
} else {
|
|
250
|
-
this.#init = init;
|
|
251
|
-
}
|
|
252
|
-
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
253
|
-
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
254
|
-
this[cacheKey] = [init?.status || 200, body, headers];
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
get headers() {
|
|
258
|
-
const cache = this[cacheKey];
|
|
259
|
-
if (cache) {
|
|
260
|
-
if (!(cache[2] instanceof Headers)) {
|
|
261
|
-
cache[2] = new Headers(cache[2]);
|
|
262
|
-
}
|
|
263
|
-
return cache[2];
|
|
264
|
-
}
|
|
265
|
-
return this[getResponseCache]().headers;
|
|
266
|
-
}
|
|
267
|
-
get status() {
|
|
268
|
-
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
269
|
-
}
|
|
270
|
-
get ok() {
|
|
271
|
-
const status = this.status;
|
|
272
|
-
return status >= 200 && status < 300;
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
|
|
276
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
277
|
-
get() {
|
|
278
|
-
return this[getResponseCache]()[k];
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
});
|
|
282
|
-
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
283
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
284
|
-
value: function() {
|
|
285
|
-
return this[getResponseCache]()[k]();
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
});
|
|
289
|
-
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
290
|
-
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
291
|
-
|
|
292
|
-
// src/utils.ts
|
|
293
|
-
async function readWithoutBlocking(readPromise) {
|
|
294
|
-
return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
|
|
295
|
-
}
|
|
296
|
-
function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
|
|
297
|
-
const cancel = (error) => {
|
|
298
|
-
reader.cancel(error).catch(() => {
|
|
299
|
-
});
|
|
300
|
-
};
|
|
301
|
-
writable.on("close", cancel);
|
|
302
|
-
writable.on("error", cancel);
|
|
303
|
-
(currentReadPromise ?? reader.read()).then(flow, handleStreamError);
|
|
304
|
-
return reader.closed.finally(() => {
|
|
305
|
-
writable.off("close", cancel);
|
|
306
|
-
writable.off("error", cancel);
|
|
307
|
-
});
|
|
308
|
-
function handleStreamError(error) {
|
|
309
|
-
if (error) {
|
|
310
|
-
writable.destroy(error);
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
function onDrain() {
|
|
314
|
-
reader.read().then(flow, handleStreamError);
|
|
315
|
-
}
|
|
316
|
-
function flow({ done, value }) {
|
|
317
|
-
try {
|
|
318
|
-
if (done) {
|
|
319
|
-
writable.end();
|
|
320
|
-
} else if (!writable.write(value)) {
|
|
321
|
-
writable.once("drain", onDrain);
|
|
322
|
-
} else {
|
|
323
|
-
return reader.read().then(flow, handleStreamError);
|
|
324
|
-
}
|
|
325
|
-
} catch (e) {
|
|
326
|
-
handleStreamError(e);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
function writeFromReadableStream(stream, writable) {
|
|
331
|
-
if (stream.locked) {
|
|
332
|
-
throw new TypeError("ReadableStream is locked.");
|
|
333
|
-
} else if (writable.destroyed) {
|
|
334
|
-
return;
|
|
335
|
-
}
|
|
336
|
-
return writeFromReadableStreamDefaultReader(stream.getReader(), writable);
|
|
337
|
-
}
|
|
338
|
-
var buildOutgoingHttpHeaders = (headers) => {
|
|
339
|
-
const res = {};
|
|
340
|
-
if (!(headers instanceof Headers)) {
|
|
341
|
-
headers = new Headers(headers ?? void 0);
|
|
342
|
-
}
|
|
343
|
-
const cookies = [];
|
|
344
|
-
for (const [k, v] of headers) {
|
|
345
|
-
if (k === "set-cookie") {
|
|
346
|
-
cookies.push(v);
|
|
347
|
-
} else {
|
|
348
|
-
res[k] = v;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
if (cookies.length > 0) {
|
|
352
|
-
res["set-cookie"] = cookies;
|
|
353
|
-
}
|
|
354
|
-
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
355
|
-
return res;
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
// src/utils/response/constants.ts
|
|
359
|
-
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
360
|
-
|
|
361
|
-
// src/globals.ts
|
|
362
|
-
var import_node_crypto = __toESM(require("crypto"));
|
|
363
|
-
if (typeof global.crypto === "undefined") {
|
|
364
|
-
global.crypto = import_node_crypto.default;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
// src/listener.ts
|
|
368
|
-
var outgoingEnded = Symbol("outgoingEnded");
|
|
369
|
-
var handleRequestError = () => new Response(null, {
|
|
370
|
-
status: 400
|
|
371
|
-
});
|
|
372
|
-
var handleFetchError = (e) => new Response(null, {
|
|
373
|
-
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
374
|
-
});
|
|
375
|
-
var handleResponseError = (e, outgoing) => {
|
|
376
|
-
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
377
|
-
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
378
|
-
console.info("The user aborted a request.");
|
|
379
|
-
} else {
|
|
380
|
-
console.error(e);
|
|
381
|
-
if (!outgoing.headersSent) {
|
|
382
|
-
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
383
|
-
}
|
|
384
|
-
outgoing.end(`Error: ${err.message}`);
|
|
385
|
-
outgoing.destroy(err);
|
|
386
|
-
}
|
|
387
|
-
};
|
|
388
|
-
var flushHeaders = (outgoing) => {
|
|
389
|
-
if ("flushHeaders" in outgoing && outgoing.writable) {
|
|
390
|
-
outgoing.flushHeaders();
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
var responseViaCache = async (res, outgoing) => {
|
|
394
|
-
let [status, body, header] = res[cacheKey];
|
|
395
|
-
if (header instanceof Headers) {
|
|
396
|
-
header = buildOutgoingHttpHeaders(header);
|
|
397
|
-
}
|
|
398
|
-
if (typeof body === "string") {
|
|
399
|
-
header["Content-Length"] = Buffer.byteLength(body);
|
|
400
|
-
} else if (body instanceof Uint8Array) {
|
|
401
|
-
header["Content-Length"] = body.byteLength;
|
|
402
|
-
} else if (body instanceof Blob) {
|
|
403
|
-
header["Content-Length"] = body.size;
|
|
404
|
-
}
|
|
405
|
-
outgoing.writeHead(status, header);
|
|
406
|
-
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
407
|
-
outgoing.end(body);
|
|
408
|
-
} else if (body instanceof Blob) {
|
|
409
|
-
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
410
|
-
} else {
|
|
411
|
-
flushHeaders(outgoing);
|
|
412
|
-
await writeFromReadableStream(body, outgoing)?.catch(
|
|
413
|
-
(e) => handleResponseError(e, outgoing)
|
|
414
|
-
);
|
|
415
|
-
}
|
|
416
|
-
;
|
|
417
|
-
outgoing[outgoingEnded]?.();
|
|
418
|
-
};
|
|
419
|
-
var isPromise = (res) => typeof res.then === "function";
|
|
420
|
-
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
421
|
-
if (isPromise(res)) {
|
|
422
|
-
if (options.errorHandler) {
|
|
423
|
-
try {
|
|
424
|
-
res = await res;
|
|
425
|
-
} catch (err) {
|
|
426
|
-
const errRes = await options.errorHandler(err);
|
|
427
|
-
if (!errRes) {
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
res = errRes;
|
|
431
|
-
}
|
|
432
|
-
} else {
|
|
433
|
-
res = await res.catch(handleFetchError);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
if (cacheKey in res) {
|
|
437
|
-
return responseViaCache(res, outgoing);
|
|
438
|
-
}
|
|
439
|
-
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
440
|
-
if (res.body) {
|
|
441
|
-
const reader = res.body.getReader();
|
|
442
|
-
const values = [];
|
|
443
|
-
let done = false;
|
|
444
|
-
let currentReadPromise = void 0;
|
|
445
|
-
if (resHeaderRecord["transfer-encoding"] !== "chunked") {
|
|
446
|
-
let maxReadCount = 2;
|
|
447
|
-
for (let i = 0; i < maxReadCount; i++) {
|
|
448
|
-
currentReadPromise ||= reader.read();
|
|
449
|
-
const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
|
|
450
|
-
console.error(e);
|
|
451
|
-
done = true;
|
|
452
|
-
});
|
|
453
|
-
if (!chunk) {
|
|
454
|
-
if (i === 1) {
|
|
455
|
-
await new Promise((resolve) => setTimeout(resolve));
|
|
456
|
-
maxReadCount = 3;
|
|
457
|
-
continue;
|
|
458
|
-
}
|
|
459
|
-
break;
|
|
460
|
-
}
|
|
461
|
-
currentReadPromise = void 0;
|
|
462
|
-
if (chunk.value) {
|
|
463
|
-
values.push(chunk.value);
|
|
464
|
-
}
|
|
465
|
-
if (chunk.done) {
|
|
466
|
-
done = true;
|
|
467
|
-
break;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
if (done && !("content-length" in resHeaderRecord)) {
|
|
471
|
-
resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
475
|
-
values.forEach((value) => {
|
|
476
|
-
;
|
|
477
|
-
outgoing.write(value);
|
|
478
|
-
});
|
|
479
|
-
if (done) {
|
|
480
|
-
outgoing.end();
|
|
481
|
-
} else {
|
|
482
|
-
if (values.length === 0) {
|
|
483
|
-
flushHeaders(outgoing);
|
|
484
|
-
}
|
|
485
|
-
await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
|
|
486
|
-
}
|
|
487
|
-
} else if (resHeaderRecord[X_ALREADY_SENT]) {
|
|
488
|
-
} else {
|
|
489
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
490
|
-
outgoing.end();
|
|
491
|
-
}
|
|
492
|
-
;
|
|
493
|
-
outgoing[outgoingEnded]?.();
|
|
494
|
-
};
|
|
495
|
-
var getRequestListener = (fetchCallback, options = {}) => {
|
|
496
|
-
const autoCleanupIncoming = options.autoCleanupIncoming ?? true;
|
|
497
|
-
if (options.overrideGlobalObjects !== false && global.Request !== Request) {
|
|
498
|
-
Object.defineProperty(global, "Request", {
|
|
499
|
-
value: Request
|
|
500
|
-
});
|
|
501
|
-
Object.defineProperty(global, "Response", {
|
|
502
|
-
value: Response2
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
return async (incoming, outgoing) => {
|
|
506
|
-
let res, req;
|
|
507
|
-
try {
|
|
508
|
-
req = newRequest(incoming, options.hostname);
|
|
509
|
-
let incomingEnded = !autoCleanupIncoming || incoming.method === "GET" || incoming.method === "HEAD";
|
|
510
|
-
if (!incomingEnded) {
|
|
511
|
-
;
|
|
512
|
-
incoming[wrapBodyStream] = true;
|
|
513
|
-
incoming.on("end", () => {
|
|
514
|
-
incomingEnded = true;
|
|
515
|
-
});
|
|
516
|
-
if (incoming instanceof import_node_http22.Http2ServerRequest) {
|
|
517
|
-
;
|
|
518
|
-
outgoing[outgoingEnded] = () => {
|
|
519
|
-
if (!incomingEnded) {
|
|
520
|
-
setTimeout(() => {
|
|
521
|
-
if (!incomingEnded) {
|
|
522
|
-
setTimeout(() => {
|
|
523
|
-
incoming.destroy();
|
|
524
|
-
outgoing.destroy();
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
};
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
outgoing.on("close", () => {
|
|
533
|
-
const abortController = req[abortControllerKey];
|
|
534
|
-
if (abortController) {
|
|
535
|
-
if (incoming.errored) {
|
|
536
|
-
req[abortControllerKey].abort(incoming.errored.toString());
|
|
537
|
-
} else if (!outgoing.writableFinished) {
|
|
538
|
-
req[abortControllerKey].abort("Client connection prematurely closed.");
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
if (!incomingEnded) {
|
|
542
|
-
setTimeout(() => {
|
|
543
|
-
if (!incomingEnded) {
|
|
544
|
-
setTimeout(() => {
|
|
545
|
-
incoming.destroy();
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
});
|
|
551
|
-
res = fetchCallback(req, { incoming, outgoing });
|
|
552
|
-
if (cacheKey in res) {
|
|
553
|
-
return responseViaCache(res, outgoing);
|
|
554
|
-
}
|
|
555
|
-
} catch (e) {
|
|
556
|
-
if (!res) {
|
|
557
|
-
if (options.errorHandler) {
|
|
558
|
-
res = await options.errorHandler(req ? e : toRequestError(e));
|
|
559
|
-
if (!res) {
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
} else if (!req) {
|
|
563
|
-
res = handleRequestError();
|
|
564
|
-
} else {
|
|
565
|
-
res = handleFetchError(e);
|
|
566
|
-
}
|
|
567
|
-
} else {
|
|
568
|
-
return handleResponseError(e, outgoing);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
try {
|
|
572
|
-
return await responseViaResponseObject(res, outgoing, options);
|
|
573
|
-
} catch (e) {
|
|
574
|
-
return handleResponseError(e, outgoing);
|
|
575
|
-
}
|
|
576
|
-
};
|
|
577
|
-
};
|
|
578
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
579
|
-
0 && (module.exports = {
|
|
580
|
-
getRequestListener
|
|
581
|
-
});
|