Hello all,
I have an issue with read streams in nodejs 0.10.1, possibly due to my
misunderstanding of the new api.
Working through an example in the "Node Cookbook" book, the following code
attempts to find a synchro pattern (0xfffb) in a mp3 file. Once found, the
stream is "destroyed", and subsequent data is not parsed. Also, the "end"
event is not emitted.
function findBitRate(f, cb) {
var str = fs.createReadStream(f);
str.on('data', function (data) {
var i;
for (i = 0; i < data.length; i += 2) {
console.log('Now at ' + i);
if (data.readUInt16LE(i) === 64511) {
this.destroy(); // prevents 'end' event from being triggered
cb(null, bitrates[data.toString('hex', i + 2, i + 3)[0]]);
break;
}
}
});
str.on('end', function () {
cb(new Error('could not find bitrate, is this definitely an MPEG-1
MP3?'));
});
}
This example works as "advertised" in nodejs 0.8.22.
Now, using this code snippet verbatim with nodejs 0.10.1, it fails. I had
expected that nodejs 0.10.1 would use the legacy stream api (since I had
left the stream 'data' handler), and behave identically, but the behaviour
is different. The callback (cb) is executed twice.
(the reason being that two chunks are emitted as two 'data' events, and
there is a synchro pattern 0xfffb in each of these chunks) Obviously, the
stream was not destroyed.
Now, if I attempt to use the new stream api, by modifying the code as
follows:
function findBitRate(f, cb) {
var str = fs.createReadStream(f);
str.on('readable', function () {
var i;
var data = this.read();
(... rest of the code unchanged...)
the problem is exactly the same! The callback is also executed twice. So
the stream isn't destroyed either.
Has anybody hints about these behaviours? Especially, what would be the
right way of stopping emission of "readable" events (instead of destroy(),
maybe?)
Thanks a lot for your feedback.
Erwin
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.