@serwist/routing 9.0.0-preview.2 → 9.0.0-preview.21

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.
Files changed (41) hide show
  1. package/dist/index.d.ts +2 -12
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1 -468
  4. package/package.json +7 -8
  5. package/src/index.ts +2 -22
  6. package/dist/NavigationRoute.d.ts +0 -57
  7. package/dist/NavigationRoute.d.ts.map +0 -1
  8. package/dist/RegExpRoute.d.ts +0 -24
  9. package/dist/RegExpRoute.d.ts.map +0 -1
  10. package/dist/Route.d.ts +0 -33
  11. package/dist/Route.d.ts.map +0 -1
  12. package/dist/Router.d.ts +0 -124
  13. package/dist/Router.d.ts.map +0 -1
  14. package/dist/_types.d.ts +0 -48
  15. package/dist/_types.d.ts.map +0 -1
  16. package/dist/registerRoute.d.ts +0 -15
  17. package/dist/registerRoute.d.ts.map +0 -1
  18. package/dist/setCatchHandler.d.ts +0 -9
  19. package/dist/setCatchHandler.d.ts.map +0 -1
  20. package/dist/setDefaultHandler.d.ts +0 -12
  21. package/dist/setDefaultHandler.d.ts.map +0 -1
  22. package/dist/unregisterRoute.d.ts +0 -8
  23. package/dist/unregisterRoute.d.ts.map +0 -1
  24. package/dist/utils/constants.d.ts +0 -15
  25. package/dist/utils/constants.d.ts.map +0 -1
  26. package/dist/utils/getOrCreateDefaultRouter.d.ts +0 -10
  27. package/dist/utils/getOrCreateDefaultRouter.d.ts.map +0 -1
  28. package/dist/utils/normalizeHandler.d.ts +0 -10
  29. package/dist/utils/normalizeHandler.d.ts.map +0 -1
  30. package/src/NavigationRoute.ts +0 -119
  31. package/src/RegExpRoute.ts +0 -74
  32. package/src/Route.ts +0 -68
  33. package/src/Router.ts +0 -436
  34. package/src/_types.ts +0 -62
  35. package/src/registerRoute.ts +0 -89
  36. package/src/setCatchHandler.ts +0 -22
  37. package/src/setDefaultHandler.ts +0 -25
  38. package/src/unregisterRoute.ts +0 -12
  39. package/src/utils/constants.ts +0 -24
  40. package/src/utils/getOrCreateDefaultRouter.ts +0 -29
  41. package/src/utils/normalizeHandler.ts +0 -40
