Skip to content

Commit

Permalink
feat: add inMemory option support
Browse files Browse the repository at this point in the history
It's now possible to opt-out from in-memory chunks collection
  • Loading branch information
alessiofrittoli committed Jan 18, 2025
1 parent 5db1042 commit eae1a04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
14 changes: 14 additions & 0 deletions __tests__/reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ describe( 'StreamReader', () => {
} )
expect( chunks ).toEqual( defaultChunks )
} )


it( 'doen\'t collect in-memory chunks when `inMemory` options is set to `false`', async () => {
const stream = new TransformStream<Buffer, Buffer>()
const writer = stream.writable.getWriter()
const reader = new StreamReader( stream.readable, { inMemory: false } )

streamData( { writer } )

const dataRead = await reader.read()

expect( dataRead ).toEqual( [] )

} )
} )


Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export class StreamReader<I = unknown, O = I> extends EventEmitter<StreamReaderE
* @private
*/
private receivedChunks: ReadChunks<O>

private inMemory: Options<I, O>[ 'inMemory' ]
private transform: Options<I, O>[ 'transform' ]


Expand All @@ -46,6 +48,7 @@ export class StreamReader<I = unknown, O = I> extends EventEmitter<StreamReaderE
{
super( { captureRejections: true } )

this.inMemory = options?.inMemory ?? true
this.transform = options?.transform
this.reader = stream.getReader()
this.closed = false
Expand All @@ -70,8 +73,9 @@ export class StreamReader<I = unknown, O = I> extends EventEmitter<StreamReaderE
? await this.transform( chunk )
: chunk as ReadChunk<O>
)

this.receivedChunks.push( processed )
if ( this.inMemory ) {
this.receivedChunks.push( processed )
}
this.emit( 'data', processed )
}
return (
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface Options<I = unknown, O = I>
{
/** A function that transforms a chunk of data. */
transform?: TransformChunk<I, O>
/** Allows to opt-out from in-memory chunks collection. */
inMemory?: boolean
}


Expand Down

0 comments on commit eae1a04

Please sign in to comment.