| 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 | import { logger } from 'workbox-core/_private/logger.js';
|
|---|
| 9 | import { createHeaders } from './utils/createHeaders.js';
|
|---|
| 10 | import { concatenateToResponse } from './concatenateToResponse.js';
|
|---|
| 11 | import { isSupported } from './isSupported.js';
|
|---|
| 12 | import './_version.js';
|
|---|
| 13 | /**
|
|---|
| 14 | * A shortcut to create a strategy that could be dropped-in to Workbox's router.
|
|---|
| 15 | *
|
|---|
| 16 | * On browsers that do not support constructing new `ReadableStream`s, this
|
|---|
| 17 | * strategy will automatically wait for all the `sourceFunctions` to complete,
|
|---|
| 18 | * and create a final response that concatenates their values together.
|
|---|
| 19 | *
|
|---|
| 20 | * @param {Array<function({event, request, url, params})>} sourceFunctions
|
|---|
| 21 | * An array of functions similar to {@link workbox-routing~handlerCallback}
|
|---|
| 22 | * but that instead return a {@link workbox-streams.StreamSource} (or a
|
|---|
| 23 | * Promise which resolves to one).
|
|---|
| 24 | * @param {HeadersInit} [headersInit] If there's no `Content-Type` specified,
|
|---|
| 25 | * `'text/html'` will be used by default.
|
|---|
| 26 | * @return {workbox-routing~handlerCallback}
|
|---|
| 27 | * @memberof workbox-streams
|
|---|
| 28 | */
|
|---|
| 29 | function strategy(sourceFunctions, headersInit) {
|
|---|
| 30 | return async ({ event, request, url, params }) => {
|
|---|
| 31 | const sourcePromises = sourceFunctions.map((fn) => {
|
|---|
| 32 | // Ensure the return value of the function is always a promise.
|
|---|
| 33 | return Promise.resolve(fn({ event, request, url, params }));
|
|---|
| 34 | });
|
|---|
| 35 | if (isSupported()) {
|
|---|
| 36 | const { done, response } = concatenateToResponse(sourcePromises, headersInit);
|
|---|
| 37 | if (event) {
|
|---|
| 38 | event.waitUntil(done);
|
|---|
| 39 | }
|
|---|
| 40 | return response;
|
|---|
| 41 | }
|
|---|
| 42 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 43 | logger.log(`The current browser doesn't support creating response ` +
|
|---|
| 44 | `streams. Falling back to non-streaming response instead.`);
|
|---|
| 45 | }
|
|---|
| 46 | // Fallback to waiting for everything to finish, and concatenating the
|
|---|
| 47 | // responses.
|
|---|
| 48 | const blobPartsPromises = sourcePromises.map(async (sourcePromise) => {
|
|---|
| 49 | const source = await sourcePromise;
|
|---|
| 50 | if (source instanceof Response) {
|
|---|
| 51 | return source.blob();
|
|---|
| 52 | }
|
|---|
| 53 | else {
|
|---|
| 54 | // Technically, a `StreamSource` object can include any valid
|
|---|
| 55 | // `BodyInit` type, including `FormData` and `URLSearchParams`, which
|
|---|
| 56 | // cannot be passed to the Blob constructor directly, so we have to
|
|---|
| 57 | // convert them to actual Blobs first.
|
|---|
| 58 | return new Response(source).blob();
|
|---|
| 59 | }
|
|---|
| 60 | });
|
|---|
| 61 | const blobParts = await Promise.all(blobPartsPromises);
|
|---|
| 62 | const headers = createHeaders(headersInit);
|
|---|
| 63 | // Constructing a new Response from a Blob source is well-supported.
|
|---|
| 64 | // So is constructing a new Blob from multiple source Blobs or strings.
|
|---|
| 65 | return new Response(new Blob(blobParts), { headers });
|
|---|
| 66 | };
|
|---|
| 67 | }
|
|---|
| 68 | export { strategy };
|
|---|