Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top