Browse Source

API: readFileSync without encoding argument now returns a Buffer

Correctly load utf8 data; add a test test-fs-read-file-sync.js
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
1a5acd9850
  1. 7
      doc/api.markdown
  2. 23
      lib/fs.js
  3. 2
      lib/module.js
  4. 6
      test/simple/test-crypto.js
  5. 11
      test/simple/test-fs-read-file-sync.js
  6. 4
      test/simple/test-fs-symlink.js
  7. 2
      test/simple/test-fs-write-buffer.js
  8. 6
      test/simple/test-http-tls.js
  9. 6
      test/simple/test-tcp-tls.js

7
doc/api.markdown

@ -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:

23
lib/fs.js

@ -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;
}
};

2
lib/module.js

@ -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;

6
test/simple/test-crypto.js

@ -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});

11
test/simple/test-fs-read-file-sync.js

@ -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
test/simple/test-fs-symlink.js

@ -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++;
});

2
test/simple/test-fs-write-buffer.js

@ -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);
});

6
test/simple/test-http-tls.js

@ -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});

6
test/simple/test-tcp-tls.js

@ -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});

Loading…
Cancel
Save