-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: Add string encoding option for Stream method
Add string encoding option for fs.createReadStream and fs.createWriteStream. and check argument type more strictly PR-URL: #1845 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
- Loading branch information
1 parent
59d9734
commit 353e26e
Showing
5 changed files
with
103 additions
and
4 deletions.
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,17 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const stream = require('stream'); | ||
const encoding = 'base64'; | ||
|
||
const example = path.join(common.fixturesDir, 'x.txt'); | ||
const assertStream = new stream.Writable({ | ||
write: function(chunk, enc, next) { | ||
const expected = new Buffer('xyz'); | ||
assert(chunk.equals(expected)); | ||
} | ||
}); | ||
assertStream.setDefaultEncoding(encoding); | ||
fs.createReadStream(example, encoding).pipe(assertStream); |
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,33 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const example = path.join(common.fixturesDir, 'x.txt'); | ||
|
||
assert.doesNotThrow(function() { | ||
fs.createReadStream(example, undefined); | ||
}); | ||
assert.doesNotThrow(function() { | ||
fs.createReadStream(example, 'utf8'); | ||
}); | ||
assert.doesNotThrow(function() { | ||
fs.createReadStream(example, {encoding: 'utf8'}); | ||
}); | ||
|
||
assert.throws(function() { | ||
fs.createReadStream(example, null); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createReadStream(example, 123); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createReadStream(example, 0); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createReadStream(example, true); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createReadStream(example, false); | ||
}, /options must be a string or an object/); |
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,33 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const example = path.join(common.tmpDir, 'dummy'); | ||
|
||
assert.doesNotThrow(function() { | ||
fs.createWriteStream(example, undefined); | ||
}); | ||
assert.doesNotThrow(function() { | ||
fs.createWriteStream(example, 'utf8'); | ||
}); | ||
assert.doesNotThrow(function() { | ||
fs.createWriteStream(example, {encoding: 'utf8'}); | ||
}); | ||
|
||
assert.throws(function() { | ||
fs.createWriteStream(example, null); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createWriteStream(example, 123); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createWriteStream(example, 0); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createWriteStream(example, true); | ||
}, /options must be a string or an object/); | ||
assert.throws(function() { | ||
fs.createWriteStream(example, false); | ||
}, /options must be a string or an object/); |