strategy.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.js';
  8. import { createHeaders } from './utils/createHeaders.js';
  9. import { concatenateToResponse } from './concatenateToResponse.js';
  10. import { isSupported } from './isSupported.js';
  11. import './_version.js';
  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 {Array<function({event, request, url, params})>} sourceFunctions
  20. * An array of functions similar to {@link module:workbox-routing~handlerCallback}
  21. * but that instead return a {@link module: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 {module:workbox-routing~handlerCallback}
  26. * @memberof module:workbox-streams
  27. */
  28. function strategy(sourceFunctions, headersInit) {
  29. return async ({ event, request, url, params }) => {
  30. const sourcePromises = sourceFunctions.map((fn) => {
  31. // Ensure the return value of the function is always a promise.
  32. return Promise.resolve(fn({ event, request, url, params }));
  33. });
  34. if (isSupported()) {
  35. const { done, response } = concatenateToResponse(sourcePromises, headersInit);
  36. if (event) {
  37. event.waitUntil(done);
  38. }
  39. return response;
  40. }
  41. if (process.env.NODE_ENV !== 'production') {
  42. logger.log(`The current browser doesn't support creating response ` +
  43. `streams. Falling back to non-streaming response instead.`);
  44. }
  45. // Fallback to waiting for everything to finish, and concatenating the
  46. // responses.
  47. const blobPartsPromises = sourcePromises.map(async (sourcePromise) => {
  48. const source = await sourcePromise;
  49. if (source instanceof Response) {
  50. return source.blob();
  51. }
  52. else {
  53. // Technically, a `StreamSource` object can include any valid
  54. // `BodyInit` type, including `FormData` and `URLSearchParams`, which
  55. // cannot be passed to the Blob constructor directly, so we have to
  56. // convert them to actual Blobs first.
  57. return new Response(source).blob();
  58. }
  59. });
  60. const blobParts = await Promise.all(blobPartsPromises);
  61. const headers = createHeaders(headersInit);
  62. // Constructing a new Response from a Blob source is well-supported.
  63. // So is constructing a new Blob from multiple source Blobs or strings.
  64. return new Response(new Blob(blobParts), { headers });
  65. };
  66. }
  67. export { strategy };