Last active
February 26, 2019 03:56
-
-
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 ;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ...> } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After reading the docs more closely, I found that this is indeed intended