宇天 2020-09-18 18:25:56 +08:00
commit a9dd72b901
7 changed files with 277 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
.Spotlight-V100
.Trashes
.DS_Store
.AppleDouble
.LSOverride
._*
.idea
.vscode

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

12
Readme.md Normal file
View File

@ -0,0 +1,12 @@
![module info](https://nodei.co/npm/@gm5/controller.png?downloads=true&downloadRank=true&stars=true)
# @gm5/controller
> 控制器基类。
## 安装
```bash
npm install @gm5/controller
```

79
lib/native-store.js Normal file
View File

@ -0,0 +1,79 @@
/**
* 内存版会话管理(仅用于测试,数据)
* @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(store, opt, uuid) {
this.opt = opt
this.uuid = uuid
this.store = store
}
createSsid(ssid) {
if (ssid) {
if (!this.opt.jwt && this.opt.level > 0 && ssid !== this.uuid) {
ssid = this.uuid
}
} else {
ssid = this.uuid
}
this.ssid = ssid
if (
!this.store.hasOwnProperty(ssid) ||
this.store[ssid].__EXPIRES__ < Date.now()
) {
this.store[ssid] = {}
}
//设置session有效期
hideProperty(
this.store[ssid],
'__EXPIRES__',
Date.now() + this.opt.ttl * 1000
)
}
start(ssid) {
this.createSsid(ssid)
return this.ssid
}
// 获取session字段值
get(key) {
return key ? this.store[this.ssid][key] || null : this.store[this.ssid]
}
// 设置session字段值
set(key, val) {
if (typeof key === 'object') {
for (let i in key) {
this.store[this.ssid][i] = key[i]
}
} else {
this.store[this.ssid][key] = val
}
}
// 删除单个字段
unset(key) {
if (this.store[this.ssid].hasOwnProperty(key)) {
delete this.store[this.ssid][key]
}
}
// 清除个人session
clear() {
this.store[this.ssid] = {}
}
}

79
lib/redis-store.js Normal file
View File

@ -0,0 +1,79 @@
/**
* redis版会话管理
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/18 16:35:26
*/
export default class Session {
constructor(store, opt, uuid) {
this.store = store
this.opt = opt
this.uuid = uuid
}
createSsid(ssid) {
if (ssid) {
if (!this.opt.jwt && this.opt.level > 0 && ssid !== this.uuid) {
ssid = this.uuid
}
} else {
ssid = this.uuid
}
this.ssid = ssid
// 设置session有效期
this.store.expire(ssid, this.opt.ttl)
}
start(ssid) {
this.createSsid(ssid)
return this.ssid
}
// 获取session字段值, 需要await指令
get(key) {
var defer = Promise.defer()
this.store.hgetall(this.ssid, (err, obj) => {
if (err) {
return defer.reject(err)
}
for (let i in obj) {
if (!obj[i]) {
continue
}
obj[i] = Number.parse(obj[i])
}
//不传key时,直接返回全部字段
if (!key) {
defer.resolve(obj)
} else {
defer.resolve(obj.hasOwnProperty(key) ? obj[key] : null)
}
})
return defer.promise
}
//设置session字段值
set(key, val) {
if (typeof key === 'object') {
for (let i in key) {
this.store.hset(this.ssid, i, key[i])
}
} else {
this.store.hset(this.ssid, key, val)
}
}
//删除单个字段
unset(key) {
this.store.hdel(this.ssid, key)
}
//清除个人session
clear() {
this.store.del(this.ssid)
}
}

11
package.json Normal file
View File

@ -0,0 +1,11 @@
{
"name": "@gm5/session",
"version": "1.0.0",
"type": "module",
"description": "会话中间件。",
"main": "index.js",
"author": "yutent",
"keywords": ["fivejs", "controller", "http"],
"repository": "https://github.com/bytedo/gmf.session.git",
"license": "MIT"
}

66
session.js Normal file
View File

@ -0,0 +1,66 @@
/**
*
* @authors yutent (yutent@doui.cc)
* @date 2018-07-26 15:50:25
* @version $Id$
*/
import redisStore from './lib/redis-store.js'
import nativeStore from './lib/native-store.js'
import { uuid, sha1 } from 'crypto.js'
export default function(req, res, next) {
var opt = this.get('session')
var jwt = this.get('jwt')
var cookie = this.ins('cookie')
var session = null
var deviceID = uuid()
var ssid = ''
opt.jwt = jwt
if (req.method === 'OPTIONS') {
return next()
}
if (jwt) {
var auth = req.header('authorization')
if (auth) {
ssid = auth.split('.').pop()
deviceID = auth
}
} else {
ssid = cookie('NODESSID')
// 校验级别为1, 则混入ua
if (opt.level > 0) {
deviceID += req.header('user-agent')
}
// 校验级别为2, 则混入ip
if (opt.level > 1) {
deviceID += req.ip()
}
}
deviceID = sha1(deviceID)
if (opt.type === 'redis') {
session = new redisStore(this.__SESSION_STORE__, opt, deviceID)
} else {
session = new nativeStore(this.__SESSION_STORE__, opt, deviceID)
}
// 启用SESSION
// ssid非法或过期时需要重写
if (!ssid || ssid !== session.start(ssid)) {
ssid = session.start(ssid)
if (!jwt) {
cookie('NODESSID', ssid, {
httpOnly: true,
expires: opt.ttl,
domain: opt.domain
})
}
}
this.__INSTANCE__.session = session
next()
}