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