@serwist/routing 9.0.0-preview.17 → 9.0.0-preview.18
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/dist/index.d.ts +2 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -77
- package/package.json +3 -14
- package/src/index.ts +2 -22
- package/dist/NavigationRoute.d.ts +0 -57
- package/dist/NavigationRoute.d.ts.map +0 -1
- package/dist/RegExpRoute.d.ts +0 -24
- package/dist/RegExpRoute.d.ts.map +0 -1
- package/dist/Route.d.ts +0 -33
- package/dist/Route.d.ts.map +0 -1
- package/dist/Router.d.ts +0 -124
- package/dist/Router.d.ts.map +0 -1
- package/dist/_types.d.ts +0 -48
- package/dist/_types.d.ts.map +0 -1
- package/dist/chunks/parseRoute.js +0 -401
- package/dist/index.internal.d.ts +0 -4
- package/dist/index.internal.d.ts.map +0 -1
- package/dist/index.internal.js +0 -2
- package/dist/registerRoute.d.ts +0 -15
- package/dist/registerRoute.d.ts.map +0 -1
- package/dist/setCatchHandler.d.ts +0 -9
- package/dist/setCatchHandler.d.ts.map +0 -1
- package/dist/setDefaultHandler.d.ts +0 -12
- package/dist/setDefaultHandler.d.ts.map +0 -1
- package/dist/unregisterRoute.d.ts +0 -8
- package/dist/unregisterRoute.d.ts.map +0 -1
- package/dist/utils/constants.d.ts +0 -15
- package/dist/utils/constants.d.ts.map +0 -1
- package/dist/utils/getOrCreateDefaultRouter.d.ts +0 -10
- package/dist/utils/getOrCreateDefaultRouter.d.ts.map +0 -1
- package/dist/utils/normalizeHandler.d.ts +0 -10
- package/dist/utils/normalizeHandler.d.ts.map +0 -1
- package/dist/utils/parseRoute.d.ts +0 -5
- package/dist/utils/parseRoute.d.ts.map +0 -1
- package/src/NavigationRoute.ts +0 -119
- package/src/RegExpRoute.ts +0 -74
- package/src/Route.ts +0 -68
- package/src/Router.ts +0 -432
- package/src/_types.ts +0 -62
- package/src/index.internal.ts +0 -4
- package/src/registerRoute.ts +0 -33
- package/src/setCatchHandler.ts +0 -22
- package/src/setDefaultHandler.ts +0 -25
- package/src/unregisterRoute.ts +0 -12
- package/src/utils/constants.ts +0 -24
- package/src/utils/getOrCreateDefaultRouter.ts +0 -29
- package/src/utils/normalizeHandler.ts +0 -40
- package/src/utils/parseRoute.ts +0 -67
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/internal';
|
|
2
|
-
|
|
3
|
-
const defaultMethod = "GET";
|
|
4
|
-
const validMethods = [
|
|
5
|
-
"DELETE",
|
|
6
|
-
"GET",
|
|
7
|
-
"HEAD",
|
|
8
|
-
"PATCH",
|
|
9
|
-
"POST",
|
|
10
|
-
"PUT"
|
|
11
|
-
];
|
|
12
|
-
|
|
13
|
-
const normalizeHandler = (handler)=>{
|
|
14
|
-
if (handler && typeof handler === "object") {
|
|
15
|
-
if (process.env.NODE_ENV !== "production") {
|
|
16
|
-
assert.hasMethod(handler, "handle", {
|
|
17
|
-
moduleName: "@serwist/routing",
|
|
18
|
-
className: "Route",
|
|
19
|
-
funcName: "constructor",
|
|
20
|
-
paramName: "handler"
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return handler;
|
|
24
|
-
}
|
|
25
|
-
if (process.env.NODE_ENV !== "production") {
|
|
26
|
-
assert.isType(handler, "function", {
|
|
27
|
-
moduleName: "@serwist/routing",
|
|
28
|
-
className: "Route",
|
|
29
|
-
funcName: "constructor",
|
|
30
|
-
paramName: "handler"
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
34
|
-
handle: handler
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
class Route {
|
|
39
|
-
handler;
|
|
40
|
-
match;
|
|
41
|
-
method;
|
|
42
|
-
catchHandler;
|
|
43
|
-
constructor(match, handler, method = defaultMethod){
|
|
44
|
-
if (process.env.NODE_ENV !== "production") {
|
|
45
|
-
assert.isType(match, "function", {
|
|
46
|
-
moduleName: "@serwist/routing",
|
|
47
|
-
className: "Route",
|
|
48
|
-
funcName: "constructor",
|
|
49
|
-
paramName: "match"
|
|
50
|
-
});
|
|
51
|
-
if (method) {
|
|
52
|
-
assert.isOneOf(method, validMethods, {
|
|
53
|
-
paramName: "method"
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
this.handler = normalizeHandler(handler);
|
|
58
|
-
this.match = match;
|
|
59
|
-
this.method = method;
|
|
60
|
-
}
|
|
61
|
-
setCatchHandler(handler) {
|
|
62
|
-
this.catchHandler = normalizeHandler(handler);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
class RegExpRoute extends Route {
|
|
67
|
-
constructor(regExp, handler, method){
|
|
68
|
-
if (process.env.NODE_ENV !== "production") {
|
|
69
|
-
assert.isInstance(regExp, RegExp, {
|
|
70
|
-
moduleName: "@serwist/routing",
|
|
71
|
-
className: "RegExpRoute",
|
|
72
|
-
funcName: "constructor",
|
|
73
|
-
paramName: "pattern"
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
const match = ({ url })=>{
|
|
77
|
-
const result = regExp.exec(url.href);
|
|
78
|
-
if (!result) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (url.origin !== location.origin && result.index !== 0) {
|
|
82
|
-
if (process.env.NODE_ENV !== "production") {
|
|
83
|
-
logger.debug(`The regular expression '${regExp.toString()}' only partially matched against the cross-origin URL '${url.toString()}'. RegExpRoute's will only handle cross-origin requests if they match the entire URL.`);
|
|
84
|
-
}
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
return result.slice(1);
|
|
88
|
-
};
|
|
89
|
-
super(match, handler, method);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
class Router {
|
|
94
|
-
_routes;
|
|
95
|
-
_defaultHandlerMap;
|
|
96
|
-
_catchHandler;
|
|
97
|
-
constructor(){
|
|
98
|
-
this._routes = new Map();
|
|
99
|
-
this._defaultHandlerMap = new Map();
|
|
100
|
-
}
|
|
101
|
-
get routes() {
|
|
102
|
-
return this._routes;
|
|
103
|
-
}
|
|
104
|
-
addFetchListener() {
|
|
105
|
-
self.addEventListener("fetch", (event)=>{
|
|
106
|
-
const { request } = event;
|
|
107
|
-
const responsePromise = this.handleRequest({
|
|
108
|
-
request,
|
|
109
|
-
event
|
|
110
|
-
});
|
|
111
|
-
if (responsePromise) {
|
|
112
|
-
event.respondWith(responsePromise);
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
addCacheListener() {
|
|
117
|
-
self.addEventListener("message", (event)=>{
|
|
118
|
-
if (event.data && event.data.type === "CACHE_URLS") {
|
|
119
|
-
const { payload } = event.data;
|
|
120
|
-
if (process.env.NODE_ENV !== "production") {
|
|
121
|
-
logger.debug("Caching URLs from the window", payload.urlsToCache);
|
|
122
|
-
}
|
|
123
|
-
const requestPromises = Promise.all(payload.urlsToCache.map((entry)=>{
|
|
124
|
-
if (typeof entry === "string") {
|
|
125
|
-
entry = [
|
|
126
|
-
entry
|
|
127
|
-
];
|
|
128
|
-
}
|
|
129
|
-
const request = new Request(...entry);
|
|
130
|
-
return this.handleRequest({
|
|
131
|
-
request,
|
|
132
|
-
event
|
|
133
|
-
});
|
|
134
|
-
}));
|
|
135
|
-
event.waitUntil(requestPromises);
|
|
136
|
-
if (event.ports?.[0]) {
|
|
137
|
-
void requestPromises.then(()=>event.ports[0].postMessage(true));
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
handleRequest({ request, event }) {
|
|
143
|
-
if (process.env.NODE_ENV !== "production") {
|
|
144
|
-
assert.isInstance(request, Request, {
|
|
145
|
-
moduleName: "@serwist/routing",
|
|
146
|
-
className: "Router",
|
|
147
|
-
funcName: "handleRequest",
|
|
148
|
-
paramName: "options.request"
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
const url = new URL(request.url, location.href);
|
|
152
|
-
if (!url.protocol.startsWith("http")) {
|
|
153
|
-
if (process.env.NODE_ENV !== "production") {
|
|
154
|
-
logger.debug("The Serwist router only supports URLs that start with 'http'.");
|
|
155
|
-
}
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const sameOrigin = url.origin === location.origin;
|
|
159
|
-
const { params, route } = this.findMatchingRoute({
|
|
160
|
-
event,
|
|
161
|
-
request,
|
|
162
|
-
sameOrigin,
|
|
163
|
-
url
|
|
164
|
-
});
|
|
165
|
-
let handler = route?.handler;
|
|
166
|
-
const debugMessages = [];
|
|
167
|
-
if (process.env.NODE_ENV !== "production") {
|
|
168
|
-
if (handler) {
|
|
169
|
-
debugMessages.push([
|
|
170
|
-
"Found a route to handle this request:",
|
|
171
|
-
route
|
|
172
|
-
]);
|
|
173
|
-
if (params) {
|
|
174
|
-
debugMessages.push([
|
|
175
|
-
`Passing the following params to the route's handler:`,
|
|
176
|
-
params
|
|
177
|
-
]);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
const method = request.method;
|
|
182
|
-
if (!handler && this._defaultHandlerMap.has(method)) {
|
|
183
|
-
if (process.env.NODE_ENV !== "production") {
|
|
184
|
-
debugMessages.push(`Failed to find a matching route. Falling back to the default handler for ${method}.`);
|
|
185
|
-
}
|
|
186
|
-
handler = this._defaultHandlerMap.get(method);
|
|
187
|
-
}
|
|
188
|
-
if (!handler) {
|
|
189
|
-
if (process.env.NODE_ENV !== "production") {
|
|
190
|
-
logger.debug(`No route found for: ${getFriendlyURL(url)}`);
|
|
191
|
-
}
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
if (process.env.NODE_ENV !== "production") {
|
|
195
|
-
logger.groupCollapsed(`Router is responding to: ${getFriendlyURL(url)}`);
|
|
196
|
-
for (const msg of debugMessages){
|
|
197
|
-
if (Array.isArray(msg)) {
|
|
198
|
-
logger.log(...msg);
|
|
199
|
-
} else {
|
|
200
|
-
logger.log(msg);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
logger.groupEnd();
|
|
204
|
-
}
|
|
205
|
-
let responsePromise;
|
|
206
|
-
try {
|
|
207
|
-
responsePromise = handler.handle({
|
|
208
|
-
url,
|
|
209
|
-
request,
|
|
210
|
-
event,
|
|
211
|
-
params
|
|
212
|
-
});
|
|
213
|
-
} catch (err) {
|
|
214
|
-
responsePromise = Promise.reject(err);
|
|
215
|
-
}
|
|
216
|
-
const catchHandler = route?.catchHandler;
|
|
217
|
-
if (responsePromise instanceof Promise && (this._catchHandler || catchHandler)) {
|
|
218
|
-
responsePromise = responsePromise.catch(async (err)=>{
|
|
219
|
-
if (catchHandler) {
|
|
220
|
-
if (process.env.NODE_ENV !== "production") {
|
|
221
|
-
logger.groupCollapsed(`Error thrown when responding to: ${getFriendlyURL(url)}. Falling back to route's Catch Handler.`);
|
|
222
|
-
logger.error("Error thrown by:", route);
|
|
223
|
-
logger.error(err);
|
|
224
|
-
logger.groupEnd();
|
|
225
|
-
}
|
|
226
|
-
try {
|
|
227
|
-
return await catchHandler.handle({
|
|
228
|
-
url,
|
|
229
|
-
request,
|
|
230
|
-
event,
|
|
231
|
-
params
|
|
232
|
-
});
|
|
233
|
-
} catch (catchErr) {
|
|
234
|
-
if (catchErr instanceof Error) {
|
|
235
|
-
err = catchErr;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
if (this._catchHandler) {
|
|
240
|
-
if (process.env.NODE_ENV !== "production") {
|
|
241
|
-
logger.groupCollapsed(`Error thrown when responding to: ${getFriendlyURL(url)}. Falling back to global Catch Handler.`);
|
|
242
|
-
logger.error("Error thrown by:", route);
|
|
243
|
-
logger.error(err);
|
|
244
|
-
logger.groupEnd();
|
|
245
|
-
}
|
|
246
|
-
return this._catchHandler.handle({
|
|
247
|
-
url,
|
|
248
|
-
request,
|
|
249
|
-
event
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
throw err;
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
return responsePromise;
|
|
256
|
-
}
|
|
257
|
-
findMatchingRoute({ url, sameOrigin, request, event }) {
|
|
258
|
-
const routes = this._routes.get(request.method) || [];
|
|
259
|
-
for (const route of routes){
|
|
260
|
-
let params;
|
|
261
|
-
const matchResult = route.match({
|
|
262
|
-
url,
|
|
263
|
-
sameOrigin,
|
|
264
|
-
request,
|
|
265
|
-
event
|
|
266
|
-
});
|
|
267
|
-
if (matchResult) {
|
|
268
|
-
if (process.env.NODE_ENV !== "production") {
|
|
269
|
-
if (matchResult instanceof Promise) {
|
|
270
|
-
logger.warn(`While routing ${getFriendlyURL(url)}, an async matchCallback function was used. Please convert the following route to use a synchronous matchCallback function:`, route);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
params = matchResult;
|
|
274
|
-
if (Array.isArray(params) && params.length === 0) {
|
|
275
|
-
params = undefined;
|
|
276
|
-
} else if (matchResult.constructor === Object && Object.keys(matchResult).length === 0) {
|
|
277
|
-
params = undefined;
|
|
278
|
-
} else if (typeof matchResult === "boolean") {
|
|
279
|
-
params = undefined;
|
|
280
|
-
}
|
|
281
|
-
return {
|
|
282
|
-
route,
|
|
283
|
-
params
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
return {};
|
|
288
|
-
}
|
|
289
|
-
setDefaultHandler(handler, method = defaultMethod) {
|
|
290
|
-
this._defaultHandlerMap.set(method, normalizeHandler(handler));
|
|
291
|
-
}
|
|
292
|
-
setCatchHandler(handler) {
|
|
293
|
-
this._catchHandler = normalizeHandler(handler);
|
|
294
|
-
}
|
|
295
|
-
registerRoute(route) {
|
|
296
|
-
if (process.env.NODE_ENV !== "production") {
|
|
297
|
-
assert.isType(route, "object", {
|
|
298
|
-
moduleName: "@serwist/routing",
|
|
299
|
-
className: "Router",
|
|
300
|
-
funcName: "registerRoute",
|
|
301
|
-
paramName: "route"
|
|
302
|
-
});
|
|
303
|
-
assert.hasMethod(route, "match", {
|
|
304
|
-
moduleName: "@serwist/routing",
|
|
305
|
-
className: "Router",
|
|
306
|
-
funcName: "registerRoute",
|
|
307
|
-
paramName: "route"
|
|
308
|
-
});
|
|
309
|
-
assert.isType(route.handler, "object", {
|
|
310
|
-
moduleName: "@serwist/routing",
|
|
311
|
-
className: "Router",
|
|
312
|
-
funcName: "registerRoute",
|
|
313
|
-
paramName: "route"
|
|
314
|
-
});
|
|
315
|
-
assert.hasMethod(route.handler, "handle", {
|
|
316
|
-
moduleName: "@serwist/routing",
|
|
317
|
-
className: "Router",
|
|
318
|
-
funcName: "registerRoute",
|
|
319
|
-
paramName: "route.handler"
|
|
320
|
-
});
|
|
321
|
-
assert.isType(route.method, "string", {
|
|
322
|
-
moduleName: "@serwist/routing",
|
|
323
|
-
className: "Router",
|
|
324
|
-
funcName: "registerRoute",
|
|
325
|
-
paramName: "route.method"
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
if (!this._routes.has(route.method)) {
|
|
329
|
-
this._routes.set(route.method, []);
|
|
330
|
-
}
|
|
331
|
-
this._routes.get(route.method).push(route);
|
|
332
|
-
}
|
|
333
|
-
unregisterRoute(route) {
|
|
334
|
-
if (!this._routes.has(route.method)) {
|
|
335
|
-
throw new SerwistError("unregister-route-but-not-found-with-method", {
|
|
336
|
-
method: route.method
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
const routeIndex = this._routes.get(route.method).indexOf(route);
|
|
340
|
-
if (routeIndex > -1) {
|
|
341
|
-
this._routes.get(route.method).splice(routeIndex, 1);
|
|
342
|
-
} else {
|
|
343
|
-
throw new SerwistError("unregister-route-route-not-registered");
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
let defaultRouter;
|
|
349
|
-
const getOrCreateDefaultRouter = ()=>{
|
|
350
|
-
if (!defaultRouter) {
|
|
351
|
-
defaultRouter = new Router();
|
|
352
|
-
defaultRouter.addFetchListener();
|
|
353
|
-
defaultRouter.addCacheListener();
|
|
354
|
-
}
|
|
355
|
-
return defaultRouter;
|
|
356
|
-
};
|
|
357
|
-
|
|
358
|
-
const parseRoute = (capture, handler, method)=>{
|
|
359
|
-
let route;
|
|
360
|
-
if (typeof capture === "string") {
|
|
361
|
-
const captureUrl = new URL(capture, location.href);
|
|
362
|
-
if (process.env.NODE_ENV !== "production") {
|
|
363
|
-
if (!(capture.startsWith("/") || capture.startsWith("http"))) {
|
|
364
|
-
throw new SerwistError("invalid-string", {
|
|
365
|
-
moduleName: "@serwist/routing",
|
|
366
|
-
funcName: "registerRoute",
|
|
367
|
-
paramName: "capture"
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
const valueToCheck = capture.startsWith("http") ? captureUrl.pathname : capture;
|
|
371
|
-
const wildcards = "[*:?+]";
|
|
372
|
-
if (new RegExp(`${wildcards}`).exec(valueToCheck)) {
|
|
373
|
-
logger.debug(`The '$capture' parameter contains an Express-style wildcard character (${wildcards}). Strings are now always interpreted as exact matches; use a RegExp for partial or wildcard matches.`);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
const matchCallback = ({ url })=>{
|
|
377
|
-
if (process.env.NODE_ENV !== "production") {
|
|
378
|
-
if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
|
|
379
|
-
logger.debug(`${capture} only partially matches the cross-origin URL ${url.toString()}. This route will only handle cross-origin requests if they match the entire URL.`);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
return url.href === captureUrl.href;
|
|
383
|
-
};
|
|
384
|
-
route = new Route(matchCallback, handler, method);
|
|
385
|
-
} else if (capture instanceof RegExp) {
|
|
386
|
-
route = new RegExpRoute(capture, handler, method);
|
|
387
|
-
} else if (typeof capture === "function") {
|
|
388
|
-
route = new Route(capture, handler, method);
|
|
389
|
-
} else if (capture instanceof Route) {
|
|
390
|
-
route = capture;
|
|
391
|
-
} else {
|
|
392
|
-
throw new SerwistError("unsupported-route-type", {
|
|
393
|
-
moduleName: "@serwist/routing",
|
|
394
|
-
funcName: "registerRoute",
|
|
395
|
-
paramName: "capture"
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
return route;
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
export { Route as R, RegExpRoute as a, Router as b, getOrCreateDefaultRouter as g, parseRoute as p };
|
package/dist/index.internal.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.internal.d.ts","sourceRoot":"","sources":["../src/index.internal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAC/E,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,OAAO,EAAE,UAAU,EAAE,wBAAwB,EAAE,CAAC"}
|
package/dist/index.internal.js
DELETED
package/dist/registerRoute.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { RouteHandler, RouteMatchCallback } from "@serwist/core";
|
|
2
|
-
import { Route } from "./Route.js";
|
|
3
|
-
import type { HTTPMethod } from "./utils/constants.js";
|
|
4
|
-
/**
|
|
5
|
-
* Registers a RegExp, string, or function with a caching
|
|
6
|
-
* strategy to a singleton Router instance.
|
|
7
|
-
*
|
|
8
|
-
* @param capture If the capture param is a `Route`, all other arguments will be ignored.
|
|
9
|
-
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
10
|
-
* This parameter is required if `capture` is not a `Route` object.
|
|
11
|
-
* @param method The HTTP method to match the Route against. Defaults to GET.
|
|
12
|
-
* @returns The generated `Route`.
|
|
13
|
-
*/
|
|
14
|
-
export declare const registerRoute: (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod) => Route;
|
|
15
|
-
//# sourceMappingURL=registerRoute.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"registerRoute.d.ts","sourceRoot":"","sources":["../src/registerRoute.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEtE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAIvD;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,YAAa,MAAM,GAAG,MAAM,GAAG,kBAAkB,GAAG,KAAK,YAAY,YAAY,WAAW,UAAU,KAAG,KAOlI,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { RouteHandler } from "@serwist/core";
|
|
2
|
-
/**
|
|
3
|
-
* If a Route throws an error while handling a request, this `handler`
|
|
4
|
-
* will be called and given a chance to provide a response.
|
|
5
|
-
*
|
|
6
|
-
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
7
|
-
*/
|
|
8
|
-
export declare const setCatchHandler: (handler: RouteHandler) => void;
|
|
9
|
-
//# sourceMappingURL=setCatchHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"setCatchHandler.d.ts","sourceRoot":"","sources":["../src/setCatchHandler.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,YAAa,YAAY,KAAG,IAGvD,CAAC"}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { RouteHandler } from "@serwist/core";
|
|
2
|
-
/**
|
|
3
|
-
* Defines a default `handler` that's called when no routes explicitly
|
|
4
|
-
* match the incoming request.
|
|
5
|
-
*
|
|
6
|
-
* Without a default handler, unmatched requests will go against the
|
|
7
|
-
* network as if there were no service worker present.
|
|
8
|
-
*
|
|
9
|
-
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
10
|
-
*/
|
|
11
|
-
export declare const setDefaultHandler: (handler: RouteHandler) => void;
|
|
12
|
-
//# sourceMappingURL=setDefaultHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"setDefaultHandler.d.ts","sourceRoot":"","sources":["../src/setDefaultHandler.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,YAAa,YAAY,KAAG,IAGzD,CAAC"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { Route } from "./Route.js";
|
|
2
|
-
/**
|
|
3
|
-
* Unregisters a route from the singleton Router instance.
|
|
4
|
-
*
|
|
5
|
-
* @param route The route to unregister.
|
|
6
|
-
*/
|
|
7
|
-
export declare const unregisterRoute: (route: Route) => void;
|
|
8
|
-
//# sourceMappingURL=unregisterRoute.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unregisterRoute.d.ts","sourceRoot":"","sources":["../src/unregisterRoute.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGxC;;;;GAIG;AACH,eAAO,MAAM,eAAe,UAAW,KAAK,KAAG,IAG9C,CAAC"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export type HTTPMethod = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";
|
|
2
|
-
/**
|
|
3
|
-
* The default HTTP method, 'GET', used when there's no specific method
|
|
4
|
-
* configured for a route.
|
|
5
|
-
*
|
|
6
|
-
* @private
|
|
7
|
-
*/
|
|
8
|
-
export declare const defaultMethod = "GET";
|
|
9
|
-
/**
|
|
10
|
-
* The list of valid HTTP methods associated with requests that could be routed.
|
|
11
|
-
*
|
|
12
|
-
* @private
|
|
13
|
-
*/
|
|
14
|
-
export declare const validMethods: ("DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT")[];
|
|
15
|
-
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;AAE9E;;;;;GAKG;AACH,eAAO,MAAM,aAAa,QAA6B,CAAC;AAExD;;;;GAIG;AACH,eAAO,MAAM,YAAY,0DAA2E,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Router } from "../Router.js";
|
|
2
|
-
/**
|
|
3
|
-
* Creates a new, singleton Router instance if one does not exist. If one
|
|
4
|
-
* does already exist, that instance is returned.
|
|
5
|
-
*
|
|
6
|
-
* @private
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
|
-
export declare const getOrCreateDefaultRouter: () => Router;
|
|
10
|
-
//# sourceMappingURL=getOrCreateDefaultRouter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getOrCreateDefaultRouter.d.ts","sourceRoot":"","sources":["../../src/utils/getOrCreateDefaultRouter.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAItC;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,QAAO,MAS3C,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { RouteHandler, RouteHandlerObject } from "@serwist/core";
|
|
2
|
-
/**
|
|
3
|
-
* @param handler Either a function, or an object with a
|
|
4
|
-
* 'handle' method.
|
|
5
|
-
* @returns An object with a handle method.
|
|
6
|
-
*
|
|
7
|
-
* @private
|
|
8
|
-
*/
|
|
9
|
-
export declare const normalizeHandler: (handler: RouteHandler) => RouteHandlerObject;
|
|
10
|
-
//# sourceMappingURL=normalizeHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeHandler.d.ts","sourceRoot":"","sources":["../../src/utils/normalizeHandler.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAGtE;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB,YAAa,YAAY,KAAG,kBAqBxD,CAAC"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { RouteHandler, RouteMatchCallback } from "@serwist/core";
|
|
2
|
-
import { Route } from "../Route.js";
|
|
3
|
-
import type { HTTPMethod } from "./constants.js";
|
|
4
|
-
export declare const parseRoute: (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod) => Route;
|
|
5
|
-
//# sourceMappingURL=parseRoute.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parseRoute.d.ts","sourceRoot":"","sources":["../../src/utils/parseRoute.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAItE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,eAAO,MAAM,UAAU,YAAa,MAAM,GAAG,MAAM,GAAG,kBAAkB,GAAG,KAAK,YAAY,YAAY,WAAW,UAAU,KAAG,KA2D/H,CAAC"}
|
package/src/NavigationRoute.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2018 Google LLC
|
|
3
|
-
|
|
4
|
-
Use of this source code is governed by an MIT-style
|
|
5
|
-
license that can be found in the LICENSE file or at
|
|
6
|
-
https://opensource.org/licenses/MIT.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { RouteHandler, RouteMatchCallbackOptions } from "@serwist/core";
|
|
10
|
-
import { assert, logger } from "@serwist/core/internal";
|
|
11
|
-
|
|
12
|
-
import { Route } from "./Route.js";
|
|
13
|
-
|
|
14
|
-
export interface NavigationRouteMatchOptions {
|
|
15
|
-
/**
|
|
16
|
-
* If any of these patterns
|
|
17
|
-
* match the URL's pathname and search parameter, the route will handle the
|
|
18
|
-
* request (assuming the denylist doesn't match).
|
|
19
|
-
*
|
|
20
|
-
* @default [/./]
|
|
21
|
-
*/
|
|
22
|
-
allowlist?: RegExp[];
|
|
23
|
-
/**
|
|
24
|
-
* If any of these patterns match, the route will not handle the request (even if a allowlist RegExp matches).
|
|
25
|
-
*/
|
|
26
|
-
denylist?: RegExp[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* NavigationRoute makes it easy to create a `@serwist/routing` Route that matches for browser
|
|
31
|
-
* [navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests).
|
|
32
|
-
*
|
|
33
|
-
* It will only match incoming Requests whose [mode](https://fetch.spec.whatwg.org/#concept-request-mode) is set to `navigate`.
|
|
34
|
-
*
|
|
35
|
-
* You can optionally only apply this route to a subset of navigation requests
|
|
36
|
-
* by using one or both of the `denylist` and `allowlist` parameters.
|
|
37
|
-
*/
|
|
38
|
-
export class NavigationRoute extends Route {
|
|
39
|
-
private readonly _allowlist: RegExp[];
|
|
40
|
-
private readonly _denylist: RegExp[];
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* If both `denylist` and `allowlist` are provided, the `denylist` will
|
|
44
|
-
* take precedence and the request will not match this route.
|
|
45
|
-
*
|
|
46
|
-
* The regular expressions in `allowlist` and `denylist`
|
|
47
|
-
* are matched against the concatenated
|
|
48
|
-
* [`pathname`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname)
|
|
49
|
-
* and [`search`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search)
|
|
50
|
-
* portions of the requested URL.
|
|
51
|
-
*
|
|
52
|
-
* *Note*: These RegExps may be evaluated against every destination URL during
|
|
53
|
-
* a navigation. Avoid using
|
|
54
|
-
* [complex RegExps](https://github.com/GoogleChrome/workbox/issues/3077),
|
|
55
|
-
* or else your users may see delays when navigating your site.
|
|
56
|
-
*
|
|
57
|
-
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
58
|
-
* @param options
|
|
59
|
-
*/
|
|
60
|
-
constructor(handler: RouteHandler, { allowlist = [/./], denylist = [] }: NavigationRouteMatchOptions = {}) {
|
|
61
|
-
if (process.env.NODE_ENV !== "production") {
|
|
62
|
-
assert!.isArrayOfClass(allowlist, RegExp, {
|
|
63
|
-
moduleName: "@serwist/routing",
|
|
64
|
-
className: "NavigationRoute",
|
|
65
|
-
funcName: "constructor",
|
|
66
|
-
paramName: "options.allowlist",
|
|
67
|
-
});
|
|
68
|
-
assert!.isArrayOfClass(denylist, RegExp, {
|
|
69
|
-
moduleName: "@serwist/routing",
|
|
70
|
-
className: "NavigationRoute",
|
|
71
|
-
funcName: "constructor",
|
|
72
|
-
paramName: "options.denylist",
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
super((options: RouteMatchCallbackOptions) => this._match(options), handler);
|
|
77
|
-
|
|
78
|
-
this._allowlist = allowlist;
|
|
79
|
-
this._denylist = denylist;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Routes match handler.
|
|
84
|
-
*
|
|
85
|
-
* @param options
|
|
86
|
-
* @returns
|
|
87
|
-
* @private
|
|
88
|
-
*/
|
|
89
|
-
private _match({ url, request }: RouteMatchCallbackOptions): boolean {
|
|
90
|
-
if (request && request.mode !== "navigate") {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const pathnameAndSearch = url.pathname + url.search;
|
|
95
|
-
|
|
96
|
-
for (const regExp of this._denylist) {
|
|
97
|
-
if (regExp.test(pathnameAndSearch)) {
|
|
98
|
-
if (process.env.NODE_ENV !== "production") {
|
|
99
|
-
logger.log(
|
|
100
|
-
`The navigation route ${pathnameAndSearch} is not being used, since the URL matches this denylist pattern: ${regExp.toString()}`,
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (this._allowlist.some((regExp) => regExp.test(pathnameAndSearch))) {
|
|
108
|
-
if (process.env.NODE_ENV !== "production") {
|
|
109
|
-
logger.debug(`The navigation route ${pathnameAndSearch} is being used.`);
|
|
110
|
-
}
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (process.env.NODE_ENV !== "production") {
|
|
115
|
-
logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL being navigated to doesn't match the allowlist.`);
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
}
|