Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aheckmann/440186 to your computer and use it in GitHub Desktop.
Save aheckmann/440186 to your computer and use it in GitHub Desktop.
From f4554917d120b994022ed261bdbd4268c476cc0e Mon Sep 17 00:00:00 2001
From: Aaron Heckmann <aaron.heckmann@gmail.com>
Date: Wed, 16 Jun 2010 00:52:15 -0400
Subject: [PATCH] fs.writeFile accepts Buffers
---
lib/fs.js | 4 +++-
test/simple/test-fs-write-file.js | 21 ++++++++++++++++++++-
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/lib/fs.js b/lib/fs.js
index dde6ac6..5ddbc22 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -384,7 +384,9 @@ fs.writeFile = function (path, data, encoding_, callback) {
if (openErr) {
if (callback) callback(openErr);
} else {
- var buffer = new Buffer(data, encoding);
+ var buffer = data instanceof Buffer
+ ? data
+ : new Buffer(data, encoding);
writeAll(fd, buffer, callback);
}
});
diff --git a/test/simple/test-fs-write-file.js b/test/simple/test-fs-write-file.js
index cc0fdfa..14b9dcc 100644
--- a/test/simple/test-fs-write-file.js
+++ b/test/simple/test-fs-write-file.js
@@ -26,9 +26,28 @@ fs.writeFile(filename, s, function (e) {
});
});
+// test that writeFile accepts buffers
+filename2 = join(fixturesDir, 'test2.txt');
+buf = new Buffer(s, 'utf8');
+error('writing to ' + filename2);
+
+fs.writeFile(filename2, s, function (e) {
+ if (e) throw e;
+
+ ncallbacks++;
+ error('file2 written');
+
+ fs.readFile(filename2, function (e, buffer) {
+ if (e) throw e;
+ error('file2 read');
+ ncallbacks++;
+ assert.equal(buf.length, buffer.length);
+ });
+});
+
process.addListener('exit', function () {
error('done');
- assert.equal(2, ncallbacks);
+ assert.equal(4, ncallbacks);
});
\ No newline at end of file
--
1.6.3.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment