From 27a9112d359348223d0935354d977df6ba7a5b95 Mon Sep 17 00:00:00 2001 From: L3P3 Date: Wed, 2 May 2018 00:36:00 +0200 Subject: [PATCH 1/4] L3P3ify StreamCache This implementation is a noticeable bit faster than the original one and it fixes some problems like incorrect behaviour of the methods. And in my opinion, it is still a very understandable code. --- lib/StreamCache.js | 83 ++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/lib/StreamCache.js b/lib/StreamCache.js index a795e2a..00808b3 100644 --- a/lib/StreamCache.js +++ b/lib/StreamCache.js @@ -8,50 +8,77 @@ function StreamCache() { this.writable = true; this.readable = true; - + + //contains just one buffer if ended this._buffers = []; + //is set to null if ended this._dests = []; - this._ended = false; } StreamCache.prototype.write = function(buffer) { this._buffers.push(buffer); - this._dests.forEach(function(dest) { - dest.write(buffer); - }); + const dests=this._dests; + var i=dests.length; + while(i--) + dests[i].write(buffer); + + return true; }; StreamCache.prototype.pipe = function(dest, options) { - if (options) { + if (options===undefined){} + else throw Error('StreamCache#pipe: options are not supported yet.'); - } - - this._buffers.forEach(function(buffer) { - dest.write(buffer); - }); - - if (this._ended) { - dest.end(); - return dest; - } - - this._dests.push(dest); + + //TODO try to asynchronize buffer flow, since with this implementation, + // it blocks execution until all data is written out + + const buffers=this._buffers,dests=this._dests; + if (dests===null) + return dest.end(buffers[0]); + + for(var i=0,l=buffers.length;i Date: Wed, 2 May 2018 00:44:33 +0200 Subject: [PATCH 2/4] Added ending check to write() aswell --- lib/StreamCache.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/StreamCache.js b/lib/StreamCache.js index 00808b3..1cf29fa 100644 --- a/lib/StreamCache.js +++ b/lib/StreamCache.js @@ -16,9 +16,13 @@ function StreamCache() { } StreamCache.prototype.write = function(buffer) { + const dests=this._dests; + if (dests!==null){} + else + throw Error('StreamCache#write: stream already ended.'); + this._buffers.push(buffer); - const dests=this._dests; var i=dests.length; while(i--) dests[i].write(buffer); From 013cd52dc3e41ad7536258f7e28ceed4c2df3d9c Mon Sep 17 00:00:00 2001 From: L3P3 Date: Wed, 2 May 2018 15:13:01 +0200 Subject: [PATCH 3/4] I am happy with that. 100% compatible with the original StreamCache module but more modern and faster. --- lib/StreamCache.js | 180 ++++++++++++++++++++++++--------------------- 1 file changed, 95 insertions(+), 85 deletions(-) diff --git a/lib/StreamCache.js b/lib/StreamCache.js index 1cf29fa..dd75378 100644 --- a/lib/StreamCache.js +++ b/lib/StreamCache.js @@ -1,88 +1,98 @@ -var Util = require('util'); -var Stream = require('stream').Stream; +"use strict"; -module.exports = StreamCache; -Util.inherits(StreamCache, Stream); -function StreamCache() { - Stream.call(this); - - this.writable = true; - this.readable = true; - - //contains just one buffer if ended - this._buffers = []; - //is set to null if ended - this._dests = []; +class StreamCache extends require('stream').Stream{ + constructor(){ + super(); + + this.writable=true; + this.readable=true; + + //contains just one buffer if ended + this._buffers=[]; + //is set to null if ended + this._dests=[]; + } + + write(buffer){ + const dests=this._dests; + if(dests!==null){} + else + throw Error('stream already ended'); + + if(buffer.constructor===Buffer){} + else + throw Error('buffer expected'); + + this._buffers.push(buffer); + + var i=dests.length; + while(i--) + dests[i].write(buffer); + + return true; + } + + end(buffer){ + const dests=this._dests; + if(buffer===undefined){ + if(dests!==null){} + else + throw Error('stream already ended'); + } + else + this.write(buffer); + + var i=dests.length; + while(i--) + dests[i].end(); + + this._dests=null; + + //merge all buffers into one since there will be no more being added + //(saves memory and loopings in future pipe calls) + this._buffers=[ + Buffer.concat(this._buffers) + ]; + + return this; + } + + pipe(dest,options){ + if(options===undefined){} + else + throw Error('options not supported'); + + //TODO try to asynchronize buffer flow, since with this implementation, + // it blocks execution until all data is written out + + const buffers=this._buffers,dests=this._dests; + if(dests===null) + return dest.end(buffers[0]); + + for(var i=0,l=buffers.length;i Date: Sat, 11 Aug 2018 11:02:56 +0200 Subject: [PATCH 4/4] Precalculate length Optimize getLength from O(n) to O(1). --- lib/StreamCache.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/StreamCache.js b/lib/StreamCache.js index dd75378..9009af6 100644 --- a/lib/StreamCache.js +++ b/lib/StreamCache.js @@ -7,6 +7,8 @@ class StreamCache extends require('stream').Stream{ this.writable=true; this.readable=true; + this.length=0; + //contains just one buffer if ended this._buffers=[]; //is set to null if ended @@ -24,6 +26,7 @@ class StreamCache extends require('stream').Stream{ throw Error('buffer expected'); this._buffers.push(buffer); + this.length+=buffer.length; var i=dests.length; while(i--) @@ -77,18 +80,6 @@ class StreamCache extends require('stream').Stream{ return dest; } - get length(){ - if(this._dests===null) - return this._buffers[0].length; - - const buffers=this._buffers; - var l=0,i=buffers.length; - while(i--) - l+=buffers[i].length; - - return l; - } - //deprecated getLength(){ return this.length;