import { Readable } from 'stream' ; export function toNodeReadable ( stream ) { return new NodeReadable ( stream , { } ) ; } export class NodeReadable extends Readable { private webStream : ReadableStream ; private reader : ReadableStreamDefaultReader ; private reading : boolean ; constructor ( webStream , options ) { super ( options ) ; this . webStream = webStream ; this . reader = webStream . getReader ( ) ; this . reading = false ; } _read ( size ) { if ( this . reading ) { return ; } this . reading = true ; this . doRead ( size ) ; } doRead ( size ) { this . reader . read ( ) . then ( ( { done , value } ) => { if ( done ) { this . push ( null ) ; return ; } if ( this . push ( value ) ) { this . doRead ( size ) ; return ; } this . reading = false ; } ) . catch ( ( e ) => { this . emit ( 'error' , e ) ; } ) ; } }