@hono/node-server 1.5.0 → 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 +34 -14
- package/dist/index.mjs +34 -14
- package/dist/listener.js +34 -14
- package/dist/listener.mjs +34 -14
- 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 +34 -14
- package/dist/server.mjs +34 -14
- 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 +34 -14
- package/dist/vercel.mjs +34 -14
- 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
|
@@ -42,6 +42,22 @@ var import_node_http = require("http");
|
|
|
42
42
|
// src/request.ts
|
|
43
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
63
|
const rawHeaders = incoming.rawHeaders;
|
|
@@ -58,7 +74,6 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
58
74
|
};
|
|
59
75
|
if (!(method === "GET" || method === "HEAD")) {
|
|
60
76
|
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
61
|
-
init.duplex = "half";
|
|
62
77
|
}
|
|
63
78
|
return new Request(url, init);
|
|
64
79
|
};
|
|
@@ -108,7 +123,7 @@ var requestPrototype = {
|
|
|
108
123
|
}
|
|
109
124
|
});
|
|
110
125
|
});
|
|
111
|
-
Object.setPrototypeOf(requestPrototype,
|
|
126
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
112
127
|
var newRequest = (incoming) => {
|
|
113
128
|
const req = Object.create(requestPrototype);
|
|
114
129
|
req[incomingKey] = incoming;
|
|
@@ -137,8 +152,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
137
152
|
function cancel(error) {
|
|
138
153
|
reader.cancel(error).catch(() => {
|
|
139
154
|
});
|
|
140
|
-
if (error)
|
|
155
|
+
if (error) {
|
|
141
156
|
writable.destroy(error);
|
|
157
|
+
}
|
|
142
158
|
}
|
|
143
159
|
function onDrain() {
|
|
144
160
|
reader.read().then(flow, cancel);
|
|
@@ -242,9 +258,6 @@ Object.defineProperty(global, "Response", {
|
|
|
242
258
|
|
|
243
259
|
// src/globals.ts
|
|
244
260
|
var import_node_crypto = __toESM(require("crypto"));
|
|
245
|
-
Object.defineProperty(global, "Response", {
|
|
246
|
-
value: Response2
|
|
247
|
-
});
|
|
248
261
|
var webFetch = global.fetch;
|
|
249
262
|
if (typeof global.crypto === "undefined") {
|
|
250
263
|
global.crypto = import_node_crypto.default;
|
|
@@ -271,8 +284,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
271
284
|
console.info("The user aborted a request.");
|
|
272
285
|
} else {
|
|
273
286
|
console.error(e);
|
|
274
|
-
if (!outgoing.headersSent)
|
|
287
|
+
if (!outgoing.headersSent) {
|
|
275
288
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
289
|
+
}
|
|
276
290
|
outgoing.end(`Error: ${err.message}`);
|
|
277
291
|
outgoing.destroy(err);
|
|
278
292
|
}
|
|
@@ -291,11 +305,10 @@ var responseViaCache = (res, outgoing) => {
|
|
|
291
305
|
}
|
|
292
306
|
};
|
|
293
307
|
var responseViaResponseObject = async (res, outgoing) => {
|
|
294
|
-
|
|
295
|
-
res = await res.catch(handleFetchError);
|
|
296
|
-
}
|
|
308
|
+
res = res instanceof Promise ? await res.catch(handleFetchError) : res;
|
|
297
309
|
try {
|
|
298
|
-
|
|
310
|
+
const isCached = cacheKey in res;
|
|
311
|
+
if (isCached) {
|
|
299
312
|
return responseViaCache(res, outgoing);
|
|
300
313
|
}
|
|
301
314
|
} catch (e) {
|
|
@@ -304,8 +317,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
304
317
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
305
318
|
if (res.body) {
|
|
306
319
|
try {
|
|
307
|
-
|
|
308
|
-
|
|
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)) {
|
|
309
329
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
310
330
|
await writeFromReadableStream(res.body, outgoing);
|
|
311
331
|
} else {
|
|
@@ -327,7 +347,7 @@ var getRequestListener = (fetchCallback) => {
|
|
|
327
347
|
let res;
|
|
328
348
|
const req = newRequest(incoming);
|
|
329
349
|
try {
|
|
330
|
-
res = fetchCallback(req);
|
|
350
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
331
351
|
if (cacheKey in res) {
|
|
332
352
|
return responseViaCache(res, outgoing);
|
|
333
353
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -4,6 +4,22 @@ import { createServer as createServerHTTP } from "http";
|
|
|
4
4
|
// src/request.ts
|
|
5
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
25
|
const rawHeaders = incoming.rawHeaders;
|
|
@@ -20,7 +36,6 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
20
36
|
};
|
|
21
37
|
if (!(method === "GET" || method === "HEAD")) {
|
|
22
38
|
init.body = Readable.toWeb(incoming);
|
|
23
|
-
init.duplex = "half";
|
|
24
39
|
}
|
|
25
40
|
return new Request(url, init);
|
|
26
41
|
};
|
|
@@ -70,7 +85,7 @@ var requestPrototype = {
|
|
|
70
85
|
}
|
|
71
86
|
});
|
|
72
87
|
});
|
|
73
|
-
Object.setPrototypeOf(requestPrototype,
|
|
88
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
74
89
|
var newRequest = (incoming) => {
|
|
75
90
|
const req = Object.create(requestPrototype);
|
|
76
91
|
req[incomingKey] = incoming;
|
|
@@ -99,8 +114,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
99
114
|
function cancel(error) {
|
|
100
115
|
reader.cancel(error).catch(() => {
|
|
101
116
|
});
|
|
102
|
-
if (error)
|
|
117
|
+
if (error) {
|
|
103
118
|
writable.destroy(error);
|
|
119
|
+
}
|
|
104
120
|
}
|
|
105
121
|
function onDrain() {
|
|
106
122
|
reader.read().then(flow, cancel);
|
|
@@ -204,9 +220,6 @@ Object.defineProperty(global, "Response", {
|
|
|
204
220
|
|
|
205
221
|
// src/globals.ts
|
|
206
222
|
import crypto from "crypto";
|
|
207
|
-
Object.defineProperty(global, "Response", {
|
|
208
|
-
value: Response2
|
|
209
|
-
});
|
|
210
223
|
var webFetch = global.fetch;
|
|
211
224
|
if (typeof global.crypto === "undefined") {
|
|
212
225
|
global.crypto = crypto;
|
|
@@ -233,8 +246,9 @@ var handleResponseError = (e, outgoing) => {
|
|
|
233
246
|
console.info("The user aborted a request.");
|
|
234
247
|
} else {
|
|
235
248
|
console.error(e);
|
|
236
|
-
if (!outgoing.headersSent)
|
|
249
|
+
if (!outgoing.headersSent) {
|
|
237
250
|
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
251
|
+
}
|
|
238
252
|
outgoing.end(`Error: ${err.message}`);
|
|
239
253
|
outgoing.destroy(err);
|
|
240
254
|
}
|
|
@@ -253,11 +267,10 @@ var responseViaCache = (res, outgoing) => {
|
|
|
253
267
|
}
|
|
254
268
|
};
|
|
255
269
|
var responseViaResponseObject = async (res, outgoing) => {
|
|
256
|
-
|
|
257
|
-
res = await res.catch(handleFetchError);
|
|
258
|
-
}
|
|
270
|
+
res = res instanceof Promise ? await res.catch(handleFetchError) : res;
|
|
259
271
|
try {
|
|
260
|
-
|
|
272
|
+
const isCached = cacheKey in res;
|
|
273
|
+
if (isCached) {
|
|
261
274
|
return responseViaCache(res, outgoing);
|
|
262
275
|
}
|
|
263
276
|
} catch (e) {
|
|
@@ -266,8 +279,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
266
279
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
267
280
|
if (res.body) {
|
|
268
281
|
try {
|
|
269
|
-
|
|
270
|
-
|
|
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)) {
|
|
271
291
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
272
292
|
await writeFromReadableStream(res.body, outgoing);
|
|
273
293
|
} else {
|
|
@@ -289,7 +309,7 @@ var getRequestListener = (fetchCallback) => {
|
|
|
289
309
|
let res;
|
|
290
310
|
const req = newRequest(incoming);
|
|
291
311
|
try {
|
|
292
|
-
res = fetchCallback(req);
|
|
312
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
293
313
|
if (cacheKey in res) {
|
|
294
314
|
return responseViaCache(res, outgoing);
|
|
295
315
|
}
|
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
|
}
|
|
@@ -286,11 +300,10 @@ var responseViaCache = (res, outgoing) => {
|
|
|
286
300
|
}
|
|
287
301
|
};
|
|
288
302
|
var responseViaResponseObject = async (res, outgoing) => {
|
|
289
|
-
|
|
290
|
-
res = await res.catch(handleFetchError);
|
|
291
|
-
}
|
|
303
|
+
res = res instanceof Promise ? await res.catch(handleFetchError) : res;
|
|
292
304
|
try {
|
|
293
|
-
|
|
305
|
+
const isCached = cacheKey in res;
|
|
306
|
+
if (isCached) {
|
|
294
307
|
return responseViaCache(res, outgoing);
|
|
295
308
|
}
|
|
296
309
|
} catch (e) {
|
|
@@ -299,8 +312,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
299
312
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
300
313
|
if (res.body) {
|
|
301
314
|
try {
|
|
302
|
-
|
|
303
|
-
|
|
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)) {
|
|
304
324
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
305
325
|
await writeFromReadableStream(res.body, outgoing);
|
|
306
326
|
} else {
|
|
@@ -322,7 +342,7 @@ var getRequestListener = (fetchCallback) => {
|
|
|
322
342
|
let res;
|
|
323
343
|
const req = newRequest(incoming);
|
|
324
344
|
try {
|
|
325
|
-
res = fetchCallback(req);
|
|
345
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
326
346
|
if (cacheKey in res) {
|
|
327
347
|
return responseViaCache(res, outgoing);
|
|
328
348
|
}
|
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
|
}
|
|
@@ -250,11 +264,10 @@ var responseViaCache = (res, outgoing) => {
|
|
|
250
264
|
}
|
|
251
265
|
};
|
|
252
266
|
var responseViaResponseObject = async (res, outgoing) => {
|
|
253
|
-
|
|
254
|
-
res = await res.catch(handleFetchError);
|
|
255
|
-
}
|
|
267
|
+
res = res instanceof Promise ? await res.catch(handleFetchError) : res;
|
|
256
268
|
try {
|
|
257
|
-
|
|
269
|
+
const isCached = cacheKey in res;
|
|
270
|
+
if (isCached) {
|
|
258
271
|
return responseViaCache(res, outgoing);
|
|
259
272
|
}
|
|
260
273
|
} catch (e) {
|
|
@@ -263,8 +276,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
263
276
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
264
277
|
if (res.body) {
|
|
265
278
|
try {
|
|
266
|
-
|
|
267
|
-
|
|
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)) {
|
|
268
288
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
269
289
|
await writeFromReadableStream(res.body, outgoing);
|
|
270
290
|
} else {
|
|
@@ -286,7 +306,7 @@ var getRequestListener = (fetchCallback) => {
|
|
|
286
306
|
let res;
|
|
287
307
|
const req = newRequest(incoming);
|
|
288
308
|
try {
|
|
289
|
-
res = fetchCallback(req);
|
|
309
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
290
310
|
if (cacheKey in res) {
|
|
291
311
|
return responseViaCache(res, outgoing);
|
|
292
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 };
|