StrategyHandler.d.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { HandlerCallbackOptions, WorkboxPlugin, WorkboxPluginCallbackParam } from 'workbox-core/types.js';
  2. import { Strategy } from './Strategy.js';
  3. import './_version.js';
  4. /**
  5. * A class created every time a Strategy instance instance calls
  6. * {@link workbox-strategies.Strategy~handle} or
  7. * {@link workbox-strategies.Strategy~handleAll} that wraps all fetch and
  8. * cache actions around plugin callbacks and keeps track of when the strategy
  9. * is "done" (i.e. all added `event.waitUntil()` promises have resolved).
  10. *
  11. * @memberof workbox-strategies
  12. */
  13. declare class StrategyHandler {
  14. request: Request;
  15. url?: URL;
  16. event: ExtendableEvent;
  17. params?: any;
  18. private _cacheKeys;
  19. private readonly _strategy;
  20. private readonly _extendLifetimePromises;
  21. private readonly _handlerDeferred;
  22. private readonly _plugins;
  23. private readonly _pluginStateMap;
  24. /**
  25. * Creates a new instance associated with the passed strategy and event
  26. * that's handling the request.
  27. *
  28. * The constructor also initializes the state that will be passed to each of
  29. * the plugins handling this request.
  30. *
  31. * @param {workbox-strategies.Strategy} strategy
  32. * @param {Object} options
  33. * @param {Request|string} options.request A request to run this strategy for.
  34. * @param {ExtendableEvent} options.event The event associated with the
  35. * request.
  36. * @param {URL} [options.url]
  37. * @param {*} [options.params] The return value from the
  38. * {@link workbox-routing~matchCallback} (if applicable).
  39. */
  40. constructor(strategy: Strategy, options: HandlerCallbackOptions);
  41. /**
  42. * Fetches a given request (and invokes any applicable plugin callback
  43. * methods) using the `fetchOptions` (for non-navigation requests) and
  44. * `plugins` defined on the `Strategy` object.
  45. *
  46. * The following plugin lifecycle methods are invoked when using this method:
  47. * - `requestWillFetch()`
  48. * - `fetchDidSucceed()`
  49. * - `fetchDidFail()`
  50. *
  51. * @param {Request|string} input The URL or request to fetch.
  52. * @return {Promise<Response>}
  53. */
  54. fetch(input: RequestInfo): Promise<Response>;
  55. /**
  56. * Calls `this.fetch()` and (in the background) runs `this.cachePut()` on
  57. * the response generated by `this.fetch()`.
  58. *
  59. * The call to `this.cachePut()` automatically invokes `this.waitUntil()`,
  60. * so you do not have to manually call `waitUntil()` on the event.
  61. *
  62. * @param {Request|string} input The request or URL to fetch and cache.
  63. * @return {Promise<Response>}
  64. */
  65. fetchAndCachePut(input: RequestInfo): Promise<Response>;
  66. /**
  67. * Matches a request from the cache (and invokes any applicable plugin
  68. * callback methods) using the `cacheName`, `matchOptions`, and `plugins`
  69. * defined on the strategy object.
  70. *
  71. * The following plugin lifecycle methods are invoked when using this method:
  72. * - cacheKeyWillByUsed()
  73. * - cachedResponseWillByUsed()
  74. *
  75. * @param {Request|string} key The Request or URL to use as the cache key.
  76. * @return {Promise<Response|undefined>} A matching response, if found.
  77. */
  78. cacheMatch(key: RequestInfo): Promise<Response | undefined>;
  79. /**
  80. * Puts a request/response pair in the cache (and invokes any applicable
  81. * plugin callback methods) using the `cacheName` and `plugins` defined on
  82. * the strategy object.
  83. *
  84. * The following plugin lifecycle methods are invoked when using this method:
  85. * - cacheKeyWillByUsed()
  86. * - cacheWillUpdate()
  87. * - cacheDidUpdate()
  88. *
  89. * @param {Request|string} key The request or URL to use as the cache key.
  90. * @param {Response} response The response to cache.
  91. * @return {Promise<boolean>} `false` if a cacheWillUpdate caused the response
  92. * not be cached, and `true` otherwise.
  93. */
  94. cachePut(key: RequestInfo, response: Response): Promise<boolean>;
  95. /**
  96. * Checks the list of plugins for the `cacheKeyWillBeUsed` callback, and
  97. * executes any of those callbacks found in sequence. The final `Request`
  98. * object returned by the last plugin is treated as the cache key for cache
  99. * reads and/or writes. If no `cacheKeyWillBeUsed` plugin callbacks have
  100. * been registered, the passed request is returned unmodified
  101. *
  102. * @param {Request} request
  103. * @param {string} mode
  104. * @return {Promise<Request>}
  105. */
  106. getCacheKey(request: Request, mode: 'read' | 'write'): Promise<Request>;
  107. /**
  108. * Returns true if the strategy has at least one plugin with the given
  109. * callback.
  110. *
  111. * @param {string} name The name of the callback to check for.
  112. * @return {boolean}
  113. */
  114. hasCallback<C extends keyof WorkboxPlugin>(name: C): boolean;
  115. /**
  116. * Runs all plugin callbacks matching the given name, in order, passing the
  117. * given param object (merged ith the current plugin state) as the only
  118. * argument.
  119. *
  120. * Note: since this method runs all plugins, it's not suitable for cases
  121. * where the return value of a callback needs to be applied prior to calling
  122. * the next callback. See
  123. * {@link workbox-strategies.StrategyHandler#iterateCallbacks}
  124. * below for how to handle that case.
  125. *
  126. * @param {string} name The name of the callback to run within each plugin.
  127. * @param {Object} param The object to pass as the first (and only) param
  128. * when executing each callback. This object will be merged with the
  129. * current plugin state prior to callback execution.
  130. */
  131. runCallbacks<C extends keyof NonNullable<WorkboxPlugin>>(name: C, param: Omit<WorkboxPluginCallbackParam[C], 'state'>): Promise<void>;
  132. /**
  133. * Accepts a callback and returns an iterable of matching plugin callbacks,
  134. * where each callback is wrapped with the current handler state (i.e. when
  135. * you call each callback, whatever object parameter you pass it will
  136. * be merged with the plugin's current state).
  137. *
  138. * @param {string} name The name fo the callback to run
  139. * @return {Array<Function>}
  140. */
  141. iterateCallbacks<C extends keyof WorkboxPlugin>(name: C): Generator<NonNullable<WorkboxPlugin[C]>>;
  142. /**
  143. * Adds a promise to the
  144. * [extend lifetime promises]{@link https://w3c.github.io/ServiceWorker/#extendableevent-extend-lifetime-promises}
  145. * of the event event associated with the request being handled (usually a
  146. * `FetchEvent`).
  147. *
  148. * Note: you can await
  149. * {@link workbox-strategies.StrategyHandler~doneWaiting}
  150. * to know when all added promises have settled.
  151. *
  152. * @param {Promise} promise A promise to add to the extend lifetime promises
  153. * of the event that triggered the request.
  154. */
  155. waitUntil<T>(promise: Promise<T>): Promise<T>;
  156. /**
  157. * Returns a promise that resolves once all promises passed to
  158. * {@link workbox-strategies.StrategyHandler~waitUntil}
  159. * have settled.
  160. *
  161. * Note: any work done after `doneWaiting()` settles should be manually
  162. * passed to an event's `waitUntil()` method (not this handler's
  163. * `waitUntil()` method), otherwise the service worker thread my be killed
  164. * prior to your work completing.
  165. */
  166. doneWaiting(): Promise<void>;
  167. /**
  168. * Stops running the strategy and immediately resolves any pending
  169. * `waitUntil()` promises.
  170. */
  171. destroy(): void;
  172. /**
  173. * This method will call cacheWillUpdate on the available plugins (or use
  174. * status === 200) to determine if the Response is safe and valid to cache.
  175. *
  176. * @param {Request} options.request
  177. * @param {Response} options.response
  178. * @return {Promise<Response|undefined>}
  179. *
  180. * @private
  181. */
  182. _ensureResponseSafeToCache(response: Response): Promise<Response | undefined>;
  183. }
  184. export { StrategyHandler };