@tmsfe/tms-core 0.0.155 → 0.0.156

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.155",
3
+ "version": "0.0.156",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -223,9 +223,9 @@ const eccUtil = {
223
223
  // 加密规则判断工具
224
224
  const cryptRuleUtil = {
225
225
  // 远程加密服务是否开启
226
- _isServerOpen: (): boolean => !!wx.$_publicKey,
226
+ isServerOpen: (): boolean => !!wx.$_publicKey,
227
227
  // 检查path是否符合下发的路由前缀
228
- _pathInEnablePrefix: (path: string): boolean => {
228
+ pathInEnablePrefix: (path: string): boolean => {
229
229
  if (!wx.$_publicKey) {
230
230
  return false;
231
231
  }
@@ -242,7 +242,7 @@ const cryptRuleUtil = {
242
242
  return false;
243
243
  },
244
244
  // 判断是否是性能埋点上报接口
245
- _isPerformanceReport: (path: string, params: any): boolean => {
245
+ isPerformanceReport: (path: string, params: any): boolean => {
246
246
  // 如果是日志上报接口,需要过滤性能日志,不需要加密
247
247
  if (path.indexOf('basic/event/upload') > -1) {
248
248
  if (params.batch?.length === 1 && params.batch[0]?.[31] === 'tms-performance-log') {
@@ -261,7 +261,7 @@ const cryptRuleUtil = {
261
261
  '^/tde', '^/basic/crypto/lastkey',
262
262
  ],
263
263
  },
264
- _isHostValid: (url) => {
264
+ isHostValid: (url) => {
265
265
  // 使用正则表达式解析URL
266
266
  const urlPattern = /^(https?:\/\/)?([^/?#]+)([/?#].*)?$/;
267
267
  const matches = url.match(urlPattern);
@@ -297,19 +297,19 @@ const isCryptoRuleMath = (path: string, reqData: any): BaseResp<boolean> => {
297
297
  return new baseUtil.BaseRespFac(false, false, '本地加密未开启');
298
298
  }
299
299
  // 如果服务端下发的加密开关关闭,不走加密
300
- if (!cryptRuleUtil._isServerOpen()) {
300
+ if (!cryptRuleUtil.isServerOpen()) {
301
301
  return new baseUtil.BaseRespFac(false, false, '服务端加密未开启');
302
302
  }
303
303
  // 请求路由不满足服务端下发的加密规则,不走加密
304
- if (!cryptRuleUtil._pathInEnablePrefix(path)) {
304
+ if (!cryptRuleUtil.pathInEnablePrefix(path)) {
305
305
  return new baseUtil.BaseRespFac(false, false, '未命中服务端加密规则');
306
306
  }
307
307
  // 请求接口是加密性能埋点上报接口,不加密
308
- if (cryptRuleUtil._isPerformanceReport(path, reqData)) {
308
+ if (cryptRuleUtil.isPerformanceReport(path, reqData)) {
309
309
  return new baseUtil.BaseRespFac(false, false, '性能埋点');
310
310
  }
311
311
  // 请求路由不走sinan网关,不加密
312
- if (!cryptRuleUtil._isHostValid(path)) {
312
+ if (!cryptRuleUtil.isHostValid(path)) {
313
313
  return new baseUtil.BaseRespFac(false, false, '非sinan网关加密接口');
314
314
  }
315
315
  return new baseUtil.BaseRespFac(true);;
@@ -1,12 +1,6 @@
1
1
  import encryptUtil from './encrypt-util';
2
2
  import { genTraceparent } from './traceUtils';
3
3
 
4
- interface Params {
5
- filePath?: string,
6
- name?: string,
7
- data?: any,
8
- }
9
-
10
4
  const logger = wx.getLogManager({});
11
5
  const util = {
12
6
  // 统一格式化日志输出
@@ -22,8 +16,9 @@ const util = {
22
16
  return args;
23
17
  },
24
18
  logInfo: (...args) => {
25
- console.log(...args);
19
+ args.unshift('request_encrypt_log');
26
20
  const items = util.formatLog(args);
21
+ console.log(...items);
27
22
  logger.log(...items);
28
23
  },
29
24
  reportFunc: (...args) => {
@@ -45,16 +40,24 @@ const util = {
45
40
  return Object.assign({ msg }, reqEncryptRes.res);
46
41
  },
47
42
  };
48
-
43
+ let originalRequestApi;
44
+ let originalUploadFileApi;
49
45
  // 劫持wx.request和wx.uploadFile函数
50
- const requestInit = () => {
46
+ const requestInit = (utilFunc) => {
51
47
  if (!wx.request.cryptoFlag) {
52
- wx._request = wx.request;
48
+ originalRequestApi = wx.request;
49
+ // 初始化参数加签函数和性能上报函数
50
+ const { report, composeParamsFunc } = utilFunc;
51
+ encryptUtil.init(composeParamsFunc);
52
+ util.reportFunc = (...args) => {
53
+ util.logInfo(...args);
54
+ report('request_encrypt_log', ...args);
55
+ };
53
56
  proxyWxRequest();
54
57
  wx.request.cryptoFlag = true;
55
58
  }
56
59
  if (!wx.uploadFile.cryptoFlag) {
57
- wx._uploadFile = wx.uploadFile;
60
+ originalUploadFileApi = wx.uploadFile;
58
61
  proxyWxUploadFile();
59
62
  wx.uploadFile.cryptoFlag = true;
60
63
  }
@@ -68,43 +71,47 @@ function proxyWxRequest(): void {
68
71
  value(options: any) {
69
72
  const { url, method, data, header = {}, success, fail, complete, dataType, responseType } = options;
70
73
  const traceparent = genTraceparent();
74
+ const originalOptions = { ...options };
75
+
71
76
  // 如果用户自定义了dataType或者responseType,则不做处理
72
77
  if (dataType || responseType) {
73
78
  util.reportFunc(url, traceparent, '用户自定义了dataType和responseType');
74
- wx._request.call(this, Object.assign(options, {
79
+ originalRequestApi.call(this, {
80
+ ...originalOptions,
75
81
  success: (res) => {
76
82
  encryptUtil.dealEncryptionSwitch(url, res.header);
77
83
  success?.call(this, res);
78
84
  },
79
85
  header: { ...header, Traceparent: traceparent },
80
- }));
86
+ });
81
87
  return;
82
88
  }
83
89
  // 加密请求数据
84
- const { data: formatData, header: formatHeader, msg, cryptoKeyInfo } = util
85
- .reqEncrypt({ url, method, data, header });
90
+ const { data: formatData, header: formatHeader, msg, cryptoKeyInfo } = util.reqEncrypt({ url, method, data, header });
86
91
  if (!cryptoKeyInfo) {
87
92
  // 如果没有加密信息,则不走加密
88
93
  util.logInfo(url, traceparent, msg);
89
- wx._request.call(this, Object.assign(options, {
94
+ originalRequestApi.call(this, {
95
+ ...originalOptions,
90
96
  success: (res) => {
91
97
  encryptUtil.dealEncryptionSwitch(url, res.header);
92
98
  success?.call(this, res);
93
99
  },
94
100
  header: { ...header, Traceparent: traceparent },
95
- }));
101
+ });
96
102
  return;
97
103
  }
104
+
98
105
  let completeResolver;
99
- // eslint-disable-next-line
100
106
  const completePromp = new Promise(resolve => { completeResolver = resolve; });
101
- wx._request.call(this, Object.assign(options, {
107
+
108
+ originalRequestApi.call(this, {
109
+ ...originalOptions,
102
110
  data: formatData,
103
111
  header: { ...formatHeader, Traceparent: traceparent },
104
112
  dataType: '其他',
105
113
  responseType: 'arraybuffer',
106
114
  success: async (result) => {
107
- // 解密响应
108
115
  const { header: resHeader, data: resData } = result;
109
116
  const { success: resSuccess, msg, res } = await encryptUtil.resDecrypt(resHeader, resData, cryptoKeyInfo);
110
117
  if (resSuccess) {
@@ -116,13 +123,14 @@ function proxyWxRequest(): void {
116
123
  complete?.call(this, completeRes);
117
124
  } else {
118
125
  util.reportFunc(url, traceparent, `解密失败:${msg}`);
119
- wx._request.call(this, Object.assign(options, {
126
+ originalRequestApi.call(this, {
127
+ ...originalOptions,
120
128
  success: (res) => {
121
129
  encryptUtil.dealEncryptionSwitch(url, res.header);
122
130
  success?.call(this, res);
123
131
  },
124
132
  header: { ...header, Traceparent: genTraceparent() },
125
- }));
133
+ });
126
134
  }
127
135
  },
128
136
  fail: async (err) => {
@@ -133,7 +141,7 @@ function proxyWxRequest(): void {
133
141
  complete: (res) => {
134
142
  completeResolver(res);
135
143
  },
136
- }));
144
+ });
137
145
  },
138
146
  });
139
147
  }
@@ -144,7 +152,7 @@ function proxyWxUploadFile(): void {
144
152
  enumerable: true,
145
153
  configurable: true,
146
154
  value(options: any) {
147
- wx._uploadFile.call(this, Object.assign(options, {
155
+ originalUploadFileApi.call(this, Object.assign(options, {
148
156
  header: { ...options.header, Traceparent: genTraceparent() },
149
157
  }));
150
158
  },
@@ -155,14 +163,7 @@ export const encryptObjInit = (utilFunc: {
155
163
  composeParamsFunc: Function,
156
164
  report: Function,
157
165
  }) => {
158
- // 初始化参数加签函数和性能上报函数
159
- const { report, composeParamsFunc } = utilFunc;
160
- encryptUtil.init(composeParamsFunc);
161
- util.reportFunc = (...args) => {
162
- util.logInfo('request_encrypt_log', ...args);
163
- report('request_encrypt_log', ...args);
164
- };
165
166
  // 劫持wx.request和wx.uploadFile函数
166
- requestInit();
167
+ requestInit(utilFunc);
167
168
  };
168
169