@hono/node-server 1.6.0 → 1.8.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/README.md +34 -0
- package/dist/index.js +102 -42
- package/dist/index.mjs +102 -42
- package/dist/listener.d.mts +4 -2
- package/dist/listener.d.ts +4 -2
- package/dist/listener.js +102 -42
- package/dist/listener.mjs +102 -42
- package/dist/request.d.mts +2 -1
- package/dist/request.d.ts +2 -1
- package/dist/request.js +14 -3
- package/dist/request.mjs +13 -3
- package/dist/response.d.mts +9 -2
- package/dist/response.d.ts +9 -2
- package/dist/response.js +27 -8
- package/dist/response.mjs +25 -7
- package/dist/serve-static.js +6 -28
- package/dist/serve-static.mjs +6 -28
- package/dist/server.js +102 -42
- package/dist/server.mjs +102 -42
- package/dist/types.d.mts +2 -1
- package/dist/types.d.ts +2 -1
- package/dist/utils.js +1 -1
- package/dist/utils.mjs +1 -1
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +102 -42
- package/dist/vercel.mjs +102 -42
- package/package.json +2 -2
package/dist/vercel.mjs
CHANGED
|
@@ -17,7 +17,7 @@ var Request = class extends GlobalRequest {
|
|
|
17
17
|
Object.defineProperty(global, "Request", {
|
|
18
18
|
value: Request
|
|
19
19
|
});
|
|
20
|
-
var newRequestFromIncoming = (method, url, incoming) => {
|
|
20
|
+
var newRequestFromIncoming = (method, url, incoming, abortController) => {
|
|
21
21
|
const headerRecord = [];
|
|
22
22
|
const rawHeaders = incoming.rawHeaders;
|
|
23
23
|
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
@@ -29,7 +29,8 @@ var newRequestFromIncoming = (method, url, incoming) => {
|
|
|
29
29
|
}
|
|
30
30
|
const init = {
|
|
31
31
|
method,
|
|
32
|
-
headers: headerRecord
|
|
32
|
+
headers: headerRecord,
|
|
33
|
+
signal: abortController.signal
|
|
33
34
|
};
|
|
34
35
|
if (!(method === "GET" || method === "HEAD")) {
|
|
35
36
|
init.body = Readable.toWeb(incoming);
|
|
@@ -40,6 +41,8 @@ var getRequestCache = Symbol("getRequestCache");
|
|
|
40
41
|
var requestCache = Symbol("requestCache");
|
|
41
42
|
var incomingKey = Symbol("incomingKey");
|
|
42
43
|
var urlKey = Symbol("urlKey");
|
|
44
|
+
var abortControllerKey = Symbol("abortControllerKey");
|
|
45
|
+
var getAbortController = Symbol("getAbortController");
|
|
43
46
|
var requestPrototype = {
|
|
44
47
|
get method() {
|
|
45
48
|
return this[incomingKey].method || "GET";
|
|
@@ -47,11 +50,17 @@ var requestPrototype = {
|
|
|
47
50
|
get url() {
|
|
48
51
|
return this[urlKey];
|
|
49
52
|
},
|
|
53
|
+
[getAbortController]() {
|
|
54
|
+
this[getRequestCache]();
|
|
55
|
+
return this[abortControllerKey];
|
|
56
|
+
},
|
|
50
57
|
[getRequestCache]() {
|
|
58
|
+
this[abortControllerKey] ||= new AbortController();
|
|
51
59
|
return this[requestCache] ||= newRequestFromIncoming(
|
|
52
60
|
this.method,
|
|
53
61
|
this[urlKey],
|
|
54
|
-
this[incomingKey]
|
|
62
|
+
this[incomingKey],
|
|
63
|
+
this[abortControllerKey]
|
|
55
64
|
);
|
|
56
65
|
}
|
|
57
66
|
};
|
|
@@ -145,18 +154,19 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
145
154
|
if (cookies.length > 0) {
|
|
146
155
|
res["set-cookie"] = cookies;
|
|
147
156
|
}
|
|
148
|
-
res["content-type"] ??= "text/plain;charset=UTF-8";
|
|
157
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
149
158
|
return res;
|
|
150
159
|
};
|
|
151
160
|
|
|
152
161
|
// src/response.ts
|
|
153
162
|
var responseCache = Symbol("responseCache");
|
|
163
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
154
164
|
var cacheKey = Symbol("cache");
|
|
155
165
|
var GlobalResponse = global.Response;
|
|
156
166
|
var Response2 = class _Response {
|
|
157
167
|
#body;
|
|
158
168
|
#init;
|
|
159
|
-
|
|
169
|
+
[getResponseCache]() {
|
|
160
170
|
delete this[cacheKey];
|
|
161
171
|
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
162
172
|
}
|
|
@@ -166,7 +176,7 @@ var Response2 = class _Response {
|
|
|
166
176
|
const cachedGlobalResponse = init[responseCache];
|
|
167
177
|
if (cachedGlobalResponse) {
|
|
168
178
|
this.#init = cachedGlobalResponse;
|
|
169
|
-
this
|
|
179
|
+
this[getResponseCache]();
|
|
170
180
|
return;
|
|
171
181
|
} else {
|
|
172
182
|
this.#init = init.#init;
|
|
@@ -175,7 +185,7 @@ var Response2 = class _Response {
|
|
|
175
185
|
this.#init = init;
|
|
176
186
|
}
|
|
177
187
|
if (typeof body === "string" || body instanceof ReadableStream) {
|
|
178
|
-
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" };
|
|
188
|
+
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
179
189
|
if (headers instanceof Headers) {
|
|
180
190
|
headers = buildOutgoingHttpHeaders(headers);
|
|
181
191
|
}
|
|
@@ -198,14 +208,14 @@ var Response2 = class _Response {
|
|
|
198
208
|
].forEach((k) => {
|
|
199
209
|
Object.defineProperty(Response2.prototype, k, {
|
|
200
210
|
get() {
|
|
201
|
-
return this
|
|
211
|
+
return this[getResponseCache]()[k];
|
|
202
212
|
}
|
|
203
213
|
});
|
|
204
214
|
});
|
|
205
215
|
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
206
216
|
Object.defineProperty(Response2.prototype, k, {
|
|
207
217
|
value: function() {
|
|
208
|
-
return this
|
|
218
|
+
return this[getResponseCache]()[k]();
|
|
209
219
|
}
|
|
210
220
|
});
|
|
211
221
|
});
|
|
@@ -214,6 +224,22 @@ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
|
214
224
|
Object.defineProperty(global, "Response", {
|
|
215
225
|
value: Response2
|
|
216
226
|
});
|
|
227
|
+
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
228
|
+
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
229
|
+
);
|
|
230
|
+
if (!stateKey) {
|
|
231
|
+
console.warn("Failed to find Response internal state key");
|
|
232
|
+
}
|
|
233
|
+
function getInternalBody(response) {
|
|
234
|
+
if (!stateKey) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (response instanceof Response2) {
|
|
238
|
+
response = response[getResponseCache]();
|
|
239
|
+
}
|
|
240
|
+
const state = response[stateKey];
|
|
241
|
+
return state && state.body || void 0;
|
|
242
|
+
}
|
|
217
243
|
|
|
218
244
|
// src/globals.ts
|
|
219
245
|
import crypto from "crypto";
|
|
@@ -263,48 +289,71 @@ var responseViaCache = (res, outgoing) => {
|
|
|
263
289
|
);
|
|
264
290
|
}
|
|
265
291
|
};
|
|
266
|
-
var responseViaResponseObject = async (res, outgoing) => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
292
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
293
|
+
if (res instanceof Promise) {
|
|
294
|
+
if (options.errorHandler) {
|
|
295
|
+
try {
|
|
296
|
+
res = await res;
|
|
297
|
+
} catch (err) {
|
|
298
|
+
const errRes = await options.errorHandler(err);
|
|
299
|
+
if (!errRes) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
res = errRes;
|
|
303
|
+
}
|
|
304
|
+
} else {
|
|
305
|
+
res = await res.catch(handleFetchError);
|
|
272
306
|
}
|
|
273
|
-
}
|
|
274
|
-
|
|
307
|
+
}
|
|
308
|
+
if (cacheKey in res) {
|
|
309
|
+
return responseViaCache(res, outgoing);
|
|
275
310
|
}
|
|
276
311
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
312
|
+
const internalBody = getInternalBody(res);
|
|
313
|
+
if (internalBody) {
|
|
314
|
+
if (internalBody.length) {
|
|
315
|
+
resHeaderRecord["content-length"] = internalBody.length;
|
|
316
|
+
}
|
|
317
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
318
|
+
if (typeof internalBody.source === "string" || internalBody.source instanceof Uint8Array) {
|
|
319
|
+
outgoing.end(internalBody.source);
|
|
320
|
+
} else if (internalBody.source instanceof Blob) {
|
|
321
|
+
outgoing.end(new Uint8Array(await internalBody.source.arrayBuffer()));
|
|
322
|
+
} else {
|
|
323
|
+
await writeFromReadableStream(internalBody.stream, outgoing);
|
|
324
|
+
}
|
|
325
|
+
} else if (res.body) {
|
|
326
|
+
const {
|
|
327
|
+
"transfer-encoding": transferEncoding,
|
|
328
|
+
"content-encoding": contentEncoding,
|
|
329
|
+
"content-length": contentLength,
|
|
330
|
+
"x-accel-buffering": accelBuffering,
|
|
331
|
+
"content-type": contentType
|
|
332
|
+
} = resHeaderRecord;
|
|
333
|
+
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant
|
|
334
|
+
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) {
|
|
335
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
336
|
+
await writeFromReadableStream(res.body, outgoing);
|
|
337
|
+
} else {
|
|
338
|
+
const buffer = await res.arrayBuffer();
|
|
339
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
340
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
341
|
+
outgoing.end(new Uint8Array(buffer));
|
|
298
342
|
}
|
|
299
343
|
} else {
|
|
300
344
|
outgoing.writeHead(res.status, resHeaderRecord);
|
|
301
345
|
outgoing.end();
|
|
302
346
|
}
|
|
303
347
|
};
|
|
304
|
-
var getRequestListener = (fetchCallback) => {
|
|
305
|
-
return (incoming, outgoing) => {
|
|
348
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
349
|
+
return async (incoming, outgoing) => {
|
|
306
350
|
let res;
|
|
307
351
|
const req = newRequest(incoming);
|
|
352
|
+
outgoing.on("close", () => {
|
|
353
|
+
if (incoming.destroyed) {
|
|
354
|
+
req[getAbortController]().abort();
|
|
355
|
+
}
|
|
356
|
+
});
|
|
308
357
|
try {
|
|
309
358
|
res = fetchCallback(req, { incoming, outgoing });
|
|
310
359
|
if (cacheKey in res) {
|
|
@@ -312,12 +361,23 @@ var getRequestListener = (fetchCallback) => {
|
|
|
312
361
|
}
|
|
313
362
|
} catch (e) {
|
|
314
363
|
if (!res) {
|
|
315
|
-
|
|
364
|
+
if (options.errorHandler) {
|
|
365
|
+
res = await options.errorHandler(e);
|
|
366
|
+
if (!res) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
370
|
+
res = handleFetchError(e);
|
|
371
|
+
}
|
|
316
372
|
} else {
|
|
317
373
|
return handleResponseError(e, outgoing);
|
|
318
374
|
}
|
|
319
375
|
}
|
|
320
|
-
|
|
376
|
+
try {
|
|
377
|
+
return responseViaResponseObject(res, outgoing, options);
|
|
378
|
+
} catch (e) {
|
|
379
|
+
return handleResponseError(e, outgoing);
|
|
380
|
+
}
|
|
321
381
|
};
|
|
322
382
|
};
|
|
323
383
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hono/node-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Node.js Adapter for Hono",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"@types/supertest": "^2.0.12",
|
|
71
71
|
"@whatwg-node/fetch": "^0.9.14",
|
|
72
72
|
"eslint": "^8.55.0",
|
|
73
|
-
"hono": "^
|
|
73
|
+
"hono": "^4.0.3",
|
|
74
74
|
"jest": "^29.6.1",
|
|
75
75
|
"np": "^7.7.0",
|
|
76
76
|
"prettier": "^3.2.4",
|