11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci(function () {
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci  class RandomPushSource {
51cb0ef41Sopenharmony_ci    constructor(toPush) {
61cb0ef41Sopenharmony_ci      this.pushed = 0;
71cb0ef41Sopenharmony_ci      this.toPush = toPush;
81cb0ef41Sopenharmony_ci      this.started = false;
91cb0ef41Sopenharmony_ci      this.paused = false;
101cb0ef41Sopenharmony_ci      this.closed = false;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci      this._intervalHandle = null;
131cb0ef41Sopenharmony_ci    }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci    readStart() {
161cb0ef41Sopenharmony_ci      if (this.closed) {
171cb0ef41Sopenharmony_ci        return;
181cb0ef41Sopenharmony_ci      }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci      if (!this.started) {
211cb0ef41Sopenharmony_ci        this._intervalHandle = setInterval(writeChunk, 2);
221cb0ef41Sopenharmony_ci        this.started = true;
231cb0ef41Sopenharmony_ci      }
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci      if (this.paused) {
261cb0ef41Sopenharmony_ci        this._intervalHandle = setInterval(writeChunk, 2);
271cb0ef41Sopenharmony_ci        this.paused = false;
281cb0ef41Sopenharmony_ci      }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci      const source = this;
311cb0ef41Sopenharmony_ci      function writeChunk() {
321cb0ef41Sopenharmony_ci        if (source.paused) {
331cb0ef41Sopenharmony_ci          return;
341cb0ef41Sopenharmony_ci        }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci        source.pushed++;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci        if (source.toPush > 0 && source.pushed > source.toPush) {
391cb0ef41Sopenharmony_ci          if (source._intervalHandle) {
401cb0ef41Sopenharmony_ci            clearInterval(source._intervalHandle);
411cb0ef41Sopenharmony_ci            source._intervalHandle = undefined;
421cb0ef41Sopenharmony_ci          }
431cb0ef41Sopenharmony_ci          source.closed = true;
441cb0ef41Sopenharmony_ci          source.onend();
451cb0ef41Sopenharmony_ci        } else {
461cb0ef41Sopenharmony_ci          source.ondata(randomChunk(128));
471cb0ef41Sopenharmony_ci        }
481cb0ef41Sopenharmony_ci      }
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci    readStop() {
521cb0ef41Sopenharmony_ci      if (this.paused) {
531cb0ef41Sopenharmony_ci        return;
541cb0ef41Sopenharmony_ci      }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci      if (this.started) {
571cb0ef41Sopenharmony_ci        this.paused = true;
581cb0ef41Sopenharmony_ci        clearInterval(this._intervalHandle);
591cb0ef41Sopenharmony_ci        this._intervalHandle = undefined;
601cb0ef41Sopenharmony_ci      } else {
611cb0ef41Sopenharmony_ci        throw new Error('Can\'t pause reading an unstarted source.');
621cb0ef41Sopenharmony_ci      }
631cb0ef41Sopenharmony_ci    }
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  function randomChunk(size) {
671cb0ef41Sopenharmony_ci    let chunk = '';
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    for (let i = 0; i < size; ++i) {
701cb0ef41Sopenharmony_ci      // Add a random character from the basic printable ASCII set.
711cb0ef41Sopenharmony_ci      chunk += String.fromCharCode(Math.round(Math.random() * 84) + 32);
721cb0ef41Sopenharmony_ci    }
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    return chunk;
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  function readableStreamToArray(readable, reader) {
781cb0ef41Sopenharmony_ci    if (reader === undefined) {
791cb0ef41Sopenharmony_ci      reader = readable.getReader();
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci    const chunks = [];
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci    return pump();
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    function pump() {
871cb0ef41Sopenharmony_ci      return reader.read().then(result => {
881cb0ef41Sopenharmony_ci        if (result.done) {
891cb0ef41Sopenharmony_ci          return chunks;
901cb0ef41Sopenharmony_ci        }
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci        chunks.push(result.value);
931cb0ef41Sopenharmony_ci        return pump();
941cb0ef41Sopenharmony_ci      });
951cb0ef41Sopenharmony_ci    }
961cb0ef41Sopenharmony_ci  }
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  class SequentialPullSource {
991cb0ef41Sopenharmony_ci    constructor(limit, options) {
1001cb0ef41Sopenharmony_ci      const async = options && options.async;
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci      this.current = 0;
1031cb0ef41Sopenharmony_ci      this.limit = limit;
1041cb0ef41Sopenharmony_ci      this.opened = false;
1051cb0ef41Sopenharmony_ci      this.closed = false;
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci      this._exec = f => f();
1081cb0ef41Sopenharmony_ci      if (async) {
1091cb0ef41Sopenharmony_ci        this._exec = f => step_timeout(f, 0);
1101cb0ef41Sopenharmony_ci      }
1111cb0ef41Sopenharmony_ci    }
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci    open(cb) {
1141cb0ef41Sopenharmony_ci      this._exec(() => {
1151cb0ef41Sopenharmony_ci        this.opened = true;
1161cb0ef41Sopenharmony_ci        cb();
1171cb0ef41Sopenharmony_ci      });
1181cb0ef41Sopenharmony_ci    }
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci    read(cb) {
1211cb0ef41Sopenharmony_ci      this._exec(() => {
1221cb0ef41Sopenharmony_ci        if (++this.current <= this.limit) {
1231cb0ef41Sopenharmony_ci          cb(null, false, this.current);
1241cb0ef41Sopenharmony_ci        } else {
1251cb0ef41Sopenharmony_ci          cb(null, true, null);
1261cb0ef41Sopenharmony_ci        }
1271cb0ef41Sopenharmony_ci      });
1281cb0ef41Sopenharmony_ci    }
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci    close(cb) {
1311cb0ef41Sopenharmony_ci      this._exec(() => {
1321cb0ef41Sopenharmony_ci        this.closed = true;
1331cb0ef41Sopenharmony_ci        cb();
1341cb0ef41Sopenharmony_ci      });
1351cb0ef41Sopenharmony_ci    }
1361cb0ef41Sopenharmony_ci  }
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  function sequentialReadableStream(limit, options) {
1391cb0ef41Sopenharmony_ci    const sequentialSource = new SequentialPullSource(limit, options);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci    const stream = new ReadableStream({
1421cb0ef41Sopenharmony_ci      start() {
1431cb0ef41Sopenharmony_ci        return new Promise((resolve, reject) => {
1441cb0ef41Sopenharmony_ci          sequentialSource.open(err => {
1451cb0ef41Sopenharmony_ci            if (err) {
1461cb0ef41Sopenharmony_ci              reject(err);
1471cb0ef41Sopenharmony_ci            }
1481cb0ef41Sopenharmony_ci            resolve();
1491cb0ef41Sopenharmony_ci          });
1501cb0ef41Sopenharmony_ci        });
1511cb0ef41Sopenharmony_ci      },
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci      pull(c) {
1541cb0ef41Sopenharmony_ci        return new Promise((resolve, reject) => {
1551cb0ef41Sopenharmony_ci          sequentialSource.read((err, done, chunk) => {
1561cb0ef41Sopenharmony_ci            if (err) {
1571cb0ef41Sopenharmony_ci              reject(err);
1581cb0ef41Sopenharmony_ci            } else if (done) {
1591cb0ef41Sopenharmony_ci              sequentialSource.close(err2 => {
1601cb0ef41Sopenharmony_ci                if (err2) {
1611cb0ef41Sopenharmony_ci                  reject(err2);
1621cb0ef41Sopenharmony_ci                }
1631cb0ef41Sopenharmony_ci                c.close();
1641cb0ef41Sopenharmony_ci                resolve();
1651cb0ef41Sopenharmony_ci              });
1661cb0ef41Sopenharmony_ci            } else {
1671cb0ef41Sopenharmony_ci              c.enqueue(chunk);
1681cb0ef41Sopenharmony_ci              resolve();
1691cb0ef41Sopenharmony_ci            }
1701cb0ef41Sopenharmony_ci          });
1711cb0ef41Sopenharmony_ci        });
1721cb0ef41Sopenharmony_ci      }
1731cb0ef41Sopenharmony_ci    });
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci    stream.source = sequentialSource;
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci    return stream;
1781cb0ef41Sopenharmony_ci  }
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci  function transferArrayBufferView(view) {
1811cb0ef41Sopenharmony_ci    const noopByteStream = new ReadableStream({
1821cb0ef41Sopenharmony_ci      type: 'bytes',
1831cb0ef41Sopenharmony_ci      pull(c) {
1841cb0ef41Sopenharmony_ci        c.byobRequest.respond(c.byobRequest.view.byteLength);
1851cb0ef41Sopenharmony_ci        c.close();
1861cb0ef41Sopenharmony_ci      }
1871cb0ef41Sopenharmony_ci    });
1881cb0ef41Sopenharmony_ci    const reader = noopByteStream.getReader({ mode: 'byob' });
1891cb0ef41Sopenharmony_ci    return reader.read(view).then((result) => result.value);
1901cb0ef41Sopenharmony_ci  }
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  self.RandomPushSource = RandomPushSource;
1931cb0ef41Sopenharmony_ci  self.readableStreamToArray = readableStreamToArray;
1941cb0ef41Sopenharmony_ci  self.sequentialReadableStream = sequentialReadableStream;
1951cb0ef41Sopenharmony_ci  self.transferArrayBufferView = transferArrayBufferView;
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci}());
198