fixed merged

v1
宇天 2021-01-26 14:02:15 +08:00
commit ecaece0463
3 changed files with 7 additions and 73 deletions

View File

@ -7,20 +7,14 @@
import { uuid, sha1 } from 'crypto.js'
import RedisStore from './lib/redis-store.js'
import MemStore from './lib/mem-store.js'
// 会话安装包
export const sessionPackage = {
name: 'session',
install() {
var session = this.get('session')
// 这里只创建session的存储器, 而初始化操作在中间件中进行
if (session.type === 'redis') {
return new RedisStore(session)
} else {
return new MemStore(session)
}
return new RedisStore(session)
}
}

View File

@ -1,64 +0,0 @@
/**
* 内存版会话管理(仅用于测试,数据)
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/18 16:35:26
*/
function hideProperty(host, name, value) {
Object.defineProperty(host, name, {
value: value,
writable: true,
enumerable: false,
configurable: true
})
}
export default class Session {
constructor(opt) {
this.store = Object.create(null)
this.ttl = opt.ttl
}
start(ssid) {
var session = this.store[ssid]
// 内存版会话管理, 没有设计计划任务来清理过期数据
// 需要在初始化时先判断, 过期的自动清除, 没过期的, 直接重新续期
if (session) {
if (Date.now() > session.__expires__) {
this.clear(ssid)
}
} else {
session = this.store[ssid] = {}
}
// 设置session有效期
hideProperty(session, '__expires__', Date.now() + this.ttl * 1000)
}
// 获取session字段值
get(ssid, key) {
return key ? this.store[ssid][key] || null : this.store[ssid]
}
// 设置session字段值
set(ssid, key, val) {
if (typeof key === 'object') {
for (let i in key) {
this.store[ssid][i] = key[i]
}
} else {
this.store[ssid][key] = val
}
}
// 删除单个字段
unset(ssid, key) {
delete this.store[ssid][key]
}
// 清除个人session
clear(ssid) {
this.store[ssid] = {}
}
}

View File

@ -1,11 +1,15 @@
{
"name": "@gm5/session",
"version": "1.0.1",
"version": "1.1.2",
"type": "module",
"description": "会话中间件。",
"main": "index.js",
"author": "yutent <yutent.io@gmail.com>",
"keywords": ["fivejs", "controller", "http"],
"keywords": ["fivejs", "session", "http"],
"dependencies": {
"crypto.js": "^2.0.2",
"ioredis": "^4.17.3"
},
"repository": "https://github.com/bytedo/gmf.session.git",
"license": "MIT"
}