Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HashCalculator } from "./hash-calculator";
import { HashCalculator } from "./HashCalculator";

function createMockHash(): {
updates: Buffer[];
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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";