| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import {createHeaders} from './utils/createHeaders.js';
|
|---|
| 10 | import {concatenate} from './concatenate.js';
|
|---|
| 11 | import {StreamSource} from './_types.js';
|
|---|
| 12 | import './_version.js';
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Takes multiple source Promises, each of which could resolve to a Response, a
|
|---|
| 16 | * ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit),
|
|---|
| 17 | * along with a
|
|---|
| 18 | * [HeadersInit](https://fetch.spec.whatwg.org/#typedefdef-headersinit).
|
|---|
| 19 | *
|
|---|
| 20 | * Returns an object exposing a Response whose body consists of each individual
|
|---|
| 21 | * stream's data returned in sequence, along with a Promise which signals when
|
|---|
| 22 | * the stream is finished (useful for passing to a FetchEvent's waitUntil()).
|
|---|
| 23 | *
|
|---|
| 24 | * @param {Array<Promise<workbox-streams.StreamSource>>} sourcePromises
|
|---|
| 25 | * @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
|
|---|
| 26 | * `'text/html'` will be used by default.
|
|---|
| 27 | * @return {Object<{done: Promise, response: Response}>}
|
|---|
| 28 | *
|
|---|
| 29 | * @memberof workbox-streams
|
|---|
| 30 | */
|
|---|
| 31 | function concatenateToResponse(
|
|---|
| 32 | sourcePromises: Promise<StreamSource>[],
|
|---|
| 33 | headersInit: HeadersInit,
|
|---|
| 34 | ): {done: Promise<void>; response: Response} {
|
|---|
| 35 | const {done, stream} = concatenate(sourcePromises);
|
|---|
| 36 |
|
|---|
| 37 | const headers = createHeaders(headersInit);
|
|---|
| 38 | const response = new Response(stream, {headers});
|
|---|
| 39 |
|
|---|
| 40 | return {done, response};
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | export {concatenateToResponse};
|
|---|