@kevisual/router 0.2.4 → 0.2.5

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.
@@ -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.0/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,8 @@ 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 SIMPLE_TOKENS = {
47
- "{": "{",
48
- "}": "}",
49
- "(": "(",
50
- ")": ")",
51
- "[": "[",
52
- "]": "]",
53
- "+": "+",
54
- "?": "?",
55
- "!": "!"
56
- };
46
+ var ID = /^[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u;
47
+ var SIMPLE_TOKENS = "{}()[]+?!";
57
48
  function escapeText(str) {
58
49
  return str.replace(/[{}()\[\]+?!:*\\]/g, "\\$&");
59
50
  }
@@ -94,8 +85,8 @@ var require_dist = __commonJS((exports) => {
94
85
  } while (ID_CONTINUE.test(chars[index]));
95
86
  } else if (chars[index] === '"') {
96
87
  let quoteStart = index;
97
- while (index++ < chars.length) {
98
- if (chars[index] === '"') {
88
+ while (index < chars.length) {
89
+ if (chars[++index] === '"') {
99
90
  index++;
100
91
  quoteStart = 0;
101
92
  break;
@@ -114,18 +105,17 @@ var require_dist = __commonJS((exports) => {
114
105
  return value;
115
106
  }
116
107
  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 });
108
+ const value = chars[index++];
109
+ if (SIMPLE_TOKENS.includes(value)) {
110
+ tokens.push({ type: value, index, value });
121
111
  } else if (value === "\\") {
122
- tokens.push({ type: "escape", index: index++, value: chars[index++] });
112
+ tokens.push({ type: "escape", index, value: chars[index++] });
123
113
  } else if (value === ":") {
124
- tokens.push({ type: "param", index: index++, value: name() });
114
+ tokens.push({ type: "param", index, value: name() });
125
115
  } else if (value === "*") {
126
- tokens.push({ type: "wildcard", index: index++, value: name() });
116
+ tokens.push({ type: "wildcard", index, value: name() });
127
117
  } else {
128
- tokens.push({ type: "char", index: index++, value });
118
+ tokens.push({ type: "char", index, value });
129
119
  }
130
120
  }
131
121
  tokens.push({ type: "end", index, value: "" });
@@ -261,107 +251,159 @@ var require_dist = __commonJS((exports) => {
261
251
  }
262
252
  function pathToRegexp(path, options = {}) {
263
253
  const { delimiter = DEFAULT_DELIMITER, end = true, sensitive = false, trailing = true } = options;
264
- const keys = [];
265
- const flags = sensitive ? "" : "i";
266
- const sources = [];
267
- for (const input of pathsToArray(path, [])) {
268
- const data = typeof input === "object" ? input : parse(input, options);
269
- for (const tokens of flatten(data.tokens, 0, [])) {
270
- sources.push(toRegExpSource(tokens, delimiter, keys, data.originalPath));
254
+ const root = new SourceNode("^");
255
+ const paths = [path];
256
+ let combinations = 0;
257
+ while (paths.length) {
258
+ const path2 = paths.shift();
259
+ if (Array.isArray(path2)) {
260
+ paths.push(...path2);
261
+ continue;
271
262
  }
263
+ const data = typeof path2 === "object" ? path2 : parse(path2, options);
264
+ flatten(data.tokens, 0, [], (tokens) => {
265
+ if (combinations++ >= 256) {
266
+ throw new PathError("Too many path combinations", data.originalPath);
267
+ }
268
+ let node = root;
269
+ for (const part of toRegExpSource(tokens, delimiter, data.originalPath)) {
270
+ node = node.add(part.source, part.key);
271
+ }
272
+ node.add("");
273
+ });
272
274
  }
273
- let pattern = `^(?:${sources.join("|")})`;
275
+ const keys = [];
276
+ let pattern = toRegExp(root, keys);
274
277
  if (trailing)
275
- pattern += `(?:${escape(delimiter)}$)?`;
276
- pattern += end ? "$" : `(?=${escape(delimiter)}|$)`;
277
- const regexp = new RegExp(pattern, flags);
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;
278
+ pattern += "(?:" + escape(delimiter) + "$)?";
279
+ pattern += end ? "$" : "(?=" + escape(delimiter) + "|$)";
280
+ return { regexp: new RegExp(pattern, sensitive ? "" : "i"), keys };
288
281
  }
289
- function* flatten(tokens, index, init) {
290
- if (index === tokens.length) {
291
- return yield init;
282
+ function toRegExp(node, keys) {
283
+ if (node.key)
284
+ keys.push(node.key);
285
+ const children = Object.keys(node.children);
286
+ const text = children.map((id) => toRegExp(node.children[id], keys)).join("|");
287
+ return node.source + (children.length < 2 ? text : `(?:${text})`);
288
+ }
289
+
290
+ class SourceNode {
291
+ constructor(source, key) {
292
+ this.source = source;
293
+ this.key = key;
294
+ this.children = Object.create(null);
292
295
  }
293
- const token = tokens[index];
294
- if (token.type === "group") {
295
- for (const seq of flatten(token.tokens, 0, init.slice())) {
296
- yield* flatten(tokens, index + 1, seq);
296
+ add(source, key) {
297
+ var _a;
298
+ const id = source + ":" + (key ? key.name : "");
299
+ return (_a = this.children)[id] || (_a[id] = new SourceNode(source, key));
300
+ }
301
+ }
302
+ function flatten(tokens, index, result, callback) {
303
+ while (index < tokens.length) {
304
+ const token = tokens[index++];
305
+ if (token.type === "group") {
306
+ flatten(token.tokens, 0, result.slice(), (seq) => flatten(tokens, index, seq, callback));
307
+ continue;
297
308
  }
298
- } else {
299
- init.push(token);
309
+ result.push(token);
300
310
  }
301
- yield* flatten(tokens, index + 1, init);
311
+ callback(result);
302
312
  }
303
- function toRegExpSource(tokens, delimiter, keys, originalPath) {
304
- let result = "";
313
+ function toRegExpSource(tokens, delimiter, originalPath) {
314
+ let result = [];
305
315
  let backtrack = "";
306
- let isSafeSegmentParam = true;
307
- for (const token of tokens) {
316
+ let wildcardBacktrack = "";
317
+ let prevCaptureType = 0;
318
+ let hasSegmentCapture = 0;
319
+ let index = 0;
320
+ function hasInSegment(index2, type) {
321
+ while (index2 < tokens.length) {
322
+ const token = tokens[index2++];
323
+ if (token.type === type)
324
+ return true;
325
+ if (token.type === "text") {
326
+ if (token.value.includes(delimiter))
327
+ break;
328
+ }
329
+ }
330
+ return false;
331
+ }
332
+ function peekText(index2) {
333
+ let result2 = "";
334
+ while (index2 < tokens.length) {
335
+ const token = tokens[index2++];
336
+ if (token.type !== "text")
337
+ break;
338
+ result2 += token.value;
339
+ }
340
+ return result2;
341
+ }
342
+ while (index < tokens.length) {
343
+ const token = tokens[index++];
308
344
  if (token.type === "text") {
309
- result += escape(token.value);
345
+ result.push({ source: escape(token.value) });
310
346
  backtrack += token.value;
311
- isSafeSegmentParam || (isSafeSegmentParam = token.value.includes(delimiter));
347
+ if (prevCaptureType === 2)
348
+ wildcardBacktrack += token.value;
349
+ if (token.value.includes(delimiter))
350
+ hasSegmentCapture = 0;
312
351
  continue;
313
352
  }
314
353
  if (token.type === "param" || token.type === "wildcard") {
315
- if (!isSafeSegmentParam && !backtrack) {
354
+ if (prevCaptureType && !backtrack) {
316
355
  throw new PathError(`Missing text before "${token.name}" ${token.type}`, originalPath);
317
356
  }
318
357
  if (token.type === "param") {
319
- result += `(${negate(delimiter, isSafeSegmentParam ? "" : backtrack)}+)`;
358
+ result.push({
359
+ source: hasSegmentCapture ? `(${negate(delimiter, backtrack)}+?)` : hasInSegment(index, "wildcard") ? `(${negate(delimiter, peekText(index))}+?)` : `(${negate(delimiter, "")}+?)`,
360
+ key: token
361
+ });
362
+ hasSegmentCapture |= prevCaptureType = 1;
320
363
  } else {
321
- result += `([\\s\\S]+)`;
364
+ result.push({
365
+ source: hasSegmentCapture & 2 ? `(${negate(backtrack, "")}+?)` : hasSegmentCapture & 1 ? `(${negate(wildcardBacktrack, "")}+?)` : wildcardBacktrack ? `(${negate(wildcardBacktrack, "")}+?|${negate(delimiter, "")}+?)` : `([^]+?)`,
366
+ key: token
367
+ });
368
+ wildcardBacktrack = "";
369
+ hasSegmentCapture |= prevCaptureType = 2;
322
370
  }
323
- keys.push(token);
324
371
  backtrack = "";
325
- isSafeSegmentParam = false;
326
372
  continue;
327
373
  }
374
+ throw new TypeError(`Unknown token type: ${token.type}`);
328
375
  }
329
376
  return result;
330
377
  }
331
- function negate(delimiter, backtrack) {
332
- if (backtrack.length < 2) {
333
- if (delimiter.length < 2)
334
- return `[^${escape(delimiter + backtrack)}]`;
335
- return `(?:(?!${escape(delimiter)})[^${escape(backtrack)}])`;
336
- }
337
- if (delimiter.length < 2) {
338
- return `(?:(?!${escape(backtrack)})[^${escape(delimiter)}])`;
339
- }
340
- return `(?:(?!${escape(backtrack)}|${escape(delimiter)})[\\s\\S])`;
341
- }
342
- function stringifyTokens(tokens) {
378
+ function negate(a, b) {
379
+ if (b.length > a.length)
380
+ return negate(b, a);
381
+ if (a === b)
382
+ b = "";
383
+ if (b.length > 1)
384
+ return `(?:(?!${escape(a)}|${escape(b)})[^])`;
385
+ if (a.length > 1)
386
+ return `(?:(?!${escape(a)})[^${escape(b)}])`;
387
+ return `[^${escape(a + b)}]`;
388
+ }
389
+ function stringifyTokens(tokens, index) {
343
390
  let value = "";
344
- let i = 0;
345
- function name(value2) {
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++];
391
+ while (index < tokens.length) {
392
+ const token = tokens[index++];
351
393
  if (token.type === "text") {
352
394
  value += escapeText(token.value);
353
395
  continue;
354
396
  }
355
397
  if (token.type === "group") {
356
- value += `{${stringifyTokens(token.tokens)}}`;
398
+ value += "{" + stringifyTokens(token.tokens, 0) + "}";
357
399
  continue;
358
400
  }
359
401
  if (token.type === "param") {
360
- value += `:${name(token.name)}`;
402
+ value += ":" + stringifyName(token.name, tokens[index]);
361
403
  continue;
362
404
  }
363
405
  if (token.type === "wildcard") {
364
- value += `*${name(token.name)}`;
406
+ value += "*" + stringifyName(token.name, tokens[index]);
365
407
  continue;
366
408
  }
367
409
  throw new TypeError(`Unknown token type: ${token.type}`);
@@ -369,16 +411,15 @@ var require_dist = __commonJS((exports) => {
369
411
  return value;
370
412
  }
371
413
  function stringify(data) {
372
- return stringifyTokens(data.tokens);
414
+ return stringifyTokens(data.tokens, 0);
373
415
  }
374
- function isNameSafe(name) {
375
- const [first, ...rest] = name;
376
- return ID_START.test(first) && rest.every((char) => ID_CONTINUE.test(char));
377
- }
378
- function isNextNameSafe(token) {
379
- if (token && token.type === "text")
380
- return !ID_CONTINUE.test(token.value[0]);
381
- return true;
416
+ function stringifyName(name, next) {
417
+ if (!ID.test(name))
418
+ return JSON.stringify(name);
419
+ if ((next === null || next === undefined ? undefined : next.type) === "text" && ID_CONTINUE.test(next.value[0])) {
420
+ return JSON.stringify(name);
421
+ }
422
+ return name;
382
423
  }
383
424
  });
384
425