@arms/rum-core 0.0.33 → 0.0.34

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.
@@ -14,6 +14,7 @@ declare class Client implements IClient {
14
14
  setContext(ctx: IContext): void;
15
15
  getContext(): IContext;
16
16
  useCollectors(collectors: ICollector[]): void;
17
+ getCollectors(): ICollector[];
17
18
  useProcessors(processors: IProcessor[]): void;
18
19
  useReporter(reporter: IReporter): void;
19
20
  }
@@ -7,9 +7,9 @@ var _client = require("../types/client");
7
7
  var _events = require("events");
8
8
  var _context = _interopRequireDefault(require("./context"));
9
9
  var _is = require("../utils/is");
10
- function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
12
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
10
+ function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
11
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
12
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
13
13
  var Client = /*#__PURE__*/function () {
14
14
  function Client() {
15
15
  var _this = this;
@@ -67,6 +67,9 @@ var Client = /*#__PURE__*/function () {
67
67
  _proto.useCollectors = function useCollectors(collectors) {
68
68
  this.collectors = collectors;
69
69
  };
70
+ _proto.getCollectors = function getCollectors() {
71
+ return this.collectors;
72
+ };
70
73
  _proto.useProcessors = function useProcessors(processors) {
71
74
  this.processors = processors;
72
75
  };
@@ -4,6 +4,7 @@ import { IShell } from "../types/shell";
4
4
  export default abstract class Shell implements IShell {
5
5
  client: IClient;
6
6
  constructor(config?: IConfiguration);
7
+ updateFromRemoteConfig(config: any): void;
7
8
  /**
8
9
  * 初始化
9
10
  */
@@ -13,6 +14,18 @@ export default abstract class Shell implements IShell {
13
14
  * get config
14
15
  */
15
16
  getConfig(): IConfiguration;
17
+ /**
18
+ * 获取远程配置
19
+ */
20
+ abstract getRemoteConfig(url: string, config: any): object;
21
+ /**
22
+ * 存储远程配置到本地
23
+ */
24
+ abstract storeRemoteConfig(config: any, pid: string): boolean;
25
+ /**
26
+ * 提取本地存储的配置
27
+ */
28
+ abstract getLocalConfig(duration: number, pid: string): any;
16
29
  /**
17
30
  * set config
18
31
  */
@@ -7,19 +7,64 @@ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")
7
7
  var _rumEvent = require("../types/rum-event");
8
8
  var _client = _interopRequireDefault(require("../model/client"));
9
9
  var _is = require("../utils/is");
10
+ var _combineConfig = _interopRequireWildcard(require("../utils/combineConfig"));
11
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
12
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
10
13
  var Shell = exports["default"] = /*#__PURE__*/function () {
11
14
  function Shell(config) {
12
15
  this.client = void 0;
13
16
  this.client = new _client["default"]();
14
17
  if (config && this.init) {
15
- this.init(config);
18
+ var combinedConf;
19
+ if ((0, _combineConfig.isRemoteConfigEnable)(config) && (0, _is.isFunction)(this.getLocalConfig)) {
20
+ var localConfig = this.getLocalConfig(0, config.pid);
21
+ combinedConf = (0, _combineConfig["default"])(config, localConfig);
22
+ }
23
+ this.init(combinedConf || config);
24
+
25
+ // 拉取远程配置进行更新
26
+ this.updateFromRemoteConfig(config);
27
+ }
28
+ }
29
+ var _proto = Shell.prototype;
30
+ _proto.updateFromRemoteConfig = function updateFromRemoteConfig(config) {
31
+ // 请求动态配置
32
+ if (config.pid && (0, _combineConfig.isRemoteConfigEnable)(config) && (0, _is.isFunction)(this.getRemoteConfig)) {
33
+ var that = this;
34
+ this.getRemoteConfig((0, _combineConfig.getRemoteConfigUrl)(config), config).then(function (remoteConfig) {
35
+ if (!remoteConfig || remoteConfig.errorStatus !== undefined) return;
36
+ var combinedConf = (0, _combineConfig["default"])(config, remoteConfig);
37
+
38
+ // 无有效的新配置,无需更新
39
+ if (!combinedConf) return;
40
+ if ((0, _is.isObject)(console) && (0, _is.isFunction)(console.log)) {
41
+ console.log("合并后动态配置后的新配置:\n", combinedConf);
42
+ }
43
+ var context = that.client.getContext();
44
+
45
+ // 更新配置
46
+ context.setConfig(combinedConf);
47
+
48
+ // 逐个更新collector
49
+ var collectors = that.client.getCollectors();
50
+ collectors.forEach(function (collector) {
51
+ if ((0, _is.isFunction)(collector.destroy)) {
52
+ collector.destroy();
53
+ collector.setup(context, that.client.sendEvent);
54
+ }
55
+ });
56
+
57
+ // 存储到本地
58
+ if ((0, _is.isFunction)(that.storeRemoteConfig)) {
59
+ that.storeRemoteConfig(remoteConfig, config.pid);
60
+ }
61
+ });
16
62
  }
17
63
  }
18
64
 
19
65
  /**
20
66
  * 初始化
21
- */
22
- var _proto = Shell.prototype;
67
+ */;
23
68
  _proto.sendEvent = function sendEvent(payload) {
24
69
  if (this.client) {
25
70
  this.client.sendEvent(payload);
@@ -36,7 +81,7 @@ var Shell = exports["default"] = /*#__PURE__*/function () {
36
81
  }
37
82
 
38
83
  /**
39
- * set config
84
+ * 获取远程配置
40
85
  */;
41
86
  /**
42
87
  * 自定义事件上报
@@ -67,6 +67,10 @@ export interface IClient {
67
67
  * 装载 client 需要的 Collector
68
68
  */
69
69
  useCollectors(collectors: ICollector[]): void;
70
+ /**
71
+ * 返回装载的collector
72
+ */
73
+ getCollectors(): ICollector[];
70
74
  /**
71
75
  * 装载 client 需要的 processor
72
76
  */
@@ -3,6 +3,9 @@
3
3
  exports.__esModule = true;
4
4
  exports.EventType = void 0;
5
5
  var EventType = exports.EventType = /*#__PURE__*/function (EventType) {
6
+ /**
7
+ * 收集数据
8
+ */
6
9
  EventType["collect"] = "collect";
7
10
  return EventType;
8
11
  }({});
@@ -2,6 +2,10 @@
2
2
  * 劫持函数
3
3
  */
4
4
  export declare function interceptFunction(target: any, name: string, callback: Function): void;
5
+ /**
6
+ * 还原函数
7
+ */
8
+ export declare function restoreFunction(target: any, name: string): void;
5
9
  /**
6
10
  * @description: GUID v4
7
11
  * @return {String}
@@ -37,3 +41,20 @@ export declare function debounce(func: Function, wait: number): () => void;
37
41
  * @param wait 延迟执行毫秒数
38
42
  */
39
43
  export declare function delay(func: Function, wait: number, ...args: any[]): number;
44
+ /**
45
+ * @desc 需要严格区分object和array
46
+ * @param obj 任意对象
47
+ */
48
+ export declare function getType(obj: any): any;
49
+ /**
50
+ * @desc 将字符串转换成正则表达式
51
+ * @param str 任意字符串
52
+ * 注意,这里只支持 /xxx/ 这种格式,不支持修饰符,例如 /xxx/ig
53
+ */
54
+ export declare function transStrToReg(str: string): string | RegExp;
55
+ /**
56
+ *
57
+ * @param config 配置
58
+ * @param keys 需要转换的字段
59
+ */
60
+ export declare function toRegFormat(config: any, keys: any[]): void;
package/lib/utils/base.js CHANGED
@@ -7,13 +7,23 @@ exports.generateEventId = generateEventId;
7
7
  exports.generateGUID = generateGUID;
8
8
  exports.generateSpanId = generateSpanId;
9
9
  exports.generateTraceId = generateTraceId;
10
+ exports.getType = getType;
10
11
  exports.interceptFunction = interceptFunction;
12
+ exports.restoreFunction = restoreFunction;
13
+ exports.toRegFormat = toRegFormat;
14
+ exports.transStrToReg = transStrToReg;
11
15
  var _is = require("./is");
16
+ // 插值前缀
17
+ var PRE_SUF_FIX = '__';
18
+ // 类型匹配正则
19
+ var TYPE_REG = /^\[object ([a-z]*)\]$/;
20
+
12
21
  /**
13
22
  * 劫持函数
14
23
  */
15
24
  function interceptFunction(target, name, callback) {
16
25
  var registeredMethod = target[name];
26
+ target["" + PRE_SUF_FIX + name + PRE_SUF_FIX] = registeredMethod;
17
27
  target[name] = function () {
18
28
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
19
29
  args[_key] = arguments[_key];
@@ -25,6 +35,17 @@ function interceptFunction(target, name, callback) {
25
35
  };
26
36
  }
27
37
 
38
+ /**
39
+ * 还原函数
40
+ */
41
+ function restoreFunction(target, name) {
42
+ var key = "" + PRE_SUF_FIX + name + PRE_SUF_FIX;
43
+ if (typeof target[key] == 'function') {
44
+ target[name] = target[key];
45
+ delete target[key];
46
+ }
47
+ }
48
+
28
49
  /**
29
50
  * @description: GUID v4
30
51
  * @return {String}
@@ -137,4 +158,68 @@ function delay(func, wait) {
137
158
  args[_key2 - 2] = arguments[_key2];
138
159
  }
139
160
  return setTimeout.apply(void 0, [func, +wait || 0].concat(args));
161
+ }
162
+
163
+ /**
164
+ * @desc 需要严格区分object和array
165
+ * @param obj 任意对象
166
+ */
167
+ function getType(obj) {
168
+ var type = Object.prototype.toString.call(obj);
169
+ type = type.toLowerCase() || '';
170
+ var arr = type.match(TYPE_REG);
171
+ return arr === null || arr === void 0 ? void 0 : arr[1];
172
+ }
173
+
174
+ /**
175
+ * @desc 将字符串转换成正则表达式
176
+ * @param str 任意字符串
177
+ * 注意,这里只支持 /xxx/ 这种格式,不支持修饰符,例如 /xxx/ig
178
+ */
179
+ function transStrToReg(str) {
180
+ if (getType(str) !== 'string') return str;
181
+ if (str.length > 2 && str[0] === '/' && str[str.length - 1] === '/') {
182
+ return new RegExp(str.substr(1, str.length - 2));
183
+ }
184
+ return str;
185
+ }
186
+
187
+ /**
188
+ *
189
+ * @param config 配置
190
+ * @param keys 需要转换的字段
191
+ */
192
+ function toRegFormat(config, keys) {
193
+ if (getType(keys) === 'string') {
194
+ keys = [keys];
195
+ }
196
+ for (var i = 0, len = keys.length; i < len; i++) {
197
+ var paths = keys[i].split('.');
198
+ var lastIndex = paths.length - 1;
199
+ if (!config) break;
200
+ var tmp = config;
201
+ for (var j = 0, jlen = paths.length; j < jlen; j++) {
202
+ if (paths[j] === '[]') {
203
+ if (getType(tmp) === 'array') {
204
+ var lastPath = paths.splice(j + 1);
205
+ lastPath = lastPath.join('.');
206
+ for (var x = 0, xlen = tmp.length; x < xlen; x++) {
207
+ toRegFormat(tmp[x], lastPath);
208
+ }
209
+ }
210
+ break;
211
+ }
212
+ // 最后一层了,需要往前一层才能修改对象
213
+ if (lastIndex === j && getType(tmp[paths[j]]) === 'string') {
214
+ tmp[paths[j]] = transStrToReg(tmp[paths[j]]);
215
+ break;
216
+ }
217
+ tmp = tmp[paths[j]];
218
+ if (!tmp) break;
219
+ }
220
+ if (!tmp || getType(tmp) !== 'array') continue;
221
+ for (var _j = 0, _jlen = tmp.length; _j < _jlen; _j++) {
222
+ tmp[_j] = transStrToReg(tmp[_j]);
223
+ }
224
+ }
140
225
  }
@@ -0,0 +1,15 @@
1
+ export declare function isRemoteConfigEnable(config: any): boolean;
2
+ /**
3
+ *
4
+ * @param config
5
+ * @returns
6
+ */
7
+ export declare function getRemoteConfigUrl(config: any): any;
8
+ /**
9
+ *
10
+ * @param oldConfig
11
+ * @param remoteConfig
12
+ * @returns config
13
+ */
14
+ declare function combineConfig(oldConfig?: {}, remoteConfig?: any): {};
15
+ export default combineConfig;
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ exports.__esModule = true;
5
+ exports["default"] = void 0;
6
+ exports.getRemoteConfigUrl = getRemoteConfigUrl;
7
+ exports.isRemoteConfigEnable = isRemoteConfigEnable;
8
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
+ var _base = require("./base");
10
+ // 需要检查是否需要字符串格式化为正则的配置路径
11
+ var REG_FORMAT_PATHS = ['filters.exception', 'filters.resource', 'tracing.allowedUrls.[].match'];
12
+ function isRemoteConfigEnable(config) {
13
+ return config.remoteConfig === true || config.remoteConfig && config.remoteConfig.enable !== false;
14
+ }
15
+
16
+ /**
17
+ *
18
+ * @param config
19
+ * @returns
20
+ */
21
+ function getRemoteConfigUrl(config) {
22
+ if (config.remoteConfig && config.remoteConfig.url) {
23
+ return config.remoteConfig.url;
24
+ }
25
+ var sereis = config.pid.split('@');
26
+ if (!sereis || sereis.length < 2) return;
27
+ var timestamp = Date.now();
28
+ return "//" + sereis[0] + "-sdk.rum.aliyuncs.com/config/" + (config.remoteConfig.region || 'cn-hangzhou') + "/" + sereis[1] + "?t=" + timestamp;
29
+ }
30
+
31
+ /**
32
+ *
33
+ * @param oldConfig
34
+ * @param remoteConfig
35
+ */
36
+ function doCover(oldConfig, remoteConfig) {
37
+ if (oldConfig === void 0) {
38
+ oldConfig = {};
39
+ }
40
+ if (remoteConfig === void 0) {
41
+ remoteConfig = {};
42
+ }
43
+ for (var key in oldConfig) {
44
+ var _remoteConfig, _remoteConfig2;
45
+ if (key === 'pid' || key === 'endpoint') continue;
46
+ if (((_remoteConfig = remoteConfig) === null || _remoteConfig === void 0 ? void 0 : _remoteConfig[key]) === undefined) continue;
47
+ var oldV = oldConfig[key];
48
+ var newV = (_remoteConfig2 = remoteConfig) === null || _remoteConfig2 === void 0 ? void 0 : _remoteConfig2[key];
49
+ if ((0, _base.getType)(oldV) === 'object' && (0, _base.getType)(newV) === 'object') {
50
+ doCover(oldV, newV);
51
+ } else {
52
+ oldConfig[key] = newV;
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ *
59
+ * @param oldConfig
60
+ * @param remoteConfig
61
+ */
62
+ function doSupplement(oldConfig, remoteConfig) {
63
+ if (oldConfig === void 0) {
64
+ oldConfig = {};
65
+ }
66
+ if (remoteConfig === void 0) {
67
+ remoteConfig = {};
68
+ }
69
+ for (var key in remoteConfig) {
70
+ var _oldConfig;
71
+ if (((_oldConfig = oldConfig) === null || _oldConfig === void 0 ? void 0 : _oldConfig[key]) === undefined) {
72
+ oldConfig[key] = remoteConfig[key];
73
+ } else {
74
+ var oldV = oldConfig[key];
75
+ var newV = remoteConfig[key];
76
+ if ((0, _base.getType)(oldV) === 'object' && (0, _base.getType)(newV) === 'object') {
77
+ doSupplement(oldV, newV);
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ /**
84
+ *
85
+ * @param oldConfig
86
+ * @param remoteConfig
87
+ * @returns config
88
+ */
89
+ function combineConfig(oldConfig, remoteConfig) {
90
+ if (oldConfig === void 0) {
91
+ oldConfig = {};
92
+ }
93
+ if (remoteConfig === void 0) {
94
+ remoteConfig = null;
95
+ }
96
+ if (!remoteConfig) return null;
97
+ if ((0, _base.getType)(oldConfig) !== 'object' || (0, _base.getType)(remoteConfig) !== 'object') return null;
98
+ var config = (0, _extends2["default"])({}, oldConfig);
99
+ // 先检查远程配置的,是否有需要将字符串格式化为正则的
100
+ (0, _base.toRegFormat)(remoteConfig, REG_FORMAT_PATHS);
101
+ // 执行覆盖
102
+ doCover(config, remoteConfig);
103
+ // 执行补充
104
+ doSupplement(config, remoteConfig);
105
+ return config;
106
+ }
107
+ var _default = exports["default"] = combineConfig;
@@ -1,7 +1,7 @@
1
- export type MatchOption = string | RegExp | ((value: string) => boolean);
1
+ export type MatchOption = string | RegExp | ((value: string, option?: unknown) => boolean);
2
2
  export declare function isMatchOption(item: unknown): item is MatchOption;
3
3
  /**
4
4
  * 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
5
5
  */
6
- export declare function matchList(list: MatchOption[], value: string, useStartsWith?: boolean): boolean;
6
+ export declare function matchList(list: MatchOption[], value: string, useStartsWith?: boolean, options?: unknown): boolean;
7
7
  export declare function urlMatch(url: string, list?: MatchOption | MatchOption[]): boolean;
@@ -13,14 +13,14 @@ function isMatchOption(item) {
13
13
  /**
14
14
  * 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
15
15
  */
16
- function matchList(list, value, useStartsWith) {
16
+ function matchList(list, value, useStartsWith, options) {
17
17
  if (useStartsWith === void 0) {
18
18
  useStartsWith = true;
19
19
  }
20
20
  return list.some(function (item) {
21
21
  try {
22
22
  if (typeof item === 'function') {
23
- return item(value);
23
+ return item(value, options);
24
24
  } else if (item instanceof RegExp) {
25
25
  return item.test(value);
26
26
  } else if (typeof item === 'string') {
@@ -19,6 +19,9 @@ function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, subOption
19
19
  if (propagatorTypes.includes('sw8')) {
20
20
  propagatorTypes = ['sw8'];
21
21
  }
22
+ if (!(0, _is.isArray)(propagatorTypes)) {
23
+ propagatorTypes = [propagatorTypes];
24
+ }
22
25
  propagatorTypes.forEach(function (propagatorType) {
23
26
  switch (propagatorType) {
24
27
  case 'jaeger':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arms/rum-core",
3
- "version": "0.0.33",
3
+ "version": "0.0.34",
4
4
  "description": "arms rum javascript sdk core",
5
5
  "author": "guangli.fj <guangli.fj@alibaba-inc.com>",
6
6
  "license": "ISC",