Skip to content

Commit

Permalink
Fix: Support strings in writeFile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Nov 30, 2017
1 parent 5708795 commit 453f063
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/fileOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var MASK_MODE = parseInt('0777', 8);
var DEFAULT_FILE_MODE = parseInt('0666', 8);
var APPEND_MODE_REGEXP = /a/;

function isString(value) {
return (typeof value === 'string');
}

function closeFd(propagatedErr, fd, callback) {
if (typeof fd !== 'number') {
return callback(propagatedErr);
Expand Down Expand Up @@ -169,8 +173,12 @@ function writeFile(path, data, options, callback) {
}

if (!isBuffer(data)) {
callback(new TypeError('Data must be a Buffer'));
return;
if (isString(data)) {
data = new Buffer(data);
} else {
callback(new TypeError('Data must be a Buffer'));
return;
}
}

if (!options) {
Expand Down
10 changes: 9 additions & 1 deletion test/fileOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,16 @@ describe('writeFile', function() {
});
});

it('passes an error if called without buffer for data', function(done) {
it('does not pass an error if called with string as data', function(done) {
writeFile(filepath, 'test', function(err) {
expect(err).toNotExist();

done();
});
});

it('passes an error if called with non-buffer/non-string as data', function(done) {
writeFile(filepath, {}, function(err) {
expect(err).toExist();

done();
Expand Down

0 comments on commit 453f063

Please sign in to comment.