hono 1.3.4 → 1.4.0
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 +92 -18
- package/dist/compose.js +12 -6
- package/dist/context.d.ts +1 -3
- package/dist/context.js +35 -18
- package/dist/hono.d.ts +10 -5
- package/dist/hono.js +26 -17
- package/dist/middleware/jwt/index.js +3 -1
- package/dist/middleware/mustache/mustache.js +18 -17
- package/dist/middleware/serve-static/serve-static.js +11 -7
- package/dist/router/reg-exp-router/router.d.ts +10 -4
- package/dist/router/reg-exp-router/router.js +42 -14
- package/dist/router/trie-router/index.js +5 -1
- package/dist/router/trie-router/node.d.ts +11 -4
- package/dist/router/trie-router/node.js +55 -30
- package/dist/router/trie-router/router.d.ts +2 -3
- package/dist/router/trie-router/router.js +7 -5
- package/dist/router.d.ts +4 -5
- package/dist/router.js +5 -10
- package/dist/utils/buffer.js +11 -5
- package/dist/utils/cloudflare.js +7 -2
- package/dist/utils/crypto.js +11 -14
- package/dist/utils/encode.js +0 -26
- package/dist/utils/http-status.js +5 -1
- package/dist/utils/mime.js +5 -1
- package/dist/utils/url.js +13 -5
- package/package.json +2 -2
|
@@ -64,6 +64,14 @@ function compareRoute(a, b) {
|
|
|
64
64
|
}
|
|
65
65
|
return i === b.hint.regExpComponents.length || a.hint.endWithWildcard ? 1 : 0;
|
|
66
66
|
}
|
|
67
|
+
function compareHandler(a, b) {
|
|
68
|
+
return a.componentsLength !== b.componentsLength
|
|
69
|
+
? a.componentsLength - b.componentsLength
|
|
70
|
+
: a.index - b.index;
|
|
71
|
+
}
|
|
72
|
+
function getSortedHandlers(handlers) {
|
|
73
|
+
return [...handlers].sort(compareHandler).map((h) => h.handler);
|
|
74
|
+
}
|
|
67
75
|
function buildMatcherFromPreprocessedRoutes(routes, hasAmbiguous = false) {
|
|
68
76
|
const trie = new trie_1.Trie({ reverse: hasAmbiguous });
|
|
69
77
|
const handlers = [];
|
|
@@ -76,6 +84,9 @@ function buildMatcherFromPreprocessedRoutes(routes, hasAmbiguous = false) {
|
|
|
76
84
|
[...routes[i].middleware, ...routes[i].handlers],
|
|
77
85
|
Object.keys(paramMap).length !== 0 ? paramMap : null,
|
|
78
86
|
];
|
|
87
|
+
if (!hasAmbiguous) {
|
|
88
|
+
handlers[i][0] = getSortedHandlers(handlers[i][0]);
|
|
89
|
+
}
|
|
79
90
|
}
|
|
80
91
|
const [regexp, indexReplacementMap, paramReplacementMap] = trie.buildRegExp();
|
|
81
92
|
for (let i = 0, len = handlers.length; i < len; i++) {
|
|
@@ -130,22 +141,28 @@ function verifyDuplicateParam(routes) {
|
|
|
130
141
|
}
|
|
131
142
|
return true;
|
|
132
143
|
}
|
|
133
|
-
class RegExpRouter
|
|
144
|
+
class RegExpRouter {
|
|
134
145
|
constructor() {
|
|
135
|
-
|
|
136
|
-
this.routeData = { routes: [], methods: new Set() };
|
|
146
|
+
this.routeData = { index: 0, routes: [], methods: new Set() };
|
|
137
147
|
}
|
|
138
148
|
add(method, path, handler) {
|
|
139
149
|
if (!this.routeData) {
|
|
140
150
|
throw new Error('Can not add a route since the matcher is already built.');
|
|
141
151
|
}
|
|
142
|
-
|
|
152
|
+
this.routeData.index++;
|
|
153
|
+
const { index, routes, methods } = this.routeData;
|
|
143
154
|
if (path === '/*') {
|
|
144
155
|
path = '*';
|
|
145
156
|
}
|
|
157
|
+
const hint = initHint(path);
|
|
158
|
+
const handlerWithSortIndex = {
|
|
159
|
+
index,
|
|
160
|
+
handler,
|
|
161
|
+
componentsLength: hint.components.length,
|
|
162
|
+
};
|
|
146
163
|
for (let i = 0, len = routes.length; i < len; i++) {
|
|
147
164
|
if (routes[i].method === method && routes[i].path === path) {
|
|
148
|
-
routes[i].handlers.push(
|
|
165
|
+
routes[i].handlers.push(handlerWithSortIndex);
|
|
149
166
|
return;
|
|
150
167
|
}
|
|
151
168
|
}
|
|
@@ -153,8 +170,8 @@ class RegExpRouter extends router_1.Router {
|
|
|
153
170
|
routes.push({
|
|
154
171
|
method,
|
|
155
172
|
path,
|
|
156
|
-
|
|
157
|
-
|
|
173
|
+
hint,
|
|
174
|
+
handlers: [handlerWithSortIndex],
|
|
158
175
|
middleware: [],
|
|
159
176
|
paramAliasMap: {},
|
|
160
177
|
});
|
|
@@ -163,7 +180,8 @@ class RegExpRouter extends router_1.Router {
|
|
|
163
180
|
const [primaryMatchers, secondaryMatchers, hasAmbiguous] = this.buildAllMatchers();
|
|
164
181
|
this.match = hasAmbiguous
|
|
165
182
|
? (method, path) => {
|
|
166
|
-
const matcher = primaryMatchers[method] ||
|
|
183
|
+
const matcher = (primaryMatchers[method] ||
|
|
184
|
+
primaryMatchers[router_1.METHOD_NAME_ALL]);
|
|
167
185
|
let match = path.match(matcher[0]);
|
|
168
186
|
if (!match) {
|
|
169
187
|
// do not support secondary matchers here.
|
|
@@ -193,10 +211,10 @@ class RegExpRouter extends router_1.Router {
|
|
|
193
211
|
regExpSrc = regExpSrc.replace(new RegExp(`((?:(?:\\(\\?:|.)*?\\([^)]*\\)){${index - 1}}.*?)\\(\\)`), '$1(^)');
|
|
194
212
|
match = path.match(new RegExp(regExpSrc));
|
|
195
213
|
}
|
|
196
|
-
return
|
|
214
|
+
return { handlers: getSortedHandlers(handlers.values()), params };
|
|
197
215
|
}
|
|
198
216
|
: (method, path) => {
|
|
199
|
-
let matcher = primaryMatchers[method] || primaryMatchers[router_1.METHOD_NAME_ALL];
|
|
217
|
+
let matcher = (primaryMatchers[method] || primaryMatchers[router_1.METHOD_NAME_ALL]);
|
|
200
218
|
let match = path.match(matcher[0]);
|
|
201
219
|
if (!match) {
|
|
202
220
|
const matchers = secondaryMatchers[method] || secondaryMatchers[router_1.METHOD_NAME_ALL];
|
|
@@ -209,15 +227,15 @@ class RegExpRouter extends router_1.Router {
|
|
|
209
227
|
}
|
|
210
228
|
}
|
|
211
229
|
const index = match.indexOf('', 1);
|
|
212
|
-
const [
|
|
230
|
+
const [handlers, paramMap] = matcher[1][index];
|
|
213
231
|
if (!paramMap) {
|
|
214
|
-
return
|
|
232
|
+
return { handlers, params: emptyParam };
|
|
215
233
|
}
|
|
216
234
|
const params = {};
|
|
217
235
|
for (let i = 0, len = paramMap.length; i < len; i++) {
|
|
218
236
|
params[paramMap[i][0]] = match[paramMap[i][1]];
|
|
219
237
|
}
|
|
220
|
-
return
|
|
238
|
+
return { handlers, params };
|
|
221
239
|
};
|
|
222
240
|
return this.match(method, path);
|
|
223
241
|
}
|
|
@@ -296,7 +314,17 @@ class RegExpRouter extends router_1.Router {
|
|
|
296
314
|
routes[j].path = components.join('');
|
|
297
315
|
}
|
|
298
316
|
}
|
|
299
|
-
routes[j].
|
|
317
|
+
if (routes[j].hint.components.length < routes[i].hint.components.length) {
|
|
318
|
+
const componentsLength = routes[j].hint.components.length;
|
|
319
|
+
routes[j].middleware.push(...routes[i].handlers.map((h) => ({
|
|
320
|
+
componentsLength,
|
|
321
|
+
index: h.index,
|
|
322
|
+
handler: h.handler,
|
|
323
|
+
})));
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
routes[j].middleware.push(...routes[i].handlers);
|
|
327
|
+
}
|
|
300
328
|
routes[i].hint.maybeHandler = false;
|
|
301
329
|
}
|
|
302
330
|
else if (compareResult === 2) {
|
|
@@ -1 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TrieRouter = void 0;
|
|
4
|
+
var router_1 = require("./router");
|
|
5
|
+
Object.defineProperty(exports, "TrieRouter", { enumerable: true, get: function () { return router_1.TrieRouter; } });
|
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
import { Result } from '../../router';
|
|
1
|
+
import type { Result } from '../../router';
|
|
2
2
|
import type { Pattern } from '../../utils/url';
|
|
3
|
+
declare type HandlerSet<T> = {
|
|
4
|
+
handler: T;
|
|
5
|
+
score: number;
|
|
6
|
+
name: string;
|
|
7
|
+
};
|
|
3
8
|
export declare class Node<T> {
|
|
4
|
-
methods: Record<string, T
|
|
5
|
-
handlers: T[];
|
|
9
|
+
methods: Record<string, HandlerSet<T>>[];
|
|
6
10
|
children: Record<string, Node<T>>;
|
|
7
11
|
patterns: Pattern[];
|
|
12
|
+
order: number;
|
|
13
|
+
name: string;
|
|
8
14
|
constructor(method?: string, handler?: T, children?: Record<string, Node<T>>);
|
|
9
15
|
insert(method: string, path: string, handler: T): Node<T>;
|
|
10
|
-
private
|
|
16
|
+
private getHandlerSets;
|
|
11
17
|
private next;
|
|
12
18
|
search(method: string, path: string): Result<T>;
|
|
13
19
|
}
|
|
20
|
+
export {};
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Node = void 0;
|
|
4
|
+
const router_1 = require("../../router");
|
|
5
|
+
const url_1 = require("../../utils/url");
|
|
3
6
|
function findParam(node, name) {
|
|
4
7
|
for (let i = 0, len = node.patterns.length; i < len; i++) {
|
|
5
8
|
if (typeof node.patterns[i] === 'object' && node.patterns[i][1] === name) {
|
|
@@ -14,21 +17,24 @@ function findParam(node, name) {
|
|
|
14
17
|
}
|
|
15
18
|
return false;
|
|
16
19
|
}
|
|
17
|
-
|
|
20
|
+
class Node {
|
|
18
21
|
constructor(method, handler, children) {
|
|
22
|
+
this.order = 0;
|
|
19
23
|
this.children = children || {};
|
|
20
24
|
this.methods = [];
|
|
21
25
|
if (method && handler) {
|
|
22
26
|
const m = {};
|
|
23
|
-
m[method] = handler;
|
|
27
|
+
m[method] = { handler: handler, score: 0, name: this.name };
|
|
24
28
|
this.methods = [m];
|
|
25
29
|
}
|
|
26
30
|
this.patterns = [];
|
|
27
31
|
}
|
|
28
32
|
insert(method, path, handler) {
|
|
33
|
+
this.name = `${method} ${path}`;
|
|
34
|
+
this.order = ++this.order;
|
|
29
35
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
30
36
|
let curNode = this;
|
|
31
|
-
const parts = splitPath(path);
|
|
37
|
+
const parts = (0, url_1.splitPath)(path);
|
|
32
38
|
const parentPatterns = [];
|
|
33
39
|
const errorMessage = (name) => {
|
|
34
40
|
return `Duplicate param name, use another name instead of '${name}' - ${method} ${path} <--- '${name}'`;
|
|
@@ -41,7 +47,7 @@ export class Node {
|
|
|
41
47
|
continue;
|
|
42
48
|
}
|
|
43
49
|
curNode.children[p] = new Node();
|
|
44
|
-
const pattern = getPattern(p);
|
|
50
|
+
const pattern = (0, url_1.getPattern)(p);
|
|
45
51
|
if (pattern) {
|
|
46
52
|
if (typeof pattern === 'object') {
|
|
47
53
|
for (let j = 0, len = parentPatterns.length; j < len; j++) {
|
|
@@ -59,32 +65,39 @@ export class Node {
|
|
|
59
65
|
parentPatterns.push(...curNode.patterns);
|
|
60
66
|
curNode = curNode.children[p];
|
|
61
67
|
}
|
|
68
|
+
let score = 1;
|
|
69
|
+
if (path === '*') {
|
|
70
|
+
score = score + this.order * 0.01;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
score = parts.length + 1 + this.order * 0.01;
|
|
74
|
+
}
|
|
62
75
|
if (!curNode.methods.length) {
|
|
63
76
|
curNode.methods = [];
|
|
64
77
|
}
|
|
65
78
|
const m = {};
|
|
66
|
-
|
|
79
|
+
const handlerSet = { handler: handler, name: this.name, score: score };
|
|
80
|
+
m[method] = handlerSet;
|
|
67
81
|
curNode.methods.push(m);
|
|
68
82
|
return curNode;
|
|
69
83
|
}
|
|
70
|
-
|
|
71
|
-
const
|
|
84
|
+
getHandlerSets(node, method, wildcard) {
|
|
85
|
+
const handlerSets = [];
|
|
72
86
|
node.methods.map((m) => {
|
|
73
|
-
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
handlers.push(handler);
|
|
87
|
+
const handlerSet = m[method] || m[router_1.METHOD_NAME_ALL];
|
|
88
|
+
if (handlerSet !== undefined) {
|
|
89
|
+
const hs = Object.assign({}, handlerSet);
|
|
90
|
+
if (wildcard) {
|
|
91
|
+
hs.score = handlerSet.score - 1;
|
|
92
|
+
}
|
|
93
|
+
handlerSets.push(hs);
|
|
81
94
|
return;
|
|
82
95
|
}
|
|
83
96
|
});
|
|
84
|
-
return
|
|
97
|
+
return handlerSets;
|
|
85
98
|
}
|
|
86
99
|
next(node, part, method, isLast) {
|
|
87
|
-
const
|
|
100
|
+
const handlerSets = [];
|
|
88
101
|
const nextNodes = [];
|
|
89
102
|
const params = {};
|
|
90
103
|
for (let j = 0, len = node.patterns.length; j < len; j++) {
|
|
@@ -94,7 +107,11 @@ export class Node {
|
|
|
94
107
|
if (pattern === '*') {
|
|
95
108
|
const astNode = node.children['*'];
|
|
96
109
|
if (astNode) {
|
|
97
|
-
|
|
110
|
+
let wildcard = false;
|
|
111
|
+
if (!Object.keys(astNode.children).length) {
|
|
112
|
+
wildcard = true;
|
|
113
|
+
}
|
|
114
|
+
handlerSets.push(...this.getHandlerSets(astNode, method, wildcard));
|
|
98
115
|
nextNodes.push(astNode);
|
|
99
116
|
}
|
|
100
117
|
}
|
|
@@ -106,7 +123,7 @@ export class Node {
|
|
|
106
123
|
if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {
|
|
107
124
|
if (typeof key === 'string') {
|
|
108
125
|
if (isLast === true) {
|
|
109
|
-
|
|
126
|
+
handlerSets.push(...this.getHandlerSets(node.children[key], method));
|
|
110
127
|
}
|
|
111
128
|
nextNodes.push(node.children[key]);
|
|
112
129
|
}
|
|
@@ -119,27 +136,27 @@ export class Node {
|
|
|
119
136
|
if (nextNode) {
|
|
120
137
|
if (isLast === true) {
|
|
121
138
|
// '/hello/*' => match '/hello'
|
|
122
|
-
if (nextNode.children['*']
|
|
123
|
-
|
|
139
|
+
if (nextNode.children['*']) {
|
|
140
|
+
handlerSets.push(...this.getHandlerSets(nextNode.children['*'], method, true));
|
|
124
141
|
}
|
|
125
|
-
|
|
142
|
+
handlerSets.push(...this.getHandlerSets(nextNode, method));
|
|
126
143
|
}
|
|
127
144
|
nextNodes.push(nextNode);
|
|
128
145
|
}
|
|
129
146
|
const next = {
|
|
130
147
|
nodes: nextNodes,
|
|
131
|
-
|
|
148
|
+
handlerSets: handlerSets,
|
|
132
149
|
params: params,
|
|
133
150
|
};
|
|
134
151
|
return next;
|
|
135
152
|
}
|
|
136
153
|
search(method, path) {
|
|
137
|
-
const
|
|
154
|
+
const handlerSets = [];
|
|
138
155
|
let params = {};
|
|
139
156
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
140
157
|
const curNode = this;
|
|
141
158
|
let curNodes = [curNode];
|
|
142
|
-
const parts = splitPath(path);
|
|
159
|
+
const parts = (0, url_1.splitPath)(path);
|
|
143
160
|
for (let i = 0, len = parts.length; i < len; i++) {
|
|
144
161
|
const p = parts[i];
|
|
145
162
|
const isLast = i === len - 1;
|
|
@@ -149,14 +166,22 @@ export class Node {
|
|
|
149
166
|
if (res.nodes.length === 0) {
|
|
150
167
|
continue;
|
|
151
168
|
}
|
|
152
|
-
|
|
169
|
+
handlerSets.push(...res.handlerSets);
|
|
153
170
|
params = Object.assign(params, res.params);
|
|
154
171
|
tempNodes.push(...res.nodes);
|
|
155
172
|
}
|
|
156
173
|
curNodes = tempNodes;
|
|
157
174
|
}
|
|
158
|
-
if (
|
|
175
|
+
if (handlerSets.length <= 0)
|
|
159
176
|
return null;
|
|
160
|
-
|
|
177
|
+
const handlers = handlerSets
|
|
178
|
+
.sort((a, b) => {
|
|
179
|
+
return a.score - b.score;
|
|
180
|
+
})
|
|
181
|
+
.map((s) => {
|
|
182
|
+
return s.handler;
|
|
183
|
+
});
|
|
184
|
+
return { handlers, params };
|
|
161
185
|
}
|
|
162
186
|
}
|
|
187
|
+
exports.Node = Node;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Router } from '../../router';
|
|
2
|
-
import type { Result } from '../../router';
|
|
1
|
+
import type { Result, Router } from '../../router';
|
|
3
2
|
import { Node } from './node';
|
|
4
|
-
export declare class TrieRouter<T>
|
|
3
|
+
export declare class TrieRouter<T> implements Router<T> {
|
|
5
4
|
node: Node<T>;
|
|
6
5
|
constructor();
|
|
7
6
|
add(method: string, path: string, handler: T): void;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TrieRouter = void 0;
|
|
4
|
+
const node_1 = require("./node");
|
|
5
|
+
class TrieRouter {
|
|
4
6
|
constructor() {
|
|
5
|
-
|
|
6
|
-
this.node = new Node();
|
|
7
|
+
this.node = new node_1.Node();
|
|
7
8
|
}
|
|
8
9
|
add(method, path, handler) {
|
|
9
10
|
this.node.insert(method, path, handler);
|
|
@@ -12,3 +13,4 @@ export class TrieRouter extends Router {
|
|
|
12
13
|
return this.node.search(method, path);
|
|
13
14
|
}
|
|
14
15
|
}
|
|
16
|
+
exports.TrieRouter = TrieRouter;
|
package/dist/router.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
export declare const METHOD_NAME_ALL: "ALL";
|
|
2
2
|
export declare const METHOD_NAME_ALL_LOWERCASE: "all";
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
export interface Router<T> {
|
|
4
|
+
add(method: string, path: string, handler: T): void;
|
|
5
|
+
match(method: string, path: string): Result<T> | null;
|
|
6
6
|
}
|
|
7
|
-
export
|
|
7
|
+
export interface Result<T> {
|
|
8
8
|
handlers: T[];
|
|
9
9
|
params: Record<string, string>;
|
|
10
|
-
constructor(handlers: T[], params: Record<string, string>);
|
|
11
10
|
}
|
package/dist/router.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
constructor(handlers, params) {
|
|
7
|
-
this.handlers = handlers;
|
|
8
|
-
this.params = params;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.METHOD_NAME_ALL_LOWERCASE = exports.METHOD_NAME_ALL = void 0;
|
|
4
|
+
exports.METHOD_NAME_ALL = 'ALL';
|
|
5
|
+
exports.METHOD_NAME_ALL_LOWERCASE = 'all';
|
package/dist/utils/buffer.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bufferToString = exports.timingSafeEqual = exports.equal = void 0;
|
|
4
|
+
const crypto_1 = require("./crypto");
|
|
5
|
+
const equal = (a, b) => {
|
|
3
6
|
if (a === b) {
|
|
4
7
|
return true;
|
|
5
8
|
}
|
|
@@ -16,18 +19,21 @@ export const equal = (a, b) => {
|
|
|
16
19
|
}
|
|
17
20
|
return true;
|
|
18
21
|
};
|
|
19
|
-
|
|
22
|
+
exports.equal = equal;
|
|
23
|
+
const timingSafeEqual = async (a, b, hashFunction) => {
|
|
20
24
|
if (!hashFunction) {
|
|
21
|
-
hashFunction = sha256;
|
|
25
|
+
hashFunction = crypto_1.sha256;
|
|
22
26
|
}
|
|
23
27
|
const sa = await hashFunction(a);
|
|
24
28
|
const sb = await hashFunction(b);
|
|
25
29
|
return sa === sb && a === b;
|
|
26
30
|
};
|
|
27
|
-
|
|
31
|
+
exports.timingSafeEqual = timingSafeEqual;
|
|
32
|
+
const bufferToString = (buffer) => {
|
|
28
33
|
if (buffer instanceof ArrayBuffer) {
|
|
29
34
|
const enc = new TextDecoder('utf-8');
|
|
30
35
|
return enc.decode(buffer);
|
|
31
36
|
}
|
|
32
37
|
return buffer;
|
|
33
38
|
};
|
|
39
|
+
exports.bufferToString = bufferToString;
|
package/dist/utils/cloudflare.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getKVFilePath = exports.getContentFromKVAsset = void 0;
|
|
4
|
+
const getContentFromKVAsset = async (path, options) => {
|
|
2
5
|
let ASSET_MANIFEST = {};
|
|
3
6
|
if (options && options.manifest) {
|
|
4
7
|
if (typeof options.manifest === 'string') {
|
|
@@ -33,7 +36,8 @@ export const getContentFromKVAsset = async (path, options) => {
|
|
|
33
36
|
}
|
|
34
37
|
return content;
|
|
35
38
|
};
|
|
36
|
-
|
|
39
|
+
exports.getContentFromKVAsset = getContentFromKVAsset;
|
|
40
|
+
const getKVFilePath = (options) => {
|
|
37
41
|
let filename = options.filename;
|
|
38
42
|
let root = options.root || '';
|
|
39
43
|
const defaultDocument = options.defaultDocument || 'index.html';
|
|
@@ -54,3 +58,4 @@ export const getKVFilePath = (options) => {
|
|
|
54
58
|
path = path.replace(/^\.?\//, '');
|
|
55
59
|
return path;
|
|
56
60
|
};
|
|
61
|
+
exports.getKVFilePath = getKVFilePath;
|
package/dist/utils/crypto.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHash = exports.sha1 = exports.sha256 = void 0;
|
|
4
|
+
const sha256 = async (data) => {
|
|
2
5
|
const algorithm = { name: 'SHA-256', alias: 'sha256' };
|
|
3
|
-
const hash = await createHash(data, algorithm);
|
|
6
|
+
const hash = await (0, exports.createHash)(data, algorithm);
|
|
4
7
|
return hash;
|
|
5
8
|
};
|
|
6
|
-
|
|
9
|
+
exports.sha256 = sha256;
|
|
10
|
+
const sha1 = async (data) => {
|
|
7
11
|
const algorithm = { name: 'SHA-1', alias: 'sha1' };
|
|
8
|
-
const hash = await createHash(data, algorithm);
|
|
12
|
+
const hash = await (0, exports.createHash)(data, algorithm);
|
|
9
13
|
return hash;
|
|
10
14
|
};
|
|
11
|
-
|
|
15
|
+
exports.sha1 = sha1;
|
|
16
|
+
const createHash = async (data, algorithm) => {
|
|
12
17
|
if (crypto && crypto.subtle) {
|
|
13
18
|
const buffer = await crypto.subtle.digest({
|
|
14
19
|
name: algorithm.name,
|
|
@@ -18,13 +23,5 @@ export const createHash = async (data, algorithm) => {
|
|
|
18
23
|
.join('');
|
|
19
24
|
return hash;
|
|
20
25
|
}
|
|
21
|
-
try {
|
|
22
|
-
const crypto = require('crypto');
|
|
23
|
-
const hash = crypto.createHash(algorithm.alias).update(data).digest('hex');
|
|
24
|
-
return hash;
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
console.error(`If you want to create hash ${algorithm.name}, polyfill "crypto" module.`);
|
|
28
|
-
throw e;
|
|
29
|
-
}
|
|
30
26
|
};
|
|
27
|
+
exports.createHash = createHash;
|
package/dist/utils/encode.js
CHANGED
|
@@ -1,27 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
3
|
exports.arrayBufferToBase64URL = exports.arrayBufferToBase64 = exports.utf8ToUint8Array = exports.decodeBase64URL = exports.encodeBase64URL = exports.decodeBase64 = exports.encodeBase64 = void 0;
|
|
27
4
|
const encodeBase64 = (str) => {
|
|
@@ -35,7 +12,6 @@ const encodeBase64 = (str) => {
|
|
|
35
12
|
}
|
|
36
13
|
catch (_a) { }
|
|
37
14
|
try {
|
|
38
|
-
const { Buffer } = require('buffer');
|
|
39
15
|
return Buffer.from(str).toString('base64');
|
|
40
16
|
}
|
|
41
17
|
catch (e) {
|
|
@@ -56,7 +32,6 @@ const decodeBase64 = (str) => {
|
|
|
56
32
|
}
|
|
57
33
|
catch (_a) { }
|
|
58
34
|
try {
|
|
59
|
-
const { Buffer } = require('buffer');
|
|
60
35
|
return Buffer.from(str, 'base64').toString();
|
|
61
36
|
}
|
|
62
37
|
catch (e) {
|
|
@@ -93,7 +68,6 @@ const arrayBufferToBase64 = async (buf) => {
|
|
|
93
68
|
return btoa(String.fromCharCode(...new Uint8Array(buf)));
|
|
94
69
|
}
|
|
95
70
|
try {
|
|
96
|
-
const { Buffer } = await Promise.resolve().then(() => __importStar(require('buffer')));
|
|
97
71
|
return Buffer.from(String.fromCharCode(...new Uint8Array(buf))).toString('base64');
|
|
98
72
|
}
|
|
99
73
|
catch (e) { }
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStatusText = void 0;
|
|
4
|
+
const getStatusText = (statusCode) => {
|
|
2
5
|
const text = statuses[statusCode];
|
|
3
6
|
return text;
|
|
4
7
|
};
|
|
8
|
+
exports.getStatusText = getStatusText;
|
|
5
9
|
const statuses = {
|
|
6
10
|
100: 'Continue',
|
|
7
11
|
101: 'Switching Protocols',
|
package/dist/utils/mime.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMimeType = void 0;
|
|
4
|
+
const getMimeType = (filename) => {
|
|
2
5
|
const regexp = /\.([a-zA-Z0-9]+?)$/;
|
|
3
6
|
const match = filename.match(regexp);
|
|
4
7
|
if (!match)
|
|
@@ -9,6 +12,7 @@ export const getMimeType = (filename) => {
|
|
|
9
12
|
}
|
|
10
13
|
return mimeType;
|
|
11
14
|
};
|
|
15
|
+
exports.getMimeType = getMimeType;
|
|
12
16
|
const mimes = {
|
|
13
17
|
aac: 'audio/aac',
|
|
14
18
|
abw: 'application/x-abiword',
|
package/dist/utils/url.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergePath = exports.isAbsoluteURL = exports.getPathFromURL = exports.getPattern = exports.splitPath = void 0;
|
|
1
4
|
const URL_REGEXP = /^https?:\/\/[a-zA-Z0-9\-\.:]+(\/?[^?#]*)/;
|
|
2
|
-
|
|
5
|
+
const splitPath = (path) => {
|
|
3
6
|
const paths = path.split(/\//); // faster than path.split('/')
|
|
4
7
|
if (paths[0] === '') {
|
|
5
8
|
paths.shift();
|
|
6
9
|
}
|
|
7
10
|
return paths;
|
|
8
11
|
};
|
|
12
|
+
exports.splitPath = splitPath;
|
|
9
13
|
const patternCache = {};
|
|
10
|
-
|
|
14
|
+
const getPattern = (label) => {
|
|
11
15
|
// * => wildcard
|
|
12
16
|
// :id{[0-9]+} => ([0-9]+)
|
|
13
17
|
// :id => (.+)
|
|
@@ -29,7 +33,8 @@ export const getPattern = (label) => {
|
|
|
29
33
|
}
|
|
30
34
|
return null;
|
|
31
35
|
};
|
|
32
|
-
|
|
36
|
+
exports.getPattern = getPattern;
|
|
37
|
+
const getPathFromURL = (url, params = { strict: true }) => {
|
|
33
38
|
// if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same
|
|
34
39
|
// default is true
|
|
35
40
|
if (params.strict === false && url.endsWith('/')) {
|
|
@@ -41,14 +46,16 @@ export const getPathFromURL = (url, params = { strict: true }) => {
|
|
|
41
46
|
}
|
|
42
47
|
return '';
|
|
43
48
|
};
|
|
44
|
-
|
|
49
|
+
exports.getPathFromURL = getPathFromURL;
|
|
50
|
+
const isAbsoluteURL = (url) => {
|
|
45
51
|
const match = url.match(URL_REGEXP);
|
|
46
52
|
if (match) {
|
|
47
53
|
return true;
|
|
48
54
|
}
|
|
49
55
|
return false;
|
|
50
56
|
};
|
|
51
|
-
|
|
57
|
+
exports.isAbsoluteURL = isAbsoluteURL;
|
|
58
|
+
const mergePath = (...paths) => {
|
|
52
59
|
let p = '';
|
|
53
60
|
let endsWithSlash = false;
|
|
54
61
|
for (let path of paths) {
|
|
@@ -75,3 +82,4 @@ export const mergePath = (...paths) => {
|
|
|
75
82
|
}
|
|
76
83
|
return p;
|
|
77
84
|
};
|
|
85
|
+
exports.mergePath = mergePath;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Ultrafast web framework for Cloudflare Workers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"test": "jest",
|
|
12
12
|
"lint": "eslint --ext js,ts src .eslintrc.js",
|
|
13
13
|
"lint:fix": "eslint --ext js,ts src .eslintrc.js --fix",
|
|
14
|
-
"build": "rimraf dist && tsc --project tsconfig.build.json && tsc --project tsconfig.build.
|
|
14
|
+
"build": "rimraf dist && tsc --project tsconfig.build.esm.json && tsc --project tsconfig.build.json",
|
|
15
15
|
"watch": "tsc --project tsconfig.build.json -w",
|
|
16
16
|
"prepublishOnly": "yarn build"
|
|
17
17
|
},
|