diff --git a/src/packet/literal.js b/src/packet/literal.js index ce275952..6ee33a74 100644 --- a/src/packet/literal.js +++ b/src/packet/literal.js @@ -64,12 +64,11 @@ function normalize(text) { * @returns {String} literal data as text */ Literal.prototype.getText = function() { - if (this.text !== null) { - return this.text; - } - let lastChar = ''; - this.text = this.data.transform((done, value) => { - if (!done) { + let text; + if (this.text === null) { + let lastChar = ''; + [this.data, this.text] = this.data.tee(); + this.text = stream.transform(this.text, value => { const text = lastChar + util.Uint8Array_to_str(value); // decode UTF8 and normalize EOL to \n const normalized = normalize(text); @@ -81,11 +80,10 @@ Literal.prototype.getText = function() { // else, store the last character for the next chunk in case it's \r or half an UTF8 sequence lastChar = text[text.length - 1]; return normalized.slice(0, -1); - } else { - return lastChar; - } - }); - return this.text; + }, () => lastChar); + } + [text, this.text] = this.text.tee(); + return text; }; /** diff --git a/src/util.js b/src/util.js index d9c799e3..641d8bcd 100644 --- a/src/util.js +++ b/src/util.js @@ -226,15 +226,17 @@ export default { * @returns {Uint8Array} An array of 8-bit integers */ str_to_Uint8Array: function (str) { - if (!util.isString(str)) { - throw new Error('str_to_Uint8Array: Data must be in the form of a string'); - } + return stream.transform(str, str => { + if (!util.isString(str)) { + throw new Error('str_to_Uint8Array: Data must be in the form of a string'); + } - const result = new Uint8Array(str.length); - for (let i = 0; i < str.length; i++) { - result[i] = str.charCodeAt(i); - } - return result; + const result = new Uint8Array(str.length); + for (let i = 0; i < str.length; i++) { + result[i] = str.charCodeAt(i); + } + return result; + }); }, /** @@ -260,7 +262,7 @@ export default { * @returns {String} A valid squence of utf8 bytes */ encode_utf8: function (str) { - return unescape(encodeURIComponent(str)); + return stream.transform(str, value => unescape(encodeURIComponent(value))); }, /** @@ -626,7 +628,11 @@ export default { * Normalize line endings to \r\n */ canonicalizeEOL: function(text) { - return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n"); + return stream.transform(text, (done, value) => { + if (!done) { + return value.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n"); + } + }); }, /**