serwist 10.0.0-preview.7 → 10.0.0-preview.9

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 (261) hide show
  1. package/dist/chunks/waitUntil.js +129 -129
  2. package/dist/index.d.ts +19 -21
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.internal.d.ts +2 -2
  5. package/dist/index.internal.d.ts.map +1 -1
  6. package/dist/index.internal.js +3 -3
  7. package/dist/index.js +2026 -1917
  8. package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncPlugin.d.ts +1 -1
  9. package/dist/lib/background-sync/BackgroundSyncPlugin.d.ts.map +1 -0
  10. package/dist/lib/background-sync/BackgroundSyncQueue.d.ts.map +1 -0
  11. package/dist/lib/background-sync/BackgroundSyncQueueDb.d.ts.map +1 -0
  12. package/dist/lib/background-sync/BackgroundSyncQueueStore.d.ts.map +1 -0
  13. package/dist/lib/{backgroundSync → background-sync}/StorableRequest.d.ts +1 -1
  14. package/dist/lib/background-sync/StorableRequest.d.ts.map +1 -0
  15. package/dist/lib/background-sync/index.d.ts.map +1 -0
  16. package/dist/lib/{broadcastUpdate → broadcast-update}/BroadcastCacheUpdate.d.ts +1 -1
  17. package/dist/lib/broadcast-update/BroadcastCacheUpdate.d.ts.map +1 -0
  18. package/dist/lib/{broadcastUpdate → broadcast-update}/BroadcastUpdatePlugin.d.ts +1 -1
  19. package/dist/lib/broadcast-update/BroadcastUpdatePlugin.d.ts.map +1 -0
  20. package/dist/lib/broadcast-update/constants.d.ts.map +1 -0
  21. package/dist/lib/broadcast-update/index.d.ts.map +1 -0
  22. package/dist/lib/broadcast-update/responsesAreSame.d.ts.map +1 -0
  23. package/dist/lib/{broadcastUpdate → broadcast-update}/types.d.ts +1 -1
  24. package/dist/lib/broadcast-update/types.d.ts.map +1 -0
  25. package/dist/{cacheNames.d.ts → lib/cache-name.d.ts} +1 -1
  26. package/dist/lib/cache-name.d.ts.map +1 -0
  27. package/dist/lib/cacheable-response/CacheableResponse.d.ts.map +1 -0
  28. package/dist/lib/{cacheableResponse → cacheable-response}/CacheableResponsePlugin.d.ts +1 -1
  29. package/dist/lib/cacheable-response/CacheableResponsePlugin.d.ts.map +1 -0
  30. package/dist/lib/{cacheableResponse → cacheable-response}/index.d.ts +1 -1
  31. package/dist/lib/cacheable-response/index.d.ts.map +1 -0
  32. package/dist/{constants.d.ts → lib/constants.d.ts} +1 -1
  33. package/dist/lib/constants.d.ts.map +1 -0
  34. package/dist/lib/core.d.ts +62 -0
  35. package/dist/lib/core.d.ts.map +1 -0
  36. package/dist/lib/expiration/ExpirationPlugin.d.ts +1 -1
  37. package/dist/lib/expiration/ExpirationPlugin.d.ts.map +1 -1
  38. package/dist/lib/extension.d.ts +30 -0
  39. package/dist/lib/extension.d.ts.map +1 -0
  40. package/dist/lib/extensions/google-analytics/constants.d.ts.map +1 -0
  41. package/dist/lib/extensions/google-analytics/extension.d.ts +79 -0
  42. package/dist/lib/extensions/google-analytics/extension.d.ts.map +1 -0
  43. package/dist/lib/extensions/google-analytics/index.d.ts +2 -0
  44. package/dist/lib/extensions/google-analytics/index.d.ts.map +1 -0
  45. package/dist/lib/extensions/google-analytics/initialize.d.ts +12 -0
  46. package/dist/lib/extensions/google-analytics/initialize.d.ts.map +1 -0
  47. package/dist/lib/extensions/index.d.ts +14 -0
  48. package/dist/lib/extensions/index.d.ts.map +1 -0
  49. package/dist/lib/{controllers/PrecacheController/PrecacheController.d.ts → extensions/precache/extension.d.ts} +11 -34
  50. package/dist/lib/extensions/precache/extension.d.ts.map +1 -0
  51. package/dist/lib/extensions/precache/options.d.ts +26 -0
  52. package/dist/lib/extensions/precache/options.d.ts.map +1 -0
  53. package/dist/lib/{controllers/PrecacheController/PrecacheCacheKeyPlugin.d.ts → extensions/precache/plugin-cache-key.d.ts} +4 -4
  54. package/dist/lib/extensions/precache/plugin-cache-key.d.ts.map +1 -0
  55. package/dist/lib/{precaching/PrecacheFallbackPlugin.d.ts → extensions/precache/plugin-fallback.d.ts} +7 -12
  56. package/dist/lib/extensions/precache/plugin-fallback.d.ts.map +1 -0
  57. package/dist/lib/{controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts → extensions/precache/plugin-install-report.d.ts} +2 -2
  58. package/dist/lib/extensions/precache/plugin-install-report.d.ts.map +1 -0
  59. package/dist/lib/extensions/precache/route.d.ts +42 -0
  60. package/dist/lib/extensions/precache/route.d.ts.map +1 -0
  61. package/dist/lib/{controllers/PrecacheController/PrecacheStrategy.d.ts → extensions/precache/strategy.d.ts} +2 -2
  62. package/dist/lib/extensions/precache/strategy.d.ts.map +1 -0
  63. package/dist/lib/{controllers/RuntimeCacheController.d.ts → extensions/runtime-cache.d.ts} +9 -8
  64. package/dist/lib/extensions/runtime-cache.d.ts.map +1 -0
  65. package/dist/lib/functions/handlers.d.ts +60 -0
  66. package/dist/lib/functions/handlers.d.ts.map +1 -0
  67. package/dist/lib/functions/router.d.ts +60 -0
  68. package/dist/lib/functions/router.d.ts.map +1 -0
  69. package/dist/{navigationPreload.d.ts → lib/navigation-preload.d.ts} +1 -1
  70. package/dist/lib/navigation-preload.d.ts.map +1 -0
  71. package/dist/lib/{rangeRequests → range-requests}/RangeRequestsPlugin.d.ts +1 -1
  72. package/dist/lib/range-requests/RangeRequestsPlugin.d.ts.map +1 -0
  73. package/dist/lib/range-requests/createPartialResponse.d.ts.map +1 -0
  74. package/dist/lib/{rangeRequests → range-requests}/index.d.ts +1 -1
  75. package/dist/lib/range-requests/index.d.ts.map +1 -0
  76. package/dist/lib/range-requests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
  77. package/dist/lib/range-requests/utils/parseRangeHeader.d.ts.map +1 -0
  78. package/dist/lib/route.d.ts +106 -0
  79. package/dist/lib/route.d.ts.map +1 -0
  80. package/dist/{Serwist.d.ts → lib/serwist.d.ts} +27 -40
  81. package/dist/lib/serwist.d.ts.map +1 -0
  82. package/dist/lib/strategies/NetworkFirst.d.ts.map +1 -1
  83. package/dist/lib/strategies/StaleWhileRevalidate.d.ts.map +1 -1
  84. package/dist/lib/strategies/Strategy.d.ts +1 -1
  85. package/dist/lib/strategies/Strategy.d.ts.map +1 -1
  86. package/dist/lib/strategies/StrategyHandler.d.ts +1 -1
  87. package/dist/lib/strategies/StrategyHandler.d.ts.map +1 -1
  88. package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +1 -1
  89. package/dist/lib/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -1
  90. package/dist/{types.d.ts → lib/types.d.ts} +15 -43
  91. package/dist/lib/types.d.ts.map +1 -0
  92. package/dist/{copyResponse.d.ts → lib/utils.d.ts} +22 -1
  93. package/dist/lib/utils.d.ts.map +1 -0
  94. package/dist/models/messages/messageGenerator.d.ts +1 -1
  95. package/dist/models/messages/messageGenerator.d.ts.map +1 -1
  96. package/dist/utils/SerwistError.d.ts +1 -1
  97. package/dist/utils/SerwistError.d.ts.map +1 -1
  98. package/dist/utils/assert.d.ts +1 -1
  99. package/dist/utils/assert.d.ts.map +1 -1
  100. package/dist/utils/cacheNames.d.ts.map +1 -1
  101. package/dist/utils/createCacheKey.d.ts +1 -1
  102. package/dist/utils/createCacheKey.d.ts.map +1 -1
  103. package/dist/utils/generateURLVariations.d.ts +1 -1
  104. package/dist/utils/generateURLVariations.d.ts.map +1 -1
  105. package/dist/utils/logger.d.ts +1 -1
  106. package/dist/utils/logger.d.ts.map +1 -1
  107. package/dist/utils/normalizeHandler.d.ts +1 -1
  108. package/dist/utils/normalizeHandler.d.ts.map +1 -1
  109. package/dist/utils/parseRoute.d.ts +3 -3
  110. package/dist/utils/parseRoute.d.ts.map +1 -1
  111. package/dist/utils/pluginUtils.d.ts +1 -1
  112. package/dist/utils/pluginUtils.d.ts.map +1 -1
  113. package/package.json +5 -5
  114. package/src/index.internal.ts +2 -2
  115. package/src/index.ts +51 -33
  116. package/src/lib/{backgroundSync → background-sync}/BackgroundSyncPlugin.ts +1 -1
  117. package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueue.ts +4 -4
  118. package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueueDb.ts +1 -1
  119. package/src/lib/{backgroundSync → background-sync}/BackgroundSyncQueueStore.ts +0 -2
  120. package/src/lib/{backgroundSync → background-sync}/StorableRequest.ts +1 -1
  121. package/src/lib/{broadcastUpdate → broadcast-update}/BroadcastCacheUpdate.ts +1 -1
  122. package/src/lib/{broadcastUpdate → broadcast-update}/BroadcastUpdatePlugin.ts +1 -1
  123. package/src/lib/{broadcastUpdate → broadcast-update}/types.ts +1 -1
  124. package/src/lib/{cacheableResponse → cacheable-response}/CacheableResponsePlugin.ts +1 -1
  125. package/src/lib/core.ts +128 -0
  126. package/src/lib/expiration/CacheExpiration.ts +1 -1
  127. package/src/lib/expiration/ExpirationPlugin.ts +4 -5
  128. package/src/lib/extension.ts +37 -0
  129. package/src/lib/{googleAnalytics/initializeGoogleAnalytics.ts → extensions/google-analytics/extension.ts} +40 -46
  130. package/src/lib/extensions/google-analytics/index.ts +0 -0
  131. package/src/lib/extensions/google-analytics/initialize.ts +48 -0
  132. package/src/lib/extensions/index.ts +13 -0
  133. package/src/lib/{controllers/PrecacheController/PrecacheController.ts → extensions/precache/extension.ts} +23 -45
  134. package/src/lib/{controllers/PrecacheController/parsePrecacheOptions.ts → extensions/precache/options.ts} +11 -8
  135. package/src/lib/{controllers/PrecacheController/PrecacheCacheKeyPlugin.ts → extensions/precache/plugin-cache-key.ts} +4 -4
  136. package/src/lib/{precaching/PrecacheFallbackPlugin.ts → extensions/precache/plugin-fallback.ts} +22 -21
  137. package/src/lib/{controllers/PrecacheController/PrecacheInstallReportPlugin.ts → extensions/precache/plugin-install-report.ts} +1 -1
  138. package/src/lib/extensions/precache/route.ts +72 -0
  139. package/src/lib/{controllers/PrecacheController/PrecacheStrategy.ts → extensions/precache/strategy.ts} +5 -5
  140. package/src/lib/{controllers/RuntimeCacheController.ts → extensions/runtime-cache.ts} +21 -14
  141. package/src/lib/functions/handlers.ts +149 -0
  142. package/src/lib/functions/router.ts +314 -0
  143. package/src/lib/{rangeRequests → range-requests}/RangeRequestsPlugin.ts +1 -1
  144. package/src/lib/route.ts +234 -0
  145. package/src/lib/serwist.ts +443 -0
  146. package/src/lib/strategies/CacheFirst.ts +2 -2
  147. package/src/lib/strategies/CacheOnly.ts +1 -1
  148. package/src/lib/strategies/NetworkFirst.ts +4 -4
  149. package/src/lib/strategies/NetworkOnly.ts +2 -2
  150. package/src/lib/strategies/StaleWhileRevalidate.ts +3 -3
  151. package/src/lib/strategies/Strategy.ts +6 -6
  152. package/src/lib/strategies/StrategyHandler.ts +5 -5
  153. package/src/lib/strategies/plugins/cacheOkAndOpaquePlugin.ts +1 -1
  154. package/src/{types.ts → lib/types.ts} +17 -57
  155. package/src/{copyResponse.ts → lib/utils.ts} +81 -2
  156. package/src/models/messages/messageGenerator.ts +1 -1
  157. package/src/utils/SerwistError.ts +1 -1
  158. package/src/utils/assert.ts +1 -2
  159. package/src/utils/cacheNames.ts +0 -2
  160. package/src/utils/canConstructReadableStream.ts +1 -1
  161. package/src/utils/canConstructResponseFromBodyStream.ts +1 -1
  162. package/src/utils/createCacheKey.ts +1 -2
  163. package/src/utils/generateURLVariations.ts +1 -1
  164. package/src/utils/normalizeHandler.ts +1 -1
  165. package/src/utils/parseRoute.ts +4 -5
  166. package/src/utils/pluginUtils.ts +1 -1
  167. package/src/utils/resultingClientExists.ts +1 -1
  168. package/dist/NavigationRoute.d.ts +0 -56
  169. package/dist/NavigationRoute.d.ts.map +0 -1
  170. package/dist/RegExpRoute.d.ts +0 -24
  171. package/dist/RegExpRoute.d.ts.map +0 -1
  172. package/dist/Route.d.ts +0 -33
  173. package/dist/Route.d.ts.map +0 -1
  174. package/dist/Serwist.d.ts.map +0 -1
  175. package/dist/cacheNames.d.ts.map +0 -1
  176. package/dist/constants.d.ts.map +0 -1
  177. package/dist/copyResponse.d.ts.map +0 -1
  178. package/dist/disableDevLogs.d.ts +0 -7
  179. package/dist/disableDevLogs.d.ts.map +0 -1
  180. package/dist/lib/backgroundSync/BackgroundSyncPlugin.d.ts.map +0 -1
  181. package/dist/lib/backgroundSync/BackgroundSyncQueue.d.ts.map +0 -1
  182. package/dist/lib/backgroundSync/BackgroundSyncQueueDb.d.ts.map +0 -1
  183. package/dist/lib/backgroundSync/BackgroundSyncQueueStore.d.ts.map +0 -1
  184. package/dist/lib/backgroundSync/StorableRequest.d.ts.map +0 -1
  185. package/dist/lib/backgroundSync/index.d.ts.map +0 -1
  186. package/dist/lib/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +0 -1
  187. package/dist/lib/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +0 -1
  188. package/dist/lib/broadcastUpdate/constants.d.ts.map +0 -1
  189. package/dist/lib/broadcastUpdate/index.d.ts.map +0 -1
  190. package/dist/lib/broadcastUpdate/responsesAreSame.d.ts.map +0 -1
  191. package/dist/lib/broadcastUpdate/types.d.ts.map +0 -1
  192. package/dist/lib/cacheableResponse/CacheableResponse.d.ts.map +0 -1
  193. package/dist/lib/cacheableResponse/CacheableResponsePlugin.d.ts.map +0 -1
  194. package/dist/lib/cacheableResponse/index.d.ts.map +0 -1
  195. package/dist/lib/controllers/PrecacheController/PrecacheCacheKeyPlugin.d.ts.map +0 -1
  196. package/dist/lib/controllers/PrecacheController/PrecacheController.d.ts.map +0 -1
  197. package/dist/lib/controllers/PrecacheController/PrecacheInstallReportPlugin.d.ts.map +0 -1
  198. package/dist/lib/controllers/PrecacheController/PrecacheRoute.d.ts +0 -15
  199. package/dist/lib/controllers/PrecacheController/PrecacheRoute.d.ts.map +0 -1
  200. package/dist/lib/controllers/PrecacheController/PrecacheStrategy.d.ts.map +0 -1
  201. package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts +0 -25
  202. package/dist/lib/controllers/PrecacheController/parsePrecacheOptions.d.ts.map +0 -1
  203. package/dist/lib/controllers/RuntimeCacheController.d.ts.map +0 -1
  204. package/dist/lib/controllers/index.d.ts +0 -4
  205. package/dist/lib/controllers/index.d.ts.map +0 -1
  206. package/dist/lib/googleAnalytics/constants.d.ts.map +0 -1
  207. package/dist/lib/googleAnalytics/index.d.ts +0 -3
  208. package/dist/lib/googleAnalytics/index.d.ts.map +0 -1
  209. package/dist/lib/googleAnalytics/initializeGoogleAnalytics.d.ts +0 -30
  210. package/dist/lib/googleAnalytics/initializeGoogleAnalytics.d.ts.map +0 -1
  211. package/dist/lib/precaching/PrecacheFallbackPlugin.d.ts.map +0 -1
  212. package/dist/lib/precaching/index.d.ts +0 -3
  213. package/dist/lib/precaching/index.d.ts.map +0 -1
  214. package/dist/lib/rangeRequests/RangeRequestsPlugin.d.ts.map +0 -1
  215. package/dist/lib/rangeRequests/createPartialResponse.d.ts.map +0 -1
  216. package/dist/lib/rangeRequests/index.d.ts.map +0 -1
  217. package/dist/lib/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +0 -1
  218. package/dist/lib/rangeRequests/utils/parseRangeHeader.d.ts.map +0 -1
  219. package/dist/navigationPreload.d.ts.map +0 -1
  220. package/dist/registerQuotaErrorCallback.d.ts +0 -8
  221. package/dist/registerQuotaErrorCallback.d.ts.map +0 -1
  222. package/dist/setCacheNameDetails.d.ts +0 -9
  223. package/dist/setCacheNameDetails.d.ts.map +0 -1
  224. package/dist/types.d.ts.map +0 -1
  225. package/src/NavigationRoute.ts +0 -118
  226. package/src/RegExpRoute.ts +0 -74
  227. package/src/Route.ts +0 -67
  228. package/src/Serwist.ts +0 -766
  229. package/src/disableDevLogs.ts +0 -10
  230. package/src/lib/controllers/PrecacheController/PrecacheRoute.ts +0 -44
  231. package/src/lib/controllers/index.ts +0 -3
  232. package/src/lib/googleAnalytics/index.ts +0 -2
  233. package/src/lib/precaching/index.ts +0 -2
  234. package/src/registerQuotaErrorCallback.ts +0 -34
  235. package/src/setCacheNameDetails.ts +0 -53
  236. package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncQueue.d.ts +0 -0
  237. package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncQueueDb.d.ts +0 -0
  238. package/dist/lib/{backgroundSync → background-sync}/BackgroundSyncQueueStore.d.ts +0 -0
  239. package/dist/lib/{backgroundSync → background-sync}/index.d.ts +0 -0
  240. package/dist/lib/{broadcastUpdate → broadcast-update}/constants.d.ts +0 -0
  241. package/dist/lib/{broadcastUpdate → broadcast-update}/index.d.ts +0 -0
  242. package/dist/lib/{broadcastUpdate → broadcast-update}/responsesAreSame.d.ts +0 -0
  243. package/dist/lib/{cacheableResponse → cacheable-response}/CacheableResponse.d.ts +0 -0
  244. package/dist/lib/{googleAnalytics → extensions/google-analytics}/constants.d.ts +0 -0
  245. package/dist/lib/{rangeRequests → range-requests}/createPartialResponse.d.ts +0 -0
  246. package/dist/lib/{rangeRequests → range-requests}/utils/calculateEffectiveBoundaries.d.ts +0 -0
  247. package/dist/lib/{rangeRequests → range-requests}/utils/parseRangeHeader.d.ts +0 -0
  248. package/src/lib/{backgroundSync → background-sync}/index.ts +0 -0
  249. package/src/lib/{broadcastUpdate → broadcast-update}/constants.ts +0 -0
  250. package/src/lib/{broadcastUpdate → broadcast-update}/index.ts +0 -0
  251. package/src/lib/{broadcastUpdate → broadcast-update}/responsesAreSame.ts +1 -1
  252. package/src/{cacheNames.ts → lib/cache-name.ts} +0 -0
  253. package/src/lib/{cacheableResponse → cacheable-response}/CacheableResponse.ts +1 -1
  254. package/src/lib/{cacheableResponse → cacheable-response}/index.ts +1 -1
  255. package/src/{constants.ts → lib/constants.ts} +0 -0
  256. package/src/lib/{googleAnalytics → extensions/google-analytics}/constants.ts +0 -0
  257. package/src/{navigationPreload.ts → lib/navigation-preload.ts} +0 -0
  258. package/src/lib/{rangeRequests → range-requests}/createPartialResponse.ts +1 -1
  259. package/src/lib/{rangeRequests → range-requests}/index.ts +1 -1
  260. package/src/lib/{rangeRequests → range-requests}/utils/calculateEffectiveBoundaries.ts +1 -1
  261. package/src/lib/{rangeRequests → range-requests}/utils/parseRangeHeader.ts +1 -1
