Skip to content

Instantly share code, notes, and snippets.

@thlorenz
Last active February 26, 2019 03:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thlorenz/7846391 to your computer and use it in GitHub Desktop.
Save thlorenz/7846391 to your computer and use it in GitHub Desktop.
Why is fs.createReadStream always emitting buffers? Read the comment and corrected sample to find the answer ;)
var stream = require('stream');
var util = require('util');
var Writable = stream.Writable;
module.exports = CheckEncodingWritable;
util.inherits(CheckEncodingWritable, Writable);
function CheckEncodingWritable (opts) {
if (!(this instanceof CheckEncodingWritable)) return new CheckEncodingWritable(opts);
opts = opts || {};
Writable.call(this, opts);
}
CheckEncodingWritable.prototype._write = function (chunk, encoding, cb) {
console.log({ encoding: encoding, chunk: chunk });
cb();
};
require('fs')
.createReadStream(__filename, { encoding: 'utf8' })
.pipe(new CheckEncodingWritable())
// =>
// { encoding: 'buffer',
// chunk: <Buffer 27 75 73 65 20 73 74 72 69 63 74 27 3b 0a 0a 76 61 72 20 73 74 72 65 61 6d 20 3d 20 72 65 71 75 69 72 65 28 27 73 74 72 65 61 6d 27 29 3b 0a 76 61 72 20 ...> }
'use strict';
var stream = require('stream');
var util = require('util');
var Writable = stream.Writable;
module.exports = CheckEncodingWritable;
util.inherits(CheckEncodingWritable, Writable);
function CheckEncodingWritable (opts) {
if (!(this instanceof CheckEncodingWritable)) return new CheckEncodingWritable(opts);
opts = opts || {};
Writable.call(this, opts);
}
CheckEncodingWritable.prototype._write = function (chunk, encoding, cb) {
console.log({ encoding: encoding, chunk: chunk });
cb();
};
require('fs')
.createReadStream(__filename, { encoding: 'utf8' })
.pipe(new CheckEncodingWritable({ decodeStrings: false })) // <-----
// =>
// { encoding: 'utf8',
// chunk: '\'use strict\';\n\nvar stream = require(\'stream\');\nvar util = require(\'util\');\n\nvar Writable = stream.Writable;\n\nmodule.exports = CheckEncodingWritable;\n\nutil.inherits(CheckEncodingWritable, Writable);\n\nfunction CheckEncodingWritable (opts) {\n if (!(this instanceof CheckEncodingWritable)) return new CheckEncodingWritable(opts);\n\n opts = opts || {};\n \n Writable.call(this, opts);\n}\n\nCheckEncodingWritable.prototype._write = function (chunk, encoding, cb) {\n console.log({ encoding: encoding, chunk: chunk });\n cb();\n};\n\nvar readable = require(\'fs\')\n .createReadStream(__filename, { encoding: \'utf8\' })\n .pipe(new CheckEncodingWritable({ decodeStrings: false }))\n' }
@thlorenz
Copy link
Author

thlorenz commented Dec 7, 2013

After reading the docs more closely, I found that this is indeed intended

chunk will always be a buffer unless the decodeStrings option is explicitly set to false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment