From 4e7c37b87c0ea62aa419bc4b512ccf0f9511b8f5 Mon Sep 17 00:00:00 2001 From: koichik Date: Wed, 2 Mar 2011 00:35:32 +0900 Subject: [PATCH] Fix fs.WriteStream.end(data, [encoding]) throws TypeError --- lib/fs.js | 10 +++++++++- test/simple/test-fs-write-stream-end.js | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/simple/test-fs-write-stream-end.js diff --git a/lib/fs.js b/lib/fs.js index 8350c6159c7..0ae455016d4 100644 --- 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 index 00000000000..4c256207deb --- /dev/null +++ b/test/simple/test-fs-write-stream-end.js @@ -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); +}); +