@@ -0,0 +1,314 @@
1
+ import { assert } from "#utils/assert.js";
2
+ import { getFriendlyURL } from "#utils/getFriendlyURL.js";
3
+ import { logger } from "#utils/logger.js";
4
+ import { parseRoute } from "#utils/parseRoute.js";
5
+ import { SerwistError } from "#utils/SerwistError.js";
6
+ import type { HTTPMethod } from "../constants.js";
7
+ import type { Serwist } from "../core.js";
8
+ import type { Route } from "../route.js";
9
+ import type { RouteHandler, RouteHandlerCallbackOptions, RouteMatchCallback, RouteMatchCallbackOptions } from "../types.js";
10
+
11
+ /**
12
+ * Registers a `RegExp`, string, or function with a caching
13
+ * strategy to the router.
14
+ *
15
+ * @param capture If the capture param is a {@linkcode Route} object, all other arguments will be ignored.
16
+ * @param handler A callback function that returns a `Promise` resulting in a `Response`.
17
+ * This parameter is required if `capture` is not a {@linkcode Route} object.
18
+ * @param method The HTTP method to match the route against. Defaults to `'GET'`.
19
+ * @returns The generated {@linkcode Route} object.
20
+ */
21
+ export const registerCapture = <T extends RegExp | string | RouteMatchCallback | Route>(
22
+ state: Serwist,
23
+ capture: T,
24
+ handler?: T extends Route ? never : RouteHandler,
25
+ method?: T extends Route ? never : HTTPMethod,
26
+ ): Route => {
27
+ const route = parseRoute(capture, handler, method);
28
+ registerRoute(state, route);
29
+ return route;
30
+ };
31
+
32
+ /**
33
+ * Registers a {@linkcode Route} with the router.
34
+ *
35
+ * @param route The {@linkcode Route} to register.
36
+ */
37
+ export const registerRoute = (state: Serwist, route: Route): void => {
38
+ if (process.env.NODE_ENV !== "production") {
39
+ assert!.isType(route, "object", {
40
+ moduleName: "serwist",
41
+ className: "Serwist",
42
+ funcName: "registerRoute",
43
+ paramName: "route",
44
+ });
45
+
46
+ assert!.hasMethod(route, "match", {
47
+ moduleName: "serwist",
48
+ className: "Serwist",
49
+ funcName: "registerRoute",
50
+ paramName: "route",
51
+ });
52
+
53
+ assert!.isType(route.handler, "object", {
54
+ moduleName: "serwist",
55
+ className: "Serwist",
56
+ funcName: "registerRoute",
57
+ paramName: "route",
58
+ });
59
+
60
+ assert!.hasMethod(route.handler, "handle", {
61
+ moduleName: "serwist",
62
+ className: "Serwist",
63
+ funcName: "registerRoute",
64
+ paramName: "route.handler",
65
+ });
66
+
67
+ assert!.isType(route.method, "string", {
68
+ moduleName: "serwist",
69
+ className: "Serwist",
70
+ funcName: "registerRoute",
71
+ paramName: "route.method",
72
+ });
73
+ }
74
+
75
+ if (!state.routes.has(route.method)) {
76
+ state.routes.set(route.method, []);
77
+ }
78
+
79
+ // Give precedence to all of the earlier routes by adding this additional
80
+ // route to the end of the array.
81
+ state.routes.get(route.method)!.push(route);
82
+ };
83
+
84
+ /**
85
+ * Unregisters a route from the router.
86
+ *
87
+ * @param route The {@linkcode Route} object to unregister.
88
+ */
89
+ export const unregisterRoute = (state: Serwist, route: Route): void => {
90
+ if (!state.routes.has(route.method)) {
91
+ throw new SerwistError("unregister-route-but-not-found-with-method", {
92
+ method: route.method,
93
+ });
94
+ }
95
+
96
+ const routeIndex = state.routes.get(route.method)!.indexOf(route);
97
+ if (routeIndex > -1) {
98
+ state.routes.get(route.method)!.splice(routeIndex, 1);
99
+ } else {
100
+ throw new SerwistError("unregister-route-route-not-registered");
101
+ }
102
+ };
103
+
104
+ /**
105
+ * Applies the routing rules to a `FetchEvent` object to get a response from an
106
+ * appropriate route.
107
+ *
108
+ * @param options
109
+ * @returns A promise is returned if a registered route can handle the request.
110
+ * If there is no matching route and there's no default handler, `undefined`
111
+ * is returned.
112
+ */
113
+ export const handleRequest = (
114
+ state: Serwist,
115
+ {
116
+ request,
117
+ event,
118
+ }: {
119
+ /**
120
+ * The request to handle.
121
+ */
122
+ request: Request;
123
+ /**
124
+ * The event that triggered the request.
125
+ */
126
+ event: ExtendableEvent;
127
+ },
128
+ ): Promise<Response> | undefined => {
129
+ if (process.env.NODE_ENV !== "production") {
130
+ assert!.isInstance(request, Request, {
131
+ moduleName: "serwist",
132
+ className: "Serwist",
133
+ funcName: "handleRequest",
134
+ paramName: "options.request",
135
+ });
136
+ }
137
+
138
+ const url = new URL(request.url, location.href);
139
+ if (!url.protocol.startsWith("http")) {
140
+ if (process.env.NODE_ENV !== "production") {
141
+ logger.debug("Router only supports URLs that start with 'http'.");
142
+ }
143
+ return;
144
+ }
145
+
146
+ const sameOrigin = url.origin === location.origin;
147
+ const { params, route } = findMatchingRoute(state, {
148
+ event,
149
+ request,
150
+ sameOrigin,
151
+ url,
152
+ });
153
+ let handler = route?.handler;
154
+
155
+ const debugMessages = [];
156
+ if (process.env.NODE_ENV !== "production") {
157
+ if (handler) {
158
+ debugMessages.push(["Found a route to handle this request:", route]);
159
+
160
+ if (params) {
161
+ debugMessages.push([`Passing the following params to the route's handler:`, params]);
162
+ }
163
+ }
164
+ }
165
+
166
+ // If we don't have a handler because there was no matching route, then
167
+ // fall back to defaultHandler if that's defined.
168
+ const method = request.method as HTTPMethod;
169
+ if (!handler && state.defaultHandlerMap.has(method)) {
170
+ if (process.env.NODE_ENV !== "production") {
171
+ debugMessages.push(`Failed to find a matching route. Falling back to the default handler for ${method}.`);
172
+ }
173
+ handler = state.defaultHandlerMap.get(method);
174
+ }
175
+
176
+ if (!handler) {
177
+ if (process.env.NODE_ENV !== "production") {
178
+ // No handler so Serwist will do nothing. If logs is set of debug
179
+ // i.e. verbose, we should print out this information.
180
+ logger.debug(`No route found for: ${getFriendlyURL(url)}`);
181
+ }
182
+ return;
183
+ }
184
+
185
+ if (process.env.NODE_ENV !== "production") {
186
+ // We have a handler, meaning Serwist is going to handle the route.
187
+ // print the routing details to the console.
188
+ logger.groupCollapsed(`Router is responding to: ${getFriendlyURL(url)}`);
189
+
190
+ for (const msg of debugMessages) {
191
+ if (Array.isArray(msg)) {
192
+ logger.log(...msg);
193
+ } else {
194
+ logger.log(msg);
195
+ }
196
+ }
197
+
198
+ logger.groupEnd();
199
+ }
200
+
201
+ // Wrap in try and catch in case the handle method throws a synchronous
202
+ // error. It should still callback to the catch handler.
203
+ let responsePromise: Promise<Response>;
204
+ try {
205
+ responsePromise = handler.handle({ url, request, event, params });
206
+ } catch (err) {
207
+ responsePromise = Promise.reject(err);
208
+ }
209
+
210
+ // Get route's catch handler, if it exists
211
+ const catchHandler = route?.catchHandler;
212
+
213
+ if (responsePromise instanceof Promise && (state.catchHandler || catchHandler)) {
214
+ responsePromise = responsePromise.catch(async (err) => {
215
+ // If there's a route catch handler, process that first
216
+ if (catchHandler) {
217
+ if (process.env.NODE_ENV !== "production") {
218
+ // Still include URL here as it will be async from the console group
219
+ // and may not make sense without the URL
220
+ logger.groupCollapsed(`Error thrown when responding to: ${getFriendlyURL(url)}. Falling back to route's Catch Handler.`);
221
+ logger.error("Error thrown by:", route);
222
+ logger.error(err);
223
+ logger.groupEnd();
224
+ }
225
+
226
+ try {
227
+ return await catchHandler.handle({ url, request, event, params });
228
+ } catch (catchErr) {
229
+ if (catchErr instanceof Error) {
230
+ err = catchErr;
231
+ }
232
+ }
233
+ }
234
+
235
+ if (state.catchHandler) {
236
+ if (process.env.NODE_ENV !== "production") {
237
+ // Still include URL here as it will be async from the console group
238
+ // and may not make sense without the URL
239
+ logger.groupCollapsed(`Error thrown when responding to: ${getFriendlyURL(url)}. Falling back to global Catch Handler.`);
240
+ logger.error("Error thrown by:", route);
241
+ logger.error(err);
242
+ logger.groupEnd();
243
+ }
244
+ return state.catchHandler.handle({ url, request, event });
245
+ }
246
+
247
+ throw err;
248
+ });
249
+ }
250
+
251
+ return responsePromise;
252
+ };
253
+
254
+ /**
255
+ * Checks a request and URL (and optionally an event) against the list of
256
+ * registered routes, and if there's a match, returns the corresponding
257
+ * route along with any params generated by the match.
258
+ *
259
+ * @param options
260
+ * @returns An object with `route` and `params` properties. They are populated
261
+ * if a matching route was found or `undefined` otherwise.
262
+ */
263
+ export const findMatchingRoute = (
264
+ state: Serwist,
265
+ { url, sameOrigin, request, event }: RouteMatchCallbackOptions,
266
+ ): {
267
+ route?: Route;
268
+ params?: RouteHandlerCallbackOptions["params"];
269
+ } => {
270
+ const routes = state.routes.get(request.method as HTTPMethod) || [];
271
+ for (const route of routes) {
272
+ let params: Promise<any> | undefined;
273
+ // route.match returns type any, not possible to change right now.
274
+ const matchResult = route.match({ url, sameOrigin, request, event });
275
+ if (matchResult) {
276
+ if (process.env.NODE_ENV !== "production") {
277
+ // Warn developers that using an async matchCallback is almost always
278
+ // not the right thing to do.
279
+ if (matchResult instanceof Promise) {
280
+ logger.warn(
281
+ `While routing ${getFriendlyURL(
282
+ url,
283
+ )}, an async matchCallback function was used. Please convert the following route to use a synchronous matchCallback function:`,
284
+ route,
285
+ );
286
+ }
287
+ }
288
+
289
+ // See https://github.com/GoogleChrome/workbox/issues/2079
290
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
291
+ params = matchResult;
292
+ if (Array.isArray(params) && params.length === 0) {
293
+ // Instead of passing an empty array in as params, use undefined.
294
+ params = undefined;
295
+ } else if (
296
+ matchResult.constructor === Object && // eslint-disable-line
297
+ Object.keys(matchResult).length === 0
298
+ ) {
299
+ // Instead of passing an empty object in as params, use undefined.
300
+ params = undefined;
301
+ } else if (typeof matchResult === "boolean") {
302
+ // For the boolean value true (rather than just something truth-y),
303
+ // don't set params.
304
+ // See https://github.com/GoogleChrome/workbox/pull/2134#issuecomment-513924353
305
+ params = undefined;
306
+ }
307
+
308
+ // Return early if have a match.
309
+ return { route, params };
310
+ }
311
+ }
312
+ // If no match was found above, return and empty object.
313
+ return {};
314
+ };
@@ -6,7 +6,7 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import type { StrategyPlugin } from "../../types.js";
9
+ import type { StrategyPlugin } from "#lib/types.js";
10
10
  import { createPartialResponse } from "./createPartialResponse.js";
