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,iEAAiE;IACjE,IAAI,+CAA+C,GAAG,CAAI,MAAW,EAAE,IAAU,EAAE,EAAE;QACnF,OAAO,MAAM,YAAY,iCAAiC;YACxD,CAAC,CAAE,IAAI,gCAAgC,CAAC,MAAM,EAAE,IAAI,CAAuB;YAC3E,CAAC,CAAE,IAAI,mCAAmC,CAAC,MAAM,EAAE,IAAI,CAAuB,CAAC;IACnF,CAAC,CAAC;IAEF,OAAO,CAAI,MAAW,EAAE,IAAU,EAAE,EAAE,CAAC,iCAAiC,CAAI,MAAM,EAAE,IAAI,CAAC,CAAC;AAC5F,CAAC,CAAC,EAAE,CAAC;AAWL,MAAM,UAAU,WAAW,CACzB,MAA0B,EAC1B,OAA+D;IAE/D,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,EAAE;QACxC,OAAO,2BAA2B,CAChC,IAAI,oCAAoC,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,EACxE,OAAO,CACR,CAAC;KACH;IACD,OAAO,2BAA2B,CAChC,IAAI,iCAAiC,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,EACrE,OAAO,CACR,CAAC;AACJ,CAAC","file":"todomstream.js","sourcesContent":["import { fromDOMStream } from './fromdomstream';\n\nexport type ReadableBYOBStreamOptions<T = any> = QueuingStrategy<T> & { type: 'bytes' };\nexport type ReadableByteStreamOptions<T = any> = QueuingStrategy<T> & {\n type: 'bytes';\n autoAllocateChunkSize?: number;\n};\n\n/** @ignore */\nfunction memcpy<TTarget extends ArrayBufferView, TSource extends ArrayBufferView>(\n target: TTarget,\n source: TSource,\n targetByteOffset = 0,\n sourceByteLength = source.byteLength\n) {\n const targetByteLength = target.byteLength;\n const dst = new Uint8Array(target.buffer, target.byteOffset, targetByteLength);\n const src = new Uint8Array(\n source.buffer,\n source.byteOffset,\n Math.min(sourceByteLength, targetByteLength)\n );\n dst.set(src, targetByteOffset);\n return src.byteLength;\n}\n\nabstract class AbstractUnderlyingSource<TSource> {\n constructor(protected _source: AsyncIterator<TSource> | null) {}\n async cancel() {\n const source = this._source;\n if (source && source.return) {\n await source.return();\n }\n this._source = null;\n }\n}\n\nclass UnderlyingAsyncIterableDefaultSource<TSource = any> extends AbstractUnderlyingSource<TSource>\n implements UnderlyingSource<TSource> {\n constructor(source: AsyncIterator<TSource> | null) {\n super(source);\n }\n async pull(controller: ReadableStreamDefaultController<TSource>) {\n const source = this._source;\n if (source) {\n const r = await source.next(controller.desiredSize);\n if (!r.done) {\n return controller.enqueue(r.value);\n }\n }\n controller.close();\n }\n}\n\nclass UnderlyingAsyncIterableByteSource<TSource extends ArrayBufferView = Uint8Array>\n extends AbstractUnderlyingSource<TSource>\n implements UnderlyingByteSource {\n public readonly type: 'bytes';\n public readonly autoAllocateChunkSize?: number;\n\n // If we can't create a \"byob\" reader (no browsers currently suppor it),\n // fallback to pulling values from the source iterator and enqueueing like\n // object streams\n private fallbackDefaultSource: UnderlyingAsyncIterableDefaultSource<TSource>;\n\n constructor(\n reader: AsyncIterator<TSource> | null,\n opts: { autoAllocateChunkSize?: number } = {}\n ) {\n super(reader);\n this.type = 'bytes';\n this.autoAllocateChunkSize = opts.autoAllocateChunkSize;\n this.fallbackDefaultSource = new UnderlyingAsyncIterableDefaultSource<TSource>(reader);\n }\n async pull(controller: ReadableByteStreamController) {\n if (!controller.byobRequest) {\n return await this.fallbackDefaultSource.pull(controller);\n }\n if (this._source) {\n const { view } = controller.byobRequest;\n const { done, value } = await this._source.next(view);\n if (!done) {\n // Did the source write into the BYOB view itself,\n // then yield us the `bytesWritten` value? If so,\n // pass that along\n if (typeof value === 'number') {\n return controller.byobRequest.respond(value);\n }\n // otherwise if the source is only producing buffers\n // but doesn't expect to be given one, we should copy\n // the produced buffer into the front of the BYOB view\n if (ArrayBuffer.isView(value)) {\n return value.buffer === view.buffer\n ? controller.byobRequest.respondWithNewView(value)\n : controller.byobRequest.respond(memcpy(view, value));\n }\n }\n }\n controller.close();\n }\n}\n\n// Generate subclasses of ReadableStream that conform to the\n// AsyncIterable protocol. These classes are dynamically created\n// the first time a ReadableStream is produced because ReadableStream\n// is a browser-only API, and closure-compiler won't compile if they're\n// statically defined at the module scope.\n/** @ignore */\nconst asyncIterableReadableStream = (() => {\n let AsyncIterableReadableByteStream_: any;\n let AsyncIterableDefaultReadableStream_: any;\n\n // A function that's called the first time someone creates a\n // ReadableStream via `toDOMStream()`\n let createFirstTime = <T>(source: any, opts?: any) => {\n // Generate the subclasses with [Symbol.asyncIterator]() methods\n class AsyncIterableDefaultReadableStream extends ReadableStream<T> {\n [Symbol.asyncIterator]() {\n return fromDOMStream(this)[Symbol.asyncIterator]();\n }\n }\n class AsyncIterableReadableByteStream extends ReadableStream<Uint8Array> {\n [Symbol.asyncIterator]() {\n return fromDOMStream(this, { mode: 'byob' })[Symbol.asyncIterator]();\n }\n }\n AsyncIterableReadableByteStream_ = AsyncIterableReadableByteStream;\n AsyncIterableDefaultReadableStream_ = AsyncIterableDefaultReadableStream;\n // Now point `createAsyncIterableReadableStream` to the function that\n // instantiates the classes we just created\n //tslint:disable-next-line\n createAsyncIterableReadableStream = createAsyncIterableReadableStreamEveryOtherTime;\n // Create and return the first ReadableStream<T> instance\n //tslint:disable-next-line\n return createAsyncIterableReadableStreamEveryOtherTime<T>(source, opts) as ReadableStream<T>;\n };\n\n // Shared function pointer that's called by the wrapper closure we return\n //tslint:disable-next-line\n let createAsyncIterableReadableStream = createFirstTime;\n // Create instances of the classes generated by `createFirstTime`\n let createAsyncIterableReadableStreamEveryOtherTime = <T>(source: any, opts?: any) => {\n return source instanceof UnderlyingAsyncIterableByteSource\n ? (new AsyncIterableReadableByteStream_(source, opts) as ReadableStream<T>)\n : (new AsyncIterableDefaultReadableStream_(source, opts) as ReadableStream<T>);\n };\n\n return <T>(source: any, opts?: any) => createAsyncIterableReadableStream<T>(source, opts);\n})();\n\nexport function toDOMStream<T>(source: AsyncIterable<T>): ReadableStream<T>;\nexport function toDOMStream<T>(\n source: AsyncIterable<T>,\n options: ReadableBYOBStreamOptions<Uint8Array>\n): ReadableStream<Uint8Array>;\nexport function toDOMStream<T>(\n source: AsyncIterable<T>,\n options: ReadableByteStreamOptions<Uint8Array>\n): ReadableStream<Uint8Array>;\nexport function toDOMStream(\n source: AsyncIterable<any>,\n options?: ReadableBYOBStreamOptions | ReadableByteStreamOptions\n) {\n if (!options || options.type !== 'bytes') {\n return asyncIterableReadableStream(\n new UnderlyingAsyncIterableDefaultSource(source[Symbol.asyncIterator]()),\n options\n );\n }\n return asyncIterableReadableStream(\n new UnderlyingAsyncIterableByteSource(source[Symbol.asyncIterator]()),\n options\n );\n}\n"]}
\No newline at end of file