@hono/node-server 1.14.0 → 1.14.2

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/vercel.mjs CHANGED
@@ -159,6 +159,74 @@ var newRequest = (incoming, defaultHostname) => {
159
159
  return req;
160
160
  };
161
161
 
162
+ // src/response.ts
163
+ var responseCache = Symbol("responseCache");
164
+ var getResponseCache = Symbol("getResponseCache");
165
+ var cacheKey = Symbol("cache");
166
+ var GlobalResponse = global.Response;
167
+ var Response2 = class _Response {
168
+ #body;
169
+ #init;
170
+ [getResponseCache]() {
171
+ delete this[cacheKey];
172
+ return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
173
+ }
174
+ constructor(body, init) {
175
+ let headers;
176
+ this.#body = body;
177
+ if (init instanceof _Response) {
178
+ const cachedGlobalResponse = init[responseCache];
179
+ if (cachedGlobalResponse) {
180
+ this.#init = cachedGlobalResponse;
181
+ this[getResponseCache]();
182
+ return;
183
+ } else {
184
+ this.#init = init.#init;
185
+ headers = new Headers(init.#init.headers);
186
+ }
187
+ } else {
188
+ this.#init = init;
189
+ }
190
+ if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
191
+ headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
192
+ this[cacheKey] = [init?.status || 200, body, headers];
193
+ }
194
+ }
195
+ get headers() {
196
+ const cache = this[cacheKey];
197
+ if (cache) {
198
+ if (!(cache[2] instanceof Headers)) {
199
+ cache[2] = new Headers(cache[2]);
200
+ }
201
+ return cache[2];
202
+ }
203
+ return this[getResponseCache]().headers;
204
+ }
205
+ get status() {
206
+ return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
207
+ }
208
+ get ok() {
209
+ const status = this.status;
210
+ return status >= 200 && status < 300;
211
+ }
212
+ };
213
+ ["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
214
+ Object.defineProperty(Response2.prototype, k, {
215
+ get() {
216
+ return this[getResponseCache]()[k];
217
+ }
218
+ });
219
+ });
220
+ ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
221
+ Object.defineProperty(Response2.prototype, k, {
222
+ value: function() {
223
+ return this[getResponseCache]()[k]();
224
+ }
225
+ });
226
+ });
227
+ Object.setPrototypeOf(Response2, GlobalResponse);
228
+ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
229
+
162
230
  // src/utils.ts
163
231
  function writeFromReadableStream(stream, writable) {
164
232
  if (stream.locked) {
@@ -219,86 +287,6 @@ var buildOutgoingHttpHeaders = (headers) => {
219
287
  return res;
220
288
  };
221
289
 
222
- // src/response.ts
223
- var responseCache = Symbol("responseCache");
224
- var getResponseCache = Symbol("getResponseCache");
225
- var cacheKey = Symbol("cache");
226
- var GlobalResponse = global.Response;
227
- var Response2 = class _Response {
228
- #body;
229
- #init;
230
- [getResponseCache]() {
231
- delete this[cacheKey];
232
- return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
233
- }
234
- constructor(body, init) {
235
- this.#body = body;
236
- if (init instanceof _Response) {
237
- const cachedGlobalResponse = init[responseCache];
238
- if (cachedGlobalResponse) {
239
- this.#init = cachedGlobalResponse;
240
- this[getResponseCache]();
241
- return;
242
- } else {
243
- this.#init = init.#init;
244
- }
245
- } else {
246
- this.#init = init;
247
- }
248
- if (typeof body === "string" || typeof body?.getReader !== "undefined") {
249
- let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
250
- if (headers instanceof Headers) {
251
- headers = buildOutgoingHttpHeaders(headers);
252
- }
253
- ;
254
- this[cacheKey] = [init?.status || 200, body, headers];
255
- }
256
- }
257
- };
258
- [
259
- "body",
260
- "bodyUsed",
261
- "headers",
262
- "ok",
263
- "redirected",
264
- "status",
265
- "statusText",
266
- "trailers",
267
- "type",
268
- "url"
269
- ].forEach((k) => {
270
- Object.defineProperty(Response2.prototype, k, {
271
- get() {
272
- return this[getResponseCache]()[k];
273
- }
274
- });
275
- });
276
- ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
277
- Object.defineProperty(Response2.prototype, k, {
278
- value: function() {
279
- return this[getResponseCache]()[k]();
280
- }
281
- });
282
- });
283
- Object.setPrototypeOf(Response2, GlobalResponse);
284
- Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
285
- var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
286
- (k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
287
- );
288
- if (!stateKey) {
289
- console.warn("Failed to find Response internal state key");
290
- }
291
- function getInternalBody(response) {
292
- if (!stateKey) {
293
- return;
294
- }
295
- if (response instanceof Response2) {
296
- response = response[getResponseCache]();
297
- }
298
- const state = response[stateKey];
299
- return state && state.body || void 0;
300
- }
301
-
302
290
  // src/utils/response/constants.ts
