mm_session 1.5.9 → 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
@@ -18,20 +18,14 @@ class Session {
18
18
  * 默认配置
19
19
  */
20
20
  static config = {
21
- // 密钥
22
21
  encrypt_key: $.config?.encrypt_key ? $.config.encrypt_key : 'mm'.md5().substring(0, 16),
23
- // session存储在缓存中的key前缀,默认'mm:uuid'
24
22
  key_prefix: $.dict.session_id || 'mm:uuid',
25
- // session ID存储在cookie中的字段名,默认'mm:uuid'
26
23
  cookie_token: $.dict.cookie_token || 'mm:uuid',
27
- // HTTP请求头中用于传递session ID的字段名,默认'x-auth-token'
28
24
  header_token: $.dict.header_token || 'x-auth-token',
29
- // session ID过期时间,单位秒,默认7天
30
25
  uuid_expire: 60 * 60 * 24 * 7,
31
- // session过期时间,单位秒,默认2小时
32
26
  max_age: 60 * 60 * 2,
33
- // 过期token处理选项:'reject'(拒绝访问)或'allow'(允许访问缓存)
34
27
  expired_handling: 'reject',
28
+ options: {},
35
29
  };
36
30
 
37
31
  /**
@@ -122,7 +116,8 @@ Session.prototype.get = async function (ctx) {
122
116
  * @param {Function} next 下一个中间件
123
117
  */
124
118
  Session.prototype._handle = async function (ctx, next) {
125
- let session_id = await this.get(ctx);
119
+ let session_id = this.getId(ctx);
120
+ await this.get(ctx);
126
121
  await next();
127
122
  await this._save(ctx, session_id);
128
123
  };
@@ -353,14 +348,6 @@ Session.prototype._save = async function (ctx, session_id) {
353
348
  // 提取session数据
354
349
  const session_to_save = this._extractSessionData(ctx);
355
350
 
356
- // 保存内部属性(_is_new和_modified)
357
- if (ctx.session._is_new !== undefined) {
358
- session_to_save._is_new = ctx.session._is_new;
359
- }
360
- if (ctx.session._modified !== undefined) {
361
- session_to_save._modified = ctx.session._modified;
362
- }
363
-
364
351
  // 清理session
365
352
  this._cleanup(ctx);
366
353
 
@@ -403,7 +390,7 @@ Session.prototype.save = async function (ctx, session_id_or_obj) {
403
390
  */
404
391
  Session.prototype._delSession = async function (ctx, session_id) {
405
392
  ctx.cookies.set(this.config.cookie_token, null);
406
- this._store.del(session_id).catch();
393
+ await this._store.del(session_id);
407
394
  };
408
395
 
409
396
  /**
@@ -422,10 +409,9 @@ Session.prototype._shouldSetCookie = function (session_id, sid) {
422
409
  * @param {string} sid session ID
423
410
  */
424
411
  Session.prototype._setCookie = function (ctx, sid) {
425
- let cg = { ...this.config.config };
426
- if (cg.max_age) {
427
- cg.maxAge = cg.max_age * 1000;
428
- 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;
429
415
  }
430
416
  ctx.cookies.set(this.config.cookie_token, sid, cg);
431
417
  };
@@ -435,7 +421,7 @@ Session.prototype._setCookie = function (ctx, sid) {
435
421
  * @param {number} end_time token过期时间戳
436
422
  * @returns {boolean} 是否过期
437
423
  */
438
- Session.prototype._verifyExpiration = async function (end_time) {
424
+ Session.prototype._verifyExpiration = function (end_time) {
439
425
  var current_time = Date.parse(new Date()) / 1000;
440
426
  return current_time > end_time;
441
427
  };
@@ -457,7 +443,7 @@ Session.prototype._checkSessionId = async function (session_id, client_ip, clien
457
443
  if (ip !== client_ip) return false;
458
444
 
459
445
  // 验证时效性
460
- var is_expired = await this._verifyExpiration(end_time);
446
+ var is_expired = this._verifyExpiration(end_time);
461
447
  if (is_expired) {
462
448
  if (this.config.expired_handling !== 'allow') {
463
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.9",
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.9"
33
+ "mm_cache": "^1.5.0"
34
34
  },
35
35
  "devDependencies": {
36
- "eslint-plugin-jsdoc": "^63.0.8",
36
+ "eslint-plugin-jsdoc": "^63.0.10",
37
37
  "mm_eslint": "^1.7.5"
38
38
  }
39
39
  }