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CAAC,IAAI,qBAAqB,CAAU,MAAM,EAAE,OAAO,CAAC;QACrD,CAAC,CAAC,IAAI,qBAAqB,CAA6C,MAAM,EAAE,OAAO,CAAC,CAAC;AAC7F,CAAC;AAgBD,MAAM,UAAU,iBAAiB,CAE/B,OAAyB;IAEzB,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,UAAU,KAAK,IAAI;QAC5C,CAAC,CAAC,IAAI,qBAAqB,CAAU,IAAI,EAAE,OAAO,CAAC;QACnD,CAAC,CAAC,IAAI,qBAAqB,CAA6C,IAAI,EAAE,OAAO,CAAC,CAAC;AAC3F,CAAC;AAED,cAAc,CAAC,SAAS,CAAC,YAAY,GAAG,iBAAiB,CAAC","file":"tonodestream.js","sourcesContent":["import { BufferLike } from '../interfaces';\nimport { AsyncIterableX } from './asynciterablex';\nimport { Readable, ReadableOptions } from 'stream';\n\nconst done = async (_: any) => null as any;\n\ntype AsyncSourceIterator<TSource> = AsyncIterator<\nTSource,\nany,\nnumber | ArrayBufferView | undefined | null\n>;\n\nexport class AsyncIterableReadable<T> extends Readable {\n private _pulling: boolean = false;\n private _objectMode: boolean = true;\n private _iterator: AsyncSourceIterator<T> | undefined;\n constructor(source: AsyncIterable<T>, options?: ReadableOptions) {\n super(options);\n this._iterator = source[Symbol.asyncIterator]();\n this._objectMode = !options || !!options.objectMode;\n }\n public _read(size: number) {\n const it = this._iterator;\n if (it && !this._pulling && (this._pulling = true)) {\n Promise.resolve(this._pull(it, size)).then((p) => (this._pulling = p));\n }\n }\n public _destroy(err: Error | null, cb: (err: Error | null) => void) {\n const it = this._iterator;\n this._iterator = undefined;\n const fn = (it && (err ? it.throw : it.return)) || done;\n fn.call(it, err).then(() => cb && cb(null));\n }\n // eslint-disable-next-line complexity\n async _pull(it: AsyncSourceIterator<T>, size: number) {\n let innerSize = size;\n const objectMode = this._objectMode;\n let r: IteratorResult<BufferLike | T> | undefined;\n while (this.readable && !(r = await it.next(innerSize)).done) {\n if (innerSize != null) {\n if (objectMode) {\n innerSize -= 1;\n } else {\n innerSize -= Buffer.byteLength(<BufferLike>r.value || '');\n }\n }\n if (!this.push(r.value) || innerSize <= 0) {\n break;\n }\n }\n if ((r && r.done) || !this.readable) {\n this.push(null);\n if (it.return) {\n await it.return();\n }\n }\n return !this.readable;\n }\n}\n\n/**\n * Converts an existing async-iterable to a Node.js stream.\n * @param source The async-iterable to convert to a Node.js stream.\n * @param options The optional Readable options for the Node.js stream.\n */\nexport function toNodeStream<TSource>(\n source: AsyncIterable<TSource>\n): AsyncIterableReadable<TSource>;\nexport function toNodeStream<TSource>(\n source: AsyncIterable<TSource>,\n options: ReadableOptions & { objectMode: true }\n): AsyncIterableReadable<TSource>;\nexport function toNodeStream<TSource extends BufferLike>(\n source: AsyncIterable<TSource>,\n options: ReadableOptions & { objectMode: false }\n): AsyncIterableReadable<TSource>;\nexport function toNodeStream<TSource>(\n source: AsyncIterable<any>,\n options?: ReadableOptions\n): AsyncIterableReadable<TSource> {\n return !options || options.objectMode === true\n ? new AsyncIterableReadable<TSource>(source, options)\n : new AsyncIterableReadable<TSource extends BufferLike ? TSource : any>(source, options);\n}\n\n/**\n * @ignore\n */\nexport function toNodeStreamProto<TSource>(\n this: AsyncIterable<TSource>\n): AsyncIterableReadable<TSource>;\nexport function toNodeStreamProto<TSource>(\n this: AsyncIterable<TSource>,\n options: ReadableOptions & { objectMode: true }\n): AsyncIterableReadable<TSource>;\nexport function toNodeStreamProto<TSource extends BufferLike>(\n this: AsyncIterable<TSource>,\n options: ReadableOptions & { objectMode: false }\n): AsyncIterableReadable<TSource>;\nexport function toNodeStreamProto<TSource>(\n this: AsyncIterable<any>,\n options?: ReadableOptions\n): AsyncIterableReadable<TSource> {\n return !options || options.objectMode === true\n ? new AsyncIterableReadable<TSource>(this, options)\n : new AsyncIterableReadable<TSource extends BufferLike ? TSource : any>(this, options);\n}\n\nAsyncIterableX.prototype.toNodeStream = toNodeStreamProto;\n\ndeclare module '../asynciterable/asynciterablex' {\n interface AsyncIterableX<T> {\n toNodeStream: typeof toNodeStreamProto;\n }\n}\n"]}
\No newline at end of file