StrategyHandler.d.ts 7.9 KB

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