javascript - Unknown encoding: buffer when pushing to stream - Stack Overflow

I'm implementing a simple transform stream that reads a string from a stream, operates on it, and

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

The '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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信