Skip to content

Commit

Permalink
chore(hash-stream-node): move fileStreamHasher to own file (#3086)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Dec 9, 2021
1 parent b0e1422 commit dd66d1a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 42 deletions.
@@ -1,4 +1,4 @@
import { HashCalculator } from "./hash-calculator";
import { HashCalculator } from "./HashCalculator";

function createMockHash(): {
updates: Buffer[];
Expand Down
Expand Up @@ -5,7 +5,7 @@ import { tmpdir } from "os";
import { join } from "path";
import { Readable } from "stream";

import { fileStreamHasher } from "./index";
import { fileStreamHasher } from "./fileStreamHasher";

function createTemporaryFile(contents: string): string {
const folder = mkdtempSync(join(tmpdir(), "sha256-stream-node-"));
Expand Down
34 changes: 34 additions & 0 deletions packages/hash-stream-node/src/fileStreamHasher.ts
@@ -0,0 +1,34 @@
import { HashConstructor, StreamHasher } from "@aws-sdk/types";
import { createReadStream, ReadStream } from "fs";
import { Readable } from "stream";

import { HashCalculator } from "./HashCalculator";

export const fileStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, fileStream: Readable) =>
new Promise((resolve, reject) => {
if (!isReadStream(fileStream)) {
reject(new Error("Unable to calculate hash for non-file streams."));
return;
}

const fileStreamTee = createReadStream(fileStream.path, {
start: (fileStream as any).start,
end: (fileStream as any).end,
});

const hash = new hashCtor();
const hashCalculator = new HashCalculator(hash);

fileStreamTee.pipe(hashCalculator);
fileStreamTee.on("error", (err: any) => {
// if the source errors, the destination stream needs to manually end
hashCalculator.end();
reject(err);
});
hashCalculator.on("error", reject);
hashCalculator.on("finish", function (this: HashCalculator) {
hash.digest().then(resolve).catch(reject);
});
});

const isReadStream = (stream: Readable): stream is ReadStream => typeof (stream as ReadStream).path === "string";
41 changes: 1 addition & 40 deletions packages/hash-stream-node/src/index.ts
@@ -1,40 +1 @@
import { HashConstructor, StreamHasher } from "@aws-sdk/types";
import { createReadStream, ReadStream } from "fs";
import { Readable } from "stream";

import { HashCalculator } from "./hash-calculator";

export const fileStreamHasher: StreamHasher<Readable> = function fileStreamHasher(
hashCtor: HashConstructor,
fileStream: Readable
): Promise<Uint8Array> {
return new Promise((resolve, reject) => {
if (!isReadStream(fileStream)) {
reject(new Error("Unable to calculate hash for non-file streams."));
return;
}

const fileStreamTee = createReadStream(fileStream.path, {
start: (fileStream as any).start,
end: (fileStream as any).end,
});

const hash = new hashCtor();
const hashCalculator = new HashCalculator(hash);

fileStreamTee.pipe(hashCalculator);
fileStreamTee.on("error", (err: any) => {
// if the source errors, the destination stream needs to manually end
hashCalculator.end();
reject(err);
});
hashCalculator.on("error", reject);
hashCalculator.on("finish", function (this: HashCalculator) {
hash.digest().then(resolve).catch(reject);
});
});
};

function isReadStream(stream: Readable): stream is ReadStream {
return typeof (stream as ReadStream).path === "string";
}
export * from "./fileStreamHasher";

0 comments on commit dd66d1a

Please sign in to comment.