Skip to content

Instantly share code, notes, and snippets.

@koichik
Created December 27, 2010 16:35
Show Gist options
  • Save koichik/756270 to your computer and use it in GitHub Desktop.
Save koichik/756270 to your computer and use it in GitHub Desktop.
[PATCH] Fix fs.WriteStream.prototype.end() with data
From 067dd138e9631f55eec9a237949da53030962a2c Mon Sep 17 00:00:00 2001
From: koichik <koichik@improvement.jp>
Date: Thu, 23 Dec 2010 18:37:28 +0900
Subject: [PATCH] Fix fs.WriteStream.prototype.end() with data
---
lib/fs.js | 10 +++++++++-
test/simple/test-fs-write-stream.js | 23 +++++++++++++++++++++++
2 files changed, 32 insertions(+), 1 deletions(-)
diff --git a/lib/fs.js b/lib/fs.js
index b9e6b2d..83592c1 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -951,7 +951,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.js b/test/simple/test-fs-write-stream.js
index b038bf0..28da814 100644
--- a/test/simple/test-fs-write-stream.js
+++ b/test/simple/test-fs-write-stream.js
@@ -27,3 +27,26 @@ var file = path.join(common.tmpDir, 'write.txt');
stream.destroy();
})();
+(function() {
+ var stream = fs.createWriteStream(file),
+ wrote = [];
+
+ stream.write = function(data, encoding) {
+ wrote.push([data, encoding]);
+ }
+ stream.end();
+ stream.end(function() {});
+ assert.equal(wrote.length, 0);
+ stream.end('a');
+ stream.end('b', function() {});
+ stream.end('c', 'utf8');
+ stream.end('d', 'utf8', function() {});
+ assert.equal(wrote.length, 4)
+ assert.deepEqual(wrote, [
+ ['a', undefined],
+ ['b', undefined],
+ ['c', 'utf8'],
+ ['d', 'utf8']
+ ]);
+})();
+
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment