Skip to content

Commit 271035a

Browse files
committed
Modernize JS
1 parent efe4968 commit 271035a

File tree

2 files changed

+64
-53
lines changed

2 files changed

+64
-53
lines changed

bcrypt.js

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1-
'use strict';
1+
const path = require('path');
2+
const bindings = require('node-gyp-build')(path.resolve(__dirname));
23

3-
var path = require('path');
4-
var bindings = require('node-gyp-build')(path.resolve(__dirname));
4+
const crypto = require('crypto');
55

6-
var crypto = require('crypto');
7-
8-
var promises = require('./promises');
6+
const promises = require('./promises');
97

108
/// generate a salt (sync)
119
/// @param {Number} [rounds] number of rounds (default 10)
1210
/// @return {String} salt
13-
module.exports.genSaltSync = function genSaltSync(rounds, minor) {
11+
function genSaltSync(rounds, minor) {
1412
// default 10 rounds
1513
if (!rounds) {
1614
rounds = 10;
1715
} else if (typeof rounds !== 'number') {
1816
throw new Error('rounds must be a number');
1917
}
2018

21-
if(!minor) {
19+
if (!minor) {
2220
minor = 'b';
23-
} else if(minor !== 'b' && minor !== 'a') {
21+
} else if (minor !== 'b' && minor !== 'a') {
2422
throw new Error('minor must be either "a" or "b"');
2523
}
2624

2725
return bindings.gen_salt_sync(minor, rounds, crypto.randomBytes(16));
28-
};
26+
}
2927

3028
/// generate a salt
3129
/// @param {Number} [rounds] number of rounds (default 10)
3230
/// @param {Function} cb callback(err, salt)
33-
module.exports.genSalt = function genSalt(rounds, minor, cb) {
34-
var error;
31+
function genSalt(rounds, minor, cb) {
32+
let error;
3533

3634
// if callback is first argument, then use defaults for others
3735
if (typeof arguments[0] === 'function') {
3836
// have to set callback first otherwise arguments are overridden
3937
cb = arguments[0];
4038
rounds = 10;
4139
minor = 'b';
42-
// callback is second argument
40+
// callback is second argument
4341
} else if (typeof arguments[1] === 'function') {
4442
// have to set callback first otherwise arguments are overridden
4543
cb = arguments[1];
@@ -56,35 +54,35 @@ module.exports.genSalt = function genSalt(rounds, minor, cb) {
5654
} else if (typeof rounds !== 'number') {
5755
// callback error asynchronously
5856
error = new Error('rounds must be a number');
59-
return process.nextTick(function() {
57+
return process.nextTick(function () {
6058
cb(error);
6159
});
6260
}
6361

64-
if(!minor) {
62+
if (!minor) {
6563
minor = 'b'
66-
} else if(minor !== 'b' && minor !== 'a') {
64+
} else if (minor !== 'b' && minor !== 'a') {
6765
error = new Error('minor must be either "a" or "b"');
68-
return process.nextTick(function() {
66+
return process.nextTick(function () {
6967
cb(error);
7068
});
7169
}
7270

73-
crypto.randomBytes(16, function(error, randomBytes) {
71+
crypto.randomBytes(16, function (error, randomBytes) {
7472
if (error) {
7573
cb(error);
7674
return;
7775
}
7876

7977
bindings.gen_salt(minor, rounds, randomBytes, cb);
8078
});
81-
};
79+
}
8280

8381
/// hash data using a salt
8482
/// @param {String|Buffer} data the data to encrypt
8583
/// @param {String} salt the salt to use when hashing
8684
/// @return {String} hash
87-
module.exports.hashSync = function hashSync(data, salt) {
85+
function hashSync(data, salt) {
8886
if (data == null || salt == null) {
8987
throw new Error('data and salt arguments required');
9088
}
@@ -98,25 +96,25 @@ module.exports.hashSync = function hashSync(data, salt) {
9896
}
9997

10098
return bindings.encrypt_sync(data, salt);
101-
};
99+
}
102100

103101
/// hash data using a salt
104102
/// @param {String|Buffer} data the data to encrypt
105103
/// @param {String} salt the salt to use when hashing
106104
/// @param {Function} cb callback(err, hash)
107-
module.exports.hash = function hash(data, salt, cb) {
108-
var error;
105+
function hash(data, salt, cb) {
106+
let error;
109107

110108
if (typeof data === 'function') {
111109
error = new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
112-
return process.nextTick(function() {
110+
return process.nextTick(function () {
113111
data(error);
114112
});
115113
}
116114

117115
if (typeof salt === 'function') {
118116
error = new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
119-
return process.nextTick(function() {
117+
return process.nextTick(function () {
120118
salt(error);
121119
});
122120
}
@@ -133,33 +131,33 @@ module.exports.hash = function hash(data, salt, cb) {
133131

134132
if (data == null || salt == null) {
135133
error = new Error('data and salt arguments required');
136-
return process.nextTick(function() {
134+
return process.nextTick(function () {
137135
cb(error);
138136
});
139137
}
140138

141139
if (!(typeof data === 'string' || data instanceof Buffer) || (typeof salt !== 'string' && typeof salt !== 'number')) {
142140
error = new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
143-
return process.nextTick(function() {
141+
return process.nextTick(function () {
144142
cb(error);
145143
});
146144
}
147145

148146

149147
if (typeof salt === 'number') {
150-
return module.exports.genSalt(salt, function(err, salt) {
148+
return module.exports.genSalt(salt, function (err, salt) {
151149
return bindings.encrypt(data, salt, cb);
152150
});
153151
}
154152

155153
return bindings.encrypt(data, salt, cb);
156-
};
154+
}
157155

158156
/// compare raw data to hash
159157
/// @param {String|Buffer} data the data to hash and compare
160158
/// @param {String} hash expected hash
161159
/// @return {bool} true if hashed data matches hash
162-
module.exports.compareSync = function compareSync(data, hash) {
160+
function compareSync(data, hash) {
163161
if (data == null || hash == null) {
164162
throw new Error('data and hash arguments required');
165163
}
@@ -169,25 +167,25 @@ module.exports.compareSync = function compareSync(data, hash) {
169167
}
170168

171169
return bindings.compare_sync(data, hash);
172-
};
170+
}
173171

174172
/// compare raw data to hash
175173
/// @param {String|Buffer} data the data to hash and compare
176174
/// @param {String} hash expected hash
177175
/// @param {Function} cb callback(err, matched) - matched is true if hashed data matches hash
178-
module.exports.compare = function compare(data, hash, cb) {
179-
var error;
176+
function compare(data, hash, cb) {
177+
let error;
180178

181179
if (typeof data === 'function') {
182180
error = new Error('data and hash arguments required');
183-
return process.nextTick(function() {
181+
return process.nextTick(function () {
184182
data(error);
185183
});
186184
}
187185

188186
if (typeof hash === 'function') {
189187
error = new Error('data and hash arguments required');
190-
return process.nextTick(function() {
188+
return process.nextTick(function () {
191189
hash(error);
192190
});
193191
}
@@ -204,24 +202,24 @@ module.exports.compare = function compare(data, hash, cb) {
204202

205203
if (data == null || hash == null) {
206204
error = new Error('data and hash arguments required');
207-
return process.nextTick(function() {
205+
return process.nextTick(function () {
208206
cb(error);
209207
});
210208
}
211209

212210
if (!(typeof data === 'string' || data instanceof Buffer) || typeof hash !== 'string') {
213211
error = new Error('data and hash must be strings');
214-
return process.nextTick(function() {
212+
return process.nextTick(function () {
215213
cb(error);
216214
});
217215
}
218216

219217
return bindings.compare(data, hash, cb);
220-
};
218+
}
221219

222220
/// @param {String} hash extract rounds from this hash
223221
/// @return {Number} the number of rounds used to encrypt a given hash
224-
module.exports.getRounds = function getRounds(hash) {
222+
function getRounds(hash) {
225223
if (hash == null) {
226224
throw new Error('hash argument required');
227225
}
@@ -231,4 +229,14 @@ module.exports.getRounds = function getRounds(hash) {
231229
}
232230

233231
return bindings.get_rounds(hash);
234-
};
232+
}
233+
234+
module.exports = {
235+
genSaltSync,
236+
genSalt,
237+
hashSync,
238+
hash,
239+
compareSync,
240+
compare,
241+
getRounds,
242+
}

promises.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
'use strict';
2-
3-
var Promise = global.Promise;
1+
let Promise = global.Promise;
42

53
/// encapsulate a method with a node-style callback in a Promise
64
/// @param {object} 'this' of the encapsulated function
75
/// @param {function} function to be encapsulated
86
/// @param {Array-like} args to be passed to the called function
97
/// @return {Promise} a Promise encapsulating the function
10-
module.exports.promise = function (fn, context, args) {
11-
8+
function promise(fn, context, args) {
129
if (!Array.isArray(args)) {
1310
args = Array.prototype.slice.call(args);
1411
}
@@ -17,8 +14,8 @@ module.exports.promise = function (fn, context, args) {
1714
return Promise.reject(new Error('fn must be a function'));
1815
}
1916

20-
return new Promise(function(resolve, reject) {
21-
args.push(function(err, data) {
17+
return new Promise((resolve, reject) => {
18+
args.push((err, data) => {
2219
if (err) {
2320
reject(err);
2421
} else {
@@ -28,15 +25,21 @@ module.exports.promise = function (fn, context, args) {
2825

2926
fn.apply(context, args);
3027
});
31-
};
28+
}
3229

3330
/// @param {err} the error to be thrown
34-
module.exports.reject = function (err) {
31+
function reject(err) {
3532
return Promise.reject(err);
36-
};
33+
}
3734

3835
/// changes the promise implementation that bcrypt uses
3936
/// @param {Promise} the implementation to use
40-
module.exports.use = function(promise) {
41-
Promise = promise;
42-
};
37+
function use(promise) {
38+
Promise = promise;
39+
}
40+
41+
module.exports = {
42+
promise,
43+
reject,
44+
use
45+
}

0 commit comments

Comments
 (0)