concatenateToResponse.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  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 { createHeaders } from './utils/createHeaders.js';
  8. import { concatenate } from './concatenate.js';
  9. import './_version.js';
  10. /**
  11. * Takes multiple source Promises, each of which could resolve to a Response, a
  12. * ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit),
  13. * along with a
  14. * [HeadersInit](https://fetch.spec.whatwg.org/#typedefdef-headersinit).
  15. *
  16. * Returns an object exposing a Response whose body consists of each individual
  17. * stream's data returned in sequence, along with a Promise which signals when
  18. * the stream is finished (useful for passing to a FetchEvent's waitUntil()).
  19. *
  20. * @param {Array<Promise<module:workbox-streams.StreamSource>>} sourcePromises
  21. * @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
  22. * `'text/html'` will be used by default.
  23. * @return {Object<{done: Promise, response: Response}>}
  24. *
  25. * @memberof module:workbox-streams
  26. */
  27. function concatenateToResponse(sourcePromises, headersInit) {
  28. const { done, stream } = concatenate(sourcePromises);
  29. const headers = createHeaders(headersInit);
  30. const response = new Response(stream, { headers });
  31. return { done, response };
  32. }
  33. export { concatenateToResponse };