strategy.mjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright 2018 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import {logger} from 'workbox-core/_private/logger.mjs';
  8. import {createHeaders} from './utils/createHeaders.mjs';
  9. import {concatenateToResponse} from './concatenateToResponse.mjs';
  10. import {isSupported} from './isSupported.mjs';
  11. import './_version.mjs';
  12. /**
  13. * A shortcut to create a strategy that could be dropped-in to Workbox's router.
  14. *
  15. * On browsers that do not support constructing new `ReadableStream`s, this
  16. * strategy will automatically wait for all the `sourceFunctions` to complete,
  17. * and create a final response that concatenates their values together.
  18. *
  19. * @param {
  20. * Array<function(workbox.routing.Route~handlerCallback)>} sourceFunctions
  21. * Each function should return a {@link workbox.streams.StreamSource} (or a
  22. * Promise which resolves to one).
  23. * @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
  24. * `'text/html'` will be used by default.
  25. * @return {workbox.routing.Route~handlerCallback}
  26. *
  27. * @memberof workbox.streams
  28. */
  29. export function strategy(sourceFunctions, headersInit) {
  30. return async ({event, url, params}) => {
  31. if (isSupported()) {
  32. const {done, response} = concatenateToResponse(sourceFunctions.map(
  33. (fn) => fn({event, url, params})), headersInit);
  34. event.waitUntil(done);
  35. return response;
  36. }
  37. if (process.env.NODE_ENV !== 'production') {
  38. logger.log(`The current browser doesn't support creating response ` +
  39. `streams. Falling back to non-streaming response instead.`);
  40. }
  41. // Fallback to waiting for everything to finish, and concatenating the
  42. // responses.
  43. const parts = await Promise.all(
  44. sourceFunctions.map(
  45. (sourceFunction) => sourceFunction({event, url, params})
  46. ).map(async (responsePromise) => {
  47. const response = await responsePromise;
  48. if (response instanceof Response) {
  49. return response.blob();
  50. }
  51. // Otherwise, assume it's something like a string which can be used
  52. // as-is when constructing the final composite blob.
  53. return response;
  54. })
  55. );
  56. const headers = createHeaders(headersInit);
  57. // Constructing a new Response from a Blob source is well-supported.
  58. // So is constructing a new Blob from multiple source Blobs or strings.
  59. return new Response(new Blob(parts), {headers});
  60. };
  61. }