Fix fs.WriteStream.end(data, [encoding]) throws TypeError
authorkoichik <koichik@improvement.jp>
Tue, 1 Mar 2011 15:35:32 +0000 (00:35 +0900)
committerRyan Dahl <ry@tinyclouds.org>
Tue, 1 Mar 2011 18:49:20 +0000 (10:49 -0800)
lib/fs.js
test/simple/test-fs-write-stream-end.js [new file with mode: 0644]

index 8350c61..0ae4550 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -1052,7 +1052,15 @@ WriteStream.prototype.write = function(data) {
   return false;
 };
 
-WriteStream.prototype.end = function(cb) {
+WriteStream.prototype.end = function(data, encoding, cb) {
+  if (typeof(data) === 'function') {
+    cb = data;
+  } else if (typeof(encoding) === 'function') {
+    cb = encoding;
+    this.write(data);
+  } else if (arguments.length > 0) {
+    this.write(data, encoding);
+  }
   this.writable = false;
   this._queue.push([fs.close, cb]);
   this.flush();
diff --git a/test/simple/test-fs-write-stream-end.js b/test/simple/test-fs-write-stream-end.js
new file mode 100644 (file)
index 0000000..4c25620
--- /dev/null
@@ -0,0 +1,25 @@
+var common = require('../common');
+var assert = require('assert');
+
+var path = require('path'),
+    fs = require('fs');
+
+var writeEndOk = false;
+(function() {
+  debugger;
+  var file = path.join(common.tmpDir, 'write-end-test.txt');
+  var stream = fs.createWriteStream(file);
+
+  stream.end('a\n', 'utf8', function() {
+    var content = fs.readFileSync(file, 'utf8');
+    assert.equal(content, 'a\n');
+    writeEndOk = true;
+  });
+
+})();
+
+
+process.on('exit', function() {
+  assert.ok(writeEndOk);
+});
+