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