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,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const router_1 = require("./router");
4
+ describe('Basic Usage', () => {
5
+ const router = new router_1.TrieRouter();
6
+ router.add('GET', '/hello', 'get hello');
7
+ router.add('POST', '/hello', 'post hello');
8
+ it('get, post hello', async () => {
9
+ let res = router.match('GET', '/hello');
10
+ expect(res).not.toBeNull();
11
+ expect(res.handlers).toEqual(['get hello']);
12
+ res = router.match('POST', '/hello');
13
+ expect(res).not.toBeNull();
14
+ expect(res.handlers).toEqual(['post hello']);
15
+ res = router.match('PUT', '/hello');
16
+ expect(res).toBeNull();
17
+ res = router.match('GET', '/');
18
+ expect(res).toBeNull();
19
+ });
20
+ });
21
+ describe('Complex', () => {
22
+ const router = new router_1.TrieRouter();
23
+ it('Named Param', async () => {
24
+ router.add('GET', '/entry/:id', 'get entry');
25
+ const res = router.match('GET', '/entry/123');
26
+ expect(res).not.toBeNull();
27
+ expect(res.handlers).toEqual(['get entry']);
28
+ expect(res.params['id']).toBe('123');
29
+ });
30
+ it('Wildcard', async () => {
31
+ router.add('GET', '/wild/*/card', 'get wildcard');
32
+ const res = router.match('GET', '/wild/xxx/card');
33
+ expect(res).not.toBeNull();
34
+ expect(res.handlers).toEqual(['get wildcard']);
35
+ });
36
+ it('Default', async () => {
37
+ router.add('GET', '/api/abc', 'get api');
38
+ router.add('GET', '/api/*', 'fallback');
39
+ let res = router.match('GET', '/api/abc');
40
+ expect(res).not.toBeNull();
41
+ expect(res.handlers).toEqual(['fallback', 'get api']);
42
+ res = router.match('GET', '/api/def');
43
+ expect(res).not.toBeNull();
44
+ expect(res.handlers).toEqual(['fallback']);
45
+ });
46
+ it('Regexp', async () => {
47
+ router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post');
48
+ let res = router.match('GET', '/post/20210101/hello');
49
+ expect(res).not.toBeNull();
50
+ expect(res.handlers).toEqual(['get post']);
51
+ expect(res.params['date']).toBe('20210101');
52
+ expect(res.params['title']).toBe('hello');
53
+ res = router.match('GET', '/post/onetwothree');
54
+ expect(res).toBeNull();
55
+ res = router.match('GET', '/post/123/123');
56
+ expect(res).toBeNull();
57
+ });
58
+ });
59
+ describe('Multi match', () => {
60
+ const router = new router_1.TrieRouter();
61
+ describe('Blog', () => {
62
+ router.add('ALL', '*', 'middleware a');
63
+ router.add('GET', '*', 'middleware b');
64
+ router.add('GET', '/entry', 'get entries');
65
+ router.add('POST', '/entry/*', 'middleware c');
66
+ router.add('POST', '/entry', 'post entry');
67
+ router.add('GET', '/entry/:id', 'get entry');
68
+ router.add('GET', '/entry/:id/comment/:comment_id', 'get comment');
69
+ it('GET /', async () => {
70
+ const res = router.match('GET', '/');
71
+ expect(res).not.toBeNull();
72
+ expect(res.handlers).toEqual(['middleware a', 'middleware b']);
73
+ });
74
+ it('GET /entry/123', async () => {
75
+ const res = router.match('GET', '/entry/123');
76
+ expect(res).not.toBeNull();
77
+ expect(res.handlers).toEqual(['middleware a', 'middleware b', 'get entry']);
78
+ expect(res.params['id']).toBe('123');
79
+ });
80
+ it('GET /entry/123/comment/456', async () => {
81
+ const res = router.match('GET', '/entry/123/comment/456');
82
+ expect(res).not.toBeNull();
83
+ expect(res.handlers).toEqual(['middleware a', 'middleware b', 'get comment']);
84
+ expect(res.params['id']).toBe('123');
85
+ expect(res.params['comment_id']).toBe('456');
86
+ });
87
+ it('POST /entry', async () => {
88
+ const res = router.match('POST', '/entry');
89
+ expect(res).not.toBeNull();
90
+ expect(res.handlers).toEqual(['middleware a', 'middleware c', 'post entry']);
91
+ });
92
+ it('DELETE /entry', async () => {
93
+ const res = router.match('DELETE', '/entry');
94
+ expect(res).not.toBeNull();
95
+ expect(res.handlers).toEqual(['middleware a']);
96
+ });
97
+ });
98
+ });
package/dist/router.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- export declare const METHOD_NAME_OF_ALL = "ALL";
1
+ export declare const METHOD_NAME_ALL: "ALL";
2
+ export declare const METHOD_NAME_ALL_LOWERCASE: "all";
2
3
  export declare abstract class Router<T> {
3
4
  abstract add(method: string, path: string, handler: T): void;
4
5
  abstract match(method: string, path: string): Result<T> | null;
5
6
  }
