@mpen/rerouter 0.1.2
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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/bundle.cjs +406 -0
- package/dist/bundle.d.ts +2 -0
- package/dist/bundle.mjs +404 -0
- package/dist/dev.d.ts +1 -0
- package/dist/log.d.ts +1 -0
- package/dist/uri-template.d.ts +48 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Mark Penner
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# YourLib
|
|
2
|
+
|
|
3
|
+
Created with [`ctslib`](https://yarnpkg.com/en/package/create-tslib).
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
### Run source code directly
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
make dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Build code for production
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
make
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
It will be written to `dist/`. After that, you don't need TypeScript nor babel installed. It can be distributed for node LTS.
|
package/dist/bundle.cjs
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const PREFIX_RE = /^[+#./;?&]/;
|
|
4
|
+
|
|
5
|
+
const MODIFIER_RE = /(?<repeat>\*)?(?::(?<func>[a-zA-Z][a-zA-Z0-9_]*))?(?::(?:(?<length>\d+)))?$/;
|
|
6
|
+
|
|
7
|
+
const UNRESERVED = /[^a-zA-Z0-9\-._~]+/gsu;
|
|
8
|
+
|
|
9
|
+
const UR_SET = /[^a-zA-Z0-9\-._~:\/?#[\]@!$&'()*+,;=]+/gsu;
|
|
10
|
+
|
|
11
|
+
const PERCENT_RE = /^%[0-9a-fA-F]{2}$/;
|
|
12
|
+
|
|
13
|
+
const STR = Symbol("string");
|
|
14
|
+
|
|
15
|
+
const VAR = Symbol("var");
|
|
16
|
+
|
|
17
|
+
const SeparatorMap = {
|
|
18
|
+
"": ",",
|
|
19
|
+
"+": ",",
|
|
20
|
+
".": ".",
|
|
21
|
+
"/": "/",
|
|
22
|
+
";": ";",
|
|
23
|
+
"?": "&",
|
|
24
|
+
"&": "&",
|
|
25
|
+
"#": ","
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const FirstMap = {
|
|
29
|
+
"": "",
|
|
30
|
+
"+": "",
|
|
31
|
+
".": ".",
|
|
32
|
+
"/": "/",
|
|
33
|
+
";": ";",
|
|
34
|
+
"?": "?",
|
|
35
|
+
"&": "&",
|
|
36
|
+
"#": "#"
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const Named = {
|
|
40
|
+
"": false,
|
|
41
|
+
"+": false,
|
|
42
|
+
".": false,
|
|
43
|
+
"/": false,
|
|
44
|
+
";": true,
|
|
45
|
+
"?": true,
|
|
46
|
+
"&": true,
|
|
47
|
+
"#": false
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const IfEmp = {
|
|
51
|
+
"": "",
|
|
52
|
+
"+": "",
|
|
53
|
+
".": "",
|
|
54
|
+
"/": "",
|
|
55
|
+
";": "",
|
|
56
|
+
"?": "=",
|
|
57
|
+
"&": "=",
|
|
58
|
+
"#": ""
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const ReservedExpansion = {
|
|
62
|
+
"": false,
|
|
63
|
+
"+": true,
|
|
64
|
+
".": false,
|
|
65
|
+
"/": false,
|
|
66
|
+
";": false,
|
|
67
|
+
"?": false,
|
|
68
|
+
"&": false,
|
|
69
|
+
"#": true
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
var VarType;
|
|
73
|
+
|
|
74
|
+
(function(VarType) {
|
|
75
|
+
VarType[VarType["NAMED"] = 0] = "NAMED";
|
|
76
|
+
VarType[VarType["STATIC"] = 1] = "STATIC";
|
|
77
|
+
VarType[VarType["DYNAMIC"] = 2] = "DYNAMIC";
|
|
78
|
+
})(VarType || (VarType = {}));
|
|
79
|
+
|
|
80
|
+
class UriTemplate {
|
|
81
|
+
expandParts;
|
|
82
|
+
matchRegex;
|
|
83
|
+
matchMap;
|
|
84
|
+
constructor(template) {
|
|
85
|
+
this.expandParts = [];
|
|
86
|
+
const re = [ "^" ];
|
|
87
|
+
this.matchMap = new Map;
|
|
88
|
+
let pos = 0;
|
|
89
|
+
const matches = Array.from(template.matchAll(/\{(.+?)\}/g));
|
|
90
|
+
let matchCounter = 0;
|
|
91
|
+
for (const m of matches) {
|
|
92
|
+
if (m.index > pos) {
|
|
93
|
+
const lit = template.slice(pos, m.index);
|
|
94
|
+
this.expandParts.push({
|
|
95
|
+
type: STR,
|
|
96
|
+
value: lit
|
|
97
|
+
});
|
|
98
|
+
const groupName = `static${matchCounter++}`;
|
|
99
|
+
re.push(`(?<${groupName}>${escapeRegExp(lit)})`);
|
|
100
|
+
this.matchMap.set(groupName, {
|
|
101
|
+
type: VarType.STATIC
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
pos = m.index + m[0].length;
|
|
105
|
+
const ph = {
|
|
106
|
+
type: VAR,
|
|
107
|
+
prefix: "",
|
|
108
|
+
vars: []
|
|
109
|
+
};
|
|
110
|
+
this.expandParts.push(ph);
|
|
111
|
+
let varStr = m[1];
|
|
112
|
+
const prefix = varStr.match(PREFIX_RE);
|
|
113
|
+
if (prefix) {
|
|
114
|
+
varStr = varStr.slice(1);
|
|
115
|
+
ph.prefix = prefix[0];
|
|
116
|
+
}
|
|
117
|
+
const vars = varStr.split(",");
|
|
118
|
+
const isNamed = Named[ph.prefix];
|
|
119
|
+
let isFirst = true;
|
|
120
|
+
if (!isNamed) {
|
|
121
|
+
re.push(escapeRegExp(FirstMap[ph.prefix]));
|
|
122
|
+
}
|
|
123
|
+
for (const v of vars) {
|
|
124
|
+
const item = {
|
|
125
|
+
name: v,
|
|
126
|
+
length: null,
|
|
127
|
+
func: null,
|
|
128
|
+
repeat: false
|
|
129
|
+
};
|
|
130
|
+
const mod = v.match(MODIFIER_RE);
|
|
131
|
+
if (mod) {
|
|
132
|
+
if (mod.groups.repeat !== undefined) {
|
|
133
|
+
item.repeat = true;
|
|
134
|
+
}
|
|
135
|
+
if (mod.groups.length !== undefined) {
|
|
136
|
+
item.length = Number(mod.groups.length);
|
|
137
|
+
}
|
|
138
|
+
if (mod.groups.func !== undefined) {
|
|
139
|
+
item.func = mod.groups.func;
|
|
140
|
+
}
|
|
141
|
+
item.name = item.name.slice(0, mod.index);
|
|
142
|
+
}
|
|
143
|
+
ph.vars.push(item);
|
|
144
|
+
if (!isNamed) {
|
|
145
|
+
if (!isFirst) {
|
|
146
|
+
re.push(escapeRegExp(SeparatorMap[ph.prefix]));
|
|
147
|
+
}
|
|
148
|
+
let groupName = item.name;
|
|
149
|
+
if (!/^[a-z][a-z0-9_]*$/i.test(groupName)) {
|
|
150
|
+
groupName = "param";
|
|
151
|
+
}
|
|
152
|
+
groupName += String(matchCounter++);
|
|
153
|
+
re.push(`(?<${groupName}>`);
|
|
154
|
+
if (item.func != null) {
|
|
155
|
+
switch (item.func) {
|
|
156
|
+
case "int":
|
|
157
|
+
if (item.length != null) {
|
|
158
|
+
re.push(`\\d{${item.length}}`);
|
|
159
|
+
} else {
|
|
160
|
+
re.push("-?\\d+");
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
|
|
164
|
+
default:
|
|
165
|
+
throw new Error(`Unrecognized UriTemplate func: ${item.func}`);
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
let ch;
|
|
169
|
+
if (ph.prefix === "+") {
|
|
170
|
+
ch = "[^?#]";
|
|
171
|
+
} else if (ph.prefix === "#") {
|
|
172
|
+
ch = ".";
|
|
173
|
+
} else if (ph.prefix === "/" && item.repeat) {
|
|
174
|
+
ch = "[^?#]";
|
|
175
|
+
} else {
|
|
176
|
+
ch = "[^/?#]";
|
|
177
|
+
}
|
|
178
|
+
if (item.length != null) {
|
|
179
|
+
ch = `(?:%[0-9a-fA-F]{2}|${ch})`;
|
|
180
|
+
re.push(ch, `{${item.length}}`);
|
|
181
|
+
} else {
|
|
182
|
+
re.push(ch, "+?");
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
re.push(")");
|
|
186
|
+
this.matchMap.set(groupName, {
|
|
187
|
+
var: item,
|
|
188
|
+
type: VarType.DYNAMIC,
|
|
189
|
+
prefix: ph.prefix
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
isFirst = false;
|
|
193
|
+
}
|
|
194
|
+
if (isNamed) {
|
|
195
|
+
re.push("(?:", escapeRegExp(FirstMap[ph.prefix]));
|
|
196
|
+
const groupName = `kwargs${matchCounter++}`;
|
|
197
|
+
re.push(`(?<${groupName}>[^/?#]*)`);
|
|
198
|
+
re.push(")?");
|
|
199
|
+
this.matchMap.set(groupName, {
|
|
200
|
+
vars: ph.vars,
|
|
201
|
+
type: VarType.NAMED,
|
|
202
|
+
prefix: ph.prefix
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (pos < template.length) {
|
|
207
|
+
const lit = template.slice(pos);
|
|
208
|
+
this.expandParts.push({
|
|
209
|
+
type: STR,
|
|
210
|
+
value: lit
|
|
211
|
+
});
|
|
212
|
+
const groupName = `static${matchCounter++}`;
|
|
213
|
+
re.push(`(?<${groupName}>${escapeRegExp(lit)})`);
|
|
214
|
+
this.matchMap.set(groupName, {
|
|
215
|
+
type: VarType.STATIC
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
re.push("$");
|
|
219
|
+
this.matchRegex = new RegExp(re.join(""));
|
|
220
|
+
}
|
|
221
|
+
expand(variables) {
|
|
222
|
+
const out = [];
|
|
223
|
+
for (const p of this.expandParts) {
|
|
224
|
+
switch (p.type) {
|
|
225
|
+
case VAR:
|
|
226
|
+
const vs = [];
|
|
227
|
+
for (const v of p.vars) {
|
|
228
|
+
if (Object.hasOwn(variables, v.name)) {
|
|
229
|
+
const x = variables[v.name];
|
|
230
|
+
const esc = x => percentEncodeRegExp(x, ReservedExpansion[p.prefix], v.length);
|
|
231
|
+
let pre = "";
|
|
232
|
+
if (Named[p.prefix]) {
|
|
233
|
+
pre = v.name + (isEmpty(x) ? IfEmp[p.prefix] : "=");
|
|
234
|
+
}
|
|
235
|
+
if (Array.isArray(x)) {
|
|
236
|
+
if (x.length) {
|
|
237
|
+
vs.push((v.repeat ? "" : pre) + x.map((z => (v.repeat ? pre : "") + esc(z))).join(v.repeat ? SeparatorMap[p.prefix] : ","));
|
|
238
|
+
}
|
|
239
|
+
} else if (typeof x === "object") {
|
|
240
|
+
if (Object.keys(x).length) {
|
|
241
|
+
vs.push((v.repeat ? "" : pre) + Object.entries(x).map((([ok, ov]) => `${esc(ok)}${v.repeat ? "=" : ","}${esc(ov)}`)).join(v.repeat ? SeparatorMap[p.prefix] : ","));
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
vs.push(pre + esc(x));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (vs.length) {
|
|
249
|
+
out.push(FirstMap[p.prefix], vs.join(SeparatorMap[p.prefix]));
|
|
250
|
+
}
|
|
251
|
+
break;
|
|
252
|
+
|
|
253
|
+
case STR:
|
|
254
|
+
out.push(p.value);
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return out.join("");
|
|
259
|
+
}
|
|
260
|
+
match(url) {
|
|
261
|
+
let score = 0;
|
|
262
|
+
const m = url.match(this.matchRegex);
|
|
263
|
+
if (m !== null) {
|
|
264
|
+
let params = [];
|
|
265
|
+
if (m.groups != null) {
|
|
266
|
+
for (const [k, v] of Object.entries(m.groups)) {
|
|
267
|
+
const itemSpec = this.matchMap.get(k);
|
|
268
|
+
let value;
|
|
269
|
+
switch (itemSpec.type) {
|
|
270
|
+
case VarType.NAMED:
|
|
271
|
+
{
|
|
272
|
+
const parsed = v == null ? Object.create(null) : parseParams(v, SeparatorMap[itemSpec.prefix]);
|
|
273
|
+
for (const vs of itemSpec.vars) {
|
|
274
|
+
if (vs.repeat) {
|
|
275
|
+
score -= 1;
|
|
276
|
+
if (isEmpty(parsed)) {
|
|
277
|
+
params.push([ vs.name, EMPTY_OBJ ]);
|
|
278
|
+
} else {
|
|
279
|
+
params.push([ vs.name, mapValues(parsed, (x => formatElement(x, vs))) ]);
|
|
280
|
+
}
|
|
281
|
+
break;
|
|
282
|
+
} else {
|
|
283
|
+
if (parsed[vs.name] !== undefined) {
|
|
284
|
+
score += 2;
|
|
285
|
+
params.push([ vs.name, formatElement(parsed[vs.name], vs) ]);
|
|
286
|
+
delete parsed[vs.name];
|
|
287
|
+
} else {
|
|
288
|
+
params.push([ vs.name, null ]);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
break;
|
|
294
|
+
|
|
295
|
+
case VarType.DYNAMIC:
|
|
296
|
+
{
|
|
297
|
+
score += 2;
|
|
298
|
+
if (v == null) {
|
|
299
|
+
if (itemSpec.var.repeat) {
|
|
300
|
+
if (Named[itemSpec.prefix]) {
|
|
301
|
+
value = EMPTY_OBJ;
|
|
302
|
+
} else {
|
|
303
|
+
value = EMPTY_ARR;
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
value = null;
|
|
307
|
+
}
|
|
308
|
+
} else {
|
|
309
|
+
value = v;
|
|
310
|
+
if (itemSpec.var.repeat) {
|
|
311
|
+
value = value.split(SeparatorMap[itemSpec.prefix]).map((x => formatElement(x, itemSpec.var)));
|
|
312
|
+
} else {
|
|
313
|
+
value = formatElement(value, itemSpec.var);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
params.push([ itemSpec.var.name, value ]);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
|
|
320
|
+
case VarType.STATIC:
|
|
321
|
+
{
|
|
322
|
+
score += 3;
|
|
323
|
+
}
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
score,
|
|
330
|
+
params: Object.fromEntries(params)
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function formatElement(str, varSpec) {
|
|
338
|
+
if (str == null) return null;
|
|
339
|
+
let out = decodeURIComponent(str);
|
|
340
|
+
if (varSpec.length != null) {
|
|
341
|
+
out = out.slice(0, varSpec.length);
|
|
342
|
+
}
|
|
343
|
+
if (varSpec.func === "int") {
|
|
344
|
+
out = Number(out);
|
|
345
|
+
}
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function mapValues(obj, callback) {
|
|
350
|
+
return Object.fromEntries(Object.entries(obj).map((([key, value]) => [ key, callback(value) ])));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function parseParams(queryString, separator) {
|
|
354
|
+
const pairs = queryString.split(separator);
|
|
355
|
+
return Object.fromEntries(pairs.map((pair => {
|
|
356
|
+
const idx = pair.indexOf("=");
|
|
357
|
+
if (idx === -1) {
|
|
358
|
+
return [ pair, null ];
|
|
359
|
+
}
|
|
360
|
+
const key = pair.slice(0, idx);
|
|
361
|
+
const value = pair.slice(idx + 1);
|
|
362
|
+
return [ key, value ];
|
|
363
|
+
})));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const EMPTY_OBJ = Object.freeze(Object.create(null));
|
|
367
|
+
|
|
368
|
+
const EMPTY_ARR = Object.freeze([]);
|
|
369
|
+
|
|
370
|
+
function isEmpty(x) {
|
|
371
|
+
return x == null || x === "" || (Array.isArray(x) ? !x.length : typeof x === "object" && !Object.keys(x).length);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function escapeRegExp(string) {
|
|
375
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function percentEncodeRegExp(x, reserved, length) {
|
|
379
|
+
if (typeof x === "number") {
|
|
380
|
+
return String(x);
|
|
381
|
+
}
|
|
382
|
+
if (x === true) return "1";
|
|
383
|
+
if (x === false) return "0";
|
|
384
|
+
if (x === null) return "";
|
|
385
|
+
if (length != null) {
|
|
386
|
+
x = x.slice(0, length);
|
|
387
|
+
}
|
|
388
|
+
if (reserved) {
|
|
389
|
+
return x.replace(/%[0-9a-fA-F]{2}|./gsu, (m => {
|
|
390
|
+
let v = PERCENT_RE.test(m) ? decodeURIComponent(m) : m;
|
|
391
|
+
if (UR_SET.test(v)) {
|
|
392
|
+
return percentEncode(v);
|
|
393
|
+
}
|
|
394
|
+
return m;
|
|
395
|
+
}));
|
|
396
|
+
}
|
|
397
|
+
return x.replace(UNRESERVED, percentEncode);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const UTF8_ENCODER = new TextEncoder;
|
|
401
|
+
|
|
402
|
+
function percentEncode(str) {
|
|
403
|
+
return Array.from(UTF8_ENCODER.encode(str)).map((i => "%" + i.toString(16).toUpperCase().padStart(2, "0"))).join("");
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
exports.UriTemplate = UriTemplate;
|
package/dist/bundle.d.ts
ADDED
package/dist/bundle.mjs
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
const PREFIX_RE = /^[+#./;?&]/;
|
|
2
|
+
|
|
3
|
+
const MODIFIER_RE = /(?<repeat>\*)?(?::(?<func>[a-zA-Z][a-zA-Z0-9_]*))?(?::(?:(?<length>\d+)))?$/;
|
|
4
|
+
|
|
5
|
+
const UNRESERVED = /[^a-zA-Z0-9\-._~]+/gsu;
|
|
6
|
+
|
|
7
|
+
const UR_SET = /[^a-zA-Z0-9\-._~:\/?#[\]@!$&'()*+,;=]+/gsu;
|
|
8
|
+
|
|
9
|
+
const PERCENT_RE = /^%[0-9a-fA-F]{2}$/;
|
|
10
|
+
|
|
11
|
+
const STR = Symbol("string");
|
|
12
|
+
|
|
13
|
+
const VAR = Symbol("var");
|
|
14
|
+
|
|
15
|
+
const SeparatorMap = {
|
|
16
|
+
"": ",",
|
|
17
|
+
"+": ",",
|
|
18
|
+
".": ".",
|
|
19
|
+
"/": "/",
|
|
20
|
+
";": ";",
|
|
21
|
+
"?": "&",
|
|
22
|
+
"&": "&",
|
|
23
|
+
"#": ","
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const FirstMap = {
|
|
27
|
+
"": "",
|
|
28
|
+
"+": "",
|
|
29
|
+
".": ".",
|
|
30
|
+
"/": "/",
|
|
31
|
+
";": ";",
|
|
32
|
+
"?": "?",
|
|
33
|
+
"&": "&",
|
|
34
|
+
"#": "#"
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const Named = {
|
|
38
|
+
"": false,
|
|
39
|
+
"+": false,
|
|
40
|
+
".": false,
|
|
41
|
+
"/": false,
|
|
42
|
+
";": true,
|
|
43
|
+
"?": true,
|
|
44
|
+
"&": true,
|
|
45
|
+
"#": false
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const IfEmp = {
|
|
49
|
+
"": "",
|
|
50
|
+
"+": "",
|
|
51
|
+
".": "",
|
|
52
|
+
"/": "",
|
|
53
|
+
";": "",
|
|
54
|
+
"?": "=",
|
|
55
|
+
"&": "=",
|
|
56
|
+
"#": ""
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const ReservedExpansion = {
|
|
60
|
+
"": false,
|
|
61
|
+
"+": true,
|
|
62
|
+
".": false,
|
|
63
|
+
"/": false,
|
|
64
|
+
";": false,
|
|
65
|
+
"?": false,
|
|
66
|
+
"&": false,
|
|
67
|
+
"#": true
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
var VarType;
|
|
71
|
+
|
|
72
|
+
(function(VarType) {
|
|
73
|
+
VarType[VarType["NAMED"] = 0] = "NAMED";
|
|
74
|
+
VarType[VarType["STATIC"] = 1] = "STATIC";
|
|
75
|
+
VarType[VarType["DYNAMIC"] = 2] = "DYNAMIC";
|
|
76
|
+
})(VarType || (VarType = {}));
|
|
77
|
+
|
|
78
|
+
class UriTemplate {
|
|
79
|
+
expandParts;
|
|
80
|
+
matchRegex;
|
|
81
|
+
matchMap;
|
|
82
|
+
constructor(template) {
|
|
83
|
+
this.expandParts = [];
|
|
84
|
+
const re = [ "^" ];
|
|
85
|
+
this.matchMap = new Map;
|
|
86
|
+
let pos = 0;
|
|
87
|
+
const matches = Array.from(template.matchAll(/\{(.+?)\}/g));
|
|
88
|
+
let matchCounter = 0;
|
|
89
|
+
for (const m of matches) {
|
|
90
|
+
if (m.index > pos) {
|
|
91
|
+
const lit = template.slice(pos, m.index);
|
|
92
|
+
this.expandParts.push({
|
|
93
|
+
type: STR,
|
|
94
|
+
value: lit
|
|
95
|
+
});
|
|
96
|
+
const groupName = `static${matchCounter++}`;
|
|
97
|
+
re.push(`(?<${groupName}>${escapeRegExp(lit)})`);
|
|
98
|
+
this.matchMap.set(groupName, {
|
|
99
|
+
type: VarType.STATIC
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
pos = m.index + m[0].length;
|
|
103
|
+
const ph = {
|
|
104
|
+
type: VAR,
|
|
105
|
+
prefix: "",
|
|
106
|
+
vars: []
|
|
107
|
+
};
|
|
108
|
+
this.expandParts.push(ph);
|
|
109
|
+
let varStr = m[1];
|
|
110
|
+
const prefix = varStr.match(PREFIX_RE);
|
|
111
|
+
if (prefix) {
|
|
112
|
+
varStr = varStr.slice(1);
|
|
113
|
+
ph.prefix = prefix[0];
|
|
114
|
+
}
|
|
115
|
+
const vars = varStr.split(",");
|
|
116
|
+
const isNamed = Named[ph.prefix];
|
|
117
|
+
let isFirst = true;
|
|
118
|
+
if (!isNamed) {
|
|
119
|
+
re.push(escapeRegExp(FirstMap[ph.prefix]));
|
|
120
|
+
}
|
|
121
|
+
for (const v of vars) {
|
|
122
|
+
const item = {
|
|
123
|
+
name: v,
|
|
124
|
+
length: null,
|
|
125
|
+
func: null,
|
|
126
|
+
repeat: false
|
|
127
|
+
};
|
|
128
|
+
const mod = v.match(MODIFIER_RE);
|
|
129
|
+
if (mod) {
|
|
130
|
+
if (mod.groups.repeat !== undefined) {
|
|
131
|
+
item.repeat = true;
|
|
132
|
+
}
|
|
133
|
+
if (mod.groups.length !== undefined) {
|
|
134
|
+
item.length = Number(mod.groups.length);
|
|
135
|
+
}
|
|
136
|
+
if (mod.groups.func !== undefined) {
|
|
137
|
+
item.func = mod.groups.func;
|
|
138
|
+
}
|
|
139
|
+
item.name = item.name.slice(0, mod.index);
|
|
140
|
+
}
|
|
141
|
+
ph.vars.push(item);
|
|
142
|
+
if (!isNamed) {
|
|
143
|
+
if (!isFirst) {
|
|
144
|
+
re.push(escapeRegExp(SeparatorMap[ph.prefix]));
|
|
145
|
+
}
|
|
146
|
+
let groupName = item.name;
|
|
147
|
+
if (!/^[a-z][a-z0-9_]*$/i.test(groupName)) {
|
|
148
|
+
groupName = "param";
|
|
149
|
+
}
|
|
150
|
+
groupName += String(matchCounter++);
|
|
151
|
+
re.push(`(?<${groupName}>`);
|
|
152
|
+
if (item.func != null) {
|
|
153
|
+
switch (item.func) {
|
|
154
|
+
case "int":
|
|
155
|
+
if (item.length != null) {
|
|
156
|
+
re.push(`\\d{${item.length}}`);
|
|
157
|
+
} else {
|
|
158
|
+
re.push("-?\\d+");
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
|
|
162
|
+
default:
|
|
163
|
+
throw new Error(`Unrecognized UriTemplate func: ${item.func}`);
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
let ch;
|
|
167
|
+
if (ph.prefix === "+") {
|
|
168
|
+
ch = "[^?#]";
|
|
169
|
+
} else if (ph.prefix === "#") {
|
|
170
|
+
ch = ".";
|
|
171
|
+
} else if (ph.prefix === "/" && item.repeat) {
|
|
172
|
+
ch = "[^?#]";
|
|
173
|
+
} else {
|
|
174
|
+
ch = "[^/?#]";
|
|
175
|
+
}
|
|
176
|
+
if (item.length != null) {
|
|
177
|
+
ch = `(?:%[0-9a-fA-F]{2}|${ch})`;
|
|
178
|
+
re.push(ch, `{${item.length}}`);
|
|
179
|
+
} else {
|
|
180
|
+
re.push(ch, "+?");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
re.push(")");
|
|
184
|
+
this.matchMap.set(groupName, {
|
|
185
|
+
var: item,
|
|
186
|
+
type: VarType.DYNAMIC,
|
|
187
|
+
prefix: ph.prefix
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
isFirst = false;
|
|
191
|
+
}
|
|
192
|
+
if (isNamed) {
|
|
193
|
+
re.push("(?:", escapeRegExp(FirstMap[ph.prefix]));
|
|
194
|
+
const groupName = `kwargs${matchCounter++}`;
|
|
195
|
+
re.push(`(?<${groupName}>[^/?#]*)`);
|
|
196
|
+
re.push(")?");
|
|
197
|
+
this.matchMap.set(groupName, {
|
|
198
|
+
vars: ph.vars,
|
|
199
|
+
type: VarType.NAMED,
|
|
200
|
+
prefix: ph.prefix
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (pos < template.length) {
|
|
205
|
+
const lit = template.slice(pos);
|
|
206
|
+
this.expandParts.push({
|
|
207
|
+
type: STR,
|
|
208
|
+
value: lit
|
|
209
|
+
});
|
|
210
|
+
const groupName = `static${matchCounter++}`;
|
|
211
|
+
re.push(`(?<${groupName}>${escapeRegExp(lit)})`);
|
|
212
|
+
this.matchMap.set(groupName, {
|
|
213
|
+
type: VarType.STATIC
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
re.push("$");
|
|
217
|
+
this.matchRegex = new RegExp(re.join(""));
|
|
218
|
+
}
|
|
219
|
+
expand(variables) {
|
|
220
|
+
const out = [];
|
|
221
|
+
for (const p of this.expandParts) {
|
|
222
|
+
switch (p.type) {
|
|
223
|
+
case VAR:
|
|
224
|
+
const vs = [];
|
|
225
|
+
for (const v of p.vars) {
|
|
226
|
+
if (Object.hasOwn(variables, v.name)) {
|
|
227
|
+
const x = variables[v.name];
|
|
228
|
+
const esc = x => percentEncodeRegExp(x, ReservedExpansion[p.prefix], v.length);
|
|
229
|
+
let pre = "";
|
|
230
|
+
if (Named[p.prefix]) {
|
|
231
|
+
pre = v.name + (isEmpty(x) ? IfEmp[p.prefix] : "=");
|
|
232
|
+
}
|
|
233
|
+
if (Array.isArray(x)) {
|
|
234
|
+
if (x.length) {
|
|
235
|
+
vs.push((v.repeat ? "" : pre) + x.map((z => (v.repeat ? pre : "") + esc(z))).join(v.repeat ? SeparatorMap[p.prefix] : ","));
|
|
236
|
+
}
|
|
237
|
+
} else if (typeof x === "object") {
|
|
238
|
+
if (Object.keys(x).length) {
|
|
239
|
+
vs.push((v.repeat ? "" : pre) + Object.entries(x).map((([ok, ov]) => `${esc(ok)}${v.repeat ? "=" : ","}${esc(ov)}`)).join(v.repeat ? SeparatorMap[p.prefix] : ","));
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
vs.push(pre + esc(x));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (vs.length) {
|
|
247
|
+
out.push(FirstMap[p.prefix], vs.join(SeparatorMap[p.prefix]));
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
|
|
251
|
+
case STR:
|
|
252
|
+
out.push(p.value);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return out.join("");
|
|
257
|
+
}
|
|
258
|
+
match(url) {
|
|
259
|
+
let score = 0;
|
|
260
|
+
const m = url.match(this.matchRegex);
|
|
261
|
+
if (m !== null) {
|
|
262
|
+
let params = [];
|
|
263
|
+
if (m.groups != null) {
|
|
264
|
+
for (const [k, v] of Object.entries(m.groups)) {
|
|
265
|
+
const itemSpec = this.matchMap.get(k);
|
|
266
|
+
let value;
|
|
267
|
+
switch (itemSpec.type) {
|
|
268
|
+
case VarType.NAMED:
|
|
269
|
+
{
|
|
270
|
+
const parsed = v == null ? Object.create(null) : parseParams(v, SeparatorMap[itemSpec.prefix]);
|
|
271
|
+
for (const vs of itemSpec.vars) {
|
|
272
|
+
if (vs.repeat) {
|
|
273
|
+
score -= 1;
|
|
274
|
+
if (isEmpty(parsed)) {
|
|
275
|
+
params.push([ vs.name, EMPTY_OBJ ]);
|
|
276
|
+
} else {
|
|
277
|
+
params.push([ vs.name, mapValues(parsed, (x => formatElement(x, vs))) ]);
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
} else {
|
|
281
|
+
if (parsed[vs.name] !== undefined) {
|
|
282
|
+
score += 2;
|
|
283
|
+
params.push([ vs.name, formatElement(parsed[vs.name], vs) ]);
|
|
284
|
+
delete parsed[vs.name];
|
|
285
|
+
} else {
|
|
286
|
+
params.push([ vs.name, null ]);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
|
|
293
|
+
case VarType.DYNAMIC:
|
|
294
|
+
{
|
|
295
|
+
score += 2;
|
|
296
|
+
if (v == null) {
|
|
297
|
+
if (itemSpec.var.repeat) {
|
|
298
|
+
if (Named[itemSpec.prefix]) {
|
|
299
|
+
value = EMPTY_OBJ;
|
|
300
|
+
} else {
|
|
301
|
+
value = EMPTY_ARR;
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
value = null;
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
value = v;
|
|
308
|
+
if (itemSpec.var.repeat) {
|
|
309
|
+
value = value.split(SeparatorMap[itemSpec.prefix]).map((x => formatElement(x, itemSpec.var)));
|
|
310
|
+
} else {
|
|
311
|
+
value = formatElement(value, itemSpec.var);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
params.push([ itemSpec.var.name, value ]);
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
|
|
318
|
+
case VarType.STATIC:
|
|
319
|
+
{
|
|
320
|
+
score += 3;
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
score,
|
|
328
|
+
params: Object.fromEntries(params)
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function formatElement(str, varSpec) {
|
|
336
|
+
if (str == null) return null;
|
|
337
|
+
let out = decodeURIComponent(str);
|
|
338
|
+
if (varSpec.length != null) {
|
|
339
|
+
out = out.slice(0, varSpec.length);
|
|
340
|
+
}
|
|
341
|
+
if (varSpec.func === "int") {
|
|
342
|
+
out = Number(out);
|
|
343
|
+
}
|
|
344
|
+
return out;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function mapValues(obj, callback) {
|
|
348
|
+
return Object.fromEntries(Object.entries(obj).map((([key, value]) => [ key, callback(value) ])));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function parseParams(queryString, separator) {
|
|
352
|
+
const pairs = queryString.split(separator);
|
|
353
|
+
return Object.fromEntries(pairs.map((pair => {
|
|
354
|
+
const idx = pair.indexOf("=");
|
|
355
|
+
if (idx === -1) {
|
|
356
|
+
return [ pair, null ];
|
|
357
|
+
}
|
|
358
|
+
const key = pair.slice(0, idx);
|
|
359
|
+
const value = pair.slice(idx + 1);
|
|
360
|
+
return [ key, value ];
|
|
361
|
+
})));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const EMPTY_OBJ = Object.freeze(Object.create(null));
|
|
365
|
+
|
|
366
|
+
const EMPTY_ARR = Object.freeze([]);
|
|
367
|
+
|
|
368
|
+
function isEmpty(x) {
|
|
369
|
+
return x == null || x === "" || (Array.isArray(x) ? !x.length : typeof x === "object" && !Object.keys(x).length);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function escapeRegExp(string) {
|
|
373
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function percentEncodeRegExp(x, reserved, length) {
|
|
377
|
+
if (typeof x === "number") {
|
|
378
|
+
return String(x);
|
|
379
|
+
}
|
|
380
|
+
if (x === true) return "1";
|
|
381
|
+
if (x === false) return "0";
|
|
382
|
+
if (x === null) return "";
|
|
383
|
+
if (length != null) {
|
|
384
|
+
x = x.slice(0, length);
|
|
385
|
+
}
|
|
386
|
+
if (reserved) {
|
|
387
|
+
return x.replace(/%[0-9a-fA-F]{2}|./gsu, (m => {
|
|
388
|
+
let v = PERCENT_RE.test(m) ? decodeURIComponent(m) : m;
|
|
389
|
+
if (UR_SET.test(v)) {
|
|
390
|
+
return percentEncode(v);
|
|
391
|
+
}
|
|
392
|
+
return m;
|
|
393
|
+
}));
|
|
394
|
+
}
|
|
395
|
+
return x.replace(UNRESERVED, percentEncode);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const UTF8_ENCODER = new TextEncoder;
|
|
399
|
+
|
|
400
|
+
function percentEncode(str) {
|
|
401
|
+
return Array.from(UTF8_ENCODER.encode(str)).map((i => "%" + i.toString(16).toUpperCase().padStart(2, "0"))).join("");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export { UriTemplate };
|
package/dist/dev.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/log.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function log(...vars: any): void;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
declare const STR: unique symbol;
|
|
2
|
+
declare const VAR: unique symbol;
|
|
3
|
+
interface Placeholder {
|
|
4
|
+
type: typeof VAR;
|
|
5
|
+
prefix: string;
|
|
6
|
+
vars: VarSpec[];
|
|
7
|
+
}
|
|
8
|
+
interface StringLiteral {
|
|
9
|
+
type: typeof STR;
|
|
10
|
+
value: string;
|
|
11
|
+
}
|
|
12
|
+
type TemplateParts = Array<Placeholder | StringLiteral>;
|
|
13
|
+
interface VarSpec {
|
|
14
|
+
name: string;
|
|
15
|
+
length: number | null;
|
|
16
|
+
func: string | null;
|
|
17
|
+
repeat: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare enum VarType {
|
|
20
|
+
NAMED = 0,
|
|
21
|
+
STATIC = 1,
|
|
22
|
+
DYNAMIC = 2
|
|
23
|
+
}
|
|
24
|
+
type MapItem = {
|
|
25
|
+
type: VarType.DYNAMIC;
|
|
26
|
+
var: VarSpec;
|
|
27
|
+
prefix: string;
|
|
28
|
+
} | {
|
|
29
|
+
type: VarType.NAMED;
|
|
30
|
+
vars: VarSpec[];
|
|
31
|
+
prefix: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: VarType.STATIC;
|
|
34
|
+
};
|
|
35
|
+
export declare class UriTemplate {
|
|
36
|
+
expandParts: TemplateParts;
|
|
37
|
+
matchRegex: RegExp;
|
|
38
|
+
matchMap: Map<string, MapItem>;
|
|
39
|
+
constructor(template: string);
|
|
40
|
+
expand(variables: Record<string, UrlParamValue>): string;
|
|
41
|
+
match(url: string): Match | null;
|
|
42
|
+
}
|
|
43
|
+
export type UrlParamValue = string | number | string[] | number[] | Record<string, string | number | null>;
|
|
44
|
+
interface Match {
|
|
45
|
+
score: number;
|
|
46
|
+
params: Record<string, UrlParamValue | null>;
|
|
47
|
+
}
|
|
48
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mpen/rerouter",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"packageManager": "bun",
|
|
5
|
+
"exports": "./dist/bundle.cjs",
|
|
6
|
+
"module": "./dist/bundle.mjs",
|
|
7
|
+
"types": "./dist/bundle.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"./dist"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@rollup/plugin-commonjs": "^25.0.4",
|
|
14
|
+
"@rollup/plugin-node-resolve": "^15.2.1",
|
|
15
|
+
"@rollup/plugin-replace": "^5.0.2",
|
|
16
|
+
"@rollup/plugin-terser": "^0.4.3",
|
|
17
|
+
"@rollup/plugin-typescript": "^11.1.3",
|
|
18
|
+
"bun-types": "^1.0.2",
|
|
19
|
+
"chalk": "^5.3.0",
|
|
20
|
+
"npm-run-all": "^4.1.5",
|
|
21
|
+
"rimraf": "^5.0.1",
|
|
22
|
+
"rollup": "^3.29.2",
|
|
23
|
+
"tslib": "^2.6.2",
|
|
24
|
+
"typescript": "^5.2.2"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=10"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test": "bun test --watch src",
|
|
32
|
+
"dev": "bun run --hot src/dev.ts",
|
|
33
|
+
"bundle:clean": "rimraf -g \"dist/*\"",
|
|
34
|
+
"bundle:build": "rollup -c",
|
|
35
|
+
"bundle": "run-s bundle:clean bundle:build",
|
|
36
|
+
"patch": "npm version patch && VER=$(jq -r '.version' package.json) && hg ci -m \"Publish v$VER\" && hg tag \"v$VER\"",
|
|
37
|
+
"release": "run-s bundle patch && npm publish && hg push"
|
|
38
|
+
}
|
|
39
|
+
}
|