@@ -19,6 +19,21 @@ function toArrayBuffer(buffer: Buffer): ArrayBuffer {
1919 ? arrayBuffer // if Buffer occupies whole ArrayBuffer, no need to slice it
2020 : arrayBuffer . slice ( byteOffset , byteOffset + byteLength )
2121}
22+ type ArrayBufferCallback = ( err : Error | null , buffer : ArrayBuffer | null ) => void
23+ function concatStream ( stream : Readable , callback : ArrayBufferCallback ) {
24+ const segments : Buffer [ ] = [ ]
25+ stream
26+ . on ( 'data' , chunk =>
27+ segments . push ( chunk instanceof Buffer ? chunk : Buffer . from ( chunk ) )
28+ )
29+ . on ( 'error' , function ( this : typeof stream , err ) {
30+ this . destroy ( )
31+ callback ( err , null )
32+ } )
33+ . on ( 'end' , ( ) =>
34+ callback ( null , toArrayBuffer ( Buffer . concat ( segments ) ) )
35+ )
36+ }
2237
2338export interface WriteParams < E > {
2439 type : Type < E >
@@ -245,22 +260,14 @@ export declare namespace writeTypeAndValue {
245260export function readType < E > ( inStream : Readable , callback : TypeCallback < E > ) {
246261 assert . instanceOf ( inStream , Readable )
247262 assert . instanceOf ( callback , Function )
248- const segments : Buffer [ ] = [ ]
249- inStream
250- . on ( 'data' , chunk =>
251- segments . push ( chunk instanceof Buffer ? chunk : Buffer . from ( chunk ) )
252- )
253- . on ( 'error' , function ( this : typeof inStream , err : Error ) {
254- this . destroy ( )
255- callback ( err , null )
256- } )
257- . on ( 'end' , ( ) => {
258- const buffer = Buffer . concat ( segments )
259- let type : Type < E >
260- try { type = r . type ( toArrayBuffer ( buffer ) , false ) }
261- catch ( e ) { return callback ( e , null ) }
262- callback ( null , type )
263- } )
263+ concatStream ( inStream , ( err , buffer ) => {
264+ if ( err ) return callback ( err , null )
265+
266+ let type : Type < E >
267+ try { type = r . type ( buffer ! , false ) }
268+ catch ( e ) { return callback ( e , null ) }
269+ callback ( null , type )
270+ } )
264271}
265272export declare namespace readType {
266273 function __promisify__ < E > ( inStream : Readable ) : Promise < Type < E > >
@@ -301,20 +308,14 @@ export declare namespace readType {
301308export function readValue < E > ( { type, inStream} : ReadValueParams < E > , callback : ValueCallback < E > ) {
302309 assert . instanceOf ( inStream , Readable )
303310 assert . instanceOf ( callback , Function )
304- const segments : Buffer [ ] = [ ]
305- inStream
306- . on ( 'data' , chunk => segments . push ( chunk as Buffer ) )
307- . on ( 'error' , function ( this : typeof inStream , err : Error ) {
308- this . destroy ( )
309- callback ( err , null )
310- } )
311- . on ( 'end' , ( ) => {
312- const buffer = Buffer . concat ( segments )
313- let value : E
314- try { value = type . readValue ( toArrayBuffer ( buffer ) ) }
315- catch ( e ) { return callback ( e , null ) }
316- callback ( null , value )
317- } )
311+ concatStream ( inStream , ( err , buffer ) => {
312+ if ( err ) return callback ( err , null )
313+
314+ let value : E
315+ try { value = type . readValue ( buffer ! ) }
316+ catch ( e ) { return callback ( e , null ) }
317+ callback ( null , value )
318+ } )
318319}
319320export declare namespace readValue {
320321 function __promisify__ < E > ( params : ReadValueParams < E > ) : Promise < E >
@@ -345,24 +346,18 @@ export declare namespace readValue {
345346export function readTypeAndValue < E > ( inStream : Readable , callback : TypeAndValueCallback < E > ) {
346347 assert . instanceOf ( inStream , Readable )
347348 assert . instanceOf ( callback , Function )
348- const segments : Buffer [ ] = [ ]
349- inStream
350- . on ( 'data' , chunk => segments . push ( chunk as Buffer ) )
351- . on ( 'error' , function ( this : typeof inStream , err : Error ) {
352- this . destroy ( )
353- callback ( err , null , null )
354- } )
355- . on ( 'end' , ( ) => {
356- const buffer = Buffer . concat ( segments )
357- let type : ReadResult < Type < E > >
358- //Using consumeType() in order to get the length of the type (the start of the value)
359- try { type = r . _consumeType ( toArrayBuffer ( buffer ) , 0 ) }
360- catch ( e ) { return callback ( e , null , null ) }
361- let value : E
362- try { value = type . value . readValue ( toArrayBuffer ( buffer ) , type . length ) }
363- catch ( e ) { return callback ( e , null , null ) }
364- callback ( null , type . value , value )
365- } )
349+ concatStream ( inStream , ( err , buffer ) => {
350+ if ( err ) return callback ( err , null , null )
351+
352+ let type : ReadResult < Type < E > >
353+ //Using consumeType() in order to get the length of the type (the start of the value)
354+ try { type = r . _consumeType ( buffer ! , 0 ) }
355+ catch ( e ) { return callback ( e , null , null ) }
356+ let value : E
357+ try { value = type . value . readValue ( buffer ! , type . length ) }
358+ catch ( e ) { return callback ( e , null , null ) }
359+ callback ( null , type . value , value )
360+ } )
366361}
367362//Custom promisifiy function because Promise cannot resolve to 2 values
368363( readTypeAndValue as any ) [ promisify . custom ] = < E > ( inStream : Readable ) =>
0 commit comments