Skip to content

Commit 4597803

Browse files
committed
move TransformStream checksum to flush event
1 parent 868d7ac commit 4597803

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

packages/util-stream/src/checksum/createChecksumStream.browser.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ export interface ChecksumStreamInit {
3535
*/
3636
export type ReadableStreamType = ReadableStream;
3737

38+
/**
39+
* This is a local copy of
40+
* https://developer.mozilla.org/en-US/docs/Web/API/TransformStreamDefaultController
41+
* in case users do not have this type.
42+
*/
43+
interface TransformStreamDefaultController {
44+
enqueue(chunk: any): void;
45+
error(error: unknown): void;
46+
terminate(): void;
47+
}
48+
3849
/**
3950
* @internal
4051
*
@@ -64,33 +75,28 @@ export const createChecksumStream = ({
6475

6576
const transform = new TransformStream({
6677
start() {},
67-
transform: async (chunk: any, controller: TransformStreamDefaultController) => {
68-
/**
69-
* When the upstream source finishes, perform the checksum comparison.
70-
*/
71-
if (null === chunk) {
72-
const digest: Uint8Array = await checksum.digest();
73-
const received = encoder(digest);
74-
75-
if (expectedChecksum !== received) {
76-
const error = new Error(
77-
`Checksum mismatch: expected "${expectedChecksum}" but received "${received}"` +
78-
` in response header "${checksumSourceLocation}".`
79-
);
80-
controller.error(error);
81-
throw error;
82-
}
83-
controller.terminate();
84-
return;
85-
}
78+
async transform(chunk: any, controller: TransformStreamDefaultController) {
8679
/**
8780
* When the upstream source flows data to this stream,
8881
* calculate a step update of the checksum.
8982
*/
9083
checksum.update(chunk);
9184
controller.enqueue(chunk);
9285
},
93-
flush() {},
86+
async flush(controller: TransformStreamDefaultController) {
87+
const digest: Uint8Array = await checksum.digest();
88+
const received = encoder(digest);
89+
90+
if (expectedChecksum !== received) {
91+
const error = new Error(
92+
`Checksum mismatch: expected "${expectedChecksum}" but received "${received}"` +
93+
` in response header "${checksumSourceLocation}".`
94+
);
95+
controller.error(error);
96+
} else {
97+
controller.terminate();
98+
}
99+
},
94100
});
95101

96102
source.pipeThrough(transform);

packages/util-stream/src/checksum/createChecksumStream.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe("Checksum streams", () => {
4343
});
4444

4545
expect(checksumStream.constructor.name).toEqual("ChecksumStream");
46+
expect(checksumStream).toBeInstanceOf(Readable);
4647

4748
const collected = toUtf8(await headStream(checksumStream, Infinity));
4849
expect(collected).toEqual(canonicalUtf8);
@@ -84,7 +85,6 @@ describe("Checksum streams", () => {
8485
canonicalData.forEach((byte) => {
8586
controller.enqueue(new Uint8Array([byte]));
8687
});
87-
controller.enqueue(null);
8888
controller.close();
8989
},
9090
});

packages/util-stream/src/checksum/createChecksumStream.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,36 @@ class ChecksumStream extends Duplex {
103103
* calculate a step update of the checksum.
104104
*/
105105
public _write(chunk: Buffer, encoding: string, callback: (err?: Error) => void): void {
106-
this.checksum.update(chunk);
107-
this.push(chunk);
108-
callback();
106+
try {
107+
this.checksum.update(chunk);
108+
this.push(chunk);
109+
} catch (e: unknown) {
110+
return callback(e as Error);
111+
}
112+
return callback();
109113
}
110114

111115
/**
112116
* @internal do not call this directly.
113117
*
114118
* When the upstream source finishes, perform the checksum comparison.
115119
*/
116-
public async _final(callback: (err?: Error) => void) {
120+
public async _final(callback: (err?: Error) => void): Promise<void> {
117121
try {
118122
const digest: Uint8Array = await this.checksum.digest();
119123
const received = this.base64Encoder(digest);
120124
if (this.expectedChecksum !== received) {
121-
callback(
125+
return callback(
122126
new Error(
123127
`Checksum mismatch: expected "${this.expectedChecksum}" but received "${received}"` +
124128
` in response header "${this.checksumSourceLocation}".`
125129
)
126130
);
127131
}
128132
} catch (e: unknown) {
129-
callback(e as Error);
133+
return callback(e as Error);
130134
}
131135
this.push(null);
132-
callback();
136+
return callback();
133137
}
134138
}

0 commit comments

Comments
 (0)