Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fs.writeFile accepts Buffers

  • Loading branch information...
commit 1d088fb9062bf67d7285a801a5c1630740d1357f 1 parent c2e2479
@aheckmann aheckmann authored ry committed
Showing with 24 additions and 3 deletions.
  1. +1 −1  lib/fs.js
  2. +23 −2 test/simple/test-fs-write-file.js
View
2  lib/fs.js
@@ -384,7 +384,7 @@ 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);
}
});
View
25 test/simple/test-fs-write-file.js
@@ -26,9 +26,30 @@ 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) {

:s/s,/buf,/ --> fs.writeFile(filename2, buf, function (e) {

@ry
ry added a note

Oops. I was wondering why it was passing before the fix. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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(4, ncallbacks);
- assert.equal(2, ncallbacks);
-});
+ fs.unlinkSync(filename);
+ fs.unlinkSync(filename2);
+});
@aheckmann

:s/s,/buf,/ --> fs.writeFile(filename2, buf, function (e) {

@ry

Oops. I was wondering why it was passing before the fix. Thanks.

Please sign in to comment.
Something went wrong with that request. Please try again.