hono 1.1.1 → 1.2.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.
Files changed (91) hide show
  1. package/README.md +126 -117
  2. package/dist/compose.d.ts +2 -2
  3. package/dist/compose.js +20 -8
  4. package/dist/compose.test.d.ts +1 -0
  5. package/dist/compose.test.js +511 -0
  6. package/dist/context.d.ts +8 -1
  7. package/dist/context.js +27 -37
  8. package/dist/context.test.d.ts +1 -0
  9. package/dist/context.test.js +127 -0
  10. package/dist/hono.d.ts +44 -21
  11. package/dist/hono.js +74 -43
  12. package/dist/hono.test.d.ts +1 -0
  13. package/dist/hono.test.js +592 -0
  14. package/dist/index.d.ts +2 -2
  15. package/dist/index.js +2 -1
  16. package/dist/middleware/basic-auth/index.js +2 -2
  17. package/dist/middleware/basic-auth/index.test.d.ts +1 -0
  18. package/dist/middleware/basic-auth/index.test.js +119 -0
  19. package/dist/middleware/body-parse/index.test.d.ts +1 -0
  20. package/dist/middleware/body-parse/index.test.js +59 -0
  21. package/dist/middleware/cookie/index.d.ts +1 -1
  22. package/dist/middleware/cookie/index.test.d.ts +1 -0
  23. package/dist/middleware/cookie/index.test.js +54 -0
  24. package/dist/middleware/cors/index.test.d.ts +1 -0
  25. package/dist/middleware/cors/index.test.js +59 -0
  26. package/dist/middleware/etag/index.test.d.ts +1 -0
  27. package/dist/middleware/etag/index.test.js +45 -0
  28. package/dist/middleware/graphql-server/index.js +1 -1
  29. package/dist/middleware/graphql-server/index.test.d.ts +1 -0
  30. package/dist/middleware/graphql-server/index.test.js +480 -0
  31. package/dist/middleware/graphql-server/parse-body.test.d.ts +1 -0
  32. package/dist/middleware/graphql-server/parse-body.test.js +57 -0
  33. package/dist/middleware/jwt/index.js +9 -11
  34. package/dist/middleware/jwt/index.test.d.ts +1 -0
  35. package/dist/middleware/jwt/index.test.js +51 -0
  36. package/dist/middleware/logger/index.test.d.ts +1 -0
  37. package/dist/middleware/logger/index.test.js +49 -0
  38. package/dist/middleware/mustache/index.test.d.ts +1 -0
  39. package/dist/middleware/mustache/index.test.js +49 -0
  40. package/dist/middleware/powered-by/index.test.d.ts +1 -0
  41. package/dist/middleware/powered-by/index.test.js +15 -0
  42. package/dist/middleware/pretty-json/index.test.d.ts +1 -0
  43. package/dist/middleware/pretty-json/index.test.js +28 -0
  44. package/dist/middleware/serve-static/index.test.d.ts +1 -0
  45. package/dist/middleware/serve-static/index.test.js +58 -0
  46. package/dist/router/reg-exp-router/index.d.ts +1 -1
  47. package/dist/router/reg-exp-router/index.js +1 -1
  48. package/dist/router/reg-exp-router/node.d.ts +3 -0
  49. package/dist/router/reg-exp-router/node.js +13 -7
  50. package/dist/router/reg-exp-router/router.d.ts +21 -2
  51. package/dist/router/reg-exp-router/router.js +301 -81
  52. package/dist/router/reg-exp-router/router.test.d.ts +1 -0
  53. package/dist/router/reg-exp-router/router.test.js +212 -0
  54. package/dist/router/reg-exp-router/trie.d.ts +7 -3
  55. package/dist/router/reg-exp-router/trie.js +3 -3
  56. package/dist/router/trie-router/index.d.ts +1 -1
  57. package/dist/router/trie-router/index.js +1 -1
  58. package/dist/router/trie-router/node.d.ts +4 -3
  59. package/dist/router/trie-router/node.js +123 -55
  60. package/dist/router/trie-router/node.test.d.ts +1 -0
  61. package/dist/router/trie-router/node.test.js +351 -0
  62. package/dist/router/trie-router/router.d.ts +2 -2
  63. package/dist/router/trie-router/router.js +1 -1
  64. package/dist/router/trie-router/router.test.d.ts +1 -0
  65. package/dist/router/trie-router/router.test.js +98 -0
  66. package/dist/router.d.ts +4 -3
  67. package/dist/router.js +5 -4
  68. package/dist/utils/body.js +2 -2
  69. package/dist/utils/body.test.d.ts +1 -0
  70. package/dist/utils/body.test.js +45 -0
  71. package/dist/utils/buffer.js +1 -1
  72. package/dist/utils/buffer.test.d.ts +1 -0
  73. package/dist/utils/buffer.test.js +36 -0
  74. package/dist/utils/cloudflare.test.d.ts +1 -0
  75. package/dist/utils/cloudflare.test.js +42 -0
  76. package/dist/utils/crypto.test.d.ts +1 -0
  77. package/dist/utils/crypto.test.js +15 -0
  78. package/dist/utils/encode.test.d.ts +1 -0
  79. package/dist/utils/encode.test.js +54 -0
  80. package/dist/utils/http-status.test.d.ts +1 -0
  81. package/dist/utils/http-status.test.js +8 -0
  82. package/dist/utils/jwt/jwt.test.d.ts +1 -0
  83. package/dist/utils/jwt/jwt.test.js +171 -0
  84. package/dist/utils/jwt/types.test.d.ts +1 -0
  85. package/dist/utils/jwt/types.test.js +12 -0
  86. package/dist/utils/mime.test.d.ts +1 -0
  87. package/dist/utils/mime.test.js +13 -0
  88. package/dist/utils/url.js +4 -4
  89. package/dist/utils/url.test.d.ts +1 -0
  90. package/dist/utils/url.test.js +87 -0
  91. package/package.json +3 -4
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const hono_1 = require("../../hono");
4
+ const _1 = require(".");
5
+ // Mock
6
+ const store = {
7
+ 'index.abcdef.mustache': '{{> header}}Title: {{ title }}{{> footer}}',
8
+ 'header.abcdef.mustache': '<html><body>',
9
+ 'footer.abcdef.mustache': '</body></html>',
10
+ 'view/index.abcdef.mustache': '<h1>With Root</h1>',
11
+ };
12
+ const manifest = JSON.stringify({
13
+ 'index.mustache': 'index.abcdef.mustache',
14
+ 'header.mustache': 'header.abcdef.mustache',
15
+ 'footer.mustache': 'footer.abcdef.mustache',
16
+ 'view/index.mustache': 'view/index.abcdef.mustache',
17
+ });
18
+ Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest });
19
+ Object.assign(global, {
20
+ __STATIC_CONTENT: {
21
+ get: (path) => {
22
+ return store[path];
23
+ },
24
+ },
25
+ });
26
+ describe('Mustache by Middleware', () => {
27
+ const app = new hono_1.Hono();
28
+ app.use('/foo', (0, _1.mustache)());
29
+ app.get('/foo', (c) => {
30
+ return c.render('index', { title: 'Hono!' }, {
31
+ header: 'header',
32
+ footer: 'footer',
33
+ });
34
+ });
35
+ app.use('/bar', (0, _1.mustache)({ root: 'view' }));
36
+ app.get('/bar', (c) => {
37
+ return c.render('index');
38
+ });
39
+ it('Mustache template rendering', async () => {
40
+ const res = await app.request('http://localhost/foo');
41
+ expect(res.status).toBe(200);
42
+ expect(await res.text()).toBe('<html><body>Title: Hono!</body></html>');
43
+ });
44
+ it('Mustache template rendering with root', async () => {
45
+ const res = await app.request('http://localhost/bar');
46
+ expect(res.status).toBe(200);
47
+ expect(await res.text()).toBe('<h1>With Root</h1>');
48
+ });
49
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const hono_1 = require("../../hono");
4
+ const _1 = require(".");
5
+ describe('Powered by Middleware', () => {
6
+ const app = new hono_1.Hono();
7
+ app.use('*', (0, _1.poweredBy)());
8
+ app.get('/', (c) => c.text('root'));
9
+ it('Should return with X-Powered-By header', async () => {
10
+ const res = await app.request('http://localhost/');
11
+ expect(res).not.toBeNull();
12
+ expect(res.status).toBe(200);
13
+ expect(res.headers.get('X-Powered-By')).toBe('Hono');
14
+ });
15
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const hono_1 = require("../../hono");
4
+ const _1 = require(".");
5
+ describe('JSON pretty by Middleware', () => {
6
+ const app = new hono_1.Hono();
7
+ app.get('/', (c) => {
8
+ return c.json({ message: 'Hono!' });
9
+ });
10
+ it('Should return pretty JSON output', async () => {
11
+ app.use('*', (0, _1.prettyJSON)());
12
+ const res = await app.request('http://localhost/?pretty');
13
+ expect(res).not.toBeNull();
14
+ expect(res.status).toBe(200);
15
+ expect(await res.text()).toBe(`{
16
+ "message": "Hono!"
17
+ }`);
18
+ });
19
+ it('Should return pretty JSON output with 4 spaces', async () => {
20
+ app.use('*', (0, _1.prettyJSON)({ space: 4 }));
21
+ const res = await app.request('http://localhost/?pretty');
22
+ expect(res).not.toBeNull();
23
+ expect(res.status).toBe(200);
24
+ expect(await res.text()).toBe(`{
25
+ "message": "Hono!"
26
+ }`);
27
+ });
28
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const hono_1 = require("../../hono");
4
+ const _1 = require(".");
5
+ // Mock
6
+ const store = {
7
+ 'assets/static/plain.abcdef.txt': 'This is plain.txt',
8
+ 'assets/static/hono.abcdef.html': '<h1>Hono!</h1>',
9
+ 'assets/static/top/index.abcdef.html': '<h1>Top</h1>',
10
+ 'static-no-root/plain.abcdef.txt': 'That is plain.txt',
11
+ };
12
+ const manifest = JSON.stringify({
13
+ 'assets/static/plain.txt': 'assets/static/plain.abcdef.txt',
14
+ 'assets/static/hono.html': 'assets/static/hono.abcdef.html',
15
+ 'assets/static/top/index.html': 'assets/static/top/index.abcdef.html',
16
+ 'static-no-root/plain.txt': 'static-no-root/plain.abcdef.txt',
17
+ });
18
+ Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest });
19
+ Object.assign(global, {
20
+ __STATIC_CONTENT: {
21
+ get: (path) => {
22
+ return store[path];
23
+ },
24
+ },
25
+ });
26
+ describe('ServeStatic Middleware', () => {
27
+ const app = new hono_1.Hono();
28
+ app.use('/static/*', (0, _1.serveStatic)({ root: './assets' }));
29
+ app.use('/static-no-root/*', (0, _1.serveStatic)());
30
+ it('Should return plain.txt', async () => {
31
+ const res = await app.request('http://localhost/static/plain.txt');
32
+ expect(res.status).toBe(200);
33
+ expect(await res.text()).toBe('This is plain.txt');
34
+ expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8');
35
+ });
36
+ it('Should return hono.html', async () => {
37
+ const res = await app.request('http://localhost/static/hono.html');
38
+ expect(res.status).toBe(200);
39
+ expect(await res.text()).toBe('<h1>Hono!</h1>');
40
+ expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8');
41
+ });
42
+ it('Should return 404 response', async () => {
43
+ const res = await app.request('http://localhost/static/not-found.html');
44
+ expect(res.status).toBe(404);
45
+ });
46
+ it('Should return plan.txt', async () => {
47
+ const res = await app.request('http://localhost/static-no-root/plain.txt');
48
+ expect(res.status).toBe(200);
49
+ expect(await res.text()).toBe('That is plain.txt');
50
+ expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8');
51
+ });
52
+ it('Should return index.html', async () => {
53
+ const res = await app.request('http://localhost/static/top');
54
+ expect(res.status).toBe(200);
55
+ expect(await res.text()).toBe('<h1>Top</h1>');
56
+ expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8');
57
+ });
58
+ });
@@ -1 +1 @@
1
- export { RegExpRouter } from '../../router/reg-exp-router/router';
1
+ export { RegExpRouter } from './router';
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RegExpRouter = void 0;
4
- var router_1 = require("../../router/reg-exp-router/router");
4
+ var router_1 = require("./router");
5
5
  Object.defineProperty(exports, "RegExpRouter", { enumerable: true, get: function () { return router_1.RegExpRouter; } });
@@ -6,6 +6,9 @@ export declare class Node {
6
6
  index?: number;
7
7
  varIndex?: number;
8
8
  children: Record<string, Node>;
9
+ reverse: boolean;
10
+ constructor({ reverse }?: Partial<Node>);
11
+ newChildNode(): Node;
9
12
  insert(tokens: readonly string[], index: number, paramMap: ParamMap, context: Context): void;
10
13
  buildRegExpStr(): string;
11
14
  }
@@ -35,8 +35,12 @@ function compareKey(a, b) {
35
35
  return a.length === b.length ? (a < b ? -1 : 1) : b.length - a.length;
36
36
  }
37
37
  class Node {
38
- constructor() {
38
+ constructor({ reverse } = { reverse: false }) {
39
39
  this.children = {};
40
+ this.reverse = reverse;
41
+ }
42
+ newChildNode() {
43
+ return new Node({ reverse: this.reverse });
40
44
  }
41
45
  insert(tokens, index, paramMap, context) {
42
46
  var _a;
@@ -58,7 +62,7 @@ class Node {
58
62
  const regexpStr = pattern[2] || LABEL_REG_EXP_STR;
59
63
  node = this.children[regexpStr];
60
64
  if (!node) {
61
- node = this.children[regexpStr] = new Node();
65
+ node = this.children[regexpStr] = this.newChildNode();
62
66
  if (name !== '') {
63
67
  node.varIndex = context.varIndex++;
64
68
  }
@@ -68,19 +72,21 @@ class Node {
68
72
  }
69
73
  }
70
74
  else {
71
- node = (_a = this.children)[token] || (_a[token] = new Node());
75
+ node = (_a = this.children)[token] || (_a[token] = this.newChildNode());
72
76
  }
73
77
  node.insert(restTokens, index, paramMap, context);
74
78
  }
75
79
  buildRegExpStr() {
76
- const strList = Object.keys(this.children)
77
- .sort(compareKey)
78
- .map((k) => {
80
+ let childKeys = Object.keys(this.children).sort(compareKey);
81
+ if (this.reverse) {
82
+ childKeys = childKeys.reverse();
83
+ }
84
+ const strList = childKeys.map((k) => {
79
85
  const c = this.children[k];
80
86
  return (typeof c.varIndex === 'number' ? `(${k})@${c.varIndex}` : k) + c.buildRegExpStr();
81
87
  });
82
88
  if (typeof this.index === 'number') {
83
- strList.push(`#${this.index}`);
89
+ strList.unshift(`#${this.index}`);
84
90
  }
85
91
  if (strList.length === 0) {
86
92
  return '';
@@ -1,7 +1,26 @@
1
1
  import { Router, Result } from '../../router';
2
- declare type Route<T> = [string, T];
2
+ interface Hint {
3
+ components: string[];
4
+ regExpComponents: Array<true | string>;
5
+ componentsLength: number;
6
+ endWithWildcard: boolean;
7
+ paramIndexList: number[];
8
+ maybeHandler: boolean;
9
+ namedParams: [number, string, string][];
10
+ }
11
+ interface Route<T> {
12
+ method: string;
13
+ path: string;
14
+ hint: Hint;
15
+ handlers: T[];
16
+ middleware: T[];
17
+ paramAliasMap: Record<string, string[]>;
18
+ }
3
19
  export declare class RegExpRouter<T> extends Router<T> {
4
- routes?: Record<string, Route<T>[]>;
20
+ routeData?: {
21
+ routes: Route<T>[];
22
+ methods: Set<string>;
23
+ };
5
24
  add(method: string, path: string, handler: T): void;
6
25
  match(method: string, path: string): Result<T> | null;
7
26
  private buildAllMatchers;