@hono/node-server 1.14.1 → 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/listener.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,
@@ -1,10 +1,12 @@
1
- interface InternalBody {
2
- source: string | Uint8Array | FormData | Blob | null;
3
- stream: ReadableStream;
4
- length: number | null;
5
- }
1
+ import { OutgoingHttpHeaders } from 'node:http';
2
+
6
3
  declare const getResponseCache: unique symbol;
7
4
  declare const cacheKey: unique symbol;
5
+ type InternalCache = [
6
+ number,
7
+ string | ReadableStream,
8
+ Record<string, string> | Headers | OutgoingHttpHeaders
9
+ ];
8
10
  declare const GlobalResponse: {
9
11
  new (body?: BodyInit | null, init?: ResponseInit): globalThis.Response;
10
12
  prototype: globalThis.Response;
@@ -14,9 +16,11 @@ declare const GlobalResponse: {
14
16
  };
15
17
  declare class Response {
16
18
  #private;
17
- [getResponseCache](): typeof GlobalResponse;
19
+ [getResponseCache](): globalThis.Response;
18
20
  constructor(body?: BodyInit | null, init?: ResponseInit);
21
+ get headers(): Headers;
22
+ get status(): number;
23
+ get ok(): boolean;
19
24
  }
20
- declare function getInternalBody(response: Response | typeof GlobalResponse): InternalBody | undefined;
21
25
 
22
- export { GlobalResponse, Response, cacheKey, getInternalBody };
26
+ export { GlobalResponse, InternalCache, Response, cacheKey };
@@ -1,10 +1,12 @@
1
- interface InternalBody {
2
- source: string | Uint8Array | FormData | Blob | null;
3
- stream: ReadableStream;
4
- length: number | null;
5
- }
1
+ import { OutgoingHttpHeaders } from 'node:http';
2
+
6
3
  declare const getResponseCache: unique symbol;
7
4
  declare const cacheKey: unique symbol;
5
+ type InternalCache = [
6
+ number,
7
+ string | ReadableStream,
8
+ Record<string, string> | Headers | OutgoingHttpHeaders
9
+ ];
8
10
  declare const GlobalResponse: {
9
11
  new (body?: BodyInit | null, init?: ResponseInit): globalThis.Response;
10
12
  prototype: globalThis.Response;
@@ -14,9 +16,11 @@ declare const GlobalResponse: {
14
16
  };
15
17
  declare class Response {
16
18
  #private;
17
- [getResponseCache](): typeof GlobalResponse;
19
+ [getResponseCache](): globalThis.Response;
18
20
  constructor(body?: BodyInit | null, init?: ResponseInit);
21
+ get headers(): Headers;
22
+ get status(): number;
23
+ get ok(): boolean;
19
24
  }
20
- declare function getInternalBody(response: Response | typeof GlobalResponse): InternalBody | undefined;
21
25
 
22
- export { GlobalResponse, Response, cacheKey, getInternalBody };
26
+ export { GlobalResponse, InternalCache, Response, cacheKey };
package/dist/response.js CHANGED
@@ -22,33 +22,9 @@ var response_exports = {};
22
22
  __export(response_exports, {
23
23
  GlobalResponse: () => GlobalResponse,
24
24
  Response: () => Response,
25
- cacheKey: () => cacheKey,
26
- getInternalBody: () => getInternalBody
25
+ cacheKey: () => cacheKey
27
26
  });
28
27
  module.exports = __toCommonJS(response_exports);
29
-
30
- // src/utils.ts
31
- var buildOutgoingHttpHeaders = (headers) => {
32
- const res = {};
33
- if (!(headers instanceof Headers)) {
34
- headers = new Headers(headers ?? void 0);
35
- }
36
- const cookies = [];
37
- for (const [k, v] of headers) {
38
- if (k === "set-cookie") {
39
- cookies.push(v);
40
- } else {
41
- res[k] = v;
42
- }
43
- }
44
- if (cookies.length > 0) {
45
- res["set-cookie"] = cookies;
46
- }
47
- res["content-type"] ??= "text/plain; charset=UTF-8";
48
- return res;
49
- };
50
-
51
- // src/response.ts
52
28
  var responseCache = Symbol("responseCache");
53
29
  var getResponseCache = Symbol("getResponseCache");
54
30
  var cacheKey = Symbol("cache");
@@ -61,6 +37,7 @@ var Response = class _Response {
61
37
  return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
62
38
  }
63
39
  constructor(body, init) {
40
+ let headers;
64
41
  this.#body = body;
65
42
  if (init instanceof _Response) {
66
43
  const cachedGlobalResponse = init[responseCache];
@@ -70,32 +47,35 @@ var Response = class _Response {
70
47
  return;
71
48
  } else {
72
49
  this.#init = init.#init;
50
+ headers = new Headers(init.#init.headers);
73
51
  }
74
52
  } else {
75
53
  this.#init = init;
76
54
  }
77
- if (typeof body === "string" || typeof body?.getReader !== "undefined") {
78
- let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
79
- if (headers instanceof Headers) {
80
- headers = buildOutgoingHttpHeaders(headers);
81
- }
82
- ;
55
+ if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
56
+ headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
83
57
  this[cacheKey] = [init?.status || 200, body, headers];
84
58
  }
85
59
  }
60
+ get headers() {
61
+ const cache = this[cacheKey];
62
+ if (cache) {
63
+ if (!(cache[2] instanceof Headers)) {
64
+ cache[2] = new Headers(cache[2]);
65
+ }
66
+ return cache[2];
67
+ }
68
+ return this[getResponseCache]().headers;
69
+ }
70
+ get status() {
71
+ return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
72
+ }
73
+ get ok() {
74
+ const status = this.status;
75
+ return status >= 200 && status < 300;
76
+ }
86
77
  };
87
- [
88
- "body",
89
- "bodyUsed",
90
- "headers",
91
- "ok",
92
- "redirected",
93
- "status",
94
- "statusText",
95
- "trailers",
96
- "type",
97
- "url"
98
- ].forEach((k) => {
78
+ ["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
99
79
  Object.defineProperty(Response.prototype, k, {
100
80
  get() {
101
81
  return this[getResponseCache]()[k];
@@ -111,26 +91,9 @@ var Response = class _Response {
111
91
  });
112
92
  Object.setPrototypeOf(Response, GlobalResponse);
113
93
  Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype);
114
- var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
115
- (k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
116
- );
117
- if (!stateKey) {
118
- console.warn("Failed to find Response internal state key");
119
- }
120
- function getInternalBody(response) {
121
- if (!stateKey) {
122
- return;
123
- }
124
- if (response instanceof Response) {
125
- response = response[getResponseCache]();
126
- }
127
- const state = response[stateKey];
128
- return state && state.body || void 0;
129
- }
130
94
  // Annotate the CommonJS export names for ESM import in node:
131
95
  0 && (module.exports = {
132
96
  GlobalResponse,
133
97
  Response,
134
- cacheKey,
135
- getInternalBody
98
+ cacheKey
136
99
  });
package/dist/response.mjs CHANGED
@@ -1,24 +1,3 @@
1
- // src/utils.ts
2
- var buildOutgoingHttpHeaders = (headers) => {
3
- const res = {};
4
- if (!(headers instanceof Headers)) {
5
- headers = new Headers(headers ?? void 0);
6
- }
7
- const cookies = [];
8
- for (const [k, v] of headers) {
9
- if (k === "set-cookie") {
10
- cookies.push(v);
11
- } else {
12
- res[k] = v;
13
- }
14
- }
15
- if (cookies.length > 0) {
16
- res["set-cookie"] = cookies;
17
- }
18
- res["content-type"] ??= "text/plain; charset=UTF-8";
19
- return res;
20
- };
21
-
22
1
  // src/response.ts
23
2
  var responseCache = Symbol("responseCache");
24
3
  var getResponseCache = Symbol("getResponseCache");
@@ -32,6 +11,7 @@ var Response = class _Response {
32
11
  return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
33
12
  }
34
13
  constructor(body, init) {
14
+ let headers;
35
15
  this.#body = body;
36
16
  if (init instanceof _Response) {
37
17
  const cachedGlobalResponse = init[responseCache];
@@ -41,32 +21,35 @@ var Response = class _Response {
41
21
  return;
42
22
  } else {
43
23
  this.#init = init.#init;
24
+ headers = new Headers(init.#init.headers);
44
25
  }
45
26
  } else {
46
27
  this.#init = init;
47
28
  }
48
- if (typeof body === "string" || typeof body?.getReader !== "undefined") {
49
- let headers = init?.headers || { "content-type": "text/plain; charset=UTF-8" };
50
- if (headers instanceof Headers) {
51
- headers = buildOutgoingHttpHeaders(headers);
52
- }
53
- ;
29
+ if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
30
+ headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
54
31
  this[cacheKey] = [init?.status || 200, body, headers];
55
32
  }
56
33
  }
34
+ get headers() {
35
+ const cache = this[cacheKey];
36
+ if (cache) {
37
+ if (!(cache[2] instanceof Headers)) {
38
+ cache[2] = new Headers(cache[2]);
39
+ }
40
+ return cache[2];
41
+ }
42
+ return this[getResponseCache]().headers;
43
+ }
44
+ get status() {
45
+ return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
46
+ }
47
+ get ok() {
48
+ const status = this.status;
49
+ return status >= 200 && status < 300;
50
+ }
57
51
  };
58
- [
59
- "body",
60
- "bodyUsed",
61
- "headers",
62
- "ok",
63
- "redirected",
64
- "status",
65
- "statusText",
66
- "trailers",
67
- "type",
68
- "url"
69
- ].forEach((k) => {
52
+ ["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
70
53
  Object.defineProperty(Response.prototype, k, {
71
54
  get() {
72
55
  return this[getResponseCache]()[k];
@@ -82,25 +65,8 @@ var Response = class _Response {
82
65
  });
83
66
  Object.setPrototypeOf(Response, GlobalResponse);
84
67
  Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype);
85
- var stateKey = Reflect.ownKeys(new GlobalResponse()).find(
86
- (k) => typeof k === "symbol" && k.toString() === "Symbol(state)"
87
- );
88
- if (!stateKey) {
89
- console.warn("Failed to find Response internal state key");
90
- }
91
- function getInternalBody(response) {
92
- if (!stateKey) {
93
- return;
94
- }
95
- if (response instanceof Response) {
96
- response = response[getResponseCache]();
97
- }
98
- const state = response[stateKey];
99
- return state && state.body || void 0;
100
- }
101
68
  export {
102
69
  GlobalResponse,
103
70
  Response,
104
- cacheKey,
105
- getInternalBody
71
+ cacheKey
106
72
  };