@serwist/routing 8.0.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
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 @@
1
+ This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-routing
@@ -0,0 +1,58 @@
1
+ import type { RouteHandler } from "@serwist/core";
2
+ import { Route } from "./Route.js";
3
+ export interface NavigationRouteMatchOptions {
4
+ /**
5
+ * If any of these patterns
6
+ * match the URL's pathname and search parameter, the route will handle the
7
+ * request (assuming the denylist doesn't match).
8
+ *
9
+ * @default [/./]
10
+ */
11
+ allowlist?: RegExp[];
12
+ /**
13
+ * If any of these patterns match, the route will not handle the request (even if a allowlist RegExp matches).
14
+ */
15
+ denylist?: RegExp[];
16
+ }
17
+ /**
18
+ * NavigationRoute makes it easy to create a `@serwist/routing` Route that matches for browser
19
+ * [navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests).
20
+ *
21
+ * It will only match incoming Requests whose [mode](https://fetch.spec.whatwg.org/#concept-request-mode) is set to `navigate`.
22
+ *
23
+ * You can optionally only apply this route to a subset of navigation requests
24
+ * by using one or both of the `denylist` and `allowlist` parameters.
25
+ */
26
+ declare class NavigationRoute extends Route {
27
+ private readonly _allowlist;
28
+ private readonly _denylist;
29
+ /**
30
+ * If both `denylist` and `allowlist` are provided, the `denylist` will
31
+ * take precedence and the request will not match this route.
32
+ *
33
+ * The regular expressions in `allowlist` and `denylist`
34
+ * are matched against the concatenated
35
+ * [`pathname`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname)
36
+ * and [`search`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search)
37
+ * portions of the requested URL.
38
+ *
39
+ * *Note*: These RegExps may be evaluated against every destination URL during
40
+ * a navigation. Avoid using
41
+ * [complex RegExps](https://github.com/GoogleChrome/workbox/issues/3077),
42
+ * or else your users may see delays when navigating your site.
43
+ *
44
+ * @param handler A callback function that returns a Promise resulting in a Response.
45
+ * @param options
46
+ */
47
+ constructor(handler: RouteHandler, { allowlist, denylist }?: NavigationRouteMatchOptions);
48
+ /**
49
+ * Routes match handler.
50
+ *
51
+ * @param options
52
+ * @returns
53
+ * @private
54
+ */
55
+ private _match;
56
+ }
57
+ export { NavigationRoute };
58
+ //# sourceMappingURL=NavigationRoute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"NavigationRoute.d.ts","sourceRoot":"","sources":["../src/NavigationRoute.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAA6B,MAAM,eAAe,CAAC;AAG7E,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,MAAM,WAAW,2BAA2B;IAC1C;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED;;;;;;;;GAQG;AACH,cAAM,eAAgB,SAAQ,KAAK;IACjC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAW;IACtC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAW;IAErC;;;;;;;;;;;;;;;;;OAiBG;gBACS,OAAO,EAAE,YAAY,EAAE,EAAE,SAAiB,EAAE,QAAa,EAAE,GAAE,2BAAgC;IAsBzG;;;;;;OAMG;IACH,OAAO,CAAC,MAAM;CAgCf;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,25 @@
1
+ import type { RouteHandler } from "@serwist/core";
2
+ import { Route } from "./Route.js";
3
+ import type { HTTPMethod } from "./utils/constants.js";
4
+ /**
5
+ * RegExpRoute makes it easy to create a regular expression based on a `@serwist/routing` Route.
6
+ *
7
+ * For same-origin requests the RegExp only needs to match part of the URL. For
8
+ * requests against third-party servers, you must define a RegExp that matches
9
+ * the start of the URL.
10
+ */
11
+ declare class RegExpRoute extends Route {
12
+ /**
13
+ * If the regular expression contains
14
+ * [capture groups](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references),
15
+ * the captured values will be passed to the `params` argument.
16
+ *
17
+ * @param regExp The regular expression to match against URLs.
18
+ * @param handler A callback function that returns a Promise resulting in a Response.
19
+ * @param method The HTTP method to match the Route, defaults to GET.
20
+ * against.
21
+ */
22
+ constructor(regExp: RegExp, handler: RouteHandler, method?: HTTPMethod);
23
+ }
24
+ export { RegExpRoute };
25
+ //# sourceMappingURL=RegExpRoute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RegExpRoute.d.ts","sourceRoot":"","sources":["../src/RegExpRoute.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAiD,MAAM,eAAe,CAAC;AAGjG,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAEvD;;;;;;GAMG;AACH,cAAM,WAAY,SAAQ,KAAK;IAC7B;;;;;;;;;OASG;gBACS,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,UAAU;CA2CvE;AAED,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,34 @@
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
+ 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
+ export { Route };
34
+ //# sourceMappingURL=Route.d.ts.map
@@ -0,0 +1 @@
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,cAAM,KAAK;IACT,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;AAED,OAAO,EAAE,KAAK,EAAE,CAAC"}
@@ -0,0 +1,125 @@
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
+ 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
+ export { Router };
125
+ //# sourceMappingURL=Router.d.ts.map
@@ -0,0 +1 @@
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,cAAM,MAAM;IACV,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;IAsCxB;;;;;;;;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;AAED,OAAO,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,48 @@
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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_types.d.ts","sourceRoot":"","sources":["../src/_types.ts"],"names":[],"mappings":";AAeA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG"}
@@ -0,0 +1,12 @@
1
+ import type { NavigationRouteMatchOptions } from "./NavigationRoute.js";
2
+ import { NavigationRoute } from "./NavigationRoute.js";
3
+ import { RegExpRoute } from "./RegExpRoute.js";
4
+ import { registerRoute } from "./registerRoute.js";
5
+ import { Route } from "./Route.js";
6
+ import { Router } from "./Router.js";
7
+ import { setCatchHandler } from "./setCatchHandler.js";
8
+ import { setDefaultHandler } from "./setDefaultHandler.js";
9
+ import type { HTTPMethod } from "./utils/constants.js";
10
+ export { NavigationRoute, RegExpRoute, registerRoute, Route, Router, setCatchHandler, setDefaultHandler };
11
+ export type { HTTPMethod, NavigationRouteMatchOptions };
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,iBAAiB,EAAE,CAAC;AAE1G,YAAY,EAAE,UAAU,EAAE,2BAA2B,EAAE,CAAC"}