Skip to content

Commit c34a49d

Browse files
committed
review 2
1 parent 7376cfc commit c34a49d

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

lib/auth/streamingV4/trailingChecksumTransform.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class TrailingChecksumTransform extends Transform {
1616
this.log = log;
1717
this.errCb = errCb;
1818
this.chunkSizeBuffer = Buffer.alloc(0);
19-
this.outputBuffer = [];
2019
this.bytesToDiscard = 0; // when trailing \r\n are present, we discard them but they can be in different chunks
2120
this.bytesToRead = 0; // when a chunk is advertised, the size is put here and we forward all bytes
2221
this.streamClosed = false;
@@ -42,7 +41,7 @@ class TrailingChecksumTransform extends Transform {
4241
// forward up to bytesToRead bytes from the chunk, restart processing on leftover
4342
if (this.bytesToRead > 0) {
4443
const toRead = Math.min(this.bytesToRead, chunk.byteLength);
45-
this.outputBuffer.push(chunk.subarray(0, toRead));
44+
this.push(chunk.subarray(0, toRead));
4645
chunk = chunk.subarray(toRead);
4746
this.bytesToRead -= toRead;
4847
if (this.bytesToRead === 0) {
@@ -64,13 +63,19 @@ class TrailingChecksumTransform extends Transform {
6463
}
6564
// no delimiter, we'll keep the chunk for later
6665
this.chunkSizeBuffer = Buffer.concat([this.chunkSizeBuffer, chunk]);
67-
break;
66+
return callback();
6867
}
6968

7069
this.chunkSizeBuffer = Buffer.concat([this.chunkSizeBuffer, chunk.subarray(0, lineBreakIndex2)]);
7170
chunk = chunk.subarray(lineBreakIndex2);
7271

7372
// chunk-size is sent in hex
73+
if (!/^[0-9a-fA-F]+$/.test(this.chunkSizeBuffer.toString())) {
74+
this.log.debug('chunk size is not a valid hex number', {
75+
chunkSizeBuffer: this.chunkSizeBuffer.toString('hex'),
76+
});
77+
return callback(errors.InvalidArgument);
78+
}
7479
const dataSize = Number.parseInt(this.chunkSizeBuffer.toString(), 16);
7580
if (Number.isNaN(dataSize)) {
7681
this.log.debug('unable to parse chunk size', {
@@ -88,10 +93,7 @@ class TrailingChecksumTransform extends Transform {
8893
this.bytesToDiscard = 2;
8994
}
9095

91-
// TODO: push the stream into a checksum accumulator
92-
const toFlush = Buffer.concat(this.outputBuffer);
93-
this.outputBuffer = [];
94-
return callback(null, toFlush, encoding);
96+
return callback();
9597
}
9698
}
9799

tests/unit/auth/TrailingChecksumTransform.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,26 @@ describe('TrailingChecksumTransform class', () => {
106106
});
107107
});
108108
});
109+
110+
it('should correctly remove checksums, cut at each individual byte', done => {
111+
const trailingChecksumTransform = new TrailingChecksumTransform(log, err => {
112+
assert.strictEqual(err, null);
113+
});
114+
const chunks = [];
115+
for (let i = 0; i < objDataWithTrailingChecksum.length - 1; i++) {
116+
chunks.push(objDataWithTrailingChecksum.substring(i, i + 1));
117+
}
118+
const chunkedReader = new ChunkedReader(chunks);
119+
chunkedReader.pipe(trailingChecksumTransform);
120+
const outputChunks = [];
121+
trailingChecksumTransform.on('data', (chunk) => outputChunks.push(Buffer.from(chunk)));
122+
trailingChecksumTransform.on('finish', () => {
123+
const data = Buffer.concat(outputChunks).toString();
124+
assert.strictEqual(data, objDataWithoutTrailingChecksum);
125+
done();
126+
});
127+
trailingChecksumTransform.on('error', err => {
128+
assert.ifError(err);
129+
});
130+
});
109131
});

0 commit comments

Comments
 (0)