@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
|
@@ -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/serve-static.js
CHANGED
|
@@ -191,11 +191,9 @@ var serveStatic = (options = { root: "" }) => {
|
|
|
191
191
|
}
|
|
192
192
|
c.header("Accept-Ranges", "bytes");
|
|
193
193
|
c.header("Date", stat.birthtime.toUTCString());
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
start = parseInt(parts[0], 10);
|
|
198
|
-
end = parts[1] ? parseInt(parts[1], 10) : end;
|
|
194
|
+
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
195
|
+
const start = parts[0] ? parseInt(parts[0], 10) : 0;
|
|
196
|
+
let end = parts[1] ? parseInt(parts[1], 10) : stat.size - 1;
|
|
199
197
|
if (size < end - start + 1) {
|
|
200
198
|
end = size - 1;
|
|
201
199
|
}
|
package/dist/serve-static.mjs
CHANGED
|
@@ -167,11 +167,9 @@ var serveStatic = (options = { root: "" }) => {
|
|
|
167
167
|
}
|
|
168
168
|
c.header("Accept-Ranges", "bytes");
|
|
169
169
|
c.header("Date", stat.birthtime.toUTCString());
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
start = parseInt(parts[0], 10);
|
|
174
|
-
end = parts[1] ? parseInt(parts[1], 10) : end;
|
|
170
|
+
const parts = range.replace(/bytes=/, "").split("-", 2);
|
|
171
|
+
const start = parts[0] ? parseInt(parts[0], 10) : 0;
|
|
172
|
+
let end = parts[1] ? parseInt(parts[1], 10) : stat.size - 1;
|
|
175
173
|
if (size < end - start + 1) {
|
|
176
174
|
end = size - 1;
|
|
177
175
|
}
|
package/dist/server.js
CHANGED
|
@@ -36,43 +36,24 @@ __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) {
|
|
60
44
|
if (stream.locked) {
|
|
61
45
|
throw new TypeError("ReadableStream is locked.");
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (writable.destroyed) {
|
|
65
|
-
reader.cancel();
|
|
46
|
+
} else if (writable.destroyed) {
|
|
47
|
+
stream.cancel();
|
|
66
48
|
return;
|
|
67
49
|
}
|
|
68
|
-
|
|
50
|
+
const reader = stream.getReader();
|
|
69
51
|
writable.on("close", cancel);
|
|
70
52
|
writable.on("error", cancel);
|
|
71
53
|
reader.read().then(flow, cancel);
|
|
72
54
|
return reader.closed.finally(() => {
|
|
73
55
|
writable.off("close", cancel);
|
|
74
56
|
writable.off("error", cancel);
|
|
75
|
-
writable.off("drain", onDrain);
|
|
76
57
|
});
|
|
77
58
|
function cancel(error) {
|
|
78
59
|
reader.cancel(error).catch(() => {
|
|
@@ -87,7 +68,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
87
68
|
try {
|
|
88
69
|
if (done) {
|
|
89
70
|
writable.end();
|
|
90
|
-
} else if (writable.write(value)) {
|
|
71
|
+
} else if (!writable.write(value)) {
|
|
72
|
+
writable.once("drain", onDrain);
|
|
73
|
+
} else {
|
|
91
74
|
return reader.read().then(flow, cancel);
|
|
92
75
|
}
|
|
93
76
|
} catch (e) {
|
|
@@ -95,75 +78,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
95
78
|
}
|
|
96
79
|
}
|
|
97
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
|
+
};
|
|
98
236
|
|
|
99
237
|
// src/listener.ts
|
|
100
238
|
var regBuffer = /^no$/i;
|
|
101
239
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
102
|
-
var
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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) {
|
|
120
270
|
try {
|
|
121
|
-
|
|
271
|
+
return responseViaCache(res, outgoing);
|
|
122
272
|
} catch (e) {
|
|
123
|
-
|
|
124
|
-
if (e instanceof Error) {
|
|
125
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
126
|
-
res = new Response(null, { status: 504 });
|
|
127
|
-
}
|
|
128
|
-
}
|
|
273
|
+
return handleResponseError(e, outgoing);
|
|
129
274
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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);
|
|
135
283
|
} else {
|
|
136
|
-
|
|
284
|
+
const buffer = await res.arrayBuffer();
|
|
285
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
286
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
287
|
+
outgoing.end(new Uint8Array(buffer));
|
|
137
288
|
}
|
|
289
|
+
} catch (e) {
|
|
290
|
+
handleResponseError(e, outgoing);
|
|
138
291
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
} else {
|
|
159
|
-
console.error(e);
|
|
160
|
-
outgoing.destroy(err);
|
|
161
|
-
}
|
|
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);
|
|
162
311
|
}
|
|
163
|
-
} else {
|
|
164
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
165
|
-
outgoing.end();
|
|
166
312
|
}
|
|
313
|
+
return responseViaResponseObject(res, outgoing);
|
|
167
314
|
};
|
|
168
315
|
};
|
|
169
316
|
|