hono 4.12.28 → 4.12.30

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.
@@ -348,10 +348,7 @@ var defaultIsContentTypeBinary = (contentType) => {
348
348
  );
349
349
  };
350
350
  var isContentEncodingBinary = (contentEncoding) => {
351
- if (contentEncoding === null) {
352
- return false;
353
- }
354
- return /^(gzip|deflate|compress|br)/.test(contentEncoding);
351
+ return !!contentEncoding && !/^identity$/i.test(contentEncoding);
355
352
  };
356
353
  export {
357
354
  ALBProcessor,
@@ -15,21 +15,31 @@ var convertHeaders = (headers) => {
15
15
  var handle = (app) => {
16
16
  return async (event, ...args) => {
17
17
  const [context, callback] = args;
18
+ let callbackError = null;
19
+ let callbackResult;
18
20
  const res = await app.fetch(createRequest(event), {
19
21
  event,
20
22
  context,
21
23
  callback: (err, result) => {
24
+ if (!callbackError && !callbackResult) {
25
+ callbackError = err;
26
+ callbackResult = result;
27
+ }
22
28
  callback?.(err, result);
23
29
  },
24
30
  config: event.Records[0].cf.config,
25
31
  request: event.Records[0].cf.request,
26
32
  response: event.Records[0].cf.response
27
33
  });
28
- return createResult(res);
34
+ if (callbackError) {
35
+ throw callbackError;
36
+ }
37
+ return callbackResult ?? createResult(res);
29
38
  };
30
39
  };
31
40
  var createResult = async (res) => {
32
- const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "");
41
+ const contentEncoding = res.headers.get("content-encoding");
42
+ const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "") || !!contentEncoding && !/^identity$/i.test(contentEncoding);
33
43
  const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text();
34
44
  return {
35
45
  status: res.status.toString(),
@@ -378,10 +378,7 @@ const defaultIsContentTypeBinary = (contentType) => {
378
378
  );
379
379
  };
380
380
  const isContentEncodingBinary = (contentEncoding) => {
381
- if (contentEncoding === null) {
382
- return false;
383
- }
384
- return /^(gzip|deflate|compress|br)/.test(contentEncoding);
381
+ return !!contentEncoding && !/^identity$/i.test(contentEncoding);
385
382
  };
386
383
  // Annotate the CommonJS export names for ESM import in node:
387
384
  0 && (module.exports = {
@@ -48,21 +48,31 @@ const convertHeaders = (headers) => {
48
48
  const handle = (app) => {
49
49
  return async (event, ...args) => {
50
50
  const [context, callback] = args;
51
+ let callbackError = null;
52
+ let callbackResult;
51
53
  const res = await app.fetch(createRequest(event), {
52
54
  event,
53
55
  context,
54
56
  callback: (err, result) => {
57
+ if (!callbackError && !callbackResult) {
58
+ callbackError = err;
59
+ callbackResult = result;
60
+ }
55
61
  callback?.(err, result);
56
62
  },
57
63
  config: event.Records[0].cf.config,
58
64
  request: event.Records[0].cf.request,
59
65
  response: event.Records[0].cf.response
60
66
  });
61
- return createResult(res);
67
+ if (callbackError) {
68
+ throw callbackError;
69
+ }
70
+ return callbackResult ?? createResult(res);
62
71
  };
63
72
  };
64
73
  const createResult = async (res) => {
65
- const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "");
74
+ const contentEncoding = res.headers.get("content-encoding");
75
+ const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "") || !!contentEncoding && !/^identity$/i.test(contentEncoding);
66
76
  const body = isBase64Encoded ? (0, import_encode.encodeBase64)(await res.arrayBuffer()) : await res.text();
67
77
  return {
68
78
  status: res.status.toString(),
@@ -184,7 +184,16 @@ const hc = (baseUrl, options) => createProxy(function proxyCallback(opts) {
184
184
  });
185
185
  if (method) {
186
186
  options ??= {};
187
- const args = (0, import_utils.deepMerge)(options, { ...opts.args[1] });
187
+ const reqOptions = { ...opts.args[1] };
188
+ const baseHeaders = options.headers;
189
+ const reqHeaders = reqOptions.headers;
190
+ if (baseHeaders && reqHeaders) {
191
+ reqOptions.headers = async () => ({
192
+ ...typeof baseHeaders === "function" ? await baseHeaders() : baseHeaders,
193
+ ...typeof reqHeaders === "function" ? await reqHeaders() : reqHeaders
194
+ });
195
+ }
196
+ const args = (0, import_utils.deepMerge)(options, reqOptions);
188
197
  return req.fetch(opts.args[0], args);
189
198
  }
190
199
  return req;
@@ -36,7 +36,7 @@ const mergePath = (base, path) => {
36
36
  };
37
37
  const replaceUrlParam = (urlString, params) => {
38
38
  for (const [k, v] of Object.entries(params)) {
39
- const reg = new RegExp("/:" + k + "(?:{[^/]+})?\\??");
39
+ const reg = new RegExp("/:" + k + "(?:{[^/]+})?\\??(?=/|$)");
40
40
  urlString = urlString.replace(reg, v ? `/${v}` : "");
41
41
  }
42
42
  return urlString;
@@ -55,7 +55,7 @@ const cache = (options) => {
55
55
  );
56
56
  const addHeader = (c, responseVary) => {
57
57
  if (cacheControlDirectives) {
58
- const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? [];
58
+ const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0].toLowerCase()) ?? [];
59
59
  for (const directive of cacheControlDirectives) {
60
60
  let [name, value] = directive.trim().split("=", 2);
61
61
  name = name.toLowerCase();
@@ -57,7 +57,8 @@ const compress = (options) => {
57
57
  return async function compress2(ctx, next) {
58
58
  await next();
59
59
  const contentLength = ctx.res.headers.get("Content-Length");
60
- if (ctx.res.headers.has("Content-Encoding") || // already encoded
60
+ if (ctx.res.status === 206 || // partial content, Content-Range refers to the uncompressed bytes
61
+ ctx.res.headers.has("Content-Encoding") || // already encoded
61
62
  ctx.res.headers.has("Transfer-Encoding") || // already encoded or chunked
62
63
  ctx.req.method === "HEAD" || // HEAD request
63
64
  contentLength && Number(contentLength) < threshold || // content-length below threshold
@@ -70,7 +70,8 @@ const etag = (options) => {
70
70
  }
71
71
  etag3 = weak ? `W/"${hash}"` : `"${hash}"`;
72
72
  }
73
- if (etagMatches(etag3, ifNoneMatch)) {
73
+ const matched = ifNoneMatch === "*" ? (c.req.method === "GET" || c.req.method === "HEAD") && res.ok : etagMatches(etag3, ifNoneMatch);
74
+ if (matched) {
74
75
  c.res = new Response(null, {
75
76
  status: 304,
76
77
  statusText: "Not Modified",
@@ -80,11 +80,13 @@ const methodOverride = (options) => async function methodOverride2(c, next) {
80
80
  if (method) {
81
81
  const url = new URL(c.req.url);
82
82
  url.searchParams.delete(queryName);
83
- const request = new Request(url.toString(), {
83
+ const requestInit = {
84
84
  body: c.req.raw.body,
85
85
  headers: c.req.raw.headers,
86
- method
87
- });
86
+ method,
87
+ duplex: c.req.raw.body ? "half" : void 0
88
+ };
89
+ const request = new Request(url.toString(), requestInit);
88
90
  return app.fetch(request, c.env, getExecutionCtx(c));
89
91
  }
90
92
  }
@@ -155,6 +155,15 @@ class Node {
155
155
  if (m) {
156
156
  params[name] = m[0];
157
157
  this.#pushHandlerSets(handlerSets, child, method, node.#params, params);
158
+ if (m[0].length === restPathString.length && child.#children["*"]) {
159
+ this.#pushHandlerSets(
160
+ handlerSets,
161
+ child.#children["*"],
162
+ method,
163
+ node.#params,
164
+ params
165
+ );
166
+ }
158
167
  if (hasChildren(child.#children)) {
159
168
  child.#params = params;
160
169
  const componentCount = m[0].match(/\//)?.length ?? 0;
@@ -170,7 +170,16 @@ var hc = (baseUrl, options) => createProxy(function proxyCallback(opts) {
170
170
  });
171
171
  if (method) {
172
172
  options ??= {};
173
- const args = deepMerge(options, { ...opts.args[1] });
173
+ const reqOptions = { ...opts.args[1] };
174
+ const baseHeaders = options.headers;
175
+ const reqHeaders = reqOptions.headers;
176
+ if (baseHeaders && reqHeaders) {
177
+ reqOptions.headers = async () => ({
178
+ ...typeof baseHeaders === "function" ? await baseHeaders() : baseHeaders,
179
+ ...typeof reqHeaders === "function" ? await reqHeaders() : reqHeaders
180
+ });
181
+ }
182
+ const args = deepMerge(options, reqOptions);
174
183
  return req.fetch(opts.args[0], args);
175
184
  }
176
185
  return req;
@@ -8,7 +8,7 @@ var mergePath = (base, path) => {
8
8
  };
9
9
  var replaceUrlParam = (urlString, params) => {
10
10
  for (const [k, v] of Object.entries(params)) {
11
- const reg = new RegExp("/:" + k + "(?:{[^/]+})?\\??");
11
+ const reg = new RegExp("/:" + k + "(?:{[^/]+})?\\??(?=/|$)");
12
12
  urlString = urlString.replace(reg, v ? `/${v}` : "");
13
13
  }
14
14
  return urlString;
@@ -34,7 +34,7 @@ var cache = (options) => {
34
34
  );
35
35
  const addHeader = (c, responseVary) => {
36
36
  if (cacheControlDirectives) {
37
- const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? [];
37
+ const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0].toLowerCase()) ?? [];
38
38
  for (const directive of cacheControlDirectives) {
39
39
  let [name, value] = directive.trim().split("=", 2);
40
40
  name = name.toLowerCase();
@@ -35,7 +35,8 @@ var compress = (options) => {
35
35
  return async function compress2(ctx, next) {
36
36
  await next();
37
37
  const contentLength = ctx.res.headers.get("Content-Length");
38
- if (ctx.res.headers.has("Content-Encoding") || // already encoded
38
+ if (ctx.res.status === 206 || // partial content, Content-Range refers to the uncompressed bytes
39
+ ctx.res.headers.has("Content-Encoding") || // already encoded
39
40
  ctx.res.headers.has("Transfer-Encoding") || // already encoded or chunked
40
41
  ctx.req.method === "HEAD" || // HEAD request
41
42
  contentLength && Number(contentLength) < threshold || // content-length below threshold
@@ -48,7 +48,8 @@ var etag = (options) => {
48
48
  }
49
49
  etag3 = weak ? `W/"${hash}"` : `"${hash}"`;
50
50
  }
51
- if (etagMatches(etag3, ifNoneMatch)) {
51
+ const matched = ifNoneMatch === "*" ? (c.req.method === "GET" || c.req.method === "HEAD") && res.ok : etagMatches(etag3, ifNoneMatch);
52
+ if (matched) {
52
53
  c.res = new Response(null, {
53
54
  status: 304,
54
55
  statusText: "Not Modified",
@@ -59,11 +59,13 @@ var methodOverride = (options) => async function methodOverride2(c, next) {
59
59
  if (method) {
60
60
  const url = new URL(c.req.url);
61
61
  url.searchParams.delete(queryName);
62
- const request = new Request(url.toString(), {
62
+ const requestInit = {
63
63
  body: c.req.raw.body,
64
64
  headers: c.req.raw.headers,
65
- method
66
- });
65
+ method,
66
+ duplex: c.req.raw.body ? "half" : void 0
67
+ };
68
+ const request = new Request(url.toString(), requestInit);
67
69
  return app.fetch(request, c.env, getExecutionCtx(c));
68
70
  }
69
71
  }
@@ -134,6 +134,15 @@ var Node = class _Node {
134
134
  if (m) {
135
135
  params[name] = m[0];
136
136
  this.#pushHandlerSets(handlerSets, child, method, node.#params, params);
137
+ if (m[0].length === restPathString.length && child.#children["*"]) {
138
+ this.#pushHandlerSets(
139
+ handlerSets,
140
+ child.#children["*"],
141
+ method,
142
+ node.#params,
143
+ params
144
+ );
145
+ }
137
146
  if (hasChildren(child.#children)) {
138
147
  child.#params = params;
139
148
  const componentCount = m[0].match(/\//)?.length ?? 0;
@@ -81,7 +81,7 @@ interface CloudFrontResult {
81
81
  body?: string;
82
82
  bodyEncoding?: 'text' | 'base64';
83
83
  }
84
- export declare const handle: (app: Hono<any>) => ((event: CloudFrontEdgeEvent, context?: CloudFrontContext, callback?: Callback) => Promise<CloudFrontResult>);
84
+ export declare const handle: (app: Hono<any>) => ((event: CloudFrontEdgeEvent, context?: CloudFrontContext, callback?: Callback) => Promise<CloudFrontResult | CloudFrontRequest>);
85
85
  export declare const createBody: (method: string, requestBody: CloudFrontRequest["body"]) => string | Uint8Array<ArrayBuffer> | undefined;
86
86
  export declare const isContentTypeBinary: (contentType: string) => boolean;
87
87
  export {};
@@ -31,7 +31,7 @@ export type WSReadyState = 0 | 1 | 2 | 3;
31
31
  * An argument for WSContext class
32
32
  */
33
33
  export interface WSContextInit<T = unknown> {
34
- send(data: string | ArrayBuffer | Uint8Array, options: SendOptions): void;
34
+ send(data: string | ArrayBuffer | Uint8Array<ArrayBuffer>, options: SendOptions): void;
35
35
  close(code?: number, reason?: string): void;
36
36
  raw?: T;
37
37
  readyState: WSReadyState;
@@ -98,5 +98,23 @@ export declare function validateOptions(options: DetectorOptions): void;
98
98
  * Language detector middleware factory
99
99
  * @param userOptions Configuration options for the language detector
100
100
  * @returns Hono middleware function
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * type Variables = LanguageVariables
105
+ * const app = new Hono<{ Variables: Variables }>()
106
+ *
107
+ * app.use(
108
+ * languageDetector({
109
+ * supportedLanguages: ['en', 'ja'], // Must include fallback
110
+ * fallbackLanguage: 'en', // Required
111
+ * })
112
+ * )
113
+ *
114
+ * app.get('/', (c) => {
115
+ * const lang = c.get('language')
116
+ * return c.text(`Current language: ${lang}`)
117
+ * })
118
+ * ```
101
119
  */
102
120
  export declare const languageDetector: (userOptions: Partial<DetectorOptions>) => MiddlewareHandler;
@@ -42,7 +42,7 @@ export type JSONParsed<T, TError = bigint | ReadonlyArray<bigint>> = T extends {
42
42
  toJSON(): unknown;
43
43
  } ? {} : JSONParsed<J, TError> : T extends JSONPrimitive ? T : T extends InvalidJSONValue ? never : T extends ReadonlyArray<unknown> ? {
44
44
  [K in keyof T]: JSONParsed<InvalidToNull<T[K]>, TError>;
45
- } : T extends Set<unknown> | Map<unknown, unknown> | Record<string, never> ? {} : T extends object ? T[keyof T] extends TError ? never : {
45
+ } extends infer A ? A extends ReadonlyArray<unknown> ? A : JSONParsed<InvalidToNull<T[number]>, TError>[] : never : T extends Set<unknown> | Map<unknown, unknown> | Record<string, never> ? {} : T extends object ? T[keyof T] extends TError ? never : {
46
46
  [K in keyof OmitSymbolKeys<T> as IsInvalid<T[K]> extends true ? never : K]: boolean extends IsInvalid<T[K]> ? JSONParsed<T[K], TError> | undefined : JSONParsed<T[K], TError>;
47
47
  } : T extends unknown ? T extends TError ? never : JSONValue : never;
48
48
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "4.12.28",
3
+ "version": "4.12.30",
4
4
  "description": "Web framework built on Web Standards",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",
@@ -11,7 +11,7 @@
11
11
  "!dist/**/*.tsbuildinfo"
12
12
  ],
13
13
  "scripts": {
14
- "test": "tsc --noEmit && vitest --run",
14
+ "test": "tsc -p tsconfig.spec.json && vitest --run",
15
15
  "test:watch": "vitest --watch",
16
16
  "test:deno": "deno test --allow-read --allow-env --allow-write --allow-net -c runtime-tests/deno/deno.json runtime-tests/deno && deno test --no-lock -c runtime-tests/deno-jsx/deno.precompile.json runtime-tests/deno-jsx && deno test --no-lock -c runtime-tests/deno-jsx/deno.react-jsx.json runtime-tests/deno-jsx",
17
17
  "test:bun": "bun test --jsx-import-source ../../src/jsx runtime-tests/bun/*",
@@ -666,7 +666,7 @@
666
666
  "nodejs"
667
667
  ],
668
668
  "devDependencies": {
669
- "@hono/eslint-config": "^2.1.0",
669
+ "@hono/eslint-config": "^2.1.1",
670
670
  "@hono/node-server": "^2.0.2",
671
671
  "@types/jsdom": "^21.1.7",
672
672
  "@types/node": "^24.3.0",
@@ -684,7 +684,7 @@
684
684
  "pkg-pr-new": "^0.0.53",
685
685
  "prettier": "3.7.4",
686
686
  "publint": "0.3.15",
687
- "typescript": "^5.9.2",
687
+ "typescript": "^6.0.3",
688
688
  "undici": "^6.27.0",
689
689
  "vite-plugin-fastly-js-compute": "^0.4.2",
690
690
  "vitest": "^4.1.9",