hono 2.0.8 → 2.0.9

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/README.md CHANGED
@@ -46,13 +46,13 @@ export default app
46
46
  **Hono is fastest**, compared to other routers for Cloudflare Workers.
47
47
 
48
48
  ```plain
49
- hono - trie-router(default) x 389,510 ops/sec ±3.16% (85 runs sampled)
50
- hono - regexp-router x 452,290 ops/sec ±2.64% (84 runs sampled)
51
- itty-router x 206,013 ops/sec ±3.39% (90 runs sampled)
52
- sunder x 323,131 ops/sec ±0.75% (97 runs sampled)
53
- worktop x 191,218 ops/sec ±2.70% (91 runs sampled)
49
+ hono - trie-router(default) x 424,449 ops/sec ±4.98% (77 runs sampled)
50
+ hono - regexp-router x 516,228 ops/sec ±4.79% (81 runs sampled)
51
+ itty-router x 206,641 ops/sec ±3.59% (87 runs sampled)
52
+ sunder x 319,500 ops/sec ±1.33% (93 runs sampled)
53
+ worktop x 187,280 ops/sec ±3.09% (87 runs sampled)
54
54
  Fastest is hono - regexp-router
55
- ✨ Done in 43.56s.
55
+ ✨ Done in 38.32s.
56
56
  ```
57
57
 
58
58
  ## Documentation
package/dist/compose.d.ts CHANGED
@@ -1,6 +1,2 @@
1
1
  import type { ErrorHandler, NotFoundHandler } from './hono';
2
- export declare const compose: <C>(middleware: Function[], onError?: ErrorHandler<{
3
- [x: string]: any;
4
- }> | undefined, onNotFound?: NotFoundHandler<{
5
- [x: string]: any;
6
- }> | undefined) => (context: C, next?: Function | undefined) => Promise<C>;
2
+ export declare const compose: <C>(middleware: Function[], onError?: ErrorHandler, onNotFound?: NotFoundHandler) => (context: C, next?: Function) => Promise<C>;
package/dist/compose.js CHANGED
@@ -10,7 +10,7 @@ const compose = (middleware, onError, onNotFound) => {
10
10
  return dispatch(0);
11
11
  async function dispatch(i) {
12
12
  if (i <= index) {
13
- return Promise.reject(new Error('next() called multiple times'));
13
+ throw new Error('next() called multiple times');
14
14
  }
15
15
  let handler = middleware[i];
16
16
  index = i;
@@ -20,27 +20,29 @@ const compose = (middleware, onError, onNotFound) => {
20
20
  if (context instanceof context_1.HonoContext && context.finalized === false && onNotFound) {
21
21
  context.res = await onNotFound(context);
22
22
  }
23
- return Promise.resolve(context);
24
- }
25
- return Promise.resolve(handler(context, () => dispatch(i + 1)))
26
- .then((res) => {
27
- // If handler return Response like `return c.text('foo')`
28
- if (res && context instanceof context_1.HonoContext) {
29
- context.res = res;
30
- }
31
23
  return context;
32
- })
33
- .catch((err) => {
24
+ }
25
+ let res;
26
+ let isError = false;
27
+ try {
28
+ const tmp = handler(context, () => dispatch(i + 1));
29
+ res = tmp instanceof Promise ? await tmp : tmp;
30
+ }
31
+ catch (err) {
34
32
  if (context instanceof context_1.HonoContext && onError) {
35
33
  if (err instanceof Error) {
36
- context.res = onError(err, context);
34
+ isError = true;
35
+ res = onError(err, context);
37
36
  }
38
- return context;
39
37
  }
40
- else {
38
+ if (!res) {
41
39
  throw err;
42
40
  }
43
- });
41
+ }
42
+ if (res && context instanceof context_1.HonoContext && (!context.finalized || isError)) {
43
+ context.res = res;
44
+ }
45
+ return context;
44
46
  }
45
47
  };
46
48
  };
package/dist/context.d.ts CHANGED
@@ -52,8 +52,10 @@ export declare class HonoContext<RequestParamKeyType extends string = string, E
52
52
  set res(_res: Response);
53
53
  header(name: string, value: string): void;
54
54
  status(status: StatusCode): void;
55
+ set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void;
55
56
  set(key: string, value: any): void;
56
- get(key: string): any;
57
+ get<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key];
58
+ get<T = any>(key: string): T;
57
59
  pretty(prettyJSON: boolean, space?: number): void;
58
60
  newResponse(data: Data | null, status: StatusCode, headers?: Headers): Response;
59
61
  body(data: Data | null, status?: StatusCode, headers?: Headers): Response;
package/dist/hono.d.ts CHANGED
@@ -50,7 +50,7 @@ export declare class Hono<E extends Env = Env, P extends string = '/'> extends H
50
50
  private matchRoute;
51
51
  private dispatch;
52
52
  handleEvent(event: FetchEvent): Promise<Response>;
53
- fetch: (request: Request, env?: E | undefined, executionCtx?: ExecutionContext | undefined) => Promise<Response>;
53
+ fetch: (request: Request, env?: E, executionCtx?: ExecutionContext) => Promise<Response>;
54
54
  request(input: RequestInfo, requestInit?: RequestInit): Promise<Response>;
55
55
  }
56
56
  export {};
@@ -4,5 +4,5 @@ declare type EncodingType = 'gzip' | 'deflate';
4
4
  interface CompressionOptions {
5
5
  encoding?: EncodingType;
6
6
  }
7
- export declare const compress: (options?: CompressionOptions | undefined) => (ctx: Context, next: Next) => Promise<void>;
7
+ export declare const compress: (options?: CompressionOptions) => (ctx: Context, next: Next) => Promise<void>;
8
8
  export {};
@@ -8,5 +8,5 @@ declare type CORSOptions = {
8
8
  credentials?: boolean;
9
9
  exposeHeaders?: string[];
10
10
  };
11
- export declare const cors: (options?: CORSOptions | undefined) => (c: Context, next: Next) => Promise<void>;
11
+ export declare const cors: (options?: CORSOptions) => (c: Context, next: Next) => Promise<void>;
12
12
  export {};
@@ -1,3 +1,3 @@
1
1
  export declare const equal: (a: ArrayBuffer, b: ArrayBuffer) => boolean;
2
- export declare const timingSafeEqual: (a: string | object | boolean, b: string | object | boolean, hashFunction?: Function | undefined) => Promise<boolean>;
2
+ export declare const timingSafeEqual: (a: string | object | boolean, b: string | object | boolean, hashFunction?: Function) => Promise<boolean>;
3
3
  export declare const bufferToString: (buffer: ArrayBuffer) => string;
@@ -3,4 +3,4 @@ export declare type KVAssetOptions = {
3
3
  manifest?: object | string;
4
4
  namespace?: KVNamespace;
5
5
  };
6
- export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions | undefined) => Promise<ArrayBuffer | null>;
6
+ export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions) => Promise<ArrayBuffer | null>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",