Hi everyone,
I'm a bit confused about how the streams2 Readble implementation using
push() interacts with the on("data") event. Consider these examples. When I
push 3 times in succession on a Readable, I get one data event with the
concatenated output, but when I pipe the readable into a Transform, those
chunks are yielded on at a time.
This is important since certain streams e.g. split() require "data" to
place chunk boundaries in certain places. Which APIs do I use to ensure
boundaries stay where they should be?
The code:
var stream = require("stream"),
util = require("util")
var R = function() {
stream.Readable.call(this)
this.setEncoding("utf8")
}
util.inherits(R, stream.Readable)
R.prototype._read = function() {
this.push("a")
this.push("b")
this.push("c")
this.push(null)
}
var T = function() {
stream.Transform.call(this)
this.setEncoding("utf8")
}
util.inherits(T, stream.Transform)
T.prototype._transform = function(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase())
callback()
}
var r = new R(), rd = []
r.on("data", rd.push.bind(rd))
r.on("end", function() {
console.log(rd) // -> [ 'abc ']
})
var r = new R(), t = new T(), td = []
var x = r.pipe(t)
x.on("data", td.push.bind(td))
x.on("end", function() {
console.log(td) // -> [ 'A', 'B', 'C' ]
})
--
James Coglan
http://jcoglan.com
+44 (0) 7771512510
--
--
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.