@hono/node-server 1.2.3 → 1.3.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 +78 -0
- package/dist/globals.mjs +78 -0
- package/dist/index.js +221 -73
- package/dist/index.mjs +221 -73
- package/dist/listener.d.mts +1 -1
- package/dist/listener.d.ts +1 -1
- package/dist/listener.js +221 -71
- package/dist/listener.mjs +221 -73
- package/dist/request.d.mts +6 -0
- package/dist/request.d.ts +6 -0
- package/dist/request.js +93 -0
- package/dist/request.mjs +68 -0
- package/dist/response.d.mts +15 -0
- package/dist/response.d.ts +15 -0
- package/dist/response.js +106 -0
- package/dist/response.mjs +77 -0
- package/dist/serve-static.d.mts +1 -1
- package/dist/serve-static.d.ts +1 -1
- package/dist/server.js +221 -73
- package/dist/server.mjs +221 -73
- package/dist/types.d.mts +9 -9
- package/dist/types.d.ts +9 -9
- package/dist/utils.d.mts +3 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.js +18 -0
- package/dist/utils.mjs +17 -0
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +221 -73
- package/dist/vercel.mjs +221 -73
- package/package.json +3 -4
package/dist/vercel.js
CHANGED
|
@@ -34,24 +34,8 @@ __export(vercel_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(vercel_exports);
|
|
36
36
|
|
|
37
|
-
// src/listener.ts
|
|
38
|
-
var import_node_stream = require("stream");
|
|
39
|
-
|
|
40
37
|
// src/globals.ts
|
|
41
38
|
var import_node_crypto = __toESM(require("crypto"));
|
|
42
|
-
var webFetch = global.fetch;
|
|
43
|
-
if (typeof global.crypto === "undefined") {
|
|
44
|
-
global.crypto = import_node_crypto.default;
|
|
45
|
-
}
|
|
46
|
-
global.fetch = (info, init) => {
|
|
47
|
-
init = {
|
|
48
|
-
// Disable compression handling so people can return the result of a fetch
|
|
49
|
-
// directly in the loader without messing with the Content-Encoding header.
|
|
50
|
-
compress: false,
|
|
51
|
-
...init
|
|
52
|
-
};
|
|
53
|
-
return webFetch(info, init);
|
|
54
|
-
};
|
|
55
39
|
|
|
56
40
|
// src/utils.ts
|
|
57
41
|
function writeFromReadableStream(stream, writable) {
|
|
@@ -92,75 +76,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
92
76
|
}
|
|
93
77
|
}
|
|
94
78
|
}
|
|
79
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
80
|
+
const res = {};
|
|
81
|
+
const cookies = [];
|
|
82
|
+
for (const [k, v] of headers) {
|
|
83
|
+
if (k === "set-cookie") {
|
|
84
|
+
cookies.push(v);
|
|
85
|
+
} else {
|
|
86
|
+
res[k] = v;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (cookies.length > 0) {
|
|
90
|
+
res["set-cookie"] = cookies;
|
|
91
|
+
}
|
|
92
|
+
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
93
|
+
return res;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/response.ts
|
|
97
|
+
var responseCache = Symbol("responseCache");
|
|
98
|
+
var cacheKey = Symbol("cache");
|
|
99
|
+
var globalResponse = global.Response;
|
|
100
|
+
var Response2 = class {
|
|
101
|
+
#body;
|
|
102
|
+
#init;
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
get cache() {
|
|
105
|
+
delete this[cacheKey];
|
|
106
|
+
return this[responseCache] ||= new globalResponse(this.#body, this.#init);
|
|
107
|
+
}
|
|
108
|
+
constructor(body, init) {
|
|
109
|
+
this.#body = body;
|
|
110
|
+
this.#init = init;
|
|
111
|
+
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
112
|
+
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
113
|
+
if (headers instanceof Headers) {
|
|
114
|
+
headers = buildOutgoingHttpHeaders(headers);
|
|
115
|
+
}
|
|
116
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
[
|
|
121
|
+
"body",
|
|
122
|
+
"bodyUsed",
|
|
123
|
+
"headers",
|
|
124
|
+
"ok",
|
|
125
|
+
"redirected",
|
|
126
|
+
"status",
|
|
127
|
+
"statusText",
|
|
128
|
+
"trailers",
|
|
129
|
+
"type",
|
|
130
|
+
"url"
|
|
131
|
+
].forEach((k) => {
|
|
132
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
133
|
+
get() {
|
|
134
|
+
return this.cache[k];
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
139
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
140
|
+
value: function() {
|
|
141
|
+
return this.cache[k]();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
Object.setPrototypeOf(Response2, globalResponse);
|
|
146
|
+
Object.setPrototypeOf(Response2.prototype, globalResponse.prototype);
|
|
147
|
+
Object.defineProperty(global, "Response", {
|
|
148
|
+
value: Response2
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// src/globals.ts
|
|
152
|
+
Object.defineProperty(global, "Response", {
|
|
153
|
+
value: Response2
|
|
154
|
+
});
|
|
155
|
+
var webFetch = global.fetch;
|
|
156
|
+
if (typeof global.crypto === "undefined") {
|
|
157
|
+
global.crypto = import_node_crypto.default;
|
|
158
|
+
}
|
|
159
|
+
global.fetch = (info, init) => {
|
|
160
|
+
init = {
|
|
161
|
+
// Disable compression handling so people can return the result of a fetch
|
|
162
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
163
|
+
compress: false,
|
|
164
|
+
...init
|
|
165
|
+
};
|
|
166
|
+
return webFetch(info, init);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/request.ts
|
|
170
|
+
var import_node_stream = require("stream");
|
|
171
|
+
var newRequestFromIncoming = (method, url, incoming) => {
|
|
172
|
+
const headerRecord = [];
|
|
173
|
+
const len = incoming.rawHeaders.length;
|
|
174
|
+
for (let i = 0; i < len; i += 2) {
|
|
175
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
176
|
+
}
|
|
177
|
+
const init = {
|
|
178
|
+
method,
|
|
179
|
+
headers: headerRecord
|
|
180
|
+
};
|
|
181
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
182
|
+
init.body = import_node_stream.Readable.toWeb(incoming);
|
|
183
|
+
init.duplex = "half";
|
|
184
|
+
}
|
|
185
|
+
return new Request(url, init);
|
|
186
|
+
};
|
|
187
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
188
|
+
var requestCache = Symbol("requestCache");
|
|
189
|
+
var incomingKey = Symbol("incomingKey");
|
|
190
|
+
var requestPrototype = {
|
|
191
|
+
get method() {
|
|
192
|
+
return this[incomingKey].method || "GET";
|
|
193
|
+
},
|
|
194
|
+
get url() {
|
|
195
|
+
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
196
|
+
},
|
|
197
|
+
[getRequestCache]() {
|
|
198
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
[
|
|
202
|
+
"body",
|
|
203
|
+
"bodyUsed",
|
|
204
|
+
"cache",
|
|
205
|
+
"credentials",
|
|
206
|
+
"destination",
|
|
207
|
+
"headers",
|
|
208
|
+
"integrity",
|
|
209
|
+
"mode",
|
|
210
|
+
"redirect",
|
|
211
|
+
"referrer",
|
|
212
|
+
"referrerPolicy",
|
|
213
|
+
"signal"
|
|
214
|
+
].forEach((k) => {
|
|
215
|
+
Object.defineProperty(requestPrototype, k, {
|
|
216
|
+
get() {
|
|
217
|
+
return this[getRequestCache]()[k];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
222
|
+
Object.defineProperty(requestPrototype, k, {
|
|
223
|
+
value: function() {
|
|
224
|
+
return this[getRequestCache]()[k]();
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
229
|
+
var newRequest = (incoming) => {
|
|
230
|
+
const req = Object.create(requestPrototype);
|
|
231
|
+
req[incomingKey] = incoming;
|
|
232
|
+
return req;
|
|
233
|
+
};
|
|
95
234
|
|
|
96
235
|
// src/listener.ts
|
|
97
236
|
var regBuffer = /^no$/i;
|
|
98
237
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
99
|
-
var
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
238
|
+
var handleFetchError = (e) => new Response(null, {
|
|
239
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
240
|
+
});
|
|
241
|
+
var handleResponseError = (e, outgoing) => {
|
|
242
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
243
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
244
|
+
console.info("The user aborted a request.");
|
|
245
|
+
} else {
|
|
246
|
+
console.error(e);
|
|
247
|
+
outgoing.destroy(err);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
var responseViaCache = (res, outgoing) => {
|
|
251
|
+
const [status, body, header] = res[cacheKey];
|
|
252
|
+
if (typeof body === "string") {
|
|
253
|
+
header["content-length"] ||= "" + Buffer.byteLength(body);
|
|
254
|
+
outgoing.writeHead(status, header);
|
|
255
|
+
outgoing.end(body);
|
|
256
|
+
} else {
|
|
257
|
+
outgoing.writeHead(status, header);
|
|
258
|
+
return writeFromReadableStream(body, outgoing)?.catch(
|
|
259
|
+
(e) => handleResponseError(e, outgoing)
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
var responseViaResponseObject = async (res, outgoing) => {
|
|
264
|
+
if (res instanceof Promise) {
|
|
265
|
+
res = await res.catch(handleFetchError);
|
|
266
|
+
}
|
|
267
|
+
if (cacheKey in res) {
|
|
117
268
|
try {
|
|
118
|
-
|
|
269
|
+
return responseViaCache(res, outgoing);
|
|
119
270
|
} catch (e) {
|
|
120
|
-
|
|
121
|
-
if (e instanceof Error) {
|
|
122
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
123
|
-
res = new Response(null, { status: 504 });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
271
|
+
return handleResponseError(e, outgoing);
|
|
126
272
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
273
|
+
}
|
|
274
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
275
|
+
if (res.body) {
|
|
276
|
+
try {
|
|
277
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
278
|
+
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
279
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
280
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
132
281
|
} else {
|
|
133
|
-
|
|
282
|
+
const buffer = await res.arrayBuffer();
|
|
283
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
284
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
285
|
+
outgoing.end(new Uint8Array(buffer));
|
|
134
286
|
}
|
|
287
|
+
} catch (e) {
|
|
288
|
+
handleResponseError(e, outgoing);
|
|
135
289
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
} else {
|
|
156
|
-
console.error(e);
|
|
157
|
-
outgoing.destroy(err);
|
|
158
|
-
}
|
|
290
|
+
} else {
|
|
291
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
292
|
+
outgoing.end();
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
var getRequestListener = (fetchCallback) => {
|
|
296
|
+
return (incoming, outgoing) => {
|
|
297
|
+
let res;
|
|
298
|
+
const req = newRequest(incoming);
|
|
299
|
+
try {
|
|
300
|
+
res = fetchCallback(req);
|
|
301
|
+
if (cacheKey in res) {
|
|
302
|
+
return responseViaCache(res, outgoing);
|
|
303
|
+
}
|
|
304
|
+
} catch (e) {
|
|
305
|
+
if (!res) {
|
|
306
|
+
res = handleFetchError(e);
|
|
307
|
+
} else {
|
|
308
|
+
return handleResponseError(e, outgoing);
|
|
159
309
|
}
|
|
160
|
-
} else {
|
|
161
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
162
|
-
outgoing.end();
|
|
163
310
|
}
|
|
311
|
+
return responseViaResponseObject(res, outgoing);
|
|
164
312
|
};
|
|
165
313
|
};
|
|
166
314
|
|
package/dist/vercel.mjs
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
// src/listener.ts
|
|
2
|
-
import { Readable } from "stream";
|
|
3
|
-
|
|
4
1
|
// src/globals.ts
|
|
5
2
|
import crypto from "crypto";
|
|
6
|
-
var webFetch = global.fetch;
|
|
7
|
-
if (typeof global.crypto === "undefined") {
|
|
8
|
-
global.crypto = crypto;
|
|
9
|
-
}
|
|
10
|
-
global.fetch = (info, init) => {
|
|
11
|
-
init = {
|
|
12
|
-
// Disable compression handling so people can return the result of a fetch
|
|
13
|
-
// directly in the loader without messing with the Content-Encoding header.
|
|
14
|
-
compress: false,
|
|
15
|
-
...init
|
|
16
|
-
};
|
|
17
|
-
return webFetch(info, init);
|
|
18
|
-
};
|
|
19
3
|
|
|
20
4
|
// src/utils.ts
|
|
21
5
|
function writeFromReadableStream(stream, writable) {
|
|
@@ -56,75 +40,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
56
40
|
}
|
|
57
41
|
}
|
|
58
42
|
}
|
|
43
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
44
|
+
const res = {};
|
|
45
|
+
const cookies = [];
|
|
46
|
+
for (const [k, v] of headers) {
|
|
47
|
+
if (k === "set-cookie") {
|
|
48
|
+
cookies.push(v);
|
|
49
|
+
} else {
|
|
50
|
+
res[k] = v;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (cookies.length > 0) {
|
|
54
|
+
res["set-cookie"] = cookies;
|
|
55
|
+
}
|
|
56
|
+
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
57
|
+
return res;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/response.ts
|
|
61
|
+
var responseCache = Symbol("responseCache");
|
|
62
|
+
var cacheKey = Symbol("cache");
|
|
63
|
+
var globalResponse = global.Response;
|
|
64
|
+
var Response2 = class {
|
|
65
|
+
#body;
|
|
66
|
+
#init;
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
get cache() {
|
|
69
|
+
delete this[cacheKey];
|
|
70
|
+
return this[responseCache] ||= new globalResponse(this.#body, this.#init);
|
|
71
|
+
}
|
|
72
|
+
constructor(body, init) {
|
|
73
|
+
this.#body = body;
|
|
74
|
+
this.#init = init;
|
|
75
|
+
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
76
|
+
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
77
|
+
if (headers instanceof Headers) {
|
|
78
|
+
headers = buildOutgoingHttpHeaders(headers);
|
|
79
|
+
}
|
|
80
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
[
|
|
85
|
+
"body",
|
|
86
|
+
"bodyUsed",
|
|
87
|
+
"headers",
|
|
88
|
+
"ok",
|
|
89
|
+
"redirected",
|
|
90
|
+
"status",
|
|
91
|
+
"statusText",
|
|
92
|
+
"trailers",
|
|
93
|
+
"type",
|
|
94
|
+
"url"
|
|
95
|
+
].forEach((k) => {
|
|
96
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
97
|
+
get() {
|
|
98
|
+
return this.cache[k];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
103
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
104
|
+
value: function() {
|
|
105
|
+
return this.cache[k]();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
Object.setPrototypeOf(Response2, globalResponse);
|
|
110
|
+
Object.setPrototypeOf(Response2.prototype, globalResponse.prototype);
|
|
111
|
+
Object.defineProperty(global, "Response", {
|
|
112
|
+
value: Response2
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// src/globals.ts
|
|
116
|
+
Object.defineProperty(global, "Response", {
|
|
117
|
+
value: Response2
|
|
118
|
+
});
|
|
119
|
+
var webFetch = global.fetch;
|
|
120
|
+
if (typeof global.crypto === "undefined") {
|
|
121
|
+
global.crypto = crypto;
|
|
122
|
+
}
|
|
123
|
+
global.fetch = (info, init) => {
|
|
124
|
+
init = {
|
|
125
|
+
// Disable compression handling so people can return the result of a fetch
|
|
126
|
+
// directly in the loader without messing with the Content-Encoding header.
|
|
127
|
+
compress: false,
|
|
128
|
+
...init
|
|
129
|
+
};
|
|
130
|
+
return webFetch(info, init);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// src/request.ts
|
|
134
|
+
import { Readable } from "stream";
|
|
135
|
+
var newRequestFromIncoming = (method, url, incoming) => {
|
|
136
|
+
const headerRecord = [];
|
|
137
|
+
const len = incoming.rawHeaders.length;
|
|
138
|
+
for (let i = 0; i < len; i += 2) {
|
|
139
|
+
headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
|
|
140
|
+
}
|
|
141
|
+
const init = {
|
|
142
|
+
method,
|
|
143
|
+
headers: headerRecord
|
|
144
|
+
};
|
|
145
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
146
|
+
init.body = Readable.toWeb(incoming);
|
|
147
|
+
init.duplex = "half";
|
|
148
|
+
}
|
|
149
|
+
return new Request(url, init);
|
|
150
|
+
};
|
|
151
|
+
var getRequestCache = Symbol("getRequestCache");
|
|
152
|
+
var requestCache = Symbol("requestCache");
|
|
153
|
+
var incomingKey = Symbol("incomingKey");
|
|
154
|
+
var requestPrototype = {
|
|
155
|
+
get method() {
|
|
156
|
+
return this[incomingKey].method || "GET";
|
|
157
|
+
},
|
|
158
|
+
get url() {
|
|
159
|
+
return `http://${this[incomingKey].headers.host}${this[incomingKey].url}`;
|
|
160
|
+
},
|
|
161
|
+
[getRequestCache]() {
|
|
162
|
+
return this[requestCache] ||= newRequestFromIncoming(this.method, this.url, this[incomingKey]);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
[
|
|
166
|
+
"body",
|
|
167
|
+
"bodyUsed",
|
|
168
|
+
"cache",
|
|
169
|
+
"credentials",
|
|
170
|
+
"destination",
|
|
171
|
+
"headers",
|
|
172
|
+
"integrity",
|
|
173
|
+
"mode",
|
|
174
|
+
"redirect",
|
|
175
|
+
"referrer",
|
|
176
|
+
"referrerPolicy",
|
|
177
|
+
"signal"
|
|
178
|
+
].forEach((k) => {
|
|
179
|
+
Object.defineProperty(requestPrototype, k, {
|
|
180
|
+
get() {
|
|
181
|
+
return this[getRequestCache]()[k];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
186
|
+
Object.defineProperty(requestPrototype, k, {
|
|
187
|
+
value: function() {
|
|
188
|
+
return this[getRequestCache]()[k]();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
Object.setPrototypeOf(requestPrototype, global.Request.prototype);
|
|
193
|
+
var newRequest = (incoming) => {
|
|
194
|
+
const req = Object.create(requestPrototype);
|
|
195
|
+
req[incomingKey] = incoming;
|
|
196
|
+
return req;
|
|
197
|
+
};
|
|
59
198
|
|
|
60
199
|
// src/listener.ts
|
|
61
200
|
var regBuffer = /^no$/i;
|
|
62
201
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
63
|
-
var
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
202
|
+
var handleFetchError = (e) => new Response(null, {
|
|
203
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
204
|
+
});
|
|
205
|
+
var handleResponseError = (e, outgoing) => {
|
|
206
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
207
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
208
|
+
console.info("The user aborted a request.");
|
|
209
|
+
} else {
|
|
210
|
+
console.error(e);
|
|
211
|
+
outgoing.destroy(err);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
var responseViaCache = (res, outgoing) => {
|
|
215
|
+
const [status, body, header] = res[cacheKey];
|
|
216
|
+
if (typeof body === "string") {
|
|
217
|
+
header["content-length"] ||= "" + Buffer.byteLength(body);
|
|
218
|
+
outgoing.writeHead(status, header);
|
|
219
|
+
outgoing.end(body);
|
|
220
|
+
} else {
|
|
221
|
+
outgoing.writeHead(status, header);
|
|
222
|
+
return writeFromReadableStream(body, outgoing)?.catch(
|
|
223
|
+
(e) => handleResponseError(e, outgoing)
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
var responseViaResponseObject = async (res, outgoing) => {
|
|
228
|
+
if (res instanceof Promise) {
|
|
229
|
+
res = await res.catch(handleFetchError);
|
|
230
|
+
}
|
|
231
|
+
if (cacheKey in res) {
|
|
81
232
|
try {
|
|
82
|
-
|
|
233
|
+
return responseViaCache(res, outgoing);
|
|
83
234
|
} catch (e) {
|
|
84
|
-
|
|
85
|
-
if (e instanceof Error) {
|
|
86
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
87
|
-
res = new Response(null, { status: 504 });
|
|
88
|
-
}
|
|
89
|
-
}
|
|
235
|
+
return handleResponseError(e, outgoing);
|
|
90
236
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
237
|
+
}
|
|
238
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
239
|
+
if (res.body) {
|
|
240
|
+
try {
|
|
241
|
+
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
|
|
242
|
+
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
|
|
243
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
244
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
96
245
|
} else {
|
|
97
|
-
|
|
246
|
+
const buffer = await res.arrayBuffer();
|
|
247
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
248
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
249
|
+
outgoing.end(new Uint8Array(buffer));
|
|
98
250
|
}
|
|
251
|
+
} catch (e) {
|
|
252
|
+
handleResponseError(e, outgoing);
|
|
99
253
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
} else {
|
|
120
|
-
console.error(e);
|
|
121
|
-
outgoing.destroy(err);
|
|
122
|
-
}
|
|
254
|
+
} else {
|
|
255
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
256
|
+
outgoing.end();
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
var getRequestListener = (fetchCallback) => {
|
|
260
|
+
return (incoming, outgoing) => {
|
|
261
|
+
let res;
|
|
262
|
+
const req = newRequest(incoming);
|
|
263
|
+
try {
|
|
264
|
+
res = fetchCallback(req);
|
|
265
|
+
if (cacheKey in res) {
|
|
266
|
+
return responseViaCache(res, outgoing);
|
|
267
|
+
}
|
|
268
|
+
} catch (e) {
|
|
269
|
+
if (!res) {
|
|
270
|
+
res = handleFetchError(e);
|
|
271
|
+
} else {
|
|
272
|
+
return handleResponseError(e, outgoing);
|
|
123
273
|
}
|
|
124
|
-
} else {
|
|
125
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
126
|
-
outgoing.end();
|
|
127
274
|
}
|
|
275
|
+
return responseViaResponseObject(res, outgoing);
|
|
128
276
|
};
|
|
129
277
|
};
|
|
130
278
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hono/node-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Node.js Adapter for Hono",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -59,10 +59,9 @@
|
|
|
59
59
|
"engines": {
|
|
60
60
|
"node": ">=18.14.1"
|
|
61
61
|
},
|
|
62
|
-
"dependencies": {},
|
|
63
62
|
"devDependencies": {
|
|
64
63
|
"@types/jest": "^29.5.3",
|
|
65
|
-
"@types/node": "^
|
|
64
|
+
"@types/node": "^20.10.0",
|
|
66
65
|
"@types/supertest": "^2.0.12",
|
|
67
66
|
"hono": "^3.9.2",
|
|
68
67
|
"jest": "^29.6.1",
|
|
@@ -71,6 +70,6 @@
|
|
|
71
70
|
"supertest": "^6.3.3",
|
|
72
71
|
"ts-jest": "^29.1.1",
|
|
73
72
|
"tsup": "^7.2.0",
|
|
74
|
-
"typescript": "^
|
|
73
|
+
"typescript": "^5.3.2"
|
|
75
74
|
}
|
|
76
75
|
}
|