@smithy/core 3.29.0 → 3.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ const { createHmac, createHash, getRandomValues } = require("node:crypto");
2
2
  const { ReadStream, lstatSync, fstatSync } = require("node:fs");
3
3
  const { HttpResponse } = require("@smithy/core/transport");
4
4
  const { toEndpointV1 } = require("@smithy/core/endpoints");
5
- const { Duplex, Readable, Writable, PassThrough } = require("node:stream");
5
+ const { Readable, Writable, PassThrough } = require("node:stream");
6
6
 
7
7
  const isArrayBuffer = (arg) => (typeof ArrayBuffer === "function" && arg instanceof ArrayBuffer) ||
8
8
  Object.prototype.toString.call(arg) === "[object ArrayBuffer]";
@@ -987,62 +987,66 @@ function castSourceData(toCast, encoding) {
987
987
  return fromArrayBuffer(toCast);
988
988
  }
989
989
 
990
- let ChecksumStream$1 = class ChecksumStream extends Duplex {
990
+ let ChecksumStream$1 = class ChecksumStream extends Readable {
991
991
  expectedChecksum;
992
992
  checksumSourceLocation;
993
993
  checksum;
994
994
  source;
995
995
  base64Encoder;
996
- pendingCallback = null;
997
996
  constructor({ expectedChecksum, checksum, source, checksumSourceLocation, base64Encoder, }) {
998
997
  super();
999
- if (typeof source.pipe === "function") {
1000
- this.source = source;
1001
- }
1002
- else {
998
+ if (typeof source.pipe !== "function") {
1003
999
  throw new Error(`@smithy/util-stream: unsupported source type ${source?.constructor?.name ?? source} in ChecksumStream.`);
1004
1000
  }
1001
+ this.source = source;
1005
1002
  this.base64Encoder = base64Encoder ?? toBase64$1;
1006
1003
  this.expectedChecksum = expectedChecksum;
1007
1004
  this.checksum = checksum;
1008
1005
  this.checksumSourceLocation = checksumSourceLocation;
1009
- this.source.pipe(this);
1006
+ this.source.on("data", this.onSourceData);
1007
+ this.source.on("end", this.onSourceEnd);
1008
+ this.source.on("error", this.onSourceError);
1009
+ this.source.pause();
1010
1010
  }
1011
- _read(size) {
1012
- if (this.pendingCallback) {
1013
- const callback = this.pendingCallback;
1014
- this.pendingCallback = null;
1015
- callback();
1011
+ onSourceData = (chunk) => {
1012
+ if (this.destroyed) {
1013
+ return;
1016
1014
  }
1017
- }
1018
- _write(chunk, encoding, callback) {
1019
1015
  try {
1020
1016
  this.checksum.update(chunk);
1021
- const canPushMore = this.push(chunk);
1022
- if (!canPushMore) {
1023
- this.pendingCallback = callback;
1024
- return;
1025
- }
1026
1017
  }
1027
1018
  catch (e) {
1028
- return callback(e);
1019
+ this.destroy(e);
1020
+ return;
1021
+ }
1022
+ if (!this.push(chunk)) {
1023
+ this.source.pause();
1024
+ }
1025
+ };
1026
+ onSourceEnd = async () => {
1027
+ if (this.destroyed) {
1028
+ return;
1029
1029
  }
1030
- return callback();
1031
- }
1032
- async _final(callback) {
1033
1030
  try {
1034
1031
  const digest = await this.checksum.digest();
1035
1032
  const received = this.base64Encoder(digest);
1036
1033
  if (this.expectedChecksum !== received) {
1037
- return callback(new Error(`Checksum mismatch: expected "${this.expectedChecksum}" but received "${received}"` +
1034
+ this.destroy(new Error(`Checksum mismatch: expected "${this.expectedChecksum}" but received "${received}"` +
1038
1035
  ` in response header "${this.checksumSourceLocation}".`));
1036
+ return;
1039
1037
  }
1040
1038
  }
1041
1039
  catch (e) {
1042
- return callback(e);
1040
+ this.destroy(e);
1041
+ return;
1043
1042
  }
1044
1043
  this.push(null);
1045
- return callback();
1044
+ };
1045
+ onSourceError = (error) => {
1046
+ this.destroy(error);
1047
+ };
1048
+ _read(size) {
1049
+ this.source.resume();
1046
1050
  }
1047
1051
  _destroy(error, callback) {
1048
1052
  this.source?.destroy();
@@ -1,61 +1,65 @@
1
- import { Duplex } from "node:stream";
1
+ import { Readable } from "node:stream";
2
2
  import { toBase64 } from "../../util-base64/toBase64";
3
- export class ChecksumStream extends Duplex {
3
+ export class ChecksumStream extends Readable {
4
4
  expectedChecksum;
5
5
  checksumSourceLocation;
6
6
  checksum;
7
7
  source;
8
8
  base64Encoder;
9
- pendingCallback = null;
10
9
  constructor({ expectedChecksum, checksum, source, checksumSourceLocation, base64Encoder, }) {
11
10
  super();
12
- if (typeof source.pipe === "function") {
13
- this.source = source;
14
- }
15
- else {
11
+ if (typeof source.pipe !== "function") {
16
12
  throw new Error(`@smithy/util-stream: unsupported source type ${source?.constructor?.name ?? source} in ChecksumStream.`);
17
13
  }
14
+ this.source = source;
18
15
  this.base64Encoder = base64Encoder ?? toBase64;
19
16
  this.expectedChecksum = expectedChecksum;
20
17
  this.checksum = checksum;
21
18
  this.checksumSourceLocation = checksumSourceLocation;
22
- this.source.pipe(this);
19
+ this.source.on("data", this.onSourceData);
20
+ this.source.on("end", this.onSourceEnd);
21
+ this.source.on("error", this.onSourceError);
22
+ this.source.pause();
23
23
  }
24
- _read(size) {
25
- if (this.pendingCallback) {
26
- const callback = this.pendingCallback;
27
- this.pendingCallback = null;
28
- callback();
24
+ onSourceData = (chunk) => {
25
+ if (this.destroyed) {
26
+ return;
29
27
  }
30
- }
31
- _write(chunk, encoding, callback) {
32
28
  try {
33
29
  this.checksum.update(chunk);
34
- const canPushMore = this.push(chunk);
35
- if (!canPushMore) {
36
- this.pendingCallback = callback;
37
- return;
38
- }
39
30
  }
40
31
  catch (e) {
41
- return callback(e);
32
+ this.destroy(e);
33
+ return;
34
+ }
35
+ if (!this.push(chunk)) {
36
+ this.source.pause();
37
+ }
38
+ };
39
+ onSourceEnd = async () => {
40
+ if (this.destroyed) {
41
+ return;
42
42
  }
43
- return callback();
44
- }
45
- async _final(callback) {
46
43
  try {
47
44
  const digest = await this.checksum.digest();
48
45
  const received = this.base64Encoder(digest);
49
46
  if (this.expectedChecksum !== received) {
50
- return callback(new Error(`Checksum mismatch: expected "${this.expectedChecksum}" but received "${received}"` +
47
+ this.destroy(new Error(`Checksum mismatch: expected "${this.expectedChecksum}" but received "${received}"` +
51
48
  ` in response header "${this.checksumSourceLocation}".`));
49
+ return;
52
50
  }
53
51
  }
54
52
  catch (e) {
55
- return callback(e);
53
+ this.destroy(e);
54
+ return;
56
55
  }
57
56
  this.push(null);
58
- return callback();
57
+ };
58
+ onSourceError = (error) => {
59
+ this.destroy(error);
60
+ };
61
+ _read(size) {
62
+ this.source.resume();
59
63
  }
60
64
  _destroy(error, callback) {
61
65
  this.source?.destroy();
@@ -1,4 +1,4 @@
1
- import { Duplex, type Readable } from "node:stream";
1
+ import { Readable } from "node:stream";
2
2
  import type { Checksum, Encoder } from "@smithy/types";
3
3
  /**
4
4
  * @internal
@@ -29,34 +29,42 @@ export interface ChecksumStreamInit<T extends Readable | ReadableStream> {
29
29
  * Wrapper for throwing checksum errors for streams without
30
30
  * buffering the stream.
31
31
  *
32
+ * Note: this effectively behaves as a duplex, reading from the source on one
33
+ * side and forwarding chunks to its own readable side on the other. It should
34
+ * not be rewritten back into a Duplex (or Transform). The source is observed
35
+ * and driven manually (pause/resume in onSourceData/_read) so data is pulled
36
+ * at the rate it is consumed and never buffered twice; this manual control is
37
+ * used deliberately for performance and would be lost with the built-in duplex
38
+ * machinery.
39
+ *
32
40
  * @internal
33
41
  */
34
- export declare class ChecksumStream extends Duplex {
42
+ export declare class ChecksumStream extends Readable {
35
43
  private expectedChecksum;
36
44
  private checksumSourceLocation;
37
45
  private checksum;
38
- private source?;
46
+ private source;
39
47
  private base64Encoder;
40
- private pendingCallback;
41
48
  constructor({ expectedChecksum, checksum, source, checksumSourceLocation, base64Encoder, }: ChecksumStreamInit<Readable>);
42
49
  /**
43
- * Do not call this directly.
44
- * @internal
50
+ * Update the checksum and forward each source chunk to the readable side,
51
+ * pausing the source when the readable side signals backpressure.
45
52
  */
46
- _read(size: number): void;
53
+ private onSourceData;
47
54
  /**
48
- * When the upstream source flows data to this stream,
49
- * calculate a step update of the checksum.
50
- * Do not call this directly.
51
- * @internal
55
+ * When the source finishes, perform the checksum comparison and end this stream.
56
+ */
57
+ private onSourceEnd;
58
+ /**
59
+ * Surface source errors on this stream.
52
60
  */
53
- _write(chunk: Buffer, encoding: string, callback: (err?: Error) => void): void;
61
+ private onSourceError;
54
62
  /**
55
- * When the upstream source finishes, perform the checksum comparison.
63
+ * Resume the source so it flows at the rate this stream is consumed.
56
64
  * Do not call this directly.
57
65
  * @internal
58
66
  */
59
- _final(callback: (err?: Error) => void): Promise<void>;
67
+ _read(size: number): void;
60
68
  /**
61
69
  * Destroy the upstream source for cleanup so it is not left dangling, then
62
70
  * complete this stream's destruction. The error is intentionally not forwarded
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smithy/core",
3
- "version": "3.29.0",
3
+ "version": "3.29.1",
4
4
  "scripts": {
5
5
  "benchmark:cbor": "node ./scripts/cbor-perf.mjs",
6
6
  "benchmark:checksum": "node ./scripts/checksum-perf.mjs",