koa-generic-session
Advanced tools
Comparing version 1.7.0 to 1.8.0
1.8.0 / 2015-03-15 | ||
================== | ||
* feat: support reconnectTimeout | ||
1.7.0 / 2015-03-01 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -61,2 +61,3 @@ /**! | ||
var errorHandler = options.errorHandler || defaultErrorHanlder; | ||
var reconnectTimeout = options.reconnectTimeout || 10000; | ||
@@ -73,3 +74,4 @@ var store = new Store(client, { | ||
var storeAvailable = true; | ||
var storeStatus = 'avaliable'; | ||
var waitStore = Promise.resolve(); | ||
@@ -81,5 +83,20 @@ // notify user that this store is not | ||
store.on('disconnect', function() { storeAvailable = false; }); | ||
store.on('connect', function() { storeAvailable = true; }); | ||
store.on('disconnect', function() { | ||
if (storeStatus !== 'avaliable') return; | ||
storeStatus = 'pending'; | ||
waitStore = new Promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
if (storeStatus === 'pending') storeStatus = 'unavaliable'; | ||
reject(new Error('session store is unavaliable')); | ||
}, reconnectTimeout); | ||
store.once('connect', resolve); | ||
}); | ||
}); | ||
store.on('connect', function() { | ||
storeStatus = 'avaliable'; | ||
waitStore = Promise.resolve(); | ||
}); | ||
// save empty session hash for compare | ||
@@ -123,9 +140,11 @@ var EMPTY_SESSION_HASH = hash(generateSession()); | ||
function *getSession() { | ||
if (!storeAvailable) { | ||
debug('store is disconnect'); | ||
throw new Error('session store error'); | ||
if (!matchPath(this)) return; | ||
if (storeStatus === 'pending') { | ||
debug('store is disconnect and pending'); | ||
yield waitStore; | ||
} else if (storeStatus === 'unavaliable') { | ||
debug('store is unavaliable'); | ||
throw new Error('session store is unavaliable'); | ||
} | ||
if (!matchPath(this)) return; | ||
if (!this.sessionId) { | ||
@@ -132,0 +151,0 @@ this.sessionId = this.cookies.get(key, { |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/generic-session", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"keywords": ["koa", "middleware", "session"], | ||
@@ -8,0 +8,0 @@ "author": "dead_horse <dead_horse@qq.com>", |
@@ -102,2 +102,3 @@ generic-session | ||
* `errorHandler(err, type, ctx)`: `Store.get` and `Store.set` will throw in some situation, use `errorHandle` to handle these errors by yourself. Default will throw. | ||
* `reconnectTimeout`: When store is disconnected, don't throw `store unavaliable` error immediately, wait `reconnectTimeout` to reconnect, default is `10s`. | ||
@@ -104,0 +105,0 @@ * Store can be any Object that has the methods `set`, `get`, `destroy` like [MemoryStore](https://github.com/koajs/koa-session/blob/master/lib/store.js). |
@@ -44,2 +44,32 @@ /**! | ||
it('should get session ok when reconnect', function (done) { | ||
commonApp.store.emit('disconnect'); | ||
setTimeout(function () { | ||
commonApp.store.emit('connect'); | ||
}, 10); | ||
request(commonApp) | ||
.get('/session/get') | ||
.expect(200) | ||
.expect('1', done); | ||
}); | ||
it('should ignore disconnect event', function (done) { | ||
commonApp.store.emit('disconnect'); | ||
commonApp.store.emit('disconnect'); | ||
request(commonApp) | ||
.get('/session/get') | ||
.expect(500) | ||
.expect('Internal Server Error', done); | ||
}); | ||
it('should error when status is unavaliable', function (done) { | ||
commonApp.store.emit('disconnect'); | ||
setTimeout(function () { | ||
request(commonApp) | ||
.get('/session/get') | ||
.expect(500) | ||
.expect('Internal Server Error', done); | ||
}, 200); | ||
}); | ||
it('should get session ok when store.get error but session not exist', function (done) { | ||
@@ -46,0 +76,0 @@ mm.error(commonApp.store, 'get', 'mock error'); |
@@ -35,3 +35,4 @@ /**! | ||
defer: true, | ||
store: store | ||
store: store, | ||
reconnectTimeout: 100 | ||
})); | ||
@@ -38,0 +39,0 @@ |
@@ -48,3 +48,4 @@ /**! | ||
return uid(len) + this.request.query.test_sid_append; | ||
} | ||
}, | ||
reconnectTimeout: 100 | ||
})); | ||
@@ -51,0 +52,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50265
1261
156