hono 2.0.2 → 2.0.3

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
@@ -61,19 +61,19 @@ The documentation is available on [honojs.dev](https://honojs.dev).
61
61
 
62
62
  ## Migration
63
63
 
64
- Migration guide is available on [docs/MIGRATION.md](docs/MIGRATION.md)
64
+ Migration guide is available on [docs/MIGRATION.md](docs/MIGRATION.md).
65
65
 
66
66
  ## Contributing
67
67
 
68
68
  Contributions Welcome! You can contribute in the following ways.
69
69
 
70
- - Fix bugs.
71
- - Create built-in or third-party middleware.
72
- - Propose new feature.
73
- - Refactor the code.
74
- - Write an article about Hono on your Blog.
75
- - Fix a typo.
76
- - etc.
70
+ - Create an Issue - Propose a new feature. Report a bug.
71
+ - Pull Request - Fix a bug and typo. Refactor the code.
72
+ - Create third-party middleware - Instruct below.
73
+ - Share - Share your thoughts on the Blog, Twitter, and others.
74
+ - Make your application - Please try to use Hono.
75
+
76
+ For more details, see [docs/CONTRIBUTING.md](docs/CONTRIBUTING.md).
77
77
 
78
78
  ## Contributors
79
79
 
package/dist/compose.d.ts CHANGED
@@ -1,2 +1,6 @@
1
1
  import type { ErrorHandler, NotFoundHandler } from './hono';
2
- export declare const compose: <C>(middleware: Function[], onError?: ErrorHandler, onNotFound?: NotFoundHandler) => (context: C, next?: Function) => Promise<C>;
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>;
package/dist/hono.d.ts CHANGED
@@ -48,7 +48,7 @@ export declare class Hono<E extends Env = Env, P extends string = '/'> extends H
48
48
  private matchRoute;
49
49
  private dispatch;
50
50
  handleEvent(event: FetchEvent): Promise<Response>;
51
- fetch: (request: Request, env?: E, executionCtx?: ExecutionContext) => Promise<Response>;
51
+ fetch: (request: Request, env?: E | undefined, executionCtx?: ExecutionContext | undefined) => Promise<Response>;
52
52
  request(input: RequestInfo, requestInit?: RequestInit): Promise<Response>;
53
53
  }
54
54
  export {};
@@ -0,0 +1,7 @@
1
+ import type { Context } from '../../context';
2
+ import type { Next } from '../../hono';
3
+ export declare const cache: (options: {
4
+ cacheName: string;
5
+ wait?: boolean;
6
+ cacheControl?: string;
7
+ }) => (c: Context, next: Next) => Promise<Response | undefined>;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cache = void 0;
4
+ const cache = (options) => {
5
+ if (options.wait === undefined) {
6
+ options.wait = false;
7
+ }
8
+ const addHeader = (response) => {
9
+ if (options.cacheControl)
10
+ response.headers.append('Cache-Control', options.cacheControl);
11
+ };
12
+ return async (c, next) => {
13
+ const key = c.req;
14
+ const cache = await caches.open(options.cacheName);
15
+ const response = await cache.match(key);
16
+ if (!response) {
17
+ await next();
18
+ addHeader(c.res);
19
+ const response = c.res.clone();
20
+ if (options.wait) {
21
+ await cache.put(key, response);
22
+ }
23
+ else {
24
+ c.executionCtx.waitUntil(cache.put(key, response));
25
+ }
26
+ }
27
+ else {
28
+ return response;
29
+ }
30
+ };
31
+ };
32
+ exports.cache = cache;
@@ -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) => (ctx: Context, next: Next) => Promise<void>;
7
+ export declare const compress: (options?: CompressionOptions | undefined) => (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) => (c: Context, next: Next) => Promise<void>;
11
+ export declare const cors: (options?: CORSOptions | undefined) => (c: Context, next: Next) => Promise<void>;
12
12
  export {};
@@ -6,11 +6,11 @@ declare global {
6
6
  }
7
7
  }
8
8
  }
9
- export declare const jsx: (tag: string | Function, props: Record<string, any>, ...children: (string | HtmlEscapedString)[]) => HtmlEscapedString;
9
+ export { jsxFn as jsx };
10
+ declare const jsxFn: (tag: string | Function, props: Record<string, any>, ...children: (string | HtmlEscapedString)[]) => HtmlEscapedString;
10
11
  declare type FC<T = Record<string, any>> = (props: T) => HtmlEscapedString;