11
11
 
12
12
  /**
@@ -0,0 +1,234 @@
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
+ import { assert } from "#utils/assert.js";
9
+ import { logger } from "#utils/logger.js";
10
+ import { normalizeHandler } from "#utils/normalizeHandler.js";
11
+ import type { HTTPMethod } from "./constants.js";
12
+ import { defaultMethod, validMethods } from "./constants.js";
13
+ import type { RouteHandler, RouteHandlerObject, RouteMatchCallback, RouteMatchCallbackOptions } from "./types.js";
14
+
15
+ /**
16
+ * A `Route` consists of a pair of callback functions, `match` and `handler`.
17
+ * The `match` callback determines if a route should be used to handle a
18
+ * request by returning a truthy value if it can. The `handler` callback
19
+ * is called when the route matches and should return a promise that resolves
20
+ * to a response.
21
+ */
22
+ export class Route {
23
+ handler: RouteHandlerObject;
24
+ match: RouteMatchCallback;
25
+ method: HTTPMethod;
26
+ catchHandler?: RouteHandlerObject;
27
+
28
+ /**
29
+ * Constructor for Route class.
30
+ *
31
+ * @param match A callback function that determines whether the
32
+ * route matches a given `fetch` event by returning a truthy value.
33
+ * @param handler A callback function that returns a `Promise` resolving
34
+ * to a `Response`.
35
+ * @param method The HTTP method to match the route against. Defaults
36
+ * to `GET`.
37
+ */
38
+ constructor(match: RouteMatchCallback, handler: RouteHandler, method: HTTPMethod = defaultMethod) {
39
+ if (process.env.NODE_ENV !== "production") {
40
+ assert!.isType(match, "function", {
41
+ moduleName: "serwist",
42
+ className: "Route",
43
+ funcName: "constructor",
44
+ paramName: "match",
45
+ });
46
+
47
+ if (method) {
48
+ assert!.isOneOf(method, validMethods, { paramName: "method" });
49
+ }
50
+ }
51
+
52
+ // These values are referenced directly by Router so cannot be
53
+ // altered by minificaton.
54
+ this.handler = normalizeHandler(handler);
55
+ this.match = match;
56
+ this.method = method;
57
+ }
58
+
59
+ /**
60
+ *
61
+ * @param handler A callback function that returns a Promise resolving
62
+ * to a Response.
63
+ */
64
+ setCatchHandler(handler: RouteHandler): void {
65
+ this.catchHandler = normalizeHandler(handler);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * A class that makes it easy to create a {@linkcode Route} object with a regular expression.
71
+ *
72
+ * For same-origin requests the `RegExp` only needs to match part of the URL. For
73
+ * requests against third-party servers, you must define a `RegExp` that matches
74
+ * the start of the URL.
75
+ */
76
+ export class RegExpRoute extends Route {
77
+ /**
78
+ * If the regular expression contains
79
+ * [capture groups](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references),
80
+ * the captured values will be passed to the `params` argument.
81
+ *
82
+ * @param regExp The regular expression to match against URLs.
83
+ * @param handler A callback function that returns a `Promise` resulting in a `Response`.
84
+ * @param method The HTTP method to match the {@linkcode Route} against. Defaults to `GET`.
85
+ * against.
86
+ */
87
+ constructor(regExp: RegExp, handler: RouteHandler, method?: HTTPMethod) {
88
+ if (process.env.NODE_ENV !== "production") {
89
+ assert!.isInstance(regExp, RegExp, {
90
+ moduleName: "serwist",
91
+ className: "RegExpRoute",
92
+ funcName: "constructor",
93
+ paramName: "pattern",
94
+ });
95
+ }
96
+
97
+ const match: RouteMatchCallback = ({ url }: RouteMatchCallbackOptions) => {
98
+ const result = regExp.exec(url.href);
99
+
100
+ // Return immediately if there's no match.
101
+ if (!result) {
102
+ return;
103
+ }
104
+
105
+ // Require that the match start at the first character in the URL string
106
+ // if it's a cross-origin request.
107
+ // See https://github.com/GoogleChrome/workbox/issues/281 for the context
108
+ // behind this behavior.
109
+ if (url.origin !== location.origin && result.index !== 0) {
110
+ if (process.env.NODE_ENV !== "production") {
111
+ logger.debug(
112
+ `The regular expression '${regExp.toString()}' only partially matched against the cross-origin URL '${url.toString()}'. RegExpRoute's will only handle cross-origin requests if they match the entire URL.`,
113
+ );
114
+ }
115
+
116
+ return;
117
+ }
118
+
119
+ // If the route matches, but there aren't any capture groups defined, then
120
+ // this will return [], which is truthy and therefore sufficient to
121
+ // indicate a match.
122
+ // If there are capture groups, then it will return their values.
123
+ return result.slice(1);
124
+ };
125
+
126
+ super(match, handler, method);
127
+ }
128
+ }
129
+
130
+ export interface NavigationRouteMatchOptions {
131
+ /**
132
+ * If any of these patterns
133
+ * match the URL's pathname and search parameter, the route will handle the
134
+ * request (assuming the denylist doesn't match).
135
+ *
136
+ * @default [/./]
137
+ */
138
+ allowlist?: RegExp[];
139
+ /**
140
+ * If any of these patterns match, the route will not handle the request (even if a allowlist RegExp matches).
141
+ */
142
+ denylist?: RegExp[];
143
+ }
144
+
145
+ /**
146
+ * A class that makes it easy to create a {@linkcode Route} object that matches navigation requests.
147
+ *
148
+ * It will only match incoming requests whose [`mode`](https://fetch.spec.whatwg.org/#concept-request-mode) is set to `"navigate"`.
149
+ *
150
+ * You can optionally only apply this route to a subset of navigation requests
151
+ * by using one or both of the `denylist` and `allowlist` parameters.
152
+ */
153
+ export class NavigationRoute extends Route {
154
+ private readonly _allowlist: RegExp[];
155
+ private readonly _denylist: RegExp[];
156
+
157
+ /**
158
+ * If both `denylist` and `allowlist` are provided, `denylist` will
159
+ * take precedence.
160
+ *
161
+ * The regular expressions in `allowlist` and `denylist`
162
+ * are matched against the concatenated
163
+ * [`pathname`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname)
164
+ * and [`search`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search)
165
+ * portions of the requested URL.
166
+ *
167
+ * *Note*: These RegExps may be evaluated against every destination URL during
168
+ * a navigation. Avoid using
169
+ * [complex RegExps](https://github.com/GoogleChrome/workbox/issues/3077),
170
+ * or else your users may see delays when navigating your site.
171
+ *
172
+ * @param handler A callback function that returns a `Promise` resulting in a `Response`.
173
+ * @param options
174
+ */
175
+ constructor(handler: RouteHandler, { allowlist = [/./], denylist = [] }: NavigationRouteMatchOptions = {}) {
176
+ if (process.env.NODE_ENV !== "production") {
177
+ assert!.isArrayOfClass(allowlist, RegExp, {
178
+ moduleName: "serwist",
179
+ className: "NavigationRoute",
180
+ funcName: "constructor",
181
+ paramName: "options.allowlist",
182
+ });
183
+ assert!.isArrayOfClass(denylist, RegExp, {
184
+ moduleName: "serwist",
185
+ className: "NavigationRoute",
186
+ funcName: "constructor",
187
+ paramName: "options.denylist",
188
+ });
189
+ }
190
+
191
+ super((options: RouteMatchCallbackOptions) => this._match(options), handler);
192
+
193
+ this._allowlist = allowlist;
194
+ this._denylist = denylist;
195
+ }
196
+
197
+ /**
198
+ * Routes match handler.
199
+ *
200
+ * @param options
201
+ * @returns
202
+ * @private
203
+ */
204
+ private _match({ url, request }: RouteMatchCallbackOptions): boolean {
205
+ if (request && request.mode !== "navigate") {
206
+ return false;
207
+ }
208
+
209
+ const pathnameAndSearch = url.pathname + url.search;
210
+
211
+ for (const regExp of this._denylist) {
212
+ if (regExp.test(pathnameAndSearch)) {
213
+ if (process.env.NODE_ENV !== "production") {
214
+ logger.log(
215
+ `The navigation route ${pathnameAndSearch} is not being used, since the URL matches this denylist pattern: ${regExp.toString()}`,
216
+ );
217
+ }
218
+ return false;
219
+ }
220
+ }
221
+
222
+ if (this._allowlist.some((regExp) => regExp.test(pathnameAndSearch))) {
223
+ if (process.env.NODE_ENV !== "production") {
224
+ logger.debug(`The navigation route ${pathnameAndSearch} is being used.`);
225
+ }
226
+ return true;
227
+ }
228
+
229
+ if (process.env.NODE_ENV !== "production") {
230
+ logger.log(`The navigation route ${pathnameAndSearch} is not being used, since the URL being navigated to doesn't match the allowlist.`);
231
+ }
232
+ return false;
233
+ }
234
+ }