@tmsfe/tms-core 0.0.155 → 0.0.157

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.157",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -166,8 +166,14 @@ const eccUtil = {
166
166
  }
167
167
  case 11307: // 加密未开启
168
168
  return { retry: false, success: false };
169
- default:
169
+ case 11301:
170
+ case 11302:
171
+ case 11303:
172
+ case 11304:
173
+ case 11308:
170
174
  return { retry: false, success: false };
175
+ default: // 其他网关错误码
176
+ return { retry: false, success: true };
171
177
  }
172
178
  },
173
179
  execEncrypt: (input: string, ignoreNull = false): BaseResp<{
@@ -223,9 +229,9 @@ const eccUtil = {
223
229
  // 加密规则判断工具
224
230
  const cryptRuleUtil = {
225
231
  // 远程加密服务是否开启
226
- _isServerOpen: (): boolean => !!wx.$_publicKey,
232
+ isServerOpen: (): boolean => !!wx.$_publicKey,
227
233
  // 检查path是否符合下发的路由前缀
228
- _pathInEnablePrefix: (path: string): boolean => {
234
+ pathInEnablePrefix: (path: string): boolean => {
229
235
  if (!wx.$_publicKey) {
230
236
  return false;
231
237
  }
@@ -242,7 +248,7 @@ const cryptRuleUtil = {
242
248
  return false;
243
249
  },
244
250
  // 判断是否是性能埋点上报接口
245
- _isPerformanceReport: (path: string, params: any): boolean => {
251
+ isPerformanceReport: (path: string, params: any): boolean => {
246
252
  // 如果是日志上报接口,需要过滤性能日志,不需要加密
247
253
  if (path.indexOf('basic/event/upload') > -1) {
248
254
  if (params.batch?.length === 1 && params.batch[0]?.[31] === 'tms-performance-log') {
@@ -261,7 +267,7 @@ const cryptRuleUtil = {
261
267
  '^/tde', '^/basic/crypto/lastkey',
262
268
  ],
263
269
  },
264
- _isHostValid: (url) => {
270
+ isHostValid: (url) => {
265
271
  // 使用正则表达式解析URL
266
272
  const urlPattern = /^(https?:\/\/)?([^/?#]+)([/?#].*)?$/;
267
273
  const matches = url.match(urlPattern);
@@ -297,19 +303,19 @@ const isCryptoRuleMath = (path: string, reqData: any): BaseResp<boolean> => {
297
303
  return new baseUtil.BaseRespFac(false, false, '本地加密未开启');
298
304
  }
299
305
  // 如果服务端下发的加密开关关闭,不走加密
300
- if (!cryptRuleUtil._isServerOpen()) {
306
+ if (!cryptRuleUtil.isServerOpen()) {
301
307
  return new baseUtil.BaseRespFac(false, false, '服务端加密未开启');
302
308
  }
303
309
  // 请求路由不满足服务端下发的加密规则,不走加密
304
- if (!cryptRuleUtil._pathInEnablePrefix(path)) {
310
+ if (!cryptRuleUtil.pathInEnablePrefix(path)) {
305
311
  return new baseUtil.BaseRespFac(false, false, '未命中服务端加密规则');
306
312
  }
307
313
  // 请求接口是加密性能埋点上报接口,不加密
308
- if (cryptRuleUtil._isPerformanceReport(path, reqData)) {
314
+ if (cryptRuleUtil.isPerformanceReport(path, reqData)) {
309
315
  return new baseUtil.BaseRespFac(false, false, '性能埋点');
310
316
  }
311
317
  // 请求路由不走sinan网关,不加密
312
- if (!cryptRuleUtil._isHostValid(path)) {
318
+ if (!cryptRuleUtil.isHostValid(path)) {
313
319
  return new baseUtil.BaseRespFac(false, false, '非sinan网关加密接口');
314
320
  }
315
321
  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,16 +71,19 @@ 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
  // 加密请求数据
@@ -86,25 +92,28 @@ function proxyWxRequest(): void {
86
92
  if (!cryptoKeyInfo) {
87
93
  // 如果没有加密信息,则不走加密
88
94
  util.logInfo(url, traceparent, msg);
89
- wx._request.call(this, Object.assign(options, {
95
+ originalRequestApi.call(this, {
96
+ ...originalOptions,
90
97
  success: (res) => {
91
98
  encryptUtil.dealEncryptionSwitch(url, res.header);
92
99
  success?.call(this, res);
93
100
  },
94
101
  header: { ...header, Traceparent: traceparent },
95
- }));
102
+ });
96
103
  return;
97
104
  }
105
+
98
106
  let completeResolver;
99
- // eslint-disable-next-line
100
- const completePromp = new Promise(resolve => { completeResolver = resolve; });
101
- wx._request.call(this, Object.assign(options, {
107
+ const completePromp = new Promise((resolve) => {
108
+ completeResolver = resolve;
109
+ });
110
+ originalRequestApi.call(this, {
111
+ ...originalOptions,
102
112
  data: formatData,
103
113
  header: { ...formatHeader, Traceparent: traceparent },
104
114
  dataType: '其他',
105
115
  responseType: 'arraybuffer',
106
116
  success: async (result) => {
107
- // 解密响应
108
117
  const { header: resHeader, data: resData } = result;
109
118
  const { success: resSuccess, msg, res } = await encryptUtil.resDecrypt(resHeader, resData, cryptoKeyInfo);
110
119
  if (resSuccess) {
@@ -116,13 +125,14 @@ function proxyWxRequest(): void {
116
125
  complete?.call(this, completeRes);
117
126
  } else {
118
127
  util.reportFunc(url, traceparent, `解密失败:${msg}`);
119
- wx._request.call(this, Object.assign(options, {
128
+ originalRequestApi.call(this, {
129
+ ...originalOptions,
120
130
  success: (res) => {
121
131
  encryptUtil.dealEncryptionSwitch(url, res.header);
122
132
  success?.call(this, res);
123
133
  },
124
134
  header: { ...header, Traceparent: genTraceparent() },
125
- }));
135
+ });
126
136
  }
127
137
  },
128
138
  fail: async (err) => {
@@ -133,7 +143,7 @@ function proxyWxRequest(): void {
133
143
  complete: (res) => {
134
144
  completeResolver(res);
135
145
  },
136
- }));
146
+ });
137
147
  },
138
148
  });
139
149
  }
@@ -144,7 +154,7 @@ function proxyWxUploadFile(): void {
144
154
  enumerable: true,
145
155
  configurable: true,
146
156
  value(options: any) {
147
- wx._uploadFile.call(this, Object.assign(options, {
157
+ originalUploadFileApi.call(this, Object.assign(options, {
148
158
  header: { ...options.header, Traceparent: genTraceparent() },
149
159
  }));
150
160
  },
@@ -155,14 +165,7 @@ export const encryptObjInit = (utilFunc: {
155
165
  composeParamsFunc: Function,
156
166
  report: Function,
157
167
  }) => {
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
168
  // 劫持wx.request和wx.uploadFile函数
166
- requestInit();
169
+ requestInit(utilFunc);
167
170
  };
168
171