Am Dienstag, 17. Dezember 2013 02:44:04 UTC+1 schrieb Stephen Bartell: > > On the writable end, I push a `JSON.stringify`d object. > On the other reader end, can I expect `chunk` to be `JSON.parse` -able? > by the fast look into the code it looks like its normal behavior. but there is no guaratees documented, so i wouldn't rely on that.
> > When running the code below, it seems like it. Like, it works and I can't > logically make sense of it. I would expect that if not in objectMode, the > reader will need to buffer things until some sentinel pattern is found > which will tell me it's safe to call JSON.parse. > > Could someone shed some light on why this works and if I can rely on this > in practice? > the buffering happens by pushing the chunk as is to an array (used as queue). so the chunks in your example are just like as you push them in the queue. so everytime your transform reads from the readable it gets the same chunk as before (not exactly, it gets the buffer made from it). as i said, there is no gurantee, that it will stay like this, since it's not documented. Plus if you make this asumtion, your consume stream is less robust nd less reusable. you couldnt plug your transform in a pipeline, where it gets json data from http or tcp source. > > ``` > var Readable = require('stream').Readable; > var Transform = require('stream').Transform > > printer = new Transform() > printer._transform = function (chunk, enc, next) { > console.log(JSON.parse(chunk)) // <- safe to assume I can JSON.parse > this? > this.push(chunk) > next() > } > > var rs = new Readable > rs.push(JSON.stringify({foo: 'bar'})) > rs.push(JSON.stringify({foo: 'bar'})) > rs.push(JSON.stringify({foo: 'bar'})) > rs.push(null) > > rs.pipe(printer) > > ``` > -- -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.