@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.js
CHANGED
|
@@ -34,43 +34,24 @@ __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) {
|
|
58
42
|
if (stream.locked) {
|
|
59
43
|
throw new TypeError("ReadableStream is locked.");
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (writable.destroyed) {
|
|
63
|
-
reader.cancel();
|
|
44
|
+
} else if (writable.destroyed) {
|
|
45
|
+
stream.cancel();
|
|
64
46
|
return;
|
|
65
47
|
}
|
|
66
|
-
|
|
48
|
+
const reader = stream.getReader();
|
|
67
49
|
writable.on("close", cancel);
|
|
68
50
|
writable.on("error", cancel);
|
|
69
51
|
reader.read().then(flow, cancel);
|
|
70
52
|
return reader.closed.finally(() => {
|
|
71
53
|
writable.off("close", cancel);
|
|
72
54
|
writable.off("error", cancel);
|
|
73
|
-
writable.off("drain", onDrain);
|
|
74
55
|
});
|
|
75
56
|
function cancel(error) {
|
|
76
57
|
reader.cancel(error).catch(() => {
|
|
@@ -85,7 +66,9 @@ function writeFromReadableStream(stream, writable) {
|
|
|
85
66
|
try {
|
|
86
67
|
if (done) {
|
|
87
68
|
writable.end();
|
|
88
|
-
} else if (writable.write(value)) {
|
|
69
|
+
} else if (!writable.write(value)) {
|
|
70
|
+
writable.once("drain", onDrain);
|
|
71
|
+
} else {
|
|
89
72
|
return reader.read().then(flow, cancel);
|
|
90
73
|
}
|
|
91
74
|
} catch (e) {
|
|
@@ -93,75 +76,239 @@ function writeFromReadableStream(stream, writable) {
|
|
|
93
76
|
}
|
|
94
77
|
}
|
|
95
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
|
+
};
|
|
96
234
|
|
|
97
235
|
// src/listener.ts
|
|
98
236
|
var regBuffer = /^no$/i;
|
|
99
237
|
var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
|
|
100
|
-
var
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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) {
|
|
118
268
|
try {
|
|
119
|
-
|
|
269
|
+
return responseViaCache(res, outgoing);
|
|
120
270
|
} catch (e) {
|
|
121
|
-
|
|
122
|
-
if (e instanceof Error) {
|
|
123
|
-
if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
|
|
124
|
-
res = new Response(null, { status: 504 });
|
|
125
|
-
}
|
|
126
|
-
}
|
|
271
|
+
return handleResponseError(e, outgoing);
|
|
127
272
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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);
|
|
133
281
|
} else {
|
|
134
|
-
|
|
282
|
+
const buffer = await res.arrayBuffer();
|
|
283
|
+
resHeaderRecord["content-length"] = buffer.byteLength;
|
|
284
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
285
|
+
outgoing.end(new Uint8Array(buffer));
|
|
135
286
|
}
|
|
287
|
+
} catch (e) {
|
|
288
|
+
handleResponseError(e, outgoing);
|
|
136
289
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
} else {
|
|
157
|
-
console.error(e);
|
|
158
|
-
outgoing.destroy(err);
|
|
159
|
-
}
|
|
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);
|
|
160
309
|
}
|
|
161
|
-
} else {
|
|
162
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
163
|
-
outgoing.end();
|
|
164
310
|
}
|
|
311
|
+
return responseViaResponseObject(res, outgoing);
|
|
165
312
|
};
|
|
166
313
|
};
|
|
167
314
|
|