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,184 @@
|
|
|
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 { DBSchema, IDBPDatabase } from "idb";
|
|
10
|
+
import { deleteDB, openDB } from "idb";
|
|
11
|
+
|
|
12
|
+
const DB_NAME = "serwist-expiration";
|
|
13
|
+
const CACHE_OBJECT_STORE = "cache-entries";
|
|
14
|
+
|
|
15
|
+
const normalizeURL = (unNormalizedUrl: string) => {
|
|
16
|
+
const url = new URL(unNormalizedUrl, location.href);
|
|
17
|
+
url.hash = "";
|
|
18
|
+
|
|
19
|
+
return url.href;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
interface CacheTimestampsModelEntry {
|
|
23
|
+
id: string;
|
|
24
|
+
cacheName: string;
|
|
25
|
+
url: string;
|
|
26
|
+
timestamp: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface CacheDbSchema extends DBSchema {
|
|
30
|
+
"cache-entries": {
|
|
31
|
+
key: string;
|
|
32
|
+
value: CacheTimestampsModelEntry;
|
|
33
|
+
indexes: { cacheName: string; timestamp: number };
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns the timestamp model.
|
|
39
|
+
*
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
export class CacheTimestampsModel {
|
|
43
|
+
private readonly _cacheName: string;
|
|
44
|
+
private _db: IDBPDatabase<CacheDbSchema> | null = null;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* @param cacheName
|
|
49
|
+
*
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
constructor(cacheName: string) {
|
|
53
|
+
this._cacheName = cacheName;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Takes a URL and returns an ID that will be unique in the object store.
|
|
58
|
+
*
|
|
59
|
+
* @param url
|
|
60
|
+
* @returns
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
private _getId(url: string): string {
|
|
64
|
+
return `${this._cacheName}|${normalizeURL(url)}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Performs an upgrade of indexedDB.
|
|
69
|
+
*
|
|
70
|
+
* @param db
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
private _upgradeDb(db: IDBPDatabase<CacheDbSchema>) {
|
|
75
|
+
const objStore = db.createObjectStore(CACHE_OBJECT_STORE, {
|
|
76
|
+
keyPath: "id",
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// TODO(philipwalton): once we don't have to support EdgeHTML, we can
|
|
80
|
+
// create a single index with the keyPath `['cacheName', 'timestamp']`
|
|
81
|
+
// instead of doing both these indexes.
|
|
82
|
+
objStore.createIndex("cacheName", "cacheName", { unique: false });
|
|
83
|
+
objStore.createIndex("timestamp", "timestamp", { unique: false });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Performs an upgrade of indexedDB and deletes deprecated DBs.
|
|
88
|
+
*
|
|
89
|
+
* @param db
|
|
90
|
+
*
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
private _upgradeDbAndDeleteOldDbs(db: IDBPDatabase<CacheDbSchema>) {
|
|
94
|
+
this._upgradeDb(db);
|
|
95
|
+
if (this._cacheName) {
|
|
96
|
+
void deleteDB(this._cacheName);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @param url
|
|
102
|
+
* @param timestamp
|
|
103
|
+
*
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
async setTimestamp(url: string, timestamp: number): Promise<void> {
|
|
107
|
+
url = normalizeURL(url);
|
|
108
|
+
|
|
109
|
+
const entry = {
|
|
110
|
+
id: this._getId(url),
|
|
111
|
+
cacheName: this._cacheName,
|
|
112
|
+
url,
|
|
113
|
+
timestamp,
|
|
114
|
+
} satisfies CacheTimestampsModelEntry;
|
|
115
|
+
const db = await this.getDb();
|
|
116
|
+
const tx = db.transaction(CACHE_OBJECT_STORE, "readwrite", {
|
|
117
|
+
durability: "relaxed",
|
|
118
|
+
});
|
|
119
|
+
await tx.store.put(entry);
|
|
120
|
+
await tx.done;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Returns the timestamp stored for a given URL.
|
|
125
|
+
*
|
|
126
|
+
* @param url
|
|
127
|
+
* @returns
|
|
128
|
+
* @private
|
|
129
|
+
*/
|
|
130
|
+
async getTimestamp(url: string): Promise<number | undefined> {
|
|
131
|
+
const db = await this.getDb();
|
|
132
|
+
const entry = await db.get(CACHE_OBJECT_STORE, this._getId(url));
|
|
133
|
+
return entry?.timestamp;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Iterates through all the entries in the object store (from newest to
|
|
138
|
+
* oldest) and removes entries once either `maxCount` is reached or the
|
|
139
|
+
* entry's timestamp is less than `minTimestamp`.
|
|
140
|
+
*
|
|
141
|
+
* @param minTimestamp
|
|
142
|
+
* @param maxCount
|
|
143
|
+
* @returns
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
async expireEntries(minTimestamp: number, maxCount?: number): Promise<string[]> {
|
|
147
|
+
const db = await this.getDb();
|
|
148
|
+
let cursor = await db.transaction(CACHE_OBJECT_STORE, "readwrite").store.index("timestamp").openCursor(null, "prev");
|
|
149
|
+
const urlsDeleted: string[] = [];
|
|
150
|
+
let entriesNotDeletedCount = 0;
|
|
151
|
+
while (cursor) {
|
|
152
|
+
const result = cursor.value;
|
|
153
|
+
// TODO(philipwalton): once we can use a multi-key index, we
|
|
154
|
+
// won't have to check `cacheName` here.
|
|
155
|
+
if (result.cacheName === this._cacheName) {
|
|
156
|
+
// Delete an entry if it's older than the max age or
|
|
157
|
+
// if we already have the max number allowed.
|
|
158
|
+
if ((minTimestamp && result.timestamp < minTimestamp) || (maxCount && entriesNotDeletedCount >= maxCount)) {
|
|
159
|
+
cursor.delete();
|
|
160
|
+
urlsDeleted.push(result.url);
|
|
161
|
+
} else {
|
|
162
|
+
entriesNotDeletedCount++;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
cursor = await cursor.continue();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return urlsDeleted;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Returns an open connection to the database.
|
|
173
|
+
*
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
private async getDb() {
|
|
177
|
+
if (!this._db) {
|
|
178
|
+
this._db = await openDB(DB_NAME, 1, {
|
|
179
|
+
upgrade: this._upgradeDbAndDeleteOldDbs.bind(this),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return this._db;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
export const QUEUE_NAME = "serwist-google-analytics";
|
|
10
|
+
export const MAX_RETENTION_TIME = 60 * 48; // Two days in minutes
|
|
11
|
+
export const GOOGLE_ANALYTICS_HOST = "www.google-analytics.com";
|
|
12
|
+
export const GTM_HOST = "www.googletagmanager.com";
|
|
13
|
+
export const ANALYTICS_JS_PATH = "/analytics.js";
|
|
14
|
+
export const GTAG_JS_PATH = "/gtag/js";
|
|
15
|
+
export const GTM_JS_PATH = "/gtm.js";
|
|
16
|
+
export const COLLECT_DEFAULT_PATH = "/collect";
|
|
17
|
+
|
|
18
|
+
// This RegExp matches all known Measurement Protocol single-hit collect
|
|
19
|
+
// endpoints. Most of the time the default path (/collect) is used, but
|
|
20
|
+
// occasionally an experimental endpoint is used when testing new features,
|
|
21
|
+
// (e.g. /r/collect or /j/collect)
|
|
22
|
+
export const COLLECT_PATHS_REGEX = /^\/(\w+\/)?collect/;
|
|
@@ -0,0 +1,209 @@
|
|
|
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 { RouteMatchCallbackOptions } from "../../types.js";
|
|
10
|
+
import { getFriendlyURL } from "../../utils/getFriendlyURL.js";
|
|
11
|
+
import { logger } from "../../utils/logger.js";
|
|
12
|
+
import { cacheNames as privateCacheNames } from "../../utils/cacheNames.js";
|
|
13
|
+
import { Route } from "../../Route.js";
|
|
14
|
+
import { NetworkFirst } from "../../strategies/NetworkFirst.js";
|
|
15
|
+
import { NetworkOnly } from "../../strategies/NetworkOnly.js";
|
|
16
|
+
import { BackgroundSyncPlugin } from "../backgroundSync/BackgroundSyncPlugin.js";
|
|
17
|
+
import type { Queue, QueueEntry } from "../backgroundSync/Queue.js";
|
|
18
|
+
import {
|
|
19
|
+
ANALYTICS_JS_PATH,
|
|
20
|
+
COLLECT_PATHS_REGEX,
|
|
21
|
+
GOOGLE_ANALYTICS_HOST,
|
|
22
|
+
GTAG_JS_PATH,
|
|
23
|
+
GTM_HOST,
|
|
24
|
+
GTM_JS_PATH,
|
|
25
|
+
MAX_RETENTION_TIME,
|
|
26
|
+
QUEUE_NAME,
|
|
27
|
+
} from "./constants.js";
|
|
28
|
+
import type { Serwist } from "../../Serwist.js";
|
|
29
|
+
|
|
30
|
+
export interface GoogleAnalyticsInitializeOptions {
|
|
31
|
+
serwist: Serwist;
|
|
32
|
+
/**
|
|
33
|
+
* The cache name to store and retrieve analytics.js. Defaults to Serwist's default cache names.
|
|
34
|
+
*/
|
|
35
|
+
cacheName?: string;
|
|
36
|
+
/**
|
|
37
|
+
* [Measurement Protocol parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters),
|
|
38
|
+
* expressed as key/value pairs, to be added to replayed Google Analytics
|
|
39
|
+
* requests. This can be used to, e.g., set a custom dimension indicating
|
|
40
|
+
* that the request was replayed.
|
|
41
|
+
*/
|
|
42
|
+
parameterOverrides?: { [paramName: string]: string };
|
|
43
|
+
/**
|
|
44
|
+
* A function that allows you to modify the hit parameters prior to replaying
|
|
45
|
+
* the hit. The function is invoked with the original hit's URLSearchParams
|
|
46
|
+
* object as its only argument.
|
|
47
|
+
*/
|
|
48
|
+
hitFilter?: (params: URLSearchParams) => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Creates the requestWillDequeue callback to be used with the background
|
|
53
|
+
* sync plugin. The callback takes the failed request and adds the
|
|
54
|
+
* `qt` param based on the current time, as well as applies any other
|
|
55
|
+
* user-defined hit modifications.
|
|
56
|
+
*
|
|
57
|
+
* @param config
|
|
58
|
+
* @returns The requestWillDequeue callback function.
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
const createOnSyncCallback = (config: Pick<GoogleAnalyticsInitializeOptions, "parameterOverrides" | "hitFilter">) => {
|
|
62
|
+
return async ({ queue }: { queue: Queue }) => {
|
|
63
|
+
let entry: QueueEntry | undefined = undefined;
|
|
64
|
+
while ((entry = await queue.shiftRequest())) {
|
|
65
|
+
const { request, timestamp } = entry;
|
|
66
|
+
const url = new URL(request.url);
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
// Measurement protocol requests can set their payload parameters in
|
|
70
|
+
// either the URL query string (for GET requests) or the POST body.
|
|
71
|
+
const params = request.method === "POST" ? new URLSearchParams(await request.clone().text()) : url.searchParams;
|
|
72
|
+
|
|
73
|
+
// Calculate the qt param, accounting for the fact that an existing
|
|
74
|
+
// qt param may be present and should be updated rather than replaced.
|
|
75
|
+
const originalHitTime = timestamp! - (Number(params.get("qt")) || 0);
|
|
76
|
+
const queueTime = Date.now() - originalHitTime;
|
|
77
|
+
|
|
78
|
+
// Set the qt param prior to applying hitFilter or parameterOverrides.
|
|
79
|
+
params.set("qt", String(queueTime));
|
|
80
|
+
|
|
81
|
+
// Apply `parameterOverrides`, if set.
|
|
82
|
+
if (config.parameterOverrides) {
|
|
83
|
+
for (const param of Object.keys(config.parameterOverrides)) {
|
|
84
|
+
const value = config.parameterOverrides[param];
|
|
85
|
+
params.set(param, value);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Apply `hitFilter`, if set.
|
|
90
|
+
if (typeof config.hitFilter === "function") {
|
|
91
|
+
config.hitFilter.call(null, params);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Retry the fetch. Ignore URL search params from the URL as they're
|
|
95
|
+
// now in the post body.
|
|
96
|
+
await fetch(
|
|
97
|
+
new Request(url.origin + url.pathname, {
|
|
98
|
+
body: params.toString(),
|
|
99
|
+
method: "POST",
|
|
100
|
+
mode: "cors",
|
|
101
|
+
credentials: "omit",
|
|
102
|
+
headers: { "Content-Type": "text/plain" },
|
|
103
|
+
}),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
if (process.env.NODE_ENV !== "production") {
|
|
107
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' has been replayed`);
|
|
108
|
+
}
|
|
109
|
+
} catch (err) {
|
|
110
|
+
await queue.unshiftRequest(entry);
|
|
111
|
+
|
|
112
|
+
if (process.env.NODE_ENV !== "production") {
|
|
113
|
+
logger.log(`Request for '${getFriendlyURL(url.href)}' failed to replay, putting it back in the queue.`);
|
|
114
|
+
}
|
|
115
|
+
throw err;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (process.env.NODE_ENV !== "production") {
|
|
119
|
+
logger.log("All Google Analytics request successfully replayed; " + "the queue is now empty!");
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Creates GET and POST routes to catch failed Measurement Protocol hits.
|
|
126
|
+
*
|
|
127
|
+
* @param bgSyncPlugin
|
|
128
|
+
* @returns The created routes.
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
const createCollectRoutes = (bgSyncPlugin: BackgroundSyncPlugin) => {
|
|
132
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && COLLECT_PATHS_REGEX.test(url.pathname);
|
|
133
|
+
|
|
134
|
+
const handler = new NetworkOnly({
|
|
135
|
+
plugins: [bgSyncPlugin],
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return [new Route(match, handler, "GET"), new Route(match, handler, "POST")];
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Creates a route with a network first strategy for the analytics.js script.
|
|
143
|
+
*
|
|
144
|
+
* @param cacheName
|
|
145
|
+
* @returns The created route.
|
|
146
|
+
* @private
|
|
147
|
+
*/
|
|
148
|
+
const createAnalyticsJsRoute = (cacheName: string) => {
|
|
149
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GOOGLE_ANALYTICS_HOST && url.pathname === ANALYTICS_JS_PATH;
|
|
150
|
+
|
|
151
|
+
const handler = new NetworkFirst({ cacheName });
|
|
152
|
+
|
|
153
|
+
return new Route(match, handler, "GET");
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Creates a route with a network first strategy for the gtag.js script.
|
|
158
|
+
*
|
|
159
|
+
* @param cacheName
|
|
160
|
+
* @returns The created route.
|
|
161
|
+
* @private
|
|
162
|
+
*/
|
|
163
|
+
const createGtagJsRoute = (cacheName: string) => {
|
|
164
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTAG_JS_PATH;
|
|
165
|
+
|
|
166
|
+
const handler = new NetworkFirst({ cacheName });
|
|
167
|
+
|
|
168
|
+
return new Route(match, handler, "GET");
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Creates a route with a network first strategy for the gtm.js script.
|
|
173
|
+
*
|
|
174
|
+
* @param cacheName
|
|
175
|
+
* @returns The created route.
|
|
176
|
+
* @private
|
|
177
|
+
*/
|
|
178
|
+
const createGtmJsRoute = (cacheName: string) => {
|
|
179
|
+
const match = ({ url }: RouteMatchCallbackOptions) => url.hostname === GTM_HOST && url.pathname === GTM_JS_PATH;
|
|
180
|
+
|
|
181
|
+
const handler = new NetworkFirst({ cacheName });
|
|
182
|
+
|
|
183
|
+
return new Route(match, handler, "GET");
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Initialize Serwist's offline Google Analytics v3 support.
|
|
188
|
+
*
|
|
189
|
+
* @param options
|
|
190
|
+
*/
|
|
191
|
+
export const initialize = ({ serwist, cacheName, ...options }: GoogleAnalyticsInitializeOptions): void => {
|
|
192
|
+
const resolvedCacheName = privateCacheNames.getGoogleAnalyticsName(cacheName);
|
|
193
|
+
|
|
194
|
+
const bgSyncPlugin = new BackgroundSyncPlugin(QUEUE_NAME, {
|
|
195
|
+
maxRetentionTime: MAX_RETENTION_TIME,
|
|
196
|
+
onSync: createOnSyncCallback(options),
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const routes = [
|
|
200
|
+
createGtmJsRoute(resolvedCacheName),
|
|
201
|
+
createAnalyticsJsRoute(resolvedCacheName),
|
|
202
|
+
createGtagJsRoute(resolvedCacheName),
|
|
203
|
+
...createCollectRoutes(bgSyncPlugin),
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
for (const route of routes) {
|
|
207
|
+
serwist.registerRoute(route);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
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 { HandlerDidErrorCallbackParam, SerwistPlugin } from "../../types.js";
|
|
10
|
+
import type { Serwist } from "../../Serwist.js";
|
|
11
|
+
|
|
12
|
+
export interface PrecacheFallbackEntry {
|
|
13
|
+
/**
|
|
14
|
+
* A function that checks whether the fallback entry can be used
|
|
15
|
+
* for a request.
|
|
16
|
+
*/
|
|
17
|
+
matcher: (param: HandlerDidErrorCallbackParam) => boolean;
|
|
18
|
+
/**
|
|
19
|
+
* A precached URL to be used as a fallback.
|
|
20
|
+
*/
|
|
21
|
+
url: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PrecacheFallbackPluginOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Precached URLs to be used as the fallback
|
|
27
|
+
* if the associated strategy can't generate a response.
|
|
28
|
+
*/
|
|
29
|
+
fallbackUrls: (string | PrecacheFallbackEntry)[];
|
|
30
|
+
/**
|
|
31
|
+
* Your `Serwist` instance.
|
|
32
|
+
*/
|
|
33
|
+
serwist: Serwist;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `PrecacheFallbackPlugin` allows you to specify offline fallbacks
|
|
38
|
+
* to be used when a given strategy is unable to generate a response.
|
|
39
|
+
*
|
|
40
|
+
* It does this by intercepting the `handlerDidError` plugin callback
|
|
41
|
+
* and returning a precached response, taking the expected revision parameter
|
|
42
|
+
* into account automatically.
|
|
43
|
+
*
|
|
44
|
+
* Unless you explicitly pass in a `PrecacheController` instance to the
|
|
45
|
+
* constructor, the default instance will be used. Generally speaking, most
|
|
46
|
+
* developers will end up using the default.
|
|
47
|
+
*/
|
|
48
|
+
export class PrecacheFallbackPlugin implements SerwistPlugin {
|
|
49
|
+
private readonly _fallbackUrls: (string | PrecacheFallbackEntry)[];
|
|
50
|
+
private readonly _serwist: Serwist;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Constructs a new `PrecacheFallbackPlugin` with the associated `fallbackUrls`.
|
|
54
|
+
*
|
|
55
|
+
* @param config
|
|
56
|
+
*/
|
|
57
|
+
constructor({ fallbackUrls, serwist }: PrecacheFallbackPluginOptions) {
|
|
58
|
+
this._fallbackUrls = fallbackUrls;
|
|
59
|
+
this._serwist = serwist;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @returns The precache response for one of the fallback URLs, or `undefined` if
|
|
64
|
+
* nothing satisfies the conditions.
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
async handlerDidError(param: HandlerDidErrorCallbackParam) {
|
|
68
|
+
for (const fallback of this._fallbackUrls) {
|
|
69
|
+
if (typeof fallback === "string") {
|
|
70
|
+
const fallbackResponse = await this._serwist.matchPrecache(fallback);
|
|
71
|
+
if (fallbackResponse !== undefined) {
|
|
72
|
+
return fallbackResponse;
|
|
73
|
+
}
|
|
74
|
+
} else if (fallback.matcher(param)) {
|
|
75
|
+
const fallbackResponse = await this._serwist.matchPrecache(fallback.url);
|
|
76
|
+
if (fallbackResponse !== undefined) {
|
|
77
|
+
return fallbackResponse;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { SerwistPlugin } from "../../types.js";
|
|
10
|
+
import { createPartialResponse } from "./createPartialResponse.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The range request plugin makes it easy for a request with a 'Range' header to
|
|
14
|
+
* be fulfilled by a cached response.
|
|
15
|
+
*
|
|
16
|
+
* It does this by intercepting the `cachedResponseWillBeUsed` plugin callback
|
|
17
|
+
* and returning the appropriate subset of the cached response body.
|
|
18
|
+
*/
|
|
19
|
+
export class RangeRequestsPlugin implements SerwistPlugin {
|
|
20
|
+
/**
|
|
21
|
+
* @param options
|
|
22
|
+
* @returns If request contains a 'Range' header, then a
|
|
23
|
+
* new response with status 206 whose body is a subset of `cachedResponse` is
|
|
24
|
+
* returned. Otherwise, `cachedResponse` is returned as-is.
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"] = async ({ request, cachedResponse }) => {
|
|
28
|
+
// Only return a sliced response if there's something valid in the cache,
|
|
29
|
+
// and there's a Range: header in the request.
|
|
30
|
+
if (cachedResponse && request.headers.has("range")) {
|
|
31
|
+
return await createPartialResponse(request, cachedResponse);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// If there was no Range: header, or if cachedResponse wasn't valid, just
|
|
35
|
+
// pass it through as-is.
|
|
36
|
+
return cachedResponse;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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 { assert } from "../../utils/assert.js";
|
|
10
|
+
import { SerwistError } from "../../utils/SerwistError.js";
|
|
11
|
+
import { logger } from "../../utils/logger.js";
|
|
12
|
+
import { calculateEffectiveBoundaries } from "./utils/calculateEffectiveBoundaries.js";
|
|
13
|
+
import { parseRangeHeader } from "./utils/parseRangeHeader.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Given a `Request` and `Response` objects as input, this will return a
|
|
17
|
+
* promise for a new `Response`.
|
|
18
|
+
*
|
|
19
|
+
* If the original `Response` already contains partial content (i.e. it has
|
|
20
|
+
* a status of 206), then this assumes it already fulfills the `Range:`
|
|
21
|
+
* requirements, and will return it as-is.
|
|
22
|
+
*
|
|
23
|
+
* @param request A request, which should contain a Range:
|
|
24
|
+
* header.
|
|
25
|
+
* @param originalResponse A response.
|
|
26
|
+
* @returns Either a `206 Partial Content` response, with
|
|
27
|
+
* the response body set to the slice of content specified by the request's
|
|
28
|
+
* `Range:` header, or a `416 Range Not Satisfiable` response if the
|
|
29
|
+
* conditions of the `Range:` header can't be met.
|
|
30
|
+
*/
|
|
31
|
+
export const createPartialResponse = async (request: Request, originalResponse: Response): Promise<Response> => {
|
|
32
|
+
try {
|
|
33
|
+
if (process.env.NODE_ENV !== "production") {
|
|
34
|
+
assert!.isInstance(request, Request, {
|
|
35
|
+
moduleName: "@serwist/range-requests",
|
|
36
|
+
funcName: "createPartialResponse",
|
|
37
|
+
paramName: "request",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
assert!.isInstance(originalResponse, Response, {
|
|
41
|
+
moduleName: "@serwist/range-requests",
|
|
42
|
+
funcName: "createPartialResponse",
|
|
43
|
+
paramName: "originalResponse",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (originalResponse.status === 206) {
|
|
48
|
+
// If we already have a 206, then just pass it through as-is;
|
|
49
|
+
// see https://github.com/GoogleChrome/workbox/issues/1720
|
|
50
|
+
return originalResponse;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const rangeHeader = request.headers.get("range");
|
|
54
|
+
if (!rangeHeader) {
|
|
55
|
+
throw new SerwistError("no-range-header");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const boundaries = parseRangeHeader(rangeHeader);
|
|
59
|
+
const originalBlob = await originalResponse.blob();
|
|
60
|
+
|
|
61
|
+
const effectiveBoundaries = calculateEffectiveBoundaries(originalBlob, boundaries.start, boundaries.end);
|
|
62
|
+
|
|
63
|
+
const slicedBlob = originalBlob.slice(effectiveBoundaries.start, effectiveBoundaries.end);
|
|
64
|
+
const slicedBlobSize = slicedBlob.size;
|
|
65
|
+
|
|
66
|
+
const slicedResponse = new Response(slicedBlob, {
|
|
67
|
+
// Status code 206 is for a Partial Content response.
|
|
68
|
+
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206
|
|
69
|
+
status: 206,
|
|
70
|
+
statusText: "Partial Content",
|
|
71
|
+
headers: originalResponse.headers,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
slicedResponse.headers.set("Content-Length", String(slicedBlobSize));
|
|
75
|
+
slicedResponse.headers.set("Content-Range", `bytes ${effectiveBoundaries.start}-${effectiveBoundaries.end - 1}/` + `${originalBlob.size}`);
|
|
76
|
+
|
|
77
|
+
return slicedResponse;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (process.env.NODE_ENV !== "production") {
|
|
80
|
+
logger.warn("Unable to construct a partial response; returning a " + "416 Range Not Satisfiable response instead.");
|
|
81
|
+
logger.groupCollapsed("View details here.");
|
|
82
|
+
logger.log(error);
|
|
83
|
+
logger.log(request);
|
|
84
|
+
logger.log(originalResponse);
|
|
85
|
+
logger.groupEnd();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return new Response("", {
|
|
89
|
+
status: 416,
|
|
90
|
+
statusText: "Range Not Satisfiable",
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
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 { assert } from "../../../utils/assert.js";
|
|
10
|
+
import { SerwistError } from "../../../utils/SerwistError.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param blob A source blob.
|
|
14
|
+
* @param start The offset to use as the start of the
|
|
15
|
+
* slice.
|
|
16
|
+
* @param end The offset to use as the end of the slice.
|
|
17
|
+
* @returns An object with `start` and `end` properties, reflecting
|
|
18
|
+
* the effective boundaries to use given the size of the blob.
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
export const calculateEffectiveBoundaries = (blob: Blob, start?: number, end?: number): { start: number; end: number } => {
|
|
22
|
+
if (process.env.NODE_ENV !== "production") {
|
|
23
|
+
assert!.isInstance(blob, Blob, {
|
|
24
|
+
moduleName: "@serwist/range-requests",
|
|
25
|
+
funcName: "calculateEffectiveBoundaries",
|
|
26
|
+
paramName: "blob",
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const blobSize = blob.size;
|
|
31
|
+
|
|
32
|
+
if ((end && end > blobSize) || (start && start < 0)) {
|
|
33
|
+
throw new SerwistError("range-not-satisfiable", {
|
|
34
|
+
size: blobSize,
|
|
35
|
+
end,
|
|
36
|
+
start,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let effectiveStart: number;
|
|
41
|
+
let effectiveEnd: number;
|
|
42
|
+
|
|
43
|
+
if (start !== undefined && end !== undefined) {
|
|
44
|
+
effectiveStart = start;
|
|
45
|
+
// Range values are inclusive, so add 1 to the value.
|
|
46
|
+
effectiveEnd = end + 1;
|
|
47
|
+
} else if (start !== undefined && end === undefined) {
|
|
48
|
+
effectiveStart = start;
|
|
49
|
+
effectiveEnd = blobSize;
|
|
50
|
+
} else if (end !== undefined && start === undefined) {
|
|
51
|
+
effectiveStart = blobSize - end;
|
|
52
|
+
effectiveEnd = blobSize;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
start: effectiveStart!,
|
|
57
|
+
end: effectiveEnd!,
|
|
58
|
+
};
|
|
59
|
+
};
|