Repository navigation
Expand file tree
/
Copy pathuri-pattern.js
More file actions
348 lines (302 loc) · 9.14 KB
/
Copy pathuri-pattern.js
File metadata and controls
348 lines (302 loc) · 9.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import url from 'url';
/**
* Match and capture parts of a URI, like a specialized dialect of regular expression. This is used by PaneItem to
* describe URIs that should launch specific panes.
*
* URI patterns used `{name}` placeholders to match any non-empty path segment or URI part (host, protocol) and capture
* it as a parameter called "name". Any segment that is not recognized as a parameter will match exactly.
*
* Examples:
*
* `atom-github://hostname/exact/path?p0=value&p1=value` contains no parameters, so it will match _only_ that exact URL,
* including query parameters and their values. Extra query parameters or a fragment (`#`) will cause the match to fail.
*
* `atom-github://hostname/path/{name}/fragment` will match and capture any second path segment.
* * `atom-github://hostname/path/one/fragment` will match with `{name: 'one'}`
* * `atom-github://hostname/path/two/fragment` will match with `{name: 'two'}`
* * `atom-github://hostname/path/fragment` will not.
*
* `atom-github://hostname/root/{segments...}` will capture any number of path segments as an array. For example,
* * `atom-github://hostname/root/foo/bar/baz/` will match with `{segments: ['foo', 'bar', 'baz']}`.
* * `atom-github://hostname/root/single` will match with `{segments: ['single']}`; even a single segment will be
* matched as an array.
* * `atom-github://hostname/root/` will match with `{segments: []}`.
*
* Query parameters and their values may be captured. Given: `atom-github://hostname?q={value}`
* * `atom-github://hostname?q=foo` will match with `{value: 'foo'}`.
* * `atom-github://hostname?q=one&q=two` will _not_ match.
*
* To match multiple query parameters, use a splat parameter. Given: `atom-github://hostname?q={value...}`
* * `atom-github://hostname?q=one&q=two` will match with `{value: ['one', 'two']}`.
* * `atom-github://hostname?q=single` will match with `{value: ['single']}`.
* * `atom-github://hostname` will match with `{value: []}`.
*
* Protocol, username, password, or hostname may also contain capture expressions: `{p}://hostname`,
* `foo://me:{password}@hostname`.
*/
export default class URIPattern {
constructor(string) {
this.original = string;
const parsed = url.parse(dashEscape(string), true);
this.parts = {
protocol: asPart(parsed.protocol, '', ':'),
auth: splitAuth(parsed.auth, asPart),
hostname: asPart(parsed.hostname),
port: asPart(parsed.port),
pathname: (parsed.pathname || '').split('/').slice(1).map(segment => asPart(segment)),
query: Object.keys(parsed.query).reduce(
(acc, current) => {
acc[current] = asPart(parsed.query[current]);
return acc;
},
{},
),
hash: asPart(parsed.hash, '#', ''),
};
}
matches(string) {
if (string === undefined || string === null) {
return nonURIMatch;
}
const other = url.parse(string, true);
const params = {};
// direct matches
for (const attr of ['protocol', 'hostname', 'port', 'hash']) {
if (!this.parts[attr].matchesIn(params, other[attr])) {
return nonURIMatch;
}
}
// auth
const auth = splitAuth(other.auth);
if (!this.parts.auth.username.matchesIn(params, auth.username)) {
return nonURIMatch;
}
if (!this.parts.auth.password.matchesIn(params, auth.password)) {
return nonURIMatch;
}
// pathname
const pathParts = (other.pathname || '').split('/').filter(p => p.length > 0);
let mineInd = 0;
let yoursInd = 0;
while (mineInd < this.parts.pathname.length && yoursInd < pathParts.length) {
const mine = this.parts.pathname[mineInd];
const yours = pathParts[yoursInd];
if (!mine.matchesIn(params, yours)) {
return nonURIMatch;
} else {
if (!mine.isSplat()) {
mineInd++;
}
yoursInd++;
}
}
while (mineInd < this.parts.pathname.length) {
const part = this.parts.pathname[mineInd];
if (!part.matchesEmptyIn(params)) {
return nonURIMatch;
}
mineInd++;
}
if (yoursInd !== pathParts.length) {
return nonURIMatch;
}
// query string
const remaining = new Set(Object.keys(this.parts.query));
for (const k in other.query) {
const yours = other.query[k];
remaining.delete(k);
const mine = this.parts.query[k];
if (mine === undefined) {
return nonURIMatch;
}
const allYours = yours instanceof Array ? yours : [yours];
for (const each of allYours) {
if (!mine.matchesIn(params, each)) {
return nonURIMatch;
}
}
}
for (const k of remaining) {
const part = this.parts.query[k];
if (!part.matchesEmptyIn(params)) {
return nonURIMatch;
}
}
return new URIMatch(string, params);
}
// Access the original string used to create this pattern.
getOriginal() {
return this.original;
}
toString() {
return `<URIPattern ${this.original}>`;
}
}
/**
* Pattern component that matches its corresponding segment exactly.
*/
class ExactPart {
constructor(string) {
this.string = string;
}
matchesIn(params, other) {
return other === this.string;
}
matchesEmptyIn(params) {
return false;
}
isSplat() {
return false;
}
}
/**
* Pattern component that matches and captures any non-empty corresponding segment within a URI.
*/
class CapturePart {
constructor(name, splat, prefix, suffix) {
this.name = name;
this.splat = splat;
this.prefix = prefix;
this.suffix = suffix;
}
matchesIn(params, other) {
if (this.prefix.length > 0 && other.startsWith(this.prefix)) {
other = other.slice(this.prefix.length);
}
if (this.suffix.length > 0 && other.endsWith(this.suffix)) {
other = other.slice(0, -this.suffix.length);
}
other = decodeURIComponent(other);
if (this.name.length > 0) {
if (this.splat) {
if (params[this.name] === undefined) {
params[this.name] = [other];
} else {
params[this.name].push(other);
}
} else {
if (params[this.name] !== undefined) {
return false;
}
params[this.name] = other;
}
}
return true;
}
matchesEmptyIn(params) {
if (this.splat) {
if (params[this.name] === undefined) {
params[this.name] = [];
}
return true;
}
return false;
}
isSplat() {
return this.splat;
}
}
/**
* Including `{}` characters in certain URI components (hostname, protocol) cause `url.parse()` to lump everything into
* the `pathname`. Escape brackets from a pattern with `-a` and `-z`, and literal dashes with `--`.
*/
function dashEscape(raw) {
return raw.replace(/[{}-]/g, ch => {
if (ch === '{') {
return '-a';
} else if (ch === '}') {
return '-z';
} else {
return '--';
}
});
}
/**
* Reverse the escaping performed by `dashEscape` by un-doubling `-` characters.
*/
function dashUnescape(escaped) {
return escaped.replace(/--/g, '-');
}
/**
* Parse a URI pattern component as either an `ExactPart` or a `CapturePart`. Recognize captures ending with `...` as
* splat captures that can consume zero to many components.
*/
function asPart(patternSegment, prefix = '', suffix = '') {
if (patternSegment === null) {
return new ExactPart(null);
}
let subPattern = patternSegment;
if (prefix.length > 0 && subPattern.startsWith(prefix)) {
subPattern = subPattern.slice(prefix.length);
}
if (suffix.length > 0 && subPattern.endsWith(suffix)) {
subPattern = subPattern.slice(0, -suffix.length);
}
if (subPattern.startsWith('-a') && subPattern.endsWith('-z')) {
const splat = subPattern.endsWith('...-z');
if (splat) {
subPattern = subPattern.slice(2, -5);
} else {
subPattern = subPattern.slice(2, -2);
}
return new CapturePart(dashUnescape(subPattern), splat, prefix, suffix);
} else {
return new ExactPart(dashUnescape(patternSegment));
}
}
/**
* Split the `.auth` field into username and password subcomponent.
*/
function splitAuth(auth, fn = x => x) {
if (auth === null) {
return {username: fn(null), password: fn(null)};
}
const ind = auth.indexOf(':');
return ind !== -1
? {username: fn(auth.slice(0, ind)), password: fn(auth.slice(ind + 1))}
: {username: fn(auth), password: fn(null)};
}
/**
* Memorialize a successful match between a URI and a URIPattern, including any parameters that have been captured.
*/
class URIMatch {
constructor(uri, params) {
this.uri = uri;
this.params = params;
}
ok() {
return true;
}
getURI() {
return this.uri;
}
getParams() {
return this.params;
}
toString() {
let s = '<URIMatch ok';
for (const k in this.params) {
s += ` ${k}="${this.params[k]}"`;
}
s += '>';
return s;
}
}
/**
* Singleton object that memorializes an unsuccessful match between a URIPattern and an URI. Matches the API of a
* URIMatch, but returns false for ok() and so on.
*/
export const nonURIMatch = {
ok() {
return false;
},
getURI() {
return undefined;
},
getParams() {
return {};
},
toString() {
return '<nonURIMatch>';
},
};