Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2018 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {logger} from 'workbox-core/_private/logger.mjs';
  14. import {assert} from 'workbox-core/_private/assert.mjs';
  15. import './_version.mjs';
  16. /**
  17. * Takes either a Response, a ReadableStream, or a
  18. * [BodyInit](https://fetch.spec.whatwg.org/#bodyinit) and returns the
  19. * ReadableStreamReader object associated with it.
  20. *
  21. * @param {workbox.streams.StreamSource} source
  22. * @return {ReadableStreamReader}
  23. * @private
  24. */
  25. function _getReaderFromSource(source) {
  26. if (source.body && source.body.getReader) {
  27. return source.body.getReader();
  28. }
  29. if (source.getReader) {
  30. return source.getReader();
  31. }
  32. // TODO: This should be possible to do by constructing a ReadableStream, but
  33. // I can't get it to work. As a hack, construct a new Response, and use the
  34. // reader associated with its body.
  35. return new Response(source).body.getReader();
  36. }
  37. /**
  38. * Takes multiple source Promises, each of which could resolve to a Response, a
  39. * ReadableStream, or a [BodyInit](https://fetch.spec.whatwg.org/#bodyinit).
  40. *
  41. * Returns an object exposing a ReadableStream with each individual stream's
  42. * data returned in sequence, along with a Promise which signals when the
  43. * stream is finished (useful for passing to a FetchEvent's waitUntil()).
  44. *
  45. * @param {Array<Promise<workbox.streams.StreamSource>>} sourcePromises
  46. * @return {Object<{done: Promise, stream: ReadableStream}>}
  47. *
  48. * @memberof workbox.streams
  49. */
  50. function concatenate(sourcePromises) {
  51. if (process.env.NODE_ENV !== 'production') {
  52. assert.isArray(sourcePromises, {
  53. moduleName: 'workbox-streams',
  54. funcName: 'concatenate',
  55. paramName: 'sourcePromises',
  56. });
  57. }
  58. const readerPromises = sourcePromises.map((sourcePromise) => {
  59. return Promise.resolve(sourcePromise).then((source) => {
  60. return _getReaderFromSource(source);
  61. });
  62. });
  63. let fullyStreamedResolve;
  64. let fullyStreamedReject;
  65. const done = new Promise((resolve, reject) => {
  66. fullyStreamedResolve = resolve;
  67. fullyStreamedReject = reject;
  68. });
  69. let i = 0;
  70. const logMessages = [];
  71. const stream = new ReadableStream({
  72. pull(controller) {
  73. return readerPromises[i]
  74. .then((reader) => reader.read())
  75. .then((result) => {
  76. if (result.done) {
  77. if (process.env.NODE_ENV !== 'production') {
  78. logMessages.push(['Reached the end of source:',
  79. sourcePromises[i]]);
  80. }
  81. i++;
  82. if (i >= readerPromises.length) {
  83. // Log all the messages in the group at once in a single group.
  84. if (process.env.NODE_ENV !== 'production') {
  85. logger.groupCollapsed(
  86. `Concatenating ${readerPromises.length} sources.`);
  87. for (const message of logMessages) {
  88. if (Array.isArray(message)) {
  89. logger.log(...message);
  90. } else {
  91. logger.log(message);
  92. }
  93. }
  94. logger.log('Finished reading all sources.');
  95. logger.groupEnd();
  96. }
  97. controller.close();
  98. fullyStreamedResolve();
  99. return;
  100. }
  101. return this.pull(controller);
  102. } else {
  103. controller.enqueue(result.value);
  104. }
  105. }).catch((error) => {
  106. if (process.env.NODE_ENV !== 'production') {
  107. logger.error('An error occurred:', error);
  108. }
  109. fullyStreamedReject(error);
  110. throw error;
  111. });
  112. },
  113. cancel() {
  114. if (process.env.NODE_ENV !== 'production') {
  115. logger.warn('The ReadableStream was cancelled.');
  116. }
  117. fullyStreamedResolve();
  118. },
  119. });
  120. return {done, stream};
  121. }
  122. export {concatenate};