@arms/rum-core 0.0.3 → 0.0.5

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/index.d.ts CHANGED
@@ -8,5 +8,9 @@ export * from './types/reporter';
8
8
  export * from './types/rum-event';
9
9
  export * from './types/shell';
10
10
  export * from './utils/base';
11
+ export * from './utils/polyfills';
11
12
  export * from './utils/is';
13
+ export * from './utils/number';
14
+ export * from './utils/match';
15
+ export * from './utils/trace';
12
16
  export { Client, Context, Reporter };
package/lib/index.js CHANGED
@@ -62,10 +62,38 @@ Object.keys(_base).forEach(function (key) {
62
62
  if (key in exports && exports[key] === _base[key]) return;
63
63
  exports[key] = _base[key];
64
64
  });
65
+ var _polyfills = require("./utils/polyfills");
66
+ Object.keys(_polyfills).forEach(function (key) {
67
+ if (key === "default" || key === "__esModule") return;
68
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
69
+ if (key in exports && exports[key] === _polyfills[key]) return;
70
+ exports[key] = _polyfills[key];
71
+ });
65
72
  var _is = require("./utils/is");
66
73
  Object.keys(_is).forEach(function (key) {
67
74
  if (key === "default" || key === "__esModule") return;
68
75
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
69
76
  if (key in exports && exports[key] === _is[key]) return;
70
77
  exports[key] = _is[key];
78
+ });
79
+ var _number = require("./utils/number");
80
+ Object.keys(_number).forEach(function (key) {
81
+ if (key === "default" || key === "__esModule") return;
82
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
83
+ if (key in exports && exports[key] === _number[key]) return;
84
+ exports[key] = _number[key];
85
+ });
86
+ var _match = require("./utils/match");
87
+ Object.keys(_match).forEach(function (key) {
88
+ if (key === "default" || key === "__esModule") return;
89
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
90
+ if (key in exports && exports[key] === _match[key]) return;
91
+ exports[key] = _match[key];
92
+ });
93
+ var _trace = require("./utils/trace");
94
+ Object.keys(_trace).forEach(function (key) {
95
+ if (key === "default" || key === "__esModule") return;
96
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
97
+ if (key in exports && exports[key] === _trace[key]) return;
98
+ exports[key] = _trace[key];
71
99
  });
@@ -41,10 +41,11 @@ var Client = /*#__PURE__*/function () {
41
41
  return true;
42
42
  };
43
43
  }
44
- ;
45
44
  if (processor.match(ctx)) {
46
45
  var res = processor.process(ctx);
47
- ctx.setRumEvent(res);
46
+ if (res) {
47
+ ctx.setRumEvent(res);
48
+ }
48
49
  }
49
50
  }
50
51
  reporter.report(ctx);
@@ -2,6 +2,7 @@ import { ICollector } from './collector';
2
2
  import { IProcessor } from './processor';
3
3
  import { IReporter } from './reporter';
4
4
  import { RumEvent, RumEventBundle } from './rum-event';
5
+ import { ITracingOption } from '../utils/trace';
5
6
  export declare enum EventType {
6
7
  /**
7
8
  * 收集数据
@@ -95,5 +96,9 @@ export interface IConfiguration {
95
96
  * 各个 collector config
96
97
  */
97
98
  collectors?: any;
99
+ /**
100
+ * tracing 配置 'tracecontext' | 'b3' | 'b3multi' | 'jaeger';
101
+ */
102
+ tracing?: boolean | ITracingOption;
98
103
  [key: string]: any;
99
104
  }
@@ -3,12 +3,14 @@
3
3
  */
4
4
  export declare function interceptFunction(options: any, name: string, callback: Function): void;
5
5
  /**
6
- * GUID v4
7
- * from https://gist.github.com/jed/982883
6
+ * @description: GUID v4
7
+ * @from https://gist.github.com/jed/982883
8
+ * @param placeholder {String}
9
+ * @return {String}
8
10
  */
9
11
  export declare function generateGUID(placeholder?: string): string;
10
12
  /**
11
- * 唯一的字符串 universe unique id
13
+ * @description: 唯一的字符串 universe unique id
12
14
  * @param len {String}
13
15
  * @return {String}
14
16
  */
package/lib/utils/base.js CHANGED
@@ -21,8 +21,10 @@ function interceptFunction(options, name, callback) {
21
21
  }
22
22
 
23
23
  /**
24
- * GUID v4
25
- * from https://gist.github.com/jed/982883
24
+ * @description: GUID v4
25
+ * @from https://gist.github.com/jed/982883
26
+ * @param placeholder {String}
27
+ * @return {String}
26
28
  */
