Skip to content

Commit dd66d1a

Browse files
authored
chore(hash-stream-node): move fileStreamHasher to own file (#3086)
1 parent b0e1422 commit dd66d1a

File tree

5 files changed

+37
-42
lines changed

5 files changed

+37
-42
lines changed

packages/hash-stream-node/src/hash-calculator.spec.ts renamed to packages/hash-stream-node/src/HashCalculator.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HashCalculator } from "./hash-calculator";
1+
import { HashCalculator } from "./HashCalculator";
22

33
function createMockHash(): {
44
updates: Buffer[];

packages/hash-stream-node/src/index.spec.ts renamed to packages/hash-stream-node/src/fileStreamHasher.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { tmpdir } from "os";
55
import { join } from "path";
66
import { Readable } from "stream";
77

8-
import { fileStreamHasher } from "./index";
8+
import { fileStreamHasher } from "./fileStreamHasher";
99

1010
function createTemporaryFile(contents: string): string {
1111
const folder = mkdtempSync(join(tmpdir(), "sha256-stream-node-"));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { HashConstructor, StreamHasher } from "@aws-sdk/types";
2+
import { createReadStream, ReadStream } from "fs";
3+
import { Readable } from "stream";
4+
5+
import { HashCalculator } from "./HashCalculator";
6+
7+
export const fileStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, fileStream: Readable) =>
8+
new Promise((resolve, reject) => {
9+
if (!isReadStream(fileStream)) {
10+
reject(new Error("Unable to calculate hash for non-file streams."));
11+
return;
12+
}
13+
14+
const fileStreamTee = createReadStream(fileStream.path, {
15+
start: (fileStream as any).start,
16+
end: (fileStream as any).end,
17+
});
18+
19+
const hash = new hashCtor();
20+
const hashCalculator = new HashCalculator(hash);
21+
22+
fileStreamTee.pipe(hashCalculator);
23+
fileStreamTee.on("error", (err: any) => {
24+
// if the source errors, the destination stream needs to manually end
25+
hashCalculator.end();
26+
reject(err);
27+
});
28+
hashCalculator.on("error", reject);
29+
hashCalculator.on("finish", function (this: HashCalculator) {
30+
hash.digest().then(resolve).catch(reject);
31+
});
32+
});
33+
34+
const isReadStream = (stream: Readable): stream is ReadStream => typeof (stream as ReadStream).path === "string";
Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1 @@
1-
import { HashConstructor, StreamHasher } from "@aws-sdk/types";
2-
import { createReadStream, ReadStream } from "fs";
3-
import { Readable } from "stream";
4-
5-
import { HashCalculator } from "./hash-calculator";
6-
7-
export const fileStreamHasher: StreamHasher<Readable> = function fileStreamHasher(
8-
hashCtor: HashConstructor,
9-
fileStream: Readable
10-
): Promise<Uint8Array> {
11-
return new Promise((resolve, reject) => {
12-
if (!isReadStream(fileStream)) {
13-
reject(new Error("Unable to calculate hash for non-file streams."));
14-
return;
15-
}
16-
17-
const fileStreamTee = createReadStream(fileStream.path, {
18-
start: (fileStream as any).start,
19-
end: (fileStream as any).end,
20-
});
21-
22-
const hash = new hashCtor();
23-
const hashCalculator = new HashCalculator(hash);
24-
25-
fileStreamTee.pipe(hashCalculator);
26-
fileStreamTee.on("error", (err: any) => {
27-
// if the source errors, the destination stream needs to manually end
28-
hashCalculator.end();
29-
reject(err);
30-
});
31-
hashCalculator.on("error", reject);
32-
hashCalculator.on("finish", function (this: HashCalculator) {
33-
hash.digest().then(resolve).catch(reject);
34-
});
35-
});
36-
};
37-
38-
function isReadStream(stream: Readable): stream is ReadStream {
39-
return typeof (stream as ReadStream).path === "string";
40-
}
1+
export * from "./fileStreamHasher";

0 commit comments

Comments
 (0)