package/dist/Route.d.ts DELETED
@@ -1,33 +0,0 @@
1
- import type { RouteHandler, RouteHandlerObject, RouteMatchCallback } from "@serwist/core";
2
- import type { HTTPMethod } from "./utils/constants.js";
3
- /**
4
- * A `Route` consists of a pair of callback functions, "match" and "handler".
5
- * The "match" callback determine if a route should be used to "handle" a
6
- * request by returning a non-falsy value if it can. The "handler" callback
7
- * is called when there is a match and should return a Promise that resolves
8
- * to a `Response`.
9
- */
10
- export declare class Route {
11
- handler: RouteHandlerObject;
12
- match: RouteMatchCallback;
13
- method: HTTPMethod;
14
- catchHandler?: RouteHandlerObject;
15
- /**
16
- * Constructor for Route class.
17
- *
18
- * @param match A callback function that determines whether the
19
- * route matches a given `fetch` event by returning a non-falsy value.
20
- * @param handler A callback function that returns a Promise resolving
21
- * to a Response.
22
- * @param method The HTTP method to match the Route against. Defaults
23
- * to GET.
24
- */
25
- constructor(match: RouteMatchCallback, handler: RouteHandler, method?: HTTPMethod);
26
- /**
27
- *
28
- * @param handler A callback function that returns a Promise resolving
29
- * to a Response.
30
- */
31
- setCatchHandler(handler: RouteHandler): void;
32
- }
33
- //# sourceMappingURL=Route.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Route.d.ts","sourceRoot":"","sources":["../src/Route.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAG1F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAIvD;;;;;;GAMG;AACH,qBAAa,KAAK;IAChB,OAAO,EAAE,kBAAkB,CAAC;IAC5B,KAAK,EAAE,kBAAkB,CAAC;IAC1B,MAAM,EAAE,UAAU,CAAC;IACnB,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAElC;;;;;;;;;OASG;gBACS,KAAK,EAAE,kBAAkB,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,GAAE,UAA0B;IAqBhG;;;;OAIG;IACH,eAAe,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI;CAG7C"}
package/dist/Router.d.ts DELETED
@@ -1,124 +0,0 @@
1
- import type { RouteHandler, RouteHandlerCallbackOptions, RouteMatchCallbackOptions } from "@serwist/core";
2
- import type { Route } from "./Route.js";
3
- import type { HTTPMethod } from "./utils/constants.js";
4
- /**
5
- * The Router can be used to process a `FetchEvent` using one or more `@serwist/routing` Route(s),
6
- * responding with a `Response` if a matching route exists.
7
- *
8
- * If no route matches a given a request, the Router will use a "default" handler if one is defined.
9
- *
10
- * Should the matching Route throw an error, the Router will use a "catch" handler if one is defined to
11
- * gracefully deal with issues and respond with a Request.
12
- *
13
- * If a request matches multiple routes, the **earliest** registered route will
14
- * be used to respond to the request.
15
- */
16
- export declare class Router {
17
- private readonly _routes;
18
- private readonly _defaultHandlerMap;
19
- private _catchHandler?;
20
- /**
21
- * Initializes a new Router.
22
- */
23
- constructor();
24
- /**
25
- * @returns routes A `Map` of HTTP method name ('GET', etc.) to an array of all the corresponding `Route`
26
- * instances that are registered.
27
- */
28
- get routes(): Map<HTTPMethod, Route[]>;
29
- /**
30
- * Adds a fetch event listener to respond to events when a route matches
31
- * the event's request.
32
- */
33
- addFetchListener(): void;
34
- /**
35
- * Adds a message event listener for URLs to cache from the window.
36
- * This is useful to cache resources loaded on the page prior to when the
37
- * service worker started controlling it.
38
- *
39
- * The format of the message data sent from the window should be as follows.
40
- * Where the `urlsToCache` array may consist of URL strings or an array of
41
- * URL string + `requestInit` object (the same as you'd pass to `fetch()`).
42
- *
43
- * ```
44
- * {
45
- * type: 'CACHE_URLS',
46
- * payload: {
47
- * urlsToCache: [
48
- * './script1.js',
49
- * './script2.js',
50
- * ['./script3.js', {mode: 'no-cors'}],
51
- * ],
52
- * },
53
- * }
54
- * ```
55
- */
56
- addCacheListener(): void;
57
- /**
58
- * Apply the routing rules to a FetchEvent object to get a Response from an
59
- * appropriate Route's handler.
60
- *
61
- * @param options
62
- * @returns A promise is returned if a registered route can handle the request.
63
- * If there is no matching route and there's no `defaultHandler`, `undefined`
64
- * is returned.
65
- */
66
- handleRequest({ request, event, }: {
67
- /**
68
- * The request to handle.
69
- */
70
- request: Request;
71
- /**
72
- * The event that triggered the request.
73
- */
74
- event: ExtendableEvent;
75
- }): Promise<Response> | undefined;
76
- /**
77
- * Checks a request and URL (and optionally an event) against the list of
78
- * registered routes, and if there's a match, returns the corresponding
79
- * route along with any params generated by the match.
80
- *
81
- * @param options
82
- * @returns An object with `route` and `params` properties. They are populated
83
- * if a matching route was found or `undefined` otherwise.
84
- */
85
- findMatchingRoute({ url, sameOrigin, request, event }: RouteMatchCallbackOptions): {
86
- route?: Route;
87
- params?: RouteHandlerCallbackOptions["params"];
88
- };
89
- /**
90
- * Define a default `handler` that's called when no routes explicitly
91
- * match the incoming request.
92
- *
93
- * Each HTTP method ('GET', 'POST', etc.) gets its own default handler.
94
- *
95
- * Without a default handler, unmatched requests will go against the
96
- * network as if there were no service worker present.
97
- *
98
- * @param handler A callback function that returns a Promise resulting in a Response.
99
- * @param method The HTTP method to associate with this default handler. Each method
100
- * has its own default. Defaults to GET.
101
- */
102
- setDefaultHandler(handler: RouteHandler, method?: HTTPMethod): void;
103
- /**
104
- * If a Route throws an error while handling a request, this `handler`
105
- * will be called and given a chance to provide a response.
106
- *
107
- * @param handler A callback function that returns a Promise resulting
108
- * in a Response.
109
- */
110
- setCatchHandler(handler: RouteHandler): void;
111
- /**
112
- * Registers a route with the router.
113
- *
114
- * @param route The route to register.
115
- */
116
- registerRoute(route: Route): void;
117
- /**
118
- * Unregisters a route with the router.
119
- *
120
- * @param route The route to unregister.
121
- */
122
- unregisterRoute(route: Route): void;
123
- }
124
- //# sourceMappingURL=Router.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Router.d.ts","sourceRoot":"","sources":["../src/Router.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,2BAA2B,EAAsB,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAG9H,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAavD;;;;;;;;;;;GAWG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAsC;IACzE,OAAO,CAAC,aAAa,CAAC,CAAqB;IAE3C;;OAEG;;IAMH;;;OAGG;IACH,IAAI,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,KAAK,EAAE,CAAC,CAErC;IAED;;;OAGG;IACH,gBAAgB,IAAI,IAAI;IAWxB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,gBAAgB,IAAI,IAAI;IAkCxB;;;;;;;;OAQG;IACH,aAAa,CAAC,EACZ,OAAO,EACP,KAAK,GACN,EAAE;QACD;;WAEG;QACH,OAAO,EAAE,OAAO,CAAC;QACjB;;WAEG;QACH,KAAK,EAAE,eAAe,CAAC;KACxB,GAAG,OAAO,CAAC,QAAQ,CAAC,GAAG,SAAS;IA8HjC;;;;;;;;OAQG;IACH,iBAAiB,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,yBAAyB,GAAG;QACjF,KAAK,CAAC,EAAE,KAAK,CAAC;QACd,MAAM,CAAC,EAAE,2BAA2B,CAAC,QAAQ,CAAC,CAAC;KAChD;IAgDD;;;;;;;;;;;;OAYG;IACH,iBAAiB,CAAC,OAAO,EAAE,YAAY,EAAE,MAAM,GAAE,UAA0B,GAAG,IAAI;IAIlF;;;;;;OAMG;IACH,eAAe,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI;IAI5C;;;;OAIG;IACH,aAAa,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IA+CjC;;;;OAIG;IACH,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;CAcpC"}
package/dist/_types.d.ts DELETED
@@ -1,48 +0,0 @@
1
- export {};
2
- /**
3
- * The "match" callback is used to determine if a `Route` should apply for a
4
- * particular URL. When matching occurs in response to a fetch event from the
5
- * client, the `event` object is supplied in addition to the `url`, `request`,
6
- * and `sameOrigin` value. However, since the match callback can be invoked
7
- * outside of a fetch event, matching logic should not assume the `event`
8
- * object will always be available.
9
- *
10
- * If the match callback returns a truthy value, the matching route's
11
- * {@link workbox-routing~handlerCallback} will be
12
- * invoked immediately. If the value returned is a non-empty array or object,
13
- * that value will be set on the handler's `context.params` argument.
14
- *
15
- * @callback ~matchCallback
16
- * @param context
17
- * @param context.request The corresponding request.
18
- * @param context.url The request's URL.
19
- * @param context.event The corresponding event that triggered
20
- * the request.
21
- * @param context.sameOrigin The result of comparing `url.origin`
22
- * against the current origin.
23
- * @returns To signify a match, return a truthy value.
24
- */
25
- /**
26
- * The "handler" callback is invoked whenever a `Router` matches a URL to a
27
- * `Route` via its {@link workbox-routing~matchCallback}
28
- * callback. This callback should return a Promise that resolves with a
29
- * `Response`.
30
- *
31
- * If a non-empty array or object is returned by the
32
- * {@link workbox-routing~matchCallback} it
33
- * will be passed in as the handler's `context.params` argument.
34
- *
35
- * @callback ~handlerCallback
36
- * @param context
37
- * @param context.request The corresponding request.
38
- * @param context.url The URL that matched, if available.
39
- * @param context.event The corresponding event that triggered
40
- * the request.
41
- * @param context.params Array or Object parameters returned by the
42
- * Route's {@link workbox-routing~matchCallback}.
43
- * This will be undefined if an empty array or object were returned.
44
- * @returns The response that will fulfill the request.
45
- *
46
- * @memberof workbox-routing
47
- */
48
- //# sourceMappingURL=_types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_types.d.ts","sourceRoot":"","sources":["../src/_types.ts"],"names":[],"mappings":";AAeA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG"}
@@ -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;AAItE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGvD;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,YAAa,MAAM,GAAG,MAAM,GAAG,kBAAkB,GAAG,KAAK,YAAY,YAAY,WAAW,UAAU,KAAG,KA8DlI,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,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
- }
@@ -1,74 +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, RouteMatchCallback, RouteMatchCallbackOptions } from "@serwist/core";
10
- import { assert, logger } from "@serwist/core/internal";
11
-
12
- import { Route } from "./Route.js";
13
- import type { HTTPMethod } from "./utils/constants.js";
14
-
15
- /**
16
- * RegExpRoute makes it easy to create a regular expression based on a `@serwist/routing` Route.
17
- *
18
- * For same-origin requests the RegExp only needs to match part of the URL. For
19
- * requests against third-party servers, you must define a RegExp that matches
20
- * the start of the URL.
21
- */
22
- export class RegExpRoute extends Route {
23
- /**
24
- * If the regular expression contains
25
- * [capture groups](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references),
26
- * the captured values will be passed to the `params` argument.
27
- *
28
- * @param regExp The regular expression to match against URLs.
29
- * @param handler A callback function that returns a Promise resulting in a Response.
30
- * @param method The HTTP method to match the Route, defaults to GET.
31
- * against.
32
- */
33
- constructor(regExp: RegExp, handler: RouteHandler, method?: HTTPMethod) {
34
- if (process.env.NODE_ENV !== "production") {
35
- assert!.isInstance(regExp, RegExp, {
36
- moduleName: "@serwist/routing",
37
- className: "RegExpRoute",
38
- funcName: "constructor",
39
- paramName: "pattern",
40
- });
41
- }
42
-
43
- const match: RouteMatchCallback = ({ url }: RouteMatchCallbackOptions) => {
44
- const result = regExp.exec(url.href);
45
-
46
- // Return immediately if there's no match.
47
- if (!result) {
48
- return;
49
- }
50
-
51
- // Require that the match start at the first character in the URL string
52
- // if it's a cross-origin request.
53
- // See https://github.com/GoogleChrome/workbox/issues/281 for the context
54
- // behind this behavior.
55
- if (url.origin !== location.origin && result.index !== 0) {
56
- if (process.env.NODE_ENV !== "production") {
57
- logger.debug(
58
- `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.`,
59
- );
60
- }
61
-
62
- return;
63
- }
64
-
65
- // If the route matches, but there aren't any capture groups defined, then
66
- // this will return [], which is truthy and therefore sufficient to
67
- // indicate a match.
68
- // If there are capture groups, then it will return their values.
69
- return result.slice(1);
70
- };
71
-
72
- super(match, handler, method);
73
- }
74
- }
package/src/Route.ts DELETED
@@ -1,68 +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, RouteHandlerObject, RouteMatchCallback } from "@serwist/core";
10
- import { assert } from "@serwist/core/internal";
11
-
12
- import type { HTTPMethod } from "./utils/constants.js";
13
- import { defaultMethod, validMethods } from "./utils/constants.js";
14
- import { normalizeHandler } from "./utils/normalizeHandler.js";
15
-
16
- /**
17
- * A `Route` consists of a pair of callback functions, "match" and "handler".
18
- * The "match" callback determine if a route should be used to "handle" a
19
- * request by returning a non-falsy value if it can. The "handler" callback
20
- * is called when there is a match and should return a Promise that resolves
21
- * to a `Response`.
22
- */
23
- export class Route {
24
- handler: RouteHandlerObject;
25
- match: RouteMatchCallback;
26
- method: HTTPMethod;
27
- catchHandler?: RouteHandlerObject;
28
-
29
- /**
30
- * Constructor for Route class.
31
- *
32
- * @param match A callback function that determines whether the
33
- * route matches a given `fetch` event by returning a non-falsy value.
34
- * @param handler A callback function that returns a Promise resolving
35
- * to a Response.
36
- * @param method The HTTP method to match the Route against. Defaults
37
- * to GET.
38
- */
39
- constructor(match: RouteMatchCallback, handler: RouteHandler, method: HTTPMethod = defaultMethod) {
40
- if (process.env.NODE_ENV !== "production") {
41
- assert!.isType(match, "function", {
42
- moduleName: "@serwist/routing",
43
- className: "Route",
44
- funcName: "constructor",
45
- paramName: "match",
46
- });
47
-
48
- if (method) {
49
- assert!.isOneOf(method, validMethods, { paramName: "method" });
50
- }
51
- }
52
-
53
- // These values are referenced directly by Router so cannot be
54
- // altered by minificaton.
55
- this.handler = normalizeHandler(handler);
56
- this.match = match;
57
- this.method = method;
58
- }
59
-
60
- /**
61
- *
62
- * @param handler A callback function that returns a Promise resolving
63
- * to a Response.
64
- */
65
- setCatchHandler(handler: RouteHandler): void {
66
- this.catchHandler = normalizeHandler(handler);
67
- }
68
- }