@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/README.md
CHANGED
|
@@ -178,6 +178,40 @@ app.use(
|
|
|
178
178
|
)
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
+
## Accessing Node.js API
|
|
182
|
+
|
|
183
|
+
You can access the Node.js API from `c.env` in Node.js. For example, if you want to specify a type, you can write the following.
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import { serve } from '@hono/node-server'
|
|
187
|
+
import type { HttpBindings } from '@hono/node-server'
|
|
188
|
+
import { Hono } from 'hono'
|
|
189
|
+
|
|
190
|
+
const app = new Hono<{ Bindings: HttpBindings }>()
|
|
191
|
+
|
|
192
|
+
app.get('/', (c) => {
|
|
193
|
+
return c.json({
|
|
194
|
+
remoteAddress: c.env.incoming.socket.remoteAddress,
|
|
195
|
+
})
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
serve(app)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
The APIs that you can get from `c.env` are as follows.
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
type HttpBindings = {
|
|
205
|
+
incoming: IncomingMessage
|
|
206
|
+
outgoing: ServerResponse
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
type Http2Bindings = {
|
|
210
|
+
incoming: Http2ServerRequest
|
|
211
|
+
outgoing: Http2ServerResponse
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
181
215
|
## Related projects
|
|
182
216
|
|
|
183
217
|
- Hono - <https://hono.dev>
|
package/dist/index.js
CHANGED
|
@@ -58,7 +58,7 @@ var Request = class extends GlobalRequest {
|
|
|
58
58
|
Object.defineProperty(global, "Request", {
|
|
59
59
|
value: Request
|
|
60
60
|
});
|
|
61
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
61
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
62
62
|
const headerRecord = [];
|
|
63
63
|
const rawHeaders = incoming.rawHeaders;
|
|
64
64
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -70,7 +70,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
70
70
|
}
|
|
71
71
|
const init = {
|
|
72
72
|
method,
|
|
73
|
-
headers: headerRecord
|
|
73
|
+
headers: headerRecord,
|
|
74
|
+
signal: abortController.signal
|
|
74
75
|
};
|
|
75
76
|
if (!(method === "GET" || method === "HEAD")) {
|
|
76
77
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
@@ -81,6 +82,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
81
82
|
var requestCache = Symbol("requestCache");
|
|
82
83
|
var incomingKey = Symbol("incomingKey");
|
|
83
84
|
var urlKey = Symbol("urlKey");
|
|
85
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
86
|
+
var getAbortController = Symbol("getAbortController");
|
|
84
87
|
var requestPrototype = {
|
|
85
88
|
get method() {
|
|
86
89
|
return this[incomingKey].method || "GET";
|
|
@@ -88,11 +91,17 @@ var requestPrototype = {
|
|
|
88
91
|
get url() {
|
|
89
92
|
return this[urlKey];
|
|
90
93
|
},
|
|
94
|
+
[getAbortController]() {
|
|
95
|
+
this[getRequestCache]();
|
|
96
|
+
return this[abortControllerKey];
|
|
97
|
+
},
|
|
91
98
|
[getRequestCache]() {
|
|
99
|
+
this[abortControllerKey] ||= new AbortController();
|
|
92
100
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
93
101
|
this.method,
|
|
94
102
|
this[urlKey],
|
|
95
|
-
this[incomingKey]
|
|
103
|
+
this[incomingKey],
|
|
104
|
+
this[abortControllerKey]
|
|
96
105
|
);
|
|
97
106
|
}
|
|
98
107
|
};
|
|
@@ -186,18 +195,19 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
186
195
|
if (cookies.length > 0) {
|
|
187
196
|
res["set-cookie"] = cookies;
|
|
188
197
|
}
|
|
189
|
-
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
198
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
190
199
|
return res;
|
|
191
200
|
};
|
|
192
201
|
|
|
193
202
|
// src/response.ts
|
|
194
203
|
var responseCache = Symbol("responseCache");
|
|
204
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
195
205
|
var cacheKey = Symbol("cache");
|
|
196
206
|
var GlobalResponse = global.Response;
|
|
197
207
|
var Response2 = class _Response {
|
|
198
208
|
#body;
|
|
199
209
|
#init;
|
|
200
|
-
|
|
210
|
+
[getResponseCache]() {
|
|
201
211
|
delete this[cacheKey];
|
|
202
212
|
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
203
213
|
}
|
|
@@ -207,7 +217,7 @@ var Response2 = class _Response {
|
|
|
207
217
|
const cachedGlobalResponse = init[responseCache];
|
|
208
218
|
if (cachedGlobalResponse) {
|
|
209
219
|
this.#init = cachedGlobalResponse;
|
|
210
|
-
this
|
|
220
|
+
this[getResponseCache]();
|
|
211
221
|
return;
|
|
212
222
|
} else {
|
|
213
223
|
this.#init = init.#init;
|
|
@@ -216,7 +226,7 @@ var Response2 = class _Response {
|
|
|
216
226
|
this.#init = init;
|
|
217
227
|
}
|
|
218
228
|
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
219
|
-
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
229
|
+
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
220
230
|
if (headers instanceof Headers) {
|
|
221
231
|
headers = buildOutgoingHttpHeaders(headers);
|
|
222
232
|
}
|
|
@@ -239,14 +249,14 @@ var Response2 = class _Response {
|
|
|
239
249
|
].forEach((k) => {
|
|
240
250
|
Object.defineProperty(Response2.prototype, k, {
|
|
241
251
|
get() {
|
|
242
|
-
return this
|
|
252
|
+
return this[getResponseCache]()[k];
|
|
243
253
|
}
|
|
244
254
|
});
|
|
245
255
|
});
|
|
246
256
|
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
247
257
|
Object.defineProperty(Response2.prototype, k, {
|
|
248
258
|
value: function() {
|
|
249
|
-
return this
|
|
259
|
+
return this[getResponseCache]()[k]();
|
|
250
260
|
}
|
|
251
261
|
});
|
|
252
262
|
});
|
|
@@ -255,6 +265,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
|
255
265
|
Object.defineProperty(global, "Response", {
|
|
256
266
|
value: Response2
|
|
257
267
|
});
|
|
268
|
+
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
269
|
+
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
270
|
+
);
|
|
271
|
+
if (!stateKey) {
|
|
272
|
+
console.warn("Failed to find Response internal state key");
|
|
273
|
+
}
|
|
274
|
+
function getInternalBody(response) {
|
|
275
|
+
if (!stateKey) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (response instanceof Response2) {
|
|
279
|
+
response = response[getResponseCache]();
|
|
280
|
+
}
|
|
281
|
+
const state = response[stateKey];
|
|
282
|
+
return state && state.body || void 0;
|
|
283
|
+
}
|
|
258
284
|
|
|
259
285
|
// src/globals.ts
|
|
260
286
|
var import_node_crypto = __toESM(require("crypto"));
|
|
@@ -304,48 +330,71 @@ var responseViaCache = (res, outgoing) => {
|
|
|
304
330
|
);
|
|
305
331
|
}
|
|
306
332
|
};
|
|
307
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
333
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
334
|
+
if (res instanceof Promise) {
|
|
335
|
+
if (options.errorHandler) {
|
|
336
|
+
try {
|
|
337
|
+
res = await res;
|
|
338
|
+
} catch (err) {
|
|
339
|
+
const errRes = await options.errorHandler(err);
|
|
340
|
+
if (!errRes) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
res = errRes;
|
|
344
|
+
}
|
|
345
|
+
} else {
|
|
346
|
+
res = await res.catch(handleFetchError);
|
|
313
347
|
}
|
|
314
|
-
}
|
|
315
|
-
|
|
348
|
+
}
|
|
349
|
+
if (cacheKey in res) {
|
|
350
|
+
return responseViaCache(res, outgoing);
|
|
316
351
|
}
|
|
317
352
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
353
|
+
const internalBody = getInternalBody(res);
|
|
354
|
+
if (internalBody) {
|
|
355
|
+
if (internalBody.length) {
|
|
356
|
+
resHeaderRecord["content-length"] = internalBody.length;
|
|
357
|
+
}
|
|
358
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
359
|
+
if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
|
|
360
|
+
outgoing.end(internalBody.source);
|
|
361
|
+
} else if (internalBody.source instanceof Blob) {
|
|
362
|
+
outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
|
|
363
|
+
} else {
|
|
364
|
+
await writeFromReadableStream(internalBody.stream, outgoing);
|
|
365
|
+
}
|
|
366
|
+
} else if (res.body) {
|
|
367
|
+
const {
|
|
368
|
+
"transfer-encoding": transferEncoding,
|
|
369
|
+
"content-encoding": contentEncoding,
|
|
370
|
+
"content-length": contentLength,
|
|
371
|
+
"x-accel-buffering": accelBuffering,
|
|
372
|
+
"content-type": contentType
|
|
373
|
+
} = resHeaderRecord;
|
|
374
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
375
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
376
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
377
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
378
|
+
} else {
|
|
379
|
+
const buffer = await res.arrayBuffer();
|
|
380
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
381
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
382
|
+
outgoing.end(new Uint8Array(buffer));
|
|
339
383
|
}
|
|
340
384
|
} else {
|
|
341
385
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
342
386
|
outgoing.end();
|
|
343
387
|
}
|
|
344
388
|
};
|
|
345
|
-
var getRequestListener = (fetchCallback) => {
|
|
346
|
-
return (incoming, outgoing) => {
|
|
389
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
390
|
+
return async (incoming, outgoing) => {
|
|
347
391
|
let res;
|
|
348
392
|
const req = newRequest(incoming);
|
|
393
|
+
outgoing.on("close", () => {
|
|
394
|
+
if (incoming.destroyed) {
|
|
395
|
+
req[getAbortController]().abort();
|
|
396
|
+
}
|
|
397
|
+
});
|
|
349
398
|
try {
|
|
350
399
|
res = fetchCallback(req, { incoming, outgoing });
|
|
351
400
|
if (cacheKey in res) {
|
|
@@ -353,12 +402,23 @@ var getRequestListener = (fetchCallback) => {
|
|
|
353
402
|
}
|
|
354
403
|
} catch (e) {
|
|
355
404
|
if (!res) {
|
|
356
|
-
|
|
405
|
+
if (options.errorHandler) {
|
|
406
|
+
res = await options.errorHandler(e);
|
|
407
|
+
if (!res) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
res = handleFetchError(e);
|
|
412
|
+
}
|
|
357
413
|
} else {
|
|
358
414
|
return handleResponseError(e, outgoing);
|
|
359
415
|
}
|
|
360
416
|
}
|
|
361
|
-
|
|
417
|
+
try {
|
|
418
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
419
|
+
} catch (e) {
|
|
420
|
+
return handleResponseError(e, outgoing);
|
|
421
|
+
}
|
|
362
422
|
};
|
|
363
423
|
};
|
|
364
424
|
|
package/dist/index.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/listener.d.mts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
2
|
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
3
|
-
import { FetchCallback } from './types.mjs';
|
|
3
|
+
import { FetchCallback, CustomErrorHandler } from './types.mjs';
|
|
4
4
|
import 'node:https';
|
|
5
5
|
|
|
6
|
-
declare const getRequestListener: (fetchCallback: FetchCallback
|
|
6
|
+
declare const getRequestListener: (fetchCallback: FetchCallback, options?: {
|
|
7
|
+
errorHandler?: CustomErrorHandler;
|
|
8
|
+
}) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
|
|
7
9
|
|
|
8
10
|
export { getRequestListener };
|
package/dist/listener.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
2
|
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
3
|
-
import { FetchCallback } from './types.js';
|
|
3
|
+
import { FetchCallback, CustomErrorHandler } from './types.js';
|
|
4
4
|
import 'node:https';
|
|
5
5
|
|
|
6
|
-
declare const getRequestListener: (fetchCallback: FetchCallback
|
|
6
|
+
declare const getRequestListener: (fetchCallback: FetchCallback, options?: {
|
|
7
|
+
errorHandler?: CustomErrorHandler;
|
|
8
|
+
}) => (incoming: IncomingMessage | Http2ServerRequest, outgoing: ServerResponse | Http2ServerResponse) => Promise<void>;
|
|
7
9
|
|
|
8
10
|
export { getRequestListener };
|