Skip to content

Commit

Permalink
API: readFileSync without encoding argument now returns a Buffer
Browse files Browse the repository at this point in the history
Correctly load utf8 data; add a test test-fs-read-file-sync.js
  • Loading branch information
ry committed May 29, 2010
1 parent c82d646 commit 1a5acd9
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 25 deletions.
7 changes: 6 additions & 1 deletion doc/api.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1443,10 +1443,15 @@ contents of the file.

If no encoding is specified, then the raw buffer is returned.

### fs.readFileSync(filename, encoding='utf8')

### fs.readFileSync(filename [, encoding])

Synchronous version of `fs.readFile`. Returns the contents of the `filename`.

If `encoding` is specified then this function returns a string. Otherwise it
returns a buffer.


### fs.writeFile(filename, data, encoding='utf8', callback)

Asynchronously writes data to a file. Example:
Expand Down
23 changes: 12 additions & 11 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,22 @@ fs.readFile = function (path, encoding_, callback) {
};

fs.readFileSync = function (path, encoding) {
encoding = encoding || "utf8"; // default to utf8

var fd = binding.open(path, process.O_RDONLY, 0666);
var content = '';
var pos = null; // leave null to allow reads on unseekable devices
var r;
var fd = fs.openSync(path, process.O_RDONLY, 0666);
var stat = fs.statSync(path);
var buffer = new Buffer(stat.size);
var nread = 0;

while ((r = fs.readSync(fd, 4*1024, pos, encoding)) && r[0]) {
content += r[0];
pos += r[1]
while (nread < buffer.length) {
nread += fs.readSync(fd, buffer, nread, buffer.length - nread, null);
}

binding.close(fd);
fs.closeSync(fd);

return content;
if (encoding) {
return buffer.toString(encoding);
} else {
return buffer;
}
};


Expand Down
2 changes: 1 addition & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ Module.prototype._compile = function (content, filename) {


Module.prototype._loadScriptSync = function (filename) {
var content = requireNative('fs').readFileSync(filename);
var content = requireNative('fs').readFileSync(filename, 'utf8');
var e = this._compile(content, filename);
if (e) {
throw e;
Expand Down
6 changes: 3 additions & 3 deletions test/simple/test-crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ try {
process.exit();
}

var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem");
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem");
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem");
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem", 'ascii');
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem", 'ascii');
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem", 'ascii');

var credentials = crypto.createCredentials({key:keyPem, cert:certPem, ca:caPem});

Expand Down
11 changes: 11 additions & 0 deletions test/simple/test-fs-read-file-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('../common');

path = require('path');
fs = require('fs');
fn = path.join(fixturesDir, 'elipses.txt');

var s = fs.readFileSync(fn, 'utf8');
for (var i = 0; i < s.length; i++) {
assert.equal("\u2026", s[i]);
}
assert.equal(10000, s.length);
4 changes: 2 additions & 2 deletions test/simple/test-fs-symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ try {fs.unlinkSync(dstPath);}catch(e){}
fs.link(srcPath, dstPath, function(err){
if (err) throw err;
puts('hard link done');
var srcContent = fs.readFileSync(srcPath);
var dstContent = fs.readFileSync(dstPath);
var srcContent = fs.readFileSync(srcPath, 'utf8');
var dstContent = fs.readFileSync(dstPath, 'utf8');
assert.equal(srcContent, dstContent);
completed++;
});
Expand Down
2 changes: 1 addition & 1 deletion test/simple/test-fs-write-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fs.open(filename, 'w', 0644, function (err, fd) {
assert.equal(expected.length, written);
fs.closeSync(fd);

var found = fs.readFileSync(filename);
var found = fs.readFileSync(filename, 'utf8');
assert.deepEqual(expected.toString(), found);
fs.unlinkSync(filename);
});
Expand Down
6 changes: 3 additions & 3 deletions test/simple/test-http-tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var request_number = 0;
var requests_sent = 0;
var server_response = "";
var client_got_eof = false;
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem");
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem");
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem");
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem", 'ascii');
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem", 'ascii');
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem", 'ascii');

var credentials = crypto.createCredentials({key:keyPem, cert:certPem, ca:caPem});

Expand Down
6 changes: 3 additions & 3 deletions test/simple/test-tcp-tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ try {
process.exit();
}

var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem");
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem");
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem");
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem", 'ascii');
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem", 'ascii');
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem", 'ascii');

var credentials = crypto.createCredentials({key:keyPem, cert:certPem, ca:caPem});

Expand Down

0 comments on commit 1a5acd9

Please sign in to comment.