Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/rel/1.0' into fwdport
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Zapata committed Apr 26, 2016
2 parents d6eb5dc + bf7af08 commit e58f2ce
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 67 deletions.
2 changes: 1 addition & 1 deletion errors/arsenalErrors.json
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@
},
"BadSessionIndex": {
"description": "session index not ok",
"code": 5045
"code": 5045
},
"Unauthorized": {
"description": "not authenticated",
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
db: require('./lib/db'),
shuffle: require('./lib/shuffle'),
errors: require('./lib/errors.js'),
shuffle: require('./lib/shuffle'),
stringHash: require('./lib/stringHash'),
};
2 changes: 1 addition & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class IndexTransaction {
// The array-of-operations variant of the `batch` method
// allows passing options such has `sync: true` whereas the
// chained form does not.
this.db.batch(this.operations, writeOptions, cb);
return this.db.batch(this.operations, writeOptions, cb);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function errorsGen() {

Object.keys(errorsObj)
.filter(index => index !== '_comment')
.forEach((index) => {
.forEach(index => {
errors[index] = new ArsenalError(index, errorsObj[index].code,
errorsObj[index].description);
});
Expand Down
43 changes: 21 additions & 22 deletions lib/shuffle.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const randomBytes = require('crypto').randomBytes;
*/
function bitsNeeded(number) {
if (number < 0) {
throw new Error("Input must be greater than or equal to zero");
throw new Error('Input must be greater than or equal to zero');
} else if (number === 0) {
return 1;
} else {
Expand All @@ -29,7 +29,7 @@ function bitsNeeded(number) {
}

/*
* Returns a "numbits"-long sequence of 1s *as a base-10 integer*.
* Returns a 'numbits'-long sequence of 1s *as a base-10 integer*.
* Sync function.
* @param {number} numbits - a positive integer
* @return {number} the sequence of 1s
Expand All @@ -38,25 +38,25 @@ function bitsNeeded(number) {
*/
function createMaskOnes(numBits) {
if (numBits < 0)
throw new Error("Input must be greater than or equal to zero");
throw new Error('Input must be greater than or equal to zero');
return Math.pow(2, numBits) - 1;
}

/*
* Returns a buffer of cryptographically secure pseudo-random bytes. The
* source of bytes is nodejs' crypto.randomBytes. Sync function.
* @param{number} howMany - the number of bytes to return
* @return {buffer} a InRangebuffer with "howMany" pseudo-random bytes.
* @return {buffer} a InRangebuffer with 'howMany' pseudo-random bytes.
* @throws Error if numBytes < 0 or if insufficient entropy
*/
function nextBytes(numBytes) {
if (numBytes < 0) {
throw new Error("Input must be greater than or equal to zero");
throw new Error('Input must be greater than or equal to zero');
}
try {
return randomBytes(numBytes);
} catch (ex) {
throw new Error("Insufficient entropy");
throw new Error('Insufficient entropy');
}
}

Expand All @@ -68,7 +68,7 @@ function nextBytes(numBytes) {
*/
function bitsToBytes(numBits) {
if (numBits < 0) {
throw new Error("Input must be greater than or equal to zero");
throw new Error('Input must be greater than or equal to zero');
}
return Math.ceil(numBits / 8);
}
Expand All @@ -84,25 +84,24 @@ function bitsToBytes(numBits) {
*/
function randomRange(min, max) {
if (max < min) {
throw new Error("Invalid range");
throw new Error('Invalid range');
}
if (min === max) {
return min;
} else if (min < max) {
const range = (max - min);
const bits = bitsNeeded(range);
// decide how many bytes we need to draw from nextBytes: drawing less
// bytes means being more efficient
const bytes = bitsToBytes(bits);
// we use a mask as an optimization: it increases the chances for the
// candidate to be in range
const mask = createMaskOnes(bits);
let candidate;
do {
candidate = parseInt(nextBytes(bytes).toString('hex'), 16) & mask;
} while (candidate > range);
return (candidate + min);
}
const range = (max - min);
const bits = bitsNeeded(range);
// decide how many bytes we need to draw from nextBytes: drawing less
// bytes means being more efficient
const bytes = bitsToBytes(bits);
// we use a mask as an optimization: it increases the chances for the
// candidate to be in range
const mask = createMaskOnes(bits);
let candidate;
do {
candidate = parseInt(nextBytes(bytes).toString('hex'), 16) & mask;
} while (candidate > range);
return (candidate + min);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions lib/stringHash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';// eslint-disable-line strict

/**
* This function compute a hash from a string
* https://github.com/darkskyapp/string-hash/blob/master/index.js
* @param {String} str - The string to compute the hash
* @return {Number} The computed hash
*/
function stringHash(str) {
let hash = 5381;
let i = str.length;

while (i) {
hash = hash * 33 ^ str.charCodeAt(--i);
}

/* JavaScript does bitwise operations (like XOR, above) on
* 32-bit signed integers. Since we want the results to be
* always positive, convert the signed int to an unsigned by
* doing an unsigned bitshift.
*/
return hash >>> 0;
}

module.exports = stringHash;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"dependencies": {
},
"devDependencies": {
"eslint": "^1.5.1",
"eslint-config-airbnb": "^1.0.2",
"eslint": "^2.4.0",
"eslint-config-airbnb": "^6.0.0",
"eslint-config-ironman": "scality/IronMan-Guidelines",
"level": "^1.3.0",
"mocha": "^2.3.3",
Expand All @@ -31,7 +31,7 @@
"lint": "eslint $(git ls-files '*.js')",
"lint_md": "mdlint $(git ls-files '*.md')",
"lint_yml": "yamllint $(git ls-files '*.yml')",
"test": "mocha tests/unit"
"test": "mocha --timeout 5500 tests/unit"
},
"private": true
}
Loading

0 comments on commit e58f2ce

Please sign in to comment.