source: frontend/node_modules/workbox-streams/concatenate.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: 4.8 KB
RevLine 
[9af201e]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 { assert } from 'workbox-core/_private/assert.js';
9import { Deferred } from 'workbox-core/_private/Deferred.js';
10import { logger } from 'workbox-core/_private/logger.js';
11import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
12import './_version.js';
13/**
14 * Takes either a Response, a ReadableStream, or a
15 * [BodyInit](https://fetch.spec.whatwg.org/#bodyinit) and returns the
16 * ReadableStreamReader object associated with it.
17 *
18 * @param {workbox-streams.StreamSource} source
19 * @return {ReadableStreamReader}
20 * @private
21 */
22function _getReaderFromSource(source) {
23 if (source instanceof Response) {
24 // See https://github.com/GoogleChrome/workbox/issues/2998
25 if (source.body) {
26 return source.body.getReader();
27 }
28 throw new WorkboxError('opaque-streams-source', { type: source.type });
29 }
30 if (source instanceof ReadableStream) {
31 return source.getReader();
32 }
33 return new Response(source).body.getReader();
34}
35/**
36 * Takes multiple source Promises, each of which could resolve to a Response, a
37 * ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit).
38 *
39 * Returns an object exposing a ReadableStream with each individual stream's
40 * data returned in sequence, along with a Promise which signals when the
41 * stream is finished (useful for passing to a FetchEvent's waitUntil()).
42 *
43 * @param {Array<Promise<workbox-streams.StreamSource>>} sourcePromises
44 * @return {Object<{done: Promise, stream: ReadableStream}>}
45 *
46 * @memberof workbox-streams
47 */
48function concatenate(sourcePromises) {
49 if (process.env.NODE_ENV !== 'production') {
50 assert.isArray(sourcePromises, {
51 moduleName: 'workbox-streams',
52 funcName: 'concatenate',
53 paramName: 'sourcePromises',
54 });
55 }
56 const readerPromises = sourcePromises.map((sourcePromise) => {
57 return Promise.resolve(sourcePromise).then((source) => {
58 return _getReaderFromSource(source);
59 });
60 });
61 const streamDeferred = new Deferred();
62 let i = 0;
63 const logMessages = [];
64 const stream = new ReadableStream({
65 pull(controller) {
66 return readerPromises[i]
67 .then((reader) => {
68 if (reader instanceof ReadableStreamDefaultReader) {
69 return reader.read();
70 }
71 else {
72 return;
73 }
74 })
75 .then((result) => {
76 if (result === null || result === void 0 ? void 0 : result.done) {
77 if (process.env.NODE_ENV !== 'production') {
78 logMessages.push([
79 'Reached the end of source:',
80 sourcePromises[i],
81 ]);
82 }
83 i++;
84 if (i >= readerPromises.length) {
85 // Log all the messages in the group at once in a single group.
86 if (process.env.NODE_ENV !== 'production') {
87 logger.groupCollapsed(`Concatenating ${readerPromises.length} sources.`);
88 for (const message of logMessages) {
89 if (Array.isArray(message)) {
90 logger.log(...message);
91 }
92 else {
93 logger.log(message);
94 }
95 }
96 logger.log('Finished reading all sources.');
97 logger.groupEnd();
98 }
99 controller.close();
100 streamDeferred.resolve();
101 return;
102 }
103 // The `pull` method is defined because we're inside it.
104 return this.pull(controller);
105 }
106 else {
107 controller.enqueue(result === null || result === void 0 ? void 0 : result.value);
108 }
109 })
110 .catch((error) => {
111 if (process.env.NODE_ENV !== 'production') {
112 logger.error('An error occurred:', error);
113 }
114 streamDeferred.reject(error);
115 throw error;
116 });
117 },
118 cancel() {
119 if (process.env.NODE_ENV !== 'production') {
120 logger.warn('The ReadableStream was cancelled.');
121 }
122 streamDeferred.resolve();
123 },
124 });
125 return { done: streamDeferred.promise, stream };
126}
127export { concatenate };
Note: See TracBrowser for help on using the repository browser.