@arms/rum-core 0.0.25-beta.19 → 0.0.25-beta.21

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
@@ -14,4 +14,5 @@ export * from './utils/is';
14
14
  export * from './utils/number';
15
15
  export * from './utils/match';
16
16
  export * from './utils/trace';
17
+ export * from './utils/verify';
17
18
  export { Client, Context, Reporter, Shell };
package/lib/index.js CHANGED
@@ -99,4 +99,11 @@ Object.keys(_trace).forEach(function (key) {
99
99
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
100
100
  if (key in exports && exports[key] === _trace[key]) return;
101
101
  exports[key] = _trace[key];
102
+ });
103
+ var _verify = require("./utils/verify");
104
+ Object.keys(_verify).forEach(function (key) {
105
+ if (key === "default" || key === "__esModule") return;
106
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
107
+ if (key in exports && exports[key] === _verify[key]) return;
108
+ exports[key] = _verify[key];
102
109
  });
@@ -10,7 +10,7 @@ var Context = /*#__PURE__*/function () {
10
10
  this.session = void 0;
11
11
  if (rumSession) {
12
12
  this.session = rumSession;
13
- this.session.init(config);
13
+ this.session.init(this);
14
14
  }
15
15
  }
16
16
  var _proto = Context.prototype;
@@ -1,5 +1,5 @@
1
1
  import { IContext } from '../types/client';
