node-rollout
Advanced tools
Comparing version 0.3.0 to 0.4.0
117
index.js
var crypto = require('crypto') | ||
, util = require('util') | ||
, when = require('when') | ||
, Promise = require('bluebird') | ||
, EventEmitter = require('events').EventEmitter | ||
, alpha = 'abcdefghijklmnopqrstuvwxyz'.split('') | ||
, letters = /([a-z])/g | ||
@@ -26,3 +24,3 @@ function defaultCondition() { | ||
var self = this | ||
self._handlers[key] = flags | ||
this._handlers[key] = flags | ||
var orig_percentages = [] | ||
@@ -33,3 +31,3 @@ var keys = Object.keys(flags).map(function (k) { | ||
}) | ||
self.client.mget(keys, function (err, percentages) { | ||
this.client.mget(keys, function (err, percentages) { | ||
var _keys = [] | ||
@@ -56,8 +54,14 @@ var nullKey = false | ||
var multi = this.client.multi() | ||
var self = this | ||
var settler = when.settle(keys.map(function (k) { | ||
return self.get(k[0], k[1], k[2], multi) | ||
})) | ||
multi.exec(function () {}) | ||
return settler | ||
var promises = keys.map(function (k) { | ||
return this.get(k[0], k[1], k[2], multi) | ||
}.bind(this)) | ||
return new Promise(function (resolve, reject) { | ||
multi.exec(function (err, result) { | ||
if (err) return reject(err) | ||
resolve(result) | ||
}) | ||
}) | ||
.then(function () { | ||
return Promise.all(promises.map(function (p) { return p.reflect() })) | ||
}) | ||
} | ||
@@ -73,29 +77,34 @@ | ||
if (!opt_values.id) opt_values.id = id | ||
return when.promise(function (resolve, reject) { | ||
var keys = Object.keys(flags).map(function (k) { | ||
return key + ':' + k | ||
var keys = Object.keys(flags).map(function (k) { | ||
return key + ':' + k | ||
}) | ||
var client = multi || this.client | ||
return new Promise(function (resolve, reject) { | ||
client.mget(keys, function (err, result) { | ||
if (err) return reject(err) | ||
resolve(result) | ||
}) | ||
var client = multi || this.client | ||
client.mget(keys, function (err, percentages) { | ||
var i = 0 | ||
var deferreds = [] | ||
for (var modifier in flags) { | ||
// in the circumstance that the key is not found, default to original value | ||
if (percentages[i] === null) { | ||
percentages[i] = flags[modifier].percentage | ||
}) | ||
.then(function (percentages) { | ||
var i = 0 | ||
var deferreds = [] | ||
for (var modifier in flags) { | ||
// in the circumstance that the key is not found, default to original value | ||
if (percentages[i] === null) { | ||
percentages[i] = flags[modifier].percentage | ||
} | ||
if (likely < percentages[i]) { | ||
if (!flags[modifier].condition) flags[modifier].condition = defaultCondition | ||
var output = flags[modifier].condition(opt_values[modifier]) | ||
if (output) { | ||
if (typeof output.then === 'function') deferreds.push(output) | ||
else return true | ||
} | ||
if (likely < percentages[i]) { | ||
if (!flags[modifier].condition) flags[modifier].condition = defaultCondition | ||
var output = flags[modifier].condition(opt_values[modifier]) | ||
if (when.isPromiseLike(output)) deferreds.push(output) | ||
else if (output) return resolve(true) | ||
} | ||
i++ | ||
} | ||
if (deferreds.length) { | ||
when.any(deferreds).then(resolve, reject) | ||
} else { | ||
reject() | ||
} | ||
}) | ||
i++ | ||
} | ||
if (deferreds.length) { | ||
return Promise.any(deferreds) | ||
} | ||
throw new Error('Conditions do not exist') | ||
}.bind(this)) | ||
@@ -105,14 +114,15 @@ } | ||
Rollout.prototype.update = function (key, percentage_map) { | ||
var self = this | ||
return when.promise(function (resolve) { | ||
var keys = [] | ||
for (var k in percentage_map) { | ||
keys.push(key + ':' + k, percentage_map[k]) | ||
} | ||
self.client.mset(keys, resolve) | ||
}) | ||
var keys = [] | ||
for (var k in percentage_map) { | ||
keys.push(key + ':' + k, percentage_map[k]) | ||
} | ||
return new Promise(function (resolve, reject) { | ||
this.client.mset(keys, function (err, result) { | ||
if (err) return reject(err) | ||
resolve(result) | ||
}) | ||
}.bind(this)) | ||
} | ||
Rollout.prototype.mods = function (name) { | ||
var client = this.client | ||
var keys = [] | ||
@@ -124,4 +134,9 @@ var names = [] | ||
} | ||
return when.promise(function (resolve) { | ||
client.mget(keys, function (err, values) { | ||
return new Promise(function (resolve, reject) { | ||
this.client.mget(keys, function (err, result) { | ||
if (err) return reject(err) | ||
resolve(result) | ||
}) | ||
}.bind(this)) | ||
.then(function (values) { | ||
var flags = {} | ||
@@ -131,5 +146,4 @@ values.forEach(function (val, i) { | ||
}) | ||
resolve(flags) | ||
return flags | ||
}) | ||
}) | ||
} | ||
@@ -142,6 +156,5 @@ | ||
Rollout.prototype.val_to_percent = function (text) { | ||
var n = crypto.createHash('md5').update(text).digest('hex').replace(letters, function (_, letter) { | ||
return alpha.indexOf(letter) | ||
}) | ||
return parseFloat(n.substr(0, 2) + '.' + n.substr(2, 3)) | ||
var n = crypto.createHash('md5').update(text).digest('hex') | ||
n = n.slice(0, n.length/2) | ||
return parseInt(n, 16) / parseInt(n.split('').map(function () { return 'f' }).join(''), 16) * 100 | ||
} |
{ | ||
"name": "node-rollout", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "feature rollout management", | ||
"author": "Dustin Diaz", | ||
"author": "Ali Faiz & Dustin Diaz", | ||
"license": "MIT", | ||
@@ -16,7 +16,7 @@ "main": "index.js", | ||
"dependencies": { | ||
"when": "2.8.0" | ||
"bluebird": "^3.4.6" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ded/node-rollout.git" | ||
"url": "https://github.com/mix/node-rollout.git" | ||
}, | ||
@@ -23,0 +23,0 @@ "devDependencies": { |
## Node Rollout | ||
[![CircleCI](https://circleci.com/gh/mix/node-rollout/tree/master.svg?style=svg)](https://circleci.com/gh/mix/node-rollout/tree/master) | ||
Feature rollout management for Node.js built on Redis | ||
@@ -106,3 +108,3 @@ | ||
results.forEach(function (r) { | ||
console.log(i.state) // 'fulfilled' || 'rejected' | ||
console.log(i.isFulfilled()) // Or 'isRejected()' | ||
}) | ||
@@ -192,3 +194,3 @@ }) | ||
### Tests | ||
see [tests/index.js](tests/index.js) | ||
see [tests/index-test.js](tests/index-test.js) | ||
@@ -195,0 +197,0 @@ ``` sh |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
78936
16
379
202
2
+ Addedbluebird@^3.4.6
+ Addedbluebird@3.7.2(transitive)
- Removedwhen@2.8.0
- Removedwhen@2.8.0(transitive)