Skip to content

Commit 9938548

Browse files
committed
refactor: replace buffer with chunks array for better memory management
1 parent cbb3613 commit 9938548

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/utils/stream.utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ export class StreamUtils {
6767
}
6868

6969
export class BufferStream extends Transform {
70-
public buffer: Buffer | null;
70+
private chunks: Buffer[];
7171

7272
constructor(opts?: TransformOptions) {
7373
super(opts);
74-
this.buffer = null;
74+
this.chunks = [];
7575
}
7676

7777
_transform(chunk: Buffer, _: BufferEncoding, callback: TransformCallback) {
78-
const currentBuffer = this.buffer ?? Buffer.alloc(0);
79-
this.buffer = Buffer.concat([currentBuffer, chunk]);
78+
this.chunks.push(chunk);
8079
callback(null, chunk);
8180
}
8281

@@ -85,10 +84,11 @@ export class BufferStream extends Transform {
8584
}
8685

8786
reset() {
88-
this.buffer = null;
87+
this.chunks = [];
8988
}
9089

91-
getBuffer(): Buffer | null {
92-
return this.buffer;
90+
getBuffer(): Buffer | undefined {
91+
if (this.chunks.length === 0) return undefined;
92+
return Buffer.concat(this.chunks);
9393
}
9494
}

0 commit comments

Comments
 (0)