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.
Files changed (330) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/dist/NavigationRoute.d.ts +57 -0
  4. package/dist/NavigationRoute.d.ts.map +1 -0
  5. package/dist/PrecacheRoute.d.ts +17 -0
  6. package/dist/PrecacheRoute.d.ts.map +1 -0
  7. package/dist/PrecacheStrategy.d.ts +66 -0
  8. package/dist/PrecacheStrategy.d.ts.map +1 -0
  9. package/dist/RegExpRoute.d.ts +24 -0
  10. package/dist/RegExpRoute.d.ts.map +1 -0
  11. package/dist/Route.d.ts +33 -0
  12. package/dist/Route.d.ts.map +1 -0
  13. package/dist/Serwist.d.ts +331 -0
  14. package/dist/Serwist.d.ts.map +1 -0
  15. package/dist/cacheNames.d.ts +20 -0
  16. package/dist/cacheNames.d.ts.map +1 -0
  17. package/dist/chunks/NetworkOnly.js +599 -0
  18. package/dist/chunks/PrecacheFallbackPlugin.js +634 -0
  19. package/dist/chunks/Serwist.js +1034 -0
  20. package/dist/chunks/registerQuotaErrorCallback.js +17 -0
  21. package/dist/chunks/resultingClientExists.js +32 -0
  22. package/dist/chunks/timeout.js +400 -0
  23. package/dist/chunks/waitUntil.js +24 -0
  24. package/dist/cleanupOutdatedCaches.d.ts +6 -0
  25. package/dist/cleanupOutdatedCaches.d.ts.map +1 -0
  26. package/dist/clientsClaim.d.ts +6 -0
  27. package/dist/clientsClaim.d.ts.map +1 -0
  28. package/dist/constants.d.ts +15 -0
  29. package/dist/constants.d.ts.map +1 -0
  30. package/dist/copyResponse.d.ts +20 -0
  31. package/dist/copyResponse.d.ts.map +1 -0
  32. package/dist/disableDevLogs.d.ts +7 -0
  33. package/dist/disableDevLogs.d.ts.map +1 -0
  34. package/dist/index.d.ts +21 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.internal.d.ts +16 -0
  37. package/dist/index.internal.d.ts.map +1 -0
  38. package/dist/index.internal.js +24 -0
  39. package/dist/index.js +27 -0
  40. package/dist/index.legacy.d.ts +32 -0
  41. package/dist/index.legacy.d.ts.map +1 -0
  42. package/dist/index.legacy.js +640 -0
  43. package/dist/index.plugins.d.ts +41 -0
  44. package/dist/index.plugins.d.ts.map +1 -0
  45. package/dist/index.plugins.js +669 -0
  46. package/dist/index.strategies.d.ts +22 -0
  47. package/dist/index.strategies.d.ts.map +1 -0
  48. package/dist/index.strategies.js +144 -0
  49. package/dist/legacy/PrecacheController.d.ts +146 -0
  50. package/dist/legacy/PrecacheController.d.ts.map +1 -0
  51. package/dist/legacy/PrecacheFallbackPlugin.d.ts +62 -0
  52. package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +1 -0
  53. package/dist/legacy/PrecacheRoute.d.ts +19 -0
  54. package/dist/legacy/PrecacheRoute.d.ts.map +1 -0
  55. package/dist/legacy/Router.d.ts +151 -0
  56. package/dist/legacy/Router.d.ts.map +1 -0
  57. package/dist/legacy/addPlugins.d.ts +9 -0
  58. package/dist/legacy/addPlugins.d.ts.map +1 -0
  59. package/dist/legacy/addRoute.d.ts +16 -0
  60. package/dist/legacy/addRoute.d.ts.map +1 -0
  61. package/dist/legacy/createHandlerBoundToURL.d.ts +18 -0
  62. package/dist/legacy/createHandlerBoundToURL.d.ts.map +1 -0
  63. package/dist/legacy/fallbacks.d.ts +59 -0
  64. package/dist/legacy/fallbacks.d.ts.map +1 -0
  65. package/dist/legacy/getCacheKeyForURL.d.ts +20 -0
  66. package/dist/legacy/getCacheKeyForURL.d.ts.map +1 -0
  67. package/dist/legacy/handlePrecaching.d.ts +54 -0
  68. package/dist/legacy/handlePrecaching.d.ts.map +1 -0
  69. package/dist/legacy/installSerwist.d.ts +15 -0
  70. package/dist/legacy/installSerwist.d.ts.map +1 -0
  71. package/dist/legacy/matchPrecache.d.ts +15 -0
  72. package/dist/legacy/matchPrecache.d.ts.map +1 -0
  73. package/dist/legacy/precache.d.ts +20 -0
  74. package/dist/legacy/precache.d.ts.map +1 -0
  75. package/dist/legacy/precacheAndRoute.d.ts +15 -0
  76. package/dist/legacy/precacheAndRoute.d.ts.map +1 -0
  77. package/dist/legacy/registerRoute.d.ts +16 -0
  78. package/dist/legacy/registerRoute.d.ts.map +1 -0
  79. package/dist/legacy/registerRuntimeCaching.d.ts +11 -0
  80. package/dist/legacy/registerRuntimeCaching.d.ts.map +1 -0
  81. package/dist/legacy/setCatchHandler.d.ts +10 -0
  82. package/dist/legacy/setCatchHandler.d.ts.map +1 -0
  83. package/dist/legacy/setDefaultHandler.d.ts +13 -0
  84. package/dist/legacy/setDefaultHandler.d.ts.map +1 -0
  85. package/dist/legacy/singletonPrecacheController.d.ts +34 -0
  86. package/dist/legacy/singletonPrecacheController.d.ts.map +1 -0
  87. package/dist/legacy/singletonRouter.d.ts +41 -0
  88. package/dist/legacy/singletonRouter.d.ts.map +1 -0
  89. package/dist/legacy/unregisterRoute.d.ts +9 -0
  90. package/dist/legacy/unregisterRoute.d.ts.map +1 -0
  91. package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
  92. package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
  93. package/dist/legacy/utils/getCacheKeyForURL.d.ts +14 -0
  94. package/dist/legacy/utils/getCacheKeyForURL.d.ts.map +1 -0
  95. package/dist/models/messages/messageGenerator.d.ts +4 -0
  96. package/dist/models/messages/messageGenerator.d.ts.map +1 -0
  97. package/dist/models/messages/messages.d.ts +44 -0
  98. package/dist/models/messages/messages.d.ts.map +1 -0
  99. package/dist/models/pluginEvents.d.ts +10 -0
  100. package/dist/models/pluginEvents.d.ts.map +1 -0
  101. package/dist/models/quotaErrorCallbacks.d.ts +3 -0
  102. package/dist/models/quotaErrorCallbacks.d.ts.map +1 -0
  103. package/dist/navigationPreload.d.ts +24 -0
  104. package/dist/navigationPreload.d.ts.map +1 -0
  105. package/dist/parseRoute.d.ts +16 -0
  106. package/dist/parseRoute.d.ts.map +1 -0
  107. package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
  108. package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
  109. package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
  110. package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
  111. package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
  112. package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
  113. package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
  114. package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
  115. package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
  116. package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
  117. package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
  118. package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
  119. package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
  120. package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
  121. package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
  122. package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
  123. package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
  124. package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
  125. package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
  126. package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
  127. package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
  128. package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
  129. package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
  130. package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
  131. package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
  132. package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
  133. package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
  134. package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
  135. package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
  136. package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
  137. package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
  138. package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
  139. package/dist/plugins/googleAnalytics/initialize.d.ts +30 -0
  140. package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
  141. package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +53 -0
  142. package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
  143. package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
  144. package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
  145. package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
  146. package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
  147. package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
  148. package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
  149. package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
  150. package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
  151. package/dist/registerQuotaErrorCallback.d.ts +8 -0
  152. package/dist/registerQuotaErrorCallback.d.ts.map +1 -0
  153. package/dist/setCacheNameDetails.d.ts +9 -0
  154. package/dist/setCacheNameDetails.d.ts.map +1 -0
  155. package/dist/strategies/CacheFirst.d.ts +23 -0
  156. package/dist/strategies/CacheFirst.d.ts.map +1 -0
  157. package/dist/strategies/CacheOnly.d.ts +20 -0
  158. package/dist/strategies/CacheOnly.d.ts.map +1 -0
  159. package/dist/strategies/NetworkFirst.d.ts +61 -0
  160. package/dist/strategies/NetworkFirst.d.ts.map +1 -0
  161. package/dist/strategies/NetworkOnly.d.ts +32 -0
  162. package/dist/strategies/NetworkOnly.d.ts.map +1 -0
  163. package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
  164. package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
  165. package/dist/strategies/Strategy.d.ts +83 -0
  166. package/dist/strategies/Strategy.d.ts.map +1 -0
  167. package/dist/strategies/StrategyHandler.d.ts +189 -0
  168. package/dist/strategies/StrategyHandler.d.ts.map +1 -0
  169. package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
  170. package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
  171. package/dist/strategies/utils/messages.d.ts +5 -0
  172. package/dist/strategies/utils/messages.d.ts.map +1 -0
  173. package/dist/types.d.ts +317 -0
  174. package/dist/types.d.ts.map +1 -0
  175. package/dist/utils/Deferred.d.ts +19 -0
  176. package/dist/utils/Deferred.d.ts.map +1 -0
  177. package/dist/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
  178. package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
  179. package/dist/utils/PrecacheInstallReportPlugin.d.ts +14 -0
  180. package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
  181. package/dist/utils/SerwistError.d.ts +24 -0
  182. package/dist/utils/SerwistError.d.ts.map +1 -0
  183. package/dist/utils/assert.d.ts +11 -0
  184. package/dist/utils/assert.d.ts.map +1 -0
  185. package/dist/utils/cacheMatchIgnoreParams.d.ts +15 -0
  186. package/dist/utils/cacheMatchIgnoreParams.d.ts.map +1 -0
  187. package/dist/utils/cacheNames.d.ts +40 -0
  188. package/dist/utils/cacheNames.d.ts.map +1 -0
  189. package/dist/utils/canConstructReadableStream.d.ts +12 -0
  190. package/dist/utils/canConstructReadableStream.d.ts.map +1 -0
  191. package/dist/utils/canConstructResponseFromBodyStream.d.ts +11 -0
  192. package/dist/utils/canConstructResponseFromBodyStream.d.ts.map +1 -0
  193. package/dist/utils/createCacheKey.d.ts +16 -0
  194. package/dist/utils/createCacheKey.d.ts.map +1 -0
  195. package/dist/utils/deleteOutdatedCaches.d.ts +18 -0
  196. package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -0
  197. package/dist/utils/dontWaitFor.d.ts +7 -0
  198. package/dist/utils/dontWaitFor.d.ts.map +1 -0
  199. package/dist/utils/executeQuotaErrorCallbacks.d.ts +8 -0
  200. package/dist/utils/executeQuotaErrorCallbacks.d.ts.map +1 -0
  201. package/dist/utils/generateURLVariations.d.ts +12 -0
  202. package/dist/utils/generateURLVariations.d.ts.map +1 -0
  203. package/dist/utils/getFriendlyURL.d.ts +3 -0
  204. package/dist/utils/getFriendlyURL.d.ts.map +1 -0
  205. package/dist/utils/logger.d.ts +24 -0
  206. package/dist/utils/logger.d.ts.map +1 -0
  207. package/dist/utils/normalizeHandler.d.ts +10 -0
  208. package/dist/utils/normalizeHandler.d.ts.map +1 -0
  209. package/dist/utils/pluginUtils.d.ts +5 -0
  210. package/dist/utils/pluginUtils.d.ts.map +1 -0
  211. package/dist/utils/printCleanupDetails.d.ts +6 -0
  212. package/dist/utils/printCleanupDetails.d.ts.map +1 -0
  213. package/dist/utils/printInstallDetails.d.ts +7 -0
  214. package/dist/utils/printInstallDetails.d.ts.map +1 -0
  215. package/dist/utils/removeIgnoredSearchParams.d.ts +12 -0
  216. package/dist/utils/removeIgnoredSearchParams.d.ts.map +1 -0
  217. package/dist/utils/resultingClientExists.d.ts +12 -0
  218. package/dist/utils/resultingClientExists.d.ts.map +1 -0
  219. package/dist/utils/timeout.d.ts +10 -0
  220. package/dist/utils/timeout.d.ts.map +1 -0
  221. package/dist/utils/waitUntil.d.ts +11 -0
  222. package/dist/utils/waitUntil.d.ts.map +1 -0
  223. package/dist/utils/welcome.d.ts +2 -0
  224. package/dist/utils/welcome.d.ts.map +1 -0
  225. package/package.json +85 -0
  226. package/src/NavigationRoute.ts +119 -0
  227. package/src/PrecacheRoute.ts +46 -0
  228. package/src/PrecacheStrategy.ts +239 -0
  229. package/src/RegExpRoute.ts +74 -0
  230. package/src/Route.ts +67 -0
  231. package/src/Serwist.ts +920 -0
  232. package/src/cacheNames.ts +39 -0
  233. package/src/cleanupOutdatedCaches.ts +32 -0
  234. package/src/clientsClaim.ts +18 -0
  235. package/src/constants.ts +24 -0
  236. package/src/copyResponse.ts +60 -0
  237. package/src/disableDevLogs.ts +10 -0
  238. package/src/index.internal.ts +33 -0
  239. package/src/index.legacy.ts +66 -0
  240. package/src/index.plugins.ts +95 -0
  241. package/src/index.strategies.ts +26 -0
  242. package/src/index.ts +39 -0
  243. package/src/legacy/PrecacheController.ts +337 -0
  244. package/src/legacy/PrecacheFallbackPlugin.ts +93 -0
  245. package/src/legacy/PrecacheRoute.ts +48 -0
  246. package/src/legacy/Router.ts +484 -0
  247. package/src/legacy/addPlugins.ts +21 -0
  248. package/src/legacy/addRoute.ts +29 -0
  249. package/src/legacy/createHandlerBoundToURL.ts +30 -0
  250. package/src/legacy/fallbacks.ts +94 -0
  251. package/src/legacy/getCacheKeyForURL.ts +32 -0
  252. package/src/legacy/handlePrecaching.ts +86 -0
  253. package/src/legacy/installSerwist.ts +19 -0
  254. package/src/legacy/matchPrecache.ts +26 -0
  255. package/src/legacy/precache.ts +31 -0
  256. package/src/legacy/precacheAndRoute.ts +28 -0
  257. package/src/legacy/registerRoute.ts +27 -0
  258. package/src/legacy/registerRuntimeCaching.ts +17 -0
  259. package/src/legacy/setCatchHandler.ts +21 -0
  260. package/src/legacy/setDefaultHandler.ts +24 -0
  261. package/src/legacy/singletonPrecacheController.ts +53 -0
  262. package/src/legacy/singletonRouter.ts +70 -0
  263. package/src/legacy/unregisterRoute.ts +12 -0
  264. package/src/legacy/utils/PrecacheCacheKeyPlugin.ts +33 -0
  265. package/src/legacy/utils/getCacheKeyForURL.ts +36 -0
  266. package/src/models/messages/messageGenerator.ts +29 -0
  267. package/src/models/messages/messages.ts +233 -0
  268. package/src/models/pluginEvents.ts +17 -0
  269. package/src/models/quotaErrorCallbacks.ts +13 -0
  270. package/src/navigationPreload.ts +68 -0
  271. package/src/parseRoute.ts +78 -0
  272. package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +38 -0
  273. package/src/plugins/backgroundSync/Queue.ts +440 -0
  274. package/src/plugins/backgroundSync/QueueDb.ts +176 -0
  275. package/src/plugins/backgroundSync/QueueStore.ts +160 -0
  276. package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
  277. package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +161 -0
  278. package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +42 -0
  279. package/src/plugins/broadcastUpdate/constants.ts +12 -0
  280. package/src/plugins/broadcastUpdate/responsesAreSame.ts +49 -0
  281. package/src/plugins/broadcastUpdate/types.ts +37 -0
  282. package/src/plugins/cacheableResponse/CacheableResponse.ts +144 -0
  283. package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +45 -0
  284. package/src/plugins/expiration/CacheExpiration.ts +193 -0
  285. package/src/plugins/expiration/ExpirationPlugin.ts +300 -0
  286. package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
  287. package/src/plugins/googleAnalytics/constants.ts +22 -0
  288. package/src/plugins/googleAnalytics/initialize.ts +209 -0
  289. package/src/plugins/precaching/PrecacheFallbackPlugin.ts +83 -0
  290. package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +38 -0
  291. package/src/plugins/rangeRequests/createPartialResponse.ts +93 -0
  292. package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +59 -0
  293. package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +55 -0
  294. package/src/registerQuotaErrorCallback.ts +34 -0
  295. package/src/setCacheNameDetails.ts +53 -0
  296. package/src/strategies/CacheFirst.ts +88 -0
  297. package/src/strategies/CacheOnly.ts +59 -0
  298. package/src/strategies/NetworkFirst.ts +229 -0
  299. package/src/strategies/NetworkOnly.ts +98 -0
  300. package/src/strategies/StaleWhileRevalidate.ts +110 -0
  301. package/src/strategies/Strategy.ts +204 -0
  302. package/src/strategies/StrategyHandler.ts +554 -0
  303. package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
  304. package/src/strategies/utils/messages.ts +21 -0
  305. package/src/types.ts +358 -0
  306. package/src/utils/Deferred.ts +33 -0
  307. package/src/utils/PrecacheCacheKeyPlugin.ts +33 -0
  308. package/src/utils/PrecacheInstallReportPlugin.ts +47 -0
  309. package/src/utils/SerwistError.ts +41 -0
  310. package/src/utils/assert.ts +89 -0
  311. package/src/utils/cacheMatchIgnoreParams.ts +54 -0
  312. package/src/utils/cacheNames.ts +87 -0
  313. package/src/utils/canConstructReadableStream.ts +34 -0
  314. package/src/utils/canConstructResponseFromBodyStream.ts +37 -0
  315. package/src/utils/createCacheKey.ts +68 -0
  316. package/src/utils/deleteOutdatedCaches.ts +40 -0
  317. package/src/utils/dontWaitFor.ts +16 -0
  318. package/src/utils/executeQuotaErrorCallbacks.ts +33 -0
  319. package/src/utils/generateURLVariations.ts +55 -0
  320. package/src/utils/getFriendlyURL.ts +16 -0
  321. package/src/utils/logger.ts +95 -0
  322. package/src/utils/normalizeHandler.ts +40 -0
  323. package/src/utils/pluginUtils.ts +15 -0
  324. package/src/utils/printCleanupDetails.ts +38 -0
  325. package/src/utils/printInstallDetails.ts +53 -0
  326. package/src/utils/removeIgnoredSearchParams.ts +29 -0
  327. package/src/utils/resultingClientExists.ts +58 -0
  328. package/src/utils/timeout.ts +19 -0
  329. package/src/utils/waitUntil.ts +21 -0
  330. package/src/utils/welcome.ts +19 -0
