Skip to content

Commit 76df645

Browse files
authored
fix(hash-stream-node): throw error if non-file readableStream is flowing (#3341)
1 parent 0ad6f73 commit 76df645

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/hash-stream-node/src/readableStreamHasher.spec.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ describe(readableStreamHasher.name, () => {
5353
});
5454

5555
it("creates a copy in case of fileStream", () => {
56-
(fsCreateReadStream as jest.Mock).mockReturnValue(
57-
new Readable({
58-
read: (size) => {},
59-
})
60-
);
56+
(fsCreateReadStream as jest.Mock).mockReturnValue(new Readable({ read: (size) => {} }));
6157
(isFileStream as unknown as jest.Mock).mockReturnValue(true);
6258

6359
const fsReadStream = createReadStream(__filename);
@@ -68,9 +64,7 @@ describe(readableStreamHasher.name, () => {
6864
});
6965

7066
it("computes hash for a readable stream", async () => {
71-
const readableStream = new Readable({
72-
read: (size) => {},
73-
});
67+
const readableStream = new Readable({ read: (size) => {} });
7468
const hashPromise = readableStreamHasher(mockHashCtor, readableStream);
7569

7670
// @ts-ignore Property '_readableState' does not exist on type 'Readable'.
@@ -92,6 +86,20 @@ describe(readableStreamHasher.name, () => {
9286
expect(mockHashCalculatorEnd).toHaveBeenCalledTimes(1);
9387
});
9488

89+
it("throws if readable stream is not a file stream and has started reading", async () => {
90+
const readableStream = new Readable({ read: (size) => {} });
91+
// Simulate readableFlowing to true.
92+
readableStream.resume();
93+
94+
const expectedError = new Error("Unable to calculate hash for flowing readable stream");
95+
try {
96+
readableStreamHasher(mockHashCtor, readableStream);
97+
fail(`expected ${expectedError}`);
98+
} catch (error) {
99+
expect(error).toStrictEqual(expectedError);
100+
}
101+
});
102+
95103
it("throws error if readable stream throws error", async () => {
96104
const readableStream = new Readable({
97105
read: (size) => {},

packages/hash-stream-node/src/readableStreamHasher.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { HashCalculator } from "./HashCalculator";
66
import { isFileStream } from "./isFileStream";
77

88
export const readableStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, readableStream: Readable) => {
9-
// ToDo: throw if readableStream is already flowing and it's copy can't be created.
9+
// Throw if readableStream is already flowing and it's not a file stream.
10+
if (!isFileStream(readableStream) && readableStream.readableFlowing !== null) {
11+
throw new Error("Unable to calculate hash for flowing readable stream");
12+
}
13+
1014
const streamToPipe = isFileStream(readableStream) ? fsCreateReadStream(readableStream) : readableStream;
1115

1216
const hash = new hashCtor();

0 commit comments

Comments
 (0)