text: (function (global, factory) {
      typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
      typeof define === 'function' && define.amd ? define(['exports'], factory) :
      (global = global || self, factory(global.streamingIterables = {}));
  }(this, function (exports) { 'use strict';
  
      /*! *****************************************************************************
      Copyright (c) Microsoft Corporation. All rights reserved.
      Licensed under the Apache License, Version 2.0 (the "License"); you may not use
      this file except in compliance with the License. You may obtain a copy of the
      License at http://www.apache.org/licenses/LICENSE-2.0
  
      THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
      KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
      WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
      MERCHANTABLITY OR NON-INFRINGEMENT.
  
      See the Apache Version 2.0 License for specific language governing permissions
      and limitations under the License.
      ***************************************************************************** */
  
      function __values(o) {
          var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
          if (m) return m.call(o);
          return {
              next: function () {
                  if (o && i >= o.length) o = void 0;
                  return { value: o && o[i++], done: !o };
              }
          };
      }
  
      function __await(v) {
          return this instanceof __await ? (this.v = v, this) : new __await(v);
      }
  
      function __asyncGenerator(thisArg, _arguments, generator) {
          if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
          var g = generator.apply(thisArg, _arguments || []), i, q = [];
          return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
          function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
          function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
          function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
          function fulfill(value) { resume("next", value); }
          function reject(value) { resume("throw", value); }
          function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
      }
  
      function __asyncDelegator(o) {
          var i, p;
          return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
          function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
      }
  
      function __asyncValues(o) {
          if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
          var m = o[Symbol.asyncIterator], i;
          return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
          function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
          function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
      }
  
      function _batch(size, iterable) {
          return __asyncGenerator(this, arguments, function* _batch_1() {
              var e_1, _a;
              let dataBatch = [];
              try {
                  for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
                      const data = iterable_1_1.value;
                      dataBatch.push(data);
                      if (dataBatch.length === size) {
                          yield yield __await(dataBatch);
                          dataBatch = [];
                      }
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
                  }
                  finally { if (e_1) throw e_1.error; }
              }
              if (dataBatch.length > 0) {
                  yield yield __await(dataBatch);
              }
          });
      }
      function* _syncBatch(size, iterable) {
          let dataBatch = [];
          for (const data of iterable) {
              dataBatch.push(data);
              if (dataBatch.length === size) {
                  yield dataBatch;
                  dataBatch = [];
              }
          }
          if (dataBatch.length > 0) {
              yield dataBatch;
          }
      }
      function batch(size, iterable) {
          if (iterable === undefined) {
              return curriedIterable => batch(size, curriedIterable);
          }
          if (iterable[Symbol.asyncIterator]) {
              return _batch(size, iterable);
          }
          return _syncBatch(size, iterable);
      }
  
      function getIterator(iterable) {
          if (typeof iterable.next === 'function') {
              return iterable;
          }
          if (typeof iterable[Symbol.iterator] === 'function') {
              return iterable[Symbol.iterator]();
          }
          if (typeof iterable[Symbol.asyncIterator] === 'function') {
              return iterable[Symbol.asyncIterator]();
          }
          throw new TypeError('"values" does not to conform to any of the iterator or iterable protocols');
      }
  
      function defer() {
          let reject;
          let resolve;
          const promise = new Promise((resolveFunc, rejectFunc) => {
              resolve = resolveFunc;
              reject = rejectFunc;
          });
          return {
              promise,
              reject,
              resolve,
          };
      }
  
      /// <reference lib="esnext.asynciterable" />
      function _buffer(size, iterable) {
          const iterator = getIterator(iterable);
          const resultQueue = [];
          const readQueue = [];
          let reading = false;
          let ended = false;
          function fulfillReadQueue() {
              while (readQueue.length > 0 && resultQueue.length > 0) {
                  const readDeferred = readQueue.shift();
                  const { error, value } = resultQueue.shift();
                  if (error) {
                      readDeferred.reject(error);
                  }
                  else {
                      readDeferred.resolve({ done: false, value });
                  }
              }
              while (readQueue.length > 0 && ended) {
                  const { resolve } = readQueue.shift();
                  resolve({ done: true, value: undefined });
              }
          }
          async function fillQueue() {
              if (ended) {
                  return;
              }
              if (reading) {
                  return;
              }
              if (resultQueue.length >= size) {
                  return;
              }
              reading = true;
              try {
                  const { done, value } = await iterator.next();
                  if (done) {
                      ended = true;
                  }
                  else {
                      resultQueue.push({ value });
                  }
              }
              catch (error) {
                  ended = true;
                  resultQueue.push({ error });
              }
              fulfillReadQueue();
              reading = false;
              fillQueue();
          }
          async function next() {
              if (resultQueue.length > 0) {
                  const { error, value } = resultQueue.shift();
                  if (error) {
                      throw error;
                  }
                  fillQueue();
                  return { done: false, value };
              }
              if (ended) {
                  return { done: true, value: undefined }; // stupid ts
              }
              const deferred = defer();
              readQueue.push(deferred);
              fillQueue();
              return deferred.promise;
          }
          const asyncIterableIterator = {
              next,
              [Symbol.asyncIterator]: () => asyncIterableIterator,
          };
          return asyncIterableIterator;
      }
      function* syncBuffer(size, iterable) {
          const valueQueue = [];
          let e;
          try {
              for (const value of iterable) {
                  valueQueue.push(value);
                  if (valueQueue.length <= size) {
                      continue;
                  }
                  yield valueQueue.shift();
              }
          }
          catch (error) {
              e = error;
          }
          for (const value of valueQueue) {
              yield value;
          }
          if (e) {
              throw e;
          }
      }
      function buffer(size, iterable) {
          if (iterable === undefined) {
              return curriedIterable => buffer(size, curriedIterable);
          }
          if (iterable[Symbol.asyncIterator]) {
              return _buffer(size, iterable);
          }
          return syncBuffer(size, iterable);
      }
  
      async function _collect(iterable) {
          var e_1, _a;
          const values = [];
          try {
              for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = await iterable_1.next(), !iterable_1_1.done;) {
                  const value = iterable_1_1.value;
                  values.push(value);
              }
          }
          catch (e_1_1) { e_1 = { error: e_1_1 }; }
          finally {
              try {
                  if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) await _a.call(iterable_1);
              }
              finally { if (e_1) throw e_1.error; }
          }
          return values;
      }
      function collect(iterable) {
          if (iterable[Symbol.asyncIterator]) {
              return _collect(iterable);
          }
          return Array.from(iterable);
      }
  
      function _concat(iterables) {
          return __asyncGenerator(this, arguments, function* _concat_1() {
              var e_1, _a;
              try {
                  for (var iterables_1 = __asyncValues(iterables), iterables_1_1; iterables_1_1 = yield __await(iterables_1.next()), !iterables_1_1.done;) {
                      const iterable = iterables_1_1.value;
                      yield __await(yield* __asyncDelegator(__asyncValues(iterable)));
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (iterables_1_1 && !iterables_1_1.done && (_a = iterables_1.return)) yield __await(_a.call(iterables_1));
                  }
                  finally { if (e_1) throw e_1.error; }
              }
          });
      }
      function* _syncConcat(iterables) {
          for (const iterable of iterables) {
              yield* iterable;
          }
      }
      function concat(...iterables) {
          const hasAnyAsync = iterables.find(itr => itr[Symbol.asyncIterator] !== undefined);
          if (hasAnyAsync) {
              return _concat(iterables);
          }
          else {
              return _syncConcat(iterables);
          }
      }
  
      async function _consume(iterable) {
          var e_1, _a;
          try {
              for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = await iterable_1.next(), !iterable_1_1.done;) {
                  const val = iterable_1_1.value;
              }
          }
          catch (e_1_1) { e_1 = { error: e_1_1 }; }
          finally {
              try {
                  if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) await _a.call(iterable_1);
              }
              finally { if (e_1) throw e_1.error; }
          }
      }
      function consume(iterable) {
          if (iterable[Symbol.asyncIterator]) {
              return _consume(iterable);
          }
      }
  
      function _filter(filterFunc, iterable) {
          return __asyncGenerator(this, arguments, function* _filter_1() {
              var e_1, _a;
              try {
                  for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
                      const data = iterable_1_1.value;
                      if (yield __await(filterFunc(data))) {
                          yield yield __await(data);
                      }
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
                  }
                  finally { if (e_1) throw e_1.error; }
              }
          });
      }
      function filter(filterFunc, iterable) {
          if (iterable === undefined) {
              return (curriedIterable) => _filter(filterFunc, curriedIterable);
          }
          return _filter(filterFunc, iterable);
      }
  
      function flatten(iterable) {
          return __asyncGenerator(this, arguments, function* flatten_1() {
              var e_1, _a;
              try {
                  for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
                      const maybeItr = iterable_1_1.value;
                      if (maybeItr && typeof maybeItr !== 'string' && (maybeItr[Symbol.iterator] || maybeItr[Symbol.asyncIterator])) {
                          yield __await(yield* __asyncDelegator(__asyncValues(flatten(maybeItr))));
                      }
                      else {
                          yield yield __await(maybeItr);
                      }
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
                  }
                  finally { if (e_1) throw e_1.error; }
              }
          });
      }
  
      function _map(func, iterable) {
          return __asyncGenerator(this, arguments, function* _map_1() {
              var e_1, _a;
              try {
                  for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
                      const val = iterable_1_1.value;
                      yield yield __await(yield __await(func(val)));
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
                  }
                  finally { if (e_1) throw e_1.error; }
              }
          });
      }
      function map(func, iterable) {
          if (iterable === undefined) {
              return curriedIterable => _map(func, curriedIterable);
          }
          return _map(func, iterable);
      }
  
      function flatMap(func, iterable) {
          if (iterable === undefined) {
              return (curriedIterable) => flatMap(func, curriedIterable);
          }
          return filter(i => i !== undefined && i !== null, flatten(map(func, iterable)));
      }
  
      function _flatTransform(concurrency, func, iterable) {
          const iterator = getIterator(iterable);
          const resultQueue = [];
          const readQueue = [];
          let ended = false;
          let reading = false;
          let inflightCount = 0;
          let lastError = null;
          function fulfillReadQueue() {
              while (readQueue.length > 0 && resultQueue.length > 0) {
                  const { resolve } = readQueue.shift();
                  const value = resultQueue.shift();
                  resolve({ done: false, value });
              }
              while (readQueue.length > 0 && inflightCount === 0 && ended) {
                  const { resolve, reject } = readQueue.shift();
                  if (lastError) {
                      reject(lastError);
                      lastError = null;
                  }
                  else {
                      resolve({ done: true, value: undefined });
                  }
              }
          }
          async function fillQueue() {
              if (ended) {
                  fulfillReadQueue();
                  return;
              }
              if (reading) {
                  return;
              }
              if (inflightCount + resultQueue.length >= concurrency) {
                  return;
              }
              reading = true;
              inflightCount++;
              try {
                  const { done, value } = await iterator.next();
                  if (done) {
                      ended = true;
                      inflightCount--;
                      fulfillReadQueue();
                  }
                  else {
                      mapAndQueue(value);
                  }
              }
              catch (error) {
                  ended = true;
                  inflightCount--;
                  lastError = error;
                  fulfillReadQueue();
              }
              reading = false;
              fillQueue();
          }
          async function mapAndQueue(itrValue) {
              var e_1, _a;
              try {
                  const value = await func(itrValue);
                  if (value && value[Symbol.asyncIterator]) {
                      try {
                          for (var _b = __asyncValues(value), _c; _c = await _b.next(), !_c.done;) {
                              const asyncVal = _c.value;
                              resultQueue.push(asyncVal);
                          }
                      }
                      catch (e_1_1) { e_1 = { error: e_1_1 }; }
                      finally {
                          try {
                              if (_c && !_c.done && (_a = _b.return)) await _a.call(_b);
                          }
                          finally { if (e_1) throw e_1.error; }
                      }
                  }
                  else {
                      resultQueue.push(value);
                  }
              }
              catch (error) {
                  ended = true;
                  lastError = error;
              }
              inflightCount--;
              fulfillReadQueue();
              fillQueue();
          }
          async function next() {
              if (resultQueue.length === 0) {
                  const deferred = defer();
                  readQueue.push(deferred);
                  fillQueue();
                  return deferred.promise;
              }
              const value = resultQueue.shift();
              fillQueue();
              return { done: false, value };
          }
          const asyncIterableIterator = {
              next,
              [Symbol.asyncIterator]: () => asyncIterableIterator,
          };
          return asyncIterableIterator;
      }
      function flatTransform(concurrency, func, iterable) {
          if (func === undefined) {
              return (curriedFunc, curriedIterable) => curriedIterable
                  ? flatTransform(concurrency, curriedFunc, curriedIterable)
                  : flatTransform(concurrency, curriedFunc);
          }
          if (iterable === undefined) {
              return (curriedIterable) => flatTransform(concurrency, func, curriedIterable);
          }
          return filter(i => i !== undefined && i !== null, flatten(_flatTransform(concurrency, func, iterable)));
      }
  
      async function onceReadable(stream) {
          return new Promise(resolve => {
              stream.once('readable', () => {
                  resolve();
              });
          });
      }
      function _fromStream(stream) {
          return __asyncGenerator(this, arguments, function* _fromStream_1() {
              while (true) {
                  const data = stream.read();
                  if (data !== null) {
                      yield yield __await(data);
                      continue;
                  }
                  if (stream._readableState.ended) {
                      return yield __await(void 0);
                  }
                  yield __await(onceReadable(stream));
              }
          });
      }
      function fromStream(stream) {
          if (typeof stream[Symbol.asyncIterator] === 'function') {
              return stream;
          }
          return _fromStream(stream);
      }
  
      function merge(...iterables) {
          return __asyncGenerator(this, arguments, function* merge_1() {
              const sources = new Set(iterables.map(getIterator));
              while (sources.size > 0) {
                  for (const iterator of sources) {
                      const nextVal = yield __await(iterator.next());
                      if (nextVal.done) {
                          sources.delete(iterator);
                      }
                      else {
                          yield yield __await(nextVal.value);
                      }
                  }
              }
          });
      }
  
      function pipeline(firstFn, ...fns) {
          let previousFn = firstFn();
          for (const func of fns) {
              previousFn = func(previousFn);
          }
          return previousFn;
      }
  
      function _parallelMap(concurrency, func, iterable) {
          return __asyncGenerator(this, arguments, function* _parallelMap_1() {
              let transformError = null;
              const wrapFunc = value => ({
                  value: func(value),
              });
              const stopOnError = function (source) {
                  return __asyncGenerator(this, arguments, function* () {
                      var e_1, _a;
                      try {
                          for (var source_1 = __asyncValues(source), source_1_1; source_1_1 = yield __await(source_1.next()), !source_1_1.done;) {
                              const value = source_1_1.value;
                              if (transformError) {
                                  return yield __await(void 0);
                              }
                              yield yield __await(value);
                          }
                      }
                      catch (e_1_1) { e_1 = { error: e_1_1 }; }
                      finally {
                          try {
                              if (source_1_1 && !source_1_1.done && (_a = source_1.return)) yield __await(_a.call(source_1));
                          }
                          finally { if (e_1) throw e_1.error; }
                      }
                  });
              };
              const output = pipeline(() => iterable, buffer(1), stopOnError, map(wrapFunc), buffer(concurrency));
              const itr = getIterator(output);
              while (true) {
                  const { value, done } = yield __await(itr.next());
                  if (done) {
                      break;
                  }
                  try {
                      const val = yield __await(value.value);
                      if (!transformError) {
                          yield yield __await(val);
                      }
                  }
                  catch (error) {
                      transformError = error;
                  }
              }
              if (transformError) {
                  throw transformError;
              }
          });
      }
      function parallelMap(concurrency, func, iterable) {
          if (func === undefined) {
              return (curriedFunc, curriedIterable) => parallelMap(concurrency, curriedFunc, curriedIterable);
          }
          if (iterable === undefined) {
              return curriedIterable => parallelMap(concurrency, func, curriedIterable);
          }
          return _parallelMap(concurrency, func, iterable);
      }
  
      function parallelFlatMap(concurrency, func, iterable) {
          if (func === undefined) {
              return (curriedFunc, curriedIterable) => curriedIterable
                  ? parallelFlatMap(concurrency, curriedFunc, curriedIterable)
                  : parallelFlatMap(concurrency, curriedFunc);
          }
          if (iterable === undefined) {
              return (curriedIterable) => parallelFlatMap(concurrency, func, curriedIterable);
          }
          return filter(i => i !== undefined && i !== null, flatten(parallelMap(concurrency, func, iterable)));
      }
  
      function parallelMerge(...iterables) {
          return __asyncGenerator(this, arguments, function* parallelMerge_1() {
              const inputs = iterables.map(getIterator);
              const concurrentWork = new Set();
              const values = new Map();
              let lastError = null;
              let errCb = null;
              let valueCb = null;
              const notifyError = err => {
                  lastError = err;
                  if (errCb) {
                      errCb(err);
                  }
              };
              const notifyDone = value => {
                  if (valueCb) {
                      valueCb(value);
                  }
              };
              const waitForQueue = () => new Promise((resolve, reject) => {
                  if (lastError) {
                      reject(lastError);
                  }
                  if (values.size > 0) {
                      return resolve();
                  }
                  valueCb = resolve;
                  errCb = reject;
              });
              const queueNext = input => {
                  const nextVal = Promise.resolve(input.next()).then(async ({ done, value }) => {
                      if (!done) {
                          values.set(input, value);
                      }
                      concurrentWork.delete(nextVal);
                  });
                  concurrentWork.add(nextVal);
                  nextVal.then(notifyDone, notifyError);
              };
              for (const input of inputs) {
                  queueNext(input);
              }
              while (true) {
                  // We technically don't have to check `values.size` as the for loop should have emptied it
                  // However I haven't yet found specs verifying that behavior, only tests
                  // the guard in waitForQueue() checking for values is in place for the same reason
                  if (concurrentWork.size === 0 && values.size === 0) {
                      return yield __await(void 0);
                  }
                  yield __await(waitForQueue());
                  for (const [input, value] of values) {
                      values.delete(input);
                      yield yield __await(value);
                      queueNext(input);
                  }
              }
          });
      }
  
      async function _reduce(func, start, iterable) {
          var e_1, _a;
          let value = start;
          try {
              for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = await iterable_1.next(), !iterable_1_1.done;) {
                  const nextItem = iterable_1_1.value;
                  value = await func(value, nextItem);
              }
          }
          catch (e_1_1) { e_1 = { error: e_1_1 }; }
          finally {
              try {
                  if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) await _a.call(iterable_1);
              }
              finally { if (e_1) throw e_1.error; }
          }
          return value;
      }
      function reduce(func, start, iterable) {
          if (start === undefined) {
              return (curriedStart, curriedIterable) => curriedIterable ? reduce(func, curriedStart, curriedIterable) : reduce(func, curriedStart);
          }
          if (iterable === undefined) {
              return (curriedIterable) => reduce(func, start, curriedIterable);
          }
          return _reduce(func, start, iterable);
      }
  
      function _take(count, iterable) {
          return __asyncGenerator(this, arguments, function* _take_1() {
              var e_1, _a;
              let taken = 0;
              try {
                  for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
                      const val = iterable_1_1.value;
                      yield yield __await(yield __await(val));
                      taken++;
                      if (taken >= count) {
                          return yield __await(void 0);
                      }
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
                  }
                  finally { if (e_1) throw e_1.error; }
              }
          });
      }
      function* _syncTake(count, iterable) {
          let taken = 0;
          for (const val of iterable) {
              yield val;
              taken++;
              if (taken >= count) {
                  return;
              }
          }
      }
      function take(count, iterable) {
          if (iterable === undefined) {
              return curriedIterable => take(count, curriedIterable);
          }
          if (iterable[Symbol.asyncIterator]) {
              return _take(count, iterable);
          }
          return _syncTake(count, iterable);
      }
  
      function _asyncTap(func, iterable) {
          return __asyncGenerator(this, arguments, function* _asyncTap_1() {
              var e_1, _a;
              try {
                  for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = yield __await(iterable_1.next()), !iterable_1_1.done;) {
                      const val = iterable_1_1.value;
                      yield __await(func(val));
                      yield yield __await(val);
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) yield __await(_a.call(iterable_1));
                  }
                  finally { if (e_1) throw e_1.error; }
              }
          });
      }
      function tap(func, iterable) {
          if (iterable === undefined) {
              return (curriedIterable) => _asyncTap(func, curriedIterable);
          }
          return _asyncTap(func, iterable);
      }
  
      function addTime(a, b) {
          let seconds = a[0] + b[0];
          let nanoseconds = a[1] + b[1];
          if (nanoseconds >= 1000000000) {
              const remainder = nanoseconds % 1000000000;
              seconds += (nanoseconds - remainder) / 1000000000;
              nanoseconds = remainder;
          }
          return [seconds, nanoseconds];
      }
      function _asyncTime(config, iterable) {
          return __asyncGenerator(this, arguments, function* _asyncTime_1() {
              const itr = iterable[Symbol.asyncIterator]();
              let total = [0, 0];
              while (true) {
                  const start = process.hrtime();
                  const { value, done } = yield __await(itr.next());
                  const delta = process.hrtime(start);
                  total = addTime(total, delta);
                  if (config.progress) {
                      config.progress(delta, total);
                  }
                  if (done) {
                      if (config.total) {
                          config.total(total);
                      }
                      return yield __await(value);
                  }
                  yield yield __await(value);
              }
          });
      }
      function* _syncTime(config, iterable) {
          const itr = iterable[Symbol.iterator]();
          let total = [0, 0];
          while (true) {
              const start = process.hrtime();
              const { value, done } = itr.next();
              const delta = process.hrtime(start);
              total = addTime(total, delta);
              if (config.progress) {
                  config.progress(delta, total);
              }
              if (done) {
                  if (config.total) {
                      config.total(total);
                  }
                  return value;
              }
              yield value;
          }
      }
      function time(config = {}, iterable) {
          if (iterable === undefined) {
              return curriedIterable => time(config, curriedIterable);
          }
          if (iterable[Symbol.asyncIterator] !== undefined) {
              return _asyncTime(config, iterable);
          }
          else {
              return _syncTime(config, iterable);
          }
      }
  
      function _transform(concurrency, func, iterable) {
          const iterator = getIterator(iterable);
          const resultQueue = [];
          const readQueue = [];
          let ended = false;
          let reading = false;
          let inflightCount = 0;
          let lastError = null;
          function fulfillReadQueue() {
              while (readQueue.length > 0 && resultQueue.length > 0) {
                  const { resolve } = readQueue.shift();
                  const value = resultQueue.shift();
                  resolve({ done: false, value });
              }
              while (readQueue.length > 0 && inflightCount === 0 && ended) {
                  const { resolve, reject } = readQueue.shift();
                  if (lastError) {
                      reject(lastError);
                      lastError = null;
                  }
                  else {
                      resolve({ done: true, value: undefined });
                  }
              }
          }
          async function fillQueue() {
              if (ended) {
                  fulfillReadQueue();
                  return;
              }
              if (reading) {
                  return;
              }
              if (inflightCount + resultQueue.length >= concurrency) {
                  return;
              }
              reading = true;
              inflightCount++;
              try {
                  const { done, value } = await iterator.next();
                  if (done) {
                      ended = true;
                      inflightCount--;
                      fulfillReadQueue();
                  }
                  else {
                      mapAndQueue(value);
                  }
              }
              catch (error) {
                  ended = true;
                  inflightCount--;
                  lastError = error;
                  fulfillReadQueue();
              }
              reading = false;
              fillQueue();
          }
          async function mapAndQueue(itrValue) {
              try {
                  const value = await func(itrValue);
                  resultQueue.push(value);
              }
              catch (error) {
                  ended = true;
                  lastError = error;
              }
              inflightCount--;
              fulfillReadQueue();
              fillQueue();
          }
          async function next() {
              if (resultQueue.length === 0) {
                  const deferred = defer();
                  readQueue.push(deferred);
                  fillQueue();
                  return deferred.promise;
              }
              const value = resultQueue.shift();
              fillQueue();
              return { done: false, value };
          }
          const asyncIterableIterator = {
              next,
              [Symbol.asyncIterator]: () => asyncIterableIterator,
          };
          return asyncIterableIterator;
      }
      function transform(concurrency, func, iterable) {
          if (func === undefined) {
              return (curriedFunc, curriedIterable) => curriedIterable
                  ? transform(concurrency, curriedFunc, curriedIterable)
                  : transform(concurrency, curriedFunc);
          }
          if (iterable === undefined) {
              return (curriedIterable) => transform(concurrency, func, curriedIterable);
          }
          return _transform(concurrency, func, iterable);
      }
  
      async function _writeToStream(stream, iterable) {
          var e_1, _a;
          let lastError = null;
          let errCb = null;
          let drainCb = null;
          const notifyError = err => {
              lastError = err;
              if (errCb) {
                  errCb(err);
              }
          };
          const notifyDrain = () => {
              if (drainCb) {
                  drainCb();
              }
          };
          const cleanup = () => {
              stream.removeListener('error', notifyError);
              stream.removeListener('drain', notifyDrain);
          };
          stream.once('error', notifyError);
          const waitForDrain = () => new Promise((resolve, reject) => {
              if (lastError) {
                  return reject(lastError);
              }
              stream.once('drain', notifyDrain);
              drainCb = resolve;
              errCb = reject;
          });
          try {
              for (var iterable_1 = __asyncValues(iterable), iterable_1_1; iterable_1_1 = await iterable_1.next(), !iterable_1_1.done;) {
                  const value = iterable_1_1.value;
                  if (stream.write(value) === false) {
                      await waitForDrain();
                  }
                  if (lastError) {
                      break;
                  }
              }
          }
          catch (e_1_1) { e_1 = { error: e_1_1 }; }
          finally {
              try {
                  if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) await _a.call(iterable_1);
              }
              finally { if (e_1) throw e_1.error; }
          }
          cleanup();
          if (lastError) {
              throw lastError;
          }
      }
      function writeToStream(stream, iterable) {
          if (iterable === undefined) {
              return (curriedIterable) => _writeToStream(stream, curriedIterable);
          }
          return _writeToStream(stream, iterable);
      }
  
      /// <reference lib="esnext.asynciterable" />
      if (Symbol.asyncIterator === undefined) {
          Symbol.asyncIterator = Symbol.for('asyncIterator');
      }
  
      exports.batch = batch;
      exports.buffer = buffer;
      exports.collect = collect;
      exports.concat = concat;
      exports.consume = consume;
      exports.filter = filter;
      exports.flatMap = flatMap;
      exports.flatten = flatten;
      exports.flatTransform = flatTransform;
      exports.fromStream = fromStream;
      exports.getIterator = getIterator;
      exports.map = map;
      exports.merge = merge;
      exports.parallelFlatMap = parallelFlatMap;
      exports.parallelMap = parallelMap;
      exports.parallelMerge = parallelMerge;
      exports.pipeline = pipeline;
      exports.reduce = reduce;
      exports.take = take;
      exports.tap = tap;
      exports.time = time;
      exports.transform = transform;
      exports.writeToStream = writeToStream;
  
      Object.defineProperty(exports, '__esModule', { value: true });
  
  }));
  