Skip to content

Commit b4a0554

Browse files
committed
separate files
1 parent 483742d commit b4a0554

File tree

8 files changed

+166
-159
lines changed

8 files changed

+166
-159
lines changed

packages/util-stream/karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = function (config) {
66
files: [
77
"src/checksum/createChecksumStream.browser.spec.ts",
88
"src/checksum/createChecksumStream.browser.ts",
9+
"src/checksum/ChecksumStream.browser.ts",
910
"src/getAwsChunkedEncodingStream.browser.spec.ts",
1011
"src/getAwsChunkedEncodingStream.browser.ts",
1112
"src/headStream.browser.ts",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Checksum, Encoder } from "@smithy/types";
2+
3+
/**
4+
* @internal
5+
*/
6+
export interface ChecksumStreamInit {
7+
/**
8+
* Base64 value of the expected checksum.
9+
*/
10+
expectedChecksum: string;
11+
/**
12+
* For error messaging, the location from which the checksum value was read.
13+
*/
14+
checksumSourceLocation: string;
15+
/**
16+
* The checksum calculator.
17+
*/
18+
checksum: Checksum;
19+
/**
20+
* The stream to be checked.
21+
*/
22+
source: ReadableStream;
23+
24+
/**
25+
* Optional base 64 encoder if calling from a request context.
26+
*/
27+
base64Encoder?: Encoder;
28+
}
29+
30+
const ReadableStreamRef = typeof ReadableStream === "function" ? ReadableStream : function (): void {};
31+
32+
/**
33+
* This stub exists so that the readable returned by createChecksumStream
34+
* identifies as "ChecksumStream" in alignment with the Node.js
35+
* implementation.
36+
*
37+
* @extends ReadableStream
38+
*/
39+
export class ChecksumStream extends (ReadableStreamRef as any) {}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { Checksum, Encoder } from "@smithy/types";
2+
import { toBase64 } from "@smithy/util-base64";
3+
import { Duplex, Readable } from "stream";
4+
5+
/**
6+
* @internal
7+
*/
8+
export interface ChecksumStreamInit<T extends Readable | ReadableStream> {
9+
/**
10+
* Base64 value of the expected checksum.
11+
*/
12+
expectedChecksum: string;
13+
/**
14+
* For error messaging, the location from which the checksum value was read.
15+
*/
16+
checksumSourceLocation: string;
17+
/**
18+
* The checksum calculator.
19+
*/
20+
checksum: Checksum;
21+
/**
22+
* The stream to be checked.
23+
*/
24+
source: T;
25+
26+
/**
27+
* Optional base 64 encoder if calling from a request context.
28+
*/
29+
base64Encoder?: Encoder;
30+
}
31+
32+
/**
33+
* @internal
34+
*
35+
* Wrapper for throwing checksum errors for streams without
36+
* buffering the stream.
37+
*
38+
*/
39+
export class ChecksumStream extends Duplex {
40+
private expectedChecksum: string;
41+
private checksumSourceLocation: string;
42+
private checksum: Checksum;
43+
private source?: Readable;
44+
private base64Encoder: Encoder;
45+
46+
public constructor({
47+
expectedChecksum,
48+
checksum,
49+
source,
50+
checksumSourceLocation,
51+
base64Encoder,
52+
}: ChecksumStreamInit<Readable>) {
53+
super();
54+
if (typeof (source as Readable).pipe === "function") {
55+
this.source = source as Readable;
56+
} else {
57+
throw new Error(
58+
`@smithy/util-stream: unsupported source type ${source?.constructor?.name ?? source} in ChecksumStream.`
59+
);
60+
}
61+
62+
this.base64Encoder = base64Encoder ?? toBase64;
63+
this.expectedChecksum = expectedChecksum;
64+
this.checksum = checksum;
65+
this.checksumSourceLocation = checksumSourceLocation;
66+
67+
// connect this stream to the end of the source stream.
68+
this.source.pipe(this);
69+
}
70+
71+
/**
72+
* @internal do not call this directly.
73+
*/
74+
public _read(
75+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
76+
size: number
77+
): void {}
78+
79+
/**
80+
* @internal do not call this directly.
81+
*
82+
* When the upstream source flows data to this stream,
83+
* calculate a step update of the checksum.
84+
*/
85+
public _write(chunk: Buffer, encoding: string, callback: (err?: Error) => void): void {
86+
try {
87+
this.checksum.update(chunk);
88+
this.push(chunk);
89+
} catch (e: unknown) {
90+
return callback(e as Error);
91+
}
92+
return callback();
93+
}
94+
95+
/**
96+
* @internal do not call this directly.
97+
*
98+
* When the upstream source finishes, perform the checksum comparison.
99+
*/
100+
public async _final(callback: (err?: Error) => void): Promise<void> {
101+
try {
102+
const digest: Uint8Array = await this.checksum.digest();
103+
const received = this.base64Encoder(digest);
104+
if (this.expectedChecksum !== received) {
105+
return callback(
106+
new Error(
107+
`Checksum mismatch: expected "${this.expectedChecksum}" but received "${received}"` +
108+
` in response header "${this.checksumSourceLocation}".`
109+
)
110+
);
111+
}
112+
} catch (e: unknown) {
113+
return callback(e as Error);
114+
}
115+
this.push(null);
116+
return callback();
117+
}
118+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { toBase64 } from "@smithy/util-base64";
33
import { toUtf8 } from "@smithy/util-utf8";
44

55
import { headStream } from "../headStream.browser";
6+
import { ChecksumStream as ChecksumStreamWeb } from "./ChecksumStream.browser";
67
import { createChecksumStream } from "./createChecksumStream.browser";
7-
import { ChecksumStream as ChecksumStreamWeb } from "./createChecksumStream.browser";
88

99
describe("Checksum streams", () => {
1010
/**

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,10 @@
1-
import { Checksum, Encoder } from "@smithy/types";
21
import { toBase64 } from "@smithy/util-base64";
32

43
import { isReadableStream } from "../stream-type-check";
4+
import { ChecksumStream, ChecksumStreamInit } from "./ChecksumStream.browser";
55

66
/**
77
* @internal
8-
*/
9-
export interface ChecksumStreamInit {
10-
/**
11-
* Base64 value of the expected checksum.
12-
*/
13-
expectedChecksum: string;
14-
/**
15-
* For error messaging, the location from which the checksum value was read.
16-
*/
17-
checksumSourceLocation: string;
18-
/**
19-
* The checksum calculator.
20-
*/
21-
checksum: Checksum;
22-
/**
23-
* The stream to be checked.
24-
*/
25-
source: ReadableStream;
26-
27-
/**
28-
* Optional base 64 encoder if calling from a request context.
29-
*/
30-
base64Encoder?: Encoder;
31-
}
32-
33-
/**
348
* Alias prevents compiler from turning
359
* ReadableStream into ReadableStream<any>, which is incompatible
3610
* with the NodeJS.ReadableStream global type.
@@ -106,14 +80,3 @@ export const createChecksumStream = ({
10680
Object.setPrototypeOf(readable, ChecksumStream.prototype);
10781
return readable;
10882
};
109-
110-
const ReadableStreamRef = typeof ReadableStream === "function" ? ReadableStream : function (): void {};
111-
112-
/**
113-
* This stub exists so that the readable returned by createChecksumStream
114-
* identifies as "ChecksumStream" in alignment with the Node.js
115-
* implementation.
116-
*
117-
* @extends ReadableStream
118-
*/
119-
export class ChecksumStream extends (ReadableStreamRef as any) {}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { toUtf8 } from "@smithy/util-utf8";
44
import { Readable } from "stream";
55

66
import { headStream } from "../headStream";
7-
import { ChecksumStream, createChecksumStream } from "./createChecksumStream";
8-
import { ChecksumStream as ChecksumStreamWeb } from "./createChecksumStream.browser";
7+
import { ChecksumStream } from "./ChecksumStream";
8+
import { ChecksumStream as ChecksumStreamWeb } from "./ChecksumStream.browser";
9+
import { createChecksumStream } from "./createChecksumStream";
910

1011
describe("Checksum streams", () => {
1112
/**
Lines changed: 2 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
1-
import { Checksum, Encoder } from "@smithy/types";
2-
import { toBase64 } from "@smithy/util-base64";
3-
import { Duplex, Readable } from "stream";
1+
import { Readable } from "stream";
42

53
import { isReadableStream } from "../stream-type-check";
4+
import { ChecksumStream, ChecksumStreamInit } from "./ChecksumStream";
65
import { createChecksumStream as createChecksumStreamWeb, ReadableStreamType } from "./createChecksumStream.browser";
76

8-
/**
9-
* @internal
10-
*/
11-
export interface ChecksumStreamInit<T extends Readable | ReadableStream> {
12-
/**
13-
* Base64 value of the expected checksum.
14-
*/
15-
expectedChecksum: string;
16-
/**
17-
* For error messaging, the location from which the checksum value was read.
18-
*/
19-
checksumSourceLocation: string;
20-
/**
21-
* The checksum calculator.
22-
*/
23-
checksum: Checksum;
24-
/**
25-
* The stream to be checked.
26-
*/
27-
source: T;
28-
29-
/**
30-
* Optional base 64 encoder if calling from a request context.
31-
*/
32-
base64Encoder?: Encoder;
33-
}
34-
357
/**
368
* @internal
379
*
@@ -48,91 +20,3 @@ export function createChecksumStream(
4820
}
4921
return new ChecksumStream(init as ChecksumStreamInit<Readable>);
5022
}
51-
52-
/**
53-
* @internal
54-
*
55-
* Wrapper for throwing checksum errors for streams without
56-
* buffering the stream.
57-
*
58-
*/
59-
export class ChecksumStream extends Duplex {
60-
private expectedChecksum: string;
61-
private checksumSourceLocation: string;
62-
private checksum: Checksum;
63-
private source?: Readable;
64-
private base64Encoder: Encoder;
65-
66-
public constructor({
67-
expectedChecksum,
68-
checksum,
69-
source,
70-
checksumSourceLocation,
71-
base64Encoder,
72-
}: ChecksumStreamInit<Readable>) {
73-
super();
74-
if (typeof (source as Readable).pipe === "function") {
75-
this.source = source as Readable;
76-
} else {
77-
throw new Error(
78-
`@smithy/util-stream: unsupported source type ${source?.constructor?.name ?? source} in ChecksumStream.`
79-
);
80-
}
81-
82-
this.base64Encoder = base64Encoder ?? toBase64;
83-
this.expectedChecksum = expectedChecksum;
84-
this.checksum = checksum;
85-
this.checksumSourceLocation = checksumSourceLocation;
86-
87-
// connect this stream to the end of the source stream.
88-
this.source.pipe(this);
89-
}
90-
91-
/**
92-
* @internal do not call this directly.
93-
*/
94-
public _read(
95-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
96-
size: number
97-
): void {}
98-
99-
/**
100-
* @internal do not call this directly.
101-
*
102-
* When the upstream source flows data to this stream,
103-
* calculate a step update of the checksum.
104-
*/
105-
public _write(chunk: Buffer, encoding: string, callback: (err?: Error) => void): void {
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();
113-
}
114-
115-
/**
116-
* @internal do not call this directly.
117-
*
118-
* When the upstream source finishes, perform the checksum comparison.
119-
*/
120-
public async _final(callback: (err?: Error) => void): Promise<void> {
121-
try {
122-
const digest: Uint8Array = await this.checksum.digest();
123-
const received = this.base64Encoder(digest);
124-
if (this.expectedChecksum !== received) {
125-
return callback(
126-
new Error(
127-
`Checksum mismatch: expected "${this.expectedChecksum}" but received "${received}"` +
128-
` in response header "${this.checksumSourceLocation}".`
129-
)
130-
);
131-
}
132-
} catch (e: unknown) {
133-
return callback(e as Error);
134-
}
135-
this.push(null);
136-
return callback();
137-
}
138-
}

packages/util-stream/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from "./splitStream";
55
export * from "./headStream";
66
export * from "./stream-type-check";
77
export * from "./checksum/createChecksumStream";
8+
export * from "./checksum/ChecksumStream";

0 commit comments

Comments
 (0)