Skip to content

Commit

Permalink
Process the entire input as a single value
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Nov 29, 2024
1 parent 1ed3f83 commit df2b0b8
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions lib/libqp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
Expand Down

0 comments on commit df2b0b8

Please sign in to comment.