source: frontend/node_modules/workbox-streams/strategy.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
Line 
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*/
8import { logger } from 'workbox-core/_private/logger.js';
9import { createHeaders } from './utils/createHeaders.js';
10import { concatenateToResponse } from './concatenateToResponse.js';
11import { isSupported } from './isSupported.js';
12import './_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 */
29function 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}
68export { strategy };
Note: See TracBrowser for help on using the repository browser.