2
- import { RumEvent, IViewData } from '../types/rum-event';
2
+ import { RumEvent, IViewData, RumEventBundle } from '../types/rum-event';
3
3
  export default abstract class Reporter {
4
4
  name: string;
5
5
  /**
@@ -19,10 +19,11 @@ export default abstract class Reporter {
19
19
  * eventQueue 提取最新 View 作为公共 View,event 中不同 View Event 保留 View id
20
20
  */
21
21
  protected flushEventQueue(): void;
22
+ mergeEvent(ctx: IContext, events: RumEvent[], view: IViewData): void;
22
23
  /**
23
24
  * 接口请求由各平台 sdk reporter 实现
24
25
  */
25
- abstract request(ctx: IContext, events: RumEvent[], view: IViewData): void;
26
+ abstract request(ctx: IContext, bundle: RumEventBundle): void;
26
27
  /**
27
28
  * 初始化时
28
29
  */
@@ -5,8 +5,10 @@ exports["default"] = void 0;
5
5
  var _rumEvent = require("../types/rum-event");
6
6
  var _exception = require("../utils/exception");
7
7
  var _is = require("../utils/is");
8
+ var _verify = require("../utils/verify");
8
9
  var FLUSH_TIME = 3000;
9
10
  var MAX_EVENT_COUNT = 20;
11
+ var attrs = ['app', 'user', 'device', 'os', 'geo', 'isp', 'net', 'attributes'];
10
12
  var Reporter = exports["default"] = /*#__PURE__*/function () {
11
13
  function Reporter() {
12
14
  this.name = 'reporter';
@@ -108,21 +110,77 @@ var Reporter = exports["default"] = /*#__PURE__*/function () {
108
110
  var curView = views[views.length - 1];
109
111
  views.forEach(function (view) {
110
112
  if (view.id === curView.id) {
111
- var _events = eventQueue.filter(function (event) {
113
+ var events = eventQueue.filter(function (event) {
112
114
  var _event$view;
113
115
  return ((_event$view = event.view) === null || _event$view === void 0 ? void 0 : _event$view.id) === view.id;
114
116
  });
115
- _events.forEach(function (event) {
117
+ events.forEach(function (event) {
116
118
  delete event.view;
117
119
  });
118
120
  }
119
121
  });
120
122
  var session = ctx.session;
121
123
  var sampled = session ? session.getSampled() : true;
122
- sampled && this.request(ctx, eventQueue, curView);
124
+ sampled && this.mergeEvent(ctx, eventQueue, curView);
123
125
  this.eventQueue = [];
126
+ };
127
+ _proto.mergeEvent = function mergeEvent(ctx, events, view) {
128
+ var _config$net;
129
+ var config = ctx.getConfig();
130
+ var session = ctx.session;
131
+ var sessionId = session.getSessionId();
132
+ events.forEach(function (event) {
133
+ if (event.session_id === sessionId) {
134
+ delete event.session_id;
135
+ }
136
+ });
137
+ var bundle = {
138
+ app: {
139
+ id: config.pid,
140
+ env: config.env || 'prod',
141
+ version: config.version,
142
+ type: ''
143
+ },
144
+ user: {
145
+ id: session.getUserId()
146
+ },
147
+ session: {
148
+ id: sessionId
149
+ },
150
+ net: {
151
+ model: (_config$net = config.net) === null || _config$net === void 0 ? void 0 : _config$net.model
152
+ },
153
+ view: view,
154
+ events: events
155
+ };
156
+ attrs.forEach(function (key) {
157
+ var obj = config[key];
158
+ (0, _is.isObject)(obj) && Object.keys(obj).forEach(function (k) {
159
+ var val = obj[k];
160
+ // user 的 id 不能被覆盖
161
+ if (key === 'user' && k === 'id') return;
162
+ if (key === 'attributes') {
163
+ bundle[key] = (0, _verify.verifyAttributes)(obj);
164
+ } else if ((0, _is.isString)(val) || (0, _is.isNumber)(val)) {
165
+ if (!(key in bundle)) {
166
+ bundle[key] = {};
167
+ }
168
+ bundle[key][k] = val;
169
+ }
170
+ });
171
+ });
172
+ if (typeof config.beforeReport === 'function') {
173
+ bundle = config.beforeReport(bundle);
174
+ if (!bundle) return;
175
+ }
176
+ this.request(ctx, bundle);
124
177
  }
125
178
 
179
+ // /**
180
+ // * 接口请求由各平台 sdk reporter 实现
181
+ // */
182
+ // abstract request(ctx: IContext, events: RumEvent[], view: IViewData): void;
183
+
126
184
  /**
127
185
  * 接口请求由各平台 sdk reporter 实现
128
186
  */;
@@ -36,7 +36,7 @@ export interface SessionConfig {
36
36
  storage?: string;
37
37
  }
38
38
  export interface IRumSession {
39
- init(config: IConfiguration): void;
39
+ init(ctx: IContext): void;
40
40
  sessionConfig: SessionConfig;
41
41
  getSessionId: () => string;
42
42
  getSampled: () => boolean;
@@ -44,6 +44,7 @@ export interface IRumSession {
44
44
  getEventId: () => string;
45
45
  getViewId: () => string;
46
46
  getUserId: () => string;
47
+ getBaseEvent: () => any;
47
48
  }
48
49
  export interface IClient {
49
50
  /**
@@ -1,8 +1,8 @@
1
1
  import { IContext } from './client';
2
- import { RumEvent, IViewData } from './rum-event';
2
+ import { RumEventBundle } from './rum-event';
3
3
  export interface IReporter {
4
4
  name: string;
5
5
  init(ctx: IContext): void;
6
- request(ctx: IContext, events: RumEvent[], view: IViewData): void;
6
+ request(ctx: IContext, bundle: RumEventBundle): void;
7
7
  report(ctx: IContext): void;
8
8
  }
@@ -16,7 +16,7 @@ export interface IViewData {
16
16
  }
17
17
  export type BaseObjectPrimitiveValue = string | number | boolean | null | undefined;
18
18
  export type BaseObjectValue = BaseObjectPrimitiveValue | BaseObject | BaseObjectPrimitiveValue[] | IViewData;
19
- export interface RumBaseEvent {
19
+ export interface RumBaseEvent extends BaseObject {
20
20
  event_type?: RumEventType;
21
21
  event_id?: string;
22
22
  session_id?: string;
@@ -24,7 +24,7 @@ export interface RumBaseEvent {
24
24
  times?: number;
25
25
  view?: IViewData;
26
26
  snapshots?: string;
27
- [key: string]: BaseObjectValue;
27
+ attributes?: BaseObject;
28
28
  }
29
29
  export interface RumViewEvent extends RumBaseEvent {
30
30
  loading_type?: 'initial_load' | 'route_change';
@@ -117,7 +117,7 @@ export declare enum AppType {
117
117
  export interface RumEventBundle {
118
118
  app: {
119
119
  id: string;
120
- type: AppType;
120
+ type: AppType | string;
121
121
  name?: string;
122
122
  version?: string;
123
123
  channel?: string;
@@ -166,5 +166,6 @@ export interface RumEventBundle {
166
166
  name?: string;
167
167
  };
168
168
  events: Array<RumEvent>;
169
- _v: string;
169
+ attributes?: BaseObject;
170
+ _v?: string;
170
171
  }
@@ -4,4 +4,4 @@ export declare function isMatchOption(item: unknown): item is MatchOption;
4
4
  * 如果值有一个选项匹配,则返回true。在比较字符串时,useStartsWith: true时将把值与选项的开始进行比较,而不是要求精确匹配。
5
5
  */
6
6
  export declare function matchList(list: MatchOption[], value: string, useStartsWith?: boolean): boolean;
7
- export declare function urlMatch(url: string, list?: MatchOption[]): boolean;
7
+ export declare function urlMatch(url: string, list?: MatchOption | MatchOption[]): boolean;
@@ -35,7 +35,10 @@ function urlMatch(url, list) {
35
35
  list = [];
36
36
  }
37
37
  try {
38
- var rules = [/https?:\/\/.*rum|retcode\.aliyuncs\.com/, /https?:\/\/.*log-global\.aliyuncs\.com/, /https?:\/\/.*\.mmstat\.com/].concat(list);
38
+ if (!(0, _is.isArray)(list)) {
39
+ list = [list];
40
+ }
41
+ var rules = [/https?:\/\/.*rum|retcode\.aliyuncs\.com/, /https?:\/\/.*log-global\.aliyuncs\.com/, /https?:\/\/.*\.mmstat\.com/, /data:(.+?)(;base64)?,(.+)$/].concat(list);
39
42
  return matchList(rules, url);
40
43
  } catch (e) {
41
44
  return false;
@@ -0,0 +1,2 @@
1
+ import { BaseObject } from "../types/rum-event";
2
+ export declare function verifyAttributes(attrs: BaseObject): BaseObject;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.verifyAttributes = verifyAttributes;
5
+ var _is = require("./is");
6
+ function verifyAttributes(attrs) {
7
+ if (!(0, _is.isObject)(attrs)) {
8
+ return;
9
+ }
10
+ var copyAttrs = Object.assign({}, attrs);
11
+ Object.keys(copyAttrs).forEach(function (key) {
12
+ var val = copyAttrs[key];
13
+ // 验证 key 长度
14
+ if (key.length > 50) {
15
+ var shortKey = key.substring(0, 50);
16
+ copyAttrs[shortKey] = val;
17
+ delete copyAttrs[key];
18
+ key = shortKey;
19
+ }
20
+ // 验证 value 类型和长度
21
+ if (!(0, _is.isString)(val) && !(0, _is.isNumber)(val)) {
22
+ delete copyAttrs[key];
23
+ } else if ((0, _is.isString)(val) && val.length > 2e3) {
24
+ copyAttrs[key] = val.substring(0, 2e3);
25
+ }
26
+ });
27
+ var keys = Object.keys(copyAttrs);
28
+ if (!keys.length) {
29
+ return;
30
+ }
31
+ if (keys.length > 20) {
32
+ keys.forEach(function (key, index) {
33
+ if (index > 19) {
34
+ delete copyAttrs[key];
35
+ }
36
+ });
37
+ }
38
+ return copyAttrs;
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arms/rum-core",
3
- "version": "0.0.25-beta.19",
3
+ "version": "0.0.25-beta.21",
4
4
  "description": "arms rum javascript sdk core",
5
5
  "author": "guangli.fj <guangli.fj@alibaba-inc.com>",
6
6
  "license": "ISC",