abtest
Advanced tools
Comparing version 1.0.2 to 1.1.0
1.1.0 / 2014-11-08 | ||
================== | ||
* pin autod@1, update mocha | ||
* add crc, insure buckets change let cookie expire | ||
1.0.2 / 2014-09-30 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -13,2 +13,3 @@ /*! | ||
var assert = require('assert'); | ||
var crc = require('crc').crc32; | ||
@@ -37,2 +38,3 @@ /** | ||
this.buckets = {}; | ||
this.crc = hash(this.buckets); | ||
this.enableCookie = true; | ||
@@ -56,3 +58,3 @@ this.enableQuery = true; | ||
* @param {Object} config | ||
* - {Object} Bucket | ||
* - {Object} buckets | ||
* - {String} [cookie] cookie name, default to 'abtest' | ||
@@ -66,2 +68,3 @@ * - {Function} [method] method, default to 'random' | ||
*/ | ||
ABTest.prototype.configure = function (config) { | ||
@@ -72,8 +75,11 @@ if (!config) { | ||
for (var i = 0; i < KEYS.length; i++) { | ||
var key = KEYS[i]; | ||
if (config.hasOwnProperty(key)) { | ||
this[key] = config[key]; | ||
} | ||
} | ||
var abtest = this; | ||
KEYS.forEach(function (key) { | ||
if (!config.hasOwnProperty(key)) return; | ||
abtest[key] = config[key]; | ||
// set crc | ||
if (key === 'buckets') abtest.crc = hash(abtest.buckets); | ||
}); | ||
}; | ||
@@ -95,6 +101,15 @@ | ||
if (!this.ctx.getCookie) return null; | ||
var bucket = this.ctx.getCookie(this.cookie); | ||
var cookie = this.ctx.getCookie(this.cookie); | ||
// can not get cookie | ||
if (!cookie) return null; | ||
cookie = cookie.split(':'); | ||
var bucket = cookie[0]; | ||
var crcInfo = cookie[1]; | ||
// buckets changed | ||
if (crcInfo !== this.crc) return null; | ||
// not valid | ||
if (!this.buckets.hasOwnProperty(bucket)) return null; | ||
debug('get bucket `%s` from cookie', bucket); | ||
@@ -121,4 +136,5 @@ return bucket; | ||
if (this.enableCookie && this.ctx.setCookie) { | ||
debug('set cookie %s=%s', this.cookie, this._bucket); | ||
this.ctx.setCookie(this.cookie, this._bucket); | ||
var cookie = this._bucket + ':' + this.crc; | ||
debug('set cookie %s=%s', this.cookie, cookie); | ||
this.ctx.setCookie(this.cookie, cookie); | ||
} | ||
@@ -130,1 +146,11 @@ } | ||
}); | ||
/** | ||
* hash buckets into a seperate string | ||
* @param {Object} buckets | ||
* @return {String} | ||
*/ | ||
function hash(buckets) { | ||
return String(crc(JSON.stringify(buckets))); | ||
} |
{ | ||
"name": "abtest", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "an A/B test client for node web", | ||
@@ -26,10 +26,11 @@ "main": "lib/abtest.js", | ||
"dependencies": { | ||
"debug": "~2.0.0" | ||
"crc": "~3.0.0", | ||
"debug": "~2.1.0" | ||
}, | ||
"devDependencies": { | ||
"autod": "~0.3.2", | ||
"autod": "1", | ||
"istanbul-harmony": "~0.3.0", | ||
"koa": "~0.12.2", | ||
"mocha": "~1.21.4", | ||
"should": "~4.0.4", | ||
"koa": "~0.13.0", | ||
"mocha": "~2.0.1", | ||
"should": "~4.2.1", | ||
"supertest": "~0.14.0" | ||
@@ -36,0 +37,0 @@ }, |
@@ -32,2 +32,9 @@ abtest | ||
## Feature | ||
- Random split user into different buckets. | ||
- Record user's bucket in cookie. | ||
- Force choose bucket by query. | ||
- Expire cookie when buckets changed. | ||
## Usage | ||
@@ -34,0 +41,0 @@ |
7200
162
74
2
+ Addedcrc@~3.0.0
+ Addedcrc@3.0.0(transitive)
+ Addeddebug@2.1.3(transitive)
+ Addedms@0.7.0(transitive)
- Removeddebug@2.0.0(transitive)
- Removedms@0.6.2(transitive)
Updateddebug@~2.1.0