mm_session 1.5.8 → 1.6.0

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/lib/helper.js CHANGED
@@ -81,16 +81,15 @@ Helper.prototype.genId = function (key, ip, user_agent = 'mm', uuid_expire = 604
81
81
  * @returns {object} 解析结果
82
82
  */
83
83
  Helper.prototype.parseId = function (key, uuid, user_agent = 'mm') {
84
- // 生成session ID的初始化向量
85
84
  var iv = this._getSecret(user_agent);
86
- // 解密
87
85
  var dec = this.aesDecode(uuid, key, iv);
88
- // 解析IP和过期时间
89
- var ip = dec.split('_')[0];
90
- var end_time = dec.split('_')[1];
86
+ var parts = dec.split('_');
87
+ if (parts.length !== 2) {
88
+ throw new Error('Invalid session ID format');
89
+ }
91
90
  return {
92
- ip,
93
- end_time
91
+ ip: parts[0],
92
+ end_time: Number(parts[1])
94
93
  };
95
94
  };
96
95
 
package/lib/session.js CHANGED
@@ -3,7 +3,11 @@ const { Helper } = require('./helper.js');
3
3
 
4
4
  if (!$.dict) {
5
5
  // 增加字典
6
- $.dict = {};
6
+ $.dict = {
7
+ session_id: 'mm:uuid',
8
+ cookie_token: 'mm:uuid',
9
+ header_token: 'x-auth-token',
10
+ };
7
11
  }
8
12
 
9
13
  /**
@@ -14,20 +18,14 @@ class Session {
14
18
  * 默认配置
15
19
  */
16
20
  static config = {
17
- // 密钥
18
21
  encrypt_key: $.config?.encrypt_key ? $.config.encrypt_key : 'mm'.md5().substring(0, 16),
19
- // session存储在缓存中的key前缀,默认'mm:uuid'
20
22
  key_prefix: $.dict.session_id || 'mm:uuid',
21
- // session ID存储在cookie中的字段名,默认'mm:uuid'
22
23
  cookie_token: $.dict.cookie_token || 'mm:uuid',
23
- // HTTP请求头中用于传递session ID的字段名,默认'x-auth-token'
24
24
  header_token: $.dict.header_token || 'x-auth-token',
25
- // session ID过期时间,单位秒,默认7天
26
- uuid_expire: $.dict.uuid_expire || 60 * 60 * 24 * 7,
27
- // session过期时间,单位秒,默认2小时
28
- max_age: $.dict.max_age || 60 * 60 * 2,
29
- // 过期token处理选项:'reject'(拒绝访问)或'allow'(允许访问缓存)
25
+ uuid_expire: 60 * 60 * 24 * 7,
26
+ max_age: 60 * 60 * 2,
30
27
  expired_handling: 'reject',
28
+ options: {},
31
29
  };
32
30
 
33
31
  /**
@@ -118,7 +116,8 @@ Session.prototype.get = async function (ctx) {
118
116
  * @param {Function} next 下一个中间件
119
117
  */
120
118
  Session.prototype._handle = async function (ctx, next) {
121
- let session_id = await this.get(ctx);
119
+ let session_id = this.getId(ctx);
120
+ await this.get(ctx);
122
121
  await next();
123
122
  await this._save(ctx, session_id);
124
123
  };
@@ -156,7 +155,7 @@ Session.prototype._getClientIP = function (ctx) {
156
155
  */
157
156
  Session.prototype._getHeaderInfo = function (ctx) {
158
157
  let ip = this._getClientIP(ctx);
159
- let user_agent = ctx.headers['user-agent'] || 'mm';
158
+ let user_agent = ctx.headers['user-agent'] || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36';
160
159
  return {
161
160
  ip,
162
161
  user_agent
@@ -349,14 +348,6 @@ Session.prototype._save = async function (ctx, session_id) {
349
348
  // 提取session数据
350
349
  const session_to_save = this._extractSessionData(ctx);
351
350
 
352
- // 保存内部属性(_is_new和_modified)
353
- if (ctx.session._is_new !== undefined) {
354
- session_to_save._is_new = ctx.session._is_new;
355
- }
356
- if (ctx.session._modified !== undefined) {
357
- session_to_save._modified = ctx.session._modified;
358
- }
359
-
360
351
  // 清理session
361
352
  this._cleanup(ctx);
362
353
 
@@ -399,7 +390,7 @@ Session.prototype.save = async function (ctx, session_id_or_obj) {
399
390
  */
400
391
  Session.prototype._delSession = async function (ctx, session_id) {
401
392
  ctx.cookies.set(this.config.cookie_token, null);
402
- this._store.del(session_id).catch();
393
+ await this._store.del(session_id);
403
394
  };
404
395
 
405
396
  /**
@@ -418,10 +409,9 @@ Session.prototype._shouldSetCookie = function (session_id, sid) {
418
409
  * @param {string} sid session ID
419
410
  */
420
411
  Session.prototype._setCookie = function (ctx, sid) {
421
- let cg = { ...this.config.config };
422
- if (cg.max_age) {
423
- cg.maxAge = cg.max_age * 1000;
424
- delete cg.max_age;
412
+ let cg = { ...this.config.options };
413
+ if (this.config.max_age && !cg.maxAge) {
414
+ cg.maxAge = this.config.max_age * 1000;
425
415
  }
426
416
  ctx.cookies.set(this.config.cookie_token, sid, cg);
427
417
  };
@@ -431,7 +421,7 @@ Session.prototype._setCookie = function (ctx, sid) {
431
421
  * @param {number} end_time token过期时间戳
432
422
  * @returns {boolean} 是否过期
433
423
  */
434
- Session.prototype._verifyExpiration = async function (end_time) {
424
+ Session.prototype._verifyExpiration = function (end_time) {
435
425
  var current_time = Date.parse(new Date()) / 1000;
436
426
  return current_time > end_time;
437
427
  };
@@ -453,7 +443,7 @@ Session.prototype._checkSessionId = async function (session_id, client_ip, clien
453
443
  if (ip !== client_ip) return false;
454
444
 
455
445
  // 验证时效性
456
- var is_expired = await this._verifyExpiration(end_time);
446
+ var is_expired = this._verifyExpiration(end_time);
457
447
  if (is_expired) {
458
448
  if (this.config.expired_handling !== 'allow') {
459
449
  return false;
package/lib/store.js CHANGED
@@ -27,7 +27,7 @@ class Store {
27
27
  * @returns {object|null} session对象或null
28
28
  */
29
29
  Store.prototype.get = async function (uuid) {
30
- if (!$.cache.has(this.key + uuid)) return undefined;
30
+ if (!$.cache.has(this.key + uuid)) return null;
31
31
  // 我们正在解码数据来自我们的srote, 我们假定它是在储存之前
32
32
  var val = await $.cache.get(this.key + uuid);
33
33
  if (val) {
@@ -65,8 +65,8 @@ Store.prototype.set = async function (uuid, session, max_age = 7200) {
65
65
  * 删除缓存
66
66
  * @param {string} uuid 客户端唯一ID
67
67
  */
68
- Store.prototype.del = function (uuid) {
69
- $.cache.del(this.key + uuid);
68
+ Store.prototype.del = async function (uuid) {
69
+ await $.cache.del(this.key + uuid);
70
70
  };
71
71
 
72
72
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_session",
3
- "version": "1.5.8",
3
+ "version": "1.6.0",
4
4
  "description": "这是超级美眉session函数模块,用于web服务端session缓存",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,10 +30,10 @@
30
30
  },
31
31
  "homepage": "https://gitee.com/qiuwenwu91/mm_session#readme",
32
32
  "dependencies": {
33
- "mm_cache": "^1.4.8"
33
+ "mm_cache": "^1.5.0"
34
34
  },
35
35
  "devDependencies": {
36
- "eslint-plugin-jsdoc": "^62.9.0",
37
- "mm_eslint": "^1.7.1"
36
+ "eslint-plugin-jsdoc": "^63.0.10",
37
+ "mm_eslint": "^1.7.5"
38
38
  }
39
39
  }