I'm implementing a simple transform stream that reads a string from a stream, operates on it, and then spits it out. My implementation of _transform
(the important part) looks like:
_transform = function (chunk, enc, done) {
var x,
str = chunk.toString();
for (x = 0; x < str.length; x++) {
this.push(doTransformation(str.charCodeAt(x)), enc);
}
done();
};
Right now, doTransformation
will just return return the character from that char code, so this is effectively just this.push(str[x])
. When testing this using:
process.stdin
.pipe(explosionPillsTransform())
.pipe(process.stdout)
I get the error
TypeError: Unknown encoding: buffer
This seems to be because enc
is buffer. Actually everything works perfectly if I remove the enc
argument from this.push
, as in:
this.push(doTransformation(str.charCodeAt(x)))
... but some examples that I've seen have used this second argument. Is there a good way for me to know when to use the encoding argument or when to omit it?
I'm implementing a simple transform stream that reads a string from a stream, operates on it, and then spits it out. My implementation of _transform
(the important part) looks like:
_transform = function (chunk, enc, done) {
var x,
str = chunk.toString();
for (x = 0; x < str.length; x++) {
this.push(doTransformation(str.charCodeAt(x)), enc);
}
done();
};
Right now, doTransformation
will just return return the character from that char code, so this is effectively just this.push(str[x])
. When testing this using:
process.stdin
.pipe(explosionPillsTransform())
.pipe(process.stdout)
I get the error
TypeError: Unknown encoding: buffer
This seems to be because enc
is buffer. Actually everything works perfectly if I remove the enc
argument from this.push
, as in:
this.push(doTransformation(str.charCodeAt(x)))
... but some examples that I've seen have used this second argument. Is there a good way for me to know when to use the encoding argument or when to omit it?
Share Improve this question asked Apr 30, 2014 at 0:02 Explosion PillsExplosion Pills 192k55 gold badges340 silver badges416 bronze badges2 Answers
Reset to default 5The 'buffer' encoding is redundant if chunk
is already a Buffer. If you are calling push()
with a string, then you need to specify the correct encoding. If you're passing in a Buffer instead, then you can drop the enc
if it's set to 'buffer'.
From the node.js documentation, the second argument to Stream#push() should be
encoding String Encoding of String chunks. Must be a valid Buffer encoding, such as 'utf8' or 'ascii'
I would remend always passing the encoding - if you actually know what it is. Otherwise, someone, somewhere is going to have to guess whether that byte sequence "0xC3 0xA8" should have been "è" or è.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744309236a4567867.html
评论列表(0条)