@serwist/routing 8.4.4 → 9.0.0-preview.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/dist/NavigationRoute.d.ts +2 -2
- package/dist/NavigationRoute.d.ts.map +1 -0
- package/dist/RegExpRoute.d.ts +2 -2
- package/dist/RegExpRoute.d.ts.map +1 -0
- package/dist/Route.d.ts +2 -2
- package/dist/Route.d.ts.map +1 -0
- package/dist/Router.d.ts +2 -2
- package/dist/Router.d.ts.map +1 -0
- package/dist/_types.d.ts +1 -0
- package/dist/_types.d.ts.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -17
- package/dist/registerRoute.d.ts +3 -3
- package/dist/registerRoute.d.ts.map +1 -0
- package/dist/setCatchHandler.d.ts +2 -2
- package/dist/setCatchHandler.d.ts.map +1 -0
- package/dist/setDefaultHandler.d.ts +3 -3
- package/dist/setDefaultHandler.d.ts.map +1 -0
- package/dist/unregisterRoute.d.ts +8 -0
- package/dist/unregisterRoute.d.ts.map +1 -0
- package/dist/utils/constants.d.ts +3 -6
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/getOrCreateDefaultRouter.d.ts +1 -0
- package/dist/utils/getOrCreateDefaultRouter.d.ts.map +1 -0
- package/dist/utils/normalizeHandler.d.ts +1 -0
- package/dist/utils/normalizeHandler.d.ts.map +1 -0
- package/package.json +18 -16
- package/src/NavigationRoute.ts +119 -0
- package/src/RegExpRoute.ts +74 -0
- package/src/Route.ts +68 -0
- package/src/Router.ts +436 -0
- package/src/_types.ts +62 -0
- package/src/index.ts +22 -0
- package/src/registerRoute.ts +89 -0
- package/src/setCatchHandler.ts +22 -0
- package/src/setDefaultHandler.ts +25 -0
- package/src/unregisterRoute.ts +12 -0
- package/src/utils/constants.ts +24 -0
- package/src/utils/getOrCreateDefaultRouter.ts +29 -0
- package/src/utils/normalizeHandler.ts +40 -0
- package/dist/index.cjs +0 -728
- package/dist/index.d.cts +0 -0
|
@@ -23,7 +23,7 @@ export interface NavigationRouteMatchOptions {
|
|
|
23
23
|
* You can optionally only apply this route to a subset of navigation requests
|
|
24
24
|
* by using one or both of the `denylist` and `allowlist` parameters.
|
|
25
25
|
*/
|
|
26
|
-
declare class NavigationRoute extends Route {
|
|
26
|
+
export declare class NavigationRoute extends Route {
|
|
27
27
|
private readonly _allowlist;
|
|
28
28
|
private readonly _denylist;
|
|
29
29
|
/**
|
|
@@ -54,4 +54,4 @@ declare class NavigationRoute extends Route {
|
|
|
54
54
|
*/
|
|
55
55
|
private _match;
|
|
56
56
|
}
|
|
57
|
-
|
|
57
|
+
//# 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,qBAAa,eAAgB,SAAQ,KAAK;IACxC,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;CA8Bf"}
|
package/dist/RegExpRoute.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type { HTTPMethod } from "./utils/constants.js";
|
|
|
8
8
|
* requests against third-party servers, you must define a RegExp that matches
|
|
9
9
|
* the start of the URL.
|
|
10
10
|
*/
|
|
11
|
-
declare class RegExpRoute extends Route {
|
|
11
|
+
export declare class RegExpRoute extends Route {
|
|
12
12
|
/**
|
|
13
13
|
* If the regular expression contains
|
|
14
14
|
* [capture groups](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references),
|
|
@@ -21,4 +21,4 @@ declare class RegExpRoute extends Route {
|
|
|
21
21
|
*/
|
|
22
22
|
constructor(regExp: RegExp, handler: RouteHandler, method?: HTTPMethod);
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
//# 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,qBAAa,WAAY,SAAQ,KAAK;IACpC;;;;;;;;;OASG;gBACS,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,UAAU;CAyCvE"}
|
package/dist/Route.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { HTTPMethod } from "./utils/constants.js";
|
|
|
7
7
|
* is called when there is a match and should return a Promise that resolves
|
|
8
8
|
* to a `Response`.
|
|
9
9
|
*/
|
|
10
|
-
declare class Route {
|
|
10
|
+
export declare class Route {
|
|
11
11
|
handler: RouteHandlerObject;
|
|
12
12
|
match: RouteMatchCallback;
|
|
13
13
|
method: HTTPMethod;
|
|
@@ -30,4 +30,4 @@ declare class Route {
|
|
|
30
30
|
*/
|
|
31
31
|
setCatchHandler(handler: RouteHandler): void;
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
//# 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,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
CHANGED
|
@@ -13,7 +13,7 @@ import type { HTTPMethod } from "./utils/constants.js";
|
|
|
13
13
|
* If a request matches multiple routes, the **earliest** registered route will
|
|
14
14
|
* be used to respond to the request.
|
|
15
15
|
*/
|
|
16
|
-
declare class Router {
|
|
16
|
+
export declare class Router {
|
|
17
17
|
private readonly _routes;
|
|
18
18
|
private readonly _defaultHandlerMap;
|
|
19
19
|
private _catchHandler?;
|
|
@@ -121,4 +121,4 @@ declare class Router {
|
|
|
121
121
|
*/
|
|
122
122
|
unregisterRoute(route: Route): void;
|
|
123
123
|
}
|
|
124
|
-
|
|
124
|
+
//# 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,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
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_types.d.ts","sourceRoot":"","sources":["../src/_types.ts"],"names":[],"mappings":";AAeA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG"}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { Router } from "./Router.js";
|
|
|
6
6
|
import { registerRoute } from "./registerRoute.js";
|
|
7
7
|
import { setCatchHandler } from "./setCatchHandler.js";
|
|
8
8
|
import { setDefaultHandler } from "./setDefaultHandler.js";
|
|
9
|
+
import { unregisterRoute } from "./unregisterRoute.js";
|
|
9
10
|
import type { HTTPMethod } from "./utils/constants.js";
|
|
10
|
-
export { NavigationRoute, RegExpRoute, registerRoute, Route, Router, setCatchHandler, setDefaultHandler };
|
|
11
|
+
export { NavigationRoute, RegExpRoute, registerRoute, Route, Router, setCatchHandler, setDefaultHandler, unregisterRoute };
|
|
11
12
|
export type { HTTPMethod, NavigationRouteMatchOptions };
|
|
13
|
+
//# 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,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,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,eAAe,EAAE,CAAC;AAE3H,YAAY,EAAE,UAAU,EAAE,2BAA2B,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -10,15 +10,11 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
|
|
|
10
10
|
* The default HTTP method, 'GET', used when there's no specific method
|
|
11
11
|
* configured for a route.
|
|
12
12
|
*
|
|
13
|
-
* @type {string}
|
|
14
|
-
*
|
|
15
13
|
* @private
|
|
16
14
|
*/ const defaultMethod = "GET";
|
|
17
15
|
/**
|
|
18
16
|
* The list of valid HTTP methods associated with requests that could be routed.
|
|
19
17
|
*
|
|
20
|
-
* @type {Array<string>}
|
|
21
|
-
*
|
|
22
18
|
* @private
|
|
23
19
|
*/ const validMethods = [
|
|
24
20
|
"DELETE",
|
|
@@ -327,10 +323,7 @@ import { assert, logger, getFriendlyURL, SerwistError } from '@serwist/core/inte
|
|
|
327
323
|
request,
|
|
328
324
|
event
|
|
329
325
|
});
|
|
330
|
-
|
|
331
|
-
// some reason (probably a bug). The real type here should work but
|
|
332
|
-
// doesn't: `Array<Promise<Response> | undefined>`.
|
|
333
|
-
})); // TypeScript
|
|
326
|
+
}));
|
|
334
327
|
event.waitUntil(requestPromises);
|
|
335
328
|
// If a MessageChannel was used, reply to the message on success.
|
|
336
329
|
if (event.ports?.[0]) {
|
|
@@ -635,7 +628,7 @@ let defaultRouter;
|
|
|
635
628
|
};
|
|
636
629
|
|
|
637
630
|
/**
|
|
638
|
-
*
|
|
631
|
+
* Registers a RegExp, string, or function with a caching
|
|
639
632
|
* strategy to a singleton Router instance.
|
|
640
633
|
*
|
|
641
634
|
* @param capture If the capture param is a `Route`, all other arguments will be ignored.
|
|
@@ -643,7 +636,7 @@ let defaultRouter;
|
|
|
643
636
|
* This parameter is required if `capture` is not a `Route` object.
|
|
644
637
|
* @param method The HTTP method to match the Route against. Defaults to GET.
|
|
645
638
|
* @returns The generated `Route`.
|
|
646
|
-
*/
|
|
639
|
+
*/ const registerRoute = (capture, handler, method)=>{
|
|
647
640
|
let route;
|
|
648
641
|
if (typeof capture === "string") {
|
|
649
642
|
const captureUrl = new URL(capture, location.href);
|
|
@@ -692,29 +685,38 @@ let defaultRouter;
|
|
|
692
685
|
const defaultRouter = getOrCreateDefaultRouter();
|
|
693
686
|
defaultRouter.registerRoute(route);
|
|
694
687
|
return route;
|
|
695
|
-
}
|
|
688
|
+
};
|
|
696
689
|
|
|
697
690
|
/**
|
|
698
691
|
* If a Route throws an error while handling a request, this `handler`
|
|
699
692
|
* will be called and given a chance to provide a response.
|
|
700
693
|
*
|
|
701
694
|
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
702
|
-
*/
|
|
695
|
+
*/ const setCatchHandler = (handler)=>{
|
|
703
696
|
const defaultRouter = getOrCreateDefaultRouter();
|
|
704
697
|
defaultRouter.setCatchHandler(handler);
|
|
705
|
-
}
|
|
698
|
+
};
|
|
706
699
|
|
|
707
700
|
/**
|
|
708
|
-
*
|
|
701
|
+
* Defines a default `handler` that's called when no routes explicitly
|
|
709
702
|
* match the incoming request.
|
|
710
703
|
*
|
|
711
704
|
* Without a default handler, unmatched requests will go against the
|
|
712
705
|
* network as if there were no service worker present.
|
|
713
706
|
*
|
|
714
707
|
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
715
|
-
*/
|
|
708
|
+
*/ const setDefaultHandler = (handler)=>{
|
|
716
709
|
const defaultRouter = getOrCreateDefaultRouter();
|
|
717
710
|
defaultRouter.setDefaultHandler(handler);
|
|
718
|
-
}
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Unregisters a route from the singleton Router instance.
|
|
715
|
+
*
|
|
716
|
+
* @param route The route to unregister.
|
|
717
|
+
*/ const unregisterRoute = (route)=>{
|
|
718
|
+
const defaultRouter = getOrCreateDefaultRouter();
|
|
719
|
+
defaultRouter.unregisterRoute(route);
|
|
720
|
+
};
|
|
719
721
|
|
|
720
|
-
export { NavigationRoute, RegExpRoute, Route, Router, registerRoute, setCatchHandler, setDefaultHandler };
|
|
722
|
+
export { NavigationRoute, RegExpRoute, Route, Router, registerRoute, setCatchHandler, setDefaultHandler, unregisterRoute };
|
package/dist/registerRoute.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { RouteHandler, RouteMatchCallback } from "@serwist/core";
|
|
|
2
2
|
import { Route } from "./Route.js";
|
|
3
3
|
import type { HTTPMethod } from "./utils/constants.js";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Registers a RegExp, string, or function with a caching
|
|
6
6
|
* strategy to a singleton Router instance.
|
|
7
7
|
*
|
|
8
8
|
* @param capture If the capture param is a `Route`, all other arguments will be ignored.
|
|
@@ -11,5 +11,5 @@ import type { HTTPMethod } from "./utils/constants.js";
|
|
|
11
11
|
* @param method The HTTP method to match the Route against. Defaults to GET.
|
|
12
12
|
* @returns The generated `Route`.
|
|
13
13
|
*/
|
|
14
|
-
declare
|
|
15
|
-
|
|
14
|
+
export declare const registerRoute: (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod) => Route;
|
|
15
|
+
//# sourceMappingURL=registerRoute.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -5,5 +5,5 @@ import type { RouteHandler } from "@serwist/core";
|
|
|
5
5
|
*
|
|
6
6
|
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
7
7
|
*/
|
|
8
|
-
declare
|
|
9
|
-
|
|
8
|
+
export declare const setCatchHandler: (handler: RouteHandler) => void;
|
|
9
|
+
//# sourceMappingURL=setCatchHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,6 +1,6 @@
|
|
|
1
1
|
import type { RouteHandler } from "@serwist/core";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Defines a default `handler` that's called when no routes explicitly
|
|
4
4
|
* match the incoming request.
|
|
5
5
|
*
|
|
6
6
|
* Without a default handler, unmatched requests will go against the
|
|
@@ -8,5 +8,5 @@ import type { RouteHandler } from "@serwist/core";
|
|
|
8
8
|
*
|
|
9
9
|
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
10
10
|
*/
|
|
11
|
-
declare
|
|
12
|
-
|
|
11
|
+
export declare const setDefaultHandler: (handler: RouteHandler) => void;
|
|
12
|
+
//# sourceMappingURL=setDefaultHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -3,16 +3,13 @@ export type HTTPMethod = "DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT";
|
|
|
3
3
|
* The default HTTP method, 'GET', used when there's no specific method
|
|
4
4
|
* configured for a route.
|
|
5
5
|
*
|
|
6
|
-
* @type {string}
|
|
7
|
-
*
|
|
8
6
|
* @private
|
|
9
7
|
*/
|
|
10
|
-
export declare const defaultMethod
|
|
8
|
+
export declare const defaultMethod = "GET";
|
|
11
9
|
/**
|
|
12
10
|
* The list of valid HTTP methods associated with requests that could be routed.
|
|
13
11
|
*
|
|
14
|
-
* @type {Array<string>}
|
|
15
|
-
*
|
|
16
12
|
* @private
|
|
17
13
|
*/
|
|
18
|
-
export declare const validMethods:
|
|
14
|
+
export declare const validMethods: ("DELETE" | "GET" | "HEAD" | "PATCH" | "POST" | "PUT")[];
|
|
15
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/routing",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0-preview.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A service worker helper library to route request URLs to handlers.",
|
|
6
6
|
"files": [
|
|
7
|
-
"
|
|
8
|
-
"
|
|
7
|
+
"src",
|
|
8
|
+
"dist"
|
|
9
9
|
],
|
|
10
10
|
"keywords": [
|
|
11
11
|
"serwist",
|
|
@@ -20,28 +20,30 @@
|
|
|
20
20
|
"repository": "serwist/serwist",
|
|
21
21
|
"bugs": "https://github.com/serwist/serwist/issues",
|
|
22
22
|
"homepage": "https://serwist.pages.dev",
|
|
23
|
-
"
|
|
24
|
-
"main": "./dist/index.cjs",
|
|
23
|
+
"main": "./dist/index.js",
|
|
25
24
|
"types": "./dist/index.d.ts",
|
|
26
25
|
"exports": {
|
|
27
26
|
".": {
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
"default": "./dist/index.js"
|
|
31
|
-
},
|
|
32
|
-
"require": {
|
|
33
|
-
"types": "./dist/index.d.cts",
|
|
34
|
-
"default": "./dist/index.cjs"
|
|
35
|
-
}
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
36
29
|
},
|
|
37
30
|
"./package.json": "./package.json"
|
|
38
31
|
},
|
|
39
32
|
"dependencies": {
|
|
40
|
-
"@serwist/core": "
|
|
33
|
+
"@serwist/core": "9.0.0-preview.0"
|
|
41
34
|
},
|
|
42
35
|
"devDependencies": {
|
|
43
|
-
"rollup": "4.9.
|
|
44
|
-
"
|
|
36
|
+
"rollup": "4.9.6",
|
|
37
|
+
"typescript": "5.4.0-dev.20240203",
|
|
38
|
+
"@serwist/constants": "9.0.0-preview.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"typescript": ">=5.0.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependenciesMeta": {
|
|
44
|
+
"typescript": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
45
47
|
},
|
|
46
48
|
"scripts": {
|
|
47
49
|
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
@@ -0,0 +1,119 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
}
|