@hono/node-server 1.6.0 → 1.8.0
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 +34 -0
- package/dist/index.js +102 -42
- package/dist/index.mjs +102 -42
- package/dist/listener.d.mts +4 -2
- package/dist/listener.d.ts +4 -2
- package/dist/listener.js +102 -42
- package/dist/listener.mjs +102 -42
- package/dist/request.d.mts +2 -1
- package/dist/request.d.ts +2 -1
- package/dist/request.js +14 -3
- package/dist/request.mjs +13 -3
- package/dist/response.d.mts +9 -2
- package/dist/response.d.ts +9 -2
- package/dist/response.js +27 -8
- package/dist/response.mjs +25 -7
- package/dist/serve-static.js +6 -28
- package/dist/serve-static.mjs +6 -28
- package/dist/server.js +102 -42
- package/dist/server.mjs +102 -42
- package/dist/types.d.mts +2 -1
- package/dist/types.d.ts +2 -1
- package/dist/utils.js +1 -1
- package/dist/utils.mjs +1 -1
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +102 -42
- package/dist/vercel.mjs +102 -42
- package/package.json +2 -2
package/dist/server.mjs
CHANGED
|
@@ -20,7 +20,7 @@ var Request = class extends GlobalRequest {
|
|
|
20
20
|
Object.defineProperty(global, "Request", {
|
|
21
21
|
value: Request
|
|
22
22
|
});
|
|
23
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
23
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
24
24
|
const headerRecord = [];
|
|
25
25
|
const rawHeaders = incoming.rawHeaders;
|
|
26
26
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -32,7 +32,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
32
32
|
}
|
|
33
33
|
const init = {
|
|
34
34
|
method,
|
|
35
|
-
headers: headerRecord
|
|
35
|
+
headers: headerRecord,
|
|
36
|
+
signal: abortController.signal
|
|
36
37
|
};
|
|
37
38
|
if (!(method === "GET" || method === "HEAD")) {
|
|
38
39
|
init.body = Readable.toWeb(incoming);
|
|
@@ -43,6 +44,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
43
44
|
var requestCache = Symbol("requestCache");
|
|
44
45
|
var incomingKey = Symbol("incomingKey");
|
|
45
46
|
var urlKey = Symbol("urlKey");
|
|
47
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
48
|
+
var getAbortController = Symbol("getAbortController");
|
|
46
49
|
var requestPrototype = {
|
|
47
50
|
get method() {
|
|
48
51
|
return this[incomingKey].method || "GET";
|
|
@@ -50,11 +53,17 @@ var requestPrototype = {
|
|
|
50
53
|
get url() {
|
|
51
54
|
return this[urlKey];
|
|
52
55
|
},
|
|
56
|
+
[getAbortController]() {
|
|
57
|
+
this[getRequestCache]();
|
|
58
|
+
return this[abortControllerKey];
|
|
59
|
+
},
|
|
53
60
|
[getRequestCache]() {
|
|
61
|
+
this[abortControllerKey] ||= new AbortController();
|
|
54
62
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
55
63
|
this.method,
|
|
56
64
|
this[urlKey],
|
|
57
|
-
this[incomingKey]
|
|
65
|
+
this[incomingKey],
|
|
66
|
+
this[abortControllerKey]
|
|
58
67
|
);
|
|
59
68
|
}
|
|
60
69
|
};
|
|
@@ -148,18 +157,19 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
148
157
|
if (cookies.length > 0) {
|
|
149
158
|
res["set-cookie"] = cookies;
|
|
150
159
|
}
|
|
151
|
-
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
160
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
152
161
|
return res;
|
|
153
162
|
};
|
|
154
163
|
|
|
155
164
|
// src/response.ts
|
|
156
165
|
var responseCache = Symbol("responseCache");
|
|
166
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
157
167
|
var cacheKey = Symbol("cache");
|
|
158
168
|
var GlobalResponse = global.Response;
|
|
159
169
|
var Response2 = class _Response {
|
|
160
170
|
#body;
|
|
161
171
|
#init;
|
|
162
|
-
|
|
172
|
+
[getResponseCache]() {
|
|
163
173
|
delete this[cacheKey];
|
|
164
174
|
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
165
175
|
}
|
|
@@ -169,7 +179,7 @@ var Response2 = class _Response {
|
|
|
169
179
|
const cachedGlobalResponse = init[responseCache];
|
|
170
180
|
if (cachedGlobalResponse) {
|
|
171
181
|
this.#init = cachedGlobalResponse;
|
|
172
|
-
this
|
|
182
|
+
this[getResponseCache]();
|
|
173
183
|
return;
|
|
174
184
|
} else {
|
|
175
185
|
this.#init = init.#init;
|
|
@@ -178,7 +188,7 @@ var Response2 = class _Response {
|
|
|
178
188
|
this.#init = init;
|
|
179
189
|
}
|
|
180
190
|
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
181
|
-
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
191
|
+
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
182
192
|
if (headers instanceof Headers) {
|
|
183
193
|
headers = buildOutgoingHttpHeaders(headers);
|
|
184
194
|
}
|
|
@@ -201,14 +211,14 @@ var Response2 = class _Response {
|
|
|
201
211
|
].forEach((k) => {
|
|
202
212
|
Object.defineProperty(Response2.prototype, k, {
|
|
203
213
|
get() {
|
|
204
|
-
return this
|
|
214
|
+
return this[getResponseCache]()[k];
|
|
205
215
|
}
|
|
206
216
|
});
|
|
207
217
|
});
|
|
208
218
|
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
209
219
|
Object.defineProperty(Response2.prototype, k, {
|
|
210
220
|
value: function() {
|
|
211
|
-
return this
|
|
221
|
+
return this[getResponseCache]()[k]();
|
|
212
222
|
}
|
|
213
223
|
});
|
|
214
224
|
});
|
|
@@ -217,6 +227,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
|
217
227
|
Object.defineProperty(global, "Response", {
|
|
218
228
|
value: Response2
|
|
219
229
|
});
|
|
230
|
+
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
231
|
+
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
232
|
+
);
|
|
233
|
+
if (!stateKey) {
|
|
234
|
+
console.warn("Failed to find Response internal state key");
|
|
235
|
+
}
|
|
236
|
+
function getInternalBody(response) {
|
|
237
|
+
if (!stateKey) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (response instanceof Response2) {
|
|
241
|
+
response = response[getResponseCache]();
|
|
242
|
+
}
|
|
243
|
+
const state = response[stateKey];
|
|
244
|
+
return state && state.body || void 0;
|
|
245
|
+
}
|
|
220
246
|
|
|
221
247
|
// src/globals.ts
|
|
222
248
|
import crypto from "crypto";
|
|
@@ -266,48 +292,71 @@ var responseViaCache = (res, outgoing) => {
|
|
|
266
292
|
);
|
|
267
293
|
}
|
|
268
294
|
};
|
|
269
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
295
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
296
|
+
if (res instanceof Promise) {
|
|
297
|
+
if (options.errorHandler) {
|
|
298
|
+
try {
|
|
299
|
+
res = await res;
|
|
300
|
+
} catch (err) {
|
|
301
|
+
const errRes = await options.errorHandler(err);
|
|
302
|
+
if (!errRes) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
res = errRes;
|
|
306
|
+
}
|
|
307
|
+
} else {
|
|
308
|
+
res = await res.catch(handleFetchError);
|
|
275
309
|
}
|
|
276
|
-
}
|
|
277
|
-
|
|
310
|
+
}
|
|
311
|
+
if (cacheKey in res) {
|
|
312
|
+
return responseViaCache(res, outgoing);
|
|
278
313
|
}
|
|
279
314
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
315
|
+
const internalBody = getInternalBody(res);
|
|
316
|
+
if (internalBody) {
|
|
317
|
+
if (internalBody.length) {
|
|
318
|
+
resHeaderRecord["content-length"] = internalBody.length;
|
|
319
|
+
}
|
|
320
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
321
|
+
if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
|
|
322
|
+
outgoing.end(internalBody.source);
|
|
323
|
+
} else if (internalBody.source instanceof Blob) {
|
|
324
|
+
outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
|
|
325
|
+
} else {
|
|
326
|
+
await writeFromReadableStream(internalBody.stream, outgoing);
|
|
327
|
+
}
|
|
328
|
+
} else if (res.body) {
|
|
329
|
+
const {
|
|
330
|
+
"transfer-encoding": transferEncoding,
|
|
331
|
+
"content-encoding": contentEncoding,
|
|
332
|
+
"content-length": contentLength,
|
|
333
|
+
"x-accel-buffering": accelBuffering,
|
|
334
|
+
"content-type": contentType
|
|
335
|
+
} = resHeaderRecord;
|
|
336
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
337
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
338
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
339
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
340
|
+
} else {
|
|
341
|
+
const buffer = await res.arrayBuffer();
|
|
342
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
343
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
344
|
+
outgoing.end(new Uint8Array(buffer));
|
|
301
345
|
}
|
|
302
346
|
} else {
|
|
303
347
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
304
348
|
outgoing.end();
|
|
305
349
|
}
|
|
306
350
|
};
|
|
307
|
-
var getRequestListener = (fetchCallback) => {
|
|
308
|
-
return (incoming, outgoing) => {
|
|
351
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
352
|
+
return async (incoming, outgoing) => {
|
|
309
353
|
let res;
|
|
310
354
|
const req = newRequest(incoming);
|
|
355
|
+
outgoing.on("close", () => {
|
|
356
|
+
if (incoming.destroyed) {
|
|
357
|
+
req[getAbortController]().abort();
|
|
358
|
+
}
|
|
359
|
+
});
|
|
311
360
|
try {
|
|
312
361
|
res = fetchCallback(req, { incoming, outgoing });
|
|
313
362
|
if (cacheKey in res) {
|
|
@@ -315,12 +364,23 @@ var getRequestListener = (fetchCallback) => {
|
|
|
315
364
|
}
|
|
316
365
|
} catch (e) {
|
|
317
366
|
if (!res) {
|
|
318
|
-
|
|
367
|
+
if (options.errorHandler) {
|
|
368
|
+
res = await options.errorHandler(e);
|
|
369
|
+
if (!res) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
res = handleFetchError(e);
|
|
374
|
+
}
|
|
319
375
|
} else {
|
|
320
376
|
return handleResponseError(e, outgoing);
|
|
321
377
|
}
|
|
322
378
|
}
|
|
323
|
-
|
|
379
|
+
try {
|
|
380
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
381
|
+
} catch (e) {
|
|
382
|
+
return handleResponseError(e, outgoing);
|
|
383
|
+
}
|
|
324
384
|
};
|
|
325
385
|
};
|
|
326
386
|
|
package/dist/types.d.mts
CHANGED
|
@@ -37,5 +37,6 @@ type Options = {
|
|
|
37
37
|
port?: number;
|
|
38
38
|
hostname?: string;
|
|
39
39
|
} & ServerOptions;
|
|
40
|
+
type CustomErrorHandler = (err: unknown) => void | Response | Promise<void | Response>;
|
|
40
41
|
|
|
41
|
-
export { FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType };
|
|
42
|
+
export { CustomErrorHandler, FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType };
|
package/dist/types.d.ts
CHANGED
|
@@ -37,5 +37,6 @@ type Options = {
|
|
|
37
37
|
port?: number;
|
|
38
38
|
hostname?: string;
|
|
39
39
|
} & ServerOptions;
|
|
40
|
+
type CustomErrorHandler = (err: unknown) => void | Response | Promise<void | Response>;
|
|
40
41
|
|
|
41
|
-
export { FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType };
|
|
42
|
+
export { CustomErrorHandler, FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType };
|
package/dist/utils.js
CHANGED
|
@@ -76,7 +76,7 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
76
76
|
if (cookies.length > 0) {
|
|
77
77
|
res["set-cookie"] = cookies;
|
|
78
78
|
}
|
|
79
|
-
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
79
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
80
80
|
return res;
|
|
81
81
|
};
|
|
82
82
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/utils.mjs
CHANGED
package/dist/vercel.d.mts
CHANGED
|
@@ -2,6 +2,6 @@ import * as http2 from 'http2';
|
|
|
2
2
|
import * as http from 'http';
|
|
3
3
|
import { Hono } from 'hono';
|
|
4
4
|
|
|
5
|
-
declare const handle: (app: Hono<any, any, any>) => (incoming: http.IncomingMessage | http2.Http2ServerRequest, outgoing: http.ServerResponse<http.IncomingMessage> | http2.Http2ServerResponse) =>
|
|
5
|
+
declare const handle: (app: Hono<any, any, any>) => (incoming: http.IncomingMessage | http2.Http2ServerRequest, outgoing: http.ServerResponse<http.IncomingMessage> | http2.Http2ServerResponse) => Promise<void>;
|
|
6
6
|
|
|
7
7
|
export { handle };
|
package/dist/vercel.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ import * as http2 from 'http2';
|
|
|
2
2
|
import * as http from 'http';
|
|
3
3
|
import { Hono } from 'hono';
|
|
4
4
|
|
|
5
|
-
declare const handle: (app: Hono<any, any, any>) => (incoming: http.IncomingMessage | http2.Http2ServerRequest, outgoing: http.ServerResponse<http.IncomingMessage> | http2.Http2ServerResponse) =>
|
|
5
|
+
declare const handle: (app: Hono<any, any, any>) => (incoming: http.IncomingMessage | http2.Http2ServerRequest, outgoing: http.ServerResponse<http.IncomingMessage> | http2.Http2ServerResponse) => Promise<void>;
|
|
6
6
|
|
|
7
7
|
export { handle };
|
package/dist/vercel.js
CHANGED
|
@@ -53,7 +53,7 @@ var Request = class extends GlobalRequest {
|
|
|
53
53
|
Object.defineProperty(global, "Request", {
|
|
54
54
|
value: Request
|
|
55
55
|
});
|
|
56
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
56
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
57
57
|
const headerRecord = [];
|
|
58
58
|
const rawHeaders = incoming.rawHeaders;
|
|
59
59
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -65,7 +65,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
65
65
|
}
|
|
66
66
|
const init = {
|
|
67
67
|
method,
|
|
68
|
-
headers: headerRecord
|
|
68
|
+
headers: headerRecord,
|
|
69
|
+
signal: abortController.signal
|
|
69
70
|
};
|
|
70
71
|
if (!(method === "GET" || method === "HEAD")) {
|
|
71
72
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
@@ -76,6 +77,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
76
77
|
var requestCache = Symbol("requestCache");
|
|
77
78
|
var incomingKey = Symbol("incomingKey");
|
|
78
79
|
var urlKey = Symbol("urlKey");
|
|
80
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
81
|
+
var getAbortController = Symbol("getAbortController");
|
|
79
82
|
var requestPrototype = {
|
|
80
83
|
get method() {
|
|
81
84
|
return this[incomingKey].method || "GET";
|
|
@@ -83,11 +86,17 @@ var requestPrototype = {
|
|
|
83
86
|
get url() {
|
|
84
87
|
return this[urlKey];
|
|
85
88
|
},
|
|
89
|
+
[getAbortController]() {
|
|
90
|
+
this[getRequestCache]();
|
|
91
|
+
return this[abortControllerKey];
|
|
92
|
+
},
|
|
86
93
|
[getRequestCache]() {
|
|
94
|
+
this[abortControllerKey] ||= new AbortController();
|
|
87
95
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
88
96
|
this.method,
|
|
89
97
|
this[urlKey],
|
|
90
|
-
this[incomingKey]
|
|
98
|
+
this[incomingKey],
|
|
99
|
+
this[abortControllerKey]
|
|
91
100
|
);
|
|
92
101
|
}
|
|
93
102
|
};
|
|
@@ -181,18 +190,19 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
181
190
|
if (cookies.length > 0) {
|
|
182
191
|
res["set-cookie"] = cookies;
|
|
183
192
|
}
|
|
184
|
-
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
193
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
185
194
|
return res;
|
|
186
195
|
};
|
|
187
196
|
|
|
188
197
|
// src/response.ts
|
|
189
198
|
var responseCache = Symbol("responseCache");
|
|
199
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
190
200
|
var cacheKey = Symbol("cache");
|
|
191
201
|
var GlobalResponse = global.Response;
|
|
192
202
|
var Response2 = class _Response {
|
|
193
203
|
#body;
|
|
194
204
|
#init;
|
|
195
|
-
|
|
205
|
+
[getResponseCache]() {
|
|
196
206
|
delete this[cacheKey];
|
|
197
207
|
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
198
208
|
}
|
|
@@ -202,7 +212,7 @@ var Response2 = class _Response {
|
|
|
202
212
|
const cachedGlobalResponse = init[responseCache];
|
|
203
213
|
if (cachedGlobalResponse) {
|
|
204
214
|
this.#init = cachedGlobalResponse;
|
|
205
|
-
this
|
|
215
|
+
this[getResponseCache]();
|
|
206
216
|
return;
|
|
207
217
|
} else {
|
|
208
218
|
this.#init = init.#init;
|
|
@@ -211,7 +221,7 @@ var Response2 = class _Response {
|
|
|
211
221
|
this.#init = init;
|
|
212
222
|
}
|
|
213
223
|
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
214
|
-
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
224
|
+
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
215
225
|
if (headers instanceof Headers) {
|
|
216
226
|
headers = buildOutgoingHttpHeaders(headers);
|
|
217
227
|
}
|
|
@@ -234,14 +244,14 @@ var Response2 = class _Response {
|
|
|
234
244
|
].forEach((k) => {
|
|
235
245
|
Object.defineProperty(Response2.prototype, k, {
|
|
236
246
|
get() {
|
|
237
|
-
return this
|
|
247
|
+
return this[getResponseCache]()[k];
|
|
238
248
|
}
|
|
239
249
|
});
|
|
240
250
|
});
|
|
241
251
|
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
242
252
|
Object.defineProperty(Response2.prototype, k, {
|
|
243
253
|
value: function() {
|
|
244
|
-
return this
|
|
254
|
+
return this[getResponseCache]()[k]();
|
|
245
255
|
}
|
|
246
256
|
});
|
|
247
257
|
});
|
|
@@ -250,6 +260,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
|
250
260
|
Object.defineProperty(global, "Response", {
|
|
251
261
|
value: Response2
|
|
252
262
|
});
|
|
263
|
+
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
264
|
+
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
265
|
+
);
|
|
266
|
+
if (!stateKey) {
|
|
267
|
+
console.warn("Failed to find Response internal state key");
|
|
268
|
+
}
|
|
269
|
+
function getInternalBody(response) {
|
|
270
|
+
if (!stateKey) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (response instanceof Response2) {
|
|
274
|
+
response = response[getResponseCache]();
|
|
275
|
+
}
|
|
276
|
+
const state = response[stateKey];
|
|
277
|
+
return state && state.body || void 0;
|
|
278
|
+
}
|
|
253
279
|
|
|
254
280
|
// src/globals.ts
|
|
255
281
|
var import_node_crypto = __toESM(require("crypto"));
|
|
@@ -299,48 +325,71 @@ var responseViaCache = (res, outgoing) => {
|
|
|
299
325
|
);
|
|
300
326
|
}
|
|
301
327
|
};
|
|
302
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
328
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
329
|
+
if (res instanceof Promise) {
|
|
330
|
+
if (options.errorHandler) {
|
|
331
|
+
try {
|
|
332
|
+
res = await res;
|
|
333
|
+
} catch (err) {
|
|
334
|
+
const errRes = await options.errorHandler(err);
|
|
335
|
+
if (!errRes) {
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
res = errRes;
|
|
339
|
+
}
|
|
340
|
+
} else {
|
|
341
|
+
res = await res.catch(handleFetchError);
|
|
308
342
|
}
|
|
309
|
-
}
|
|
310
|
-
|
|
343
|
+
}
|
|
344
|
+
if (cacheKey in res) {
|
|
345
|
+
return responseViaCache(res, outgoing);
|
|
311
346
|
}
|
|
312
347
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
348
|
+
const internalBody = getInternalBody(res);
|
|
349
|
+
if (internalBody) {
|
|
350
|
+
if (internalBody.length) {
|
|
351
|
+
resHeaderRecord["content-length"] = internalBody.length;
|
|
352
|
+
}
|
|
353
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
354
|
+
if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
|
|
355
|
+
outgoing.end(internalBody.source);
|
|
356
|
+
} else if (internalBody.source instanceof Blob) {
|
|
357
|
+
outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
|
|
358
|
+
} else {
|
|
359
|
+
await writeFromReadableStream(internalBody.stream, outgoing);
|
|
360
|
+
}
|
|
361
|
+
} else if (res.body) {
|
|
362
|
+
const {
|
|
363
|
+
"transfer-encoding": transferEncoding,
|
|
364
|
+
"content-encoding": contentEncoding,
|
|
365
|
+
"content-length": contentLength,
|
|
366
|
+
"x-accel-buffering": accelBuffering,
|
|
367
|
+
"content-type": contentType
|
|
368
|
+
} = resHeaderRecord;
|
|
369
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
370
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
371
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
372
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
373
|
+
} else {
|
|
374
|
+
const buffer = await res.arrayBuffer();
|
|
375
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
376
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
377
|
+
outgoing.end(new Uint8Array(buffer));
|
|
334
378
|
}
|
|
335
379
|
} else {
|
|
336
380
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
337
381
|
outgoing.end();
|
|
338
382
|
}
|
|
339
383
|
};
|
|
340
|
-
var getRequestListener = (fetchCallback) => {
|
|
341
|
-
return (incoming, outgoing) => {
|
|
384
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
385
|
+
return async (incoming, outgoing) => {
|
|
342
386
|
let res;
|
|
343
387
|
const req = newRequest(incoming);
|
|
388
|
+
outgoing.on("close", () => {
|
|
389
|
+
if (incoming.destroyed) {
|
|
390
|
+
req[getAbortController]().abort();
|
|
391
|
+
}
|
|
392
|
+
});
|
|
344
393
|
try {
|
|
345
394
|
res = fetchCallback(req, { incoming, outgoing });
|
|
346
395
|
if (cacheKey in res) {
|
|
@@ -348,12 +397,23 @@ var getRequestListener = (fetchCallback) => {
|
|
|
348
397
|
}
|
|
349
398
|
} catch (e) {
|
|
350
399
|
if (!res) {
|
|
351
|
-
|
|
400
|
+
if (options.errorHandler) {
|
|
401
|
+
res = await options.errorHandler(e);
|
|
402
|
+
if (!res) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
} else {
|
|
406
|
+
res = handleFetchError(e);
|
|
407
|
+
}
|
|
352
408
|
} else {
|
|
353
409
|
return handleResponseError(e, outgoing);
|
|
354
410
|
}
|
|
355
411
|
}
|
|
356
|
-
|
|
412
|
+
try {
|
|
413
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
414
|
+
} catch (e) {
|
|
415
|
+
return handleResponseError(e, outgoing);
|
|
416
|
+
}
|
|
357
417
|
};
|
|
358
418
|
};
|
|
359
419
|
|