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