@arms/rum-core 0.0.25-beta.16 → 0.0.25-beta.18

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,4 +14,4 @@ export * from './utils/is';
14
14
  export * from './utils/number';
15
15
  export * from './utils/match';
16
16
  export * from './utils/trace';
17
- export { Client, Context, Reporter, Shell };
17
+ export { Client, Context, Reporter, Shell };
@@ -0,0 +1,20 @@
1
+ import { IClient, IConfiguration, IContext, IRumSession } from '../types/client';
2
+ import { RumEvent } from '../types/rum-event';
3
+ import { IProcessor } from '../types/processor';
4
+ import { ICollector } from '../types/collector';
5
+ import { IReporter } from '../types/reporter';
6
+ declare class Client implements IClient {
7
+ private emitter;
8
+ private collectors;
9
+ private processors;
10
+ private reporter;
11
+ private ctx;
12
+ init(config: IConfiguration, rumSession?: IRumSession): void;
13
+ sendEvent: (payload: RumEvent) => void;
14
+ setContext(ctx: IContext): void;
15
+ getContext(): IContext;
16
+ useCollectors(collectors: ICollector[]): void;
17
+ useProcessors(processors: IProcessor[]): void;
18
+ useReporter(reporter: IReporter): void;
19
+ }
20
+ export default Client;
@@ -0,0 +1,18 @@
1
+ import { IConfiguration, IContext, IRumSession } from "../types/client";
2
+ import { RumEvent, IViewData } from "../types/rum-event";
3
+ declare class Context implements IContext {
4
+ private config;
5
+ private rumEvent;
6
+ private views;
7
+ session: IRumSession;
8
+ constructor(config: IConfiguration, rumSession?: IRumSession);
9
+ getConfig(): IConfiguration;
10
+ setConfig(config: IConfiguration): void;
11
+ getViews(): IViewData[];
12
+ addView(view: IViewData): void;
13
+ removeView(viewId: string): void;
14
+ getRumEvent(): RumEvent;
15
+ setRumEvent(rumEvent: RumEvent): void;
16
+ [key: string]: any;
17
+ }
18
+ export default Context;
@@ -0,0 +1,30 @@
1
+ import { IContext } from '../types/client';
2
+ import { RumEvent, IViewData } from '../types/rum-event';
3
+ export default abstract class Reporter {
4
+ name: string;
5
+ /**
6
+ * 新上报事件优先检测队列是否超过 50 条,如果超过立即 report。
7
+ * 如果没超过则检测 FlushTime 内有无新上报事件,如果有新事件,重置 timer,无则 report
8
+ */
9
+ protected eventQueue: RumEvent[];
10
+ protected ctx: IContext;
11
+ private timer;
12
+ getReportCfg(): {
13
+ flushTime?: number;
14
+ maxEventCount?: number;
15
+ };
16
+ report(ctx: IContext): void;
17
+ private pushToQueue;
18
+ /**
19
+ * eventQueue 提取最新 View 作为公共 View,event 中不同 View Event 保留 View id
20
+ */
21
+ protected flushEventQueue(): void;
22
+ /**
23
+ * 接口请求由各平台 sdk reporter 实现
24
+ */
25
+ abstract request(ctx: IContext, events: RumEvent[], view: IViewData): void;
26
+ /**
27
+ * 初始化时
28
+ */
29
+ protected init(ctx: IContext): void;
30
+ }
@@ -0,0 +1,33 @@
1
+ import { IClient, IConfiguration } from "../types/client";
2
+ import { RumCustomEvent, RumEvent, RumExceptionEvent, RumResourceEvent } from "../types/rum-event";
3
+ import { IShell } from "../types/shell";
4
+ export default abstract class Shell implements IShell {
5
+ client: IClient;
6
+ constructor(config?: IConfiguration);
7
+ /**
8
+ * 初始化
9
+ */
10
+ abstract init(configuration: IConfiguration): void;
11
+ sendEvent(payload: RumEvent): void;
12
+ /**
13
+ * get config
14
+ */
15
+ getConfig(): IConfiguration;
16
+ /**
17
+ * set config
18
+ */
19
+ abstract setConfig<T extends keyof IConfiguration>(key: T, value: IConfiguration[T]): void;
20
+ abstract setConfig(value: IConfiguration): void;
21
+ /**
22
+ * 自定义事件上报
23
+ */
24
+ sendCustom(payload: RumCustomEvent): void;
25
+ /**
26
+ * 自定义异常上报
27
+ */
28
+ sendException(payload: RumExceptionEvent): void;
29
+ /**
30
+ * 自定义资源上报
31
+ */
32
+ sendResource(payload: RumResourceEvent): void;
33
+ }
@@ -0,0 +1,133 @@
1
+ import { ICollector } from './collector';
2
+ import { IProcessor } from './processor';
3
+ import { IReporter } from './reporter';
4
+ import { RumEvent, RumEventBundle, IViewData } from './rum-event';
5
+ import { MatchOption } from "../utils/match";
6
+ export declare enum EventType {
7
+ /**
8
+ * 收集数据
9
+ */
10
+ collect = "collect"
11
+ }
12
+ export interface IContext {
13
+ session: IRumSession;
14
+ /**
15
+ * user config
16
+ */
17
+ getConfig(): IConfiguration;
18
+ setConfig(config: IConfiguration): void;
19
+ /**
20
+ * view
21
+ */
22
+ getViews(): IViewData[];
23
+ addView(view: IViewData): void;
24
+ removeView(viewId: string): void;
25
+ /**
26
+ * event
27
+ */
28
+ getRumEvent(): RumEvent;
29
+ setRumEvent(rumEvent: RumEvent): void;
30
+ [key: string]: any;
31
+ }
32
+ export interface SessionConfig {
33
+ sampleRate?: number;
34
+ maxDuration?: number;
35
+ overtime?: number;
36
+ }
37
+ export interface IRumSession {
38
+ init(config: IConfiguration): void;
39
+ sessionConfig: SessionConfig;
40
+ getSessionId: () => string;
41
+ getSampled: () => boolean;
42
+ updateSession: () => void;
43
+ getEventId: () => string;
44
+ getViewId: () => string;
45
+ getUserId: () => string;
46
+ }
47
+ export interface IClient {
48
+ /**
49
+ * 初始化
50
+ */
51
+ init: (configuration: IConfiguration, rumSession?: IRumSession) => void;
52
+ /**
53
+ * 业务自定义上传数据
54
+ */
55
+ sendEvent(payload: RumEvent): void;
56
+ /**
57
+ * 修改运行时上下文
58
+ */
59
+ setContext(ctx: IContext): void;
60
+ /**
61
+ * 获取运行时上下文
62
+ */
63
+ getContext(): IContext;
64
+ /**
65
+ * 装载 client 需要的 Collector
66
+ */
67
+ useCollectors(collectors: ICollector[]): void;
68
+ /**
69
+ * 装载 client 需要的 processor
70
+ */
71
+ useProcessors(processors: IProcessor[]): void;
72
+ /**
73
+ * 装载 client 需要的 report
74
+ */
75
+ useReporter(reporter: IReporter): void;
76
+ }
77
+ export interface IConfiguration {
78
+ /**
79
+ * 项目 id
80
+ */
81
+ pid: string;
82
+ /**
83
+ * 应用环境
84
+ */
85
+ env?: 'prod' | 'gray' | 'pre' | 'daily' | 'local';
86
+ /**
87
+ * 应用版本
88
+ */
89
+ version?: string;
90
+ /**
91
+ * 用户配置
92
+ */
93
+ user?: {
94
+ id?: string;
95
+ tags?: string;
96
+ name?: string;
97
+ [key: string]: any;
98
+ };
99
+ /**
100
+ * 上报地址
101
+ */
102
+ endpoint: string;
103
+ /**
104
+ * reporter 发送事件之前
105
+ */
106
+ beforeReport?(bundle: RumEventBundle): any;
107
+ /**
108
+ * reporter 发送节奏配置
109
+ */
110
+ reportConfig?: {
111
+ flushTime?: number;
112
+ maxEventCount?: number;
113
+ };
114
+ /**
115
+ * session config
116
+ */
117
+ sessionConfig?: SessionConfig;
118
+ /**
119
+ * 各个 collector config
120
+ */
121
+ collectors?: {
122
+ [key: string]: unknown;
123
+ };
124
+ /**
125
+ * 事件过滤器配置
126
+ */
127
+ filters?: {
128
+ view?: MatchOption | MatchOption[];
129
+ resource?: MatchOption | MatchOption[];
130
+ exception?: MatchOption | MatchOption[];
131
+ };
132
+ [key: string]: any;
133
+ }
@@ -0,0 +1,7 @@
1
+ import { IContext } from './client';
2
+ import { RumEvent } from './rum-event';
3
+ export interface ICollector {
4
+ name: string;
5
+ setup(ctx: IContext, sendEvent: (payload: RumEvent) => void): void;
6
+ destroy?(): void;
7
+ }
@@ -0,0 +1,8 @@
1
+ import { IContext } from './client';
2
+ import { RumEvent } from './rum-event';
3
+ export interface IProcessor {
4
+ name: string;
5
+ setup?(ctx: IContext): void;
6
+ process(ctx: IContext): RumEvent;
7
+ match?(ctx: IContext): boolean;
8
+ }
@@ -0,0 +1,8 @@
1
+ import { IContext } from './client';
2
+ import { RumEvent, IViewData } from './rum-event';
3
+ export interface IReporter {
4
+ name: string;
5
+ init(ctx: IContext): void;
6
+ request(ctx: IContext, events: RumEvent[], view: IViewData): void;
7
+ report(ctx: IContext): void;
8
+ }
@@ -0,0 +1,164 @@
1
+ export declare enum RumEventType {
2
+ VIEW = "view",
3
+ RESOURCE = "resource",
4
+ EXCEPTION = "exception",
5
+ LONG_TASK = "longtask",
6
+ ACTION = "action",
7
+ CUSTOM = "custom"
8
+ }
9
+ export interface BaseObject {
10
+ [key: string]: BaseObjectValue;
11
+ }
12
+ export interface IViewData {
13
+ id: string;
14
+ name: string;
15
+ }
16
+ export type BaseObjectPrimitiveValue = string | number | boolean | null | undefined;
17
+ export type BaseObjectValue = BaseObjectPrimitiveValue | BaseObject | BaseObjectPrimitiveValue[] | IViewData;
18
+ export interface RumBaseEvent {
19
+ event_type?: RumEventType;
20
+ event_id?: string;
21
+ session_id?: string;
22
+ timestamp?: number;
23
+ times?: number;
24
+ view?: IViewData;
25
+ snapshots?: string;
26
+ [key: string]: BaseObjectValue;
27
+ }
28
+ export interface RumViewEvent extends RumBaseEvent {
29
+ loading_type?: 'initial_load' | 'route_change';
30
+ type: 'pv' | 'perf' | 'webvitals';
31
+ largest_contentful_paint?: number;
32
+ first_input_delay?: number;
33
+ cumulative_layout_shift?: number;
34
+ first_input_time?: number;
35
+ loading_time?: number;
36
+ first_paint?: number;
37
+ first_contentful_paint?: number;
38
+ dom_interactive?: number;
39
+ dom_content_loaded?: number;
40
+ dom_complete?: number;
41
+ load_event?: number;
42
+ referrer?: string;
43
+ url?: string;
44
+ timing_data?: string;
45
+ }
46
+ /**
47
+ * 枚举值,表示资源是否加载成功
48
+ */
49
+ export declare enum ResourceStatus {
50
+ Unknown = -1,
51
+ Failed = 0,
52
+ Success = 1
53
+ }
54
+ export interface RumResourceEvent extends RumBaseEvent {
55
+ name?: string;
56
+ type?: string;
57
+ method?: string;
58
+ status_code?: number | string;
59
+ message?: string;
60
+ success?: -1 | 0 | 1 | ResourceStatus;
61
+ trace_id?: string;
62
+ duration?: number;
63
+ size?: number;
64
+ connect_duration?: number;
65
+ ssl_duration?: number;
66
+ dns_duration?: number;
67
+ redirect_duration?: number;
68
+ first_byte_duration?: number;
69
+ download_duration?: number;
70
+ url?: string;
71
+ timing_data?: string;
72
+ tracing_data?: string;
73
+ }
74
+ export interface RumExceptionEvent extends RumBaseEvent {
75
+ source?: string;
76
+ type?: 'crash' | 'custom' | 'error' | 'blank';
77
+ name?: string;
78
+ message?: string;
79
+ file?: string;
80
+ stack?: string;
81
+ line?: number;
82
+ column?: number;
83
+ }
84
+ export interface RumLongTaskEvent extends RumBaseEvent {
85
+ view_name: string;
86
+ source?: string;
87
+ type?: string;
88
+ message?: string;
89
+ count: number;
90
+ stack?: string;
91
+ caused_by?: string;
92
+ }
93
+ export interface RumActionEvent extends RumBaseEvent {
94
+ type?: string;
95
+ name?: string;
96
+ target_name?: string;
97
+ duration?: number;
98
+ }
99
+ export interface RumCustomEvent extends RumBaseEvent {
100
+ type: string;
101
+ name: string;
102
+ group?: string;
103
+ value: number;
104
+ }
105
+ export type RumEvent = RumViewEvent | RumResourceEvent | RumExceptionEvent | RumActionEvent | RumCustomEvent;
106
+ export declare enum AppType {
107
+ browser = "browser",
108
+ miniapp = "miniapp",
109
+ uniapp = "uniapp"
110
+ }
111
+ export interface RumEventBundle {
112
+ app: {
113
+ id: string;
114
+ type: AppType;
115
+ name?: string;
116
+ version?: string;
117
+ channel?: string;
118
+ env?: string;
119
+ package?: string;
120
+ };
121
+ user: {
122
+ id: string;
123
+ name?: string;
124
+ tags?: string;
125
+ };
126
+ session: {
127
+ id: string;
128
+ };
129
+ view: {
130
+ id: string;
131
+ name: string;
132
+ };
133
+ device?: {
134
+ id?: string;
135
+ type?: string;
136
+ brand?: string;
137
+ model?: string;
138
+ name?: string;
139
+ };
140
+ os?: {
141
+ type?: string;
142
+ version?: string;
143
+ container?: string;
144
+ container_version?: string;
145
+ };
146
+ geo?: {
147
+ country?: string;
148
+ country_id?: string;
149
+ province?: string;
150
+ province_id?: string;
151
+ city?: string;
152
+ city_id?: string;
153
+ };
154
+ isp?: {
155
+ id?: string;
156
+ name?: string;
157
+ };
158
+ net?: {
159
+ model?: string;
160
+ name?: string;
161
+ };
162
+ events: Array<RumEvent>;
163
+ _v: string;
164
+ }
@@ -0,0 +1,28 @@
1
+ import { IConfiguration } from "./client";
2
+ import { RumEvent } from "./rum-event";
3
+ /**
4
+ * 每个 sdk shell 导出固定需要实现的基础 api
5
+ * 对外导出 shell 层, 所有 shell 层模型的 API 设计约定:
6
+ * 1. API 命名空间按照 variables / functions / events 来组织
7
+ * 2. 事件(events)的命名格式为:on[Will|Did]VerbNoun?,参考 https://code.visualstudio.com/api/references/vscode-api#events
8
+ * 3. 基于 Disposable 模式,对于事件的绑定、快捷键的绑定函数,返回值则是解绑函数
9
+ */
10
+ export interface IShell {
11
+ /**
12
+ * 初始化
13
+ */
14
+ init(configuration: IConfiguration): void;
15
+ /**
16
+ * 自定义上传数据
17
+ */
18
+ sendEvent(payload: RumEvent): void;
19
+ /**
20
+ * get config
21
+ */
22
+ getConfig(): IConfiguration;
23
+ /**
24
+ * set config
25
+ */
26
+ setConfig<T extends keyof IConfiguration>(key: T, value: IConfiguration[T]): void;
27
+ setConfig(value: IConfiguration): void;
28
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * 劫持函数
3
+ */
4
+ export declare function interceptFunction(target: any, name: string, callback: Function): void;
5
+ /**
6
+ * @description: GUID v4
7
+ * @return {String}
8
+ */
9
+ export declare function generateGUID(): string;
10
+ /**
11
+ * @description: 随机生成 traceId 或 sessionId
12
+ * @return {String}
13
+ */
14
+ export declare function generateTraceId(): string;
15
+ /**
16
+ * @description: 随机生成 spanId
17
+ * @param len {number}
18
+ * @param radix {number}
19
+ * @return {String}
20
+ */
21
+ export declare function generateSpanId(len?: number, radix?: number): string;
22
+ /**
23
+ * @description: 随机生成 eventId
24
+ * @param sessionId {string} sessionId
25
+ * @return {String}
26
+ */
27
+ export declare function generateEventId(sessionId: string): string;
28
+ /**
29
+ * @desc 函数防抖
30
+ * @param func 回调函数
31
+ * @param wait 延迟执行毫秒数
32
+ */
33
+ export declare function debounce(func: Function, wait: number): () => void;
34
+ /**
35
+ * @desc 函数延迟执行
36
+ * @param func 回调函数
37
+ * @param wait 延迟执行毫秒数
38
+ */
39
+ export declare function delay(func: Function, wait: number, ...args: any[]): number;
package/lib/utils/base.js CHANGED
@@ -14,15 +14,27 @@ var _is = require("./is");
14
14
  */
15
15
  function interceptFunction(target, name, callback) {
16
16
  var registeredMethod = target[name];
17
- target[name] = function () {
18
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
19
- args[_key] = arguments[_key];
20
- }
21
- callback.apply(this, args);
22
- if (registeredMethod) {
23
- return registeredMethod.apply(this, args);
24
- }
25
- };
17
+ try {
18
+ Object.defineProperty(target, name, {
19
+ configurable: true,
20
+ writable: true
21
+ });
22
+ } catch (e) {
23
+ //
24
+ }
25
+ try {
26
+ target[name] = function () {
27
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
28
+ args[_key] = arguments[_key];
29
+ }
30
+ callback.apply(this, args);
31
+ if (registeredMethod) {
32
+ return registeredMethod.apply(this, args);
33
+ }
34
+ };
35
+ } catch (e) {
36
+ //
37
+ }
26
38
  }
