fs: Add string encoding option for Stream method
authorYosuke Furukawa <yosuke.furukawa@gmail.com>
Mon, 13 Apr 2015 17:46:52 +0000 (02:46 +0900)
committerYosuke Furukawa <yosuke.furukawa@gmail.com>
Fri, 5 Jun 2015 01:30:32 +0000 (10:30 +0900)
Add string encoding option for fs.createReadStream and
fs.createWriteStream. and check argument type more strictly

PR-URL: https://github.com/nodejs/io.js/pull/1845
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
doc/api/fs.markdown
lib/fs.js
test/parallel/test-fs-read-stream-encoding.js [new file with mode: 0644]
test/parallel/test-fs-read-stream-throw-type-error.js [new file with mode: 0644]
test/parallel/test-fs-write-stream-throw-type-error.js [new file with mode: 0644]

index b9eca2f..bb90cc8 100644 (file)
@@ -795,7 +795,7 @@ on Unix systems, it never was.
 
 Returns a new ReadStream object (See `Readable Stream`).
 
-`options` is an object with the following defaults:
+`options` is an object or string with the following defaults:
 
     { flags: 'r',
       encoding: null,
@@ -821,6 +821,7 @@ An example to read the last 10 bytes of a file which is 100 bytes long:
 
     fs.createReadStream('sample.txt', {start: 90, end: 99});
 
+If `options` is a string, then it specifies the encoding.
 
 ## Class: fs.ReadStream
 
@@ -837,7 +838,7 @@ Emitted when the ReadStream's file is opened.
 
 Returns a new WriteStream object (See `Writable Stream`).
 
-`options` is an object with the following defaults:
+`options` is an object or string with the following defaults:
 
     { flags: 'w',
       encoding: null,
@@ -854,6 +855,7 @@ Like `ReadStream` above, if `fd` is specified, `WriteStream` will ignore the
 `path` argument and will use the specified file descriptor. This means that no
 `open` event will be emitted.
 
+If `options` is a string, then it specifies the encoding.
 
 ## Class: fs.WriteStream
 
index e86a7d0..72251d0 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -1617,8 +1617,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 (options === null || 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;
 
@@ -1783,7 +1790,14 @@ function WriteStream(path, options) {
   if (!(this instanceof WriteStream))
     return new WriteStream(path, options);
 
-  options = options || {};
+  if (options === undefined)
+    options = {};
+  else if (typeof options === 'string')
+    options = { encoding: options };
+  else if (options === null || typeof options !== 'object')
+    throw new TypeError('options must be a string or an object');
+
+  options = Object.create(options);
 
   Writable.call(this, options);
 
diff --git a/test/parallel/test-fs-read-stream-encoding.js b/test/parallel/test-fs-read-stream-encoding.js
new file mode 100644 (file)
index 0000000..6869913
--- /dev/null
@@ -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);
diff --git a/test/parallel/test-fs-read-stream-throw-type-error.js b/test/parallel/test-fs-read-stream-throw-type-error.js
new file mode 100644 (file)
index 0000000..b3e6381
--- /dev/null
@@ -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/);
diff --git a/test/parallel/test-fs-write-stream-throw-type-error.js b/test/parallel/test-fs-write-stream-throw-type-error.js
new file mode 100644 (file)
index 0000000..8568dd4
--- /dev/null
@@ -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/);