hono 1.2.2 → 1.3.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.
- package/README.md +90 -19
- package/dist/context.d.ts +2 -2
- package/dist/context.js +24 -6
- package/dist/hono.d.ts +21 -29
- package/dist/hono.js +27 -65
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -2
- package/dist/middleware/cookie/index.d.ts +4 -1
- package/dist/middleware/cookie/index.js +9 -4
- package/dist/middleware/serve-static/index.d.ts +1 -1
- package/dist/middleware/serve-static/index.js +7 -2
- package/dist/utils/url.js +4 -0
- package/package.json +3 -3
- package/dist/compose.test.d.ts +0 -1
- package/dist/compose.test.js +0 -511
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -127
- package/dist/hono.test.d.ts +0 -1
- package/dist/hono.test.js +0 -592
- package/dist/middleware/basic-auth/index.test.d.ts +0 -1
- package/dist/middleware/basic-auth/index.test.js +0 -119
- package/dist/middleware/body-parse/index.test.d.ts +0 -1
- package/dist/middleware/body-parse/index.test.js +0 -59
- package/dist/middleware/cookie/index.test.d.ts +0 -1
- package/dist/middleware/cookie/index.test.js +0 -54
- package/dist/middleware/cors/index.test.d.ts +0 -1
- package/dist/middleware/cors/index.test.js +0 -59
- package/dist/middleware/etag/index.test.d.ts +0 -1
- package/dist/middleware/etag/index.test.js +0 -45
- package/dist/middleware/graphql-server/index.test.d.ts +0 -1
- package/dist/middleware/graphql-server/index.test.js +0 -480
- package/dist/middleware/graphql-server/parse-body.test.d.ts +0 -1
- package/dist/middleware/graphql-server/parse-body.test.js +0 -57
- package/dist/middleware/jwt/index.test.d.ts +0 -1
- package/dist/middleware/jwt/index.test.js +0 -51
- package/dist/middleware/logger/index.test.d.ts +0 -1
- package/dist/middleware/logger/index.test.js +0 -49
- package/dist/middleware/mustache/index.test.d.ts +0 -1
- package/dist/middleware/mustache/index.test.js +0 -49
- package/dist/middleware/powered-by/index.test.d.ts +0 -1
- package/dist/middleware/powered-by/index.test.js +0 -15
- package/dist/middleware/pretty-json/index.test.d.ts +0 -1
- package/dist/middleware/pretty-json/index.test.js +0 -28
- package/dist/middleware/serve-static/index.test.d.ts +0 -1
- package/dist/middleware/serve-static/index.test.js +0 -58
- package/dist/router/reg-exp-router/router.test.d.ts +0 -1
- package/dist/router/reg-exp-router/router.test.js +0 -212
- package/dist/router/trie-router/node.test.d.ts +0 -1
- package/dist/router/trie-router/node.test.js +0 -351
- package/dist/router/trie-router/router.test.d.ts +0 -1
- package/dist/router/trie-router/router.test.js +0 -98
- package/dist/utils/body.test.d.ts +0 -1
- package/dist/utils/body.test.js +0 -45
- package/dist/utils/buffer.test.d.ts +0 -1
- package/dist/utils/buffer.test.js +0 -36
- package/dist/utils/cloudflare.test.d.ts +0 -1
- package/dist/utils/cloudflare.test.js +0 -42
- package/dist/utils/crypto.test.d.ts +0 -1
- package/dist/utils/crypto.test.js +0 -15
- package/dist/utils/encode.test.d.ts +0 -1
- package/dist/utils/encode.test.js +0 -54
- package/dist/utils/http-status.test.d.ts +0 -1
- package/dist/utils/http-status.test.js +0 -8
- package/dist/utils/jwt/jwt.test.d.ts +0 -1
- package/dist/utils/jwt/jwt.test.js +0 -171
- package/dist/utils/jwt/types.test.d.ts +0 -1
- package/dist/utils/jwt/types.test.js +0 -12
- package/dist/utils/mime.test.d.ts +0 -1
- package/dist/utils/mime.test.js +0 -13
- package/dist/utils/url.test.d.ts +0 -1
- package/dist/utils/url.test.js +0 -87
|
@@ -1,351 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const node_1 = require("./node");
|
|
4
|
-
describe('Root Node', () => {
|
|
5
|
-
const node = new node_1.Node();
|
|
6
|
-
node.insert('get', '/', 'get root');
|
|
7
|
-
it('get /', () => {
|
|
8
|
-
const res = node.search('get', '/');
|
|
9
|
-
expect(res).not.toBeNull();
|
|
10
|
-
expect(res.handlers).toEqual(['get root']);
|
|
11
|
-
expect(node.search('get', '/hello')).toBeNull();
|
|
12
|
-
});
|
|
13
|
-
});
|
|
14
|
-
describe('Root Node is not defined', () => {
|
|
15
|
-
const node = new node_1.Node();
|
|
16
|
-
node.insert('get', '/hello', 'get hello');
|
|
17
|
-
it('get /', () => {
|
|
18
|
-
expect(node.search('get', '/')).toBeNull();
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
describe('Get with *', () => {
|
|
22
|
-
const node = new node_1.Node();
|
|
23
|
-
node.insert('get', '*', 'get all');
|
|
24
|
-
it('get /', () => {
|
|
25
|
-
expect(node.search('get', '/')).not.toBeNull();
|
|
26
|
-
expect(node.search('get', '/hello')).not.toBeNull();
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
describe('Basic Usage', () => {
|
|
30
|
-
const node = new node_1.Node();
|
|
31
|
-
node.insert('get', '/hello', 'get hello');
|
|
32
|
-
node.insert('post', '/hello', 'post hello');
|
|
33
|
-
node.insert('get', '/hello/foo', 'get hello foo');
|
|
34
|
-
it('get, post /hello', () => {
|
|
35
|
-
expect(node.search('get', '/')).toBeNull();
|
|
36
|
-
expect(node.search('post', '/')).toBeNull();
|
|
37
|
-
expect(node.search('get', '/hello').handlers).toEqual(['get hello']);
|
|
38
|
-
expect(node.search('post', '/hello').handlers).toEqual(['post hello']);
|
|
39
|
-
expect(node.search('put', '/hello')).toBeNull();
|
|
40
|
-
});
|
|
41
|
-
it('get /nothing', () => {
|
|
42
|
-
expect(node.search('get', '/nothing')).toBeNull();
|
|
43
|
-
});
|
|
44
|
-
it('/hello/foo, /hello/bar', () => {
|
|
45
|
-
expect(node.search('get', '/hello/foo').handlers).toEqual(['get hello foo']);
|
|
46
|
-
expect(node.search('post', '/hello/foo')).toBeNull();
|
|
47
|
-
expect(node.search('get', '/hello/bar')).toBeNull();
|
|
48
|
-
});
|
|
49
|
-
it('/hello/foo/bar', () => {
|
|
50
|
-
expect(node.search('get', '/hello/foo/bar')).toBeNull();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
describe('Name path', () => {
|
|
54
|
-
const node = new node_1.Node();
|
|
55
|
-
node.insert('get', '/entry/:id', 'get entry');
|
|
56
|
-
node.insert('get', '/entry/:id/comment/:comment_id', 'get comment');
|
|
57
|
-
node.insert('get', '/map/:location/events', 'get events');
|
|
58
|
-
it('get /entry/123', () => {
|
|
59
|
-
const res = node.search('get', '/entry/123');
|
|
60
|
-
expect(res).not.toBeNull();
|
|
61
|
-
expect(res.handlers).toEqual(['get entry']);
|
|
62
|
-
expect(res.params).not.toBeNull();
|
|
63
|
-
expect(res.params['id']).toBe('123');
|
|
64
|
-
expect(res.params['id']).not.toBe('1234');
|
|
65
|
-
});
|
|
66
|
-
it('get /entry/456/comment', () => {
|
|
67
|
-
const res = node.search('get', '/entry/456/comment');
|
|
68
|
-
expect(res).toBeNull();
|
|
69
|
-
});
|
|
70
|
-
it('get /entry/789/comment/123', () => {
|
|
71
|
-
const res = node.search('get', '/entry/789/comment/123');
|
|
72
|
-
expect(res).not.toBeNull();
|
|
73
|
-
expect(res.handlers).toEqual(['get comment']);
|
|
74
|
-
expect(res.params['id']).toBe('789');
|
|
75
|
-
expect(res.params['comment_id']).toBe('123');
|
|
76
|
-
});
|
|
77
|
-
it('get /map/:location/events', () => {
|
|
78
|
-
const res = node.search('get', '/map/yokohama/events');
|
|
79
|
-
expect(res).not.toBeNull();
|
|
80
|
-
expect(res.handlers).toEqual(['get events']);
|
|
81
|
-
expect(res.params['location']).toBe('yokohama');
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
describe('Name path - Multiple route', () => {
|
|
85
|
-
const node = new node_1.Node();
|
|
86
|
-
node.insert('get', '/:type/:id', 'common');
|
|
87
|
-
node.insert('get', '/posts/:id', 'specialized');
|
|
88
|
-
it('get /posts/123', () => {
|
|
89
|
-
const res = node.search('get', '/posts/123');
|
|
90
|
-
expect(res).not.toBeNull();
|
|
91
|
-
expect(res.handlers).toEqual(['common', 'specialized']);
|
|
92
|
-
expect(res.params['id']).toBe('123');
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
describe('Wildcard', () => {
|
|
96
|
-
const node = new node_1.Node();
|
|
97
|
-
node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard');
|
|
98
|
-
it('/wildcard-abc/xxxxxx/wildcard-efg', () => {
|
|
99
|
-
const res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg');
|
|
100
|
-
expect(res).not.toBeNull();
|
|
101
|
-
expect(res.handlers).toEqual(['wildcard']);
|
|
102
|
-
});
|
|
103
|
-
node.insert('get', '/wildcard-abc/*/wildcard-efg/hijk', 'wildcard');
|
|
104
|
-
it('/wildcard-abc/xxxxxx/wildcard-efg/hijk', () => {
|
|
105
|
-
const res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg/hijk');
|
|
106
|
-
expect(res).not.toBeNull();
|
|
107
|
-
expect(res.handlers).toEqual(['wildcard']);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
describe('Regexp', () => {
|
|
111
|
-
const node = new node_1.Node();
|
|
112
|
-
node.insert('get', '/regex-abc/:id{[0-9]+}/comment/:comment_id{[a-z]+}', 'regexp');
|
|
113
|
-
it('/regexp-abc/123/comment/abc', () => {
|
|
114
|
-
const res = node.search('get', '/regex-abc/123/comment/abc');
|
|
115
|
-
expect(res).not.toBeNull();
|
|
116
|
-
expect(res.handlers).toEqual(['regexp']);
|
|
117
|
-
expect(res.params['id']).toBe('123');
|
|
118
|
-
expect(res.params['comment_id']).toBe('abc');
|
|
119
|
-
});
|
|
120
|
-
it('/regexp-abc/abc', () => {
|
|
121
|
-
const res = node.search('get', '/regex-abc/abc');
|
|
122
|
-
expect(res).toBeNull();
|
|
123
|
-
});
|
|
124
|
-
it('/regexp-abc/123/comment/123', () => {
|
|
125
|
-
const res = node.search('get', '/regex-abc/123/comment/123');
|
|
126
|
-
expect(res).toBeNull();
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
describe('All', () => {
|
|
130
|
-
const node = new node_1.Node();
|
|
131
|
-
node.insert('ALL', '/all-methods', 'all methods'); // ALL
|
|
132
|
-
it('/all-methods', () => {
|
|
133
|
-
let res = node.search('get', '/all-methods');
|
|
134
|
-
expect(res).not.toBeNull();
|
|
135
|
-
expect(res.handlers).toEqual(['all methods']);
|
|
136
|
-
res = node.search('put', '/all-methods');
|
|
137
|
-
expect(res).not.toBeNull();
|
|
138
|
-
expect(res.handlers).toEqual(['all methods']);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
describe('Special Wildcard', () => {
|
|
142
|
-
const node = new node_1.Node();
|
|
143
|
-
node.insert('ALL', '*', 'match all');
|
|
144
|
-
it('/foo', () => {
|
|
145
|
-
const res = node.search('get', '/foo');
|
|
146
|
-
expect(res).not.toBeNull();
|
|
147
|
-
expect(res.handlers).toEqual(['match all']);
|
|
148
|
-
});
|
|
149
|
-
it('/hello', () => {
|
|
150
|
-
const res = node.search('get', '/hello');
|
|
151
|
-
expect(res).not.toBeNull();
|
|
152
|
-
expect(res.handlers).toEqual(['match all']);
|
|
153
|
-
});
|
|
154
|
-
it('/hello/foo', () => {
|
|
155
|
-
const res = node.search('get', '/hello/foo');
|
|
156
|
-
expect(res).not.toBeNull();
|
|
157
|
-
expect(res.handlers).toEqual(['match all']);
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
describe('Special Wildcard deeply', () => {
|
|
161
|
-
const node = new node_1.Node();
|
|
162
|
-
node.insert('ALL', '/hello/*', 'match hello');
|
|
163
|
-
it('/hello', () => {
|
|
164
|
-
const res = node.search('get', '/hello');
|
|
165
|
-
expect(res).not.toBeNull();
|
|
166
|
-
expect(res.handlers).toEqual(['match hello']);
|
|
167
|
-
});
|
|
168
|
-
it('/hello/foo', () => {
|
|
169
|
-
const res = node.search('get', '/hello/foo');
|
|
170
|
-
expect(res).not.toBeNull();
|
|
171
|
-
expect(res.handlers).toEqual(['match hello']);
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
describe('Default with wildcard', () => {
|
|
175
|
-
const node = new node_1.Node();
|
|
176
|
-
node.insert('ALL', '/api/abc', 'match api');
|
|
177
|
-
node.insert('ALL', '/api/*', 'fallback');
|
|
178
|
-
it('/api/abc', () => {
|
|
179
|
-
const res = node.search('get', '/api/abc');
|
|
180
|
-
expect(res).not.toBeNull();
|
|
181
|
-
expect(res.handlers).toEqual(['fallback', 'match api']);
|
|
182
|
-
});
|
|
183
|
-
it('/api/def', () => {
|
|
184
|
-
const res = node.search('get', '/api/def');
|
|
185
|
-
expect(res).not.toBeNull();
|
|
186
|
-
expect(res.handlers).toEqual(['fallback']);
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
describe('Multi match', () => {
|
|
190
|
-
describe('Basic', () => {
|
|
191
|
-
const node = new node_1.Node();
|
|
192
|
-
node.insert('get', '*', 'GET *');
|
|
193
|
-
node.insert('get', '/abc/*', 'GET /abc/*');
|
|
194
|
-
node.insert('get', '/abc/*/edf', 'GET /abc/*/edf');
|
|
195
|
-
node.insert('get', '/abc/edf', 'GET /abc/edf');
|
|
196
|
-
node.insert('get', '/abc/*/ghi/jkl', 'GET /abc/*/ghi/jkl');
|
|
197
|
-
it('get /abc/edf', () => {
|
|
198
|
-
const res = node.search('get', '/abc/edf');
|
|
199
|
-
expect(res).not.toBeNull();
|
|
200
|
-
expect(res.handlers).toEqual(['GET *', 'GET /abc/*', 'GET /abc/edf']);
|
|
201
|
-
});
|
|
202
|
-
it('get /abc/xxx/edf', () => {
|
|
203
|
-
const res = node.search('get', '/abc/xxx/edf');
|
|
204
|
-
expect(res).not.toBeNull();
|
|
205
|
-
expect(res.handlers).toEqual(['GET *', 'GET /abc/*', 'GET /abc/*/edf']);
|
|
206
|
-
});
|
|
207
|
-
it('get /', () => {
|
|
208
|
-
const res = node.search('get', '/');
|
|
209
|
-
expect(res).not.toBeNull();
|
|
210
|
-
expect(res.handlers).toEqual(['GET *']);
|
|
211
|
-
});
|
|
212
|
-
it('post /', () => {
|
|
213
|
-
const res = node.search('post', '/');
|
|
214
|
-
expect(res).toBeNull();
|
|
215
|
-
});
|
|
216
|
-
it('get /abc/edf/ghi', () => {
|
|
217
|
-
const res = node.search('get', '/abc/edf/ghi');
|
|
218
|
-
expect(res).not.toBeNull();
|
|
219
|
-
expect(res.handlers).toEqual(['GET *', 'GET /abc/*']);
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
describe('Blog', () => {
|
|
223
|
-
const node = new node_1.Node();
|
|
224
|
-
node.insert('get', '*', 'middleware a');
|
|
225
|
-
node.insert('ALL', '*', 'middleware b');
|
|
226
|
-
node.insert('get', '/entry', 'get entries');
|
|
227
|
-
node.insert('post', '/entry/*', 'middleware c');
|
|
228
|
-
node.insert('post', '/entry', 'post entry');
|
|
229
|
-
node.insert('get', '/entry/:id', 'get entry');
|
|
230
|
-
node.insert('get', '/entry/:id/comment/:comment_id', 'get comment');
|
|
231
|
-
it('get /entry/123', async () => {
|
|
232
|
-
const res = node.search('get', '/entry/123');
|
|
233
|
-
expect(res).not.toBeNull();
|
|
234
|
-
expect(res.handlers).toEqual(['middleware a', 'middleware b', 'get entry']);
|
|
235
|
-
expect(res.params['id']).toBe('123');
|
|
236
|
-
});
|
|
237
|
-
it('get /entry/123/comment/456', async () => {
|
|
238
|
-
const res = node.search('get', '/entry/123/comment/456');
|
|
239
|
-
expect(res).not.toBeNull();
|
|
240
|
-
expect(res.handlers).toEqual(['middleware a', 'middleware b', 'get comment']);
|
|
241
|
-
expect(res.params['id']).toBe('123');
|
|
242
|
-
expect(res.params['comment_id']).toBe('456');
|
|
243
|
-
});
|
|
244
|
-
it('post /entry', async () => {
|
|
245
|
-
const res = node.search('post', '/entry');
|
|
246
|
-
expect(res).not.toBeNull();
|
|
247
|
-
expect(res.handlers).toEqual(['middleware b', 'middleware c', 'post entry']);
|
|
248
|
-
});
|
|
249
|
-
it('delete /entry', async () => {
|
|
250
|
-
const res = node.search('delete', '/entry');
|
|
251
|
-
expect(res).not.toBeNull();
|
|
252
|
-
expect(res.handlers).toEqual(['middleware b']);
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
describe('ALL', () => {
|
|
256
|
-
const node = new node_1.Node();
|
|
257
|
-
node.insert('ALL', '*', 'ALL *');
|
|
258
|
-
node.insert('ALL', '/abc/*', 'ALL /abc/*');
|
|
259
|
-
node.insert('ALL', '/abc/*/def', 'ALL /abc/*/def');
|
|
260
|
-
it('get /', () => {
|
|
261
|
-
const res = node.search('get', '/');
|
|
262
|
-
expect(res).not.toBeNull();
|
|
263
|
-
expect(res.handlers).toEqual(['ALL *']);
|
|
264
|
-
});
|
|
265
|
-
it('post /abc', () => {
|
|
266
|
-
const res = node.search('post', '/abc');
|
|
267
|
-
expect(res).not.toBeNull();
|
|
268
|
-
expect(res.handlers).toEqual(['ALL *', 'ALL /abc/*']);
|
|
269
|
-
});
|
|
270
|
-
it('delete /abc/xxx/def', () => {
|
|
271
|
-
const res = node.search('post', '/abc/xxx/def');
|
|
272
|
-
expect(res).not.toBeNull();
|
|
273
|
-
expect(res.handlers).toEqual(['ALL *', 'ALL /abc/*', 'ALL /abc/*/def']);
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
describe('Regexp', () => {
|
|
277
|
-
const node = new node_1.Node();
|
|
278
|
-
node.insert('get', '/regex-abc/:id{[0-9]+}/*', 'middleware a');
|
|
279
|
-
node.insert('get', '/regex-abc/:id{[0-9]+}/def', 'regexp');
|
|
280
|
-
it('/regexp-abc/123/def', () => {
|
|
281
|
-
const res = node.search('get', '/regex-abc/123/def');
|
|
282
|
-
expect(res).not.toBeNull();
|
|
283
|
-
expect(res.handlers).toEqual(['middleware a', 'regexp']);
|
|
284
|
-
expect(res.params['id']).toBe('123');
|
|
285
|
-
});
|
|
286
|
-
it('/regexp-abc/123', () => {
|
|
287
|
-
const res = node.search('get', '/regex-abc/123/ghi');
|
|
288
|
-
expect(res).not.toBeNull();
|
|
289
|
-
expect(res.handlers).toEqual(['middleware a']);
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
describe('Trailing slash', () => {
|
|
293
|
-
const node = new node_1.Node();
|
|
294
|
-
node.insert('get', '/book', 'GET /book');
|
|
295
|
-
node.insert('get', '/book/:id', 'GET /book/:id');
|
|
296
|
-
it('get /book', () => {
|
|
297
|
-
const res = node.search('get', '/book');
|
|
298
|
-
expect(res).not.toBeNull();
|
|
299
|
-
});
|
|
300
|
-
it('get /book/', () => {
|
|
301
|
-
const res = node.search('get', '/book/');
|
|
302
|
-
expect(res).toBeNull();
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
describe('Same path', () => {
|
|
306
|
-
const node = new node_1.Node();
|
|
307
|
-
node.insert('get', '/hey', 'Middleware A');
|
|
308
|
-
node.insert('get', '/hey', 'Middleware B');
|
|
309
|
-
it('get /hey', () => {
|
|
310
|
-
const res = node.search('get', '/hey');
|
|
311
|
-
expect(res).not.toBeNull();
|
|
312
|
-
expect(res.handlers).toEqual(['Middleware A', 'Middleware B']);
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
describe('Duplicate param name', () => {
|
|
317
|
-
it('self', () => {
|
|
318
|
-
const node = new node_1.Node();
|
|
319
|
-
expect(() => {
|
|
320
|
-
node.insert('get', '/:id/:id', 'foo');
|
|
321
|
-
}).toThrowError(/Duplicate param name/);
|
|
322
|
-
});
|
|
323
|
-
it('parent', () => {
|
|
324
|
-
const node = new node_1.Node();
|
|
325
|
-
node.insert('get', '/:id/:action', 'foo');
|
|
326
|
-
expect(() => {
|
|
327
|
-
node.insert('get', '/posts/:id', 'bar');
|
|
328
|
-
}).toThrowError(/Duplicate param name/);
|
|
329
|
-
});
|
|
330
|
-
it('child', () => {
|
|
331
|
-
const node = new node_1.Node();
|
|
332
|
-
node.insert('get', '/posts/:id', 'foo');
|
|
333
|
-
expect(() => {
|
|
334
|
-
node.insert('get', '/:id/:action', 'bar');
|
|
335
|
-
}).toThrowError(/Duplicate param name/);
|
|
336
|
-
});
|
|
337
|
-
it('hierarchy', () => {
|
|
338
|
-
const node = new node_1.Node();
|
|
339
|
-
node.insert('get', '/posts/:id/comments/:comment_id', 'foo');
|
|
340
|
-
expect(() => {
|
|
341
|
-
node.insert('get', '/posts/:id', 'bar');
|
|
342
|
-
}).not.toThrowError();
|
|
343
|
-
});
|
|
344
|
-
it('regular expression', () => {
|
|
345
|
-
const node = new node_1.Node();
|
|
346
|
-
node.insert('get', '/:id/:action{create|update}', 'foo');
|
|
347
|
-
expect(() => {
|
|
348
|
-
node.insert('get', '/:id/:action{delete}', 'bar');
|
|
349
|
-
}).not.toThrowError();
|
|
350
|
-
});
|
|
351
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,98 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/utils/body.test.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,42 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|