Skip to content

Commit a854843

Browse files
committed
Use official "Readable.from" methods where possible
1 parent c2bdf24 commit a854843

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/private/StreamUtils.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,27 @@ import type * as stream from "stream";
33

44
export class StreamUtils {
55
static create(): stream.Readable {
6-
const readable = new Readable();
7-
readable._read = () => {}; // _read is required but you can noop it
8-
return readable;
6+
return new Readable({
7+
read(_size) {
8+
// No implementation needed here.
9+
// The stream will wait for data to be pushed externally.
10+
}
11+
});
912
}
1013

11-
static empty(): NodeJS.ReadableStream {
12-
const readable = StreamUtils.create();
13-
StreamUtils.endStream(readable);
14-
return readable;
14+
static endStream(readable: stream.Readable): void {
15+
readable.push(null);
1516
}
1617

17-
static fromArrayBuffer(buffer: ArrayBuffer): NodeJS.ReadableStream {
18-
return StreamUtils.fromBuffer(Buffer.from(buffer));
18+
static empty(): NodeJS.ReadableStream {
19+
return Readable.from([]);
1920
}
2021

2122
static fromBuffer(buffer: Buffer): NodeJS.ReadableStream {
22-
const readable = StreamUtils.create();
23-
readable.push(buffer);
24-
StreamUtils.endStream(readable);
25-
return readable;
23+
return Readable.from(buffer);
2624
}
2725

28-
static endStream(readable: stream.Readable): void {
29-
readable.push(null);
26+
static fromArrayBuffer(buffer: ArrayBuffer): NodeJS.ReadableStream {
27+
return StreamUtils.fromBuffer(Buffer.from(buffer));
3028
}
3129
}

0 commit comments

Comments
 (0)