barejs 0.1.55 → 0.1.57
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 +13 -13
- package/dist/index.js +216 -132
- package/dist/types/core/bare.d.ts +2 -2
- package/dist/types/core/context.d.ts +5 -0
- package/dist/types/core/radix.d.ts +3 -5
- package/dist/types/core/router.d.ts +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/security/idempotency.d.ts +6 -1
- package/dist/types/security/store.d.ts +4 -1
- package/dist/types/utils/openapi.d.ts +1 -0
- package/llms.txt +3 -3
- package/package.json +2 -2
- package/src/client/index.ts +41 -13
- package/src/core/bare.ts +316 -95
- package/src/core/config.ts +3 -2
- package/src/core/context.ts +232 -54
- package/src/core/loader.ts +13 -3
- package/src/core/radix.ts +197 -110
- package/src/core/router.ts +39 -12
- package/src/index.ts +2 -2
- package/src/security/auth.ts +98 -9
- package/src/security/cors.ts +59 -9
- package/src/security/csrf.ts +6 -5
- package/src/security/hash.ts +9 -3
- package/src/security/idempotency.ts +169 -52
- package/src/security/rateLimit.ts +15 -5
- package/src/security/resilience.ts +34 -7
- package/src/security/store.ts +52 -8
- package/src/security/timeout.ts +5 -3
- package/src/security/validators.ts +574 -65
- package/src/utils/compress.ts +18 -13
- package/src/utils/openapi.ts +46 -8
- package/src/utils/static.ts +33 -7
package/src/core/radix.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { Context } from "./context";
|
|
2
2
|
|
|
3
3
|
export class RadixNode {
|
|
4
|
-
public children: Record<string, RadixNode> =
|
|
4
|
+
public children: Record<string, RadixNode> = Object.create(null);
|
|
5
5
|
public staticKeys: string[] = [];
|
|
6
6
|
public paramNode: RadixNode | null = null;
|
|
7
7
|
public paramName: string | null = null;
|
|
8
8
|
public wildcardNode: RadixNode | null = null;
|
|
9
|
-
public handlers: Record<string, Function> =
|
|
9
|
+
public handlers: Record<string, Function> = Object.create(null);
|
|
10
|
+
public handlerParams: Record<string, string[]> = Object.create(null);
|
|
10
11
|
|
|
11
12
|
public insert(path: string, method: string, handler: Function) {
|
|
12
13
|
let node: RadixNode = this;
|
|
13
14
|
const parts = path.split('/').filter(Boolean);
|
|
15
|
+
const paramNames: string[] = [];
|
|
14
16
|
for (const part of parts) {
|
|
15
17
|
if (part === '*') {
|
|
16
18
|
if (!node.wildcardNode) node.wildcardNode = new RadixNode();
|
|
@@ -18,7 +20,9 @@ export class RadixNode {
|
|
|
18
20
|
break;
|
|
19
21
|
} else if (part.startsWith(':')) {
|
|
20
22
|
if (!node.paramNode) node.paramNode = new RadixNode();
|
|
21
|
-
|
|
23
|
+
const pName = part.slice(1);
|
|
24
|
+
if (!node.paramName) node.paramName = pName;
|
|
25
|
+
paramNames.push(pName);
|
|
22
26
|
node = node.paramNode;
|
|
23
27
|
} else {
|
|
24
28
|
if (!node.children[part]) {
|
|
@@ -29,33 +33,55 @@ export class RadixNode {
|
|
|
29
33
|
}
|
|
30
34
|
}
|
|
31
35
|
node.handlers[method] = handler;
|
|
36
|
+
node.handlerParams[method] = paramNames;
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
public lookup(method: string, url: string, start: number, ctx: Context) {
|
|
35
|
-
let node: RadixNode = this;
|
|
36
40
|
let i = start === -1 ? 0 : start;
|
|
37
|
-
const
|
|
41
|
+
const q = url.indexOf('?', i);
|
|
42
|
+
const len = q === -1 ? url.length : q;
|
|
38
43
|
if (url[i] === '/') i++;
|
|
39
44
|
|
|
40
|
-
|
|
45
|
+
const applyParams = (names: string[], values: string[], wildcardVal?: string) => {
|
|
46
|
+
const hasProto = names.includes('__proto__');
|
|
47
|
+
const out: Record<string, string> = hasProto ? Object.create(null) : {};
|
|
48
|
+
for (let p = 0; p < names.length; p++) {
|
|
49
|
+
out[names[p]!] = values[p]!;
|
|
50
|
+
}
|
|
51
|
+
if (wildcardVal !== undefined) out['*'] = wildcardVal;
|
|
52
|
+
ctx.params = out;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const matchNode = (node: RadixNode, pos: number, pVals: string[]): Function | null => {
|
|
56
|
+
if (pos >= len) {
|
|
57
|
+
const h = node.handlers[method];
|
|
58
|
+
if (h) {
|
|
59
|
+
applyParams(node.handlerParams[method] || [], pVals);
|
|
60
|
+
return h;
|
|
61
|
+
}
|
|
62
|
+
if (node.wildcardNode && node.wildcardNode.handlers[method]) {
|
|
63
|
+
applyParams(node.wildcardNode.handlerParams[method] || [], pVals, '');
|
|
64
|
+
return node.wildcardNode.handlers[method]!;
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
41
68
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const segmentLen = j - i;
|
|
46
|
-
let found = false;
|
|
69
|
+
let j = url.indexOf('/', pos);
|
|
70
|
+
if (j === -1 || j > len) j = len;
|
|
71
|
+
const segmentLen = j - pos;
|
|
47
72
|
|
|
48
73
|
const keys = node.staticKeys;
|
|
49
74
|
const kLen = keys.length;
|
|
75
|
+
let matchedChild: RadixNode | null = null;
|
|
50
76
|
|
|
51
77
|
if (kLen === 1) {
|
|
52
78
|
const key = keys[0]!;
|
|
53
79
|
if (key.length === segmentLen) {
|
|
54
80
|
let match = true;
|
|
55
81
|
for (let m = 0; m < segmentLen; m++) {
|
|
56
|
-
if (key[m] !== url[
|
|
82
|
+
if (key[m] !== url[pos + m]) { match = false; break; }
|
|
57
83
|
}
|
|
58
|
-
if (match)
|
|
84
|
+
if (match) matchedChild = node.children[key]!;
|
|
59
85
|
}
|
|
60
86
|
} else {
|
|
61
87
|
for (let k = 0; k < kLen; k++) {
|
|
@@ -63,25 +89,48 @@ export class RadixNode {
|
|
|
63
89
|
if (key.length === segmentLen) {
|
|
64
90
|
let match = true;
|
|
65
91
|
for (let m = 0; m < segmentLen; m++) {
|
|
66
|
-
if (key[m] !== url[
|
|
92
|
+
if (key[m] !== url[pos + m]) { match = false; break; }
|
|
67
93
|
}
|
|
68
|
-
if (match) {
|
|
94
|
+
if (match) { matchedChild = node.children[key]!; break; }
|
|
69
95
|
}
|
|
70
96
|
}
|
|
71
97
|
}
|
|
72
98
|
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
99
|
+
if (matchedChild) {
|
|
100
|
+
const res = matchNode(matchedChild, j + 1, pVals);
|
|
101
|
+
if (res) return res;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (node.paramNode && segmentLen > 0) {
|
|
105
|
+
pVals.push(url.slice(pos, j));
|
|
106
|
+
const res = matchNode(node.paramNode, j + 1, pVals);
|
|
107
|
+
if (res) return res;
|
|
108
|
+
pVals.pop();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (node.wildcardNode && node.wildcardNode.handlers[method]) {
|
|
112
|
+
applyParams(node.wildcardNode.handlerParams[method] || [], pVals, url.slice(pos, len));
|
|
113
|
+
return node.wildcardNode.handlers[method]!;
|
|
81
114
|
}
|
|
82
|
-
|
|
115
|
+
|
|
116
|
+
return null;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
ctx.params = {};
|
|
120
|
+
return matchNode(this, i, []);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private emitParamsAssign(indent: string, names: string[], vars: string[]): string {
|
|
124
|
+
if (names.length === 0) return '';
|
|
125
|
+
if (names.includes('__proto__')) {
|
|
126
|
+
let s = `${indent}{ const _p = Object.create(null);\n`;
|
|
127
|
+
for (let i = 0; i < names.length; i++) {
|
|
128
|
+
s += `${indent}_p[${JSON.stringify(names[i])}] = ${vars[i]};\n`;
|
|
129
|
+
}
|
|
130
|
+
return s + `${indent}ctx.params = _p; }\n`;
|
|
83
131
|
}
|
|
84
|
-
|
|
132
|
+
const props = names.map((n, i) => `${JSON.stringify(n)}: ${vars[i]}`).join(', ');
|
|
133
|
+
return `${indent}ctx.params = { ${props} };\n`;
|
|
85
134
|
}
|
|
86
135
|
|
|
87
136
|
public jitCompile(
|
|
@@ -89,112 +138,152 @@ export class RadixNode {
|
|
|
89
138
|
level = 0,
|
|
90
139
|
pathPrefix = 'url',
|
|
91
140
|
idxPrefix = 's',
|
|
92
|
-
|
|
141
|
+
paramVars: string[] = [],
|
|
142
|
+
emitCall: (hName: string) => string = (h) => `return ${h}(ctx);`
|
|
93
143
|
): string {
|
|
94
144
|
let code = '';
|
|
95
145
|
const indent = ' '.repeat(level);
|
|
96
146
|
|
|
97
|
-
|
|
98
147
|
if (Object.keys(this.handlers).length > 0) {
|
|
99
148
|
code += `${indent}if (${idxPrefix} >= urlLen) {\n`;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (paramAccumulator.length > 0) {
|
|
103
|
-
const props = paramAccumulator.map((p: any) => `${JSON.stringify(p.name)}: ${p.varName}`).join(', ');
|
|
104
|
-
code += `${indent} ctx.params = { ${props} };\n`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
149
|
code += `${indent} switch(method) {\n`;
|
|
108
150
|
for (const [m, h] of Object.entries(this.handlers)) {
|
|
109
151
|
const hName = register(h as Function);
|
|
110
|
-
|
|
152
|
+
const pNames = this.handlerParams[m] || [];
|
|
153
|
+
const pAssign = pNames.length > 0 ? this.emitParamsAssign('', pNames, paramVars).trim() + ' ' : '';
|
|
154
|
+
code += `${indent} case ${JSON.stringify(m)}: ${pAssign}${emitCall(hName)}\n`;
|
|
111
155
|
}
|
|
112
156
|
code += `${indent} }\n`;
|
|
113
157
|
code += `${indent}}\n`;
|
|
114
158
|
}
|
|
115
159
|
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const kLen = key.length;
|
|
121
|
-
const nextIdxVar = `nextIdxL${level}`;
|
|
160
|
+
const hasChildren = this.staticKeys.length > 0 || this.paramNode !== null;
|
|
161
|
+
if (hasChildren) {
|
|
162
|
+
const innerIndent = indent + ' ';
|
|
163
|
+
code += `${indent}if (${idxPrefix} < urlLen) {\n`;
|
|
122
164
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const child = this.children[key]!;
|
|
128
|
-
const newIdxVar = `newIdxL${level}`;
|
|
129
|
-
code += `${indent} const ${newIdxVar} = ${nextIdxVar} === urlLen ? urlLen : (${nextIdxVar} + 1);\n`;
|
|
130
|
-
|
|
131
|
-
code += child.jitCompile(register, level + 1, pathPrefix, newIdxVar, paramAccumulator);
|
|
132
|
-
|
|
133
|
-
code += `${indent} }\n`;
|
|
134
|
-
code += `${indent}}\n`;
|
|
135
|
-
|
|
136
|
-
} else if (this.staticKeys.length > 4) {
|
|
137
|
-
code += `${indent}let slash${level} = ${pathPrefix}.indexOf('/', ${idxPrefix});\n`;
|
|
138
|
-
code += `${indent}if (slash${level} === -1 || slash${level} > urlLen) slash${level} = urlLen;\n`;
|
|
139
|
-
code += `${indent}switch (${pathPrefix}.slice(${idxPrefix}, slash${level})) {\n`;
|
|
140
|
-
for (const key of this.staticKeys) {
|
|
141
|
-
code += `${indent} case ${JSON.stringify(key)}: {\n`;
|
|
165
|
+
if (this.staticKeys.length > 0) {
|
|
166
|
+
if (this.staticKeys.length === 1) {
|
|
167
|
+
const key = this.staticKeys[0]!;
|
|
142
168
|
const child = this.children[key]!;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
169
|
+
const kLen = key.length;
|
|
170
|
+
const endCheck = `${idxPrefix} + ${kLen} === urlLen || ${pathPrefix}.charCodeAt(${idxPrefix} + ${kLen}) === 47`;
|
|
171
|
+
if (kLen === 1) {
|
|
172
|
+
code += `${innerIndent}if (${pathPrefix}.charCodeAt(${idxPrefix}) === ${key.charCodeAt(0)} && (${endCheck})) {\n`;
|
|
173
|
+
} else {
|
|
174
|
+
code += `${innerIndent}if (${pathPrefix}.startsWith(${JSON.stringify(key)}, ${idxPrefix}) && (${endCheck})) {\n`;
|
|
175
|
+
}
|
|
176
|
+
code += child.jitCompile(register, level + 1, pathPrefix, `${idxPrefix} + ${kLen + 1}`, paramVars, emitCall);
|
|
177
|
+
code += `${innerIndent}}\n`;
|
|
178
|
+
} else {
|
|
179
|
+
code += `${innerIndent}let slash${level} = ${pathPrefix}.indexOf('/', ${idxPrefix});\n`;
|
|
180
|
+
code += `${innerIndent}if (slash${level} === -1 || slash${level} > urlLen) slash${level} = urlLen;\n`;
|
|
181
|
+
code += `${innerIndent}const len${level} = slash${level} - (${idxPrefix});\n`;
|
|
182
|
+
|
|
183
|
+
const byLen = new Map<number, string[]>();
|
|
184
|
+
for (const key of this.staticKeys) {
|
|
185
|
+
let list = byLen.get(key.length);
|
|
186
|
+
if (!list) {
|
|
187
|
+
list = [];
|
|
188
|
+
byLen.set(key.length, list);
|
|
189
|
+
}
|
|
190
|
+
list.push(key);
|
|
159
191
|
}
|
|
160
|
-
list.push(key);
|
|
161
|
-
}
|
|
162
192
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
193
|
+
if (this.staticKeys.length > 8) {
|
|
194
|
+
const byBody = new Map<string, string[]>();
|
|
195
|
+
for (const key of this.staticKeys) {
|
|
196
|
+
const child = this.children[key]!;
|
|
197
|
+
let hIdx = 0;
|
|
198
|
+
const tmpl = child.jitCompile(() => `__H${hIdx++}__`, level + 1, pathPrefix, `slash${level} + 1`, paramVars, emitCall);
|
|
199
|
+
let list = byBody.get(tmpl);
|
|
200
|
+
if (!list) {
|
|
201
|
+
list = [];
|
|
202
|
+
byBody.set(tmpl, list);
|
|
203
|
+
}
|
|
204
|
+
list.push(key);
|
|
205
|
+
}
|
|
206
|
+
code += `${innerIndent}const seg${level} = ${pathPrefix}.slice(${idxPrefix}, slash${level});\n`;
|
|
207
|
+
code += `${innerIndent}switch(seg${level}) {\n`;
|
|
208
|
+
for (const [tmpl, groupKeys] of byBody) {
|
|
209
|
+
if (groupKeys.length === 1) {
|
|
210
|
+
const key = groupKeys[0]!;
|
|
211
|
+
const child = this.children[key]!;
|
|
212
|
+
code += `${innerIndent} case ${JSON.stringify(key)}:\n`;
|
|
213
|
+
code += child.jitCompile(register, level + 1, pathPrefix, `slash${level} + 1`, paramVars, emitCall);
|
|
214
|
+
code += `${innerIndent} break;\n`;
|
|
215
|
+
} else {
|
|
216
|
+
const fnLists: string[][] = [];
|
|
217
|
+
for (const key of groupKeys) {
|
|
218
|
+
const child = this.children[key]!;
|
|
219
|
+
const fns: string[] = [];
|
|
220
|
+
child.jitCompile((h) => { const n = register(h); fns.push(n); return n; }, level + 1, pathPrefix, `slash${level} + 1`, paramVars, emitCall);
|
|
221
|
+
fnLists.push(fns);
|
|
222
|
+
}
|
|
223
|
+
const slotCount = fnLists[0]!.length;
|
|
224
|
+
const slotVars = Array.from({ length: slotCount }, (_, idx) => `_rh${level}_${idx}`);
|
|
225
|
+
for (let gi = 0; gi < groupKeys.length; gi++) {
|
|
226
|
+
code += `${innerIndent} case ${JSON.stringify(groupKeys[gi]!)}:\n`;
|
|
227
|
+
}
|
|
228
|
+
code += `${innerIndent} {\n`;
|
|
229
|
+
if (slotCount > 0) {
|
|
230
|
+
code += `${innerIndent} let ${slotVars.join(', ')};\n`;
|
|
231
|
+
code += `${innerIndent} switch(seg${level}) {\n`;
|
|
232
|
+
for (let gi = 0; gi < groupKeys.length; gi++) {
|
|
233
|
+
const assigns = slotVars.map((sv, si) => `${sv} = ${fnLists[gi]![si]}`).join('; ');
|
|
234
|
+
code += `${innerIndent} case ${JSON.stringify(groupKeys[gi]!)}: ${assigns}; break;\n`;
|
|
235
|
+
}
|
|
236
|
+
code += `${innerIndent} }\n`;
|
|
237
|
+
}
|
|
238
|
+
let bodyCode = tmpl;
|
|
239
|
+
for (let si = 0; si < slotCount; si++) {
|
|
240
|
+
bodyCode = bodyCode.replaceAll(`__H${si}__`, slotVars[si]!);
|
|
241
|
+
}
|
|
242
|
+
code += bodyCode;
|
|
243
|
+
code += `${innerIndent} break;\n`;
|
|
244
|
+
code += `${innerIndent} }\n`;
|
|
245
|
+
}
|
|
171
246
|
}
|
|
247
|
+
code += `${innerIndent}}\n`;
|
|
248
|
+
} else {
|
|
249
|
+
code += `${innerIndent}switch(len${level}) {\n`;
|
|
250
|
+
for (const [kLen, keysForLen] of byLen) {
|
|
251
|
+
code += `${innerIndent} case ${kLen}:\n`;
|
|
252
|
+
for (const key of keysForLen) {
|
|
253
|
+
if (kLen === 1) {
|
|
254
|
+
code += `${innerIndent} if (${pathPrefix}.charCodeAt(${idxPrefix}) === ${key.charCodeAt(0)}) {\n`;
|
|
255
|
+
} else {
|
|
256
|
+
code += `${innerIndent} if (${pathPrefix}.startsWith(${JSON.stringify(key)}, ${idxPrefix})) {\n`;
|
|
257
|
+
}
|
|
172
258
|
|
|
173
|
-
|
|
174
|
-
|
|
259
|
+
const child = this.children[key]!;
|
|
260
|
+
code += child.jitCompile(register, level + 1, pathPrefix, `slash${level} + 1`, paramVars, emitCall);
|
|
175
261
|
|
|
176
|
-
|
|
262
|
+
code += `${innerIndent} }\n`;
|
|
263
|
+
}
|
|
264
|
+
code += `${innerIndent} break;\n`;
|
|
265
|
+
}
|
|
266
|
+
code += `${innerIndent}}\n`;
|
|
177
267
|
}
|
|
178
|
-
code += `${indent} break;\n`;
|
|
179
268
|
}
|
|
180
|
-
code += `${indent}}\n`;
|
|
181
269
|
}
|
|
182
|
-
}
|
|
183
270
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
271
|
+
if (this.paramNode) {
|
|
272
|
+
const pNode = this.paramNode;
|
|
273
|
+
if (this.staticKeys.length <= 1) {
|
|
274
|
+
code += `${innerIndent}let slash${level} = ${pathPrefix}.indexOf('/', ${idxPrefix});\n`;
|
|
275
|
+
code += `${innerIndent}if (slash${level} === -1 || slash${level} > urlLen) slash${level} = urlLen;\n`;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const pVar = `pL${level}`;
|
|
279
|
+
code += `${innerIndent}if (slash${level} > ${idxPrefix}) {\n`;
|
|
280
|
+
code += `${innerIndent} const ${pVar} = ${pathPrefix}.slice(${idxPrefix}, slash${level});\n`;
|
|
191
281
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
282
|
+
const newVars = [...paramVars, pVar];
|
|
283
|
+
code += pNode.jitCompile(register, level + 1, pathPrefix, `slash${level} + 1`, newVars, emitCall);
|
|
284
|
+
code += `${innerIndent}}\n`;
|
|
285
|
+
}
|
|
195
286
|
|
|
196
|
-
const newAccumulator = [...paramAccumulator, { name: this.paramName!, varName: pVar }];
|
|
197
|
-
code += pNode.jitCompile(register, level + 1, pathPrefix, `slash${level} + 1`, newAccumulator);
|
|
198
287
|
code += `${indent}}\n`;
|
|
199
288
|
}
|
|
200
289
|
|
|
@@ -202,19 +291,17 @@ export class RadixNode {
|
|
|
202
291
|
if (this.wildcardNode && Object.keys(this.wildcardNode.handlers).length > 0) {
|
|
203
292
|
const wVar = `wL${level}`;
|
|
204
293
|
code += `${indent}const ${wVar} = ${pathPrefix}.slice(${idxPrefix}, urlLen);\n`;
|
|
205
|
-
const wAcc = [...paramAccumulator, { name: '*', varName: wVar }];
|
|
206
|
-
const props = wAcc.map((p: any) => `${JSON.stringify(p.name)}: ${p.varName}`).join(', ');
|
|
207
|
-
code += `${indent}ctx.params = { ${props} };\n`;
|
|
208
294
|
code += `${indent}switch(method) {\n`;
|
|
209
295
|
for (const [m, h] of Object.entries(this.wildcardNode.handlers)) {
|
|
210
296
|
const hName = register(h as Function);
|
|
211
|
-
|
|
297
|
+
const pNames = [...(this.wildcardNode.handlerParams[m] || []), '*'];
|
|
298
|
+
const pVars = [...paramVars, wVar];
|
|
299
|
+
const pAssign = this.emitParamsAssign('', pNames, pVars).trim();
|
|
300
|
+
code += `${indent} case ${JSON.stringify(m)}: ${pAssign} ${emitCall(hName)}\n`;
|
|
212
301
|
}
|
|
213
302
|
code += `${indent}}\n`;
|
|
214
303
|
}
|
|
215
304
|
|
|
216
305
|
return code;
|
|
217
306
|
}
|
|
218
|
-
|
|
219
|
-
public id = Math.random().toString(36).slice(2);
|
|
220
307
|
}
|
package/src/core/router.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type Context,
|
|
3
3
|
type Middleware,
|
|
4
|
-
type Handler
|
|
5
|
-
type GroupCallback
|
|
4
|
+
type Handler
|
|
6
5
|
} from '../core/context';
|
|
7
6
|
import { schemaValidator, type RouteSchemas } from '../security/validators';
|
|
8
7
|
|
|
@@ -150,16 +149,44 @@ export class BareRouter<Routes extends RouteSchema = {}> {
|
|
|
150
149
|
|
|
151
150
|
public use<R2 extends RouteSchema = {}>(mw: BareRouter<R2>): BareRouter<Routes & R2>;
|
|
152
151
|
public use<P extends string, R2 extends RouteSchema = {}>(prefix: P, mw: BareRouter<R2>): BareRouter<Routes & PrefixRoutes<P, R2>>;
|
|
153
|
-
public use(
|
|
154
|
-
public use(prefixOrMw: any,
|
|
155
|
-
if (typeof prefixOrMw === 'string'
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
152
|
+
public use(...mws: any[]): BareRouter<Routes>;
|
|
153
|
+
public use(prefixOrMw: any, ...rest: any[]): any {
|
|
154
|
+
if (typeof prefixOrMw === 'string') {
|
|
155
|
+
const prefixMws: any[] = [];
|
|
156
|
+
const prefixRouters: any[] = [];
|
|
157
|
+
for (let i = 0; i < rest.length; i++) {
|
|
158
|
+
const item = rest[i];
|
|
159
|
+
if (item instanceof BareRouter || (item && item.routes)) {
|
|
160
|
+
prefixRouters.push(item);
|
|
161
|
+
} else if (typeof item === 'function') {
|
|
162
|
+
prefixMws.push(item);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (prefixRouters.length === 0) {
|
|
166
|
+
throw new TypeError(`BareRouter.use("${prefixOrMw}", ...) requires a BareRouter instance`);
|
|
167
|
+
}
|
|
168
|
+
for (let i = 0; i < prefixRouters.length; i++) {
|
|
169
|
+
const prevLen = this.routes.length;
|
|
170
|
+
this._mountRouter(prefixOrMw, prefixRouters[i]);
|
|
171
|
+
if (prefixMws.length > 0) {
|
|
172
|
+
for (let j = prevLen; j < this.routes.length; j++) {
|
|
173
|
+
this.routes[j].handlers = [...this.groupMiddleware, ...prefixMws, ...this.routes[j].handlers.slice(this.groupMiddleware.length)];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const all = [prefixOrMw, ...rest];
|
|
181
|
+
for (let i = 0; i < all.length; i++) {
|
|
182
|
+
const item = all[i];
|
|
183
|
+
if (item instanceof BareRouter || (item && item.routes)) {
|
|
184
|
+
this._mountRouter("", item);
|
|
185
|
+
} else if (item && typeof item.install === 'function') {
|
|
186
|
+
item.install(this);
|
|
187
|
+
} else if (typeof item === 'function') {
|
|
188
|
+
this.groupMiddleware.push(item);
|
|
189
|
+
}
|
|
163
190
|
}
|
|
164
191
|
return this;
|
|
165
192
|
}
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
// --- 1. Core Engine ---
|
|
9
|
-
export { BareJS, type WebSocketRouteHandler, type CronOptions } from './core/bare';
|
|
9
|
+
export { BareJS, type WebSocketRouteHandler, type CronOptions, type NotFoundResponse } from './core/bare';
|
|
10
10
|
export { BareRouter, type InferSchema, type PrefixRoutes } from './core/router';
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -61,7 +61,7 @@ export {
|
|
|
61
61
|
// --- 5. Middlewares & Utilities ---
|
|
62
62
|
export { logger, type LoggerOptions, type LogEntry } from './utils/logger';
|
|
63
63
|
export { healthcheck, type HealthcheckOptions, type HealthCheckFn } from './utils/health';
|
|
64
|
-
export { cors } from './security/cors';
|
|
64
|
+
export { cors, type CorsOptions } from './security/cors';
|
|
65
65
|
export { csrf, type CsrfOptions } from './security/csrf';
|
|
66
66
|
export { timeout, type TimeoutOptions } from './security/timeout';
|
|
67
67
|
export { rateLimit, type RateLimitOptions } from './security/rateLimit';
|