mm_session 1.6.1 → 1.6.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/lib/session.js CHANGED
@@ -1,14 +1,12 @@
1
1
  const { Store } = require('./store.js');
2
2
  const { Helper } = require('./helper.js');
3
+ const { merge } = require('mm_helper');
4
+ const dict = require('mm_dict').getDict();
5
+ const config = require('mm_config').getConfig();
3
6
 
4
- if (!$.dict) {
5
- // 增加字典
6
- $.dict = {
7
- session_id: 'mm:uuid',
8
- cookie_token: 'mm:uuid',
9
- header_token: 'x-auth-token',
10
- };
11
- }
7
+ dict.set('session_id', 'mm:uuid');
8
+ dict.set('cookie_token', 'mm:uuid');
9
+ dict.set('header_token', 'x-auth-token');
12
10
 
13
11
  /**
14
12
  * Session类
@@ -18,10 +16,10 @@ class Session {
18
16
  * 默认配置
19
17
  */
20
18
  static config = {
21
- encrypt_key: $.config?.encrypt_key ? $.config.encrypt_key : 'mm'.md5().substring(0, 16),
22
- key_prefix: $.dict.session_id || 'mm:uuid',
23
- cookie_token: $.dict.cookie_token || 'mm:uuid',
24
- header_token: $.dict.header_token || 'x-auth-token',
19
+ encrypt_key: config.get('encrypt_key') || 'mm'.md5().substring(0, 16),
20
+ key_prefix: dict.get('session_id') || 'mm:uuid',
21
+ cookie_token: dict.get('cookie_token') || 'mm:uuid',
22
+ header_token: dict.get('header_token') || 'x-auth-token',
25
23
  uuid_expire: 60 * 60 * 24 * 7,
26
24
  max_age: 60 * 60 * 2,
27
25
  expired_handling: 'reject',
@@ -43,7 +41,7 @@ class Session {
43
41
  * @param {object} config 配置选项
44
42
  */
45
43
  Session.prototype.setConfig = function (config) {
46
- $.push(this.config, config);
44
+ this.config = merge.mergeConfig(this.config, config || {});
47
45
  this._preset();
48
46
  };
49
47
 
@@ -439,7 +437,8 @@ Session.prototype._checkSessionId = async function (session_id, client_ip, clien
439
437
 
440
438
  return true;
441
439
  } catch (err) {
442
- $.log.debug('Session ID验证错误:', err);
440
+ const log = require('mm_log').getLogger();
441
+ log.error('Session ID验证错误:', err);
443
442
  return false;
444
443
  }
445
444
  };
package/lib/store.js CHANGED
@@ -1,8 +1,5 @@
1
- const { Cache } = require('mm_cache');
2
-
3
- if (!$.cache) {
4
- $.cache = new Cache();
5
- }
1
+ const cache = require('mm_cache').getCache();
2
+ const dict = require('mm_dict').getDict();
6
3
 
7
4
  /**
8
5
  * 缓存类
@@ -16,7 +13,7 @@ class Store {
16
13
  if (key) {
17
14
  this.key = key + '_';
18
15
  } else {
19
- this.key = $.dict.session_id + '_';
16
+ this.key = dict.get('session_id') + '_';
20
17
  }
21
18
  }
22
19
  }
@@ -27,9 +24,9 @@ class Store {
27
24
  * @returns {object|null} session对象或null
28
25
  */
29
26
  Store.prototype.get = async function (uuid) {
30
- if (!$.cache.has(this.key + uuid)) return null;
27
+ if (!cache.has(this.key + uuid)) return null;
31
28
  // 我们正在解码数据来自我们的srote, 我们假定它是在储存之前
32
- var val = await $.cache.get(this.key + uuid);
29
+ var val = await cache.get(this.key + uuid);
33
30
  if (val) {
34
31
  if (typeof (val) == 'string') {
35
32
  return JSON.parse(val);
@@ -54,9 +51,9 @@ Store.prototype.set = async function (uuid, session, max_age = 7200) {
54
51
  session_str = JSON.stringify(session_str);
55
52
  }
56
53
 
57
- await $.cache.set(this.key + uuid, session_str, max_age);
54
+ await cache.set(this.key + uuid, session_str, max_age);
58
55
  } catch (err) {
59
- $.log.debug('Set session error:', err);
56
+ log.error('Set session error:', err);
60
57
  }
61
58
  return uuid;
62
59
  };
@@ -66,7 +63,7 @@ Store.prototype.set = async function (uuid, session, max_age = 7200) {
66
63
  * @param {string} uuid 客户端唯一ID
67
64
  */
68
65
  Store.prototype.del = async function (uuid) {
69
- await $.cache.del(this.key + uuid);
66
+ await cache.del(this.key + uuid);
70
67
  };
71
68
 
72
69
  module.exports = {
package/package.json CHANGED
@@ -1,39 +1,48 @@
1
- {
2
- "name": "mm_session",
3
- "version": "1.6.1",
4
- "description": "这是超级美眉session函数模块,用于web服务端session缓存",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://gitee.com/qiuwenwu91/mm_session.git"
12
- },
13
- "keywords": [
14
- "mysession",
15
- "async",
16
- "await",
17
- "promise",
18
- "add",
19
- "del",
20
- "set",
21
- "get",
22
- "query",
23
- "run",
24
- "exec"
25
- ],
26
- "author": "邱文武",
27
- "license": "ISC",
28
- "bugs": {
29
- "url": "https://gitee.com/qiuwenwu91/mm_session/issues"
30
- },
31
- "homepage": "https://gitee.com/qiuwenwu91/mm_session#readme",
32
- "dependencies": {
33
- "mm_cache": "^1.5.0"
34
- },
35
- "devDependencies": {
36
- "eslint-plugin-jsdoc": "^63.0.10",
37
- "mm_eslint": "^1.7.5"
38
- }
39
- }
1
+ {
2
+ "name": "mm_session",
3
+ "version": "1.6.3",
4
+ "description": "这是超级美眉session函数模块,用于web服务端session缓存",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://gitee.com/qiuwenwu91/mm_session.git"
12
+ },
13
+ "keywords": [
14
+ "mm_mod",
15
+ "mm_infra",
16
+ "mm_middleware",
17
+ "session",
18
+ "async",
19
+ "await",
20
+ "promise",
21
+ "add",
22
+ "del",
23
+ "set",
24
+ "get",
25
+ "query",
26
+ "run",
27
+ "exec"
28
+ ],
29
+ "layer": "middleware",
30
+ "group": "web_infra",
31
+ "author": "邱文武",
32
+ "license": "ISC",
33
+ "bugs": {
34
+ "url": "https://gitee.com/qiuwenwu91/mm_session/issues"
35
+ },
36
+ "homepage": "https://gitee.com/qiuwenwu91/mm_session#readme",
37
+ "dependencies": {
38
+ "mm_cache": "^1.5.4",
39
+ "mm_config": "^2.5.3",
40
+ "mm_dict": "^1.0.3",
41
+ "mm_helper": "^1.0.3"
42
+ },
43
+ "devDependencies": {
44
+ "eslint-plugin-jsdoc": "^64.0.1",
45
+ "mm_eslint": "^1.7.7",
46
+ "mm_expand": "^2.5.4"
47
+ }
48
+ }
package/test.js CHANGED
@@ -1,5 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ // 初始化全局 $ 对象(模块内部依赖 $.push)
4
+ require('mm_expand');
5
+
3
6
  const { session, Session, Store } = require('./index.js');
4
7
 
5
8
  /**