Last active
August 29, 2015 13:56
-
-
Save xdenser/8944752 to your computer and use it in GitHub Desktop.
Weird behaviour of CatchStream implementation. It seems made by NodeJS recomendations. Intended to read growing file, but sometimes chunks order is swapped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
fs = require('fs'), | |
path =require('path'), | |
len = 64*1024, | |
qlen = (len >> 2), | |
testFile = path.join(__dirname,'testFile'); | |
/* | |
Generate test file with growing bytes | |
*/ | |
function gen(){ | |
var | |
stream = fs.createWriteStream(testFile), | |
buffer = new Buffer(len), | |
i = 0; | |
(function writeChunk(skipSlowdown){ | |
if(i>255) return stream.end(); | |
if(!i%10 && !skipSlowdown) return setTimeout(writeChunk.bind(this,true),100); | |
buffer.fill(i++,0,qlen); | |
buffer.fill(i++,qlen,2*qlen); | |
buffer.fill(i++,2*qlen,3*qlen); | |
buffer.fill(i++,3*qlen); | |
stream.write(buffer,writeChunk); | |
})(); | |
return stream; | |
} | |
// this variant of read is working ok | |
var rs = 0,lr; | |
function read(){ | |
var stream = fs.createReadStream(testFile,{start: rs}); | |
stream.on('readable',function(){ | |
var chunk; | |
while(chunk=stream.read(qlen)){ | |
if(lr && lr>chunk[0]) console.log('WTF1') | |
rs+=chunk.length; | |
lr = chunk[0]; | |
} | |
}); | |
stream.on('end',function(){ | |
if(rs<qlen*256) read(); | |
else console.log('ended',rs); | |
}); | |
} | |
// gen(); | |
// read(); | |
// THE PROBLEMS BEGIN HERE | |
var | |
stream = require('stream'), | |
util = require('util'); | |
util.inherits(CatchStream,stream.Readable); | |
function CatchStream(fileName,writeStream,start){ | |
stream.Readable.call(this); | |
console.log(fileName); | |
this._fileName = fileName; | |
console.log(this._fileName); | |
this._offset = start; | |
this._writeStream = writeStream; | |
this._writeStream.on('finish',function(){ | |
console.log('writeStream finish'); | |
this._writeStreamFinished = true; | |
this._writeStreamBytesWritten = this._writeStream.bytesWritten; | |
}.bind(this)) | |
} | |
CatchStream.prototype._read = function(n){ | |
var chunk; | |
this._needToReadAgain = null; | |
if(!this._stream){ | |
this._createStream(); | |
} | |
if(this._stream) { | |
chunk = this._stream.read(n); | |
} | |
if(chunk){ | |
this.push(chunk); | |
// here we test if everything ok | |
if(this._prevTestValue){ | |
if(chunk[0]<this._prevTestValue) console.log('WTF!',this._offset); | |
//else console.log('Ok'); | |
} | |
this._prevTestValue = chunk[0]; | |
this._offset += chunk.length; | |
return; | |
} | |
if(this._lastStreamFinished){ | |
console.log('read Stream Finished'); | |
this.push(null); | |
return ; | |
} | |
this._needToReadAgain = n; | |
} | |
CatchStream.prototype._createStream = function(){ | |
var last; | |
if(!this._stream){ | |
last = this._writeStreamFinished; | |
this._stream = fs.createReadStream(this._fileName,{start: this._offset}); | |
this._stream.on('readable',function(){ | |
if(this._needToReadAgain != null) this._read(this._needToReadAgain); | |
}.bind(this)); | |
this._stream.on('end',function(){ | |
this._stream = null; | |
this._lastStreamFinished = last; | |
if(this._needToReadAgain != null) this._read(this._needToReadAgain); | |
}.bind(this)); | |
} | |
} | |
var cs = new CatchStream(testFile,gen(),0); | |
cs.on('data',function(){}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks I have added 'return' before setTimeout.
I also have added read function which reads growing stream w/o problems.