/
readableStreamHasher.spec.ts
170 lines (140 loc) · 5.24 KB
/
readableStreamHasher.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Hash } from "@aws-sdk/types";
import { createReadStream } from "fs";
import { Readable, Writable } from "stream";
import { fsCreateReadStream } from "./fsCreateReadStream";
import { HashCalculator } from "./HashCalculator";
import { isFileStream } from "./isFileStream";
import { readableStreamHasher } from "./readableStreamHasher";
jest.mock("./fsCreateReadStream");
jest.mock("./HashCalculator");
jest.mock("./isFileStream");
jest.mock("fs");
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)
);
(isFileStream as unknown as jest.Mock).mockReturnValue(false);
mockDigest.mockResolvedValue(mockHash);
});
afterEach(() => {
jest.clearAllMocks();
});
it("creates a copy in case of fileStream", () => {
(fsCreateReadStream as jest.Mock).mockReturnValue(new Readable({ read: (size) => {} }));
(isFileStream as unknown as jest.Mock).mockReturnValue(true);
const fsReadStream = createReadStream(__filename);
readableStreamHasher(mockHashCtor, fsReadStream);
expect(isFileStream).toHaveBeenCalledWith(fsReadStream);
expect(fsCreateReadStream).toHaveBeenCalledWith(fsReadStream);
});
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 if readable stream is not a file stream and has started reading", async () => {
const readableStream = new Readable({ read: (size) => {} });
// Simulate readableFlowing to true.
readableStream.resume();
const expectedError = new Error("Unable to calculate hash for flowing readable stream");
try {
readableStreamHasher(mockHashCtor, readableStream);
fail(`expected ${expectedError}`);
} catch (error) {
expect(error).toStrictEqual(expectedError);
}
});
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);
}
});
});