From b3b52fce624a8071ada3e16ff2da5f329135e96b Mon Sep 17 00:00:00 2001 From: Jonas Hermsmeier Date: Fri, 17 Nov 2017 21:37:49 +0100 Subject: [PATCH] fix(writer): Don't calculate checksum before block-stream (#1858) The checksum-stream being situated in front of the block-stream, which ensures block size alignment to multiples of 512, and pads the last block, caused the checksum to be incorrectly calculated for images where the last block needed to be padded. Change-Type: patch --- lib/writer/checksum-stream.js | 22 +++++++++++++++++++--- lib/writer/index.js | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/writer/checksum-stream.js b/lib/writer/checksum-stream.js index 7aa05c22..1e9585b8 100644 --- a/lib/writer/checksum-stream.js +++ b/lib/writer/checksum-stream.js @@ -121,7 +121,7 @@ class ChecksumStream extends stream.Transform { }) } - return this.pipe(hash) + return hash } /** @@ -134,8 +134,24 @@ class ChecksumStream extends stream.Transform { * checksumStream.write(buffer) */ _transform (chunk, encoding, next) { - this.push(chunk) - next() + for (let index = 0; index < this.hashes.length; index += 1) { + this.hashes[index].write(chunk) + } + next(null, chunk) + } + + /** + * @summary End the hash streams once this stream ends + * @private + * @param {Function} done - callback + * @example + * checksumStream.end() + */ + _flush (done) { + for (let index = 0; index < this.hashes.length; index += 1) { + this.hashes[index].end() + } + done() } } diff --git a/lib/writer/index.js b/lib/writer/index.js index dba5aa6b..9f7680e2 100644 --- a/lib/writer/index.js +++ b/lib/writer/index.js @@ -206,11 +206,12 @@ class ImageWriter extends EventEmitter { } else { debug('write:blockstream') const checksumStream = new ChecksumStream({ + objectMode: true, algorithms: options.checksumAlgorithms }) + pipeline.append(new BlockStream()) pipeline.append(checksumStream) pipeline.bind(checksumStream, 'checksum') - pipeline.append(new BlockStream()) } const target = new BlockWriteStream({