From df2b0b8d9f13c9e4c9ffa034c0e5ffa126f7d095 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Fri, 29 Nov 2024 11:10:29 +0200 Subject: [PATCH] Process the entire input as a single value --- lib/libqp.js | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/lib/libqp.js b/lib/libqp.js index 937543e..ec821b1 100644 --- a/lib/libqp.js +++ b/lib/libqp.js @@ -255,6 +255,7 @@ class Encoder extends Transform { /** * Creates a transform stream for decoding Quoted-Printable encoded strings + * The input is not actually processed as a stream but concatted and processed as a single input * * @constructor * @param {Object} options Stream options @@ -270,42 +271,32 @@ class Decoder extends Transform { this.inputBytes = 0; this.outputBytes = 0; + + this.qpChunks = []; } _transform(chunk, encoding, done) { - let qp, buf; - - chunk = chunk.toString('ascii'); - if (!chunk || !chunk.length) { return done(); } - this.inputBytes += chunk.length; - - qp = this._curLine + chunk; - this._curLine = ''; - qp = qp.replace(/[\t ]*(?:=[^\n]?)?$/, lastLine => { - this._curLine = lastLine; - return ''; - }); - - if (qp) { - buf = decode(qp); - this.outputBytes += buf.length; - this.push(buf); + if (typeof chunk === 'string') { + chunk = Buffer.from(chunk, encoding); } + this.qpChunks.push(chunk); + this.inputBytes += chunk.length; + done(); } _flush(done) { - let buf; - if (this._curLine) { - buf = decode(this._curLine); + if (this.inputBytes) { + let buf = decode(Buffer.concat(this.qpChunks, this.inputBytes).toString()); this.outputBytes += buf.length; this.push(buf); } + done(); } }