@hono/node-server 1.4.1 → 1.6.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/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 +52 -26
- package/dist/index.mjs +52 -26
- package/dist/listener.js +52 -26
- package/dist/listener.mjs +52 -26
- package/dist/request.d.mts +8 -1
- package/dist/request.d.ts +8 -1
- package/dist/request.js +39 -14
- package/dist/request.mjs +37 -14
- package/dist/serve-static.js +4 -2
- package/dist/serve-static.mjs +4 -2
- package/dist/server.js +52 -26
- package/dist/server.mjs +52 -26
- package/dist/types.d.mts +12 -4
- package/dist/types.d.ts +12 -4
- package/dist/utils.js +2 -1
- package/dist/utils.mjs +2 -1
- package/dist/vercel.js +52 -26
- package/dist/vercel.mjs +52 -26
- package/package.json +6 -3
package/dist/listener.mjs
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
// src/request.ts
|
|
2
|
-
import {
|
|
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
|
-
const
|
|
7
|
-
for (let i = 0; i <
|
|
8
|
-
|
|
22
|
+
const rawHeaders = incoming.rawHeaders;
|
|
23
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
24
|
+
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
25
|
+
if (key.charCodeAt(0) !== /*:*/
|
|
26
|
+
58) {
|
|
27
|
+
headerRecord.push([key, value]);
|
|
28
|
+
}
|
|
9
29
|
}
|
|
10
30
|
const init = {
|
|
11
31
|
method,
|
|
@@ -13,28 +33,26 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
13
33
|
};
|
|
14
34
|
if (!(method === "GET" || method === "HEAD")) {
|
|
15
35
|
init.body = Readable.toWeb(incoming);
|
|
16
|
-
init.duplex = "half";
|
|
17
36
|
}
|
|
18
37
|
return new Request(url, init);
|
|
19
38
|
};
|
|
20
39
|
var getRequestCache = Symbol("getRequestCache");
|
|
21
40
|
var requestCache = Symbol("requestCache");
|
|
22
41
|
var incomingKey = Symbol("incomingKey");
|
|
42
|
+
var urlKey = Symbol("urlKey");
|
|
23
43
|
var requestPrototype = {
|
|
24
44
|
get method() {
|
|
25
45
|
return this[incomingKey].method || "GET";
|
|
26
46
|
},
|
|
27
47
|
get url() {
|
|
28
|
-
|
|
29
|
-
if (!path) {
|
|
30
|
-
const originalPath = this[incomingKey].url;
|
|
31
|
-
path = /\.\./.test(originalPath) ? resolve(originalPath) : originalPath;
|
|
32
|
-
this[incomingKey]["path"] = path;
|
|
33
|
-
}
|
|
34
|
-
return `http://${this[incomingKey].headers.host}${path}`;
|
|
48
|
+
return this[urlKey];
|
|
35
49
|
},
|
|
36
50
|
[getRequestCache]() {
|
|
37
|
-
return this[requestCache] ||= newRequestFromIncoming(
|
|
51
|
+
return this[requestCache] ||= newRequestFromIncoming(
|
|
52
|
+
this.method,
|
|
53
|
+
this[urlKey],
|
|
54
|
+
this[incomingKey]
|
|
55
|
+
);
|
|
38
56
|
}
|
|
39
57
|
};
|
|
40
58
|
[
|
|
@@ -64,10 +82,13 @@ var requestPrototype = {
|
|
|
64
82
|
}
|
|
65
83
|
});
|
|
66
84
|
});
|
|
67
|
-
Object.setPrototypeOf(requestPrototype,
|
|
85
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
68
86
|
var newRequest = (incoming) => {
|
|
69
87
|
const req = Object.create(requestPrototype);
|
|
70
88
|
req[incomingKey] = incoming;
|
|
89
|
+
req[urlKey] = new URL(
|
|
90
|
+
`http://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
|
|
91
|
+
).href;
|
|
71
92
|
return req;
|
|
72
93
|
};
|
|
73
94
|
|
|
@@ -90,8 +111,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
90
111
|
function cancel(error) {
|
|
91
112
|
reader.cancel(error).catch(() => {
|
|
92
113
|
});
|
|
93
|
-
if (error)
|
|
114
|
+
if (error) {
|
|
94
115
|
writable.destroy(error);
|
|
116
|
+
}
|
|
95
117
|
}
|
|
96
118
|
function onDrain() {
|
|
97
119
|
reader.read().then(flow, cancel);
|
|
@@ -195,9 +217,6 @@ Object.defineProperty(global, "Response", {
|
|
|
195
217
|
|
|
196
218
|
// src/globals.ts
|
|
197
219
|
import crypto from "crypto";
|
|
198
|
-
Object.defineProperty(global, "Response", {
|
|
199
|
-
value: Response2
|
|
200
|
-
});
|
|
201
220
|
var webFetch = global.fetch;
|
|
202
221
|
if (typeof global.crypto === "undefined") {
|
|
203
222
|
global.crypto = crypto;
|
|
@@ -224,8 +243,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
224
243
|
console.info("The user aborted a request.");
|
|
225
244
|
} else {
|
|
226
245
|
console.error(e);
|
|
227
|
-
if (!outgoing.headersSent)
|
|
246
|
+
if (!outgoing.headersSent) {
|
|
228
247
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
248
|
+
}
|
|
229
249
|
outgoing.end(`Error: ${err.message}`);
|
|
230
250
|
outgoing.destroy(err);
|
|
231
251
|
}
|
|
@@ -244,11 +264,10 @@ var responseViaCache = (res, outgoing) => {
|
|
|
244
264
|
}
|
|
245
265
|
};
|
|
246
266
|
var responseViaResponseObject = async (res, outgoing) => {
|
|
247
|
-
|
|
248
|
-
res = await res.catch(handleFetchError);
|
|
249
|
-
}
|
|
267
|
+
res = res instanceof Promise ? await res.catch(handleFetchError) : res;
|
|
250
268
|
try {
|
|
251
|
-
|
|
269
|
+
const isCached = cacheKey in res;
|
|
270
|
+
if (isCached) {
|
|
252
271
|
return responseViaCache(res, outgoing);
|
|
253
272
|
}
|
|
254
273
|
} catch (e) {
|
|
@@ -257,8 +276,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
257
276
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
258
277
|
if (res.body) {
|
|
259
278
|
try {
|
|
260
|
-
|
|
261
|
-
|
|
279
|
+
const {
|
|
280
|
+
"transfer-encoding": transferEncoding,
|
|
281
|
+
"content-encoding": contentEncoding,
|
|
282
|
+
"content-length": contentLength,
|
|
283
|
+
"x-accel-buffering": accelBuffering,
|
|
284
|
+
"content-type": contentType
|
|
285
|
+
} = resHeaderRecord;
|
|
286
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
287
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
262
288
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
263
289
|
await writeFromReadableStream(res.body, outgoing);
|
|
264
290
|
} else {
|
|
@@ -280,7 +306,7 @@ var getRequestListener = (fetchCallback) => {
|
|
|
280
306
|
let res;
|
|
281
307
|
const req = newRequest(incoming);
|
|
282
308
|
try {
|
|
283
|
-
res = fetchCallback(req);
|
|
309
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
284
310
|
if (cacheKey in res) {
|
|
285
311
|
return responseViaCache(res, outgoing);
|
|
286
312
|
}
|
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,16 +20,38 @@ 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
|
-
var
|
|
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
|
-
const
|
|
31
|
-
for (let i = 0; i <
|
|
32
|
-
|
|
48
|
+
const rawHeaders = incoming.rawHeaders;
|
|
49
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
50
|
+
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
51
|
+
if (key.charCodeAt(0) !== /*:*/
|
|
52
|
+
58) {
|
|
53
|
+
headerRecord.push([key, value]);
|
|
54
|
+
}
|
|
33
55
|
}
|
|
34
56
|
const init = {
|
|
35
57
|
method,
|
|
@@ -37,28 +59,26 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
37
59
|
};
|
|
38
60
|
if (!(method === "GET" || method === "HEAD")) {
|
|
39
61
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
40
|
-
init.duplex = "half";
|
|
41
62
|
}
|
|
42
63
|
return new Request(url, init);
|
|
43
64
|
};
|
|
44
65
|
var getRequestCache = Symbol("getRequestCache");
|
|
45
66
|
var requestCache = Symbol("requestCache");
|
|
46
67
|
var incomingKey = Symbol("incomingKey");
|
|
68
|
+
var urlKey = Symbol("urlKey");
|
|
47
69
|
var requestPrototype = {
|
|
48
70
|
get method() {
|
|
49
71
|
return this[incomingKey].method || "GET";
|
|
50
72
|
},
|
|
51
73
|
get url() {
|
|
52
|
-
|
|
53
|
-
if (!path) {
|
|
54
|
-
const originalPath = this[incomingKey].url;
|
|
55
|
-
path = /\.\./.test(originalPath) ? (0, import_node_path.resolve)(originalPath) : originalPath;
|
|
56
|
-
this[incomingKey]["path"] = path;
|
|
57
|
-
}
|
|
58
|
-
return `http://${this[incomingKey].headers.host}${path}`;
|
|
74
|
+
return this[urlKey];
|
|
59
75
|
},
|
|
60
76
|
[getRequestCache]() {
|
|
61
|
-
return this[requestCache] ||= newRequestFromIncoming(
|
|
77
|
+
return this[requestCache] ||= newRequestFromIncoming(
|
|
78
|
+
this.method,
|
|
79
|
+
this[urlKey],
|
|
80
|
+
this[incomingKey]
|
|
81
|
+
);
|
|
62
82
|
}
|
|
63
83
|
};
|
|
64
84
|
[
|
|
@@ -88,13 +108,18 @@ var requestPrototype = {
|
|
|
88
108
|
}
|
|
89
109
|
});
|
|
90
110
|
});
|
|
91
|
-
Object.setPrototypeOf(requestPrototype,
|
|
111
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
92
112
|
var newRequest = (incoming) => {
|
|
93
113
|
const req = Object.create(requestPrototype);
|
|
94
114
|
req[incomingKey] = incoming;
|
|
115
|
+
req[urlKey] = new URL(
|
|
116
|
+
`http://${incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
|
|
117
|
+
).href;
|
|
95
118
|
return req;
|
|
96
119
|
};
|
|
97
120
|
// Annotate the CommonJS export names for ESM import in node:
|
|
98
121
|
0 && (module.exports = {
|
|
122
|
+
GlobalRequest,
|
|
123
|
+
Request,
|
|
99
124
|
newRequest
|
|
100
125
|
});
|
package/dist/request.mjs
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
// src/request.ts
|
|
2
|
-
import {
|
|
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
|
-
const
|
|
7
|
-
for (let i = 0; i <
|
|
8
|
-
|
|
22
|
+
const rawHeaders = incoming.rawHeaders;
|
|
23
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
24
|
+
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
25
|
+
if (key.charCodeAt(0) !== /*:*/
|
|
26
|
+
58) {
|
|
27
|
+
headerRecord.push([key, value]);
|
|
28
|
+
}
|
|
9
29
|
}
|
|
10
30
|
const init = {
|
|
11
31
|
method,
|
|
@@ -13,28 +33,26 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
13
33
|
};
|
|
14
34
|
if (!(method === "GET" || method === "HEAD")) {
|
|
15
35
|
init.body = Readable.toWeb(incoming);
|
|
16
|
-
init.duplex = "half";
|
|
17
36
|
}
|
|
18
37
|
return new Request(url, init);
|
|
19
38
|
};
|
|
20
39
|
var getRequestCache = Symbol("getRequestCache");
|
|
21
40
|
var requestCache = Symbol("requestCache");
|
|
22
41
|
var incomingKey = Symbol("incomingKey");
|
|
42
|
+
var urlKey = Symbol("urlKey");
|
|
23
43
|
var requestPrototype = {
|
|
24
44
|
get method() {
|
|
25
45
|
return this[incomingKey].method || "GET";
|
|
26
46
|
},
|
|
27
47
|
get url() {
|
|
28
|
-
|
|
29
|
-
if (!path) {
|
|
30
|
-
const originalPath = this[incomingKey].url;
|
|
31
|
-
path = /\.\./.test(originalPath) ? resolve(originalPath) : originalPath;
|
|
32
|
-
this[incomingKey]["path"] = path;
|
|
33
|
-
}
|
|
34
|
-
return `http://${this[incomingKey].headers.host}${path}`;
|
|
48
|
+
return this[urlKey];
|
|
35
49
|
},
|
|
36
50
|
[getRequestCache]() {
|
|
37
|
-
return this[requestCache] ||= newRequestFromIncoming(
|
|
51
|
+
return this[requestCache] ||= newRequestFromIncoming(
|
|
52
|
+
this.method,
|
|
53
|
+
this[urlKey],
|
|
54
|
+
this[incomingKey]
|
|
55
|
+
);
|
|
38
56
|
}
|
|
39
57
|
};
|
|
40
58
|
[
|
|
@@ -64,12 +82,17 @@ var requestPrototype = {
|
|
|
64
82
|
}
|
|
65
83
|
});
|
|
66
84
|
});
|
|
67
|
-
Object.setPrototypeOf(requestPrototype,
|
|
85
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
68
86
|
var newRequest = (incoming) => {
|
|
69
87
|
const req = Object.create(requestPrototype);
|
|
70
88
|
req[incomingKey] = incoming;
|
|
89
|
+
req[urlKey] = new URL(
|
|
90
|
+
`http://${incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
|
|
91
|
+
).href;
|
|
71
92
|
return req;
|
|
72
93
|
};
|
|
73
94
|
export {
|
|
95
|
+
GlobalRequest,
|
|
96
|
+
Request,
|
|
74
97
|
newRequest
|
|
75
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
|
@@ -37,13 +37,33 @@ module.exports = __toCommonJS(server_exports);
|
|
|
37
37
|
var import_node_http = require("http");
|
|
38
38
|
|
|
39
39
|
// src/request.ts
|
|
40
|
-
var
|
|
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
|
-
const
|
|
45
|
-
for (let i = 0; i <
|
|
46
|
-
|
|
60
|
+
const rawHeaders = incoming.rawHeaders;
|
|
61
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
62
|
+
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
63
|
+
if (key.charCodeAt(0) !== /*:*/
|
|
64
|
+
58) {
|
|
65
|
+
headerRecord.push([key, value]);
|
|
66
|
+
}
|
|
47
67
|
}
|
|
48
68
|
const init = {
|
|
49
69
|
method,
|
|
@@ -51,28 +71,26 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
51
71
|
};
|
|
52
72
|
if (!(method === "GET" || method === "HEAD")) {
|
|
53
73
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
54
|
-
init.duplex = "half";
|
|
55
74
|
}
|
|
56
75
|
return new Request(url, init);
|
|
57
76
|
};
|
|
58
77
|
var getRequestCache = Symbol("getRequestCache");
|
|
59
78
|
var requestCache = Symbol("requestCache");
|
|
60
79
|
var incomingKey = Symbol("incomingKey");
|
|
80
|
+
var urlKey = Symbol("urlKey");
|
|
61
81
|
var requestPrototype = {
|
|
62
82
|
get method() {
|
|
63
83
|
return this[incomingKey].method || "GET";
|
|
64
84
|
},
|
|
65
85
|
get url() {
|
|
66
|
-
|
|
67
|
-
if (!path) {
|
|
68
|
-
const originalPath = this[incomingKey].url;
|
|
69
|
-
path = /\.\./.test(originalPath) ? (0, import_node_path.resolve)(originalPath) : originalPath;
|
|
70
|
-
this[incomingKey]["path"] = path;
|
|
71
|
-
}
|
|
72
|
-
return `http://${this[incomingKey].headers.host}${path}`;
|
|
86
|
+
return this[urlKey];
|
|
73
87
|
},
|
|
74
88
|
[getRequestCache]() {
|
|
75
|
-
return this[requestCache] ||= newRequestFromIncoming(
|
|
89
|
+
return this[requestCache] ||= newRequestFromIncoming(
|
|
90
|
+
this.method,
|
|
91
|
+
this[urlKey],
|
|
92
|
+
this[incomingKey]
|
|
93
|
+
);
|
|
76
94
|
}
|
|
77
95
|
};
|
|
78
96
|
[
|
|
@@ -102,10 +120,13 @@ var requestPrototype = {
|
|
|
102
120
|
}
|
|
103
121
|
});
|
|
104
122
|
});
|
|
105
|
-
Object.setPrototypeOf(requestPrototype,
|
|
123
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
106
124
|
var newRequest = (incoming) => {
|
|
107
125
|
const req = Object.create(requestPrototype);
|
|
108
126
|
req[incomingKey] = incoming;
|
|
127
|
+
req[urlKey] = new URL(
|
|
128
|
+
`http://${incoming instanceof import_node_http2.Http2ServerRequest ? incoming.authority : incoming.headers.host}${incoming.url}`
|
|
129
|
+
).href;
|
|
109
130
|
return req;
|
|
110
131
|
};
|
|
111
132
|
|
|
@@ -128,8 +149,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
128
149
|
function cancel(error) {
|
|
129
150
|
reader.cancel(error).catch(() => {
|
|
130
151
|
});
|
|
131
|
-
if (error)
|
|
152
|
+
if (error) {
|
|
132
153
|
writable.destroy(error);
|
|
154
|
+
}
|
|
133
155
|
}
|
|
134
156
|
function onDrain() {
|
|
135
157
|
reader.read().then(flow, cancel);
|
|
@@ -233,9 +255,6 @@ Object.defineProperty(global, "Response", {
|
|
|
233
255
|
|
|
234
256
|
// src/globals.ts
|
|
235
257
|
var import_node_crypto = __toESM(require("crypto"));
|
|
236
|
-
Object.defineProperty(global, "Response", {
|
|
237
|
-
value: Response2
|
|
238
|
-
});
|
|
239
258
|
var webFetch = global.fetch;
|
|
240
259
|
if (typeof global.crypto === "undefined") {
|
|
241
260
|
global.crypto = import_node_crypto.default;
|
|
@@ -262,8 +281,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
262
281
|
console.info("The user aborted a request.");
|
|
263
282
|
} else {
|
|
264
283
|
console.error(e);
|
|
265
|
-
if (!outgoing.headersSent)
|
|
284
|
+
if (!outgoing.headersSent) {
|
|
266
285
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
286
|
+
}
|
|
267
287
|
outgoing.end(`Error: ${err.message}`);
|
|
268
288
|
outgoing.destroy(err);
|
|
269
289
|
}
|
|
@@ -282,11 +302,10 @@ var responseViaCache = (res, outgoing) => {
|
|
|
282
302
|
}
|
|
283
303
|
};
|
|
284
304
|
var responseViaResponseObject = async (res, outgoing) => {
|
|
285
|
-
|
|
286
|
-
res = await res.catch(handleFetchError);
|
|
287
|
-
}
|
|
305
|
+
res = res instanceof Promise ? await res.catch(handleFetchError) : res;
|
|
288
306
|
try {
|
|
289
|
-
|
|
307
|
+
const isCached = cacheKey in res;
|
|
308
|
+
if (isCached) {
|
|
290
309
|
return responseViaCache(res, outgoing);
|
|
291
310
|
}
|
|
292
311
|
} catch (e) {
|
|
@@ -295,8 +314,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
295
314
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
296
315
|
if (res.body) {
|
|
297
316
|
try {
|
|
298
|
-
|
|
299
|
-
|
|
317
|
+
const {
|
|
318
|
+
"transfer-encoding": transferEncoding,
|
|
319
|
+
"content-encoding": contentEncoding,
|
|
320
|
+
"content-length": contentLength,
|
|
321
|
+
"x-accel-buffering": accelBuffering,
|
|
322
|
+
"content-type": contentType
|
|
323
|
+
} = resHeaderRecord;
|
|
324
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
325
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
300
326
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
301
327
|
await writeFromReadableStream(res.body, outgoing);
|
|
302
328
|
} else {
|
|
@@ -318,7 +344,7 @@ var getRequestListener = (fetchCallback) => {
|
|
|
318
344
|
let res;
|
|
319
345
|
const req = newRequest(incoming);
|
|
320
346
|
try {
|
|
321
|
-
res = fetchCallback(req);
|
|
347
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
322
348
|
if (cacheKey in res) {
|
|
323
349
|
return responseViaCache(res, outgoing);
|
|
324
350
|
}
|