Skip to content

Instantly share code, notes, and snippets.

@chbrown
Created May 28, 2013 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chbrown/5665509 to your computer and use it in GitHub Desktop.
Save chbrown/5665509 to your computer and use it in GitHub Desktop.
Node.js stream.Transform bug / oddity.
var util = require('util');
var stream = require('stream');
var fs = require('fs');
// # 1. With objectMode: true
var ObjectParser = function(opts) {
stream.Transform.call(this, {
decodeStrings: true, // Writable option, ensure _transform always gets a Buffer
objectMode: true // Readable option, .read(n) should return a single value, rather than a Buffer
});
};
util.inherits(ObjectParser, stream.Transform);
ObjectParser.prototype._transform = function(chunk, encoding, done) {
console.log('chunk=', chunk, 'encoding=', encoding);
done();
};
var obj_parser2 = new ObjectParser();
fs.createReadStream('transformerrs.js').pipe(obj_parser2);
// output -> chunk= <Buffer 76 61 72 20 75 74 69 6c 20 3d 20 ...> encoding= buffer (GREAT!)
var obj_parser1 = new ObjectParser();
obj_parser1.write('Here make me an object.');
// output -> chunk= Here make me an object. encoding= utf8 (WTF?)
// # 2. No objectMode. In this case, decodeStrings needn't be specified
var NormalParser = function(opts) {
stream.Transform.call(this, {
decodeStrings: true // Writable option, ensure _transform always gets a Buffer
});
};
util.inherits(NormalParser, stream.Transform);
NormalParser.prototype._transform = function(chunk, encoding, done) {
console.log('chunk=', chunk, 'encoding=', encoding);
done();
};
var norm_parser2 = new NormalParser();
fs.createReadStream('transformerrs.js').pipe(norm_parser2);
// output -> chunk= <Buffer 76 61 72 20 75 74 69 6c 20 3d 20 72 ...> encoding= buffer (GREAT!)
var norm_parser1 = new NormalParser();
norm_parser1.write('These are just words.');
// output -> chunk= <Buffer 54 68 65 73 65 20 61 72 65 20 6a 75 73 74 20 77 6f 72 64 73 2e> encoding= utf8 (SWEET!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment