From 6e6e97467dd48ce21c8b85a2f609149bcf56ad82 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 1 Nov 2023 15:47:53 +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 --- Readme.md | 15 ++++++++---- index.js | 58 +++++++++++++++++++++++++++++----------------- lib/redis-store.js | 4 ++-- package.json | 16 +++++++++---- 4 files changed, 60 insertions(+), 33 deletions(-) diff --git a/Readme.md b/Readme.md index 6262c84..300e3e1 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,6 @@ -![module info](https://nodei.co/npm/@gm5/session.png?downloads=true&downloadRank=true&stars=true) + +![downloads](https://img.shields.io/npm/dt/@gm5/request.svg) +![version](https://img.shields.io/npm/v/@gm5/request.svg) # @gm5/session @@ -7,17 +9,20 @@ ## 安装 ```bash -npm install @gm5/session +npm i @gm5/session ``` ## 使用 ```js -import { sessionPackage, sessionConnect } from '@gm5/session' +import { createApp } from '@gm5/core +import { createSession, SessionModule } from '@gm5/session' -app.install(sessionPackage) +const app = createApp() -app.use(sessionConnect) +app.install(SessionModule, {domain: 'your_domain'}) + .use(createSession) + .run() ``` \ No newline at end of file diff --git a/index.js b/index.js index af5cb70..061afe5 100644 --- a/index.js +++ b/index.js @@ -8,45 +8,57 @@ import { uuid, sha1 } from 'crypto.js' import RedisStore from './lib/redis-store.js' -// 会话安装包 -export function createSessionPlugin(){ +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 + } +} - return { - name: 'session', - install() { - var session = this.get('session') - // 这里只创建session的存储器, 而初始化操作在中间件中进行 - return new RedisStore(session) +// 会话安装包 +export const SessionModule = { + name: 'session', + install(conf = {}) { + if (!conf.domain) { + throw new Error('Please make sure to set the `domain` field') } + let session = Object.assign({}, DEFAULT_CONFIG, conf) + this.set({ session }) + // 这里只创建session的存储器, 而初始化操作在中间件中进行 + return new RedisStore(session) } } // 会话中间件 -export function createSession(){ +export function createSession() { return function (req, res, next) { - var opt = this.get('session') - var cache = req.cookie('NODESSID') - var deviceID = '' - var ssid - + let opt = this.get('session') + let cache = req.cookie('NODESSID') + let deviceID = '' + let 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)) { @@ -58,12 +70,16 @@ export function createSession(){ } else { ssid = cache || sha1(uuid()) } - - res.cookie('NODESSID', ssid, { maxAge: opt.ttl, domain: opt.domain }) + + res.cookie('NODESSID', ssid, { + maxAge: opt.ttl, + httpOnly: true, + domain: opt.domain + }) // 缓存ssid到req上 req.ssid = ssid this.$$session.update(ssid) - + next() } } diff --git a/lib/redis-store.js b/lib/redis-store.js index 66c14ca..6437287 100644 --- a/lib/redis-store.js +++ b/lib/redis-store.js @@ -3,7 +3,7 @@ * @author yutent * @date 2020/09/18 16:35:26 */ - +import 'es.shim' import Ioredis from 'ioredis' export default class Session { @@ -27,7 +27,7 @@ export default class Session { // 获取session字段值, 需要await指令 get(ssid, key) { - var defer = Promise.defer() + let defer = Promise.defer() this.#store.hgetall(ssid, (err, obj) => { if (err) { diff --git a/package.json b/package.json index 47e2698..3f16a87 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,21 @@ { "name": "@gm5/session", - "version": "1.1.2", + "version": "2.0.0", "type": "module", "description": "会话中间件。", "main": "index.js", "author": "yutent ", - "keywords": ["fivejs", "session", "http"], + "keywords": [ + "fivejs", + "gm5", + "session", + "http" + ], "dependencies": { - "crypto.js": "^2.0.2", - "ioredis": "^4.17.3" + "crypto.js": "^3.1.2", + "ioredis": "^5.3.2", + "es.shim": "^2.2.0" }, - "repository": "https://github.com/bytedo/gmf.session.git", + "repository": "https://git.wkit.fun/gm5/session.git", "license": "MIT" }