303
291
  var X_ALREADY_SENT = "x-hono-already-sent";
304
292
 
@@ -340,14 +328,24 @@ var handleResponseError = (e, outgoing) => {
340
328
  outgoing.destroy(err);
341
329
  }
342
330
  };
343
- var responseViaCache = (res, outgoing) => {
344
- const [status, body, header] = res[cacheKey];
331
+ var responseViaCache = async (res, outgoing) => {
332
+ let [status, body, header] = res[cacheKey];
333
+ if (header instanceof Headers) {
334
+ header = buildOutgoingHttpHeaders(header);
335
+ }
345
336
  if (typeof body === "string") {
346
337
  header["Content-Length"] = Buffer.byteLength(body);
347
- outgoing.writeHead(status, header);
338
+ } else if (body instanceof Uint8Array) {
339
+ header["Content-Length"] = body.byteLength;
340
+ } else if (body instanceof Blob) {
341
+ header["Content-Length"] = body.size;
342
+ }
343
+ outgoing.writeHead(status, header);
344
+ if (typeof body === "string" || body instanceof Uint8Array) {
348
345
  outgoing.end(body);
346
+ } else if (body instanceof Blob) {
347
+ outgoing.end(new Uint8Array(await body.arrayBuffer()));
349
348
  } else {
350
- outgoing.writeHead(status, header);
351
349
  return writeFromReadableStream(body, outgoing)?.catch(
352
350
  (e) => handleResponseError(e, outgoing)
353
351
  );
@@ -373,25 +371,6 @@ var responseViaResponseObject = async (res, outgoing, options = {}) => {
373
371
  return responseViaCache(res, outgoing);
374
372
  }
375
373
  const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
376
- const internalBody = getInternalBody(res);
377
- if (internalBody) {
378
- const { length, source, stream } = internalBody;
379
- if (source instanceof Uint8Array && source.byteLength !== length) {
380
- } else {
381
- if (length) {
382
- resHeaderRecord["content-length"] = length;
383
- }
384
- outgoing.writeHead(res.status, resHeaderRecord);
385
- if (typeof source === "string" || source instanceof Uint8Array) {
386
- outgoing.end(source);
387
- } else if (source instanceof Blob) {
388
- outgoing.end(new Uint8Array(await source.arrayBuffer()));
389
- } else {
390
- await writeFromReadableStream(stream, outgoing);
391
- }
392
- return;
393
- }
394
- }
395
374
  if (res.body) {
396
375
  const {
397
376
  "transfer-encoding": transferEncoding,
@@ -461,7 +440,7 @@ var getRequestListener = (fetchCallback, options = {}) => {
461
440
  }
462
441
  }
463
442
  try {
464
- return responseViaResponseObject(res, outgoing, options);
443
+ return await responseViaResponseObject(res, outgoing, options);
465
444
  } catch (e) {
466
445
  return handleResponseError(e, outgoing);
467
446
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/node-server",
3
- "version": "1.14.0",
3
+ "version": "1.14.2",
4
4
  "description": "Node.js Adapter for Hono",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",