27
39
 
28
40
  /**
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 获取错误对象唯一标识
3
+ */
4
+ export declare const getErrorID: (error: {
5
+ message?: string;
6
+ stack?: string;
7
+ }) => string;
@@ -0,0 +1,12 @@
1
+ export type IsFnHelper<T = unknown> = (value: unknown) => value is T;
2
+ export declare function isTypeof<T = unknown>(value: unknown, type: string): value is T;
3
+ export declare const isFunction: (func: any) => boolean;
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>;
11
+ export declare const isNull: IsFnHelper<null>;
12
+ export declare const isObject: IsFnHelper<object>;
package/lib/utils/is.js CHANGED
@@ -33,7 +33,7 @@ var isNumber = exports.isNumber = function isNumber(value) {
33
33
  return isTypeof(value, 'number') && !isNaN(value) || isTypeof(value, 'bigint');
34
34
  };
35
35
  var isNull = exports.isNull = function isNull(value) {
36
- return isTypeof(value, 'null');
36
+ return value === null;
37
37
  };
38
38
  var isObject = exports.isObject = function isObject(value) {
39
39
  return !isNull(value) && isTypeof(value, 'object');
@@ -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,17 @@
1
+ export declare const ONE_SECOND = 1000;
2
+ export declare const ONE_MINUTE: number;
3
+ export declare const ONE_HOUR: number;
4
+ export declare const ONE_DAY: number;
5
+ /**
6
+ * Return true if the draw is successful
7
+ * @param threshold between 0 and 100
8
+ */
9
+ export declare function performDraw(threshold: number): boolean;
10
+ /**
11
+ * 保留指定位数的小数
12
+ * @param num 原数据
13
+ * @param decimal 小数位数
14
+ * @param min 限制最小值,否则返回undefined
15
+ * @returns
16
+ */
17
+ export declare function formatNumber(num: number, decimal?: number, min?: number): number;
@@ -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,28 @@
1
+ import { MatchOption } from "./match";
2
+ export type PropagatorType = 'tracecontext' | 'b3' | 'b3multi' | 'jaeger' | 'sw8';
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
+ propagatorTypes?: PropagatorType[];
12
+ allowedUrls?: Array<MatchOption | TraceOption> | undefined;
13
+ tracestate?: boolean;
14
+ baggage?: boolean;
15
+ }
16
+ export interface ITracingHeaders {
17
+ [key: string]: string;
18
+ }
19
+ export interface TraceSubOption {
20
+ tracestate?: string;
21
+ baggage?: string;
22
+ appId?: string;
23
+ appVersion?: string;
24
+ viewName?: string;
25
+ host?: string;
26
+ }
27
+ export declare function makeTracingHeaders(traceId: string, spanId: string, sampled: boolean, propagatorTypes: PropagatorType[], subOption?: TraceSubOption): ITracingHeaders;
28
+ export declare function parseTracingOptions(tracingOption: boolean | ITracingOption): ITracingOption;
package/package.json CHANGED
@@ -1,18 +1,16 @@
1
1
  {
2
2
  "name": "@arms/rum-core",
3
- "version": "0.0.25-beta.16",
3
+ "version": "0.0.25-beta.18",
4
4
  "description": "arms rum javascript sdk core",
5
5
  "author": "guangli.fj <guangli.fj@alibaba-inc.com>",
6
6
  "license": "ISC",
7
7
  "main": "lib/index.js",
8
- "module": "es/index.js",
9
8
  "directories": {
10
9
  "lib": "lib",
11
10
  "test": "__tests__"
12
11
  },
13
12
  "files": [
14
- "lib",
15
- "es"
13
+ "lib"
16
14
  ],
17
15
  "publishConfig": {
18
16
  "access": "public"
@@ -20,6 +18,7 @@
20
18
  "scripts": {
21
19
  "start": "build-scripts build --watch",
22
20
  "build": "build-scripts build --skip-demo",
21
+ "prepublishOnly": "npm run build",
23
22
  "test": "node ./__tests__/@arms/core.test.js"
24
23
  },
25
24
  "dependencies": {