From 368b6585ba070c2556749c94606ca861d26d0f42 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Wed, 24 Nov 2021 20:00:05 +0100 Subject: [PATCH] Now using internal buffer-system --- src/streamHandler/RegexBlockStream.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/streamHandler/RegexBlockStream.js b/src/streamHandler/RegexBlockStream.js index 120e65f..62dadfe 100644 --- a/src/streamHandler/RegexBlockStream.js +++ b/src/streamHandler/RegexBlockStream.js @@ -1,3 +1,4 @@ +const logger = require.main.require("./helper/logger.js")("RegexBlockStream"); const { Transform } = require('stream') /** @@ -22,12 +23,11 @@ class RegexBlockStream extends Transform{ this.matcher = matcher; this.withholdLastBlock = withholdLastBlock; this.matchAllOnFlush = matchAllOnFlush; - - this._buffer = ""; } _transform(chunk, encoding, next){ - chunk = this._buffer + chunk; // Add previous buffer to current chunk + chunk = this.readableBuffer.length? this.readableBuffer.join() + chunk: chunk; // Add previous buffer to current chunk + this.readableBuffer.length && this.readableBuffer.clear(); // Clear buffer once we read it let matches = chunk.match(this.matcher); // Match if(matches){ @@ -35,7 +35,7 @@ class RegexBlockStream extends Transform{ chunk = this._writeMatches(matches, chunk); } - this._buffer = chunk; // Store remaining data in buffer + this.readableBuffer.push(chunk); // Store remaining data in buffer next(); // Get next chunk } @@ -51,8 +51,9 @@ class RegexBlockStream extends Transform{ _flush(next){ if(matchAllOnFlush){ // When requested, we'll match one last time over the remaining buffer - let matches = this._buffer.match(this.matcher); // Match remaining buffer - _writeMatches(this._buffer); // Write matches including last element + let chunk = this.readableBuffer.toString(); + let matches = chunk.match(this.matcher); // Match remaining buffer + _writeMatches(matches); // Write matches including last element } next(); // Tell system we are done