最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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 è.

发布评论

评论列表(0)

  1. 暂无评论