-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hash-stream-node): add readableStreamHasher (#3088)
- Loading branch information
Showing
4 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./fileStreamHasher"; | ||
export * from "./readableStreamHasher"; |
140 changes: 140 additions & 0 deletions
140
packages/hash-stream-node/src/readableStreamHasher.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { Hash } from "@aws-sdk/types"; | ||
import { Readable, Writable, WritableOptions } from "stream"; | ||
|
||
import { HashCalculator } from "./HashCalculator"; | ||
import { readableStreamHasher } from "./readableStreamHasher"; | ||
|
||
jest.mock("./HashCalculator"); | ||
|
||
describe(readableStreamHasher.name, () => { | ||
const mockDigest = jest.fn(); | ||
const mockHashCtor = jest.fn().mockImplementation(() => ({ | ||
update: jest.fn(), | ||
digest: mockDigest, | ||
})); | ||
|
||
const mockHashCalculatorWrite = jest.fn(); | ||
const mockHashCalculatorEnd = jest.fn(); | ||
|
||
const mockHash = new Uint8Array(Buffer.from("mockHash")); | ||
|
||
class MockHashCalculator extends Writable { | ||
constructor(public readonly hash: Hash, public readonly mockWrite, public readonly mockEnd) { | ||
super(); | ||
} | ||
|
||
_write(chunk: Buffer, encoding: string, callback: (err?: Error) => void) { | ||
this.mockWrite(chunk); | ||
callback(); | ||
} | ||
|
||
end() { | ||
this.mockEnd(); | ||
super.end(); | ||
} | ||
} | ||
|
||
beforeEach(() => { | ||
(HashCalculator as unknown as jest.Mock).mockImplementation( | ||
(hash) => new MockHashCalculator(hash, mockHashCalculatorWrite, mockHashCalculatorEnd) | ||
); | ||
mockDigest.mockResolvedValue(mockHash); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("computes hash for a readable stream", async () => { | ||
const readableStream = new Readable({ | ||
read: (size) => {}, | ||
}); | ||
const hashPromise = readableStreamHasher(mockHashCtor, readableStream); | ||
|
||
// @ts-ignore Property '_readableState' does not exist on type 'Readable'. | ||
const { pipesCount } = readableStream._readableState; | ||
expect(pipesCount).toEqual(1); | ||
|
||
const mockDataChunks = ["Hello", "World"]; | ||
setTimeout(() => { | ||
mockDataChunks.forEach((chunk) => readableStream.emit("data", chunk)); | ||
readableStream.emit("end"); | ||
}, 100); | ||
|
||
expect(await hashPromise).toEqual(mockHash); | ||
expect(mockHashCalculatorWrite).toHaveBeenCalledTimes(mockDataChunks.length); | ||
mockDataChunks.forEach((chunk, index) => | ||
expect(mockHashCalculatorWrite).toHaveBeenNthCalledWith(index + 1, Buffer.from(chunk)) | ||
); | ||
expect(mockDigest).toHaveBeenCalledTimes(1); | ||
expect(mockHashCalculatorEnd).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("throws error if readable stream throws error", async () => { | ||
const readableStream = new Readable({ | ||
read: (size) => {}, | ||
}); | ||
const hashPromise = readableStreamHasher(mockHashCtor, readableStream); | ||
|
||
const mockError = new Error("error"); | ||
setTimeout(() => { | ||
readableStream.emit("error", mockError); | ||
}, 100); | ||
|
||
try { | ||
await hashPromise; | ||
fail(`should throw error ${mockError}`); | ||
} catch (error) { | ||
expect(error).toEqual(mockError); | ||
expect(mockHashCalculatorEnd).toHaveBeenCalledTimes(1); | ||
} | ||
}); | ||
|
||
it("throws error if HashCalculator throws error", async () => { | ||
const mockHashCalculator = new MockHashCalculator( | ||
mockHashCtor as any, | ||
mockHashCalculatorWrite, | ||
mockHashCalculatorEnd | ||
); | ||
(HashCalculator as unknown as jest.Mock).mockImplementation((hash) => mockHashCalculator); | ||
|
||
const readableStream = new Readable({ | ||
read: (size) => {}, | ||
}); | ||
const hashPromise = readableStreamHasher(mockHashCtor, readableStream); | ||
|
||
const mockError = new Error("error"); | ||
setTimeout(() => { | ||
mockHashCalculator.emit("error", mockError); | ||
}, 100); | ||
|
||
try { | ||
await hashPromise; | ||
fail(`should throw error ${mockError}`); | ||
} catch (error) { | ||
expect(error).toEqual(mockError); | ||
} | ||
}); | ||
|
||
it("throws error if hash.digest() throws error", async () => { | ||
const readableStream = new Readable({ | ||
read: (size) => {}, | ||
}); | ||
const hashPromise = readableStreamHasher(mockHashCtor, readableStream); | ||
|
||
setTimeout(() => { | ||
readableStream.emit("end"); | ||
}, 100); | ||
|
||
const mockError = new Error("error"); | ||
mockDigest.mockRejectedValue(mockError); | ||
|
||
try { | ||
await hashPromise; | ||
fail(`should throw error ${mockError}`); | ||
} catch (error) { | ||
expect(error).toEqual(mockError); | ||
expect(mockHashCalculatorEnd).toHaveBeenCalledTimes(1); | ||
} | ||
}); | ||
}); |
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,22 @@ | ||
import { HashConstructor, StreamHasher } from "@aws-sdk/types"; | ||
import { Readable } from "stream"; | ||
|
||
import { HashCalculator } from "./HashCalculator"; | ||
|
||
export const readableStreamHasher: StreamHasher<Readable> = (hashCtor: HashConstructor, readableStream: Readable) => { | ||
const hash = new hashCtor(); | ||
const hashCalculator = new HashCalculator(hash); | ||
readableStream.pipe(hashCalculator); | ||
|
||
return new Promise((resolve, reject) => { | ||
readableStream.on("error", (err: Error) => { | ||
// if the source errors, the destination stream needs to manually end | ||
hashCalculator.end(); | ||
reject(err); | ||
}); | ||
hashCalculator.on("error", reject); | ||
hashCalculator.on("finish", () => { | ||
hash.digest().then(resolve).catch(reject); | ||
}); | ||
}); | ||
}; |