import '../asynciterablehelpers' ; import { Readable , ReadableOptions } from 'stream' ; import { from , fromDOMStream } from 'ix/asynciterable' ; ( ( ) => { if ( ! fromDOMStream || process . env . TEST_DOM_STREAMS !== 'true' ) { return test ( 'not testing node streams because process.env.TEST_DOM_STREAMS !== "true"' , ( ) => { } ) ; } const { toStream } = require ( 'web-stream-tools' ) . default ; class Counter extends Readable { private _index : number ; private _max : number ; constructor ( options ? ) { super ( options ) ; this . _max = 3 ; this . _index = 0 ; } _read ( ) { this . push ( ++ this . _index > this . _max ? null : `${ this . _index }` ) ; } } const compare = < T > ( a , b ) => { const aVal = ArrayBuffer . isView ( a ) ? `${ Buffer . from ( a . buffer , a . byteOffset , a . byteLength ) }` : a ; const bVal = ArrayBuffer . isView ( b ) ? `${ Buffer . from ( b . buffer , b . byteOffset , b . byteLength ) }` : b ; try { expect ( aVal ) . toEqual ( bVal ) ; } catch ( e ) { return false ; } return true ; } ; describe ( 'AsyncIterable#fromDOMStream' , ( ) => { test ( 'objectMode: true' , async ( ) => { const c = toStream ( new Counter ( { objectMode : true } ) ) ; const xs = fromDOMStream ( c ) as AsyncIterable < string > ; const expected = from ( [ '1' , '2' , '3' ] ) ; await expect ( xs ) . toEqualStream ( expected , compare ) ; } ) ; test ( 'objectMode: false' , async ( ) => { const c = toStream ( new Counter ( { objectMode : false } ) ) ; const xs = fromDOMStream ( c ) as AsyncIterable < Buffer > ; const expected = from ( [ '1' , '2' , '3' ] . map ( ( s ) => Buffer . from ( s ) ) ) ; await expect ( xs ) . toEqualStream ( expected , compare ) ; } ) ; } ) ; } ) ( ) ;