@hono/node-server 1.14.1 → 1.14.3
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/index.js +83 -104
- package/dist/index.mjs +83 -104
- package/dist/listener.js +83 -104
- package/dist/listener.mjs +83 -104
- package/dist/request.d.mts +0 -1
- package/dist/request.d.ts +0 -1
- package/dist/request.js +1 -1
- package/dist/request.mjs +1 -1
- package/dist/response.d.mts +12 -8
- package/dist/response.d.ts +12 -8
- package/dist/response.js +24 -61
- package/dist/response.mjs +23 -57
- package/dist/server.js +83 -104
- package/dist/server.mjs +83 -104
- package/dist/vercel.js +83 -104
- package/dist/vercel.mjs +83 -104
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -44,9 +44,9 @@ var import_node_http = require("http");
|
|
|
44
44
|
var import_node_http2 = require("http2");
|
|
45
45
|
var import_node_stream = require("stream");
|
|
46
46
|
var RequestError = class extends Error {
|
|
47
|
-
static name = "RequestError";
|
|
48
47
|
constructor(message, options) {
|
|
49
48
|
super(message, options);
|
|
49
|
+
this.name = "RequestError";
|
|
50
50
|
}
|
|
51
51
|
};
|
|
52
52
|
var toRequestError = (e) => {
|
|
@@ -201,6 +201,74 @@ var newRequest = (incoming, defaultHostname) => {
|
|
|
201
201
|
return req;
|
|
202
202
|
};
|
|
203
203
|
|
|
204
|
+
// src/response.ts
|
|
205
|
+
var responseCache = Symbol("responseCache");
|
|
206
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
207
|
+
var cacheKey = Symbol("cache");
|
|
208
|
+
var GlobalResponse = global.Response;
|
|
209
|
+
var Response2 = class _Response {
|
|
210
|
+
#body;
|
|
211
|
+
#init;
|
|
212
|
+
[getResponseCache]() {
|
|
213
|
+
delete this[cacheKey];
|
|
214
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
215
|
+
}
|
|
216
|
+
constructor(body, init) {
|
|
217
|
+
let headers;
|
|
218
|
+
this.#body = body;
|
|
219
|
+
if (init instanceof _Response) {
|
|
220
|
+
const cachedGlobalResponse = init[responseCache];
|
|
221
|
+
if (cachedGlobalResponse) {
|
|
222
|
+
this.#init = cachedGlobalResponse;
|
|
223
|
+
this[getResponseCache]();
|
|
224
|
+
return;
|
|
225
|
+
} else {
|
|
226
|
+
this.#init = init.#init;
|
|
227
|
+
headers = new Headers(init.#init.headers);
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
this.#init = init;
|
|
231
|
+
}
|
|
232
|
+
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
233
|
+
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
234
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
get headers() {
|
|
238
|
+
const cache = this[cacheKey];
|
|
239
|
+
if (cache) {
|
|
240
|
+
if (!(cache[2] instanceof Headers)) {
|
|
241
|
+
cache[2] = new Headers(cache[2]);
|
|
242
|
+
}
|
|
243
|
+
return cache[2];
|
|
244
|
+
}
|
|
245
|
+
return this[getResponseCache]().headers;
|
|
246
|
+
}
|
|
247
|
+
get status() {
|
|
248
|
+
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
249
|
+
}
|
|
250
|
+
get ok() {
|
|
251
|
+
const status = this.status;
|
|
252
|
+
return status >= 200 && status < 300;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
|
|
256
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
257
|
+
get() {
|
|
258
|
+
return this[getResponseCache]()[k];
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
263
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
264
|
+
value: function() {
|
|
265
|
+
return this[getResponseCache]()[k]();
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
270
|
+
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
271
|
+
|
|
204
272
|
// src/utils.ts
|
|
205
273
|
function writeFromReadableStream(stream, writable) {
|
|
206
274
|
if (stream.locked) {
|
|
@@ -261,86 +329,6 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
261
329
|
return res;
|
|
262
330
|
};
|
|
263
331
|
|
|
264
|
-
// src/response.ts
|
|
265
|
-
var responseCache = Symbol("responseCache");
|
|
266
|
-
var getResponseCache = Symbol("getResponseCache");
|
|
267
|
-
var cacheKey = Symbol("cache");
|
|
268
|
-
var GlobalResponse = global.Response;
|
|
269
|
-
var Response2 = class _Response {
|
|
270
|
-
#body;
|
|
271
|
-
#init;
|
|
272
|
-
[getResponseCache]() {
|
|
273
|
-
delete this[cacheKey];
|
|
274
|
-
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
275
|
-
}
|
|
276
|
-
constructor(body, init) {
|
|
277
|
-
this.#body = body;
|
|
278
|
-
if (init instanceof _Response) {
|
|
279
|
-
const cachedGlobalResponse = init[responseCache];
|
|
280
|
-
if (cachedGlobalResponse) {
|
|
281
|
-
this.#init = cachedGlobalResponse;
|
|
282
|
-
this[getResponseCache]();
|
|
283
|
-
return;
|
|
284
|
-
} else {
|
|
285
|
-
this.#init = init.#init;
|
|
286
|
-
}
|
|
287
|
-
} else {
|
|
288
|
-
this.#init = init;
|
|
289
|
-
}
|
|
290
|
-
if (typeof body === "string" || typeof body?.getReader !== "undefined") {
|
|
291
|
-
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
292
|
-
if (headers instanceof Headers) {
|
|
293
|
-
headers = buildOutgoingHttpHeaders(headers);
|
|
294
|
-
}
|
|
295
|
-
;
|
|
296
|
-
this[cacheKey] = [init?.status || 200, body, headers];
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
[
|
|
301
|
-
"body",
|
|
302
|
-
"bodyUsed",
|
|
303
|
-
"headers",
|
|
304
|
-
"ok",
|
|
305
|
-
"redirected",
|
|
306
|
-
"status",
|
|
307
|
-
"statusText",
|
|
308
|
-
"trailers",
|
|
309
|
-
"type",
|
|
310
|
-
"url"
|
|
311
|
-
].forEach((k) => {
|
|
312
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
313
|
-
get() {
|
|
314
|
-
return this[getResponseCache]()[k];
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
});
|
|
318
|
-
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
319
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
320
|
-
value: function() {
|
|
321
|
-
return this[getResponseCache]()[k]();
|
|
322
|
-
}
|
|
323
|
-
});
|
|
324
|
-
});
|
|
325
|
-
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
326
|
-
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
327
|
-
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
328
|
-
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
329
|
-
);
|
|
330
|
-
if (!stateKey) {
|
|
331
|
-
console.warn("Failed to find Response internal state key");
|
|
332
|
-
}
|
|
333
|
-
function getInternalBody(response) {
|
|
334
|
-
if (!stateKey) {
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
if (response instanceof Response2) {
|
|
338
|
-
response = response[getResponseCache]();
|
|
339
|
-
}
|
|
340
|
-
const state = response[stateKey];
|
|
341
|
-
return state && state.body || void 0;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
332
|
// src/utils/response/constants.ts
|
|
345
333
|
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
346
334
|
|
|
@@ -382,14 +370,24 @@ var handleResponseError = (e, outgoing) => {
|
|
|
382
370
|
outgoing.destroy(err);
|
|
383
371
|
}
|
|
384
372
|
};
|
|
385
|
-
var responseViaCache = (res, outgoing) => {
|
|
386
|
-
|
|
373
|
+
var responseViaCache = async (res, outgoing) => {
|
|
374
|
+
let [status, body, header] = res[cacheKey];
|
|
375
|
+
if (header instanceof Headers) {
|
|
376
|
+
header = buildOutgoingHttpHeaders(header);
|
|
377
|
+
}
|
|
387
378
|
if (typeof body === "string") {
|
|
388
379
|
header["Content-Length"] = Buffer.byteLength(body);
|
|
389
|
-
|
|
380
|
+
} else if (body instanceof Uint8Array) {
|
|
381
|
+
header["Content-Length"] = body.byteLength;
|
|
382
|
+
} else if (body instanceof Blob) {
|
|
383
|
+
header["Content-Length"] = body.size;
|
|
384
|
+
}
|
|
385
|
+
outgoing.writeHead(status, header);
|
|
386
|
+
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
390
387
|
outgoing.end(body);
|
|
388
|
+
} else if (body instanceof Blob) {
|
|
389
|
+
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
391
390
|
} else {
|
|
392
|
-
outgoing.writeHead(status, header);
|
|
393
391
|
return writeFromReadableStream(body, outgoing)?.catch(
|
|
394
392
|
(e) => handleResponseError(e, outgoing)
|
|
395
393
|
);
|
|
@@ -415,25 +413,6 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
|
415
413
|
return responseViaCache(res, outgoing);
|
|
416
414
|
}
|
|
417
415
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
418
|
-
const internalBody = getInternalBody(res);
|
|
419
|
-
if (internalBody) {
|
|
420
|
-
const { length, source, stream } = internalBody;
|
|
421
|
-
if (source instanceof Uint8Array && source.byteLength !== length) {
|
|
422
|
-
} else {
|
|
423
|
-
if (length) {
|
|
424
|
-
resHeaderRecord["content-length"] = length;
|
|
425
|
-
}
|
|
426
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
427
|
-
if (typeof source === "string" || source instanceof Uint8Array) {
|
|
428
|
-
outgoing.end(source);
|
|
429
|
-
} else if (source instanceof Blob) {
|
|
430
|
-
outgoing.end(new Uint8Array(await source.arrayBuffer()));
|
|
431
|
-
} else {
|
|
432
|
-
await writeFromReadableStream(stream, outgoing);
|
|
433
|
-
}
|
|
434
|
-
return;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
416
|
if (res.body) {
|
|
438
417
|
const {
|
|
439
418
|
"transfer-encoding": transferEncoding,
|
package/dist/index.mjs
CHANGED
|
@@ -5,9 +5,9 @@ import { createServer as createServerHTTP } from "http";
|
|
|
5
5
|
import { Http2ServerRequest } from "http2";
|
|
6
6
|
import { Readable } from "stream";
|
|
7
7
|
var RequestError = class extends Error {
|
|
8
|
-
static name = "RequestError";
|
|
9
8
|
constructor(message, options) {
|
|
10
9
|
super(message, options);
|
|
10
|
+
this.name = "RequestError";
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
13
|
var toRequestError = (e) => {
|
|
@@ -162,6 +162,74 @@ var newRequest = (incoming, defaultHostname) => {
|
|
|
162
162
|
return req;
|
|
163
163
|
};
|
|
164
164
|
|
|
165
|
+
// src/response.ts
|
|
166
|
+
var responseCache = Symbol("responseCache");
|
|
167
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
168
|
+
var cacheKey = Symbol("cache");
|
|
169
|
+
var GlobalResponse = global.Response;
|
|
170
|
+
var Response2 = class _Response {
|
|
171
|
+
#body;
|
|
172
|
+
#init;
|
|
173
|
+
[getResponseCache]() {
|
|
174
|
+
delete this[cacheKey];
|
|
175
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
176
|
+
}
|
|
177
|
+
constructor(body, init) {
|
|
178
|
+
let headers;
|
|
179
|
+
this.#body = body;
|
|
180
|
+
if (init instanceof _Response) {
|
|
181
|
+
const cachedGlobalResponse = init[responseCache];
|
|
182
|
+
if (cachedGlobalResponse) {
|
|
183
|
+
this.#init = cachedGlobalResponse;
|
|
184
|
+
this[getResponseCache]();
|
|
185
|
+
return;
|
|
186
|
+
} else {
|
|
187
|
+
this.#init = init.#init;
|
|
188
|
+
headers = new Headers(init.#init.headers);
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
this.#init = init;
|
|
192
|
+
}
|
|
193
|
+
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
194
|
+
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
195
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
get headers() {
|
|
199
|
+
const cache = this[cacheKey];
|
|
200
|
+
if (cache) {
|
|
201
|
+
if (!(cache[2] instanceof Headers)) {
|
|
202
|
+
cache[2] = new Headers(cache[2]);
|
|
203
|
+
}
|
|
204
|
+
return cache[2];
|
|
205
|
+
}
|
|
206
|
+
return this[getResponseCache]().headers;
|
|
207
|
+
}
|
|
208
|
+
get status() {
|
|
209
|
+
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
210
|
+
}
|
|
211
|
+
get ok() {
|
|
212
|
+
const status = this.status;
|
|
213
|
+
return status >= 200 && status < 300;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
|
|
217
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
218
|
+
get() {
|
|
219
|
+
return this[getResponseCache]()[k];
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
224
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
225
|
+
value: function() {
|
|
226
|
+
return this[getResponseCache]()[k]();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
231
|
+
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
232
|
+
|
|
165
233
|
// src/utils.ts
|
|
166
234
|
function writeFromReadableStream(stream, writable) {
|
|
167
235
|
if (stream.locked) {
|
|
@@ -222,86 +290,6 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
222
290
|
return res;
|
|
223
291
|
};
|
|
224
292
|
|
|
225
|
-
// src/response.ts
|
|
226
|
-
var responseCache = Symbol("responseCache");
|
|
227
|
-
var getResponseCache = Symbol("getResponseCache");
|
|
228
|
-
var cacheKey = Symbol("cache");
|
|
229
|
-
var GlobalResponse = global.Response;
|
|
230
|
-
var Response2 = class _Response {
|
|
231
|
-
#body;
|
|
232
|
-
#init;
|
|
233
|
-
[getResponseCache]() {
|
|
234
|
-
delete this[cacheKey];
|
|
235
|
-
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
236
|
-
}
|
|
237
|
-
constructor(body, init) {
|
|
238
|
-
this.#body = body;
|
|
239
|
-
if (init instanceof _Response) {
|
|
240
|
-
const cachedGlobalResponse = init[responseCache];
|
|
241
|
-
if (cachedGlobalResponse) {
|
|
242
|
-
this.#init = cachedGlobalResponse;
|
|
243
|
-
this[getResponseCache]();
|
|
244
|
-
return;
|
|
245
|
-
} else {
|
|
246
|
-
this.#init = init.#init;
|
|
247
|
-
}
|
|
248
|
-
} else {
|
|
249
|
-
this.#init = init;
|
|
250
|
-
}
|
|
251
|
-
if (typeof body === "string" || typeof body?.getReader !== "undefined") {
|
|
252
|
-
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
253
|
-
if (headers instanceof Headers) {
|
|
254
|
-
headers = buildOutgoingHttpHeaders(headers);
|
|
255
|
-
}
|
|
256
|
-
;
|
|
257
|
-
this[cacheKey] = [init?.status || 200, body, headers];
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
[
|
|
262
|
-
"body",
|
|
263
|
-
"bodyUsed",
|
|
264
|
-
"headers",
|
|
265
|
-
"ok",
|
|
266
|
-
"redirected",
|
|
267
|
-
"status",
|
|
268
|
-
"statusText",
|
|
269
|
-
"trailers",
|
|
270
|
-
"type",
|
|
271
|
-
"url"
|
|
272
|
-
].forEach((k) => {
|
|
273
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
274
|
-
get() {
|
|
275
|
-
return this[getResponseCache]()[k];
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
280
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
281
|
-
value: function() {
|
|
282
|
-
return this[getResponseCache]()[k]();
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
287
|
-
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
288
|
-
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
289
|
-
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
290
|
-
);
|
|
291
|
-
if (!stateKey) {
|
|
292
|
-
console.warn("Failed to find Response internal state key");
|
|
293
|
-
}
|
|
294
|
-
function getInternalBody(response) {
|
|
295
|
-
if (!stateKey) {
|
|
296
|
-
return;
|
|
297
|
-
}
|
|
298
|
-
if (response instanceof Response2) {
|
|
299
|
-
response = response[getResponseCache]();
|
|
300
|
-
}
|
|
301
|
-
const state = response[stateKey];
|
|
302
|
-
return state && state.body || void 0;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
293
|
// src/utils/response/constants.ts
|
|
306
294
|
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
307
295
|
|
|
@@ -343,14 +331,24 @@ var handleResponseError = (e, outgoing) => {
|
|
|
343
331
|
outgoing.destroy(err);
|
|
344
332
|
}
|
|
345
333
|
};
|
|
346
|
-
var responseViaCache = (res, outgoing) => {
|
|
347
|
-
|
|
334
|
+
var responseViaCache = async (res, outgoing) => {
|
|
335
|
+
let [status, body, header] = res[cacheKey];
|
|
336
|
+
if (header instanceof Headers) {
|
|
337
|
+
header = buildOutgoingHttpHeaders(header);
|
|
338
|
+
}
|
|
348
339
|
if (typeof body === "string") {
|
|
349
340
|
header["Content-Length"] = Buffer.byteLength(body);
|
|
350
|
-
|
|
341
|
+
} else if (body instanceof Uint8Array) {
|
|
342
|
+
header["Content-Length"] = body.byteLength;
|
|
343
|
+
} else if (body instanceof Blob) {
|
|
344
|
+
header["Content-Length"] = body.size;
|
|
345
|
+
}
|
|
346
|
+
outgoing.writeHead(status, header);
|
|
347
|
+
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
351
348
|
outgoing.end(body);
|
|
349
|
+
} else if (body instanceof Blob) {
|
|
350
|
+
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
352
351
|
} else {
|
|
353
|
-
outgoing.writeHead(status, header);
|
|
354
352
|
return writeFromReadableStream(body, outgoing)?.catch(
|
|
355
353
|
(e) => handleResponseError(e, outgoing)
|
|
356
354
|
);
|
|
@@ -376,25 +374,6 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
|
376
374
|
return responseViaCache(res, outgoing);
|
|
377
375
|
}
|
|
378
376
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
379
|
-
const internalBody = getInternalBody(res);
|
|
380
|
-
if (internalBody) {
|
|
381
|
-
const { length, source, stream } = internalBody;
|
|
382
|
-
if (source instanceof Uint8Array && source.byteLength !== length) {
|
|
383
|
-
} else {
|
|
384
|
-
if (length) {
|
|
385
|
-
resHeaderRecord["content-length"] = length;
|
|
386
|
-
}
|
|
387
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
388
|
-
if (typeof source === "string" || source instanceof Uint8Array) {
|
|
389
|
-
outgoing.end(source);
|
|
390
|
-
} else if (source instanceof Blob) {
|
|
391
|
-
outgoing.end(new Uint8Array(await source.arrayBuffer()));
|
|
392
|
-
} else {
|
|
393
|
-
await writeFromReadableStream(stream, outgoing);
|
|
394
|
-
}
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
377
|
if (res.body) {
|
|
399
378
|
const {
|
|
400
379
|
"transfer-encoding": transferEncoding,
|
package/dist/listener.js
CHANGED
|
@@ -38,9 +38,9 @@ module.exports = __toCommonJS(listener_exports);
|
|
|
38
38
|
var import_node_http2 = require("http2");
|
|
39
39
|
var import_node_stream = require("stream");
|
|
40
40
|
var RequestError = class extends Error {
|
|
41
|
-
static name = "RequestError";
|
|
42
41
|
constructor(message, options) {
|
|
43
42
|
super(message, options);
|
|
43
|
+
this.name = "RequestError";
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
46
|
var toRequestError = (e) => {
|
|
@@ -195,6 +195,74 @@ var newRequest = (incoming, defaultHostname) => {
|
|
|
195
195
|
return req;
|
|
196
196
|
};
|
|
197
197
|
|
|
198
|
+
// src/response.ts
|
|
199
|
+
var responseCache = Symbol("responseCache");
|
|
200
|
+
var getResponseCache = Symbol("getResponseCache");
|
|
201
|
+
var cacheKey = Symbol("cache");
|
|
202
|
+
var GlobalResponse = global.Response;
|
|
203
|
+
var Response2 = class _Response {
|
|
204
|
+
#body;
|
|
205
|
+
#init;
|
|
206
|
+
[getResponseCache]() {
|
|
207
|
+
delete this[cacheKey];
|
|
208
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
209
|
+
}
|
|
210
|
+
constructor(body, init) {
|
|
211
|
+
let headers;
|
|
212
|
+
this.#body = body;
|
|
213
|
+
if (init instanceof _Response) {
|
|
214
|
+
const cachedGlobalResponse = init[responseCache];
|
|
215
|
+
if (cachedGlobalResponse) {
|
|
216
|
+
this.#init = cachedGlobalResponse;
|
|
217
|
+
this[getResponseCache]();
|
|
218
|
+
return;
|
|
219
|
+
} else {
|
|
220
|
+
this.#init = init.#init;
|
|
221
|
+
headers = new Headers(init.#init.headers);
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
this.#init = init;
|
|
225
|
+
}
|
|
226
|
+
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
227
|
+
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
228
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
get headers() {
|
|
232
|
+
const cache = this[cacheKey];
|
|
233
|
+
if (cache) {
|
|
234
|
+
if (!(cache[2] instanceof Headers)) {
|
|
235
|
+
cache[2] = new Headers(cache[2]);
|
|
236
|
+
}
|
|
237
|
+
return cache[2];
|
|
238
|
+
}
|
|
239
|
+
return this[getResponseCache]().headers;
|
|
240
|
+
}
|
|
241
|
+
get status() {
|
|
242
|
+
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
243
|
+
}
|
|
244
|
+
get ok() {
|
|
245
|
+
const status = this.status;
|
|
246
|
+
return status >= 200 && status < 300;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
|
|
250
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
251
|
+
get() {
|
|
252
|
+
return this[getResponseCache]()[k];
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
257
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
258
|
+
value: function() {
|
|
259
|
+
return this[getResponseCache]()[k]();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
264
|
+
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
265
|
+
|
|
198
266
|
// src/utils.ts
|
|
199
267
|
function writeFromReadableStream(stream, writable) {
|
|
200
268
|
if (stream.locked) {
|
|
@@ -255,86 +323,6 @@ var buildOutgoingHttpHeaders = (headers) => {
|
|
|
255
323
|
return res;
|
|
256
324
|
};
|
|
257
325
|
|
|
258
|
-
// src/response.ts
|
|
259
|
-
var responseCache = Symbol("responseCache");
|
|
260
|
-
var getResponseCache = Symbol("getResponseCache");
|
|
261
|
-
var cacheKey = Symbol("cache");
|
|
262
|
-
var GlobalResponse = global.Response;
|
|
263
|
-
var Response2 = class _Response {
|
|
264
|
-
#body;
|
|
265
|
-
#init;
|
|
266
|
-
[getResponseCache]() {
|
|
267
|
-
delete this[cacheKey];
|
|
268
|
-
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
269
|
-
}
|
|
270
|
-
constructor(body, init) {
|
|
271
|
-
this.#body = body;
|
|
272
|
-
if (init instanceof _Response) {
|
|
273
|
-
const cachedGlobalResponse = init[responseCache];
|
|
274
|
-
if (cachedGlobalResponse) {
|
|
275
|
-
this.#init = cachedGlobalResponse;
|
|
276
|
-
this[getResponseCache]();
|
|
277
|
-
return;
|
|
278
|
-
} else {
|
|
279
|
-
this.#init = init.#init;
|
|
280
|
-
}
|
|
281
|
-
} else {
|
|
282
|
-
this.#init = init;
|
|
283
|
-
}
|
|
284
|
-
if (typeof body === "string" || typeof body?.getReader !== "undefined") {
|
|
285
|
-
let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
286
|
-
if (headers instanceof Headers) {
|
|
287
|
-
headers = buildOutgoingHttpHeaders(headers);
|
|
288
|
-
}
|
|
289
|
-
;
|
|
290
|
-
this[cacheKey] = [init?.status || 200, body, headers];
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
};
|
|
294
|
-
[
|
|
295
|
-
"body",
|
|
296
|
-
"bodyUsed",
|
|
297
|
-
"headers",
|
|
298
|
-
"ok",
|
|
299
|
-
"redirected",
|
|
300
|
-
"status",
|
|
301
|
-
"statusText",
|
|
302
|
-
"trailers",
|
|
303
|
-
"type",
|
|
304
|
-
"url"
|
|
305
|
-
].forEach((k) => {
|
|
306
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
307
|
-
get() {
|
|
308
|
-
return this[getResponseCache]()[k];
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
313
|
-
Object.defineProperty(Response2.prototype, k, {
|
|
314
|
-
value: function() {
|
|
315
|
-
return this[getResponseCache]()[k]();
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
});
|
|
319
|
-
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
320
|
-
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
321
|
-
var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
|
|
322
|
-
(k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
|
|
323
|
-
);
|
|
324
|
-
if (!stateKey) {
|
|
325
|
-
console.warn("Failed to find Response internal state key");
|
|
326
|
-
}
|
|
327
|
-
function getInternalBody(response) {
|
|
328
|
-
if (!stateKey) {
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
if (response instanceof Response2) {
|
|
332
|
-
response = response[getResponseCache]();
|
|
333
|
-
}
|
|
334
|
-
const state = response[stateKey];
|
|
335
|
-
return state && state.body || void 0;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
326
|
// src/utils/response/constants.ts
|
|
339
327
|
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
340
328
|
|
|
@@ -376,14 +364,24 @@ var handleResponseError = (e, outgoing) => {
|
|
|
376
364
|
outgoing.destroy(err);
|
|
377
365
|
}
|
|
378
366
|
};
|
|
379
|
-
var responseViaCache = (res, outgoing) => {
|
|
380
|
-
|
|
367
|
+
var responseViaCache = async (res, outgoing) => {
|
|
368
|
+
let [status, body, header] = res[cacheKey];
|
|
369
|
+
if (header instanceof Headers) {
|
|
370
|
+
header = buildOutgoingHttpHeaders(header);
|
|
371
|
+
}
|
|
381
372
|
if (typeof body === "string") {
|
|
382
373
|
header["Content-Length"] = Buffer.byteLength(body);
|
|
383
|
-
|
|
374
|
+
} else if (body instanceof Uint8Array) {
|
|
375
|
+
header["Content-Length"] = body.byteLength;
|
|
376
|
+
} else if (body instanceof Blob) {
|
|
377
|
+
header["Content-Length"] = body.size;
|
|
378
|
+
}
|
|
379
|
+
outgoing.writeHead(status, header);
|
|
380
|
+
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
384
381
|
outgoing.end(body);
|
|
382
|
+
} else if (body instanceof Blob) {
|
|
383
|
+
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
385
384
|
} else {
|
|
386
|
-
outgoing.writeHead(status, header);
|
|
387
385
|
return writeFromReadableStream(body, outgoing)?.catch(
|
|
388
386
|
(e) => handleResponseError(e, outgoing)
|
|
389
387
|
);
|
|
@@ -409,25 +407,6 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
|
409
407
|
return responseViaCache(res, outgoing);
|
|
410
408
|
}
|
|
411
409
|
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
412
|
-
const internalBody = getInternalBody(res);
|
|
413
|
-
if (internalBody) {
|
|
414
|
-
const { length, source, stream } = internalBody;
|
|
415
|
-
if (source instanceof Uint8Array && source.byteLength !== length) {
|
|
416
|
-
} else {
|
|
417
|
-
if (length) {
|
|
418
|
-
resHeaderRecord["content-length"] = length;
|
|
419
|
-
}
|
|
420
|
-
outgoing.writeHead(res.status, resHeaderRecord);
|
|
421
|
-
if (typeof source === "string" || source instanceof Uint8Array) {
|
|
422
|
-
outgoing.end(source);
|
|
423
|
-
} else if (source instanceof Blob) {
|
|
424
|
-
outgoing.end(new Uint8Array(await source.arrayBuffer()));
|
|
425
|
-
} else {
|
|
426
|
-
await writeFromReadableStream(stream, outgoing);
|
|
427
|
-
}
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
410
|
if (res.body) {
|
|
432
411
|
const {
|
|
433
412
|
"transfer-encoding": transferEncoding,
|