Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add inMemory option support #32

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading