-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
315 lines (281 loc) · 8.84 KB
/
mod.ts
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
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// Copyright 2021 the denosaurs team. All rights reserved. MIT license.
// deno-lint-ignore-file
import init, {
source,
urlpattern_parse as urlpatternParse,
urlpattern_process_match_input as urlpatternProcessMatchInput,
} from "./wasm/wasm.js";
import * as webidl from "./webidl.js";
await init(source);
export interface URLPatternInit {
protocol?: string;
username?: string;
password?: string;
hostname?: string;
port?: string;
pathname?: string;
search?: string;
hash?: string;
baseURL?: string;
}
export type URLPatternInput = string | URLPatternInit;
export interface URLPatternComponentResult {
input: string;
groups: Record<string, string>;
}
/** `URLPatternResult` is the object returned from `URLPattern.match`. */
export interface URLPatternResult {
/** The inputs provided when matching. */
inputs: [URLPatternInit] | [URLPatternInit, string];
/** The matched result for the `protocol` matcher. */
protocol: URLPatternComponentResult;
/** The matched result for the `username` matcher. */
username: URLPatternComponentResult;
/** The matched result for the `password` matcher. */
password: URLPatternComponentResult;
/** The matched result for the `hostname` matcher. */
hostname: URLPatternComponentResult;
/** The matched result for the `port` matcher. */
port: URLPatternComponentResult;
/** The matched result for the `pathname` matcher. */
pathname: URLPatternComponentResult;
/** The matched result for the `search` matcher. */
search: URLPatternComponentResult;
/** The matched result for the `hash` matcher. */
hash: URLPatternComponentResult;
}
const _components = Symbol("components");
/**
* The URLPattern API provides a web platform primitive for matching URLs based
* on a convenient pattern syntax.
*
* The syntax is based on path-to-regexp. Wildcards, named capture groups,
* regular groups, and group modifiers are all supported.
*
* ```ts
* // Specify the pattern as structured data.
* const pattern = new URLPattern({ pathname: "/users/:user" });
* const match = pattern.match("/users/joe");
* console.log(match.pathname.groups.user); // joe
* ```
*
* ```ts
* // Specify a fully qualified string pattern.
* const pattern = new URLPattern("https://example.com/books/:id");
* console.log(pattern.test("https://example.com/books/123")); // true
* console.log(pattern.test("https://deno.land/books/123")); // false
* ```
*
* ```ts
* // Specify a relative string pattern with a base URL.
* const pattern = new URLPattern("/:article", "https://blog.example.com");
* console.log(pattern.test("https://blog.example.com/article")); // true
* console.log(pattern.test("https://blog.example.com/article/123")); // false
* ```
*/
export class URLPattern {
[webidl.brand]: symbol;
[_components]: any;
constructor(input: URLPatternInput, baseURL: string | undefined = undefined) {
this[webidl.brand] = webidl.brand;
const prefix = "Failed to construct 'URLPattern'";
webidl.requiredArguments(arguments.length, 1, { prefix });
input = webidl.converters.URLPatternInput(input, {
prefix,
context: "Argument 1",
});
if (baseURL !== undefined) {
baseURL = webidl.converters.USVString(baseURL, {
prefix,
context: "Argument 2",
});
}
const components = urlpatternParse(input, baseURL);
for (const key of Object.keys(components)) {
try {
components[key].regexp = new RegExp(
components[key].regexpString,
"u",
);
} catch (e) {
throw new TypeError(`${prefix}: ${key} is invalid; ${e.message}`);
}
}
this[_components] = components;
}
/** The pattern string for the `protocol`. */
get protocol() {
webidl.assertBranded(this, URLPattern);
return this[_components].protocol.patternString;
}
/** The pattern string for the `username`. */
get username() {
webidl.assertBranded(this, URLPattern);
return this[_components].username.patternString;
}
/** The pattern string for the `password`. */
get password() {
webidl.assertBranded(this, URLPattern);
return this[_components].password.patternString;
}
/** The pattern string for the `hostname`. */
get hostname() {
webidl.assertBranded(this, URLPattern);
return this[_components].hostname.patternString;
}
/** The pattern string for the `port`. */
get port() {
webidl.assertBranded(this, URLPattern);
return this[_components].port.patternString;
}
/** The pattern string for the `pathname`. */
get pathname() {
webidl.assertBranded(this, URLPattern);
return this[_components].pathname.patternString;
}
/** The pattern string for the `search`. */
get search() {
webidl.assertBranded(this, URLPattern);
return this[_components].search.patternString;
}
/** The pattern string for the `hash`. */
get hash() {
webidl.assertBranded(this, URLPattern);
return this[_components].hash.patternString;
}
/**
* Test if the given input matches the stored pattern.
*
* The input can either be provided as a url string (with an optional base),
* or as individual components in the form of an object.
*
* ```ts
* const pattern = new URLPattern("https://example.com/books/:id");
*
* // Test a url string.
* console.log(pattern.test("https://example.com/books/123")); // true
*
* // Test a relative url with a base.
* console.log(pattern.test("/books/123", "https://example.com")); // true
*
* // Test an object of url components.
* console.log(pattern.test({ pathname: "/books/123" })); // true
* ```
*/
test(
input: URLPatternInput,
baseURL: string | undefined = undefined,
): boolean {
webidl.assertBranded(this, URLPattern);
const prefix = "Failed to execute 'test' on 'URLPattern'";
webidl.requiredArguments(arguments.length, 1, { prefix });
input = webidl.converters.URLPatternInput(input, {
prefix,
context: "Argument 1",
});
if (baseURL !== undefined) {
baseURL = webidl.converters.USVString(baseURL, {
prefix,
context: "Argument 2",
});
}
const res = urlpatternProcessMatchInput(
input,
baseURL,
);
if (res === null) {
return false;
}
const [values] = res;
for (const key of Object.keys(values)) {
if (!this[_components][key].regexp.test(values[key])) {
return false;
}
}
return true;
}
/**
* Match the given input against the stored pattern.
*
* The input can either be provided as a url string (with an optional base),
* or as individual components in the form of an object.
*
* ```ts
* const pattern = new URLPattern("https://example.com/books/:id");
*
* // Match a url string.
* let match = pattern.match("https://example.com/books/123");
* console.log(match.pathname.groups.id); // 123
*
* // Match a relative url with a base.
* match = pattern.match("/books/123", "https://example.com");
* console.log(match.pathname.groups.id); // 123
*
* // Match an object of url components.
* match = pattern.match({ pathname: "/books/123" });
* console.log(match.pathname.groups.id); // 123
* ```
*/
exec(
input: URLPatternInput,
baseURL: string | undefined = undefined,
): URLPatternResult | null {
webidl.assertBranded(this, URLPattern);
const prefix = "Failed to execute 'exec' on 'URLPattern'";
webidl.requiredArguments(arguments.length, 1, { prefix });
input = webidl.converters.URLPatternInput(input, {
prefix,
context: "Argument 1",
});
if (baseURL !== undefined) {
baseURL = webidl.converters.USVString(baseURL, {
prefix,
context: "Argument 2",
});
}
const res = urlpatternProcessMatchInput(
input,
baseURL,
);
if (res === null) {
return null;
}
const [values, inputs] = res;
if (inputs[1] === null) {
inputs.pop();
}
const result: any = { inputs };
for (const key of Object.keys(values)) {
const component = this[_components][key];
const input = values[key];
const match = component.regexp.exec(input);
if (match === null) {
return null;
}
const groupEntries = component.groupNameList.map(
(name: string, i: number) => [name, match[i + 1] ?? ""],
);
const groups = Object.fromEntries(groupEntries);
result[key] = {
input,
groups,
};
}
return result;
}
[Symbol.for("Deno.customInspect")](inspect: (a: unknown) => string) {
return `URLPattern ${
inspect({
protocol: this.protocol,
username: this.username,
password: this.password,
hostname: this.hostname,
port: this.port,
pathname: this.pathname,
search: this.search,
hash: this.hash,
})
}`;
}
}