Compare commits

..

No commits in common. "master" and "v1" have entirely different histories.
master ... v1

4 changed files with 65 additions and 101 deletions

View File

@ -1,6 +1,4 @@
![module info](https://nodei.co/npm/@gm5/session.png?downloads=true&downloadRank=true&stars=true)
![downloads](https://img.shields.io/npm/dt/@gm5/session.svg)
![version](https://img.shields.io/npm/v/@gm5/session.svg)
# @gm5/session # @gm5/session
@ -9,20 +7,17 @@
## 安装 ## 安装
```bash ```bash
npm i @gm5/session npm install @gm5/session
``` ```
## 使用 ## 使用
```js ```js
import { createApp } from '@gm5/core import { sessionPackage, sessionConnect } from '@gm5/session'
import { createSession, SessionModule } from '@gm5/session'
const app = createApp() app.install(sessionPackage)
app.install(SessionModule, {domain: 'your_domain'}) app.use(sessionConnect)
.use(createSession)
.run()
``` ```

107
index.js
View File

@ -8,78 +8,57 @@ import { uuid, sha1 } from 'crypto.js'
import RedisStore from './lib/redis-store.js' import RedisStore from './lib/redis-store.js'
const DEFAULT_CONFIG = {
ttl: 3600 * 24 * 7,
domain: '', // NODESSID域, 默认等于domain
level: 0, // 校验级别, 0: 不校验客户端, 2: ua, 4: ip, 6: ua + ip
db: {
host: '127.0.0.1',
port: 6379,
db: 0
}
}
// 会话安装包 // 会话安装包
export const SessionModule = { export const sessionPackage = {
name: 'session', name: 'session',
install(conf = {}) { install() {
if (!conf.domain) { var session = this.get('session')
throw new Error('Please make sure to set the `domain` field')
}
let session = Object.assign({}, DEFAULT_CONFIG, conf)
this.set({ session })
// 这里只创建session的存储器, 而初始化操作在中间件中进行 // 这里只创建session的存储器, 而初始化操作在中间件中进行
return new RedisStore(session) return new RedisStore(session)
} }
} }
// 会话中间件 // 会话中间件
export function createSession() { export function sessionConnect(req, res, next) {
return function (req, res, next) { var opt = this.get('session')
let opt = this.get('session') var cache = req.cookie('NODESSID')
let cache = req.cookie('NODESSID') var deviceID = ''
let deviceID = '' var ssid
let ssid
// options请求不处理会话 // options请求不处理会话
if (req.method === 'OPTIONS') { if (req.method === 'OPTIONS') {
return next() 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)
}
}
} else {
ssid = cache || sha1(uuid())
}
res.cookie('NODESSID', ssid, {
maxAge: opt.ttl,
httpOnly: true,
domain: opt.domain
})
// 缓存ssid到req上
req.ssid = ssid
this.$$session.update(ssid)
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)
}
}
} else {
ssid = cache || sha1(uuid())
}
res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain })
// 缓存ssid到req上
req.ssid = ssid
this.$$session.start(ssid)
next()
} }

View File

@ -3,33 +3,29 @@
* @author yutent<yutent.io@gmail.com> * @author yutent<yutent.io@gmail.com>
* @date 2020/09/18 16:35:26 * @date 2020/09/18 16:35:26
*/ */
import 'es.shim'
import Ioredis from 'ioredis' import Ioredis from 'ioredis'
export default class Session { export default class Session {
#store = null
#ttl = 60
constructor(opt) { constructor(opt) {
this.#store = new Ioredis({ this.store = new Ioredis({
host: opt.db.host || '127.0.0.1', host: opt.db.host || '127.0.0.1',
port: opt.db.port || 6379, port: opt.db.port || 6379,
db: opt.db.db || 0 db: opt.db.db || 0
}) })
this.#ttl = opt.ttl this.ttl = opt.ttl
} }
update(ssid) { start(ssid) {
// 设置session有效期 // 设置session有效期
this.#store.expire(ssid, this.#ttl) this.store.expire(ssid, this.ttl)
} }
// 获取session字段值, 需要await指令 // 获取session字段值, 需要await指令
get(ssid, key) { get(ssid, key) {
let defer = Promise.defer() var defer = Promise.defer()
this.#store.hgetall(ssid, (err, obj) => { this.store.hgetall(ssid, (err, obj) => {
if (err) { if (err) {
return defer.reject(err) return defer.reject(err)
} }
@ -41,7 +37,7 @@ export default class Session {
} }
//不传key时,直接返回全部字段 //不传key时,直接返回全部字段
if (key) { if (key) {
defer.resolve(obj[key]) defer.resolve(obj.hasOwnProperty(key) ? obj[key] : null)
} else { } else {
defer.resolve(obj) defer.resolve(obj)
} }
@ -53,20 +49,20 @@ export default class Session {
set(ssid, key, val) { set(ssid, key, val) {
if (typeof key === 'object') { if (typeof key === 'object') {
for (let i in key) { for (let i in key) {
this.#store.hset(ssid, i, key[i]) this.store.hset(ssid, i, key[i])
} }
} else { } else {
this.#store.hset(ssid, key, val) this.store.hset(ssid, key, val)
} }
} }
//删除单个字段 //删除单个字段
unset(ssid, key) { unset(ssid, key) {
this.#store.hdel(ssid, key) this.store.hdel(ssid, key)
} }
//清除个人session //清除个人session
clear(ssid) { clear(ssid) {
this.#store.del(ssid) this.store.del(ssid)
} }
} }

View File

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