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

crypto: add sync methods, deprecate optional callbacks #189

Closed
wants to merge 1 commit into from
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
14 changes: 9 additions & 5 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,11 +657,15 @@ exports.setEngine = function setEngine(id, flags) {
return binding.setEngine(id, flags);
};

exports.randomBytes = randomBytes;
exports.pseudoRandomBytes = pseudoRandomBytes;

exports.rng = randomBytes;
exports.prng = pseudoRandomBytes;
exports.randomBytes = util.deprecateSync(randomBytes, '`crypto.randomBytes(size)` is deprecated. Please use `crypto.randomBytesSync(size)` instead.');
exports.randomBytesSync = randomBytes;
exports.pseudoRandomBytes = util.deprecateSync(pseudoRandomBytes, '`crypto.pseudoRandomBytes(size)` is deprecated. Please use `crypto.pseudoRandomBytesSync(size)` instead.');
exports.pseudoRandomBytesSync = pseudoRandomBytes;

exports.rng = util.deprecateSync(randomBytes, '`crypto.rng(size)` is deprecated. Please us `crypto.rngSync(size)` instead.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“Please us” → “Please use” for these last two messages.

exports.rngSync = randomBytes;
exports.prng = util.deprecateSync(pseudoRandomBytes, '`crypto.prng(size)` is deprecated. Please us `crypto.prngSync(size)` instead.');
exports.prngSync = pseudoRandomBytes;


exports.getCiphers = function() {
Expand Down
36 changes: 36 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,42 @@ exports.deprecate = function(fn, msg) {
return deprecated;
};

// Mark that the synchronous version of a function is deprecated.
// This is essentially the same as `exports.deprecate()`,
// except it only warns if the last argument is not a callback function.
exports.deprecateSync = function(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (isUndefined(global.process)) {
return function() {
return exports.deprecateSync(fn, msg).apply(this, arguments);
};
}

if (process.noDeprecation === true) {
return fn;
}

var warned = false;
function deprecated() {
var cb = arguments[arguments.length - 1];
if (isFunction(cb))
return fn.apply(this, arguments);

if (!warned) {
if (process.throwDeprecation) {
throw new Error(msg);
} else if (process.traceDeprecation) {
console.trace(msg);
} else {
console.error(msg);
}
warned = true;
}
return fn.apply(this, arguments);
}

return deprecated;
};

var debugs = {};
var debugEnviron;
Expand Down