@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/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
|
}
|
|
@@ -288,11 +302,10 @@ var responseViaCache = (res, outgoing) => {
|
|
|
288
302
|
}
|
|
289
303
|
};
|
|
290
304
|
var responseViaResponseObject = async (res, outgoing) => {
|
|
291
|
-
|
|
292
|
-
res = await res.catch(handleFetchError);
|
|
293
|
-
}
|
|
305
|
+
res = res instanceof Promise ? await res.catch(handleFetchError) : res;
|
|
294
306
|
try {
|
|
295
|
-
|
|
307
|
+
const isCached = cacheKey in res;
|
|
308
|
+
if (isCached) {
|
|
296
309
|
return responseViaCache(res, outgoing);
|
|
297
310
|
}
|
|
298
311
|
} catch (e) {
|
|
@@ -301,8 +314,15 @@ var responseViaResponseObject = async (res, outgoing) => {
|
|
|
301
314
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
302
315
|
if (res.body) {
|
|
303
316
|
try {
|
|
304
|
-
|
|
305
|
-
|
|
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)) {
|
|
306
326
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
307
327
|
await writeFromReadableStream(res.body, outgoing);
|
|
308
328
|
} else {
|
|
@@ -324,7 +344,7 @@ var getRequestListener = (fetchCallback) => {
|
|
|
324
344
|
let res;
|
|
325
345
|
const req = newRequest(incoming);
|
|
326
346
|
try {
|
|
327
|
-
res = fetchCallback(req);
|
|
347
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
328
348
|
if (cacheKey in res) {
|
|
329
349
|
return responseViaCache(res, outgoing);
|
|
330
350
|
}
|
package/dist/server.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/types.d.mts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import { Server, ServerOptions as ServerOptions$1, createServer } from 'node:http';
|
|
2
|
-
import { Http2Server, Http2SecureServer, ServerOptions as ServerOptions$3, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
|
|
1
|
+
import { IncomingMessage, ServerResponse, Server, ServerOptions as ServerOptions$1, createServer } from 'node:http';
|
|
2
|
+
import { Http2ServerRequest, Http2ServerResponse, Http2Server, Http2SecureServer, ServerOptions as ServerOptions$3, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
|
|
3
3
|
import { ServerOptions as ServerOptions$2, createServer as createServer$1 } from 'node:https';
|
|
4
4
|
|
|
5
|
-
type
|
|
5
|
+
type HttpBindings = {
|
|
6
|
+
incoming: IncomingMessage;
|
|
7
|
+
outgoing: ServerResponse;
|
|
8
|
+
};
|
|
9
|
+
type Http2Bindings = {
|
|
10
|
+
incoming: Http2ServerRequest;
|
|
11
|
+
outgoing: Http2ServerResponse;
|
|
12
|
+
};
|
|
13
|
+
type FetchCallback = (request: Request, env: HttpBindings | Http2Bindings) => Promise<unknown> | unknown;
|
|
6
14
|
type NextHandlerOption = {
|
|
7
15
|
fetch: FetchCallback;
|
|
8
16
|
};
|
|
@@ -30,4 +38,4 @@ type Options = {
|
|
|
30
38
|
hostname?: string;
|
|
31
39
|
} & ServerOptions;
|
|
32
40
|
|
|
33
|
-
export { FetchCallback, NextHandlerOption, Options, ServerType };
|
|
41
|
+
export { FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import { Server, ServerOptions as ServerOptions$1, createServer } from 'node:http';
|
|
2
|
-
import { Http2Server, Http2SecureServer, ServerOptions as ServerOptions$3, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
|
|
1
|
+
import { IncomingMessage, ServerResponse, Server, ServerOptions as ServerOptions$1, createServer } from 'node:http';
|
|
2
|
+
import { Http2ServerRequest, Http2ServerResponse, Http2Server, Http2SecureServer, ServerOptions as ServerOptions$3, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
|
|
3
3
|
import { ServerOptions as ServerOptions$2, createServer as createServer$1 } from 'node:https';
|
|
4
4
|
|
|
5
|
-
type
|
|
5
|
+
type HttpBindings = {
|
|
6
|
+
incoming: IncomingMessage;
|
|
7
|
+
outgoing: ServerResponse;
|
|
8
|
+
};
|
|
9
|
+
type Http2Bindings = {
|
|
10
|
+
incoming: Http2ServerRequest;
|
|
11
|
+
outgoing: Http2ServerResponse;
|
|
12
|
+
};
|
|
13
|
+
type FetchCallback = (request: Request, env: HttpBindings | Http2Bindings) => Promise<unknown> | unknown;
|
|
6
14
|
type NextHandlerOption = {
|
|
7
15
|
fetch: FetchCallback;
|
|
8
16
|
};
|
|
@@ -30,4 +38,4 @@ type Options = {
|
|
|
30
38
|
hostname?: string;
|
|
31
39
|
} & ServerOptions;
|
|
32
40
|
|
|
33
|
-
export { FetchCallback, NextHandlerOption, Options, ServerType };
|
|
41
|
+
export { FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType };
|
package/dist/utils.js
CHANGED
package/dist/utils.mjs
CHANGED
package/dist/vercel.js
CHANGED
|
@@ -37,6 +37,22 @@ module.exports = __toCommonJS(vercel_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/vercel.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
|
}
|