@hono/node-server 1.5.0 → 1.7.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/globals.js +0 -89
- package/dist/globals.mjs +0 -89
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +58 -17
- package/dist/index.mjs +58 -17
- package/dist/listener.d.mts +4 -2
- package/dist/listener.d.ts +4 -2
- package/dist/listener.js +58 -17
- package/dist/listener.mjs +58 -17
- package/dist/request.d.mts +8 -1
- package/dist/request.d.ts +8 -1
- package/dist/request.js +21 -2
- package/dist/request.mjs +19 -2
- package/dist/serve-static.js +4 -2
- package/dist/serve-static.mjs +4 -2
- package/dist/server.js +58 -17
- package/dist/server.mjs +58 -17
- package/dist/types.d.mts +13 -4
- package/dist/types.d.ts +13 -4
- package/dist/utils.js +2 -1
- package/dist/utils.mjs +2 -1
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +58 -17
- package/dist/vercel.mjs +58 -17
- package/package.json +6 -3
package/dist/listener.js
CHANGED
|
@@ -37,6 +37,22 @@ module.exports = __toCommonJS(listener_exports);
|
|
|
37
37
|
// src/request.ts
|
|
38
38
|
var import_node_http2 = require("http2");
|
|
39
39
|
var import_node_stream = require("stream");
|
|
40
|
+
var GlobalRequest = global.Request;
|
|
41
|
+
var Request = class extends GlobalRequest {
|
|
42
|
+
constructor(input, options) {
|
|
43
|
+
if (typeof input === "object" && getRequestCache in input) {
|
|
44
|
+
input = input[getRequestCache]();
|
|
45
|
+
}
|
|
46
|
+
if (options?.body instanceof ReadableStream) {
|
|
47
|
+
;
|
|
48
|
+
options.duplex = "half";
|
|
49
|
+
}
|
|
50
|
+
super(input, options);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(global, "Request", {
|
|
54
|
+
value: Request
|
|
55
|
+
});
|
|
40
56
|
var newRequestFromIncoming = (method, url, incoming) => {
|
|
41
57
|
const headerRecord = [];
|
|
42
58
|
const rawHeaders = incoming.rawHeaders;
|
|
@@ -53,7 +69,6 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
53
69
|
};
|
|
54
70
|
if (!(method === "GET" || method === "HEAD")) {
|
|
55
71
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
56
|
-
init.duplex = "half";
|
|
57
72
|
}
|
|
58
73
|
return new Request(url, init);
|
|
59
74
|
};
|
|
@@ -103,7 +118,7 @@ var requestPrototype = {
|
|
|
103
118
|
}
|
|
104
119
|
});
|
|
105
120
|
});
|
|
106
|
-
Object.setPrototypeOf(requestPrototype,
|
|
121
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
107
122
|
var newRequest = (incoming) => {
|
|
108
123
|
const req = Object.create(requestPrototype);
|
|
109
124
|
req[incomingKey] = incoming;
|
|
@@ -132,8 +147,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
132
147
|
function cancel(error) {
|
|
133
148
|
reader.cancel(error).catch(() => {
|
|
134
149
|
});
|
|
135
|
-
if (error)
|
|
150
|
+
if (error) {
|
|
136
151
|
writable.destroy(error);
|
|
152
|
+
}
|
|
137
153
|
}
|
|
138
154
|
function onDrain() {
|
|
139
155
|
reader.read().then(flow, cancel);
|
|
@@ -237,9 +253,6 @@ Object.defineProperty(global, "Response", {
|
|
|
237
253
|
|
|
238
254
|
// src/globals.ts
|
|
239
255
|
var import_node_crypto = __toESM(require("crypto"));
|
|
240
|
-
Object.defineProperty(global, "Response", {
|
|
241
|
-
value: Response2
|
|
242
|
-
});
|
|
243
256
|
var webFetch = global.fetch;
|
|
244
257
|
if (typeof global.crypto === "undefined") {
|
|
245
258
|
global.crypto = import_node_crypto.default;
|
|
@@ -266,8 +279,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
266
279
|
console.info("The user aborted a request.");
|
|
267
280
|
} else {
|
|
268
281
|
console.error(e);
|
|
269
|
-
if (!outgoing.headersSent)
|
|
282
|
+
if (!outgoing.headersSent) {
|
|
270
283
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
284
|
+
}
|
|
271
285
|
outgoing.end(`Error: ${err.message}`);
|
|
272
286
|
outgoing.destroy(err);
|
|
273
287
|
}
|
|
@@ -285,12 +299,25 @@ var responseViaCache = (res, outgoing) => {
|
|
|
285
299
|
);
|
|
286
300
|
}
|
|
287
301
|
};
|
|
288
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
302
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
289
303
|
if (res instanceof Promise) {
|
|
290
|
-
|
|
304
|
+
if (options.errorHandler) {
|
|
305
|
+
try {
|
|
306
|
+
res = await res;
|
|
307
|
+
} catch (err) {
|
|
308
|
+
const errRes = await options.errorHandler(err);
|
|
309
|
+
if (!errRes) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
res = errRes;
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
res = await res.catch(handleFetchError);
|
|
316
|
+
}
|
|
291
317
|
}
|
|
292
318
|
try {
|
|
293
|
-
|
|
319
|
+
const isCached = cacheKey in res;
|
|
320
|
+
if (isCached) {
|
|
294
321
|
return responseViaCache(res, outgoing);
|
|
295
322
|
}
|
|
296
323
|
} catch (e) {
|
|
@@ -299,8 +326,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
299
326
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
300
327
|
if (res.body) {
|
|
301
328
|
try {
|
|
302
|
-
|
|
303
|
-
|
|
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)) {
|
|
304
338
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
305
339
|
await writeFromReadableStream(res.body, outgoing);
|
|
306
340
|
} else {
|
|
@@ -317,23 +351,30 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
317
351
|
outgoing.end();
|
|
318
352
|
}
|
|
319
353
|
};
|
|
320
|
-
var getRequestListener = (fetchCallback) => {
|
|
321
|
-
return (incoming, outgoing) => {
|
|
354
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
355
|
+
return async (incoming, outgoing) => {
|
|
322
356
|
let res;
|
|
323
357
|
const req = newRequest(incoming);
|
|
324
358
|
try {
|
|
325
|
-
res = fetchCallback(req);
|
|
359
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
326
360
|
if (cacheKey in res) {
|
|
327
361
|
return responseViaCache(res, outgoing);
|
|
328
362
|
}
|
|
329
363
|
} catch (e) {
|
|
330
364
|
if (!res) {
|
|
331
|
-
|
|
365
|
+
if (options.errorHandler) {
|
|
366
|
+
res = await options.errorHandler(e);
|
|
367
|
+
if (!res) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
} else {
|
|
371
|
+
res = handleFetchError(e);
|
|
372
|
+
}
|
|
332
373
|
} else {
|
|
333
374
|
return handleResponseError(e, outgoing);
|
|
334
375
|
}
|
|
335
376
|
}
|
|
336
|
-
return responseViaResponseObject(res, outgoing);
|
|
377
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
337
378
|
};
|
|
338
379
|
};
|
|
339
380
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/listener.mjs
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
// src/request.ts
|
|
2
2
|
import { Http2ServerRequest } from "http2";
|
|
3
3
|
import { Readable } from "stream";
|
|
4
|
+
var GlobalRequest = global.Request;
|
|
5
|
+
var Request = class extends GlobalRequest {
|
|
6
|
+
constructor(input, options) {
|
|
7
|
+
if (typeof input === "object" && getRequestCache in input) {
|
|
8
|
+
input = input[getRequestCache]();
|
|
9
|
+
}
|
|
10
|
+
if (options?.body instanceof ReadableStream) {
|
|
11
|
+
;
|
|
12
|
+
options.duplex = "half";
|
|
13
|
+
}
|
|
14
|
+
super(input, options);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(global, "Request", {
|
|
18
|
+
value: Request
|
|
19
|
+
});
|
|
4
20
|
var newRequestFromIncoming = (method, url, incoming) => {
|
|
5
21
|
const headerRecord = [];
|
|
6
22
|
const rawHeaders = incoming.rawHeaders;
|
|
@@ -17,7 +33,6 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
17
33
|
};
|
|
18
34
|
if (!(method === "GET" || method === "HEAD")) {
|
|
19
35
|
init.body = Readable.toWeb(incoming);
|
|
20
|
-
init.duplex = "half";
|
|
21
36
|
}
|
|
22
37
|
return new Request(url, init);
|
|
23
38
|
};
|
|
@@ -67,7 +82,7 @@ var requestPrototype = {
|
|
|
67
82
|
}
|
|
68
83
|
});
|
|
69
84
|
});
|
|
70
|
-
Object.setPrototypeOf(requestPrototype,
|
|
85
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
71
86
|
var newRequest = (incoming) => {
|
|
72
87
|
const req = Object.create(requestPrototype);
|
|
73
88
|
req[incomingKey] = incoming;
|
|
@@ -96,8 +111,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
96
111
|
function cancel(error) {
|
|
97
112
|
reader.cancel(error).catch(() => {
|
|
98
113
|
});
|
|
99
|
-
if (error)
|
|
114
|
+
if (error) {
|
|
100
115
|
writable.destroy(error);
|
|
116
|
+
}
|
|
101
117
|
}
|
|
102
118
|
function onDrain() {
|
|
103
119
|
reader.read().then(flow, cancel);
|
|
@@ -201,9 +217,6 @@ Object.defineProperty(global, "Response", {
|
|
|
201
217
|
|
|
202
218
|
// src/globals.ts
|
|
203
219
|
import crypto from "crypto";
|
|
204
|
-
Object.defineProperty(global, "Response", {
|
|
205
|
-
value: Response2
|
|
206
|
-
});
|
|
207
220
|
var webFetch = global.fetch;
|
|
208
221
|
if (typeof global.crypto === "undefined") {
|
|
209
222
|
global.crypto = crypto;
|
|
@@ -230,8 +243,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
230
243
|
console.info("The user aborted a request.");
|
|
231
244
|
} else {
|
|
232
245
|
console.error(e);
|
|
233
|
-
if (!outgoing.headersSent)
|
|
246
|
+
if (!outgoing.headersSent) {
|
|
234
247
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
248
|
+
}
|
|
235
249
|
outgoing.end(`Error: ${err.message}`);
|
|
236
250
|
outgoing.destroy(err);
|
|
237
251
|
}
|
|
@@ -249,12 +263,25 @@ var responseViaCache = (res, outgoing) => {
|
|
|
249
263
|
);
|
|
250
264
|
}
|
|
251
265
|
};
|
|
252
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
266
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
253
267
|
if (res instanceof Promise) {
|
|
254
|
-
|
|
268
|
+
if (options.errorHandler) {
|
|
269
|
+
try {
|
|
270
|
+
res = await res;
|
|
271
|
+
} catch (err) {
|
|
272
|
+
const errRes = await options.errorHandler(err);
|
|
273
|
+
if (!errRes) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
res = errRes;
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
res = await res.catch(handleFetchError);
|
|
280
|
+
}
|
|
255
281
|
}
|
|
256
282
|
try {
|
|
257
|
-
|
|
283
|
+
const isCached = cacheKey in res;
|
|
284
|
+
if (isCached) {
|
|
258
285
|
return responseViaCache(res, outgoing);
|
|
259
286
|
}
|
|
260
287
|
} catch (e) {
|
|
@@ -263,8 +290,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
263
290
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
264
291
|
if (res.body) {
|
|
265
292
|
try {
|
|
266
|
-
|
|
267
|
-
|
|
293
|
+
const {
|
|
294
|
+
"transfer-encoding": transferEncoding,
|
|
295
|
+
"content-encoding": contentEncoding,
|
|
296
|
+
"content-length": contentLength,
|
|
297
|
+
"x-accel-buffering": accelBuffering,
|
|
298
|
+
"content-type": contentType
|
|
299
|
+
} = resHeaderRecord;
|
|
300
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
301
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
268
302
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
269
303
|
await writeFromReadableStream(res.body, outgoing);
|
|
270
304
|
} else {
|
|
@@ -281,23 +315,30 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
281
315
|
outgoing.end();
|
|
282
316
|
}
|
|
283
317
|
};
|
|
284
|
-
var getRequestListener = (fetchCallback) => {
|
|
285
|
-
return (incoming, outgoing) => {
|
|
318
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
319
|
+
return async (incoming, outgoing) => {
|
|
286
320
|
let res;
|
|
287
321
|
const req = newRequest(incoming);
|
|
288
322
|
try {
|
|
289
|
-
res = fetchCallback(req);
|
|
323
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
290
324
|
if (cacheKey in res) {
|
|
291
325
|
return responseViaCache(res, outgoing);
|
|
292
326
|
}
|
|
293
327
|
} catch (e) {
|
|
294
328
|
if (!res) {
|
|
295
|
-
|
|
329
|
+
if (options.errorHandler) {
|
|
330
|
+
res = await options.errorHandler(e);
|
|
331
|
+
if (!res) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
} else {
|
|
335
|
+
res = handleFetchError(e);
|
|
336
|
+
}
|
|
296
337
|
} else {
|
|
297
338
|
return handleResponseError(e, outgoing);
|
|
298
339
|
}
|
|
299
340
|
}
|
|
300
|
-
return responseViaResponseObject(res, outgoing);
|
|
341
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
301
342
|
};
|
|
302
343
|
};
|
|
303
344
|
export {
|
package/dist/request.d.mts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { IncomingMessage } from 'node:http';
|
|
2
2
|
import { Http2ServerRequest } from 'node:http2';
|
|
3
3
|
|
|
4
|
+
declare const GlobalRequest: {
|
|
5
|
+
new (input: RequestInfo | URL, init?: RequestInit | undefined): globalThis.Request;
|
|
6
|
+
prototype: globalThis.Request;
|
|
7
|
+
};
|
|
8
|
+
declare class Request extends GlobalRequest {
|
|
9
|
+
constructor(input: string | Request, options?: RequestInit);
|
|
10
|
+
}
|
|
4
11
|
declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest) => any;
|
|
5
12
|
|
|
6
|
-
export { newRequest };
|
|
13
|
+
export { GlobalRequest, Request, newRequest };
|
package/dist/request.d.ts
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { IncomingMessage } from 'node:http';
|
|
2
2
|
import { Http2ServerRequest } from 'node:http2';
|
|
3
3
|
|
|
4
|
+
declare const GlobalRequest: {
|
|
5
|
+
new (input: RequestInfo | URL, init?: RequestInit | undefined): globalThis.Request;
|
|
6
|
+
prototype: globalThis.Request;
|
|
7
|
+
};
|
|
8
|
+
declare class Request extends GlobalRequest {
|
|
9
|
+
constructor(input: string | Request, options?: RequestInit);
|
|
10
|
+
}
|
|
4
11
|
declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest) => any;
|
|
5
12
|
|
|
6
|
-
export { newRequest };
|
|
13
|
+
export { GlobalRequest, Request, newRequest };
|
package/dist/request.js
CHANGED
|
@@ -20,11 +20,29 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/request.ts
|
|
21
21
|
var request_exports = {};
|
|
22
22
|
__export(request_exports, {
|
|
23
|
+
GlobalRequest: () => GlobalRequest,
|
|
24
|
+
Request: () => Request,
|
|
23
25
|
newRequest: () => newRequest
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(request_exports);
|
|
26
28
|
var import_node_http2 = require("http2");
|
|
27
29
|
var import_node_stream = require("stream");
|
|
30
|
+
var GlobalRequest = global.Request;
|
|
31
|
+
var Request = class extends GlobalRequest {
|
|
32
|
+
constructor(input, options) {
|
|
33
|
+
if (typeof input === "object" && getRequestCache in input) {
|
|
34
|
+
input = input[getRequestCache]();
|
|
35
|
+
}
|
|
36
|
+
if (options?.body instanceof ReadableStream) {
|
|
37
|
+
;
|
|
38
|
+
options.duplex = "half";
|
|
39
|
+
}
|
|
40
|
+
super(input, options);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(global, "Request", {
|
|
44
|
+
value: Request
|
|
45
|
+
});
|
|
28
46
|
var newRequestFromIncoming = (method, url, incoming) => {
|
|
29
47
|
const headerRecord = [];
|
|
30
48
|
const rawHeaders = incoming.rawHeaders;
|
|
@@ -41,7 +59,6 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
41
59
|
};
|
|
42
60
|
if (!(method === "GET" || method === "HEAD")) {
|
|
43
61
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
44
|
-
init.duplex = "half";
|
|
45
62
|
}
|
|
46
63
|
return new Request(url, init);
|
|
47
64
|
};
|
|
@@ -91,7 +108,7 @@ var requestPrototype = {
|
|
|
91
108
|
}
|
|
92
109
|
});
|
|
93
110
|
});
|
|
94
|
-
Object.setPrototypeOf(requestPrototype,
|
|
111
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
95
112
|
var newRequest = (incoming) => {
|
|
96
113
|
const req = Object.create(requestPrototype);
|
|
97
114
|
req[incomingKey] = incoming;
|
|
@@ -102,5 +119,7 @@ var newRequest = (incoming) => {
|
|
|
102
119
|
};
|
|
103
120
|
// Annotate the CommonJS export names for ESM import in node:
|
|
104
121
|
0 && (module.exports = {
|
|
122
|
+
GlobalRequest,
|
|
123
|
+
Request,
|
|
105
124
|
newRequest
|
|
106
125
|
});
|
package/dist/request.mjs
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
// src/request.ts
|
|
2
2
|
import { Http2ServerRequest } from "http2";
|
|
3
3
|
import { Readable } from "stream";
|
|
4
|
+
var GlobalRequest = global.Request;
|
|
5
|
+
var Request = class extends GlobalRequest {
|
|
6
|
+
constructor(input, options) {
|
|
7
|
+
if (typeof input === "object" && getRequestCache in input) {
|
|
8
|
+
input = input[getRequestCache]();
|
|
9
|
+
}
|
|
10
|
+
if (options?.body instanceof ReadableStream) {
|
|
11
|
+
;
|
|
12
|
+
options.duplex = "half";
|
|
13
|
+
}
|
|
14
|
+
super(input, options);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(global, "Request", {
|
|
18
|
+
value: Request
|
|
19
|
+
});
|
|
4
20
|
var newRequestFromIncoming = (method, url, incoming) => {
|
|
5
21
|
const headerRecord = [];
|
|
6
22
|
const rawHeaders = incoming.rawHeaders;
|
|
@@ -17,7 +33,6 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
17
33
|
};
|
|
18
34
|
if (!(method === "GET" || method === "HEAD")) {
|
|
19
35
|
init.body = Readable.toWeb(incoming);
|
|
20
|
-
init.duplex = "half";
|
|
21
36
|
}
|
|
22
37
|
return new Request(url, init);
|
|
23
38
|
};
|
|
@@ -67,7 +82,7 @@ var requestPrototype = {
|
|
|
67
82
|
}
|
|
68
83
|
});
|
|
69
84
|
});
|
|
70
|
-
Object.setPrototypeOf(requestPrototype,
|
|
85
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
71
86
|
var newRequest = (incoming) => {
|
|
72
87
|
const req = Object.create(requestPrototype);
|
|
73
88
|
req[incomingKey] = incoming;
|
|
@@ -77,5 +92,7 @@ var newRequest = (incoming) => {
|
|
|
77
92
|
return req;
|
|
78
93
|
};
|
|
79
94
|
export {
|
|
95
|
+
GlobalRequest,
|
|
96
|
+
Request,
|
|
80
97
|
newRequest
|
|
81
98
|
};
|
package/dist/serve-static.js
CHANGED
|
@@ -158,8 +158,9 @@ var createStreamBody = (stream) => {
|
|
|
158
158
|
};
|
|
159
159
|
var serveStatic = (options = { root: "" }) => {
|
|
160
160
|
return async (c, next) => {
|
|
161
|
-
if (c.finalized)
|
|
161
|
+
if (c.finalized) {
|
|
162
162
|
return next();
|
|
163
|
+
}
|
|
163
164
|
const url = new URL(c.req.url);
|
|
164
165
|
const filename = options.path ?? decodeURIComponent(url.pathname);
|
|
165
166
|
let path = getFilePath({
|
|
@@ -167,8 +168,9 @@ var serveStatic = (options = { root: "" }) => {
|
|
|
167
168
|
root: options.root,
|
|
168
169
|
defaultDocument: options.index ?? "index.html"
|
|
169
170
|
});
|
|
170
|
-
if (!path)
|
|
171
|
+
if (!path) {
|
|
171
172
|
return next();
|
|
173
|
+
}
|
|
172
174
|
path = `./${path}`;
|
|
173
175
|
if (!(0, import_fs.existsSync)(path)) {
|
|
174
176
|
await options.onNotFound?.(path, c);
|
package/dist/serve-static.mjs
CHANGED
|
@@ -134,8 +134,9 @@ var createStreamBody = (stream) => {
|
|
|
134
134
|
};
|
|
135
135
|
var serveStatic = (options = { root: "" }) => {
|
|
136
136
|
return async (c, next) => {
|
|
137
|
-
if (c.finalized)
|
|
137
|
+
if (c.finalized) {
|
|
138
138
|
return next();
|
|
139
|
+
}
|
|
139
140
|
const url = new URL(c.req.url);
|
|
140
141
|
const filename = options.path ?? decodeURIComponent(url.pathname);
|
|
141
142
|
let path = getFilePath({
|
|
@@ -143,8 +144,9 @@ var serveStatic = (options = { root: "" }) => {
|
|
|
143
144
|
root: options.root,
|
|
144
145
|
defaultDocument: options.index ?? "index.html"
|
|
145
146
|
});
|
|
146
|
-
if (!path)
|
|
147
|
+
if (!path) {
|
|
147
148
|
return next();
|
|
149
|
+
}
|
|
148
150
|
path = `./${path}`;
|
|
149
151
|
if (!existsSync(path)) {
|
|
150
152
|
await options.onNotFound?.(path, c);
|
package/dist/server.js
CHANGED
|
@@ -39,6 +39,22 @@ var import_node_http = require("http");
|
|
|
39
39
|
// src/request.ts
|
|
40
40
|
var import_node_http2 = require("http2");
|
|
41
41
|
var import_node_stream = require("stream");
|
|
42
|
+
var GlobalRequest = global.Request;
|
|
43
|
+
var Request = class extends GlobalRequest {
|
|
44
|
+
constructor(input, options) {
|
|
45
|
+
if (typeof input === "object" && getRequestCache in input) {
|
|
46
|
+
input = input[getRequestCache]();
|
|
47
|
+
}
|
|
48
|
+
if (options?.body instanceof ReadableStream) {
|
|
49
|
+
;
|
|
50
|
+
options.duplex = "half";
|
|
51
|
+
}
|
|
52
|
+
super(input, options);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
Object.defineProperty(global, "Request", {
|
|
56
|
+
value: Request
|
|
57
|
+
});
|
|
42
58
|
var newRequestFromIncoming = (method, url, incoming) => {
|
|
43
59
|
const headerRecord = [];
|
|
44
60
|
const rawHeaders = incoming.rawHeaders;
|
|
@@ -55,7 +71,6 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
55
71
|
};
|
|
56
72
|
if (!(method === "GET" || method === "HEAD")) {
|
|
57
73
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
58
|
-
init.duplex = "half";
|
|
59
74
|
}
|
|
60
75
|
return new Request(url, init);
|
|
61
76
|
};
|
|
@@ -105,7 +120,7 @@ var requestPrototype = {
|
|
|
105
120
|
}
|
|
106
121
|
});
|
|
107
122
|
});
|
|
108
|
-
Object.setPrototypeOf(requestPrototype,
|
|
123
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
109
124
|
var newRequest = (incoming) => {
|
|
110
125
|
const req = Object.create(requestPrototype);
|
|
111
126
|
req[incomingKey] = incoming;
|
|
@@ -134,8 +149,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
134
149
|
function cancel(error) {
|
|
135
150
|
reader.cancel(error).catch(() => {
|
|
136
151
|
});
|
|
137
|
-
if (error)
|
|
152
|
+
if (error) {
|
|
138
153
|
writable.destroy(error);
|
|
154
|
+
}
|
|
139
155
|
}
|
|
140
156
|
function onDrain() {
|
|
141
157
|
reader.read().then(flow, cancel);
|
|
@@ -239,9 +255,6 @@ Object.defineProperty(global, "Response", {
|
|
|
239
255
|
|
|
240
256
|
// src/globals.ts
|
|
241
257
|
var import_node_crypto = __toESM(require("crypto"));
|
|
242
|
-
Object.defineProperty(global, "Response", {
|
|
243
|
-
value: Response2
|
|
244
|
-
});
|
|
245
258
|
var webFetch = global.fetch;
|
|
246
259
|
if (typeof global.crypto === "undefined") {
|
|
247
260
|
global.crypto = import_node_crypto.default;
|
|
@@ -268,8 +281,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
268
281
|
console.info("The user aborted a request.");
|
|
269
282
|
} else {
|
|
270
283
|
console.error(e);
|
|
271
|
-
if (!outgoing.headersSent)
|
|
284
|
+
if (!outgoing.headersSent) {
|
|
272
285
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
286
|
+
}
|
|
273
287
|
outgoing.end(`Error: ${err.message}`);
|
|
274
288
|
outgoing.destroy(err);
|
|
275
289
|
}
|
|
@@ -287,12 +301,25 @@ var responseViaCache = (res, outgoing) => {
|
|
|
287
301
|
);
|
|
288
302
|
}
|
|
289
303
|
};
|
|
290
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
304
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
291
305
|
if (res instanceof Promise) {
|
|
292
|
-
|
|
306
|
+
if (options.errorHandler) {
|
|
307
|
+
try {
|
|
308
|
+
res = await res;
|
|
309
|
+
} catch (err) {
|
|
310
|
+
const errRes = await options.errorHandler(err);
|
|
311
|
+
if (!errRes) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
res = errRes;
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
res = await res.catch(handleFetchError);
|
|
318
|
+
}
|
|
293
319
|
}
|
|
294
320
|
try {
|
|
295
|
-
|
|
321
|
+
const isCached = cacheKey in res;
|
|
322
|
+
if (isCached) {
|
|
296
323
|
return responseViaCache(res, outgoing);
|
|
297
324
|
}
|
|
298
325
|
} catch (e) {
|
|
@@ -301,8 +328,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
301
328
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
302
329
|
if (res.body) {
|
|
303
330
|
try {
|
|
304
|
-
|
|
305
|
-
|
|
331
|
+
const {
|
|
332
|
+
"transfer-encoding": transferEncoding,
|
|
333
|
+
"content-encoding": contentEncoding,
|
|
334
|
+
"content-length": contentLength,
|
|
335
|
+
"x-accel-buffering": accelBuffering,
|
|
336
|
+
"content-type": contentType
|
|
337
|
+
} = resHeaderRecord;
|
|
338
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
339
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
306
340
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
307
341
|
await writeFromReadableStream(res.body, outgoing);
|
|
308
342
|
} else {
|
|
@@ -319,23 +353,30 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
319
353
|
outgoing.end();
|
|
320
354
|
}
|
|
321
355
|
};
|
|
322
|
-
var getRequestListener = (fetchCallback) => {
|
|
323
|
-
return (incoming, outgoing) => {
|
|
356
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
357
|
+
return async (incoming, outgoing) => {
|
|
324
358
|
let res;
|
|
325
359
|
const req = newRequest(incoming);
|
|
326
360
|
try {
|
|
327
|
-
res = fetchCallback(req);
|
|
361
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
328
362
|
if (cacheKey in res) {
|
|
329
363
|
return responseViaCache(res, outgoing);
|
|
330
364
|
}
|
|
331
365
|
} catch (e) {
|
|
332
366
|
if (!res) {
|
|
333
|
-
|
|
367
|
+
if (options.errorHandler) {
|
|
368
|
+
res = await options.errorHandler(e);
|
|
369
|
+
if (!res) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
res = handleFetchError(e);
|
|
374
|
+
}
|
|
334
375
|
} else {
|
|
335
376
|
return handleResponseError(e, outgoing);
|
|
336
377
|
}
|
|
337
378
|
}
|
|
338
|
-
return responseViaResponseObject(res, outgoing);
|
|
379
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
339
380
|
};
|
|
340
381
|
};
|
|
341
382
|
|