From 7cba707799e13722ca067cce9502525068c36fd9 Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 27 Oct 2023 19:18:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=842.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 97 ++++++++++++++++++++++++---------------------- lib/redis-store.js | 24 +++++++----- 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/index.js b/index.js index 739f600..af5cb70 100644 --- a/index.js +++ b/index.js @@ -9,56 +9,61 @@ import { uuid, sha1 } from 'crypto.js' import RedisStore from './lib/redis-store.js' // 会话安装包 -export const sessionPackage = { - name: 'session', - install() { - var session = this.get('session') - // 这里只创建session的存储器, 而初始化操作在中间件中进行 - return new RedisStore(session) +export function createSessionPlugin(){ + + return { + name: 'session', + install() { + var session = this.get('session') + // 这里只创建session的存储器, 而初始化操作在中间件中进行 + return new RedisStore(session) + } } } // 会话中间件 -export function sessionConnect(req, res, next) { - var opt = this.get('session') - var cache = req.cookie('NODESSID') - var deviceID = '' - var ssid - - // options请求不处理会话 - if (req.method === 'OPTIONS') { - return next() - } - - // 校验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) - } +export function createSession(){ + return function (req, res, next) { + var opt = this.get('session') + var cache = req.cookie('NODESSID') + var deviceID = '' + var ssid + + // options请求不处理会话 + if (req.method === 'OPTIONS') { + return next() } - } else { - ssid = cache || sha1(uuid()) + + // 校验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, { maxAge: opt.ttl, domain: opt.domain }) + // 缓存ssid到req上 + req.ssid = ssid + this.$$session.update(ssid) + + next() } - - res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain }) - // 缓存ssid到req上 - req.ssid = ssid - this.$$session.start(ssid) - - next() } diff --git a/lib/redis-store.js b/lib/redis-store.js index 68d6287..66c14ca 100644 --- a/lib/redis-store.js +++ b/lib/redis-store.js @@ -7,25 +7,29 @@ import Ioredis from 'ioredis' export default class Session { + + #store = null + #ttl = 60 + constructor(opt) { - this.store = new Ioredis({ + this.#store = new Ioredis({ host: opt.db.host || '127.0.0.1', port: opt.db.port || 6379, db: opt.db.db || 0 }) - this.ttl = opt.ttl + this.#ttl = opt.ttl } - start(ssid) { + update(ssid) { // 设置session有效期 - this.store.expire(ssid, this.ttl) + this.#store.expire(ssid, this.#ttl) } // 获取session字段值, 需要await指令 get(ssid, key) { var defer = Promise.defer() - this.store.hgetall(ssid, (err, obj) => { + this.#store.hgetall(ssid, (err, obj) => { if (err) { return defer.reject(err) } @@ -37,7 +41,7 @@ export default class Session { } //不传key时,直接返回全部字段 if (key) { - defer.resolve(obj.hasOwnProperty(key) ? obj[key] : null) + defer.resolve(obj[key]) } else { defer.resolve(obj) } @@ -49,20 +53,20 @@ export default class Session { set(ssid, key, val) { if (typeof key === 'object') { for (let i in key) { - this.store.hset(ssid, i, key[i]) + this.#store.hset(ssid, i, key[i]) } } else { - this.store.hset(ssid, key, val) + this.#store.hset(ssid, key, val) } } //删除单个字段 unset(ssid, key) { - this.store.hdel(ssid, key) + this.#store.hdel(ssid, key) } //清除个人session clear(ssid) { - this.store.del(ssid) + this.#store.del(ssid) } }