6
7
  export declare class Result<T> {
7
- handler: T;
8
+ handlers: T[];
8
9
  params: Record<string, string>;
9
- constructor(handler: T, params: Record<string, string>);
10
+ constructor(handlers: T[], params: Record<string, string>);
10
11
  }
package/dist/router.js CHANGED
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Result = exports.Router = exports.METHOD_NAME_OF_ALL = void 0;
4
- exports.METHOD_NAME_OF_ALL = 'ALL';
3
+ exports.Result = exports.Router = exports.METHOD_NAME_ALL_LOWERCASE = exports.METHOD_NAME_ALL = void 0;
4
+ exports.METHOD_NAME_ALL = 'ALL';
5
+ exports.METHOD_NAME_ALL_LOWERCASE = 'all';
5
6
  class Router {
6
7
  }
7
8
  exports.Router = Router;
8
9
  class Result {
9
- constructor(handler, params) {
10
- this.handler = handler;
10
+ constructor(handlers, params) {
11
+ this.handlers = handlers;
11
12
  this.params = params;
12
13
  }
13
14
  }
@@ -7,10 +7,10 @@ const parseBody = async (r) => {
7
7
  return await r.json();
8
8
  }
9
9
  else if (contentType.includes('application/text')) {
10
- return r.text();
10
+ return await r.text();
11
11
  }
12
12
  else if (contentType.startsWith('text')) {
13
- return r.text();
13
+ return await r.text();
14
14
  }
15
15
  else if (contentType.includes('form')) {
16
16
  const form = {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const body_1 = require("./body");
4
+ describe('Parse Body Middleware', () => {
5
+ it('should parse JSON', async () => {
6
+ const payload = { message: 'hello hono' };
7
+ const req = new Request('http://localhost/json', {
8
+ method: 'POST',
9
+ body: JSON.stringify(payload),
10
+ headers: new Headers({ 'Content-Type': 'application/json' }),
11
+ });
12
+ expect(await (0, body_1.parseBody)(req)).toEqual(payload);
13
+ });
14
+ it('should parse Text', async () => {
15
+ const payload = 'hello';
16
+ const req = new Request('http://localhost/text', {
17
+ method: 'POST',
18
+ body: 'hello',
19
+ headers: new Headers({ 'Content-Type': 'application/text' }),
20
+ });
21
+ expect(await (0, body_1.parseBody)(req)).toEqual(payload);
22
+ });
23
+ it('should parse Form', async () => {
24
+ const formData = new URLSearchParams();
25
+ formData.append('message', 'hello');
26
+ const req = new Request('https://localhost/form', {
27
+ method: 'POST',
28
+ body: formData,
29
+ headers: {
30
+ 'Content-Type': 'application/x-www-form-urlencoded',
31
+ },
32
+ });
33
+ expect(await (0, body_1.parseBody)(req)).toEqual({ message: 'hello' });
34
+ });
35
+ it('should parse Response body ', async () => {
36
+ const payload = 'hello';
37
+ const res = new Response(payload, {
38
+ headers: {
39
+ 'Content-Type': 'text/plain',
40
+ },
41
+ });
42
+ console.log(res.headers.get('Content-Type'));
43
+ expect(await (0, body_1.parseBody)(res)).toEqual(payload);
44
+ });
45
+ });
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.bufferToString = exports.timingSafeEqual = exports.equal = void 0;
4
- const crypto_1 = require("../utils/crypto");
4
+ const crypto_1 = require("./crypto");
5
5
  const equal = (a, b) => {
6
6
  if (a === b) {
7
7
  return true;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const crypto_js_1 = require("crypto-js");
4
+ const buffer_1 = require("./buffer");
5
+ describe('buffer', () => {
6
+ it('positive', async () => {
7
+ expect(await (0, buffer_1.timingSafeEqual)('127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935', '127e6fbfe24a750e72930c220a8e138275656b8e5d8f48a98c3c92df2caba935')).toBe(true);
8
+ expect(await (0, buffer_1.timingSafeEqual)('a', 'a')).toBe(true);
9
+ expect(await (0, buffer_1.timingSafeEqual)('', '')).toBe(true);
10
+ expect(await (0, buffer_1.timingSafeEqual)(undefined, undefined)).toBe(true);
11
+ expect(await (0, buffer_1.timingSafeEqual)(true, true)).toBe(true);
12
+ expect(await (0, buffer_1.timingSafeEqual)(false, false)).toBe(true);
13
+ expect(await (0, buffer_1.timingSafeEqual)(true, true, (d) => (0, crypto_js_1.SHA256)(d).toString()));
14
+ });
15
+ it('negative', async () => {
16
+ expect(await (0, buffer_1.timingSafeEqual)('a', 'b')).toBe(false);
17
+ expect(await (0, buffer_1.timingSafeEqual)('a', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toBe(false);
18
+ expect(await (0, buffer_1.timingSafeEqual)('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'a')).toBe(false);
19
+ expect(await (0, buffer_1.timingSafeEqual)('alpha', 'beta')).toBe(false);
20
+ expect(await (0, buffer_1.timingSafeEqual)(false, true)).toBe(false);
21
+ expect(await (0, buffer_1.timingSafeEqual)(false, undefined)).toBe(false);
22
+ expect(await (0, buffer_1.timingSafeEqual)(() => { }, () => { })).toBe(false);
23
+ expect(await (0, buffer_1.timingSafeEqual)({}, {})).toBe(false);
24
+ expect(await (0, buffer_1.timingSafeEqual)({ a: 1 }, { a: 1 })).toBe(false);
25
+ expect(await (0, buffer_1.timingSafeEqual)({ a: 1 }, { a: 2 })).toBe(false);
26
+ expect(await (0, buffer_1.timingSafeEqual)([1, 2], [1, 2])).toBe(false);
27
+ expect(await (0, buffer_1.timingSafeEqual)([1, 2], [1, 2, 3])).toBe(false);
28
+ });
29
+ });
30
+ describe('bufferToString', () => {
31
+ it('Should return あいうえお', () => {
32
+ const bytes = [227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 138];
33
+ const buffer = Uint8Array.from(bytes).buffer;
34
+ expect((0, buffer_1.bufferToString)(buffer)).toBe('あいうえお');
35
+ });
36
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const cloudflare_1 = require("./cloudflare");
4
+ // Mock
5
+ const store = {
6
+ 'index.abcdef.html': 'This is index',
7
+ 'assets/static/plain.abcdef.txt': 'Asset text',
8
+ };
9
+ const manifest = JSON.stringify({
10
+ 'index.html': 'index.abcdef.html',
11
+ 'assets/static/plain.txt': 'assets/static/plain.abcdef.txt',
12
+ });
13
+ Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest });
14
+ Object.assign(global, {
15
+ __STATIC_CONTENT: {
16
+ get: (path) => {
17
+ return store[path];
18
+ },
19
+ },
20
+ });
21
+ describe('Utils for Cloudflare Workers', () => {
22
+ it('getContentFromKVAsset', async () => {
23
+ let content = await (0, cloudflare_1.getContentFromKVAsset)('not-found.txt');
24
+ expect(content).toBeUndefined();
25
+ content = await (0, cloudflare_1.getContentFromKVAsset)('index.html');
26
+ expect(content).not.toBeUndefined();
27
+ expect(content).toBe('This is index');
28
+ content = await (0, cloudflare_1.getContentFromKVAsset)('assets/static/plain.txt');
29
+ expect(content).not.toBeUndefined();
30
+ expect(content).toBe('Asset text');
31
+ });
32
+ it('getKVFilePath', async () => {
33
+ expect((0, cloudflare_1.getKVFilePath)({ filename: 'foo' })).toBe('foo/index.html');
34
+ expect((0, cloudflare_1.getKVFilePath)({ filename: 'foo.txt' })).toBe('foo.txt');
35
+ expect((0, cloudflare_1.getKVFilePath)({ filename: 'foo', root: 'bar' })).toBe('bar/foo/index.html');
36
+ expect((0, cloudflare_1.getKVFilePath)({ filename: 'foo.txt', root: 'bar' })).toBe('bar/foo.txt');
37
+ expect((0, cloudflare_1.getKVFilePath)({ filename: 'foo', defaultDocument: 'index.txt' })).toBe('foo/index.txt');
38
+ expect((0, cloudflare_1.getKVFilePath)({ filename: 'foo', root: 'bar', defaultDocument: 'index.txt' })).toBe('bar/foo/index.txt');
39
+ expect((0, cloudflare_1.getKVFilePath)({ filename: './foo' })).toBe('foo/index.html');
40
+ expect((0, cloudflare_1.getKVFilePath)({ filename: 'foo', root: './bar' })).toBe('bar/foo/index.html');
41
+ });
42
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const crypto_1 = require("./crypto");
4
+ describe('crypto', () => {
5
+ it('sha256', async () => {
6
+ expect(await (0, crypto_1.sha256)('hono')).toBe('8b3dc17add91b7e8f0b5109a389927d66001139cd9b03fa7b95f83126e1b2b23');
7
+ expect(await (0, crypto_1.sha256)('炎')).toBe('1fddc5a562ee1fbeb4fc6def7d4be4911fcdae4273b02ae3a507b170ba0ea169');
8
+ expect(await (0, crypto_1.sha256)('abcdedf')).not.toBe('abcdef');
9
+ });
10
+ it('sha1', async () => {
11
+ expect(await (0, crypto_1.sha1)('hono')).toBe('28c7e86f5732391917876b45c06c626c04d77f39');
12
+ expect(await (0, crypto_1.sha1)('炎')).toBe('d56e09ae2421b2b8a0b5ee5fdceaed663c8c9472');
13
+ expect(await (0, crypto_1.sha1)('abcdedf')).not.toBe('abcdef');
14
+ });
15
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const encode_1 = require("./encode");
4
+ describe('encode', () => {
5
+ describe('base64', () => {
6
+ it('encodeBase64', () => {
7
+ expect((0, encode_1.encodeBase64)('hooooooooo')).toBe('aG9vb29vb29vbw==');
8
+ expect((0, encode_1.encodeBase64)('炎')).toBe('54KO');
9
+ expect((0, encode_1.encodeBase64)('abcdef')).not.toBe('abcdedf');
10
+ expect((0, encode_1.encodeBase64)('')).toBe('');
11
+ expect(() => {
12
+ (0, encode_1.encodeBase64)(null);
13
+ }).toThrow(TypeError);
14
+ });
15
+ it('decodeBase64', async () => {
16
+ expect((0, encode_1.decodeBase64)('aG9vb29vb29vbw==')).toBe('hooooooooo');
17
+ expect((0, encode_1.decodeBase64)('54KO')).toBe('炎');
18
+ expect((0, encode_1.decodeBase64)('abcdedf')).not.toBe('abcdef');
19
+ expect((0, encode_1.decodeBase64)('')).toBe('');
20
+ expect(() => {
21
+ (0, encode_1.decodeBase64)(null);
22
+ }).toThrowError(TypeError);
23
+ });
24
+ });
25
+ describe('base64url', () => {
26
+ it('encodeBase64URL', () => {
27
+ expect((0, encode_1.encodeBase64URL)('hooooooooo')).toBe('aG9vb29vb29vbw');
28
+ expect((0, encode_1.encodeBase64URL)('http://github.com/honojs/hono')).toBe('aHR0cDovL2dpdGh1Yi5jb20vaG9ub2pzL2hvbm8');
29
+ expect((0, encode_1.encodeBase64URL)('炎')).toBe('54KO');
30
+ expect((0, encode_1.encodeBase64URL)('abcdef')).not.toBe('abcdedf');
31
+ expect((0, encode_1.encodeBase64URL)('')).toBe('');
32
+ expect(() => {
33
+ (0, encode_1.encodeBase64URL)(null);
34
+ }).toThrow(TypeError);
35
+ });
36
+ it('decodeBase64URL', async () => {
37
+ expect((0, encode_1.decodeBase64URL)('aG9vb29vb29vbw')).toBe('hooooooooo');
38
+ expect((0, encode_1.decodeBase64URL)('aHR0cDovL2dpdGh1Yi5jb20vaG9ub2pzL2hvbm8')).toBe('http://github.com/honojs/hono');
39
+ expect((0, encode_1.decodeBase64URL)('54KO')).toBe('炎');
40
+ expect((0, encode_1.decodeBase64URL)('abcdedf')).not.toBe('abcdef');
41
+ expect((0, encode_1.decodeBase64URL)('')).toBe('');
42
+ expect(() => {
43
+ (0, encode_1.decodeBase64URL)(null);
44
+ }).toThrowError(TypeError);
45
+ });
46
+ });
47
+ describe('utf8ToUint8Array', () => {
48
+ it('should be equal', () => {
49
+ const k = 'a-secret-key';
50
+ const arr = (0, encode_1.utf8ToUint8Array)(k);
51
+ expect(String(arr)).toEqual('97,45,115,101,99,114,101,116,45,107,101,121');
52
+ });
53
+ });
54
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const http_status_1 = require("./http-status");
4
+ describe('http-status utility', () => {
5
+ it('getStatusText', () => {
6
+ expect((0, http_status_1.getStatusText)(200)).toBe('OK');
7
+ });
8
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const JWT = __importStar(require("./jwt"));
27
+ const types_1 = require("./types");
28
+ describe('JWT', () => {
29
+ it('JwtAlorithmNotImplemented', async () => {
30
+ const payload = { message: 'hello world' };
31
+ const secret = 'a-secret';
32
+ const alg = '';
33
+ let tok = '';
34
+ let err;
35
+ try {
36
+ tok = await JWT.sign(payload, secret, alg);
37
+ }
38
+ catch (e) {
39
+ err = e;
40
+ }
41
+ expect(tok).toBe('');
42
+ expect(err).toEqual(new types_1.JwtAlorithmNotImplemented(alg));
43
+ });
44
+ it('JwtTokenInvalid', async () => {
45
+ const tok = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ';
46
+ const secret = 'a-secret';
47
+ let err;
48
+ let authorized = false;
49
+ try {
50
+ authorized = await JWT.verify(tok, secret, types_1.AlgorithmTypes.HS256);
51
+ }
52
+ catch (e) {
53
+ err = e;
54
+ }
55
+ expect(err).toEqual(new types_1.JwtTokenInvalid(tok));
56
+ expect(authorized).toBe(false);
57
+ });
58
+ it('JwtTokenNotBefore', async () => {
59
+ const tok = 'eyJraWQiOiJFemF7bVZWbnd0TUpUNEFveFVtT0dILWJ0Y2VUVFM3djBYcEJuMm5ZZ2VjIiwiYWxnIjoiSFMyNTYifQ.eyJyb2xlIjoiYXBpX3JvbGUiLCJuYmYiOjE2NjQ1ODI0MDB9.BatadbUj5e41OZ33odEFTAndQFzX0w9aAgpQPgO-zaQ';
60
+ const secret = 'a-secret';
61
+ let err;
62
+ let authorized = false;
63
+ try {
64
+ authorized = await JWT.verify(tok, secret, types_1.AlgorithmTypes.HS256);
65
+ }
66
+ catch (e) {
67
+ err = e;
68
+ }
69
+ expect(err).toEqual(new types_1.JwtTokenNotBefore(tok));
70
+ expect(authorized).toBe(false);
71
+ });
72
+ it('JwtTokenExpired', async () => {
73
+ const tok = 'eyJraWQiOiJFemF6bVZWbnd0TUpUNEFveFVtT0dILWJ0Y2VUVFM3djBYcEJuMm5ZZ2VjIiwiYWxnIjoiSFMyNTYifQ.eyJyb2xlIjoiYXBpX3JvbGUiLCJleHAiOjE2MzMwNDY0MDB9.Gmq_dozOnwzqkMUMEm7uny7cMZuF1d0QkCnmRXAbTEk';
74
+ const secret = 'a-secret';
75
+ let err;
76
+ let authorized = false;
77
+ try {
78
+ authorized = await JWT.verify(tok, secret, types_1.AlgorithmTypes.HS256);
79
+ }
80
+ catch (e) {
81
+ err = e;
82
+ }
83
+ expect(err).toEqual(new types_1.JwtTokenExpired(tok));
84
+ expect(authorized).toBe(false);
85
+ });
86
+ it('HS256 sign & verify & decode', async () => {
87
+ const payload = { message: 'hello world' };
88
+ const secret = 'a-secret';
89
+ const tok = await JWT.sign(payload, secret, types_1.AlgorithmTypes.HS256);
90
+ const expected = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE';
91
+ expect(tok).toEqual(expected);
92
+ expect(await JWT.verify(tok, secret, types_1.AlgorithmTypes.HS256)).toBe(true);
93
+ expect(JWT.decode(tok)).toEqual({
94
+ header: {
95
+ alg: 'HS256',
96
+ typ: 'JWT',
97
+ },
98
+ payload: {
99
+ message: 'hello world',
100
+ },
101
+ });
102
+ });
103
+ it('HS256 sign & verify', async () => {
104
+ const payload = { message: 'hello world' };
105
+ const secret = 'a-secret';
106
+ const tok = await JWT.sign(payload, secret, types_1.AlgorithmTypes.HS256);
107
+ const expected = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE';
108
+ expect(tok).toEqual(expected);
109
+ let err = null;
110
+ let authorized = false;
111
+ try {
112
+ authorized = await JWT.verify(tok, secret + 'invalid', types_1.AlgorithmTypes.HS256);
113
+ }
114
+ catch (e) {
115
+ err = e;
116
+ }
117
+ expect(authorized).toBe(false);
118
+ expect(err instanceof types_1.JwtTokenSignatureMismatched).toBe(true);
119
+ });
120
+ it('HS512 sign & verify & decode', async () => {
121
+ const payload = { message: 'hello world' };
122
+ const secret = 'a-secret';
123
+ const tok = await JWT.sign(payload, secret, types_1.AlgorithmTypes.HS512);
124
+ const expected = 'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.RqVLgExB_GXF1-9T-k4V4HjFmiuQKTEjVSiZd-YL0WERIlywZ7PfzAuTZSJU4gg8cscGamQa030cieEWrYcywg';
125
+ expect(tok).toEqual(expected);
126
+ expect(await JWT.verify(tok, secret, types_1.AlgorithmTypes.HS512)).toBe(true);
127
+ expect(JWT.decode(tok)).toEqual({
128
+ header: {
129
+ alg: 'HS512',
130
+ typ: 'JWT',
131
+ },
132
+ payload: {
133
+ message: 'hello world',
134
+ },
135
+ });
136
+ });
137
+ it('HS512 sign & verify', async () => {
138
+ const payload = { message: 'hello world' };
139
+ const secret = 'a-secret';
140
+ const tok = await JWT.sign(payload, secret, types_1.AlgorithmTypes.HS512);
141
+ const expected = 'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.RqVLgExB_GXF1-9T-k4V4HjFmiuQKTEjVSiZd-YL0WERIlywZ7PfzAuTZSJU4gg8cscGamQa030cieEWrYcywg';
142
+ expect(tok).toEqual(expected);
143
+ let err = null;
144
+ let authorized = false;
145
+ try {
146
+ authorized = await JWT.verify(tok, secret + 'invalid', types_1.AlgorithmTypes.HS256);
147
+ }
148
+ catch (e) {
149
+ err = e;
150
+ }
151
+ expect(authorized).toBe(false);
152
+ expect(err instanceof types_1.JwtTokenSignatureMismatched).toBe(true);
153
+ });
154
+ it('HS384 sign & verify', async () => {
155
+ const payload = { message: 'hello world' };
156
+ const secret = 'a-secret%你好';
157
+ const tok = await JWT.sign(payload, secret, types_1.AlgorithmTypes.HS384);
158
+ const expected = 'eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.C1Br1183Oy6O7th4NDCOaI9WB75i3FMCuYlv1tCL9HggsU89T-SNutghwhJykD3r';
159
+ expect(tok).toEqual(expected);
160
+ let err = null;
161
+ let authorized = false;
162
+ try {
163
+ authorized = await JWT.verify(tok, secret + 'invalid', types_1.AlgorithmTypes.HS256);
164
+ }
165
+ catch (e) {
166
+ err = e;
167
+ }
168
+ expect(authorized).toBe(false);
169
+ expect(err instanceof types_1.JwtTokenSignatureMismatched).toBe(true);
170
+ });
171
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const types_1 = require("./types");
4
+ describe('Types', () => {
5
+ it('AlgorithmTypes', () => {
6
+ expect('HS256').toBe(types_1.AlgorithmTypes.HS256);
7
+ expect('HS384').toBe(types_1.AlgorithmTypes.HS384);
8
+ expect('HS512').toBe(types_1.AlgorithmTypes.HS512);
9
+ expect(undefined).toBe(undefined);
10
+ expect('').toBe('');
11
+ });
12
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const mime_1 = require("./mime");
4
+ describe('mime', () => {
5
+ it('getMimeType', () => {
6
+ expect((0, mime_1.getMimeType)('hello.txt')).toBe('text/plain; charset=utf-8');
7
+ expect((0, mime_1.getMimeType)('hello.html')).toBe('text/html; charset=utf-8');
8
+ expect((0, mime_1.getMimeType)('hello.json')).toBe('application/json; charset=utf-8');
9
+ expect((0, mime_1.getMimeType)('good.morning.hello.gif')).toBe('image/gif');
10
+ expect((0, mime_1.getMimeType)('goodmorninghellogif')).toBeUndefined();
11
+ expect((0, mime_1.getMimeType)('indexjs.abcd')).toBeUndefined();
12
+ });
13
+ });
package/dist/utils/url.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mergePath = exports.isAbsoluteURL = exports.getPathFromURL = exports.getPattern = exports.splitPath = void 0;
4
- const URL_REGEXP = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
4
+ const URL_REGEXP = /^https?:\/\/[a-zA-Z0-9\-\.:]+(\/?[^?#]*)/;
5
5
  const splitPath = (path) => {
6
6
  const paths = path.split(/\//); // faster than path.split('/')
7
7
  if (paths[0] === '') {
@@ -37,19 +37,19 @@ exports.getPattern = getPattern;
37
37
  const getPathFromURL = (url, params = { strict: true }) => {
38
38
  // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same
39
39
  // default is true
40
- if (!params.strict && url.endsWith('/')) {
40
+ if (params.strict === false && url.endsWith('/')) {
41
41
  url = url.slice(0, -1);
42
42
  }
43
43
  const match = url.match(URL_REGEXP);
44
44
  if (match) {
45
- return match[5];
45
+ return match[1];
46
46
  }
47
47
  return '';
48
48
  };
49
49
  exports.getPathFromURL = getPathFromURL;
50
50
  const isAbsoluteURL = (url) => {
51
51
  const match = url.match(URL_REGEXP);
52
- if (match && match[1]) {
52
+ if (match) {
53
53
  return true;
54
54
  }
55
55
  return false;
@@ -0,0 +1 @@
1
+ export {};