@kevisual/router 0.2.4 → 0.2.6
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 -12
- package/dist/app.js +131 -119
- package/dist/commander.d.ts +31 -14
- package/dist/commander.js +134 -122
- package/dist/opencode.d.ts +22 -12
- package/dist/opencode.js +84 -84
- package/dist/router-browser.d.ts +24 -33
- package/dist/router-browser.js +93 -93
- package/dist/router-define.d.ts +20 -13
- package/dist/router-simple.d.ts +3 -2
- package/dist/router-simple.js +193 -177
- package/dist/router.d.ts +32 -35
- package/dist/router.js +127 -116
- package/dist/ws.d.ts +22 -12
- package/dist/ws.js +34 -23
- package/package.json +7 -7
- package/src/auto/listen/server-time.ts +1 -1
- package/src/browser.ts +0 -2
- package/src/commander.ts +49 -37
- package/src/index.ts +0 -2
- package/src/route.ts +12 -11
- package/src/router-simple.ts +1 -1
- package/src/test/api.d.ts +72 -0
- package/src/test/api.js +1 -0
- package/src/test/cli.ts +0 -0
- package/src/test/mini.ts +11 -3
- package/src/test/route-ts.ts +1 -1
- package/src/test/run-schema.ts +2 -1
- package/src/test/static.ts +1 -1
- package/src/test/ws.ts +1 -0
- package/src/utils/listen-process.ts +1 -1
- package/src/test/define.ts +0 -14
- package/src/test/listen-ip.ts +0 -18
- package/src/test/schema.ts +0 -14
package/dist/router-define.d.ts
CHANGED
|
@@ -168,7 +168,7 @@ declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleO
|
|
|
168
168
|
description?: string;
|
|
169
169
|
metadata?: M;
|
|
170
170
|
middleware?: RouteMiddleware[];
|
|
171
|
-
type?: string
|
|
171
|
+
type?: string;
|
|
172
172
|
/**
|
|
173
173
|
* 是否开启debug,开启后会打印错误信息
|
|
174
174
|
*/
|
|
@@ -244,6 +244,7 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
244
244
|
path: string;
|
|
245
245
|
key?: string;
|
|
246
246
|
payload?: any;
|
|
247
|
+
args?: any;
|
|
247
248
|
}, ctx?: RouteContext<T> & {
|
|
248
249
|
[key: string]: any;
|
|
249
250
|
}): Promise<RouteContext<T, {}, {
|
|
@@ -260,13 +261,14 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
260
261
|
path?: string;
|
|
261
262
|
key?: string;
|
|
262
263
|
payload?: any;
|
|
264
|
+
args?: any;
|
|
263
265
|
}, ctx?: RouteContext<T> & {
|
|
264
266
|
[key: string]: any;
|
|
265
267
|
}): Promise<RouteContext<T, {}, {
|
|
266
268
|
[key: string]: any;
|
|
267
269
|
}> | {
|
|
268
270
|
code: number;
|
|
269
|
-
body:
|
|
271
|
+
body: any;
|
|
270
272
|
message: string;
|
|
271
273
|
}>;
|
|
272
274
|
/**
|
|
@@ -281,12 +283,13 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
281
283
|
path: string;
|
|
282
284
|
key?: string;
|
|
283
285
|
payload?: any;
|
|
286
|
+
args?: any;
|
|
284
287
|
}, ctx?: RouteContext & {
|
|
285
288
|
[key: string]: any;
|
|
286
289
|
}): Promise<{
|
|
287
|
-
code: number
|
|
288
|
-
data:
|
|
289
|
-
message: string
|
|
290
|
+
code: number;
|
|
291
|
+
data: any;
|
|
292
|
+
message: string;
|
|
290
293
|
}>;
|
|
291
294
|
/**
|
|
292
295
|
* Router Run获取数据
|
|
@@ -299,12 +302,13 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
299
302
|
path?: string;
|
|
300
303
|
key?: string;
|
|
301
304
|
payload?: any;
|
|
305
|
+
args?: any;
|
|
302
306
|
}, ctx?: RouteContext<T> & {
|
|
303
307
|
[key: string]: any;
|
|
304
308
|
}): Promise<{
|
|
305
|
-
code: number
|
|
306
|
-
data:
|
|
307
|
-
message: string
|
|
309
|
+
code: number;
|
|
310
|
+
data: any;
|
|
311
|
+
message: string;
|
|
308
312
|
}>;
|
|
309
313
|
/**
|
|
310
314
|
* 设置上下文
|
|
@@ -339,12 +343,12 @@ declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements
|
|
|
339
343
|
importRoutes(routes: Route[]): void;
|
|
340
344
|
importRouter(router: QueryRouter): void;
|
|
341
345
|
throw(...args: any[]): void;
|
|
342
|
-
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1
|
|
346
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
343
347
|
findRoute(opts?: {
|
|
344
348
|
path?: string;
|
|
345
349
|
key?: string;
|
|
346
350
|
rid?: string;
|
|
347
|
-
}): Route<SimpleObject$1, SimpleObject$1
|
|
351
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
348
352
|
createRouteList(opts?: {
|
|
349
353
|
force?: boolean;
|
|
350
354
|
filter?: (route: Route) => boolean;
|
|
@@ -438,6 +442,7 @@ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> exten
|
|
|
438
442
|
path?: string;
|
|
439
443
|
key?: string;
|
|
440
444
|
payload?: any;
|
|
445
|
+
args?: any;
|
|
441
446
|
token?: string;
|
|
442
447
|
data?: any;
|
|
443
448
|
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
@@ -453,7 +458,9 @@ declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> exten
|
|
|
453
458
|
* 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
|
|
454
459
|
* @param fun 认证函数,接收 RouteContext 和认证类型
|
|
455
460
|
*/
|
|
456
|
-
createAuth(fun
|
|
461
|
+
createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: {
|
|
462
|
+
overwrite?: boolean;
|
|
463
|
+
}): Promise<void>;
|
|
457
464
|
}
|
|
458
465
|
/** JSON Schema 基本类型映射到 TypeScript 类型 */
|
|
459
466
|
type JsonSchemaTypeToTS<T> = T extends {
|
|
@@ -508,8 +515,8 @@ declare class Chain {
|
|
|
508
515
|
object: RouteOpts;
|
|
509
516
|
app?: QueryRouterServer;
|
|
510
517
|
constructor(object: RouteOpts, opts?: ChainOptions);
|
|
511
|
-
get key(): string
|
|
512
|
-
get path(): string
|
|
518
|
+
get key(): string;
|
|
519
|
+
get path(): string;
|
|
513
520
|
setDescription(desc: string): this;
|
|
514
521
|
setMeta(metadata: {
|
|
515
522
|
[key: string]: any;
|
package/dist/router-simple.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as node_querystring from 'node:querystring';
|
|
1
2
|
import { Key } from 'path-to-regexp';
|
|
2
3
|
import { IncomingMessage, ServerResponse, Server } from 'node:http';
|
|
3
4
|
import { ListenOptions } from 'node:net';
|
|
@@ -24,7 +25,7 @@ declare class SimpleRouter {
|
|
|
24
25
|
exclude?: string[];
|
|
25
26
|
});
|
|
26
27
|
getBody(req: Req): Promise<Record<string, any>>;
|
|
27
|
-
getSearch(req: Req):
|
|
28
|
+
getSearch(req: Req): node_querystring.ParsedUrlQuery;
|
|
28
29
|
parseSearchValue: (value?: string, opts?: {
|
|
29
30
|
decode?: boolean;
|
|
30
31
|
}) => any;
|
|
@@ -102,7 +103,7 @@ declare class HttpChain {
|
|
|
102
103
|
listenCallBack?: () => void;
|
|
103
104
|
}): () => void;
|
|
104
105
|
getString(value: string | SimpleObject): string;
|
|
105
|
-
sse(value: string | SimpleObject): this
|
|
106
|
+
sse(value: string | SimpleObject): this;
|
|
106
107
|
close(): this;
|
|
107
108
|
}
|
|
108
109
|
|
package/dist/router-simple.js
CHANGED
|
@@ -30,7 +30,7 @@ var __toESM = (mod, isNodeMode, target) => {
|
|
|
30
30
|
};
|
|
31
31
|
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
32
32
|
|
|
33
|
-
// node_modules/path-to-regexp/dist/index.js
|
|
33
|
+
// ../../node_modules/.pnpm/path-to-regexp@8.4.2/node_modules/path-to-regexp/dist/index.js
|
|
34
34
|
var require_dist = __commonJS((exports) => {
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.PathError = exports.TokenData = undefined;
|
|
@@ -43,17 +43,7 @@ var require_dist = __commonJS((exports) => {
|
|
|
43
43
|
var NOOP_VALUE = (value) => value;
|
|
44
44
|
var ID_START = /^[$_\p{ID_Start}]$/u;
|
|
45
45
|
var ID_CONTINUE = /^[$\u200c\u200d\p{ID_Continue}]$/u;
|
|
46
|
-
var
|
|
47
|
-
"{": "{",
|
|
48
|
-
"}": "}",
|
|
49
|
-
"(": "(",
|
|
50
|
-
")": ")",
|
|
51
|
-
"[": "[",
|
|
52
|
-
"]": "]",
|
|
53
|
-
"+": "+",
|
|
54
|
-
"?": "?",
|
|
55
|
-
"!": "!"
|
|
56
|
-
};
|
|
46
|
+
var ID = /^[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u;
|
|
57
47
|
function escapeText(str) {
|
|
58
48
|
return str.replace(/[{}()\[\]+?!:*\\]/g, "\\$&");
|
|
59
49
|
}
|
|
@@ -83,97 +73,90 @@ var require_dist = __commonJS((exports) => {
|
|
|
83
73
|
function parse(str, options = {}) {
|
|
84
74
|
const { encodePath = NOOP_VALUE } = options;
|
|
85
75
|
const chars = [...str];
|
|
86
|
-
const tokens = [];
|
|
87
76
|
let index = 0;
|
|
88
|
-
|
|
89
|
-
function name() {
|
|
90
|
-
let value = "";
|
|
91
|
-
if (ID_START.test(chars[index])) {
|
|
92
|
-
do {
|
|
93
|
-
value += chars[index++];
|
|
94
|
-
} while (ID_CONTINUE.test(chars[index]));
|
|
95
|
-
} else if (chars[index] === '"') {
|
|
96
|
-
let quoteStart = index;
|
|
97
|
-
while (index++ < chars.length) {
|
|
98
|
-
if (chars[index] === '"') {
|
|
99
|
-
index++;
|
|
100
|
-
quoteStart = 0;
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
if (chars[index] === "\\")
|
|
104
|
-
index++;
|
|
105
|
-
value += chars[index];
|
|
106
|
-
}
|
|
107
|
-
if (quoteStart) {
|
|
108
|
-
throw new PathError(`Unterminated quote at index ${quoteStart}`, str);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
if (!value) {
|
|
112
|
-
throw new PathError(`Missing parameter name at index ${index}`, str);
|
|
113
|
-
}
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
while (index < chars.length) {
|
|
117
|
-
const value = chars[index];
|
|
118
|
-
const type = SIMPLE_TOKENS[value];
|
|
119
|
-
if (type) {
|
|
120
|
-
tokens.push({ type, index: index++, value });
|
|
121
|
-
} else if (value === "\\") {
|
|
122
|
-
tokens.push({ type: "escape", index: index++, value: chars[index++] });
|
|
123
|
-
} else if (value === ":") {
|
|
124
|
-
tokens.push({ type: "param", index: index++, value: name() });
|
|
125
|
-
} else if (value === "*") {
|
|
126
|
-
tokens.push({ type: "wildcard", index: index++, value: name() });
|
|
127
|
-
} else {
|
|
128
|
-
tokens.push({ type: "char", index: index++, value });
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
tokens.push({ type: "end", index, value: "" });
|
|
132
|
-
function consumeUntil(endType) {
|
|
77
|
+
function consumeUntil(end) {
|
|
133
78
|
const output = [];
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
79
|
+
let path = "";
|
|
80
|
+
function writePath() {
|
|
81
|
+
if (!path)
|
|
82
|
+
return;
|
|
83
|
+
output.push({
|
|
84
|
+
type: "text",
|
|
85
|
+
value: encodePath(path)
|
|
86
|
+
});
|
|
87
|
+
path = "";
|
|
88
|
+
}
|
|
89
|
+
while (index < chars.length) {
|
|
90
|
+
const value = chars[index++];
|
|
91
|
+
if (value === end) {
|
|
92
|
+
writePath();
|
|
93
|
+
return output;
|
|
94
|
+
}
|
|
95
|
+
if (value === "\\") {
|
|
96
|
+
if (index === chars.length) {
|
|
97
|
+
throw new PathError(`Unexpected end after \\ at index ${index}`, str);
|
|
144
98
|
}
|
|
145
|
-
|
|
146
|
-
type: "text",
|
|
147
|
-
value: encodePath(path)
|
|
148
|
-
});
|
|
99
|
+
path += chars[index++];
|
|
149
100
|
continue;
|
|
150
101
|
}
|
|
151
|
-
if (
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
102
|
+
if (value === ":" || value === "*") {
|
|
103
|
+
const type = value === ":" ? "param" : "wildcard";
|
|
104
|
+
let name = "";
|
|
105
|
+
if (ID_START.test(chars[index])) {
|
|
106
|
+
do {
|
|
107
|
+
name += chars[index++];
|
|
108
|
+
} while (ID_CONTINUE.test(chars[index]));
|
|
109
|
+
} else if (chars[index] === '"') {
|
|
110
|
+
let quoteStart = index;
|
|
111
|
+
while (index < chars.length) {
|
|
112
|
+
if (chars[++index] === '"') {
|
|
113
|
+
index++;
|
|
114
|
+
quoteStart = 0;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
if (chars[index] === "\\")
|
|
118
|
+
index++;
|
|
119
|
+
name += chars[index];
|
|
120
|
+
}
|
|
121
|
+
if (quoteStart) {
|
|
122
|
+
throw new PathError(`Unterminated quote at index ${quoteStart}`, str);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!name) {
|
|
126
|
+
throw new PathError(`Missing parameter name at index ${index}`, str);
|
|
127
|
+
}
|
|
128
|
+
writePath();
|
|
129
|
+
output.push({ type, name });
|
|
156
130
|
continue;
|
|
157
131
|
}
|
|
158
|
-
if (
|
|
132
|
+
if (value === "{") {
|
|
133
|
+
writePath();
|
|
159
134
|
output.push({
|
|
160
135
|
type: "group",
|
|
161
136
|
tokens: consumeUntil("}")
|
|
162
137
|
});
|
|
163
138
|
continue;
|
|
164
139
|
}
|
|
165
|
-
|
|
140
|
+
if (value === "}" || value === "(" || value === ")" || value === "[" || value === "]" || value === "+" || value === "?" || value === "!") {
|
|
141
|
+
throw new PathError(`Unexpected ${value} at index ${index - 1}`, str);
|
|
142
|
+
}
|
|
143
|
+
path += value;
|
|
144
|
+
}
|
|
145
|
+
if (end) {
|
|
146
|
+
throw new PathError(`Unexpected end at index ${index}, expected ${end}`, str);
|
|
166
147
|
}
|
|
148
|
+
writePath();
|
|
167
149
|
return output;
|
|
168
150
|
}
|
|
169
|
-
return new TokenData(consumeUntil("
|
|
151
|
+
return new TokenData(consumeUntil(""), str);
|
|
170
152
|
}
|
|
171
153
|
function compile(path, options = {}) {
|
|
172
154
|
const { encode = encodeURIComponent, delimiter = DEFAULT_DELIMITER } = options;
|
|
173
155
|
const data = typeof path === "object" ? path : parse(path, options);
|
|
174
156
|
const fn = tokensToFunction(data.tokens, delimiter, encode);
|
|
175
157
|
return function path2(params = {}) {
|
|
176
|
-
const
|
|
158
|
+
const missing = [];
|
|
159
|
+
const path3 = fn(params, missing);
|
|
177
160
|
if (missing.length) {
|
|
178
161
|
throw new TypeError(`Missing parameters: ${missing.join(", ")}`);
|
|
179
162
|
}
|
|
@@ -182,55 +165,61 @@ var require_dist = __commonJS((exports) => {
|
|
|
182
165
|
}
|
|
183
166
|
function tokensToFunction(tokens, delimiter, encode) {
|
|
184
167
|
const encoders = tokens.map((token) => tokenToFunction(token, delimiter, encode));
|
|
185
|
-
return (data) => {
|
|
186
|
-
|
|
168
|
+
return (data, missing) => {
|
|
169
|
+
let result = "";
|
|
187
170
|
for (const encoder of encoders) {
|
|
188
|
-
|
|
189
|
-
result[0] += value;
|
|
190
|
-
result.push(...extras);
|
|
171
|
+
result += encoder(data, missing);
|
|
191
172
|
}
|
|
192
173
|
return result;
|
|
193
174
|
};
|
|
194
175
|
}
|
|
195
176
|
function tokenToFunction(token, delimiter, encode) {
|
|
196
177
|
if (token.type === "text")
|
|
197
|
-
return () =>
|
|
178
|
+
return () => token.value;
|
|
198
179
|
if (token.type === "group") {
|
|
199
180
|
const fn = tokensToFunction(token.tokens, delimiter, encode);
|
|
200
|
-
return (data) => {
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
181
|
+
return (data, missing) => {
|
|
182
|
+
const len = missing.length;
|
|
183
|
+
const value = fn(data, missing);
|
|
184
|
+
if (missing.length === len)
|
|
185
|
+
return value;
|
|
186
|
+
missing.length = len;
|
|
187
|
+
return "";
|
|
205
188
|
};
|
|
206
189
|
}
|
|
207
190
|
const encodeValue = encode || NOOP_VALUE;
|
|
208
191
|
if (token.type === "wildcard" && encode !== false) {
|
|
209
|
-
return (data) => {
|
|
192
|
+
return (data, missing) => {
|
|
210
193
|
const value = data[token.name];
|
|
211
|
-
if (value == null)
|
|
212
|
-
|
|
194
|
+
if (value == null) {
|
|
195
|
+
missing.push(token.name);
|
|
196
|
+
return "";
|
|
197
|
+
}
|
|
213
198
|
if (!Array.isArray(value) || value.length === 0) {
|
|
214
199
|
throw new TypeError(`Expected "${token.name}" to be a non-empty array`);
|
|
215
200
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
201
|
+
let result = "";
|
|
202
|
+
for (let i = 0;i < value.length; i++) {
|
|
203
|
+
if (typeof value[i] !== "string") {
|
|
204
|
+
throw new TypeError(`Expected "${token.name}/${i}" to be a string`);
|
|
205
|
+
}
|
|
206
|
+
if (i > 0)
|
|
207
|
+
result += delimiter;
|
|
208
|
+
result += encodeValue(value[i]);
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
224
211
|
};
|
|
225
212
|
}
|
|
226
|
-
return (data) => {
|
|
213
|
+
return (data, missing) => {
|
|
227
214
|
const value = data[token.name];
|
|
228
|
-
if (value == null)
|
|
229
|
-
|
|
215
|
+
if (value == null) {
|
|
216
|
+
missing.push(token.name);
|
|
217
|
+
return "";
|
|
218
|
+
}
|
|
230
219
|
if (typeof value !== "string") {
|
|
231
220
|
throw new TypeError(`Expected "${token.name}" to be a string`);
|
|
232
221
|
}
|
|
233
|
-
return
|
|
222
|
+
return encodeValue(value);
|
|
234
223
|
};
|
|
235
224
|
}
|
|
236
225
|
function match(path, options = {}) {
|
|
@@ -262,106 +251,134 @@ var require_dist = __commonJS((exports) => {
|
|
|
262
251
|
function pathToRegexp(path, options = {}) {
|
|
263
252
|
const { delimiter = DEFAULT_DELIMITER, end = true, sensitive = false, trailing = true } = options;
|
|
264
253
|
const keys = [];
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
254
|
+
let source = "";
|
|
255
|
+
let combinations = 0;
|
|
256
|
+
function process2(path2) {
|
|
257
|
+
if (Array.isArray(path2)) {
|
|
258
|
+
for (const p of path2)
|
|
259
|
+
process2(p);
|
|
260
|
+
return;
|
|
271
261
|
}
|
|
262
|
+
const data = typeof path2 === "object" ? path2 : parse(path2, options);
|
|
263
|
+
flatten(data.tokens, 0, [], (tokens) => {
|
|
264
|
+
if (combinations >= 256) {
|
|
265
|
+
throw new PathError("Too many path combinations", data.originalPath);
|
|
266
|
+
}
|
|
267
|
+
if (combinations > 0)
|
|
268
|
+
source += "|";
|
|
269
|
+
source += toRegExpSource(tokens, delimiter, keys, data.originalPath);
|
|
270
|
+
combinations++;
|
|
271
|
+
});
|
|
272
272
|
}
|
|
273
|
-
|
|
273
|
+
process2(path);
|
|
274
|
+
let pattern = `^(?:${source})`;
|
|
274
275
|
if (trailing)
|
|
275
|
-
pattern +=
|
|
276
|
-
pattern += end ? "$" :
|
|
277
|
-
|
|
278
|
-
return { regexp, keys };
|
|
279
|
-
}
|
|
280
|
-
function pathsToArray(paths, init) {
|
|
281
|
-
if (Array.isArray(paths)) {
|
|
282
|
-
for (const p of paths)
|
|
283
|
-
pathsToArray(p, init);
|
|
284
|
-
} else {
|
|
285
|
-
init.push(paths);
|
|
286
|
-
}
|
|
287
|
-
return init;
|
|
276
|
+
pattern += "(?:" + escape(delimiter) + "$)?";
|
|
277
|
+
pattern += end ? "$" : "(?=" + escape(delimiter) + "|$)";
|
|
278
|
+
return { regexp: new RegExp(pattern, sensitive ? "" : "i"), keys };
|
|
288
279
|
}
|
|
289
|
-
function
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
280
|
+
function flatten(tokens, index, result, callback) {
|
|
281
|
+
while (index < tokens.length) {
|
|
282
|
+
const token = tokens[index++];
|
|
283
|
+
if (token.type === "group") {
|
|
284
|
+
const len = result.length;
|
|
285
|
+
flatten(token.tokens, 0, result, (seq) => flatten(tokens, index, seq, callback));
|
|
286
|
+
result.length = len;
|
|
287
|
+
continue;
|
|
297
288
|
}
|
|
298
|
-
|
|
299
|
-
init.push(token);
|
|
289
|
+
result.push(token);
|
|
300
290
|
}
|
|
301
|
-
|
|
291
|
+
callback(result);
|
|
302
292
|
}
|
|
303
293
|
function toRegExpSource(tokens, delimiter, keys, originalPath) {
|
|
304
294
|
let result = "";
|
|
305
295
|
let backtrack = "";
|
|
306
|
-
let
|
|
307
|
-
|
|
296
|
+
let wildcardBacktrack = "";
|
|
297
|
+
let prevCaptureType = 0;
|
|
298
|
+
let hasSegmentCapture = 0;
|
|
299
|
+
let index = 0;
|
|
300
|
+
function hasInSegment(index2, type) {
|
|
301
|
+
while (index2 < tokens.length) {
|
|
302
|
+
const token = tokens[index2++];
|
|
303
|
+
if (token.type === type)
|
|
304
|
+
return true;
|
|
305
|
+
if (token.type === "text") {
|
|
306
|
+
if (token.value.includes(delimiter))
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
function peekText(index2) {
|
|
313
|
+
let result2 = "";
|
|
314
|
+
while (index2 < tokens.length) {
|
|
315
|
+
const token = tokens[index2++];
|
|
316
|
+
if (token.type !== "text")
|
|
317
|
+
break;
|
|
318
|
+
result2 += token.value;
|
|
319
|
+
}
|
|
320
|
+
return result2;
|
|
321
|
+
}
|
|
322
|
+
while (index < tokens.length) {
|
|
323
|
+
const token = tokens[index++];
|
|
308
324
|
if (token.type === "text") {
|
|
309
325
|
result += escape(token.value);
|
|
310
326
|
backtrack += token.value;
|
|
311
|
-
|
|
327
|
+
if (prevCaptureType === 2)
|
|
328
|
+
wildcardBacktrack += token.value;
|
|
329
|
+
if (token.value.includes(delimiter))
|
|
330
|
+
hasSegmentCapture = 0;
|
|
312
331
|
continue;
|
|
313
332
|
}
|
|
314
333
|
if (token.type === "param" || token.type === "wildcard") {
|
|
315
|
-
if (
|
|
334
|
+
if (prevCaptureType && !backtrack) {
|
|
316
335
|
throw new PathError(`Missing text before "${token.name}" ${token.type}`, originalPath);
|
|
317
336
|
}
|
|
318
337
|
if (token.type === "param") {
|
|
319
|
-
result += `(${negate(delimiter,
|
|
338
|
+
result += hasSegmentCapture & 2 ? `(${negate(delimiter, backtrack)}+)` : hasInSegment(index, "wildcard") ? `(${negate(delimiter, peekText(index))}+)` : hasSegmentCapture & 1 ? `(${negate(delimiter, backtrack)}+|${escape(backtrack)})` : `(${negate(delimiter, "")}+)`;
|
|
339
|
+
hasSegmentCapture |= prevCaptureType = 1;
|
|
320
340
|
} else {
|
|
321
|
-
result += `([
|
|
341
|
+
result += hasSegmentCapture & 2 ? `(${negate(backtrack, "")}+)` : wildcardBacktrack ? `(${negate(wildcardBacktrack, "")}+|${negate(delimiter, "")}+)` : `([^]+)`;
|
|
342
|
+
wildcardBacktrack = "";
|
|
343
|
+
hasSegmentCapture |= prevCaptureType = 2;
|
|
322
344
|
}
|
|
323
345
|
keys.push(token);
|
|
324
346
|
backtrack = "";
|
|
325
|
-
isSafeSegmentParam = false;
|
|
326
347
|
continue;
|
|
327
348
|
}
|
|
349
|
+
throw new TypeError(`Unknown token type: ${token.type}`);
|
|
328
350
|
}
|
|
329
351
|
return result;
|
|
330
352
|
}
|
|
331
|
-
function negate(
|
|
332
|
-
if (
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
return `
|
|
341
|
-
}
|
|
342
|
-
function stringifyTokens(tokens) {
|
|
353
|
+
function negate(a, b) {
|
|
354
|
+
if (b.length > a.length)
|
|
355
|
+
return negate(b, a);
|
|
356
|
+
if (a === b)
|
|
357
|
+
b = "";
|
|
358
|
+
if (b.length > 1)
|
|
359
|
+
return `(?:(?!${escape(a)}|${escape(b)})[^])`;
|
|
360
|
+
if (a.length > 1)
|
|
361
|
+
return `(?:(?!${escape(a)})[^${escape(b)}])`;
|
|
362
|
+
return `[^${escape(a + b)}]`;
|
|
363
|
+
}
|
|
364
|
+
function stringifyTokens(tokens, index) {
|
|
343
365
|
let value = "";
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const isSafe = isNameSafe(value2) && isNextNameSafe(tokens[i]);
|
|
347
|
-
return isSafe ? value2 : JSON.stringify(value2);
|
|
348
|
-
}
|
|
349
|
-
while (i < tokens.length) {
|
|
350
|
-
const token = tokens[i++];
|
|
366
|
+
while (index < tokens.length) {
|
|
367
|
+
const token = tokens[index++];
|
|
351
368
|
if (token.type === "text") {
|
|
352
369
|
value += escapeText(token.value);
|
|
353
370
|
continue;
|
|
354
371
|
}
|
|
355
372
|
if (token.type === "group") {
|
|
356
|
-
value +=
|
|
373
|
+
value += "{" + stringifyTokens(token.tokens, 0) + "}";
|
|
357
374
|
continue;
|
|
358
375
|
}
|
|
359
376
|
if (token.type === "param") {
|
|
360
|
-
value +=
|
|
377
|
+
value += ":" + stringifyName(token.name, tokens[index]);
|
|
361
378
|
continue;
|
|
362
379
|
}
|
|
363
380
|
if (token.type === "wildcard") {
|
|
364
|
-
value +=
|
|
381
|
+
value += "*" + stringifyName(token.name, tokens[index]);
|
|
365
382
|
continue;
|
|
366
383
|
}
|
|
367
384
|
throw new TypeError(`Unknown token type: ${token.type}`);
|
|
@@ -369,16 +386,15 @@ var require_dist = __commonJS((exports) => {
|
|
|
369
386
|
return value;
|
|
370
387
|
}
|
|
371
388
|
function stringify(data) {
|
|
372
|
-
return stringifyTokens(data.tokens);
|
|
373
|
-
}
|
|
374
|
-
function isNameSafe(name) {
|
|
375
|
-
const [first, ...rest] = name;
|
|
376
|
-
return ID_START.test(first) && rest.every((char) => ID_CONTINUE.test(char));
|
|
389
|
+
return stringifyTokens(data.tokens, 0);
|
|
377
390
|
}
|
|
378
|
-
function
|
|
379
|
-
if (
|
|
380
|
-
return
|
|
381
|
-
|
|
391
|
+
function stringifyName(name, next) {
|
|
392
|
+
if (!ID.test(name))
|
|
393
|
+
return JSON.stringify(name);
|
|
394
|
+
if ((next === null || next === undefined ? undefined : next.type) === "text" && ID_CONTINUE.test(next.value[0])) {
|
|
395
|
+
return JSON.stringify(name);
|
|
396
|
+
}
|
|
397
|
+
return name;
|
|
382
398
|
}
|
|
383
399
|
});
|
|
384
400
|
|
|
@@ -684,7 +700,7 @@ class HttpChain {
|
|
|
684
700
|
setInterval(() => {
|
|
685
701
|
res.write(`
|
|
686
702
|
`);
|
|
687
|
-
}, 3000);
|
|
703
|
+
}, 3000).unref?.();
|
|
688
704
|
req.on("close", () => {
|
|
689
705
|
clearInterval(intervalId);
|
|
690
706
|
res.end();
|