diff --git a/index.js b/index.js index a75bd77..57a0a72 100644 --- a/index.js +++ b/index.js @@ -9,8 +9,8 @@ import { uuid, sha1 } from 'crypto.js' import RedisStore from './lib/redis-store.js' import MemStore from './lib/mem-store.js' -// 会话储存器 -export const sessionStore = { +// 会话安装包 +export const sessionPackage = { name: 'session', install() { var session = this.get('session') @@ -25,9 +25,10 @@ export const sessionStore = { } // 会话中间件 -export function sessionWare(req, res, next) { +export function sessionConnect(req, res, next) { var opt = this.get('session') - var jwt = this.get('jwt') + var cache = req.cookie('NODESSID') + var deviceID = '' var ssid // options请求不处理会话 @@ -35,45 +36,35 @@ export function sessionWare(req, res, next) { return next() } - // jwt模式的校验不在这里处理 - if (jwt) { - var auth = req.header('authorization') - if (auth) { - ssid = auth.split('.').pop() - this.$$session.start(ssid) + // 校验UA + if (opt.level & 2) { + deviceID += req.header('user-agent') + } + + // 校验IP + if (opt.level & 4) { + deviceID += req.ip() + } + + if (deviceID) { + deviceID = sha1(deviceID) + + // ssid 最后16位是指纹 + if (cache) { + if (cache.slice(-16) === deviceID.slice(-16)) { + ssid = cache + } else { + ssid = uuid('') + deviceID.slice(-16) + } } } else { - var cache = req.cookie('NODESSID') - var deviceID = '' - - // 校验UA - if (opt.level & 2) { - deviceID += req.header('user-agent') - } - - // 校验IP - if (opt.level & 4) { - deviceID += req.ip() - } - - if (deviceID) { - deviceID = sha1(deviceID) - - // ssid 最后16位是指纹 - if (cache) { - if (cache.slice(-16) === deviceID.slice(-16)) { - ssid = cache - } else { - ssid = uuid('') + deviceID.slice(-16) - } - } - } else { - ssid = cache || sha1(uuid()) - } - - res.cookie('NODESSID', ssid) - this.$$session.start(ssid) + ssid = cache || sha1(uuid()) } + res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain }) + // 缓存ssid到req上 + req.ssid = ssid + this.$$session.start(ssid) + next() } diff --git a/lib/mem-store.js b/lib/mem-store.js index 98b99bc..0dc8f6b 100644 --- a/lib/mem-store.js +++ b/lib/mem-store.js @@ -19,16 +19,14 @@ export default class Session { this.ttl = opt.ttl } - start(ssid, oldssid) { + start(ssid) { var session = this.store[ssid] - this.ssid = ssid - // 内存版会话管理, 没有设计计划任务来清理过期数据 // 需要在初始化时先判断, 过期的自动清除, 没过期的, 直接重新续期 if (session) { if (Date.now() > session.__expires__) { - this.clear() + this.clear(ssid) } } else { session = this.store[ssid] = {} @@ -39,28 +37,28 @@ export default class Session { } // 获取session字段值 - get(key) { - return key ? this.store[this.ssid][key] || null : this.store[this.ssid] + get(ssid, key) { + return key ? this.store[ssid][key] || null : this.store[ssid] } // 设置session字段值 - set(key, val) { + set(ssid, key, val) { if (typeof key === 'object') { for (let i in key) { - this.store[this.ssid][i] = key[i] + this.store[ssid][i] = key[i] } } else { - this.store[this.ssid][key] = val + this.store[ssid][key] = val } } // 删除单个字段 - unset(key) { - delete this.store[this.ssid][key] + unset(ssid, key) { + delete this.store[ssid][key] } // 清除个人session - clear() { - this.store[this.ssid] = {} + clear(ssid) { + this.store[ssid] = {} } } diff --git a/lib/redis-store.js b/lib/redis-store.js index e618ab3..e3e87ab 100644 --- a/lib/redis-store.js +++ b/lib/redis-store.js @@ -17,16 +17,15 @@ export default class Session { } start(ssid) { - this.ssid = ssid // 设置session有效期 this.store.expire(ssid, this.ttl) } // 获取session字段值, 需要await指令 - get(key) { + get(ssid, key) { var defer = Promise.defer() - this.store.hgetall(this.ssid, (err, obj) => { + this.store.hgetall(ssid, (err, obj) => { if (err) { return defer.reject(err) } @@ -47,23 +46,23 @@ export default class Session { } //设置session字段值 - set(key, val) { + set(ssid, key, val) { if (typeof key === 'object') { for (let i in key) { - this.store.hset(this.ssid, i, key[i]) + this.store.hset(ssid, i, key[i]) } } else { - this.store.hset(this.ssid, key, val) + this.store.hset(ssid, key, val) } } //删除单个字段 - unset(key) { - this.store.hdel(this.ssid, key) + unset(ssid, key) { + this.store.hdel(ssid, key) } //清除个人session - clear() { - this.store.del(this.ssid) + clear(ssid) { + this.store.del(ssid) } }