-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(hash-stream-node): move fileStreamHasher to own file (#3086)
- Loading branch information
Showing
5 changed files
with
37 additions
and
42 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...h-stream-node/src/hash-calculator.spec.ts → ...sh-stream-node/src/HashCalculator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |