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

fix(hash-stream-node): throw error if non-file readableStream is flowing #3341

Merged
merged 1 commit into from
Feb 22, 2022
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
24 changes: 16 additions & 8 deletions packages/hash-stream-node/src/readableStreamHasher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ describe(readableStreamHasher.name, () => {
});

it("creates a copy in case of fileStream", () => {
(fsCreateReadStream as jest.Mock).mockReturnValue(
new Readable({
read: (size) => {},
})
);
(fsCreateReadStream as jest.Mock).mockReturnValue(new Readable({ read: (size) => {} }));
(isFileStream as unknown as jest.Mock).mockReturnValue(true);

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

it("computes hash for a readable stream", async () => {
const readableStream = new Readable({
read: (size) => {},
});
const readableStream = new Readable({ read: (size) => {} });
const hashPromise = readableStreamHasher(mockHashCtor, readableStream);

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

it("throws if readable stream is not a file stream and has started reading", async () => {
const readableStream = new Readable({ read: (size) => {} });
// Simulate readableFlowing to true.
readableStream.resume();

const expectedError = new Error("Unable to calculate hash for flowing readable stream");
try {
readableStreamHasher(mockHashCtor, readableStream);
fail(`expected ${expectedError}`);
} catch (error) {
expect(error).toStrictEqual(expectedError);
}
});

it("throws error if readable stream throws error", async () => {
const readableStream = new Readable({
read: (size) => {},
Expand Down
6 changes: 5 additions & 1 deletion packages/hash-stream-node/src/readableStreamHasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { HashCalculator } from "./HashCalculator";
import { isFileStream } from "./isFileStream";

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

const streamToPipe = isFileStream(readableStream) ? fsCreateReadStream(readableStream) : readableStream;

const hash = new hashCtor();
Expand Down