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