import { publish } from './operators/publish' ; import { fromDOMStream } from './fromdomstream' ; import { AsyncIterableX } from './asynciterablex' ; export type ReadableBYOBStreamOptions < T = any > = QueuingStrategy < T > & { type : 'bytes' ; } ; export type ReadableByteStreamOptions < T = any > = QueuingStrategy < T > & { type : 'bytes' ; autoAllocateChunkSize ? : number ; } ; type AsyncSourceIterator < TSource > = AsyncIterator < TSource , any , number | ArrayBufferView | undefined | null > ; ignore function memcpy < TTarget extends ArrayBufferView , TSource extends ArrayBufferView > ( target , source , targetByteOffset = 0 , sourceByteLength = source . byteLength ) { const targetByteLength = target . byteLength ; const dst = new Uint8Array ( target . buffer , target . byteOffset , targetByteLength ) ; const src = new Uint8Array ( source . buffer , source . byteOffset , Math . min ( sourceByteLength , targetByteLength , source . buffer . byteLength - source . byteOffset ) ) ; dst . set ( src , targetByteOffset ) ; return src . byteLength ; } abstract class AbstractUnderlyingSource < TSource > { constructor ( protected _source ) { } async cancel ( ) { const source = this . _source ; if ( source && source . return ) { await source . return ( ) ; } this . _source = null ; } } class UnderlyingAsyncIterableDefaultSource < TSource = any > extends AbstractUnderlyingSource < TSource > implements UnderlyingSource < TSource > { constructor ( source ) { super ( source ) ; } async pull ( controller ) { const source = this . _source ; if ( source ) { const r = await source . next ( controller . desiredSize ) ; if ( ! r . done ) { return controller . enqueue ( r . value ) ; } } controller . close ( ) ; } } class UnderlyingAsyncIterableByteSource < TSource extends ArrayBufferView = Uint8Array > extends AbstractUnderlyingSource < TSource > implements UnderlyingSource < TSource > { public readonly autoAllocateChunkSize ? : number ; private fallbackDefaultSource : UnderlyingAsyncIterableDefaultSource < TSource > ; constructor ( reader , opts = { } ) { super ( reader ) ; ( this as any ) . type = 'bytes' ; this . autoAllocateChunkSize = opts [ 'autoAllocateChunkSize' ] ; this . fallbackDefaultSource = new UnderlyingAsyncIterableDefaultSource < TSource > ( reader ) ; } async pull ( controller ) { if ( ! ( controller as any ) . byobRequest ) { return await this . fallbackDefaultSource . pull ( controller ) ; } if ( this . _source ) { const { view } = ( controller as any ) . byobRequest ; const { done , value } = await this . _source . next ( view ) ; if ( ! done ) { if ( typeof value === 'number' ) { return ( controller as any ) . byobRequest . respond ( value ) ; } if ( ArrayBuffer . isView ( value ) ) { return value . buffer === view . buffer ? ( controller as any ) . byobRequest . respondWithNewView ( value ) : ( controller as any ) . byobRequest . respond ( memcpy ( view , value ) ) ; } } } controller . close ( ) ; } } ignore const asyncIterableReadableStream = ( ( ) => { let AsyncIterableReadableByteStream_ ; let AsyncIterableDefaultReadableStream_ ; const createFirstTime = < T > ( source , opts ? ) => { class AsyncIterableDefaultReadableStream extends ReadableStream < T > { [ Symbol . asyncIterator ] ( ) { return fromDOMStream ( this ) [ Symbol . asyncIterator ] ( ) ; } } class AsyncIterableReadableByteStream extends ReadableStream < Uint8Array > { [ Symbol . asyncIterator ] ( ) { return fromDOMStream ( this , { mode : 'byob' } ) [ Symbol . asyncIterator ] ( ) ; } } AsyncIterableReadableByteStream_ = AsyncIterableReadableByteStream ; AsyncIterableDefaultReadableStream_ = AsyncIterableDefaultReadableStream ; createAsyncIterableReadableStream = createAsyncIterableReadableStreamEveryOtherTime ; return createAsyncIterableReadableStreamEveryOtherTime < T > ( source , opts ) as ReadableStream < T > ; } ; let createAsyncIterableReadableStream = createFirstTime ; const createAsyncIterableReadableStreamEveryOtherTime = < T > ( source , opts ? ) => { return source instanceof UnderlyingAsyncIterableByteSource ? ( new AsyncIterableReadableByteStream_ ( source , opts ) as ReadableStream < T > ) : ( new AsyncIterableDefaultReadableStream_ ( source , opts ) as ReadableStream < T > ) ; } ; return < T > ( source , opts ? ) => createAsyncIterableReadableStream < T > ( source , opts ) ; } ) ( ) ; param source param strategy export function toDOMStream < T > ( source , strategy ? ) ; param source param options export function toDOMStream < T > ( source , options ) ; param source param options export function toDOMStream < T > ( source , options ) ; param source param options export function toDOMStream ( source , options ? ) { if ( ! options || ! ( 'type' in options ) || options [ 'type' ] !== 'bytes' ) { return asyncIterableReadableStream ( new UnderlyingAsyncIterableDefaultSource ( source [ Symbol . asyncIterator ] ( ) ) , options ) ; } return asyncIterableReadableStream ( new UnderlyingAsyncIterableByteSource ( source [ Symbol . asyncIterator ] ( ) , options as ReadableByteStreamOptions ) , options ) ; } AsyncIterableX . prototype . tee = function < T > ( this ) { return _getDOMStream ( this ) . tee ( ) ; } ; AsyncIterableX . prototype . pipeTo = function < T > ( this , writable , options ? ) { return _getDOMStream ( this ) . pipeTo ( writable , options ) ; } ; AsyncIterableX . prototype . pipeThrough = function < T , R extends ReadableStream < any > > ( this , duplex , options ? ) { return _getDOMStream ( this ) . pipeThrough ( duplex , options ) ; } ; function _getDOMStream < T > ( self ) { return self . _DOMStream || ( self . _DOMStream = self . pipe ( publish < T > ( ) , toDOMStream ) ) ; } ignore export function toDOMStreamProto < T > ( this , strategy ? ) ; export function toDOMStreamProto < T > ( this , options ) ; export function toDOMStreamProto < T > ( this , options ) ; export function toDOMStreamProto ( this , options ? ) { return ! options ? toDOMStream ( this ) : toDOMStream ( this , options ) ; } AsyncIterableX . prototype . toDOMStream = toDOMStreamProto ; declare module '../asynciterable/asynciterablex' { interface AsyncIterableX < T > { toDOMStream : typeof toDOMStreamProto ; tee ( ) : [ ReadableStream < T > , ReadableStream < T > ] ; pipeTo ( writable , options ? ) : Promise < void > ; pipeThrough < R extends ReadableStream < any > > ( duplex , options ? ) : R ; } }