Skip to content

Commit bbd7642

Browse files
committed
chore: pipe readableStream when hasher is called
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.
1 parent 1898ee1 commit bbd7642

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { Readable } from "stream";
33

44
import { HashCalculator } from "./HashCalculator";
55

6-
export const readableStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, readableStream: Readable) =>
7-
new Promise((resolve, reject) => {
8-
const hash = new hashCtor();
9-
const hashCalculator = new HashCalculator(hash);
6+
export const readableStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, readableStream: Readable) => {
7+
const hash = new hashCtor();
8+
const hashCalculator = new HashCalculator(hash);
9+
readableStream.pipe(hashCalculator);
1010

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

0 commit comments

Comments
 (0)