Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support promises in the fs module #173

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Benchmark.prototype._run = function() {
var argv = queue[i++];
if (!argv)
return;
argv = process.execArgv.concat(argv);
var child = spawn(node, argv, { stdio: 'inherit' });
child.on('close', function(code, signal) {
if (code)
Expand Down
88 changes: 88 additions & 0 deletions benchmark/fs/stat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Call fs.stat over and over again really fast.
// Then see how many times it got called.
// Yes, this is a silly benchmark. Most benchmarks are silly.

var path = require('path');
var common = require('../common.js');
var fs = require('fs');

var FILES = [
require.resolve('../../lib/assert.js'),
require.resolve('../../lib/console.js'),
require.resolve('../../lib/fs.js')
];

var VARIANTS = {
promise: createPromiseBasedTest(fs.stat),
callback: createCallBackBasedTest(fs.stat),
};

var bench = common.createBenchmark(main, {
dur: [5],
concurrent: [1, 10, 100],
variant: Object.keys(VARIANTS)
});

function main(conf) {
var stat = VARIANTS[conf.variant];

if (stat == VARIANTS.promise && !process.promisifyCore) {
bench.start();
bench.end(0);
return;
}

var calls = 0;
bench.start();
setTimeout(function() {
bench.end(calls);
}, +conf.dur * 1000);

var cur = +conf.concurrent;
while (cur--) run();

function run() {
var p = stat(next);
if (p) p.then(next);
}

function next() {
calls++;
run();
}
}

function createCallBackBasedTest(stat) {
return function runStatViaCallbacks(cb) {
stat(FILES[0], function(err, data) {
if (err) throw err;
second(data);
});

function second() {
stat(FILES[1], function(err, data) {
if (err) throw err;
third(data);
});
}

function third() {
stat(FILES[2], function(err, data) {
if (err) throw err;
cb(data);
});
}
};
}

function createPromiseBasedTest(stat) {
return function runStatViaPromises() {
return stat(FILES[0])
.then(function secondP(data) {
return stat(FILES[1]);
})
.then(function thirdP(data) {
return stat(FILES[2]);
});
}
}
47 changes: 42 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ function maybeCallback(cb) {
return util.isFunction(cb) ? cb : rethrow();
}

function _maybeCallbackOrPromise(cb) {
return util.isFunction(cb) ? cb : createCallbackBackedByPromise();
}

var maybeCallbackOrPromise = process.promisifyCore ?
_maybeCallbackOrPromise : maybeCallback;

// Ensure that callbacks run in the global context. Only use this function
// for callbacks that are passed to the binding layer, callbacks that are
// invoked from JS already run in the proper scope.
Expand All @@ -101,6 +108,32 @@ function makeCallback(cb) {
};
}

function _makeCallbackOrPromise(cb) {
if (!util.isFunction(cb)) {
return createCallbackBackedByPromise();
}

return function() {
return cb.apply(null, arguments);
};
}

var makeCallbackOrPromise = process.promisifyCore ?
_makeCallbackOrPromise : makeCallback;

function createCallbackBackedByPromise() {
var cb;
var promise = new Promise(function(resolve, reject) {
cb = function(err, data) {
if (err) reject(err);
else resolve(data);
};
});
cb.promise = promise;
return cb;
}


function assertEncoding(encoding) {
if (encoding && !Buffer.isEncoding(encoding)) {
throw new Error('Unknown encoding: ' + encoding);
Expand Down Expand Up @@ -241,7 +274,7 @@ fs.existsSync = function(path) {
};

fs.readFile = function(path, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
var callback = maybeCallbackOrPromise(arguments[arguments.length - 1]);

if (util.isFunction(options) || !options) {
options = { encoding: null, flag: 'r' };
Expand All @@ -262,7 +295,10 @@ fs.readFile = function(path, options, callback_) {
var fd;

var flag = options.flag || 'r';
fs.open(path, flag, 438 /*=0666*/, function(er, fd_) {
fs.open(path, flag, 438 /*=0666*/, afterOpen);
return callback.promise;

function afterOpen(er, fd_) {
if (er) return callback(er);
fd = fd_;

Expand Down Expand Up @@ -291,7 +327,7 @@ fs.readFile = function(path, options, callback_) {
buffer = new Buffer(size);
read();
});
});
}

function read() {
if (size === 0) {
Expand Down Expand Up @@ -781,11 +817,12 @@ fs.lstat = function(path, callback) {
};

fs.stat = function(path, callback) {
callback = makeCallback(callback);
if (!nullCheck(path, callback)) return;
callback = makeCallbackOrPromise(callback);
if (!nullCheck(path, callback)) return callback.promise;
var req = new FSReqWrap();
req.oncomplete = callback;
binding.stat(pathModule._makeLong(path), req);
return callback.promise;
};

fs.fstatSync = function(fd) {
Expand Down
9 changes: 9 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ static bool print_eval = false;
static bool force_repl = false;
static bool trace_deprecation = false;
static bool throw_deprecation = false;
static bool promisify_core = false;
static const char* eval_string = nullptr;
static bool use_debug_agent = false;
static bool debug_wait_connect = false;
Expand Down Expand Up @@ -2682,6 +2683,11 @@ void SetupProcessObject(Environment* env,
READONLY_PROPERTY(process, "noDeprecation", True(env->isolate()));
}

// --promisify-core
if (promisify_core) {
READONLY_PROPERTY(process, "promisifyCore", True(env->isolate()));
}

// --throw-deprecation
if (throw_deprecation) {
READONLY_PROPERTY(process, "throwDeprecation", True(env->isolate()));
Expand Down Expand Up @@ -2900,6 +2906,7 @@ static void PrintHelp() {
" --throw-deprecation throw an exception anytime a deprecated "
"function is used\n"
" --trace-deprecation show stack traces on deprecations\n"
" --promisify-core EXPERIMENTAL: promise-enabled core API\n"
" --v8-options print v8 command line options\n"
" --max-stack-size=val set max v8 stack size (bytes)\n"
#if defined(NODE_HAVE_I18N_SUPPORT)
Expand Down Expand Up @@ -3014,6 +3021,8 @@ static void ParseArgs(int* argc,
trace_deprecation = true;
} else if (strcmp(arg, "--throw-deprecation") == 0) {
throw_deprecation = true;
} else if (strcmp(arg, "--promisify-core") == 0) {
promisify_core = true;
} else if (strcmp(arg, "--v8-options") == 0) {
new_v8_argv[new_v8_argc] = "--help";
new_v8_argc += 1;
Expand Down
78 changes: 78 additions & 0 deletions test/simple/test-fs-readfile-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var path = require('path');
var fs = require('fs');
var got_error = false;
var success_count = 0;
var expected_count = 0;
var filename;

if (!process.promisifyCore) {
console.log(
'// Skipping tests of promisified API, `--promisify-core` is needed');
return;
}

// happy path, all arguments
expected_count++;
fs.readFile(__filename)
.then(function(content) {
assert.ok(/happy path, all arguments/.test(content));
success_count++;
})
.catch(reportTestError);

// empty file, optional arguments skipped
expected_count++;
filename = path.join(common.fixturesDir, 'empty.txt');
fs.readFile(filename, 'utf-8')
.then(function(content) {
assert.strictEqual('', content);
success_count++;
})
.catch(reportTestError);

// file does not exist
expected_count++;
filename = path.join(common.fixturesDir, 'does_not_exist.txt');
fs.readFile(filename)
.then(
function(content) {
throw new Error('readFile should have failed for unkown file');
},
function(err) {
assert.equal('ENOENT', err.code);
success_count++;
})
.catch(reportTestError);

process.on('exit', function() {
assert.equal(expected_count, success_count);
assert.equal(false, got_error);
});

function reportTestError(err) {
console.error('\nTEST FAILED\n', err.stack, '\n');
got_error = true;
}
33 changes: 32 additions & 1 deletion test/simple/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var assert = require('assert');
var fs = require('fs');
var got_error = false;
var success_count = 0;
var expected_count = 5;

fs.stat('.', function(err, stats) {
if (err) {
Expand All @@ -41,6 +42,32 @@ fs.stat('.', function(err, stats) {
assert.ok(stats.hasOwnProperty('blocks'));
});

if (!process.promisifyCore) {
console.log(
'// Skipping tests of promisified API, `--promisify-core` is needed');
} else {
expected_count++;
fs.stat('.')
.then(function(stats) {
assert.ok(stats.hasOwnProperty('blksize'));
assert.ok(stats.hasOwnProperty('blocks'));
success_count++;
})
.catch(reportTestError);

expected_count++;
fs.stat('./does-not-exist')
.then(
function(stats) {
throw new Error('stat should have failed for unkown file');
},
function(err) {
assert.equal('ENOENT', err.code);
success_count++;
})
.catch(reportTestError);
}

fs.lstat('.', function(err, stats) {
if (err) {
got_error = true;
Expand Down Expand Up @@ -122,7 +149,11 @@ fs.stat(__filename, function(err, s) {
});

process.on('exit', function() {
assert.equal(5, success_count);
assert.equal(expected_count, success_count);
assert.equal(false, got_error);
});

function reportTestError(err) {
console.error('\nTEST FAILED\n', err.stack, '\n');
got_error = true;
}
1 change: 1 addition & 0 deletions tools/closure_linter/closure_linter.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
README
setup.cfg
setup.py
closure_linter/__init__.py
closure_linter/checker.py
Expand Down