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