@hono/node-server 1.2.2 → 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 +227 -80
- package/dist/index.mjs +227 -80
- package/dist/listener.d.mts +1 -1
- package/dist/listener.d.ts +1 -1
- package/dist/listener.js +227 -78
- package/dist/listener.mjs +227 -80
- 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/serve-static.js +3 -5
- package/dist/serve-static.mjs +3 -5
- package/dist/server.js +227 -80
- package/dist/server.mjs +227 -80
- 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 +24 -7
- package/dist/utils.mjs +23 -7
- package/dist/vercel.d.mts +1 -1
- package/dist/vercel.d.ts +1 -1
- package/dist/vercel.js +227 -80
- package/dist/vercel.mjs +227 -80
- package/package.json +3 -4
package/dist/vercel.mjs
CHANGED
|
@@ -1,40 +1,21 @@
|
|
|
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) {
|
|
22
6
|
if (stream.locked) {
|
|
23
7
|
throw new TypeError("ReadableStream is locked.");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (writable.destroyed) {
|
|
27
|
-
reader.cancel();
|
|
8
|
+
} else if (writable.destroyed) {
|
|
9
|
+
stream.cancel();
|
|
28
10
|
return;
|
|
29
11
|
}
|
|
30
|
-
|
|
12
|
+
const reader = stream.getReader();
|
|
31
13
|
writable.on("close", cancel);
|
|
32
14
|
writable.on("error", cancel);
|
|
33
15
|
reader.read().then(flow, cancel);
|
|
34
16
|
return reader.closed.finally(() => {
|
|
35
17
|
writable.off("close", cancel);
|
|
36
18
|
writable.off("error", cancel);
|
|
37
|
-
writable.off("drain", onDrain);
|
|
38
19
|
});
|
|
39
20
|
function cancel(error) {
|
|
40
21
|
reader.cancel(error).catch(() => {
|
|
@@ -49,7 +30,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
49
30
|
try {
|
|
50
31
|
if (done) {
|
|
51
32
|
writable.end();
|
|
52
|
-
} else if (writable.write(value)) {
|
|
33
|
+
} else if (!writable.write(value)) {
|
|
34
|
+
writable.once("drain", onDrain);
|
|
35
|
+
} else {
|
|
53
36
|
return reader.read().then(flow, cancel);
|
|
54
37
|
}
|
|
55
38
|
} catch (e) {
|
|
@@ -57,75 +40,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
57
40
|
}
|
|
58
41
|
}
|
|
59
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
|
+
};
|
|
60
198
|
|
|
61
199
|
// src/listener.ts
|
|
62
200
|
var regBuffer = /^no$/i;
|
|
63
201
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
64
|
-
var
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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) {
|
|
82
232
|
try {
|
|
83
|
-
|
|
233
|
+
return responseViaCache(res, outgoing);
|
|
84
234
|
} catch (e) {
|
|
85
|
-
|
|
86
|
-
if (e instanceof Error) {
|
|
87
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
88
|
-
res = new Response(null, { status: 504 });
|
|
89
|
-
}
|
|
90
|
-
}
|
|
235
|
+
return handleResponseError(e, outgoing);
|
|
91
236
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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);
|
|
97
245
|
} else {
|
|
98
|
-
|
|
246
|
+
const buffer = await res.arrayBuffer();
|
|
247
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
248
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
249
|
+
outgoing.end(new Uint8Array(buffer));
|
|
99
250
|
}
|
|
251
|
+
} catch (e) {
|
|
252
|
+
handleResponseError(e, outgoing);
|
|
100
253
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else {
|
|
121
|
-
console.error(e);
|
|
122
|
-
outgoing.destroy(err);
|
|
123
|
-
}
|
|
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);
|
|
124
273
|
}
|
|
125
|
-
} else {
|
|
126
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
127
|
-
outgoing.end();
|
|
128
274
|
}
|
|
275
|
+
return responseViaResponseObject(res, outgoing);
|
|
129
276
|
};
|
|
130
277
|
};
|
|
131
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
|
}
|