hono 4.12.28 → 4.12.29

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;
@@ -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",
@@ -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;
@@ -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",
@@ -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 {};
@@ -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.29",
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/*",