@rhythmjs/router 0.0.9 → 0.0.11

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.
@@ -1,150 +0,0 @@
1
- //#region src/radix-tree.ts
2
- var TreeNode = class {
3
- path = "";
4
- indices = "";
5
- children = [];
6
- paramChild = null;
7
- paramName = "";
8
- wildcardChild = null;
9
- methods = null;
10
- };
11
- function createNode() {
12
- return new TreeNode();
13
- }
14
- function commonPrefixLength(a, b) {
15
- const max = a.length < b.length ? a.length : b.length;
16
- let i = 0;
17
- while (i < max && a.charCodeAt(i) === b.charCodeAt(i)) i++;
18
- return i;
19
- }
20
- function splitChild(node, at) {
21
- const tail = new TreeNode();
22
- tail.path = node.path.slice(at);
23
- tail.indices = node.indices;
24
- tail.children = node.children;
25
- tail.paramChild = node.paramChild;
26
- tail.paramName = node.paramName;
27
- tail.wildcardChild = node.wildcardChild;
28
- tail.methods = node.methods;
29
- node.path = node.path.slice(0, at);
30
- node.children = [tail];
31
- node.indices = tail.path.charAt(0);
32
- node.paramChild = null;
33
- node.paramName = "";
34
- node.wildcardChild = null;
35
- node.methods = null;
36
- }
37
- function insertAt(node, path, method, payload) {
38
- if (path.length === 0) {
39
- if (!node.methods) node.methods = /* @__PURE__ */ new Map();
40
- node.methods.set(method, payload);
41
- return;
42
- }
43
- if (path.charCodeAt(0) === 58) {
44
- const slashIndex = path.indexOf("/");
45
- const name = slashIndex === -1 ? path.slice(1) : path.slice(1, slashIndex);
46
- const rest = slashIndex === -1 ? "" : path.slice(slashIndex);
47
- if (!node.paramChild) {
48
- node.paramChild = new TreeNode();
49
- node.paramChild.paramName = name;
50
- }
51
- insertAt(node.paramChild, rest, method, payload);
52
- return;
53
- }
54
- if (path.charCodeAt(0) === 42) {
55
- if (path.length !== 1) throw new Error("wildcard \"*\" is only allowed at the end of a path");
56
- if (!node.wildcardChild) node.wildcardChild = new TreeNode();
57
- if (!node.wildcardChild.methods) node.wildcardChild.methods = /* @__PURE__ */ new Map();
58
- node.wildcardChild.methods.set(method, payload);
59
- return;
60
- }
61
- const colonIndex = path.indexOf(":");
62
- const starIndex = path.indexOf("*");
63
- let staticEnd = path.length;
64
- if (colonIndex !== -1) staticEnd = colonIndex;
65
- if (starIndex !== -1 && starIndex < staticEnd) staticEnd = starIndex;
66
- const staticPart = path.slice(0, staticEnd);
67
- const firstChar = path.charAt(0);
68
- for (let i = 0; i < node.children.length; i++) {
69
- if (node.indices.charAt(i) !== firstChar) continue;
70
- const child = node.children[i];
71
- const cpl = commonPrefixLength(staticPart, child.path);
72
- if (cpl === 0) continue;
73
- if (cpl < child.path.length) splitChild(child, cpl);
74
- insertAt(child, path.slice(cpl), method, payload);
75
- return;
76
- }
77
- const child = new TreeNode();
78
- child.path = staticPart;
79
- node.children.push(child);
80
- node.indices += firstChar;
81
- insertAt(child, path.slice(staticPart.length), method, payload);
82
- }
83
- function expandOptionalParams(path) {
84
- if (!path.includes("?")) return [path];
85
- const segments = path.split("/");
86
- let firstOptional = -1;
87
- for (let i = 0; i < segments.length; i++) {
88
- const segment = segments[i];
89
- const isOptional = segment.length > 2 && segment.charCodeAt(0) === 58 && segment.endsWith("?");
90
- if (!isOptional && segment.includes("?")) throw new Error(`"?" is only allowed to mark an optional param like ":name?" (in "${path}")`);
91
- if (firstOptional !== -1 && !isOptional) throw new Error(`only optional params may follow an optional param (in "${path}")`);
92
- if (isOptional && firstOptional === -1) firstOptional = i;
93
- }
94
- const variants = [];
95
- for (let end = firstOptional; end <= segments.length; end++) {
96
- const parts = segments.slice(0, end).map((segment) => segment.endsWith("?") ? segment.slice(0, -1) : segment);
97
- variants.push(parts.join("/") || "/");
98
- }
99
- return variants;
100
- }
101
- function insertRoute(root, method, path, payload) {
102
- for (const variant of expandOptionalParams(path)) insertAt(root, variant, method, payload);
103
- }
104
- function search(node, method, path) {
105
- if (path.length === 0) {
106
- const payload = node.methods?.get(method);
107
- if (payload !== void 0) return {
108
- payload,
109
- params: {}
110
- };
111
- } else {
112
- const firstChar = path.charAt(0);
113
- for (let i = 0; i < node.children.length; i++) {
114
- if (node.indices.charAt(i) !== firstChar) continue;
115
- const child = node.children[i];
116
- if (!path.startsWith(child.path)) continue;
117
- const result = search(child, method, path.slice(child.path.length));
118
- if (result) return result;
119
- }
120
- if (node.paramChild) {
121
- const slashIndex = path.indexOf("/");
122
- const value = slashIndex === -1 ? path : path.slice(0, slashIndex);
123
- if (value.length > 0) {
124
- const rest = slashIndex === -1 ? "" : path.slice(slashIndex);
125
- const result = search(node.paramChild, method, rest);
126
- if (result) {
127
- result.params[node.paramChild.paramName] = decodeURIComponent(value);
128
- return result;
129
- }
130
- }
131
- }
132
- }
133
- if (node.wildcardChild) {
134
- const payload = node.wildcardChild.methods?.get(method);
135
- if (payload !== void 0) return {
136
- payload,
137
- params: { "*": decodeURIComponent(path) }
138
- };
139
- }
140
- return null;
141
- }
142
- function lookupRoute(root, method, pathname) {
143
- return search(root, method, pathname);
144
- }
145
- function joinPath(prefix, path) {
146
- if (!prefix) return path;
147
- return `${prefix.endsWith("/") ? prefix.slice(0, -1) : prefix}${path.startsWith("/") ? path : `/${path}`}`;
148
- }
149
- //#endregion
150
- export { TreeNode, createNode, insertRoute, joinPath, lookupRoute };