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
package/src/types.ts
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import type { HTTPMethod } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
export type PromiseOrNot<T> = T | Promise<T>;
|
|
4
|
+
|
|
5
|
+
export interface MapLikeObject {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Using a plain `MapLikeObject` for now, but could extend/restrict this
|
|
11
|
+
* in the future.
|
|
12
|
+
*/
|
|
13
|
+
export type PluginState = MapLikeObject;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Options passed to a `RouteMatchCallback` function.
|
|
17
|
+
*/
|
|
18
|
+
export interface RouteMatchCallbackOptions {
|
|
19
|
+
event: ExtendableEvent;
|
|
20
|
+
request: Request;
|
|
21
|
+
sameOrigin: boolean;
|
|
22
|
+
url: URL;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The "match" callback is used to determine if a `Route` should apply for a
|
|
27
|
+
* particular URL and request. When matching occurs in response to a fetch
|
|
28
|
+
* event from the client, the `event` object is also supplied. However, since
|
|
29
|
+
* the match callback can be invoked outside of a fetch event, matching logic
|
|
30
|
+
* should not assume the `event` object will always be available.
|
|
31
|
+
* If the match callback returns a truthy value, the matching route's
|
|
32
|
+
* `RouteHandlerCallback` will be invoked immediately. If the value returned
|
|
33
|
+
* is a non-empty array or object, that value will be set on the handler's
|
|
34
|
+
* `options.params` argument.
|
|
35
|
+
*/
|
|
36
|
+
export type RouteMatchCallback = (options: RouteMatchCallbackOptions) => any;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Options passed to a `RouteHandlerCallback` function.
|
|
40
|
+
*/
|
|
41
|
+
export interface RouteHandlerCallbackOptions {
|
|
42
|
+
/**
|
|
43
|
+
* The event associated with the request.
|
|
44
|
+
*/
|
|
45
|
+
event: ExtendableEvent;
|
|
46
|
+
/**
|
|
47
|
+
* A request to run this strategy for.
|
|
48
|
+
*/
|
|
49
|
+
request: Request;
|
|
50
|
+
url: URL;
|
|
51
|
+
params?: string[] | MapLikeObject;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Options passed to a `ManualHandlerCallback` function.
|
|
55
|
+
*/
|
|
56
|
+
export interface ManualHandlerCallbackOptions {
|
|
57
|
+
/**
|
|
58
|
+
* The event associated with the request.
|
|
59
|
+
*/
|
|
60
|
+
event: ExtendableEvent;
|
|
61
|
+
/**
|
|
62
|
+
* A request to run this strategy for.
|
|
63
|
+
*/
|
|
64
|
+
request: Request | string;
|
|
65
|
+
url?: never;
|
|
66
|
+
/**
|
|
67
|
+
* The return value from `serwist.matchCallback` (if applicable).
|
|
68
|
+
*/
|
|
69
|
+
params?: never;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type HandlerCallbackOptions = RouteHandlerCallbackOptions | ManualHandlerCallbackOptions;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The "handler" callback is invoked whenever a `Router` matches a URL/Request
|
|
76
|
+
* to a `Route` via its `RouteMatchCallback`. This handler callback should
|
|
77
|
+
* return a `Promise` that resolves with a `Response`.
|
|
78
|
+
*
|
|
79
|
+
* If a non-empty array or object is returned by the `RouteMatchCallback` it
|
|
80
|
+
* will be passed in as this handler's `options.params` argument.
|
|
81
|
+
*/
|
|
82
|
+
export type RouteHandlerCallback = (options: RouteHandlerCallbackOptions) => Promise<Response>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The "handler" callback is invoked whenever a `Router` matches a URL/Request
|
|
86
|
+
* to a `Route` via its `RouteMatchCallback`. This handler callback should
|
|
87
|
+
* return a `Promise` that resolves with a `Response`.
|
|
88
|
+
*
|
|
89
|
+
* If a non-empty array or object is returned by the `RouteMatchCallback` it
|
|
90
|
+
* will be passed in as this handler's `options.params` argument.
|
|
91
|
+
*/
|
|
92
|
+
export type ManualHandlerCallback = (options: ManualHandlerCallbackOptions) => Promise<Response>;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* An object with a `handle` method of type `RouteHandlerCallback`.
|
|
96
|
+
*
|
|
97
|
+
* A `Route` object can be created with either an `RouteHandlerCallback`
|
|
98
|
+
* function or this `RouteHandler` object. The benefit of the `RouteHandler`
|
|
99
|
+
* is it can be extended (as is done by the `serwist/strategies` package).
|
|
100
|
+
*/
|
|
101
|
+
export interface RouteHandlerObject {
|
|
102
|
+
handle: RouteHandlerCallback;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Either a `RouteHandlerCallback` or a `RouteHandlerObject`.
|
|
107
|
+
* Most APIs that accept route handlers take either.
|
|
108
|
+
*/
|
|
109
|
+
export type RouteHandler = RouteHandlerCallback | RouteHandlerObject;
|
|
110
|
+
|
|
111
|
+
export interface HandlerWillStartCallbackParam {
|
|
112
|
+
request: Request;
|
|
113
|
+
event: ExtendableEvent;
|
|
114
|
+
state?: PluginState;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type HandlerWillStartCallback = (param: HandlerWillStartCallbackParam) => Promise<any>;
|
|
118
|
+
|
|
119
|
+
export interface CacheDidUpdateCallbackParam {
|
|
120
|
+
/**
|
|
121
|
+
* Name of the cache the responses belong to. This is included in the
|
|
122
|
+
* broadcast message.
|
|
123
|
+
*/
|
|
124
|
+
cacheName: string;
|
|
125
|
+
/**
|
|
126
|
+
* Possibly updated response to compare.
|
|
127
|
+
*/
|
|
128
|
+
newResponse: Response;
|
|
129
|
+
/**
|
|
130
|
+
* The `Request` object for the cached entry.
|
|
131
|
+
*/
|
|
132
|
+
request: Request;
|
|
133
|
+
/**
|
|
134
|
+
* The event that triggered this possible cache update.
|
|
135
|
+
*/
|
|
136
|
+
event: ExtendableEvent;
|
|
137
|
+
/**
|
|
138
|
+
* Cached response to compare.
|
|
139
|
+
*/
|
|
140
|
+
oldResponse?: Response | null;
|
|
141
|
+
state?: PluginState;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type CacheDidUpdateCallback = (param: CacheDidUpdateCallbackParam) => PromiseOrNot<any>;
|
|
145
|
+
|
|
146
|
+
export interface CacheKeyWillBeUsedCallbackParam {
|
|
147
|
+
mode: string;
|
|
148
|
+
request: Request;
|
|
149
|
+
event: ExtendableEvent;
|
|
150
|
+
params?: any;
|
|
151
|
+
state?: PluginState;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export type CacheKeyWillBeUsedCallback = (param: CacheKeyWillBeUsedCallbackParam) => PromiseOrNot<Request | string>;
|
|
155
|
+
|
|
156
|
+
export interface CacheWillUpdateCallbackParam {
|
|
157
|
+
request: Request;
|
|
158
|
+
response: Response;
|
|
159
|
+
event: ExtendableEvent;
|
|
160
|
+
state?: PluginState;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export type CacheWillUpdateCallback = (param: CacheWillUpdateCallbackParam) => PromiseOrNot<any>;
|
|
164
|
+
|
|
165
|
+
export interface CachedResponseWillBeUsedCallbackParam {
|
|
166
|
+
/**
|
|
167
|
+
* Name of the cache the response is in.
|
|
168
|
+
*/
|
|
169
|
+
cacheName: string;
|
|
170
|
+
/**
|
|
171
|
+
* The original request, which may or may not
|
|
172
|
+
* contain a Range: header.
|
|
173
|
+
*/
|
|
174
|
+
request: Request;
|
|
175
|
+
/**
|
|
176
|
+
* The complete cached `Response` object that's been read
|
|
177
|
+
* from a cache and whose freshness should be checked.
|
|
178
|
+
*/
|
|
179
|
+
cachedResponse?: Response;
|
|
180
|
+
event: ExtendableEvent;
|
|
181
|
+
matchOptions?: CacheQueryOptions;
|
|
182
|
+
state?: PluginState;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export type CachedResponseWillBeUsedCallback = (param: CachedResponseWillBeUsedCallbackParam) => PromiseOrNot<any>;
|
|
186
|
+
|
|
187
|
+
export interface FetchDidFailCallbackParam {
|
|
188
|
+
error: Error;
|
|
189
|
+
originalRequest: Request;
|
|
190
|
+
request: Request;
|
|
191
|
+
event: ExtendableEvent;
|
|
192
|
+
state?: PluginState;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export type FetchDidFailCallback = (param: FetchDidFailCallbackParam) => PromiseOrNot<any>;
|
|
196
|
+
|
|
197
|
+
export interface FetchDidSucceedCallbackParam {
|
|
198
|
+
request: Request;
|
|
199
|
+
response: Response;
|
|
200
|
+
event: ExtendableEvent;
|
|
201
|
+
state?: PluginState;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export type FetchDidSucceedCallback = (param: FetchDidSucceedCallbackParam) => PromiseOrNot<Response>;
|
|
205
|
+
|
|
206
|
+
export interface RequestWillFetchCallbackParam {
|
|
207
|
+
request: Request;
|
|
208
|
+
event: ExtendableEvent;
|
|
209
|
+
state?: PluginState;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export type RequestWillFetchCallback = (param: RequestWillFetchCallbackParam) => PromiseOrNot<Request>;
|
|
213
|
+
|
|
214
|
+
export interface HandlerWillRespondCallbackParam {
|
|
215
|
+
request: Request;
|
|
216
|
+
response: Response;
|
|
217
|
+
event: ExtendableEvent;
|
|
218
|
+
state?: PluginState;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export type HandlerWillRespondCallback = (param: HandlerWillRespondCallbackParam) => PromiseOrNot<Response>;
|
|
222
|
+
|
|
223
|
+
export interface HandlerDidErrorCallbackParam {
|
|
224
|
+
request: Request;
|
|
225
|
+
event: ExtendableEvent;
|
|
226
|
+
error: Error;
|
|
227
|
+
state?: PluginState;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export type HandlerDidErrorCallback = (param: HandlerDidErrorCallbackParam) => PromiseOrNot<Response | undefined>;
|
|
231
|
+
|
|
232
|
+
export interface HandlerDidRespondCallbackParam {
|
|
233
|
+
request: Request;
|
|
234
|
+
event: ExtendableEvent;
|
|
235
|
+
response?: Response;
|
|
236
|
+
state?: PluginState;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export type HandlerDidRespondCallback = (param: HandlerDidRespondCallbackParam) => PromiseOrNot<any>;
|
|
240
|
+
|
|
241
|
+
export interface HandlerDidCompleteCallbackParam {
|
|
242
|
+
request: Request;
|
|
243
|
+
error?: Error;
|
|
244
|
+
event: ExtendableEvent;
|
|
245
|
+
response?: Response;
|
|
246
|
+
state?: PluginState;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export type HandlerDidCompleteCallback = (param: HandlerDidCompleteCallbackParam) => PromiseOrNot<any>;
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* An object with optional lifecycle callback properties for the fetch and
|
|
253
|
+
* cache operations.
|
|
254
|
+
*/
|
|
255
|
+
export declare interface SerwistPlugin {
|
|
256
|
+
cacheDidUpdate?: CacheDidUpdateCallback;
|
|
257
|
+
cachedResponseWillBeUsed?: CachedResponseWillBeUsedCallback;
|
|
258
|
+
cacheKeyWillBeUsed?: CacheKeyWillBeUsedCallback;
|
|
259
|
+
cacheWillUpdate?: CacheWillUpdateCallback;
|
|
260
|
+
fetchDidFail?: FetchDidFailCallback;
|
|
261
|
+
fetchDidSucceed?: FetchDidSucceedCallback;
|
|
262
|
+
handlerDidComplete?: HandlerDidCompleteCallback;
|
|
263
|
+
handlerDidError?: HandlerDidErrorCallback;
|
|
264
|
+
handlerDidRespond?: HandlerDidRespondCallback;
|
|
265
|
+
handlerWillRespond?: HandlerWillRespondCallback;
|
|
266
|
+
handlerWillStart?: HandlerWillStartCallback;
|
|
267
|
+
requestWillFetch?: RequestWillFetchCallback;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export interface SerwistPluginCallbackParam {
|
|
271
|
+
cacheDidUpdate: CacheDidUpdateCallbackParam;
|
|
272
|
+
cachedResponseWillBeUsed: CachedResponseWillBeUsedCallbackParam;
|
|
273
|
+
cacheKeyWillBeUsed: CacheKeyWillBeUsedCallbackParam;
|
|
274
|
+
cacheWillUpdate: CacheWillUpdateCallbackParam;
|
|
275
|
+
fetchDidFail: FetchDidFailCallbackParam;
|
|
276
|
+
fetchDidSucceed: FetchDidSucceedCallbackParam;
|
|
277
|
+
handlerDidComplete: HandlerDidCompleteCallbackParam;
|
|
278
|
+
handlerDidError: HandlerDidErrorCallbackParam;
|
|
279
|
+
handlerDidRespond: HandlerDidRespondCallbackParam;
|
|
280
|
+
handlerWillRespond: HandlerWillRespondCallbackParam;
|
|
281
|
+
handlerWillStart: HandlerWillStartCallbackParam;
|
|
282
|
+
requestWillFetch: RequestWillFetchCallbackParam;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export interface SerwistGlobalConfig {
|
|
286
|
+
/**
|
|
287
|
+
* Whether Serwist should disable development logging.
|
|
288
|
+
*
|
|
289
|
+
* @default false
|
|
290
|
+
*/
|
|
291
|
+
__WB_DISABLE_DEV_LOGS: boolean;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export interface RuntimeCaching {
|
|
295
|
+
/**
|
|
296
|
+
* The HTTP method to match against. The default value of `'GET'` is normally
|
|
297
|
+
* sufficient, unless you explicitly need to match `'POST'`, `'PUT'`, or
|
|
298
|
+
* another type of request.
|
|
299
|
+
* @default "GET"
|
|
300
|
+
*/
|
|
301
|
+
method?: HTTPMethod;
|
|
302
|
+
/**
|
|
303
|
+
* This match criteria determines whether the configured handler will
|
|
304
|
+
* generate a response for any requests that don't match one of the precached
|
|
305
|
+
* URLs. If multiple `RuntimeCaching` routes are defined, then the first one
|
|
306
|
+
* whose `matcher` matches will be the one that responds.
|
|
307
|
+
*
|
|
308
|
+
* This value directly maps to the first parameter passed to
|
|
309
|
+
* `Serwist.registerRoute`. It's recommended to use a
|
|
310
|
+
* `serwist.RouteMatchCallback` function for greatest flexibility.
|
|
311
|
+
*/
|
|
312
|
+
matcher: RegExp | string | RouteMatchCallback;
|
|
313
|
+
/**
|
|
314
|
+
* This determines how the runtime route will generate a response. It
|
|
315
|
+
* can be a `serwist.RouteHandler` callback function with custom
|
|
316
|
+
* response logic.
|
|
317
|
+
*/
|
|
318
|
+
handler: RouteHandler;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export interface InstallResult {
|
|
322
|
+
updatedURLs: string[];
|
|
323
|
+
notUpdatedURLs: string[];
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export interface CleanupResult {
|
|
327
|
+
deletedCacheRequests: string[];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export declare interface PrecacheEntry {
|
|
331
|
+
integrity?: string;
|
|
332
|
+
url: string;
|
|
333
|
+
revision?: string | null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export interface PrecacheRouteOptions {
|
|
337
|
+
/**
|
|
338
|
+
* The `directoryIndex` will check cache entries for a URL ending with '/'
|
|
339
|
+
* to see if there is a hit when appending the `directoryIndex` value.
|
|
340
|
+
*/
|
|
341
|
+
directoryIndex?: string | null;
|
|
342
|
+
/**
|
|
343
|
+
* An array of RegExp's to remove search params when looking for a cache match.
|
|
344
|
+
*/
|
|
345
|
+
ignoreURLParametersMatching?: RegExp[];
|
|
346
|
+
/**
|
|
347
|
+
* The `cleanURLs` option will check the cache for the URL with a `.html` added
|
|
348
|
+
* to the end of the end.
|
|
349
|
+
*/
|
|
350
|
+
cleanURLs?: boolean;
|
|
351
|
+
/**
|
|
352
|
+
* This is a function that should take a URL and return an array of
|
|
353
|
+
* alternative URLs that should be checked for precache matches.
|
|
354
|
+
*/
|
|
355
|
+
urlManipulation?: UrlManipulation;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export type UrlManipulation = ({ url }: { url: URL }) => URL[];
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
/**
|
|
10
|
+
* The Deferred class composes Promises in a way that allows for them to be
|
|
11
|
+
* resolved or rejected from outside the constructor. In most cases promises
|
|
12
|
+
* should be used directly, but Deferreds can be necessary when the logic to
|
|
13
|
+
* resolve a promise must be separate.
|
|
14
|
+
*
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
class Deferred<T> {
|
|
18
|
+
promise: Promise<T>;
|
|
19
|
+
resolve!: (value: T) => void;
|
|
20
|
+
reject!: (reason?: any) => void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates a promise and exposes its resolve and reject functions as methods.
|
|
24
|
+
*/
|
|
25
|
+
constructor() {
|
|
26
|
+
this.promise = new Promise((resolve, reject) => {
|
|
27
|
+
this.resolve = resolve;
|
|
28
|
+
this.reject = reject;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { Deferred };
|
|
@@ -0,0 +1,33 @@
|
|
|
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, SerwistPluginCallbackParam } from "../types.js";
|
|
10
|
+
import type { Serwist } from "../Serwist.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A plugin, designed to be used with PrecacheController, to translate URLs into
|
|
14
|
+
* the corresponding cache key, based on the current revision info.
|
|
15
|
+
*
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
export class PrecacheCacheKeyPlugin implements SerwistPlugin {
|
|
19
|
+
private readonly _precacheController: Serwist;
|
|
20
|
+
|
|
21
|
+
constructor({ precacheController }: { precacheController: Serwist }) {
|
|
22
|
+
this._precacheController = precacheController;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
cacheKeyWillBeUsed: SerwistPlugin["cacheKeyWillBeUsed"] = async ({ request, params }: SerwistPluginCallbackParam["cacheKeyWillBeUsed"]) => {
|
|
26
|
+
// Params is type any, can't change right now.
|
|
27
|
+
/* eslint-disable */
|
|
28
|
+
const cacheKey = params?.cacheKey || this._precacheController.getPrecacheKeyForUrl(request.url);
|
|
29
|
+
/* eslint-enable */
|
|
30
|
+
|
|
31
|
+
return cacheKey ? new Request(cacheKey, { headers: request.headers }) : request;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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, SerwistPluginCallbackParam } from "../types.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A plugin designed to determine the number of assets that were updated (or not updated)
|
|
13
|
+
* during the `install` event.
|
|
14
|
+
*
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
export class PrecacheInstallReportPlugin implements SerwistPlugin {
|
|
18
|
+
updatedURLs: string[] = [];
|
|
19
|
+
notUpdatedURLs: string[] = [];
|
|
20
|
+
|
|
21
|
+
handlerWillStart: SerwistPlugin["handlerWillStart"] = async ({ request, state }: SerwistPluginCallbackParam["handlerWillStart"]) => {
|
|
22
|
+
// TODO: `state` should never be undefined...
|
|
23
|
+
if (state) {
|
|
24
|
+
state.originalRequest = request;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"] = async ({
|
|
29
|
+
event,
|
|
30
|
+
state,
|
|
31
|
+
cachedResponse,
|
|
32
|
+
}: SerwistPluginCallbackParam["cachedResponseWillBeUsed"]) => {
|
|
33
|
+
if (event.type === "install") {
|
|
34
|
+
if (state?.originalRequest && state.originalRequest instanceof Request) {
|
|
35
|
+
// TODO: `state` should never be undefined...
|
|
36
|
+
const url = state.originalRequest.url;
|
|
37
|
+
|
|
38
|
+
if (cachedResponse) {
|
|
39
|
+
this.notUpdatedURLs.push(url);
|
|
40
|
+
} else {
|
|
41
|
+
this.updatedURLs.push(url);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return cachedResponse;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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 { messageGenerator } from "../models/messages/messageGenerator.js";
|
|
10
|
+
import type { MessageKey } from "../models/messages/messages.js";
|
|
11
|
+
import type { MapLikeObject } from "../types.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Serwist errors should be thrown with this class.
|
|
15
|
+
* This allows use to ensure the type easily in tests,
|
|
16
|
+
* helps developers identify errors from Serwist
|
|
17
|
+
* easily and allows use to optimise error
|
|
18
|
+
* messages correctly.
|
|
19
|
+
*
|
|
20
|
+
* @private
|
|
21
|
+
*/
|
|
22
|
+
export class SerwistError extends Error {
|
|
23
|
+
details?: MapLikeObject;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param errorCode The error code that
|
|
28
|
+
* identifies this particular error.
|
|
29
|
+
* @param details Any relevant arguments
|
|
30
|
+
* that will help developers identify issues should
|
|
31
|
+
* be added as a key on the context object.
|
|
32
|
+
*/
|
|
33
|
+
constructor(errorCode: MessageKey, details?: MapLikeObject) {
|
|
34
|
+
const message = messageGenerator(errorCode, details);
|
|
35
|
+
|
|
36
|
+
super(message);
|
|
37
|
+
|
|
38
|
+
this.name = errorCode;
|
|
39
|
+
this.details = details;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
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 { SerwistError } from "./SerwistError.js";
|
|
10
|
+
import type { MapLikeObject } from "../types.js";
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* This method throws if the supplied value is not an array.
|
|
14
|
+
* The destructed values are required to produce a meaningful error for users.
|
|
15
|
+
* The destructed and restructured object is so it's clear what is
|
|
16
|
+
* needed.
|
|
17
|
+
*/
|
|
18
|
+
const isArray = (value: any[], details: MapLikeObject) => {
|
|
19
|
+
if (!Array.isArray(value)) {
|
|
20
|
+
throw new SerwistError("not-an-array", details);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const hasMethod = (object: MapLikeObject, expectedMethod: string, details: MapLikeObject) => {
|
|
25
|
+
const type = typeof object[expectedMethod];
|
|
26
|
+
if (type !== "function") {
|
|
27
|
+
details.expectedMethod = expectedMethod;
|
|
28
|
+
throw new SerwistError("missing-a-method", details);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const isType = (object: unknown, expectedType: string, details: MapLikeObject) => {
|
|
33
|
+
// biome-ignore lint/suspicious/useValidTypeof: Know to not make a mistake...
|
|
34
|
+
if (typeof object !== expectedType) {
|
|
35
|
+
details.expectedType = expectedType;
|
|
36
|
+
throw new SerwistError("incorrect-type", details);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const isInstance = (
|
|
41
|
+
object: unknown,
|
|
42
|
+
// biome-ignore lint/complexity/noBannedTypes: Need the general type to do the check later.
|
|
43
|
+
expectedClass: Function,
|
|
44
|
+
details: MapLikeObject,
|
|
45
|
+
) => {
|
|
46
|
+
if (!(object instanceof expectedClass)) {
|
|
47
|
+
details.expectedClassName = expectedClass.name;
|
|
48
|
+
throw new SerwistError("incorrect-class", details);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const isOneOf = (value: any, validValues: any[], details: MapLikeObject) => {
|
|
53
|
+
if (!validValues.includes(value)) {
|
|
54
|
+
details.validValueDescription = `Valid values are ${JSON.stringify(validValues)}.`;
|
|
55
|
+
throw new SerwistError("invalid-value", details);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const isArrayOfClass = (
|
|
60
|
+
value: any,
|
|
61
|
+
// biome-ignore lint/complexity/noBannedTypes: Need general type to do check later.
|
|
62
|
+
expectedClass: Function,
|
|
63
|
+
details: MapLikeObject,
|
|
64
|
+
) => {
|
|
65
|
+
const error = new SerwistError("not-array-of-class", details);
|
|
66
|
+
if (!Array.isArray(value)) {
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const item of value) {
|
|
71
|
+
if (!(item instanceof expectedClass)) {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const finalAssertExports =
|
|
78
|
+
process.env.NODE_ENV === "production"
|
|
79
|
+
? null
|
|
80
|
+
: {
|
|
81
|
+
hasMethod,
|
|
82
|
+
isArray,
|
|
83
|
+
isInstance,
|
|
84
|
+
isOneOf,
|
|
85
|
+
isType,
|
|
86
|
+
isArrayOfClass,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export { finalAssertExports as assert };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Google LLC
|
|
3
|
+
Use of this source code is governed by an MIT-style
|
|
4
|
+
license that can be found in the LICENSE file or at
|
|
5
|
+
https://opensource.org/licenses/MIT.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
function stripParams(fullURL: string, ignoreParams: string[]) {
|
|
9
|
+
const strippedURL = new URL(fullURL);
|
|
10
|
+
for (const param of ignoreParams) {
|
|
11
|
+
strippedURL.searchParams.delete(param);
|
|
12
|
+
}
|
|
13
|
+
return strippedURL.href;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Matches an item in the cache, ignoring specific URL params. This is similar
|
|
18
|
+
* to the `ignoreSearch` option, but it allows you to ignore just specific
|
|
19
|
+
* params (while continuing to match on the others).
|
|
20
|
+
*
|
|
21
|
+
* @private
|
|
22
|
+
* @param cache
|
|
23
|
+
* @param request
|
|
24
|
+
* @param matchOptions
|
|
25
|
+
* @param ignoreParams
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
async function cacheMatchIgnoreParams(
|
|
29
|
+
cache: Cache,
|
|
30
|
+
request: Request,
|
|
31
|
+
ignoreParams: string[],
|
|
32
|
+
matchOptions?: CacheQueryOptions,
|
|
33
|
+
): Promise<Response | undefined> {
|
|
34
|
+
const strippedRequestURL = stripParams(request.url, ignoreParams);
|
|
35
|
+
|
|
36
|
+
// If the request doesn't include any ignored params, match as normal.
|
|
37
|
+
if (request.url === strippedRequestURL) {
|
|
38
|
+
return cache.match(request, matchOptions);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Otherwise, match by comparing keys
|
|
42
|
+
const keysOptions = { ...matchOptions, ignoreSearch: true };
|
|
43
|
+
const cacheKeys = await cache.keys(request, keysOptions);
|
|
44
|
+
|
|
45
|
+
for (const cacheKey of cacheKeys) {
|
|
46
|
+
const strippedCacheKeyURL = stripParams(cacheKey.url, ignoreParams);
|
|
47
|
+
if (strippedRequestURL === strippedCacheKeyURL) {
|
|
48
|
+
return cache.match(cacheKey, matchOptions);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { cacheMatchIgnoreParams };
|