@@ -0,0 +1,337 @@
1
+ /*
2
+ Copyright 2019 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 { RouteHandlerCallback, SerwistPlugin } from "../types.js";
10
+ import { assert } from "../utils/assert.js";
11
+ import { SerwistError } from "../utils/SerwistError.js";
12
+ import { logger } from "../utils/logger.js";
13
+ import { cacheNames as privateCacheNames } from "../utils/cacheNames.js";
14
+ import { waitUntil } from "../utils/waitUntil.js";
15
+ import { parallel } from "@serwist/utils";
16
+ import { PrecacheStrategy } from "../PrecacheStrategy.js";
17
+ import type { Strategy } from "../strategies/Strategy.js";
18
+ import type { CleanupResult, InstallResult, PrecacheEntry } from "../types.js";
19
+ import { PrecacheInstallReportPlugin } from "../utils/PrecacheInstallReportPlugin.js";
20
+ import { createCacheKey } from "../utils/createCacheKey.js";
21
+ import { printCleanupDetails } from "../utils/printCleanupDetails.js";
22
+ import { printInstallDetails } from "../utils/printInstallDetails.js";
23
+ import { PrecacheCacheKeyPlugin } from "./utils/PrecacheCacheKeyPlugin.js";
24
+
25
+ // Give TypeScript the correct global.
26
+ declare const self: ServiceWorkerGlobalScope;
27
+
28
+ interface PrecacheControllerOptions {
29
+ /**
30
+ * The cache to use for precaching.
31
+ */
32
+ cacheName?: string;
33
+ /**
34
+ * Plugins to use when precaching as well as responding to fetch
35
+ * events for precached assets.
36
+ */
37
+ plugins?: SerwistPlugin[];
38
+ /**
39
+ * Whether to attempt to get the response from the network if there's
40
+ * a precache miss.
41
+ */
42
+ fallbackToNetwork?: boolean;
43
+ /**
44
+ * The number of precache requests that should be made concurrently.
45
+ *
46
+ * @default 1
47
+ */
48
+ concurrentPrecaching?: number;
49
+ }
50
+
51
+ /**
52
+ * Performs efficient precaching of assets.
53
+ * @deprecated
54
+ */
55
+ export class PrecacheController {
56
+ private _installAndActiveListenersAdded?: boolean;
57
+ private _concurrentPrecaching: number;
58
+ private readonly _strategy: Strategy;
59
+ private readonly _urlsToCacheKeys: Map<string, string> = new Map();
60
+ private readonly _urlsToCacheModes: Map<string, "reload" | "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached"> = new Map();
61
+ private readonly _cacheKeysToIntegrities: Map<string, string> = new Map();
62
+
63
+ /**
64
+ * Create a new PrecacheController.
65
+ *
66
+ * @param options
67
+ */
68
+ constructor({ cacheName, plugins = [], fallbackToNetwork = true, concurrentPrecaching = 1 }: PrecacheControllerOptions = {}) {
69
+ this._concurrentPrecaching = concurrentPrecaching;
70
+ this._strategy = new PrecacheStrategy({
71
+ cacheName: privateCacheNames.getPrecacheName(cacheName),
72
+ plugins: [...plugins, new PrecacheCacheKeyPlugin({ precacheController: this })],
73
+ fallbackToNetwork,
74
+ });
75
+ // Bind the install and activate methods to the instance.
76
+ this.install = this.install.bind(this);
77
+ this.activate = this.activate.bind(this);
78
+ }
79
+
80
+ /**
81
+ * The strategy created by this controller and
82
+ * used to cache assets and respond to fetch events.
83
+ */
84
+ get strategy(): Strategy {
85
+ return this._strategy;
86
+ }
87
+
88
+ /**
89
+ * Adds items to the precache list, removing any duplicates and
90
+ * stores the files in the precache cache when the service
91
+ * worker installs.
92
+ *
93
+ * This method can be called multiple times.
94
+ *
95
+ * @param entries Array of entries to precache.
96
+ */
97
+ precache(entries: (PrecacheEntry | string)[]): void {
98
+ this.addToCacheList(entries);
99
+
100
+ if (!this._installAndActiveListenersAdded) {
101
+ self.addEventListener("install", this.install);
102
+ self.addEventListener("activate", this.activate);
103
+ this._installAndActiveListenersAdded = true;
104
+ }
105
+ }
106
+
107
+ /**
108
+ * This method will add items to the precache list, removing duplicates
109
+ * and ensuring the information is valid.
110
+ *
111
+ * @param entries Array of entries to precache.
112
+ */
113
+ addToCacheList(entries: (PrecacheEntry | string)[]): void {
114
+ if (process.env.NODE_ENV !== "production") {
115
+ assert!.isArray(entries, {
116
+ moduleName: "serwist/legacy",
117
+ className: "PrecacheController",
118
+ funcName: "addToCacheList",
119
+ paramName: "entries",
120
+ });
121
+ }
122
+
123
+ const urlsToWarnAbout: string[] = [];
124
+ for (const entry of entries) {
125
+ // See https://github.com/GoogleChrome/workbox/issues/2259
126
+ if (typeof entry === "string") {
127
+ urlsToWarnAbout.push(entry);
128
+ } else if (entry && !entry.integrity && entry.revision === undefined) {
129
+ urlsToWarnAbout.push(entry.url);
130
+ }
131
+
132
+ const { cacheKey, url } = createCacheKey(entry);
133
+ const cacheMode = typeof entry !== "string" && entry.revision ? "reload" : "default";
134
+
135
+ if (this._urlsToCacheKeys.has(url) && this._urlsToCacheKeys.get(url) !== cacheKey) {
136
+ throw new SerwistError("add-to-cache-list-conflicting-entries", {
137
+ firstEntry: this._urlsToCacheKeys.get(url),
138
+ secondEntry: cacheKey,
139
+ });
140
+ }
141
+
142
+ if (typeof entry !== "string" && entry.integrity) {
143
+ if (this._cacheKeysToIntegrities.has(cacheKey) && this._cacheKeysToIntegrities.get(cacheKey) !== entry.integrity) {
144
+ throw new SerwistError("add-to-cache-list-conflicting-integrities", {
145
+ url,
146
+ });
147
+ }
148
+ this._cacheKeysToIntegrities.set(cacheKey, entry.integrity);
149
+ }
150
+
151
+ this._urlsToCacheKeys.set(url, cacheKey);
152
+ this._urlsToCacheModes.set(url, cacheMode);
153
+
154
+ if (urlsToWarnAbout.length > 0) {
155
+ const warningMessage = `Serwist is precaching URLs without revision info: ${urlsToWarnAbout.join(
156
+ ", ",
157
+ )}\nThis is generally NOT safe. Learn more at https://bit.ly/wb-precache`;
158
+ if (process.env.NODE_ENV === "production") {
159
+ // Use console directly to display this warning without bloating
160
+ // bundle sizes by pulling in all of the logger codebase in prod.
161
+ console.warn(warningMessage);
162
+ } else {
163
+ logger.warn(warningMessage);
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Precaches new and updated assets. Call this method from the service worker
171
+ * install event.
172
+ *
173
+ * Note: this method calls `event.waitUntil()` for you, so you do not need
174
+ * to call it yourself in your event handlers.
175
+ *
176
+ * @param event
177
+ * @returns
178
+ */
179
+ install(event: ExtendableEvent): Promise<InstallResult> {
180
+ return waitUntil<InstallResult>(event, async () => {
181
+ const installReportPlugin = new PrecacheInstallReportPlugin();
182
+ this.strategy.plugins.push(installReportPlugin);
183
+
184
+ await parallel(this._concurrentPrecaching, Array.from(this._urlsToCacheKeys.entries()), async ([url, cacheKey]): Promise<void> => {
185
+ const integrity = this._cacheKeysToIntegrities.get(cacheKey);
186
+ const cacheMode = this._urlsToCacheModes.get(url);
187
+
188
+ const request = new Request(url, {
189
+ integrity,
190
+ cache: cacheMode,
191
+ credentials: "same-origin",
192
+ });
193
+
194
+ await Promise.all(
195
+ this.strategy.handleAll({
196
+ event,
197
+ request,
198
+ url: new URL(request.url),
199
+ params: { cacheKey },
200
+ }),
201
+ );
202
+ });
203
+
204
+ const { updatedURLs, notUpdatedURLs } = installReportPlugin;
205
+
206
+ if (process.env.NODE_ENV !== "production") {
207
+ printInstallDetails(updatedURLs, notUpdatedURLs);
208
+ }
209
+
210
+ return { updatedURLs, notUpdatedURLs };
211
+ });
212
+ }
213
+
214
+ /**
215
+ * Deletes assets that are no longer present in the current precache manifest.
216
+ * Call this method from the service worker activate event.
217
+ *
218
+ * Note: this method calls `event.waitUntil()` for you, so you do not need
219
+ * to call it yourself in your event handlers.
220
+ *
221
+ * @param event
222
+ * @returns
223
+ */
224
+ activate(event: ExtendableEvent): Promise<CleanupResult> {
225
+ return waitUntil<CleanupResult>(event, async () => {
226
+ const cache = await self.caches.open(this.strategy.cacheName);
227
+ const currentlyCachedRequests = await cache.keys();
228
+ const expectedCacheKeys = new Set(this._urlsToCacheKeys.values());
229
+
230
+ const deletedCacheRequests: string[] = [];
231
+
232
+ for (const request of currentlyCachedRequests) {
233
+ if (!expectedCacheKeys.has(request.url)) {
234
+ await cache.delete(request);
235
+ deletedCacheRequests.push(request.url);
236
+ }
237
+ }
238
+
239
+ if (process.env.NODE_ENV !== "production") {
240
+ printCleanupDetails(deletedCacheRequests);
241
+ }
242
+
243
+ return { deletedCacheRequests };
244
+ });
245
+ }
246
+
247
+ /**
248
+ * Returns a mapping of a precached URL to the corresponding cache key, taking
249
+ * into account the revision information for the URL.
250
+ *
251
+ * @returns A URL to cache key mapping.
252
+ */
253
+ getURLsToCacheKeys(): Map<string, string> {
254
+ return this._urlsToCacheKeys;
255
+ }
256
+
257
+ /**
258
+ * Returns a list of all the URLs that have been precached by the current
259
+ * service worker.
260
+ *
261
+ * @returns The precached URLs.
262
+ */
263
+ getCachedURLs(): string[] {
264
+ return [...this._urlsToCacheKeys.keys()];
265
+ }
266
+
267
+ /**
268
+ * Returns the cache key used for storing a given URL. If that URL is
269
+ * unversioned, like `/index.html', then the cache key will be the original
270
+ * URL with a search parameter appended to it.
271
+ *
272
+ * @param url A URL whose cache key you want to look up.
273
+ * @returns The versioned URL that corresponds to a cache key
274
+ * for the original URL, or undefined if that URL isn't precached.
275
+ */
276
+ getCacheKeyForURL(url: string): string | undefined {
277
+ const urlObject = new URL(url, location.href);
278
+ return this._urlsToCacheKeys.get(urlObject.href);
279
+ }
280
+
281
+ /**
282
+ * @param url A cache key whose SRI you want to look up.
283
+ * @returns The subresource integrity associated with the cache key,
284
+ * or undefined if it's not set.
285
+ */
286
+ getIntegrityForCacheKey(cacheKey: string): string | undefined {
287
+ return this._cacheKeysToIntegrities.get(cacheKey);
288
+ }
289
+
290
+ /**
291
+ * This acts as a drop-in replacement for
292
+ * [`cache.match()`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/match)
293
+ * with the following differences:
294
+ *
295
+ * - It knows what the name of the precache is, and only checks in that cache.
296
+ * - It allows you to pass in an "original" URL without versioning parameters,
297
+ * and it will automatically look up the correct cache key for the currently
298
+ * active revision of that URL.
299
+ *
300
+ * E.g., `matchPrecache('index.html')` will find the correct precached
301
+ * response for the currently active service worker, even if the actual cache
302
+ * key is `'/index.html?__WB_REVISION__=1234abcd'`.
303
+ *
304
+ * @param request The key (without revisioning parameters)
305
+ * to look up in the precache.
306
+ * @returns
307
+ */
308
+ async matchPrecache(request: string | Request): Promise<Response | undefined> {
309
+ const url = request instanceof Request ? request.url : request;
310
+ const cacheKey = this.getCacheKeyForURL(url);
311
+ if (cacheKey) {
312
+ const cache = await self.caches.open(this.strategy.cacheName);
313
+ return cache.match(cacheKey);
314
+ }
315
+ return undefined;
316
+ }
317
+
318
+ /**
319
+ * Returns a function that looks up `url` in the precache (taking into
320
+ * account revision information), and returns the corresponding `Response`.
321
+ *
322
+ * @param url The precached URL which will be used to lookup the response.
323
+ * @return
324
+ */
325
+ createHandlerBoundToURL(url: string): RouteHandlerCallback {
326
+ const cacheKey = this.getCacheKeyForURL(url);
327
+ if (!cacheKey) {
328
+ throw new SerwistError("non-precached-url", { url });
329
+ }
330
+ return (options) => {
331
+ options.request = new Request(url);
332
+ options.params = { cacheKey, ...options.params };
333
+
334
+ return this.strategy.handle(options);
335
+ };
336
+ }
337
+ }
@@ -0,0 +1,93 @@
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 { PrecacheController } from "./PrecacheController.js";
11
+ import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
12
+
13
+ /**
14
+ * @deprecated
15
+ */
16
+ export interface PrecacheFallbackEntry {
17
+ /**
18
+ * A function that checks whether the fallback entry can be used
19
+ * for a request.
20
+ */
21
+ matcher: (param: HandlerDidErrorCallbackParam) => boolean;
22
+ /**
23
+ * A precached URL to be used as a fallback.
24
+ */
25
+ url: string;
26
+ }
27
+
28
+ /**
29
+ * @deprecated
30
+ */
31
+ export interface PrecacheFallbackPluginOptions {
32
+ /**
33
+ * Precached URLs to be used as the fallback
34
+ * if the associated strategy can't generate a response.
35
+ */
36
+ fallbackUrls: (string | PrecacheFallbackEntry)[];
37
+ /**
38
+ * An optional `PrecacheController` instance. If not provided, the default
39
+ * `PrecacheController` will be used.
40
+ */
41
+ precacheController?: PrecacheController;
42
+ }
43
+
44
+ /**
45
+ * `PrecacheFallbackPlugin` allows you to specify offline fallbacks
46
+ * to be used when a given strategy is unable to generate a response.
47
+ *
48
+ * It does this by intercepting the `handlerDidError` plugin callback
49
+ * and returning a precached response, taking the expected revision parameter
50
+ * into account automatically.
51
+ *
52
+ * Unless you explicitly pass in a `PrecacheController` instance to the
53
+ * constructor, the default instance will be used. Generally speaking, most
54
+ * developers will end up using the default.
55
+ *
56
+ * @deprecated
57
+ */
58
+ export class PrecacheFallbackPlugin implements SerwistPlugin {
59
+ private readonly _fallbackUrls: (string | PrecacheFallbackEntry)[];
60
+ private readonly _precacheController: PrecacheController;
61
+
62
+ /**
63
+ * Constructs a new `PrecacheFallbackPlugin` with the associated `fallbackUrls`.
64
+ *
65
+ * @param config
66
+ */
67
+ constructor({ fallbackUrls, precacheController }: PrecacheFallbackPluginOptions) {
68
+ this._fallbackUrls = fallbackUrls;
69
+ this._precacheController = precacheController || getSingletonPrecacheController();
70
+ }
71
+
72
+ /**
73
+ * @returns The precache response for one of the fallback URLs, or `undefined` if
74
+ * nothing satisfies the conditions.
75
+ * @private
76
+ */
77
+ async handlerDidError(param: HandlerDidErrorCallbackParam) {
78
+ for (const fallback of this._fallbackUrls) {
79
+ if (typeof fallback === "string") {
80
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback);
81
+ if (fallbackResponse !== undefined) {
82
+ return fallbackResponse;
83
+ }
84
+ } else if (fallback.matcher(param)) {
85
+ const fallbackResponse = await this._precacheController.matchPrecache(fallback.url);
86
+ if (fallbackResponse !== undefined) {
87
+ return fallbackResponse;
88
+ }
89
+ }
90
+ }
91
+ return undefined;
92
+ }
93
+ }
@@ -0,0 +1,48 @@
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 { PrecacheRouteOptions } from "../types.js";
14
+ import { generateURLVariations } from "../utils/generateURLVariations.js";
15
+ import type { PrecacheController } from "./PrecacheController.js";
16
+
17
+ /**
18
+ * A subclass of `serwist.Route` that takes a `serwist/legacy.PrecacheController`
19
+ * instance and uses it to match incoming requests and handle fetching
20
+ * responses from the precache.
21
+ * @deprecated
22
+ */
23
+ export class PrecacheRoute extends Route {
24
+ /**
25
+ * @param precacheController A `PrecacheController`
26
+ * instance used to both match requests and respond to fetch events.
27
+ * @param options Options to control how requests are matched
28
+ * against the list of precached URLs.
29
+ */
30
+ constructor(precacheController: PrecacheController, options?: PrecacheRouteOptions) {
31
+ const match: RouteMatchCallback = ({ request }: RouteMatchCallbackOptions) => {
32
+ const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
33
+ for (const possibleURL of generateURLVariations(request.url, options)) {
34
+ const cacheKey = urlsToCacheKeys.get(possibleURL);
35
+ if (cacheKey) {
36
+ const integrity = precacheController.getIntegrityForCacheKey(cacheKey);
37
+ return { cacheKey, integrity };
38
+ }
39
+ }
40
+ if (process.env.NODE_ENV !== "production") {
41
+ logger.debug(`Precaching did not find a match for ${getFriendlyURL(request.url)}`);
42
+ }
43
+ return;
44
+ };
45
+
46
+ super(match, precacheController.strategy);
47
+ }
48
+ }