11
12
  export declare const memo: <T>(component: FC<T>, propsAreEqual?: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean) => FC<T>;
12
13
  export declare const Fragment: (props: {
13
14
  key?: string;
14
15
  children?: any;
15
16
  }) => HtmlEscapedString;
16
- export {};
@@ -19,7 +19,7 @@ const emptyTags = [
19
19
  'track',
20
20
  'wbr',
21
21
  ];
22
- const jsx = (tag, props, ...children) => {
22
+ const jsxFn = (tag, props, ...children) => {
23
23
  if (typeof tag === 'function') {
24
24
  return tag.call(null, { ...props, children: children.length <= 1 ? children[0] : children });
25
25
  }
@@ -67,7 +67,7 @@ const jsx = (tag, props, ...children) => {
67
67
  escapedString.isEscaped = true;
68
68
  return escapedString;
69
69
  };
70
- exports.jsx = jsx;
70
+ exports.jsx = jsxFn;
71
71
  const shallowEqual = (a, b) => {
72
72
  if (a === b) {
73
73
  return true;
@@ -97,6 +97,6 @@ const memo = (component, propsAreEqual = shallowEqual) => {
97
97
  };
98
98
  exports.memo = memo;
99
99
  const Fragment = (props) => {
100
- return (0, exports.jsx)('', {}, ...(props.children || []));
100
+ return jsxFn('', {}, ...(props.children || []));
101
101
  };
102
102
  exports.Fragment = Fragment;
@@ -0,0 +1,2 @@
1
+ import type { HtmlEscapedString } from '../html';
2
+ export declare function jsxDEV(tag: string | Function, props: Record<string, any>): HtmlEscapedString;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsxDEV = void 0;
4
+ const _1 = require(".");
5
+ function jsxDEV(tag, props) {
6
+ const children = props.children ?? [];
7
+ delete props['children'];
8
+ return (0, _1.jsx)(tag, props, ...children);
9
+ }
10
+ exports.jsxDEV = jsxDEV;
@@ -0,0 +1,2 @@
1
+ export { jsxDEV as jsx } from './jsx-dev-runtime';
2
+ export { jsxDEV as jsxs } from './jsx-dev-runtime';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsxs = exports.jsx = void 0;
4
+ var jsx_dev_runtime_1 = require("./jsx-dev-runtime");
5
+ Object.defineProperty(exports, "jsx", { enumerable: true, get: function () { return jsx_dev_runtime_1.jsxDEV; } });
6
+ var jsx_dev_runtime_2 = require("./jsx-dev-runtime");
7
+ Object.defineProperty(exports, "jsxs", { enumerable: true, get: function () { return jsx_dev_runtime_2.jsxDEV; } });
@@ -102,8 +102,21 @@ class Node {
102
102
  const part = parts[i];
103
103
  const isLast = i === len - 1;
104
104
  const tempNodes = [];
105
+ let matched = false;
105
106
  for (let j = 0, len2 = curNodes.length; j < len2; j++) {
106
107
  const node = curNodes[j];
108
+ const nextNode = node.children[part];
109
+ if (nextNode) {
110
+ if (isLast === true) {
111
+ // '/hello/*' => match '/hello'
112
+ if (nextNode.children['*']) {
113
+ handlerSets.push(...this.getHandlerSets(nextNode.children['*'], method, true));
114
+ }
115
+ handlerSets.push(...this.getHandlerSets(nextNode, method));
116
+ matched = true;
117
+ }
118
+ tempNodes.push(nextNode);
119
+ }
107
120
  for (let k = 0, len3 = node.patterns.length; k < len3; k++) {
108
121
  const pattern = node.patterns[k];
109
122
  // Wildcard
@@ -128,22 +141,15 @@ class Node {
128
141
  }
129
142
  tempNodes.push(node.children[key]);
130
143
  }
131
- if (typeof name === 'string') {
144
+ // '/book/a' => not-slug
145
+ // '/book/:slug' => slug
146
+ // GET /book/a ~> no-slug, param['slug'] => undefined
147
+ // GET /book/foo ~> slug, param['slug'] => foo
148
+ if (typeof name === 'string' && !matched) {
132
149
  params[name] = part;
133
150
  }
134
151
  }
135
152
  }
136
- const nextNode = node.children[part];
137
- if (nextNode) {
138
- if (isLast === true) {
139
- // '/hello/*' => match '/hello'
140
- if (nextNode.children['*']) {
141
- handlerSets.push(...this.getHandlerSets(nextNode.children['*'], method, true));
142
- }
143
- handlerSets.push(...this.getHandlerSets(nextNode, method));
144
- }
145
- tempNodes.push(nextNode);
146
- }
147
153
  }
148
154
  curNodes = tempNodes;
149
155
  }
@@ -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) => Promise<boolean>;
2
+ export declare const timingSafeEqual: (a: string | object | boolean, b: string | object | boolean, hashFunction?: Function | undefined) => 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) => Promise<ArrayBuffer | null>;
6
+ export declare const getContentFromKVAsset: (path: string, options?: KVAssetOptions | undefined) => Promise<ArrayBuffer | null>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,7 +10,8 @@
10
10
  "scripts": {
11
11
  "test": "jest",
12
12
  "test:deno": "deno test --allow-read deno_test",
13
- "test:bun": "bun wiptest bun_test/index.test.ts",
13
+ "test:bun": "bun wiptest --jsx-import-source ../src/middleware/jsx/jsx-dev-runtime bun_test/index.test.tsx",
14
+ "test:all": "yarn test && yarn test:deno && yarn test:bun",
14
15
  "lint": "eslint --ext js,ts src .eslintrc.js",
15
16
  "lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",
16
17
  "denoify": "rimraf deno_dist && denoify && rimraf 'deno_dist/**/*.test.ts'",
@@ -23,10 +24,14 @@
23
24
  ".": "./dist/index.js",
24
25
  "./basic-auth": "./dist/middleware/basic-auth/index.js",
25
26
  "./bearer-auth": "./dist/middleware/bearer-auth/index.js",
27
+ "./cache": "./dist/middleware/cache/index.js",
28
+ "./compress": "./dist/middleware/compress/index.js",
26
29
  "./cors": "./dist/middleware/cors/index.js",
27
30
  "./etag": "./dist/middleware/etag/index.js",
28
31
  "./html": "./dist/middleware/html/index.js",
29
32
  "./jsx": "./dist/middleware/jsx/index.js",
33
+ "./jsx/jsx-dev-runtime": "./dist/middleware/jsx/jsx-dev-runtime.js",
34
+ "./jsx/jsx-runtime": "./dist/middleware/jsx/jsx-runtime.js",
30
35
  "./jwt": "./dist/middleware/jwt/index.js",
31
36
  "./logger": "./dist/middleware/logger/index.js",
32
37
  "./powered-by": "./dist/middleware/powered-by/index.js",
@@ -47,8 +52,11 @@
47
52
  "bearer-auth": [
48
53
  "./dist/middleware/bearer-auth"
49
54
  ],
50
- "cookie": [
51
- "./dist/middleware/cookie"
55
+ "cache": [
56
+ "./dist/middleware/cache"
57
+ ],
58
+ "compress": [
59
+ "./dist/middleware/compress"
52
60
  ],
53
61
  "cors": [
54
62
  "./dist/middleware/cors"
@@ -62,6 +70,12 @@
62
70
  "jsx": [
63
71
  "./dist/middleware/jsx"
64
72
  ],
73
+ "jsx-runtime": [
74
+ "./dist/middleware/jsx/jsx-runtime.d.ts"
75
+ ],
76
+ "jsx-dev-runtime": [
77
+ "./dist/middleware/jsx/jsx-dev-runtime.d.ts"
78
+ ],
65
79
  "jwt": [
66
80
  "./dist/middleware/jwt"
67
81
  ],
@@ -126,7 +140,6 @@
126
140
  "@cloudflare/workers-types": "^3.7.1",
127
141
  "@types/crypto-js": "^4.1.1",
128
142
  "@types/jest": "^27.4.1",
129
- "@types/mustache": "^4.1.2",
130
143
  "@types/node": "^17.0.29",
131
144
  "@typescript-eslint/eslint-plugin": "^5.21.0",
132
145
  "@typescript-eslint/parser": "^5.21.0",
@@ -143,7 +156,6 @@
143
156
  "form-data": "^4.0.0",
144
157
  "jest": "27.5.1",
145
158
  "jest-environment-miniflare": "^2.6.0",
146
- "mustache": "^4.2.0",
147
159
  "np": "^7.6.2",
148
160
  "prettier": "^2.6.2",
149
161
  "rimraf": "^3.0.2",