import { _ } from 'streamline-runtime' ; import { Reader } from '../reader' ; import { Writer } from '../writer' ; import { nextTick } from '../util' ; export interface Options { sync ? : boolean ; chunkSize ? : number | ( ( ) => number ) ; } export class BufferWriter extends Writer < Buffer > { chunks : Buffer [ ] ; constructor ( options ) { super ( ( _ , data ) => { if ( ! options . sync ) nextTick ( _ ) ; if ( data !== undefined ) this . chunks . push ( data ) ; return this ; } ) ; this . chunks = [ ] ; } toBuffer ( ) { return Buffer . concat ( this . chunks ) ; } get result ( ) { return this . toBuffer ( ) ; } } export function reader ( buffer , options ? ) { var opts ; if ( typeof options === "number" ) { opts = { chunkSize : options } ; } else opts = options || { } ; const chunkSize = opts . chunkSize || 1024 ; var pos = 0 ; return new Reader ( function read ( _ ) { if ( ! opts . sync ) nextTick ( _ ) ; if ( pos >= buffer . length ) return ; const len = typeof chunkSize === "function" ? chunkSize ( ) : chunkSize ; const s = buffer . slice ( pos , pos + len ) ; pos += len ; return s ; } ) ; } export function writer ( options ? ) { return new BufferWriter ( options || { } ) ; }