GeezerVison is showing github.com — fs: Add string encoding option for Stream method · nodejs/node@353e26e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.markdown +4-2Lines changed: 4 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -795,7 +795,7 @@ on Unix systems, it never was. | 795 | 795 |
| 796 | 796 | Returns a new ReadStream object (See `Readable Stream`). | 797 | 797 |
| 798 | | -`options` is an object with the following defaults: | | 798 | +`options` is an object or string with the following defaults: | 799 | 799 |
| 800 | 800 | { flags: 'r', | 801 | 801 | encoding: null, | @@ -821,6 +821,7 @@ An example to read the last 10 bytes of a file which is 100 bytes long: | 821 | 821 |
| 822 | 822 | fs.createReadStream('sample.txt', {start: 90, end: 99}); | 823 | 823 |
| | 824 | +If `options` is a string, then it specifies the encoding. | 824 | 825 |
| 825 | 826 | ## Class: fs.ReadStream | 826 | 827 |
| @@ -837,7 +838,7 @@ Emitted when the ReadStream's file is opened. | 837 | 838 |
| 838 | 839 | Returns a new WriteStream object (See `Writable Stream`). | 839 | 840 |
| 840 | | -`options` is an object with the following defaults: | | 841 | +`options` is an object or string with the following defaults: | 841 | 842 |
| 842 | 843 | { flags: 'w', | 843 | 844 | encoding: null, | @@ -854,6 +855,7 @@ Like `ReadStream` above, if `fd` is specified, `WriteStream` will ignore the | 854 | 855 | `path` argument and will use the specified file descriptor. This means that no | 855 | 856 | `open` event will be emitted. | 856 | 857 |
| | 858 | +If `options` is a string, then it specifies the encoding. | 857 | 859 |
| 858 | 860 | ## Class: fs.WriteStream | 859 | 861 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1617,8 +1617,15 @@ function ReadStream(path, options) { | 1617 | 1617 | if (!(this instanceof ReadStream)) | 1618 | 1618 | return new ReadStream(path, options); | 1619 | 1619 |
| | 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 | + | 1620 | 1627 | // a little bit bigger buffer and water marks by default | 1621 | | - options = Object.create(options || {}); | | 1628 | + options = Object.create(options); | 1622 | 1629 | if (options.highWaterMark === undefined) | 1623 | 1630 | options.highWaterMark = 64 * 1024; | 1624 | 1631 |
| @@ -1783,7 +1790,14 @@ function WriteStream(path, options) { | 1783 | 1790 | if (!(this instanceof WriteStream)) | 1784 | 1791 | return new WriteStream(path, options); | 1785 | 1792 |
| 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); | 1787 | 1801 |
| 1788 | 1802 | Writable.call(this, options); | 1789 | 1803 |
| |
Collapse file Copy file name to clipboard +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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); |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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/); |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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/); |
|