Skip to content

Commit

Permalink
chore: pipe readableStream when hasher is called
Browse files Browse the repository at this point in the history
The pipe will get created before await/then is called on Promise.
This way hashCalculator will consume Readable Stream if it starts
flowing before await is called.
  • Loading branch information
trivikr committed Dec 10, 2021
1 parent 1898ee1 commit bbd7642
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/hash-stream-node/src/readableStreamHasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Readable } from "stream";

import { HashCalculator } from "./HashCalculator";

export const readableStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, readableStream: Readable) =>
new Promise((resolve, reject) => {
const hash = new hashCtor();
const hashCalculator = new HashCalculator(hash);
export const readableStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, readableStream: Readable) => {
const hash = new hashCtor();
const hashCalculator = new HashCalculator(hash);
readableStream.pipe(hashCalculator);

readableStream.pipe(hashCalculator);
return new Promise((resolve, reject) => {
readableStream.on("error", (err: Error) => {
// if the source errors, the destination stream needs to manually end
hashCalculator.end();
Expand All @@ -19,3 +19,4 @@ export const readableStreamHasher: StreamHasher<Readable> = (hashCtor: HashConst
hash.digest().then(resolve).catch(reject);
});
});
};

0 comments on commit bbd7642

Please sign in to comment.