-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: set encoding on fs.createWriteStream
Enable encoding option on fs.createWriteStream. PR-URL: #1844 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
1 parent
a4dbf45
commit 8357c50
Showing
3 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const stream = require('stream'); | ||
const firstEncoding = 'base64'; | ||
const secondEncoding = 'binary'; | ||
|
||
const examplePath = path.join(common.fixturesDir, 'x.txt'); | ||
const dummyPath = path.join(common.tmpDir, 'x.txt'); | ||
|
||
const exampleReadStream = fs.createReadStream(examplePath, { | ||
encoding: firstEncoding | ||
}); | ||
|
||
const dummyWriteStream = fs.createWriteStream(dummyPath, { | ||
encoding: firstEncoding | ||
}); | ||
|
||
exampleReadStream.pipe(dummyWriteStream).on('finish', function() { | ||
const assertWriteStream = new stream.Writable({ | ||
write: function(chunk, enc, next) { | ||
const expected = new Buffer('xyz\n'); | ||
assert(chunk.equals(expected)); | ||
} | ||
}); | ||
assertWriteStream.setDefaultEncoding(secondEncoding); | ||
fs.createReadStream(dummyPath, { | ||
encoding: secondEncoding | ||
}).pipe(assertWriteStream); | ||
}); |