Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1615,8 +1615,15 @@ function ReadStream(path, options) {
if (!(this instanceof ReadStream))
return new ReadStream(path, options);

if (options === undefined)
options = {};
else if (typeof options === 'string')
options = { encoding: options };
else if (typeof options !== 'object')
throw new TypeError('options must be a string or an object');

// a little bit bigger buffer and water marks by default
options = Object.create(options || {});
options = Object.create(options);
if (options.highWaterMark === undefined)
options.highWaterMark = 64 * 1024;

Expand Down Expand Up @@ -1781,7 +1788,12 @@ function WriteStream(path, options) {
if (!(this instanceof WriteStream))
return new WriteStream(path, options);

options = options || {};
if (options === undefined || options === null)
options = {};
else if (typeof options === 'string')
options = { encoding: options };
else if (typeof options !== 'object')
throw new TypeError('options must be a string or an object');

Writable.call(this, options);

Expand All @@ -1805,6 +1817,10 @@ function WriteStream(path, options) {
this.pos = this.start;
}

if (options.encoding) {
this.setDefaultEncoding(options.encoding);
}

if (typeof this.fd !== 'number')
this.open();

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-read-stream-encoding.js
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);
19 changes: 19 additions & 0 deletions test/parallel/test-fs-read-stream-throw-type-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'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.throws(function() {
fs.createReadStream(example, 123);
}, TypeError, 'options must be a string or an object');
assert.throws(function() {
fs.createReadStream(example, 0);
}, TypeError, 'options must be a string or an object');
assert.throws(function() {
fs.createReadStream(example, true);
}, TypeError, 'options must be a string or an object');
assert.throws(function() {
fs.createReadStream(example, false);
}, TypeError, 'options must be a string or an object');
23 changes: 23 additions & 0 deletions test/parallel/test-fs-write-stream-encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'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 example = path.join(common.fixturesDir, 'x.txt');
const dummy = path.join(common.tmpDir, '/x.txt');
const exampleReadStream = fs.createReadStream(example, firstEncoding);
const dummyWriteStream = fs.createWriteStream(dummy, 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(dummy, secondEncoding).pipe(assertWriteStream);
});
19 changes: 19 additions & 0 deletions test/parallel/test-fs-write-stream-throw-type-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'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.throws(function() {
fs.createWriteStream(example, 123);
}, TypeError, 'options must be a string or an object');
assert.throws(function() {
fs.createWriteStream(example, 0);
}, TypeError, 'options must be a string or an object');
assert.throws(function() {
fs.createWriteStream(example, true);
}, TypeError, 'options must be a string or an object');
assert.throws(function() {
fs.createWriteStream(example, false);
}, TypeError, 'options must be a string or an object');