27
29
  function generateGUID(placeholder) {
28
30
  return placeholder ?
@@ -31,7 +33,7 @@ function generateGUID(placeholder) {
31
33
  }
32
34
 
33
35
  /**
34
- * 唯一的字符串 universe unique id
36
+ * @description: 唯一的字符串 universe unique id
35
37
  * @param len {String}
36
38
  * @return {String}
37
39
  */
package/lib/utils/is.d.ts CHANGED
@@ -2,3 +2,9 @@ export type IsFnHelper<T = unknown> = (value: unknown) => value is T;
2
2
  export declare function isTypeof<T = unknown>(value: unknown, type: string): value is T;
3
3
  export declare const isFunction: (func: any) => boolean;
4
4
  export declare const isUndefined: IsFnHelper<undefined>;
5
+ export declare const isString: IsFnHelper<string>;
6
+ export declare function isToString<T = unknown>(value: unknown, type: string): value is T;
7
+ export declare const isArray: IsFnHelper<unknown[]>;
8
+ export declare const isRegExp: IsFnHelper<string>;
9
+ export declare const isBoolean: IsFnHelper<boolean>;
10
+ export declare const isNumber: IsFnHelper<number | bigint>;
package/lib/utils/is.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.isFunction = void 0;
4
+ exports.isString = exports.isRegExp = exports.isNumber = exports.isFunction = exports.isBoolean = exports.isArray = void 0;
5
+ exports.isToString = isToString;
5
6
  exports.isTypeof = isTypeof;
6
7
  exports.isUndefined = void 0;
7
8
  function isTypeof(value, type) {
@@ -12,4 +13,22 @@ var isFunction = exports.isFunction = function isFunction(func) {
12
13
  };
13
14
  var isUndefined = exports.isUndefined = function isUndefined(value) {
14
15
  return isTypeof(value, 'undefined');
16
+ };
17
+ var isString = exports.isString = function isString(value) {
18
+ return isTypeof(value, 'string');
19
+ };
20
+ function isToString(value, type) {
21
+ return Object.prototype.toString.call(value) === "[object " + type + "]";
22
+ }
23
+ var isArray = exports.isArray = function isArray(value) {
24
+ return isToString(value, 'Array');
25
+ };
26
+ var isRegExp = exports.isRegExp = function isRegExp(value) {
27
+ return isToString(value, 'RegExp');
28
+ };
29
+ var isBoolean = exports.isBoolean = function isBoolean(value) {
30
+ return isTypeof(value, 'boolean');
31
+ };
32
+ var isNumber = exports.isNumber = function isNumber(value) {
33
+ return isTypeof(value, 'number') && !isNaN(value) || isTypeof(value, 'bigint');
15
34
  };
@@ -0,0 +1,7 @@
1
+ export type MatchOption = string | RegExp | ((value: string) => boolean);
2
+ export declare function isMatchOption(item: unknown): item is MatchOption;
3
+ /**
4
+ * 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
5
+ */
6
+ export declare function matchList(list: MatchOption[], value: string, useStartsWith?: boolean): boolean;
7
+ export declare function urlMatch(url: string, list?: MatchOption[]): boolean;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.isMatchOption = isMatchOption;
5
+ exports.matchList = matchList;
6
+ exports.urlMatch = urlMatch;
7
+ var _is = require("./is");
8
+ var _polyfills = require("./polyfills");
9
+ function isMatchOption(item) {
10
+ return (0, _is.isString)(item) || (0, _is.isFunction)(item) || (0, _is.isRegExp)(item);
11
+ }
12
+
13
+ /**
14
+ * 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
15
+ */
16
+ function matchList(list, value, useStartsWith) {
17
+ if (useStartsWith === void 0) {
18
+ useStartsWith = false;
19
+ }
20
+ return list.some(function (item) {
21
+ try {
22
+ if (typeof item === 'function') {
23
+ return item(value);
24
+ } else if (item instanceof RegExp) {
25
+ return item.test(value);
26
+ } else if (typeof item === 'string') {
27
+ return useStartsWith ? (0, _polyfills.startsWith)(value, item) : item === value;
28
+ }
29
+ } catch (e) {}
30
+ return false;
31
+ });
32
+ }
33
+ function urlMatch(url, list) {
34
+ if (list === void 0) {
35
+ list = [];
36
+ }
37
+ try {
38
+ var rules = [/https?:\/\/.*rum|retcode\.aliyuncs\.com/, /https?:\/\/.*log-global\.aliyuncs\.com/, /https?:\/\/.*\.mmstat\.com/].concat(list);
39
+ return matchList(rules, url);
40
+ } catch (e) {
41
+ return false;
42
+ }
43
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Return true if the draw is successful
3
+ * @param threshold between 0 and 100
4
+ */
5
+ export declare function performDraw(threshold: number): boolean;
6
+ export declare function round(num: number, decimals: 0 | 1 | 2 | 3 | 4): number;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.performDraw = performDraw;
5
+ exports.round = round;
6
+ /**
7
+ * Return true if the draw is successful
8
+ * @param threshold between 0 and 100
9
+ */
10
+ function performDraw(threshold) {
11
+ return threshold !== 0 && Math.random() * 100 <= threshold;
12
+ }
13
+ function round(num, decimals) {
14
+ return +num.toFixed(decimals);
15
+ }
@@ -0,0 +1,7 @@
1
+ export declare function find<T, S extends T>(array: ArrayLike<T>, predicate: (item: T, index: number) => item is S): S | undefined;
2
+ export declare function find<T>(array: ArrayLike<T>, predicate: (item: T, index: number) => boolean): T | undefined;
3
+ export declare function startsWith(candidate: string, search: string): boolean;
4
+ export declare function endsWith(candidate: string, search: string): boolean;
5
+ export declare function assign<T, U>(target: T, source: U): T & U;
6
+ export declare function assign<T, U, V>(target: T, source1: U, source2: V): T & U & V;
7
+ export declare function assign<T, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.assign = assign;
5
+ exports.endsWith = endsWith;
6
+ exports.find = find;
7
+ exports.startsWith = startsWith;
8
+ function find(array, predicate) {
9
+ for (var i = 0; i < array.length; i += 1) {
10
+ var _item = array[i];
11
+ if (predicate(_item, i)) {
12
+ return _item;
13
+ }
14
+ }
15
+ return undefined;
16
+ }
17
+ function startsWith(candidate, search) {
18
+ return candidate.slice(0, search.length) === search;
19
+ }
20
+ function endsWith(candidate, search) {
21
+ return candidate.slice(-search.length) === search;
22
+ }
23
+ function assign(target) {
24
+ for (var _len = arguments.length, toAssign = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
25
+ toAssign[_key - 1] = arguments[_key];
26
+ }
27
+ toAssign.forEach(function (source) {
28
+ for (var _key2 in source) {
29
+ if (Object.prototype.hasOwnProperty.call(source, _key2)) {
30
+ target[_key2] = source[_key2];
31
+ }
32
+ }
33
+ });
34
+ return target;
35
+ }
@@ -0,0 +1,20 @@
1
+ import { MatchOption } from "./match";
2
+ export type PropagatorType = 'tracecontext' | 'b3' | 'b3multi' | 'jaeger';
3
+ export type TraceOption = {
4
+ match: MatchOption;
5
+ propagatorTypes: PropagatorType[];
6
+ };
7
+ export declare function isTraceOption(option: any): option is TraceOption;
8
+ export interface ITracingOption {
9
+ enable?: boolean;
10
+ sample?: number | undefined;
11
+ allowedUrls?: Array<MatchOption | TraceOption> | undefined;
12
+ tracestate?: boolean;
13
+ }
14
+ export declare function generateTraceId(): any;
15
+ export declare function generateSpanId(): any;
16
+ export interface ITracingHeaders {
17
+ [key: string]: string;
18
+ }
19
+ export declare function makeTracingHeaders(traceId: string, spanId: string, sampled: boolean, propagatorTypes: PropagatorType[], tracestate?: string): ITracingHeaders;
20
+ export declare function parseTracingOptions(tracingOption: boolean | ITracingOption): ITracingOption;
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.generateSpanId = generateSpanId;
5
+ exports.generateTraceId = generateTraceId;
6
+ exports.isTraceOption = isTraceOption;
7
+ exports.makeTracingHeaders = makeTracingHeaders;
8
+ exports.parseTracingOptions = parseTracingOptions;
9
+ var _match = require("./match");
10
+ var _is = require("./is");
11
+ var SPAN_ID_BYTES = 8;
12
+ var TRACE_ID_BYTES = 16;
13
+ var SHARED_CHAR_CODES_ARRAY = Array(32);
14
+ function isTraceOption(option) {
15
+ return option && (0, _match.isMatchOption)(option.match) && Array.isArray(option.propagatorTypes);
16
+ }
17
+ function generateId(bytes) {
18
+ for (var i = 0; i < bytes * 2; i++) {
19
+ SHARED_CHAR_CODES_ARRAY[i] = Math.floor(Math.random() * 16) + 48;
20
+ // valid hex characters in the range 48-57 and 97-102
21
+ if (SHARED_CHAR_CODES_ARRAY[i] >= 58) {
22
+ SHARED_CHAR_CODES_ARRAY[i] += 39;
23
+ }
24
+ }
25
+ return String.fromCharCode.apply(null, SHARED_CHAR_CODES_ARRAY.slice(0, bytes * 2));
26
+ }
27
+ function generateTraceId() {
28
+ var traceId = generateId(TRACE_ID_BYTES);
29
+ // 适配OpenTracing 实现(long long转成16进制最高位不能为0)
30
+ if (traceId[0] === '0') {
31
+ traceId = '1' + traceId.substring(1);
32
+ }
33
+ if (traceId[16] === '0') {
34
+ traceId = traceId.substring(0, 16) + '1' + traceId.substring(17);
35
+ }
36
+ return traceId;
37
+ }
38
+ function generateSpanId() {
39
+ return generateId(SPAN_ID_BYTES);
40
+ }
41
+
42
+ // export function getB3Header(traceId: string, spanId: string) {
43
+ // return `${traceId}-${spanId}-1`
44
+ // }
45
+ //
46
+ // export function getW3CTraceHeader(traceId: string, spanId: string) {
47
+ // return `00-${traceId}-${spanId}-01`
48
+ // }
49
+ //
50
+ // export function getJaegerTraceHeader(traceId: string, spanId: string) {
51
+ // return `${traceId}:${spanId}:0:1`
52
+ // }
53
+
54
+ function makeTracingHeaders(traceId, spanId, sampled, propagatorTypes, tracestate) {
55
+ var tracingHeaders = {};
56
+ var sample = sampled ? '1' : '0';
57
+ propagatorTypes.forEach(function (propagatorType) {
58
+ switch (propagatorType) {
59
+ case 'jaeger':
60
+ tracingHeaders['uber-trace-id'] = traceId + ":" + spanId + ":0:" + sample;
61
+ break;
62
+ case 'b3':
63
+ // https://github.com/openzipkin/b3-propagation
64
+ tracingHeaders['b3'] = traceId + "-" + spanId + "-" + sample;
65
+ break;
66
+ case 'b3multi':
67
+ tracingHeaders['X-B3-TraceId'] = traceId;
68
+ tracingHeaders['X-B3-SpanId'] = spanId;
69
+ tracingHeaders['X-B3-Sampled'] = sample;
70
+ break;
71
+ case 'tracecontext': // https://www.w3.org/TR/trace-context/
72
+ default:
73
+ tracingHeaders['traceparent'] = "00-" + traceId + "-" + spanId + "-0" + sample;
74
+ if (tracestate) {
75
+ tracingHeaders['tracestate'] = tracestate;
76
+ }
77
+ break;
78
+ }
79
+ });
80
+ return tracingHeaders;
81
+ }
82
+ function parseTracingOptions(tracingOption) {
83
+ if ((0, _is.isBoolean)(tracingOption) || !tracingOption) {
84
+ return {
85
+ enable: !!tracingOption,
86
+ sample: 100,
87
+ allowedUrls: [],
88
+ tracestate: true
89
+ };
90
+ }
91
+ var _tracingOption$enable = tracingOption.enable,
92
+ enable = _tracingOption$enable === void 0 ? true : _tracingOption$enable,
93
+ _tracingOption$sample = tracingOption.sample,
94
+ sample = _tracingOption$sample === void 0 ? 100 : _tracingOption$sample,
95
+ _tracingOption$allowe = tracingOption.allowedUrls,
96
+ allowedUrls = _tracingOption$allowe === void 0 ? [] : _tracingOption$allowe,
97
+ _tracingOption$traces = tracingOption.tracestate,
98
+ tracestate = _tracingOption$traces === void 0 ? true : _tracingOption$traces;
99
+ var traceOptions = [];
100
+ if ((0, _is.isArray)(allowedUrls) && allowedUrls.length) {
101
+ allowedUrls.forEach(function (item) {
102
+ if ((0, _match.isMatchOption)(item)) {
103
+ traceOptions.push({
104
+ match: item,
105
+ propagatorTypes: ['tracecontext']
106
+ });
107
+ } else if (isTraceOption(item)) {
108
+ traceOptions.push(item);
109
+ }
110
+ });
111
+ }
112
+ return {
113
+ enable: (0, _is.isBoolean)(enable) ? enable : true,
114
+ sample: (0, _is.isNumber)(sample) ? sample : 100,
115
+ allowedUrls: traceOptions,
116
+ tracestate: tracestate
117
+ };
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arms/rum-core",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "arms rum javascript sdk core",
5
5
  "author": "guangli.fj <guangli.fj@alibaba-inc.com>",
6
6
  "license": "ISC",