Skip to content

Commit 2f418b6

Browse files
fs: set encoding if option argument is string
1 parent 4969579 commit 2f418b6

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

lib/fs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,10 @@ function WriteStream(path, options) {
18151815
this.pos = this.start;
18161816
}
18171817

1818+
if (options.encoding) {
1819+
this.setDefaultEncoding(options.encoding);
1820+
}
1821+
18181822
if (typeof this.fd !== 'number')
18191823
this.open();
18201824

Lines changed: 17 additions & 0 deletions
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);

test/parallel/test-fs-read-stream-throw-type-error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
const common = require('../common');
23
const assert = require('assert');
34
const fs = require('fs');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 firstEncoding = 'base64';
8+
const secondEncoding = 'binary';
9+
10+
const example = path.join(common.fixturesDir, 'x.txt');
11+
const dummy = path.join(common.tmpDir, '/x.txt');
12+
const exampleReadStream = fs.createReadStream(example, firstEncoding);
13+
const dummyWriteStream = fs.createWriteStream(dummy, firstEncoding);
14+
exampleReadStream.pipe(dummyWriteStream).on('finish', function(){
15+
const assertWriteStream = new stream.Writable({
16+
write: function(chunk, enc, next) {
17+
const expected = new Buffer('xyz\n');
18+
assert(chunk.equals(expected));
19+
}
20+
});
21+
assertWriteStream.setDefaultEncoding(secondEncoding);
22+
fs.createReadStream(dummy, secondEncoding).pipe(assertWriteStream);
23+
});

test/parallel/test-fs-write-stream-throw-type-error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
const common = require('../common');
23
const assert = require('assert');
34
const fs = require('fs');

0 commit comments

Comments
 (0)