serwist 9.0.0-preview.24
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 +21 -0
- package/README.md +1 -0
- package/dist/NavigationRoute.d.ts +57 -0
- package/dist/NavigationRoute.d.ts.map +1 -0
- package/dist/PrecacheRoute.d.ts +17 -0
- package/dist/PrecacheRoute.d.ts.map +1 -0
- package/dist/PrecacheStrategy.d.ts +66 -0
- package/dist/PrecacheStrategy.d.ts.map +1 -0
- package/dist/RegExpRoute.d.ts +24 -0
- package/dist/RegExpRoute.d.ts.map +1 -0
- package/dist/Route.d.ts +33 -0
- package/dist/Route.d.ts.map +1 -0
- package/dist/Serwist.d.ts +331 -0
- package/dist/Serwist.d.ts.map +1 -0
- package/dist/cacheNames.d.ts +20 -0
- package/dist/cacheNames.d.ts.map +1 -0
- package/dist/chunks/NetworkOnly.js +599 -0
- package/dist/chunks/PrecacheFallbackPlugin.js +634 -0
- package/dist/chunks/Serwist.js +1034 -0
- package/dist/chunks/registerQuotaErrorCallback.js +17 -0
- package/dist/chunks/resultingClientExists.js +32 -0
- package/dist/chunks/timeout.js +400 -0
- package/dist/chunks/waitUntil.js +24 -0
- package/dist/cleanupOutdatedCaches.d.ts +6 -0
- package/dist/cleanupOutdatedCaches.d.ts.map +1 -0
- package/dist/clientsClaim.d.ts +6 -0
- package/dist/clientsClaim.d.ts.map +1 -0
- package/dist/constants.d.ts +15 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/copyResponse.d.ts +20 -0
- package/dist/copyResponse.d.ts.map +1 -0
- package/dist/disableDevLogs.d.ts +7 -0
- package/dist/disableDevLogs.d.ts.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.internal.d.ts +16 -0
- package/dist/index.internal.d.ts.map +1 -0
- package/dist/index.internal.js +24 -0
- package/dist/index.js +27 -0
- package/dist/index.legacy.d.ts +32 -0
- package/dist/index.legacy.d.ts.map +1 -0
- package/dist/index.legacy.js +640 -0
- package/dist/index.plugins.d.ts +41 -0
- package/dist/index.plugins.d.ts.map +1 -0
- package/dist/index.plugins.js +669 -0
- package/dist/index.strategies.d.ts +22 -0
- package/dist/index.strategies.d.ts.map +1 -0
- package/dist/index.strategies.js +144 -0
- package/dist/legacy/PrecacheController.d.ts +146 -0
- package/dist/legacy/PrecacheController.d.ts.map +1 -0
- package/dist/legacy/PrecacheFallbackPlugin.d.ts +62 -0
- package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/legacy/PrecacheRoute.d.ts +19 -0
- package/dist/legacy/PrecacheRoute.d.ts.map +1 -0
- package/dist/legacy/Router.d.ts +151 -0
- package/dist/legacy/Router.d.ts.map +1 -0
- package/dist/legacy/addPlugins.d.ts +9 -0
- package/dist/legacy/addPlugins.d.ts.map +1 -0
- package/dist/legacy/addRoute.d.ts +16 -0
- package/dist/legacy/addRoute.d.ts.map +1 -0
- package/dist/legacy/createHandlerBoundToURL.d.ts +18 -0
- package/dist/legacy/createHandlerBoundToURL.d.ts.map +1 -0
- package/dist/legacy/fallbacks.d.ts +59 -0
- package/dist/legacy/fallbacks.d.ts.map +1 -0
- package/dist/legacy/getCacheKeyForURL.d.ts +20 -0
- package/dist/legacy/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/legacy/handlePrecaching.d.ts +54 -0
- package/dist/legacy/handlePrecaching.d.ts.map +1 -0
- package/dist/legacy/installSerwist.d.ts +15 -0
- package/dist/legacy/installSerwist.d.ts.map +1 -0
- package/dist/legacy/matchPrecache.d.ts +15 -0
- package/dist/legacy/matchPrecache.d.ts.map +1 -0
- package/dist/legacy/precache.d.ts +20 -0
- package/dist/legacy/precache.d.ts.map +1 -0
- package/dist/legacy/precacheAndRoute.d.ts +15 -0
- package/dist/legacy/precacheAndRoute.d.ts.map +1 -0
- package/dist/legacy/registerRoute.d.ts +16 -0
- package/dist/legacy/registerRoute.d.ts.map +1 -0
- package/dist/legacy/registerRuntimeCaching.d.ts +11 -0
- package/dist/legacy/registerRuntimeCaching.d.ts.map +1 -0
- package/dist/legacy/setCatchHandler.d.ts +10 -0
- package/dist/legacy/setCatchHandler.d.ts.map +1 -0
- package/dist/legacy/setDefaultHandler.d.ts +13 -0
- package/dist/legacy/setDefaultHandler.d.ts.map +1 -0
- package/dist/legacy/singletonPrecacheController.d.ts +34 -0
- package/dist/legacy/singletonPrecacheController.d.ts.map +1 -0
- package/dist/legacy/singletonRouter.d.ts +41 -0
- package/dist/legacy/singletonRouter.d.ts.map +1 -0
- package/dist/legacy/unregisterRoute.d.ts +9 -0
- package/dist/legacy/unregisterRoute.d.ts.map +1 -0
- package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
- package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/legacy/utils/getCacheKeyForURL.d.ts +14 -0
- package/dist/legacy/utils/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/models/messages/messageGenerator.d.ts +4 -0
- package/dist/models/messages/messageGenerator.d.ts.map +1 -0
- package/dist/models/messages/messages.d.ts +44 -0
- package/dist/models/messages/messages.d.ts.map +1 -0
- package/dist/models/pluginEvents.d.ts +10 -0
- package/dist/models/pluginEvents.d.ts.map +1 -0
- package/dist/models/quotaErrorCallbacks.d.ts +3 -0
- package/dist/models/quotaErrorCallbacks.d.ts.map +1 -0
- package/dist/navigationPreload.d.ts +24 -0
- package/dist/navigationPreload.d.ts.map +1 -0
- package/dist/parseRoute.d.ts +16 -0
- package/dist/parseRoute.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
- package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
- package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
- package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts +30 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +53 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
- package/dist/registerQuotaErrorCallback.d.ts +8 -0
- package/dist/registerQuotaErrorCallback.d.ts.map +1 -0
- package/dist/setCacheNameDetails.d.ts +9 -0
- package/dist/setCacheNameDetails.d.ts.map +1 -0
- package/dist/strategies/CacheFirst.d.ts +23 -0
- package/dist/strategies/CacheFirst.d.ts.map +1 -0
- package/dist/strategies/CacheOnly.d.ts +20 -0
- package/dist/strategies/CacheOnly.d.ts.map +1 -0
- package/dist/strategies/NetworkFirst.d.ts +61 -0
- package/dist/strategies/NetworkFirst.d.ts.map +1 -0
- package/dist/strategies/NetworkOnly.d.ts +32 -0
- package/dist/strategies/NetworkOnly.d.ts.map +1 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
- package/dist/strategies/Strategy.d.ts +83 -0
- package/dist/strategies/Strategy.d.ts.map +1 -0
- package/dist/strategies/StrategyHandler.d.ts +189 -0
- package/dist/strategies/StrategyHandler.d.ts.map +1 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
- package/dist/strategies/utils/messages.d.ts +5 -0
- package/dist/strategies/utils/messages.d.ts.map +1 -0
- package/dist/types.d.ts +317 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/Deferred.d.ts +19 -0
- package/dist/utils/Deferred.d.ts.map +1 -0
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/utils/PrecacheInstallReportPlugin.d.ts +14 -0
- package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
- package/dist/utils/SerwistError.d.ts +24 -0
- package/dist/utils/SerwistError.d.ts.map +1 -0
- package/dist/utils/assert.d.ts +11 -0
- package/dist/utils/assert.d.ts.map +1 -0
- package/dist/utils/cacheMatchIgnoreParams.d.ts +15 -0
- package/dist/utils/cacheMatchIgnoreParams.d.ts.map +1 -0
- package/dist/utils/cacheNames.d.ts +40 -0
- package/dist/utils/cacheNames.d.ts.map +1 -0
- package/dist/utils/canConstructReadableStream.d.ts +12 -0
- package/dist/utils/canConstructReadableStream.d.ts.map +1 -0
- package/dist/utils/canConstructResponseFromBodyStream.d.ts +11 -0
- package/dist/utils/canConstructResponseFromBodyStream.d.ts.map +1 -0
- package/dist/utils/createCacheKey.d.ts +16 -0
- package/dist/utils/createCacheKey.d.ts.map +1 -0
- package/dist/utils/deleteOutdatedCaches.d.ts +18 -0
- package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -0
- package/dist/utils/dontWaitFor.d.ts +7 -0
- package/dist/utils/dontWaitFor.d.ts.map +1 -0
- package/dist/utils/executeQuotaErrorCallbacks.d.ts +8 -0
- package/dist/utils/executeQuotaErrorCallbacks.d.ts.map +1 -0
- package/dist/utils/generateURLVariations.d.ts +12 -0
- package/dist/utils/generateURLVariations.d.ts.map +1 -0
- package/dist/utils/getFriendlyURL.d.ts +3 -0
- package/dist/utils/getFriendlyURL.d.ts.map +1 -0
- package/dist/utils/logger.d.ts +24 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/normalizeHandler.d.ts +10 -0
- package/dist/utils/normalizeHandler.d.ts.map +1 -0
- package/dist/utils/pluginUtils.d.ts +5 -0
- package/dist/utils/pluginUtils.d.ts.map +1 -0
- package/dist/utils/printCleanupDetails.d.ts +6 -0
- package/dist/utils/printCleanupDetails.d.ts.map +1 -0
- package/dist/utils/printInstallDetails.d.ts +7 -0
- package/dist/utils/printInstallDetails.d.ts.map +1 -0
- package/dist/utils/removeIgnoredSearchParams.d.ts +12 -0
- package/dist/utils/removeIgnoredSearchParams.d.ts.map +1 -0
- package/dist/utils/resultingClientExists.d.ts +12 -0
- package/dist/utils/resultingClientExists.d.ts.map +1 -0
- package/dist/utils/timeout.d.ts +10 -0
- package/dist/utils/timeout.d.ts.map +1 -0
- package/dist/utils/waitUntil.d.ts +11 -0
- package/dist/utils/waitUntil.d.ts.map +1 -0
- package/dist/utils/welcome.d.ts +2 -0
- package/dist/utils/welcome.d.ts.map +1 -0
- package/package.json +85 -0
- package/src/NavigationRoute.ts +119 -0
- package/src/PrecacheRoute.ts +46 -0
- package/src/PrecacheStrategy.ts +239 -0
- package/src/RegExpRoute.ts +74 -0
- package/src/Route.ts +67 -0
- package/src/Serwist.ts +920 -0
- package/src/cacheNames.ts +39 -0
- package/src/cleanupOutdatedCaches.ts +32 -0
- package/src/clientsClaim.ts +18 -0
- package/src/constants.ts +24 -0
- package/src/copyResponse.ts +60 -0
- package/src/disableDevLogs.ts +10 -0
- package/src/index.internal.ts +33 -0
- package/src/index.legacy.ts +66 -0
- package/src/index.plugins.ts +95 -0
- package/src/index.strategies.ts +26 -0
- package/src/index.ts +39 -0
- package/src/legacy/PrecacheController.ts +337 -0
- package/src/legacy/PrecacheFallbackPlugin.ts +93 -0
- package/src/legacy/PrecacheRoute.ts +48 -0
- package/src/legacy/Router.ts +484 -0
- package/src/legacy/addPlugins.ts +21 -0
- package/src/legacy/addRoute.ts +29 -0
- package/src/legacy/createHandlerBoundToURL.ts +30 -0
- package/src/legacy/fallbacks.ts +94 -0
- package/src/legacy/getCacheKeyForURL.ts +32 -0
- package/src/legacy/handlePrecaching.ts +86 -0
- package/src/legacy/installSerwist.ts +19 -0
- package/src/legacy/matchPrecache.ts +26 -0
- package/src/legacy/precache.ts +31 -0
- package/src/legacy/precacheAndRoute.ts +28 -0
- package/src/legacy/registerRoute.ts +27 -0
- package/src/legacy/registerRuntimeCaching.ts +17 -0
- package/src/legacy/setCatchHandler.ts +21 -0
- package/src/legacy/setDefaultHandler.ts +24 -0
- package/src/legacy/singletonPrecacheController.ts +53 -0
- package/src/legacy/singletonRouter.ts +70 -0
- package/src/legacy/unregisterRoute.ts +12 -0
- package/src/legacy/utils/PrecacheCacheKeyPlugin.ts +33 -0
- package/src/legacy/utils/getCacheKeyForURL.ts +36 -0
- package/src/models/messages/messageGenerator.ts +29 -0
- package/src/models/messages/messages.ts +233 -0
- package/src/models/pluginEvents.ts +17 -0
- package/src/models/quotaErrorCallbacks.ts +13 -0
- package/src/navigationPreload.ts +68 -0
- package/src/parseRoute.ts +78 -0
- package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +38 -0
- package/src/plugins/backgroundSync/Queue.ts +440 -0
- package/src/plugins/backgroundSync/QueueDb.ts +176 -0
- package/src/plugins/backgroundSync/QueueStore.ts +160 -0
- package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
- package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +161 -0
- package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +42 -0
- package/src/plugins/broadcastUpdate/constants.ts +12 -0
- package/src/plugins/broadcastUpdate/responsesAreSame.ts +49 -0
- package/src/plugins/broadcastUpdate/types.ts +37 -0
- package/src/plugins/cacheableResponse/CacheableResponse.ts +144 -0
- package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +45 -0
- package/src/plugins/expiration/CacheExpiration.ts +193 -0
- package/src/plugins/expiration/ExpirationPlugin.ts +300 -0
- package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
- package/src/plugins/googleAnalytics/constants.ts +22 -0
- package/src/plugins/googleAnalytics/initialize.ts +209 -0
- package/src/plugins/precaching/PrecacheFallbackPlugin.ts +83 -0
- package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +38 -0
- package/src/plugins/rangeRequests/createPartialResponse.ts +93 -0
- package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +59 -0
- package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +55 -0
- package/src/registerQuotaErrorCallback.ts +34 -0
- package/src/setCacheNameDetails.ts +53 -0
- package/src/strategies/CacheFirst.ts +88 -0
- package/src/strategies/CacheOnly.ts +59 -0
- package/src/strategies/NetworkFirst.ts +229 -0
- package/src/strategies/NetworkOnly.ts +98 -0
- package/src/strategies/StaleWhileRevalidate.ts +110 -0
- package/src/strategies/Strategy.ts +204 -0
- package/src/strategies/StrategyHandler.ts +554 -0
- package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
- package/src/strategies/utils/messages.ts +21 -0
- package/src/types.ts +358 -0
- package/src/utils/Deferred.ts +33 -0
- package/src/utils/PrecacheCacheKeyPlugin.ts +33 -0
- package/src/utils/PrecacheInstallReportPlugin.ts +47 -0
- package/src/utils/SerwistError.ts +41 -0
- package/src/utils/assert.ts +89 -0
- package/src/utils/cacheMatchIgnoreParams.ts +54 -0
- package/src/utils/cacheNames.ts +87 -0
- package/src/utils/canConstructReadableStream.ts +34 -0
- package/src/utils/canConstructResponseFromBodyStream.ts +37 -0
- package/src/utils/createCacheKey.ts +68 -0
- package/src/utils/deleteOutdatedCaches.ts +40 -0
- package/src/utils/dontWaitFor.ts +16 -0
- package/src/utils/executeQuotaErrorCallbacks.ts +33 -0
- package/src/utils/generateURLVariations.ts +55 -0
- package/src/utils/getFriendlyURL.ts +16 -0
- package/src/utils/logger.ts +95 -0
- package/src/utils/normalizeHandler.ts +40 -0
- package/src/utils/pluginUtils.ts +15 -0
- package/src/utils/printCleanupDetails.ts +38 -0
- package/src/utils/printInstallDetails.ts +53 -0
- package/src/utils/removeIgnoredSearchParams.ts +29 -0
- package/src/utils/resultingClientExists.ts +58 -0
- package/src/utils/timeout.ts +19 -0
- package/src/utils/waitUntil.ts +21 -0
- package/src/utils/welcome.ts +19 -0
|
@@ -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 "./types.js";
|
|
10
|
+
import { assert } from "./utils/assert.js";
|
|
11
|
+
import { logger } from "./utils/logger.js";
|
|
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 `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",
|
|
64
|
+
className: "NavigationRoute",
|
|
65
|
+
funcName: "constructor",
|
|
66
|
+
paramName: "options.allowlist",
|
|
67
|
+
});
|
|
68
|
+
assert!.isArrayOfClass(denylist, RegExp, {
|
|
69
|
+
moduleName: "serwist",
|
|
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,46 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 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 { RouteMatchCallback, RouteMatchCallbackOptions } from "./types.js";
|
|
10
|
+
import { getFriendlyURL } from "./utils/getFriendlyURL.js";
|
|
11
|
+
import { logger } from "./utils/logger.js";
|
|
12
|
+
import { Route } from "./Route.js";
|
|
13
|
+
import type { Serwist } from "./Serwist.js";
|
|
14
|
+
import type { PrecacheRouteOptions } from "./types.js";
|
|
15
|
+
import { generateURLVariations } from "./utils/generateURLVariations.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A subclass of `Route` that takes a `serwist.Serwist` instance and uses it to match
|
|
19
|
+
* incoming requests and handle fetching responses from the precache.
|
|
20
|
+
*/
|
|
21
|
+
export class PrecacheRoute extends Route {
|
|
22
|
+
/**
|
|
23
|
+
* @param serwist A `PrecacheController`
|
|
24
|
+
* instance used to both match requests and respond to fetch events.
|
|
25
|
+
* @param options Options to control how requests are matched
|
|
26
|
+
* against the list of precached URLs.
|
|
27
|
+
*/
|
|
28
|
+
constructor(serwist: Serwist, options?: PrecacheRouteOptions) {
|
|
29
|
+
const match: RouteMatchCallback = ({ request }: RouteMatchCallbackOptions) => {
|
|
30
|
+
const urlsToCacheKeys = serwist.getUrlsToPrecacheKeys();
|
|
31
|
+
for (const possibleURL of generateURLVariations(request.url, options)) {
|
|
32
|
+
const cacheKey = urlsToCacheKeys.get(possibleURL);
|
|
33
|
+
if (cacheKey) {
|
|
34
|
+
const integrity = serwist.getIntegrityForPrecacheKey(cacheKey);
|
|
35
|
+
return { cacheKey, integrity };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (process.env.NODE_ENV !== "production") {
|
|
39
|
+
logger.debug(`Precaching did not find a match for ${getFriendlyURL(request.url)}.`);
|
|
40
|
+
}
|
|
41
|
+
return;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
super(match, serwist.precacheStrategy);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 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 { SerwistPlugin } from "./types.js";
|
|
10
|
+
import { copyResponse } from "./copyResponse.js";
|
|
11
|
+
import { SerwistError } from "./utils/SerwistError.js";
|
|
12
|
+
import { getFriendlyURL } from "./utils/getFriendlyURL.js";
|
|
13
|
+
import { logger } from "./utils/logger.js";
|
|
14
|
+
import { cacheNames as privateCacheNames } from "./utils/cacheNames.js";
|
|
15
|
+
import type { StrategyOptions } from "./strategies/Strategy.js";
|
|
16
|
+
import { Strategy } from "./strategies/Strategy.js";
|
|
17
|
+
import type { StrategyHandler } from "./strategies/StrategyHandler.js";
|
|
18
|
+
|
|
19
|
+
interface PrecacheStrategyOptions extends StrategyOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Whether to attempt to get the response from the network
|
|
22
|
+
* if there's a precache miss.
|
|
23
|
+
*/
|
|
24
|
+
fallbackToNetwork?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A `serwist/strategies.Strategy` implementation
|
|
29
|
+
* specifically designed to both cache and fetch precached assets.
|
|
30
|
+
*
|
|
31
|
+
* Note: an instance of this class is created automatically when creating a
|
|
32
|
+
* `PrecacheController`; it's generally not necessary to create this yourself.
|
|
33
|
+
*/
|
|
34
|
+
export class PrecacheStrategy extends Strategy {
|
|
35
|
+
private readonly _fallbackToNetwork: boolean;
|
|
36
|
+
|
|
37
|
+
static readonly defaultPrecacheCacheabilityPlugin: SerwistPlugin = {
|
|
38
|
+
async cacheWillUpdate({ response }) {
|
|
39
|
+
if (!response || response.status >= 400) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return response;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
static readonly copyRedirectedCacheableResponsesPlugin: SerwistPlugin = {
|
|
48
|
+
async cacheWillUpdate({ response }) {
|
|
49
|
+
return response.redirected ? await copyResponse(response) : response;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param options
|
|
55
|
+
*/
|
|
56
|
+
constructor(options: PrecacheStrategyOptions = {}) {
|
|
57
|
+
options.cacheName = privateCacheNames.getPrecacheName(options.cacheName);
|
|
58
|
+
super(options);
|
|
59
|
+
|
|
60
|
+
this._fallbackToNetwork = options.fallbackToNetwork === false ? false : true;
|
|
61
|
+
|
|
62
|
+
// Redirected responses cannot be used to satisfy a navigation request, so
|
|
63
|
+
// any redirected response must be "copied" rather than cloned, so the new
|
|
64
|
+
// response doesn't contain the `redirected` flag. See:
|
|
65
|
+
// https://bugs.chromium.org/p/chromium/issues/detail?id=669363&desc=2#c1
|
|
66
|
+
this.plugins.push(PrecacheStrategy.copyRedirectedCacheableResponsesPlugin);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @private
|
|
71
|
+
* @param request A request to run this strategy for.
|
|
72
|
+
* @param handler The event that triggered the request.
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
async _handle(request: Request, handler: StrategyHandler): Promise<Response> {
|
|
76
|
+
const response = await handler.cacheMatch(request);
|
|
77
|
+
if (response) {
|
|
78
|
+
return response;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// If this is an `install` event for an entry that isn't already cached,
|
|
82
|
+
// then populate the cache.
|
|
83
|
+
if (handler.event && handler.event.type === "install") {
|
|
84
|
+
return await this._handleInstall(request, handler);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Getting here means something went wrong. An entry that should have been
|
|
88
|
+
// precached wasn't found in the cache.
|
|
89
|
+
return await this._handleFetch(request, handler);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async _handleFetch(request: Request, handler: StrategyHandler): Promise<Response> {
|
|
93
|
+
let response: Response | undefined = undefined;
|
|
94
|
+
const params = (handler.params || {}) as {
|
|
95
|
+
cacheKey?: string;
|
|
96
|
+
integrity?: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Fallback to the network if we're configured to do so.
|
|
100
|
+
if (this._fallbackToNetwork) {
|
|
101
|
+
if (process.env.NODE_ENV !== "production") {
|
|
102
|
+
logger.warn(`The precached response for ${getFriendlyURL(request.url)} in ${this.cacheName} was not found. Falling back to the network.`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const integrityInManifest = params.integrity;
|
|
106
|
+
const integrityInRequest = request.integrity;
|
|
107
|
+
const noIntegrityConflict = !integrityInRequest || integrityInRequest === integrityInManifest;
|
|
108
|
+
|
|
109
|
+
// Do not add integrity if the original request is no-cors
|
|
110
|
+
// See https://github.com/GoogleChrome/workbox/issues/3096
|
|
111
|
+
response = await handler.fetch(
|
|
112
|
+
new Request(request, {
|
|
113
|
+
integrity: request.mode !== "no-cors" ? integrityInRequest || integrityInManifest : undefined,
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// It's only "safe" to repair the cache if we're using SRI to guarantee
|
|
118
|
+
// that the response matches the precache manifest's expectations,
|
|
119
|
+
// and there's either a) no integrity property in the incoming request
|
|
120
|
+
// or b) there is an integrity, and it matches the precache manifest.
|
|
121
|
+
// See https://github.com/GoogleChrome/workbox/issues/2858
|
|
122
|
+
// Also if the original request users no-cors we don't use integrity.
|
|
123
|
+
// See https://github.com/GoogleChrome/workbox/issues/3096
|
|
124
|
+
if (integrityInManifest && noIntegrityConflict && request.mode !== "no-cors") {
|
|
125
|
+
this._useDefaultCacheabilityPluginIfNeeded();
|
|
126
|
+
const wasCached = await handler.cachePut(request, response.clone());
|
|
127
|
+
if (process.env.NODE_ENV !== "production") {
|
|
128
|
+
if (wasCached) {
|
|
129
|
+
logger.log(`A response for ${getFriendlyURL(request.url)} was used to "repair" the precache.`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
// This shouldn't normally happen, but there are edge cases:
|
|
135
|
+
// https://github.com/GoogleChrome/workbox/issues/1441
|
|
136
|
+
throw new SerwistError("missing-precache-entry", {
|
|
137
|
+
cacheName: this.cacheName,
|
|
138
|
+
url: request.url,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (process.env.NODE_ENV !== "production") {
|
|
143
|
+
const cacheKey = params.cacheKey || (await handler.getCacheKey(request, "read"));
|
|
144
|
+
|
|
145
|
+
// Serwist is going to handle the route.
|
|
146
|
+
// print the routing details to the console.
|
|
147
|
+
logger.groupCollapsed(`Precaching is responding to: ${getFriendlyURL(request.url)}`);
|
|
148
|
+
logger.log(`Serving the precached url: ${getFriendlyURL(cacheKey instanceof Request ? cacheKey.url : cacheKey)}`);
|
|
149
|
+
|
|
150
|
+
logger.groupCollapsed("View request details here.");
|
|
151
|
+
logger.log(request);
|
|
152
|
+
logger.groupEnd();
|
|
153
|
+
|
|
154
|
+
logger.groupCollapsed("View response details here.");
|
|
155
|
+
logger.log(response);
|
|
156
|
+
logger.groupEnd();
|
|
157
|
+
|
|
158
|
+
logger.groupEnd();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return response;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async _handleInstall(request: Request, handler: StrategyHandler): Promise<Response> {
|
|
165
|
+
this._useDefaultCacheabilityPluginIfNeeded();
|
|
166
|
+
|
|
167
|
+
const response = await handler.fetch(request);
|
|
168
|
+
|
|
169
|
+
// Make sure we defer cachePut() until after we know the response
|
|
170
|
+
// should be cached; see https://github.com/GoogleChrome/workbox/issues/2737
|
|
171
|
+
const wasCached = await handler.cachePut(request, response.clone());
|
|
172
|
+
if (!wasCached) {
|
|
173
|
+
// Throwing here will lead to the `install` handler failing, which
|
|
174
|
+
// we want to do if *any* of the responses aren't safe to cache.
|
|
175
|
+
throw new SerwistError("bad-precaching-response", {
|
|
176
|
+
url: request.url,
|
|
177
|
+
status: response.status,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return response;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* This method is complex, as there a number of things to account for:
|
|
186
|
+
*
|
|
187
|
+
* The `plugins` array can be set at construction, and/or it might be added to
|
|
188
|
+
* to at any time before the strategy is used.
|
|
189
|
+
*
|
|
190
|
+
* At the time the strategy is used (i.e. during an `install` event), there
|
|
191
|
+
* needs to be at least one plugin that implements `cacheWillUpdate` in the
|
|
192
|
+
* array, other than `copyRedirectedCacheableResponsesPlugin`.
|
|
193
|
+
*
|
|
194
|
+
* - If this method is called and there are no suitable `cacheWillUpdate`
|
|
195
|
+
* plugins, we need to add `defaultPrecacheCacheabilityPlugin`.
|
|
196
|
+
*
|
|
197
|
+
* - If this method is called and there is exactly one `cacheWillUpdate`, then
|
|
198
|
+
* we don't have to do anything (this might be a previously added
|
|
199
|
+
* `defaultPrecacheCacheabilityPlugin`, or it might be a custom plugin).
|
|
200
|
+
*
|
|
201
|
+
* - If this method is called and there is more than one `cacheWillUpdate`,
|
|
202
|
+
* then we need to check if one is `defaultPrecacheCacheabilityPlugin`. If so,
|
|
203
|
+
* we need to remove it. (This situation is unlikely, but it could happen if
|
|
204
|
+
* the strategy is used multiple times, the first without a `cacheWillUpdate`,
|
|
205
|
+
* and then later on after manually adding a custom `cacheWillUpdate`.)
|
|
206
|
+
*
|
|
207
|
+
* See https://github.com/GoogleChrome/workbox/issues/2737 for more context.
|
|
208
|
+
*
|
|
209
|
+
* @private
|
|
210
|
+
*/
|
|
211
|
+
_useDefaultCacheabilityPluginIfNeeded(): void {
|
|
212
|
+
let defaultPluginIndex: number | null = null;
|
|
213
|
+
let cacheWillUpdatePluginCount = 0;
|
|
214
|
+
|
|
215
|
+
for (const [index, plugin] of this.plugins.entries()) {
|
|
216
|
+
// Ignore the copy redirected plugin when determining what to do.
|
|
217
|
+
if (plugin === PrecacheStrategy.copyRedirectedCacheableResponsesPlugin) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Save the default plugin's index, in case it needs to be removed.
|
|
222
|
+
if (plugin === PrecacheStrategy.defaultPrecacheCacheabilityPlugin) {
|
|
223
|
+
defaultPluginIndex = index;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (plugin.cacheWillUpdate) {
|
|
227
|
+
cacheWillUpdatePluginCount++;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (cacheWillUpdatePluginCount === 0) {
|
|
232
|
+
this.plugins.push(PrecacheStrategy.defaultPrecacheCacheabilityPlugin);
|
|
233
|
+
} else if (cacheWillUpdatePluginCount > 1 && defaultPluginIndex !== null) {
|
|
234
|
+
// Only remove the default plugin; multiple custom plugins are allowed.
|
|
235
|
+
this.plugins.splice(defaultPluginIndex, 1);
|
|
236
|
+
}
|
|
237
|
+
// Nothing needs to be done if cacheWillUpdatePluginCount is 1
|
|
238
|
+
}
|
|
239
|
+
}
|
|
@@ -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 "./types.js";
|
|
10
|
+
import { assert } from "./utils/assert.js";
|
|
11
|
+
import { logger } from "./utils/logger.js";
|
|
12
|
+
import { Route } from "./Route.js";
|
|
13
|
+
import type { HTTPMethod } from "./constants.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `RegExpRoute` makes it easy to create a regular expression based on a `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",
|
|
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,67 @@
|
|
|
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 "./types.js";
|
|
10
|
+
import { assert } from "./utils/assert.js";
|
|
11
|
+
import type { HTTPMethod } from "./constants.js";
|
|
12
|
+
import { defaultMethod, validMethods } from "./constants.js";
|
|
13
|
+
import { normalizeHandler } from "./utils/normalizeHandler.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A `Route` consists of a pair of callback functions, "match" and "handler".
|
|
17
|
+
* The "match" callback determine if a route should be used to "handle" a
|
|
18
|
+
* request by returning a non-falsy value if it can. The "handler" callback
|
|
19
|
+
* is called when there is a match and should return a Promise that resolves
|
|
20
|
+
* to a `Response`.
|
|
21
|
+
*/
|
|
22
|
+
export class Route {
|
|
23
|
+
handler: RouteHandlerObject;
|
|
24
|
+
match: RouteMatchCallback;
|
|
25
|
+
method: HTTPMethod;
|
|
26
|
+
catchHandler?: RouteHandlerObject;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Constructor for Route class.
|
|
30
|
+
*
|
|
31
|
+
* @param match A callback function that determines whether the
|
|
32
|
+
* route matches a given `fetch` event by returning a non-falsy value.
|
|
33
|
+
* @param handler A callback function that returns a Promise resolving
|
|
34
|
+
* to a Response.
|
|
35
|
+
* @param method The HTTP method to match the Route against. Defaults
|
|
36
|
+
* to GET.
|
|
37
|
+
*/
|
|
38
|
+
constructor(match: RouteMatchCallback, handler: RouteHandler, method: HTTPMethod = defaultMethod) {
|
|
39
|
+
if (process.env.NODE_ENV !== "production") {
|
|
40
|
+
assert!.isType(match, "function", {
|
|
41
|
+
moduleName: "serwist",
|
|
42
|
+
className: "Route",
|
|
43
|
+
funcName: "constructor",
|
|
44
|
+
paramName: "match",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (method) {
|
|
48
|
+
assert!.isOneOf(method, validMethods, { paramName: "method" });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// These values are referenced directly by Router so cannot be
|
|
53
|
+
// altered by minificaton.
|
|
54
|
+
this.handler = normalizeHandler(handler);
|
|
55
|
+
this.match = match;
|
|
56
|
+
this.method = method;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
* @param handler A callback function that returns a Promise resolving
|
|
62
|
+
* to a Response.
|
|
63
|
+
*/
|
|
64
|
+
setCatchHandler(handler: RouteHandler): void {
|
|
65
|
+
this.catchHandler = normalizeHandler(handler);
|
|
66
|
+
}
|
|
67
|
+
}
|