mm_session 1.5.2 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -12
- package/README_EN.md +45 -12
- package/eslint.config.js +3 -4
- package/index.js +4 -3
- package/lib/helper.js +99 -0
- package/lib/session.js +335 -120
- package/lib/store.js +15 -44
- package/package.json +5 -5
- package/test.js +287 -149
package/README.md
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
# mm_session
|
|
2
2
|
|
|
3
|
-
[
|
|
3
|
+
[中文](./README.md) | [English](./README_EN.md)
|
|
4
4
|
|
|
5
5
|
## 概述
|
|
6
6
|
|
|
7
7
|
`mm_session` 是一个专为 Koa.js 框架设计的轻量级 session 管理中间件。它提供了简单易用的 session 管理功能,支持自定义存储后端,适用于各种规模的 Web 应用。
|
|
8
8
|
|
|
9
|
+
这是超级美眉session函数模块,用于web服务端session缓存。
|
|
10
|
+
|
|
9
11
|
## 特性
|
|
10
12
|
|
|
11
|
-
- ✅ **Koa 中间件标准** - 符合 `app.use(session
|
|
13
|
+
- ✅ **Koa 中间件标准** - 符合 `app.use(session({ key: 'test_session', max_age: 3600 }))` 使用方式
|
|
12
14
|
- ✅ **存储抽象层** - 支持自定义存储后端
|
|
13
15
|
- ✅ **自动 session 管理** - 自动创建、保存、销毁 session
|
|
14
16
|
- ✅ **Cookie 自动处理** - 自动设置和读取 session cookie
|
|
15
17
|
- ✅ **安全 session ID** - 基于 IP + 时间戳 + AES 加密生成
|
|
16
18
|
- ✅ **异步支持** - 完整的 async/await 支持
|
|
17
19
|
- ✅ **轻量级** - 无冗余依赖,代码简洁
|
|
20
|
+
- ✅ **生产就绪** - 经过充分测试,稳定可靠
|
|
18
21
|
|
|
19
22
|
## 安装
|
|
20
23
|
|
|
@@ -28,18 +31,15 @@ npm install mm_session
|
|
|
28
31
|
|
|
29
32
|
```javascript
|
|
30
33
|
const Koa = require('koa');
|
|
31
|
-
const {
|
|
34
|
+
const { session } = require('mm_session');
|
|
32
35
|
|
|
33
36
|
const app = new Koa();
|
|
34
37
|
|
|
35
|
-
//
|
|
36
|
-
|
|
38
|
+
// 注册中间件
|
|
39
|
+
app.use(session({
|
|
37
40
|
key: 'my_session', // cookie 名称
|
|
38
41
|
max_age: 3600 // session 过期时间(秒)
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// 注册中间件
|
|
42
|
-
app.use(session.middleware());
|
|
42
|
+
}));
|
|
43
43
|
|
|
44
44
|
// 业务路由
|
|
45
45
|
app.use(async (ctx) => {
|
|
@@ -235,18 +235,45 @@ mm_session/
|
|
|
235
235
|
|
|
236
236
|
## 贡献
|
|
237
237
|
|
|
238
|
-
|
|
238
|
+
欢迎贡献!请随时提交 Issue 和 Pull Request。
|
|
239
|
+
|
|
240
|
+
### 开发环境设置
|
|
241
|
+
|
|
242
|
+
1. 克隆仓库:
|
|
243
|
+
```bash
|
|
244
|
+
git clone https://gitee.com/qiuwenwu91/mm_session.git
|
|
245
|
+
cd mm_session
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
2. 安装依赖:
|
|
249
|
+
```bash
|
|
250
|
+
npm install
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
3. 运行测试:
|
|
254
|
+
```bash
|
|
255
|
+
npm test
|
|
256
|
+
```
|
|
239
257
|
|
|
240
258
|
## 许可证
|
|
241
259
|
|
|
242
260
|
ISC License
|
|
243
261
|
|
|
262
|
+
版权所有 (c) 2024 邱文武
|
|
263
|
+
|
|
264
|
+
特此免费授予任何获得本软件副本和相关文档文件(以下简称"软件")的人不受限制地处理本软件的权限,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售本软件的副本,以及允许提供本软件的人员这样做,但须符合以下条件:
|
|
265
|
+
|
|
266
|
+
上述版权声明和本许可声明应包含在本软件的所有副本或重要部分中。
|
|
267
|
+
|
|
244
268
|
## 作者
|
|
245
269
|
|
|
246
270
|
邱文武
|
|
247
271
|
|
|
248
272
|
## 更新日志
|
|
249
273
|
|
|
274
|
+
### v1.5.3
|
|
275
|
+
- 当前稳定版本
|
|
276
|
+
|
|
250
277
|
### v1.5.1
|
|
251
278
|
- 修复 session 保存逻辑
|
|
252
279
|
- 优化 cookie 设置机制
|
|
@@ -257,7 +284,15 @@ ISC License
|
|
|
257
284
|
- 符合 Koa 中间件标准使用方式
|
|
258
285
|
- 增强代码可维护性
|
|
259
286
|
|
|
287
|
+
## 错误报告
|
|
288
|
+
|
|
289
|
+
如果您遇到任何错误或有功能请求,请在 [Git 仓库](https://gitee.com/qiuwenwu91/mm_session/issues) 上提交 Issue。
|
|
290
|
+
|
|
260
291
|
## 相关项目
|
|
261
292
|
|
|
262
|
-
- [
|
|
263
|
-
- [mm_eslint](https://www.npmjs.com/package/mm_eslint) - ESLint
|
|
293
|
+
- [mm_cache](https://www.npmjs.com/package/mm_cache) - 缓存库(依赖项)
|
|
294
|
+
- [mm_eslint](https://www.npmjs.com/package/mm_eslint) - ESLint 配置(开发依赖项)
|
|
295
|
+
|
|
296
|
+
## 支持
|
|
297
|
+
|
|
298
|
+
如需支持和问题解答,请查阅文档或在项目仓库中创建 Issue。
|
package/README_EN.md
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
# mm_session
|
|
2
2
|
|
|
3
|
-
[
|
|
3
|
+
[中文](README.md) | [English](README_EN.md)
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
7
|
`mm_session` is a lightweight session management middleware designed specifically for the Koa.js framework. It provides simple and easy-to-use session management functionality with support for custom storage backends, suitable for web applications of all sizes.
|
|
8
8
|
|
|
9
|
+
This is a super session function module for web server-side session caching.
|
|
10
|
+
|
|
9
11
|
## Features
|
|
10
12
|
|
|
11
|
-
- ✅ **Koa Middleware Standard** - Compatible with `app.use(session
|
|
13
|
+
- ✅ **Koa Middleware Standard** - Compatible with `app.use(session({ key: 'test_session', max_age: 3600 }))` usage
|
|
12
14
|
- ✅ **Storage Abstraction Layer** - Supports custom storage backends
|
|
13
15
|
- ✅ **Automatic Session Management** - Automatic session creation, saving, and destruction
|
|
14
16
|
- ✅ **Cookie Auto-handling** - Automatic session cookie setting and reading
|
|
15
17
|
- ✅ **Secure Session ID** - Generated based on IP + timestamp + AES encryption
|
|
16
18
|
- ✅ **Async Support** - Full async/await support
|
|
17
19
|
- ✅ **Lightweight** - No redundant dependencies, clean codebase
|
|
20
|
+
- ✅ **Production Ready** - Well-tested and stable for production use
|
|
18
21
|
|
|
19
22
|
## Installation
|
|
20
23
|
|
|
@@ -28,18 +31,15 @@ npm install mm_session
|
|
|
28
31
|
|
|
29
32
|
```javascript
|
|
30
33
|
const Koa = require('koa');
|
|
31
|
-
const {
|
|
34
|
+
const { session } = require('mm_session');
|
|
32
35
|
|
|
33
36
|
const app = new Koa();
|
|
34
37
|
|
|
35
|
-
//
|
|
36
|
-
|
|
38
|
+
// Register middleware
|
|
39
|
+
app.use(session({
|
|
37
40
|
key: 'my_session', // cookie name
|
|
38
41
|
max_age: 3600 // session expiration time (seconds)
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// Register middleware
|
|
42
|
-
app.use(session.middleware());
|
|
42
|
+
}));
|
|
43
43
|
|
|
44
44
|
// Business routes
|
|
45
45
|
app.use(async (ctx) => {
|
|
@@ -235,18 +235,43 @@ mm_session/
|
|
|
235
235
|
|
|
236
236
|
## Contributing
|
|
237
237
|
|
|
238
|
-
|
|
238
|
+
We welcome contributions! Please feel free to submit issues and pull requests.
|
|
239
|
+
|
|
240
|
+
### Development Setup
|
|
241
|
+
|
|
242
|
+
1. Clone the repository:
|
|
243
|
+
```bash
|
|
244
|
+
git clone https://gitee.com/qiuwenwu91/mm_session.git
|
|
245
|
+
cd mm_session
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
2. Install dependencies:
|
|
249
|
+
```bash
|
|
250
|
+
npm install
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
3. Run tests:
|
|
254
|
+
```bash
|
|
255
|
+
npm test
|
|
256
|
+
```
|
|
239
257
|
|
|
240
258
|
## License
|
|
241
259
|
|
|
242
260
|
ISC License
|
|
243
261
|
|
|
262
|
+
Copyright (c) 2024 Qiu Wenwu
|
|
263
|
+
|
|
264
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
265
|
+
|
|
244
266
|
## Author
|
|
245
267
|
|
|
246
268
|
Qiu Wenwu
|
|
247
269
|
|
|
248
270
|
## Changelog
|
|
249
271
|
|
|
272
|
+
### v1.5.3
|
|
273
|
+
- Current stable version
|
|
274
|
+
|
|
250
275
|
### v1.5.1
|
|
251
276
|
- Fixed session saving logic
|
|
252
277
|
- Optimized cookie setting mechanism
|
|
@@ -257,7 +282,15 @@ Qiu Wenwu
|
|
|
257
282
|
- Compatible with Koa middleware standard usage
|
|
258
283
|
- Enhanced code maintainability
|
|
259
284
|
|
|
285
|
+
## Bug Reports
|
|
286
|
+
|
|
287
|
+
If you encounter any bugs or have feature requests, please file an issue on the [Git repository](https://gitee.com/qiuwenwu91/mm_session/issues).
|
|
288
|
+
|
|
260
289
|
## Related Projects
|
|
261
290
|
|
|
262
|
-
- [
|
|
263
|
-
- [mm_eslint](https://www.npmjs.com/package/mm_eslint) - ESLint configuration
|
|
291
|
+
- [mm_cache](https://www.npmjs.com/package/mm_cache) - Cache library (dependency)
|
|
292
|
+
- [mm_eslint](https://www.npmjs.com/package/mm_eslint) - ESLint configuration (dev dependency)
|
|
293
|
+
|
|
294
|
+
## Support
|
|
295
|
+
|
|
296
|
+
For support and questions, please check the documentation or create an issue on the project repository.
|
package/eslint.config.js
CHANGED
|
@@ -27,13 +27,13 @@ module.exports = [
|
|
|
27
27
|
rules: {
|
|
28
28
|
// 自定义命名规范插件规则(优先使用)
|
|
29
29
|
'naming-convention/class-name': 'error',
|
|
30
|
-
'naming-convention/function-name': '
|
|
30
|
+
'naming-convention/function-name': 'error', // 启用函数命名规则
|
|
31
31
|
'naming-convention/method-name': 'error',
|
|
32
32
|
'naming-convention/variable-name': 'warn',
|
|
33
33
|
'naming-convention/constant-name': 'error',
|
|
34
34
|
'naming-convention/private-method-naming': 'warn',
|
|
35
35
|
'naming-convention/private-variable-naming': 'warn',
|
|
36
|
-
'naming-convention/param-name': '
|
|
36
|
+
'naming-convention/param-name': 'error', // 启用入参名规则
|
|
37
37
|
'naming-convention/property-name': 'warn',
|
|
38
38
|
'naming-convention/instance-property': 'warn',
|
|
39
39
|
|
|
@@ -102,8 +102,7 @@ module.exports = [
|
|
|
102
102
|
rules: {
|
|
103
103
|
// 必须进行参数校验
|
|
104
104
|
'no-unused-vars': ['error', {
|
|
105
|
-
args: '
|
|
106
|
-
argsIgnorePattern: '^_',
|
|
105
|
+
args: 'none', // 不检查未使用的参数
|
|
107
106
|
caughtErrors: 'all',
|
|
108
107
|
caughtErrorsIgnorePattern: '^_',
|
|
109
108
|
destructuredArrayIgnorePattern: '^_',
|
package/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
const { Store } = require('./lib/store.js');
|
|
2
2
|
const { Session } = require('./lib/session.js');
|
|
3
3
|
|
|
4
|
-
let session = new Session();
|
|
5
|
-
|
|
6
4
|
module.exports = {
|
|
7
5
|
Session,
|
|
8
6
|
Store,
|
|
9
|
-
session
|
|
7
|
+
session: function(config) {
|
|
8
|
+
let session = new Session(config);
|
|
9
|
+
return session.middleware();
|
|
10
|
+
}
|
|
10
11
|
};
|
package/lib/helper.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const crypto = require('crypto');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 帮助类
|
|
5
|
+
*/
|
|
6
|
+
class Helper {
|
|
7
|
+
/**
|
|
8
|
+
* 构造函数
|
|
9
|
+
*/
|
|
10
|
+
constructor() {
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* AES编码字符串
|
|
16
|
+
* @param {string} text 输入文本
|
|
17
|
+
* @param {string} key 编码密钥
|
|
18
|
+
* @param {string} iv 初始化向量
|
|
19
|
+
* @returns {string} 编码后的字符串
|
|
20
|
+
*/
|
|
21
|
+
Helper.prototype.aesEncode = function (text, key, iv) {
|
|
22
|
+
// 1. 创建cipher对象
|
|
23
|
+
const cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
|
|
24
|
+
// 2. 更新加密数据
|
|
25
|
+
let enc = cipher.update(text, 'utf8', 'hex');
|
|
26
|
+
// 3. 完成加密
|
|
27
|
+
enc += cipher.final('hex');
|
|
28
|
+
return enc;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* AES解码字符串
|
|
33
|
+
* @param {string} text - 输入文本
|
|
34
|
+
* @param {string} key - 解码密钥
|
|
35
|
+
* @param {string} iv - 初始化向量
|
|
36
|
+
* @returns {string} 解码后的字符串
|
|
37
|
+
*/
|
|
38
|
+
Helper.prototype.aesDecode = function (text, key, iv) {
|
|
39
|
+
// 1. 创建decipher对象
|
|
40
|
+
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
|
|
41
|
+
// 2. 更新解密数据
|
|
42
|
+
let dec = decipher.update(text, 'hex', 'utf8');
|
|
43
|
+
// 3. 完成解密
|
|
44
|
+
dec += decipher.final('utf8');
|
|
45
|
+
return dec;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 生成session ID的密钥
|
|
51
|
+
* @param {string} user_agent 用户代理字符串
|
|
52
|
+
* @returns {string} 密钥
|
|
53
|
+
*/
|
|
54
|
+
Helper.prototype._getSecret = function (user_agent = 'mm') {
|
|
55
|
+
// md5方法由mm_expand模块提供
|
|
56
|
+
return user_agent.md5().substring(0, 16);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 生成session ID
|
|
61
|
+
* @param {string} key 密钥
|
|
62
|
+
* @param {string} ip 客户端IP
|
|
63
|
+
* @param {string} user_agent 用户代理字符串
|
|
64
|
+
* @param {number} uuid_expire 过期时间(秒)
|
|
65
|
+
* @returns {string} session ID
|
|
66
|
+
*/
|
|
67
|
+
Helper.prototype.genId = function (key, ip, user_agent = 'mm', uuid_expire = 604800) {
|
|
68
|
+
// 生成session ID的初始化向量
|
|
69
|
+
var iv = this._getSecret(user_agent);
|
|
70
|
+
var end_time = new Date().addSeconds(uuid_expire).stamp();
|
|
71
|
+
// 加密
|
|
72
|
+
var uuid = this.aesEncode(ip + '_' + end_time, key, iv);
|
|
73
|
+
return uuid;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 解析session ID
|
|
78
|
+
* @param {string} key 密钥
|
|
79
|
+
* @param {string} uuid session ID
|
|
80
|
+
* @param {string} user_agent 用户代理字符串
|
|
81
|
+
* @returns {object} 解析结果
|
|
82
|
+
*/
|
|
83
|
+
Helper.prototype.parseId = function (key, uuid, user_agent = 'mm') {
|
|
84
|
+
// 生成session ID的初始化向量
|
|
85
|
+
var iv = this._getSecret(user_agent);
|
|
86
|
+
// 解密
|
|
87
|
+
var dec = this.aesDecode(uuid, key, iv);
|
|
88
|
+
// 解析IP和过期时间
|
|
89
|
+
var ip = dec.split('_')[0];
|
|
90
|
+
var end_time = dec.split('_')[1];
|
|
91
|
+
return {
|
|
92
|
+
ip,
|
|
93
|
+
end_time
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
module.exports = {
|
|
98
|
+
Helper
|
|
99
|
+
};
|