Skip to content

Commit 353e26e

Browse files
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 <[email protected]>
1 parent 59d9734 commit 353e26e

5 files changed

+103
-4
lines changed

doc/api/fs.markdown

+4-2
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ on Unix systems, it never was.
795795

796796
Returns a new ReadStream object (See `Readable Stream`).
797797

798-
`options` is an object with the following defaults:
798+
`options` is an object or string with the following defaults:
799799

800800
{ flags: 'r',
801801
encoding: null,
@@ -821,6 +821,7 @@ An example to read the last 10 bytes of a file which is 100 bytes long:
821821

822822
fs.createReadStream('sample.txt', {start: 90, end: 99});
823823

824+
If `options` is a string, then it specifies the encoding.
824825

825826
## Class: fs.ReadStream
826827

@@ -837,7 +838,7 @@ Emitted when the ReadStream's file is opened.
837838

838839
Returns a new WriteStream object (See `Writable Stream`).
839840

840-
`options` is an object with the following defaults:
841+
`options` is an object or string with the following defaults:
841842

842843
{ flags: 'w',
843844
encoding: null,
@@ -854,6 +855,7 @@ Like `ReadStream` above, if `fd` is specified, `WriteStream` will ignore the
854855
`path` argument and will use the specified file descriptor. This means that no
855856
`open` event will be emitted.
856857

858+
If `options` is a string, then it specifies the encoding.
857859

858860
## Class: fs.WriteStream
859861

lib/fs.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1617,8 +1617,15 @@ function ReadStream(path, options) {
16171617
if (!(this instanceof ReadStream))
16181618
return new ReadStream(path, options);
16191619

1620+
if (options === undefined)
1621+
options = {};
1622+
else if (typeof options === 'string')
1623+
options = { encoding: options };
1624+
else if (options === null || typeof options !== 'object')
1625+
throw new TypeError('options must be a string or an object');
1626+
16201627
// a little bit bigger buffer and water marks by default
1621-
options = Object.create(options || {});
1628+
options = Object.create(options);
16221629
if (options.highWaterMark === undefined)
16231630
options.highWaterMark = 64 * 1024;
16241631

@@ -1783,7 +1790,14 @@ function WriteStream(path, options) {
17831790
if (!(this instanceof WriteStream))
17841791
return new WriteStream(path, options);
17851792

1786-
options = options || {};
1793+
if (options === undefined)
1794+
options = {};
1795+
else if (typeof options === 'string')
1796+
options = { encoding: options };
1797+
else if (options === null || typeof options !== 'object')
1798+
throw new TypeError('options must be a string or an object');
1799+
1800+
options = Object.create(options);
17871801

17881802
Writable.call(this, options);
17891803

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const stream = require('stream');
7+
const encoding = 'base64';
8+
9+
const example = path.join(common.fixturesDir, 'x.txt');
10+
const assertStream = new stream.Writable({
11+
write: function(chunk, enc, next) {
12+
const expected = new Buffer('xyz');
13+
assert(chunk.equals(expected));
14+
}
15+
});
16+
assertStream.setDefaultEncoding(encoding);
17+
fs.createReadStream(example, encoding).pipe(assertStream);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
const example = path.join(common.fixturesDir, 'x.txt');
8+
9+
assert.doesNotThrow(function() {
10+
fs.createReadStream(example, undefined);
11+
});
12+
assert.doesNotThrow(function() {
13+
fs.createReadStream(example, 'utf8');
14+
});
15+
assert.doesNotThrow(function() {
16+
fs.createReadStream(example, {encoding: 'utf8'});
17+
});
18+
19+
assert.throws(function() {
20+
fs.createReadStream(example, null);
21+
}, /options must be a string or an object/);
22+
assert.throws(function() {
23+
fs.createReadStream(example, 123);
24+
}, /options must be a string or an object/);
25+
assert.throws(function() {
26+
fs.createReadStream(example, 0);
27+
}, /options must be a string or an object/);
28+
assert.throws(function() {
29+
fs.createReadStream(example, true);
30+
}, /options must be a string or an object/);
31+
assert.throws(function() {
32+
fs.createReadStream(example, false);
33+
}, /options must be a string or an object/);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
const example = path.join(common.tmpDir, 'dummy');
8+
9+
assert.doesNotThrow(function() {
10+
fs.createWriteStream(example, undefined);
11+
});
12+
assert.doesNotThrow(function() {
13+
fs.createWriteStream(example, 'utf8');
14+
});
15+
assert.doesNotThrow(function() {
16+
fs.createWriteStream(example, {encoding: 'utf8'});
17+
});
18+
19+
assert.throws(function() {
20+
fs.createWriteStream(example, null);
21+
}, /options must be a string or an object/);
22+
assert.throws(function() {
23+
fs.createWriteStream(example, 123);
24+
}, /options must be a string or an object/);
25+
assert.throws(function() {
26+
fs.createWriteStream(example, 0);
27+
}, /options must be a string or an object/);
28+
assert.throws(function() {
29+
fs.createWriteStream(example, true);
30+
}, /options must be a string or an object/);
31+
assert.throws(function() {
32+
fs.createWriteStream(example, false);
33+
}, /options must be a string or an object/);

0 commit comments

Comments
 (0)