@openreplay/tracker 8.0.0 → 8.0.1-beta14
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/cjs/app/index.d.ts +4 -1
- package/cjs/app/index.js +18 -2
- package/cjs/app/session.d.ts +10 -0
- package/cjs/app/session.js +3 -0
- package/cjs/index.d.ts +11 -0
- package/cjs/index.js +28 -1
- package/cjs/modules/Network/fetchProxy.d.ts +34 -0
- package/cjs/modules/Network/fetchProxy.js +240 -0
- package/cjs/modules/Network/index.d.ts +3 -0
- package/cjs/modules/Network/index.js +9 -0
- package/cjs/modules/Network/networkMessage.d.ts +49 -0
- package/cjs/modules/Network/networkMessage.js +82 -0
- package/cjs/modules/Network/types.d.ts +13 -0
- package/cjs/modules/Network/types.js +3 -0
- package/cjs/modules/Network/utils.d.ts +11 -0
- package/cjs/modules/Network/utils.js +213 -0
- package/cjs/modules/Network/xhrProxy.d.ts +47 -0
- package/cjs/modules/Network/xhrProxy.js +209 -0
- package/cjs/modules/console.js +21 -13
- package/cjs/modules/featureFlags.d.ts +25 -0
- package/cjs/modules/featureFlags.js +100 -0
- package/cjs/modules/network.d.ts +3 -4
- package/cjs/modules/network.js +13 -3
- package/coverage/clover.xml +898 -449
- package/coverage/coverage-final.json +13 -8
- package/coverage/lcov-report/index.html +50 -35
- package/coverage/lcov-report/main/app/guards.ts.html +1 -1
- package/coverage/lcov-report/main/app/index.html +21 -21
- package/coverage/lcov-report/main/app/index.ts.html +46 -4
- package/coverage/lcov-report/main/app/logger.ts.html +1 -1
- package/coverage/lcov-report/main/app/messages.gen.ts.html +146 -146
- package/coverage/lcov-report/main/app/observer/iframe_observer.ts.html +1 -1
- package/coverage/lcov-report/main/app/observer/iframe_offsets.ts.html +1 -1
- package/coverage/lcov-report/main/app/observer/index.html +1 -1
- package/coverage/lcov-report/main/app/observer/shadow_root_observer.ts.html +1 -1
- package/coverage/lcov-report/main/app/observer/top_observer.ts.html +1 -1
- package/coverage/lcov-report/main/app/sanitizer.ts.html +1 -1
- package/coverage/lcov-report/main/app/session.ts.html +47 -5
- package/coverage/lcov-report/main/app/ticker.ts.html +1 -1
- package/coverage/lcov-report/main/index.html +20 -20
- package/coverage/lcov-report/main/index.ts.html +38 -26
- package/coverage/lcov-report/main/modules/Network/fetchProxy.ts.html +949 -0
- package/coverage/lcov-report/main/modules/Network/index.html +176 -0
- package/coverage/lcov-report/main/modules/Network/index.ts.html +169 -0
- package/coverage/lcov-report/main/modules/Network/networkMessage.ts.html +382 -0
- package/coverage/lcov-report/main/modules/Network/utils.ts.html +700 -0
- package/coverage/lcov-report/main/modules/Network/xhrProxy.ts.html +823 -0
- package/coverage/lcov-report/main/modules/attributeSender.ts.html +1 -1
- package/coverage/lcov-report/main/modules/axiosSpy.ts.html +1 -1
- package/coverage/lcov-report/main/modules/connection.ts.html +1 -1
- package/coverage/lcov-report/main/modules/console.ts.html +174 -147
- package/coverage/lcov-report/main/modules/constructedStyleSheets.ts.html +1 -1
- package/coverage/lcov-report/main/modules/cssrules.ts.html +1 -1
- package/coverage/lcov-report/main/modules/exception.ts.html +1 -1
- package/coverage/lcov-report/main/modules/featureFlags.ts.html +102 -51
- package/coverage/lcov-report/main/modules/focus.ts.html +1 -1
- package/coverage/lcov-report/main/modules/fonts.ts.html +1 -1
- package/coverage/lcov-report/main/modules/img.ts.html +1 -1
- package/coverage/lcov-report/main/modules/index.html +33 -33
- package/coverage/lcov-report/main/modules/input.ts.html +1 -1
- package/coverage/lcov-report/main/modules/mouse.ts.html +1 -1
- package/coverage/lcov-report/main/modules/network.ts.html +70 -70
- package/coverage/lcov-report/main/modules/performance.ts.html +1 -1
- package/coverage/lcov-report/main/modules/scroll.ts.html +1 -1
- package/coverage/lcov-report/main/modules/selection.ts.html +1 -1
- package/coverage/lcov-report/main/modules/tabs.ts.html +1 -1
- package/coverage/lcov-report/main/modules/timing.ts.html +1 -1
- package/coverage/lcov-report/main/modules/viewport.ts.html +1 -1
- package/coverage/lcov-report/main/utils.ts.html +45 -45
- package/coverage/lcov-report/webworker/BatchWriter.ts.html +1 -1
- package/coverage/lcov-report/webworker/MessageEncoder.gen.ts.html +1 -1
- package/coverage/lcov-report/webworker/PrimitiveEncoder.ts.html +1 -1
- package/coverage/lcov-report/webworker/QueueSender.ts.html +1 -1
- package/coverage/lcov-report/webworker/index.html +1 -1
- package/coverage/lcov-report/webworker/index.ts.html +1 -1
- package/coverage/lcov.info +1558 -726
- package/lib/app/index.d.ts +4 -1
- package/lib/app/index.js +18 -2
- package/lib/app/session.d.ts +10 -0
- package/lib/app/session.js +3 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +28 -1
- package/lib/modules/Network/fetchProxy.d.ts +34 -0
- package/lib/modules/Network/fetchProxy.js +234 -0
- package/lib/modules/Network/index.d.ts +3 -0
- package/lib/modules/Network/index.js +6 -0
- package/lib/modules/Network/networkMessage.d.ts +49 -0
- package/lib/modules/Network/networkMessage.js +78 -0
- package/lib/modules/Network/types.d.ts +13 -0
- package/lib/modules/Network/types.js +2 -0
- package/lib/modules/Network/utils.d.ts +11 -0
- package/lib/modules/Network/utils.js +201 -0
- package/lib/modules/Network/xhrProxy.d.ts +47 -0
- package/lib/modules/Network/xhrProxy.js +204 -0
- package/lib/modules/console.js +21 -13
- package/lib/modules/featureFlags.d.ts +25 -0
- package/lib/modules/featureFlags.js +97 -0
- package/lib/modules/network.d.ts +3 -4
- package/lib/modules/network.js +13 -3
- package/package.json +1 -1
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
export const genResponseByType = (responseType, response) => {
|
|
2
|
+
let result = '';
|
|
3
|
+
switch (responseType) {
|
|
4
|
+
case '':
|
|
5
|
+
case 'text':
|
|
6
|
+
case 'json':
|
|
7
|
+
if (typeof response == 'string') {
|
|
8
|
+
try {
|
|
9
|
+
result = JSON.parse(response);
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
// not a JSON string
|
|
13
|
+
result = response.slice(0, 10000);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else if (isPureObject(response) || Array.isArray(response)) {
|
|
17
|
+
result = JSON.stringify(response);
|
|
18
|
+
}
|
|
19
|
+
else if (typeof response !== 'undefined') {
|
|
20
|
+
result = Object.prototype.toString.call(response);
|
|
21
|
+
}
|
|
22
|
+
break;
|
|
23
|
+
case 'blob':
|
|
24
|
+
case 'document':
|
|
25
|
+
case 'arraybuffer':
|
|
26
|
+
default:
|
|
27
|
+
if (typeof response !== 'undefined') {
|
|
28
|
+
result = Object.prototype.toString.call(response);
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
export const getStringResponseByType = (responseType, response) => {
|
|
35
|
+
let result = '';
|
|
36
|
+
switch (responseType) {
|
|
37
|
+
case '':
|
|
38
|
+
case 'text':
|
|
39
|
+
case 'json':
|
|
40
|
+
if (typeof response == 'string') {
|
|
41
|
+
result = response;
|
|
42
|
+
}
|
|
43
|
+
else if (isPureObject(response) || Array.isArray(response)) {
|
|
44
|
+
result = JSON.stringify(response);
|
|
45
|
+
}
|
|
46
|
+
else if (typeof response !== 'undefined') {
|
|
47
|
+
result = Object.prototype.toString.call(response);
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
case 'blob':
|
|
51
|
+
case 'document':
|
|
52
|
+
case 'arraybuffer':
|
|
53
|
+
default:
|
|
54
|
+
if (typeof response !== 'undefined') {
|
|
55
|
+
result = Object.prototype.toString.call(response);
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
};
|
|
61
|
+
export const genStringBody = (body) => {
|
|
62
|
+
if (!body) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
let result;
|
|
66
|
+
if (typeof body === 'string') {
|
|
67
|
+
if (body[0] === '{' || body[0] === '[') {
|
|
68
|
+
result = body;
|
|
69
|
+
}
|
|
70
|
+
// 'a=1&b=2' => try to parse as query
|
|
71
|
+
const arr = body.split('&');
|
|
72
|
+
if (arr.length === 1) {
|
|
73
|
+
// not a query, parse as original string
|
|
74
|
+
result = body;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// 'a=1&b=2&c' => parse as query
|
|
78
|
+
result = arr.join(',');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (isIterable(body)) {
|
|
82
|
+
// FormData or URLSearchParams or Array
|
|
83
|
+
const arr = [];
|
|
84
|
+
for (const [key, value] of body) {
|
|
85
|
+
arr.push(`${key}=${typeof value === 'string' ? value : '[object Object]'}`);
|
|
86
|
+
}
|
|
87
|
+
result = arr.join(',');
|
|
88
|
+
}
|
|
89
|
+
else if (body instanceof Blob ||
|
|
90
|
+
body instanceof ReadableStream ||
|
|
91
|
+
body instanceof ArrayBuffer) {
|
|
92
|
+
result = 'byte data';
|
|
93
|
+
}
|
|
94
|
+
else if (isPureObject(body)) {
|
|
95
|
+
// overriding ArrayBufferView which is not convertable to string
|
|
96
|
+
result = body;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
result = `can't parse body ${typeof body}`;
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
};
|
|
103
|
+
export const genGetDataByUrl = (url, getData = {}) => {
|
|
104
|
+
if (!isPureObject(getData)) {
|
|
105
|
+
getData = {};
|
|
106
|
+
}
|
|
107
|
+
let query = url ? url.split('?') : []; // a.php?b=c&d=?e => ['a.php', 'b=c&d=', 'e']
|
|
108
|
+
query.shift(); // => ['b=c&d=', 'e']
|
|
109
|
+
if (query.length > 0) {
|
|
110
|
+
query = query.join('?').split('&'); // => 'b=c&d=?e' => ['b=c', 'd=?e']
|
|
111
|
+
for (const q of query) {
|
|
112
|
+
const kv = q.split('=');
|
|
113
|
+
try {
|
|
114
|
+
getData[kv[0]] = decodeURIComponent(kv[1]);
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
// "URIError: URI malformed" will be thrown when `kv[1]` contains "%", so just use raw data
|
|
118
|
+
// @issue #470
|
|
119
|
+
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_URI
|
|
120
|
+
getData[kv[0]] = kv[1];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return getData;
|
|
125
|
+
};
|
|
126
|
+
export const genFormattedBody = (body) => {
|
|
127
|
+
if (!body) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
let result;
|
|
131
|
+
if (typeof body === 'string') {
|
|
132
|
+
try {
|
|
133
|
+
// '{a:1}' =>
|
|
134
|
+
result = JSON.parse(body);
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
// 'a=1&b=2' => try to parse as query
|
|
138
|
+
const arr = body.split('&');
|
|
139
|
+
result = {};
|
|
140
|
+
// eslint-disable-next-line
|
|
141
|
+
for (let q of arr) {
|
|
142
|
+
const kv = q.split('=');
|
|
143
|
+
result[kv[0]] = kv[1] === undefined ? 'undefined' : kv[1];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (isIterable(body)) {
|
|
148
|
+
// FormData or URLSearchParams or Array
|
|
149
|
+
result = {};
|
|
150
|
+
for (const [key, value] of body) {
|
|
151
|
+
result[key] = typeof value === 'string' ? value : '[object Object]';
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (body instanceof Blob ||
|
|
155
|
+
body instanceof ReadableStream ||
|
|
156
|
+
body instanceof ArrayBuffer) {
|
|
157
|
+
result = 'byte data';
|
|
158
|
+
}
|
|
159
|
+
else if (isPureObject(body)) {
|
|
160
|
+
// overriding ArrayBufferView which is not convertable to string
|
|
161
|
+
result = body;
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
result = `can't parse body ${typeof body}`;
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
};
|
|
168
|
+
export function isPureObject(input) {
|
|
169
|
+
return null !== input && typeof input === 'object';
|
|
170
|
+
}
|
|
171
|
+
export function isIterable(value) {
|
|
172
|
+
if (value === null || value === undefined) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
return typeof Symbol !== 'undefined' && typeof value[Symbol.iterator] === 'function';
|
|
176
|
+
}
|
|
177
|
+
export function formatByteSize(bytes) {
|
|
178
|
+
if (bytes <= 0) {
|
|
179
|
+
// shouldn't happen?
|
|
180
|
+
return '';
|
|
181
|
+
}
|
|
182
|
+
if (bytes >= 1000 * 1000) {
|
|
183
|
+
return (bytes / 1000 / 1000).toFixed(1) + ' MB';
|
|
184
|
+
}
|
|
185
|
+
if (bytes >= 1000) {
|
|
186
|
+
return (bytes / 1000).toFixed(1) + ' KB';
|
|
187
|
+
}
|
|
188
|
+
return `${bytes}B`;
|
|
189
|
+
}
|
|
190
|
+
export const getURL = (urlString) => {
|
|
191
|
+
if (urlString.startsWith('//')) {
|
|
192
|
+
const baseUrl = new URL(window.location.href);
|
|
193
|
+
urlString = `${baseUrl.protocol}${urlString}`;
|
|
194
|
+
}
|
|
195
|
+
if (urlString.startsWith('http')) {
|
|
196
|
+
return new URL(urlString);
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
return new URL(urlString, window.location.href);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* I took inspiration in few stack exchange posts
|
|
3
|
+
* and Tencent vConsole library (MIT)
|
|
4
|
+
* by wrapping the XMLHttpRequest object in a Proxy
|
|
5
|
+
* we can intercept the network requests
|
|
6
|
+
* in not-so-hacky way
|
|
7
|
+
* */
|
|
8
|
+
import NetworkMessage from './networkMessage.js';
|
|
9
|
+
import { RequestResponseData } from './types.js';
|
|
10
|
+
import { NetworkRequest } from '../../common/messages.gen.js';
|
|
11
|
+
export declare class XHRProxyHandler<T extends XMLHttpRequest> implements ProxyHandler<T> {
|
|
12
|
+
private readonly ignoredHeaders;
|
|
13
|
+
private readonly setSessionTokenHeader;
|
|
14
|
+
private readonly sanitize;
|
|
15
|
+
private readonly sendMessage;
|
|
16
|
+
private readonly isServiceUrl;
|
|
17
|
+
XMLReq: XMLHttpRequest;
|
|
18
|
+
item: NetworkMessage;
|
|
19
|
+
constructor(XMLReq: XMLHttpRequest, ignoredHeaders: boolean | string[], setSessionTokenHeader: (cb: (name: string, value: string) => void) => void, sanitize: (data: RequestResponseData) => RequestResponseData, sendMessage: (message: NetworkRequest) => void, isServiceUrl: (url: string) => boolean);
|
|
20
|
+
get(target: T, key: string): any;
|
|
21
|
+
set(target: T, key: string, value: (args: any[]) => any): boolean;
|
|
22
|
+
onReadyStateChange(): void;
|
|
23
|
+
onAbort(): void;
|
|
24
|
+
onTimeout(): void;
|
|
25
|
+
protected getOpen(target: T): (...args: any[]) => any;
|
|
26
|
+
protected getSend(target: T): (...args: any[]) => any;
|
|
27
|
+
protected getSetRequestHeader(target: T): (...args: any[]) => any;
|
|
28
|
+
protected setOnReadyStateChange(target: T, key: string, orscFunction: (args: any[]) => any): boolean;
|
|
29
|
+
protected setOnAbort(target: T, key: string, oaFunction: (args: any[]) => any): boolean;
|
|
30
|
+
protected setOnTimeout(target: T, key: string, otFunction: (args: any[]) => any): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Update item's properties according to readyState.
|
|
33
|
+
*/
|
|
34
|
+
protected updateItemByReadyState(): void;
|
|
35
|
+
}
|
|
36
|
+
export default class XHRProxy {
|
|
37
|
+
static origXMLHttpRequest: {
|
|
38
|
+
new (): XMLHttpRequest;
|
|
39
|
+
prototype: XMLHttpRequest;
|
|
40
|
+
readonly DONE: number;
|
|
41
|
+
readonly HEADERS_RECEIVED: number;
|
|
42
|
+
readonly LOADING: number;
|
|
43
|
+
readonly OPENED: number;
|
|
44
|
+
readonly UNSENT: number;
|
|
45
|
+
};
|
|
46
|
+
static create(ignoredHeaders: boolean | string[], setSessionTokenHeader: (cb: (name: string, value: string) => void) => void, sanitize: (data: RequestResponseData) => RequestResponseData, sendMessage: (data: NetworkRequest) => void, isServiceUrl: (url: string) => boolean): any;
|
|
47
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* I took inspiration in few stack exchange posts
|
|
3
|
+
* and Tencent vConsole library (MIT)
|
|
4
|
+
* by wrapping the XMLHttpRequest object in a Proxy
|
|
5
|
+
* we can intercept the network requests
|
|
6
|
+
* in not-so-hacky way
|
|
7
|
+
* */
|
|
8
|
+
import NetworkMessage, { RequestState } from './networkMessage.js';
|
|
9
|
+
import { genGetDataByUrl, formatByteSize, genStringBody, getStringResponseByType } from './utils.js';
|
|
10
|
+
export class XHRProxyHandler {
|
|
11
|
+
constructor(XMLReq, ignoredHeaders, setSessionTokenHeader, sanitize, sendMessage, isServiceUrl) {
|
|
12
|
+
this.ignoredHeaders = ignoredHeaders;
|
|
13
|
+
this.setSessionTokenHeader = setSessionTokenHeader;
|
|
14
|
+
this.sanitize = sanitize;
|
|
15
|
+
this.sendMessage = sendMessage;
|
|
16
|
+
this.isServiceUrl = isServiceUrl;
|
|
17
|
+
this.XMLReq = XMLReq;
|
|
18
|
+
this.XMLReq.onreadystatechange = () => {
|
|
19
|
+
this.onReadyStateChange();
|
|
20
|
+
};
|
|
21
|
+
this.XMLReq.onabort = () => {
|
|
22
|
+
this.onAbort();
|
|
23
|
+
};
|
|
24
|
+
this.XMLReq.ontimeout = () => {
|
|
25
|
+
this.onTimeout();
|
|
26
|
+
};
|
|
27
|
+
this.item = new NetworkMessage(ignoredHeaders, setSessionTokenHeader, sanitize);
|
|
28
|
+
this.item.requestType = 'xhr';
|
|
29
|
+
}
|
|
30
|
+
get(target, key) {
|
|
31
|
+
switch (key) {
|
|
32
|
+
case 'open':
|
|
33
|
+
return this.getOpen(target);
|
|
34
|
+
case 'send':
|
|
35
|
+
return this.getSend(target);
|
|
36
|
+
case 'setRequestHeader':
|
|
37
|
+
return this.getSetRequestHeader(target);
|
|
38
|
+
default:
|
|
39
|
+
// eslint-disable-next-line no-case-declarations
|
|
40
|
+
const value = Reflect.get(target, key);
|
|
41
|
+
if (typeof value === 'function') {
|
|
42
|
+
return value.bind(target);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
set(target, key, value) {
|
|
50
|
+
switch (key) {
|
|
51
|
+
case 'onreadystatechange':
|
|
52
|
+
return this.setOnReadyStateChange(target, key, value);
|
|
53
|
+
case 'onabort':
|
|
54
|
+
return this.setOnAbort(target, key, value);
|
|
55
|
+
case 'ontimeout':
|
|
56
|
+
return this.setOnTimeout(target, key, value);
|
|
57
|
+
default:
|
|
58
|
+
// not tracked methods
|
|
59
|
+
}
|
|
60
|
+
return Reflect.set(target, key, value);
|
|
61
|
+
}
|
|
62
|
+
onReadyStateChange() {
|
|
63
|
+
if (this.item.url && this.isServiceUrl(this.item.url))
|
|
64
|
+
return;
|
|
65
|
+
this.item.readyState = this.XMLReq.readyState;
|
|
66
|
+
this.item.responseType = this.XMLReq.responseType;
|
|
67
|
+
this.item.endTime = performance.now();
|
|
68
|
+
this.item.duration = this.item.endTime - this.item.startTime;
|
|
69
|
+
this.updateItemByReadyState();
|
|
70
|
+
setTimeout(() => {
|
|
71
|
+
this.item.response = getStringResponseByType(this.item.responseType, this.item.response);
|
|
72
|
+
}, 0);
|
|
73
|
+
if (this.XMLReq.readyState === RequestState.DONE) {
|
|
74
|
+
this.sendMessage(this.item.getMessage());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
onAbort() {
|
|
78
|
+
this.item.cancelState = 1;
|
|
79
|
+
this.item.statusText = 'Abort';
|
|
80
|
+
this.sendMessage(this.item.getMessage());
|
|
81
|
+
}
|
|
82
|
+
onTimeout() {
|
|
83
|
+
this.item.cancelState = 3;
|
|
84
|
+
this.item.statusText = 'Timeout';
|
|
85
|
+
this.sendMessage(this.item.getMessage());
|
|
86
|
+
}
|
|
87
|
+
getOpen(target) {
|
|
88
|
+
const targetFunction = Reflect.get(target, 'open');
|
|
89
|
+
return (...args) => {
|
|
90
|
+
const method = args[0];
|
|
91
|
+
const url = args[1];
|
|
92
|
+
this.item.method = method ? method.toUpperCase() : 'GET';
|
|
93
|
+
this.item.url = url || '';
|
|
94
|
+
this.item.name = this.item.url.replace(new RegExp('/*$'), '').split('/').pop() || '';
|
|
95
|
+
this.item.getData = genGetDataByUrl(this.item.url, {});
|
|
96
|
+
return targetFunction.apply(target, args);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
getSend(target) {
|
|
100
|
+
const targetFunction = Reflect.get(target, 'send');
|
|
101
|
+
return (...args) => {
|
|
102
|
+
const data = args[0];
|
|
103
|
+
this.item.requestData = genStringBody(data);
|
|
104
|
+
return targetFunction.apply(target, args);
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
getSetRequestHeader(target) {
|
|
108
|
+
const targetFunction = Reflect.get(target, 'setRequestHeader');
|
|
109
|
+
return (...args) => {
|
|
110
|
+
if (!this.item.requestHeader) {
|
|
111
|
+
this.item.requestHeader = {};
|
|
112
|
+
}
|
|
113
|
+
// @ts-ignore
|
|
114
|
+
this.item.requestHeader[args[0]] = args[1];
|
|
115
|
+
return targetFunction.apply(target, args);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
setOnReadyStateChange(target, key, orscFunction) {
|
|
119
|
+
return Reflect.set(target, key, (...args) => {
|
|
120
|
+
this.onReadyStateChange();
|
|
121
|
+
orscFunction.apply(target, args);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
setOnAbort(target, key, oaFunction) {
|
|
125
|
+
return Reflect.set(target, key, (...args) => {
|
|
126
|
+
this.onAbort();
|
|
127
|
+
oaFunction.apply(target, args);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
setOnTimeout(target, key, otFunction) {
|
|
131
|
+
return Reflect.set(target, key, (...args) => {
|
|
132
|
+
this.onTimeout();
|
|
133
|
+
otFunction.apply(target, args);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Update item's properties according to readyState.
|
|
138
|
+
*/
|
|
139
|
+
updateItemByReadyState() {
|
|
140
|
+
switch (this.XMLReq.readyState) {
|
|
141
|
+
case RequestState.UNSENT:
|
|
142
|
+
case RequestState.OPENED:
|
|
143
|
+
this.item.status = RequestState.UNSENT;
|
|
144
|
+
this.item.statusText = 'Pending';
|
|
145
|
+
if (!this.item.startTime) {
|
|
146
|
+
this.item.startTime = performance.now();
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
case RequestState.HEADERS_RECEIVED:
|
|
150
|
+
this.item.status = this.XMLReq.status;
|
|
151
|
+
this.item.statusText = 'Loading';
|
|
152
|
+
this.item.header = {};
|
|
153
|
+
// eslint-disable-next-line no-case-declarations
|
|
154
|
+
const header = this.XMLReq.getAllResponseHeaders() || '', headerArr = header.split('\n');
|
|
155
|
+
// extract plain text to key-value format
|
|
156
|
+
for (let i = 0; i < headerArr.length; i++) {
|
|
157
|
+
const line = headerArr[i];
|
|
158
|
+
if (!line) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const arr = line.split(': ');
|
|
162
|
+
const key = arr[0];
|
|
163
|
+
this.item.header[key] = arr.slice(1).join(': ');
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
case RequestState.LOADING:
|
|
167
|
+
this.item.status = this.XMLReq.status;
|
|
168
|
+
this.item.statusText = 'Loading';
|
|
169
|
+
if (!!this.XMLReq.response && this.XMLReq.response.length) {
|
|
170
|
+
this.item.responseSize = this.XMLReq.response.length;
|
|
171
|
+
this.item.responseSizeText = formatByteSize(this.item.responseSize);
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
case RequestState.DONE:
|
|
175
|
+
// `XMLReq.abort()` will change `status` from 200 to 0, so use previous value in this case
|
|
176
|
+
this.item.status = this.XMLReq.status || this.item.status || 0;
|
|
177
|
+
// show status code when request completed
|
|
178
|
+
this.item.statusText = String(this.item.status);
|
|
179
|
+
this.item.endTime = performance.now();
|
|
180
|
+
this.item.duration = this.item.endTime - (this.item.startTime || this.item.endTime);
|
|
181
|
+
this.item.response = this.XMLReq.response;
|
|
182
|
+
if (!!this.XMLReq.response && this.XMLReq.response.length) {
|
|
183
|
+
this.item.responseSize = this.XMLReq.response.length;
|
|
184
|
+
this.item.responseSizeText = formatByteSize(this.item.responseSize);
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
default:
|
|
188
|
+
this.item.status = this.XMLReq.status;
|
|
189
|
+
this.item.statusText = 'Unknown';
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export default class XHRProxy {
|
|
195
|
+
static create(ignoredHeaders, setSessionTokenHeader, sanitize, sendMessage, isServiceUrl) {
|
|
196
|
+
return new Proxy(XMLHttpRequest, {
|
|
197
|
+
construct(original) {
|
|
198
|
+
const XMLReq = new original();
|
|
199
|
+
return new Proxy(XMLReq, new XHRProxyHandler(XMLReq, ignoredHeaders, setSessionTokenHeader, sanitize, sendMessage, isServiceUrl));
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
XHRProxy.origXMLHttpRequest = XMLHttpRequest;
|
package/lib/modules/console.js
CHANGED
|
@@ -90,26 +90,34 @@ export default function (app, opts) {
|
|
|
90
90
|
return;
|
|
91
91
|
}
|
|
92
92
|
const sendConsoleLog = app.safe((level, args) => app.send(ConsoleLog(level, printf(args))));
|
|
93
|
-
let n;
|
|
93
|
+
let n = 0;
|
|
94
94
|
const reset = () => {
|
|
95
95
|
n = 0;
|
|
96
96
|
};
|
|
97
97
|
app.attachStartCallback(reset);
|
|
98
98
|
app.ticker.attach(reset, 33, false);
|
|
99
|
-
const patchConsole = (console) =>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
99
|
+
const patchConsole = (console) => {
|
|
100
|
+
const handler = {
|
|
101
|
+
apply: function (target, thisArg, argumentsList) {
|
|
102
|
+
target.apply(console, argumentsList);
|
|
103
|
+
n = n + 1;
|
|
104
|
+
if (n > options.consoleThrottling) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
sendConsoleLog(target.name, argumentsList);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
options.consoleMethods.forEach((method) => {
|
|
113
|
+
if (consoleMethods.indexOf(method) === -1) {
|
|
114
|
+
app.debug.error(`OpenReplay: unsupported console method "${method}"`);
|
|
108
115
|
return;
|
|
109
116
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
117
|
+
const fn = console[method];
|
|
118
|
+
console[method] = new Proxy(fn, handler);
|
|
119
|
+
});
|
|
120
|
+
};
|
|
113
121
|
const patchContext = app.safe((context) => patchConsole(context.console));
|
|
114
122
|
patchContext(window);
|
|
115
123
|
app.observer.attachContextCallback(patchContext);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import App from '../app/index.js';
|
|
2
|
+
export interface IFeatureFlag {
|
|
3
|
+
key: string;
|
|
4
|
+
is_persist: boolean;
|
|
5
|
+
value: string | boolean;
|
|
6
|
+
payload: string;
|
|
7
|
+
}
|
|
8
|
+
export interface FetchPersistFlagsData {
|
|
9
|
+
key: string;
|
|
10
|
+
value: string | boolean;
|
|
11
|
+
}
|
|
12
|
+
export default class FeatureFlags {
|
|
13
|
+
private readonly app;
|
|
14
|
+
flags: IFeatureFlag[];
|
|
15
|
+
storageKey: string;
|
|
16
|
+
onFlagsCb: (flags: IFeatureFlag[]) => void;
|
|
17
|
+
constructor(app: App);
|
|
18
|
+
getFeatureFlag(flagName: string): IFeatureFlag | undefined;
|
|
19
|
+
isFlagEnabled(flagName: string): boolean;
|
|
20
|
+
onFlagsLoad(cb: (flags: IFeatureFlag[]) => void): void;
|
|
21
|
+
reloadFlags(): Promise<void>;
|
|
22
|
+
handleFlags(flags: IFeatureFlag[]): void;
|
|
23
|
+
clearPersistFlags(): void;
|
|
24
|
+
diffPersist(flags: IFeatureFlag[]): IFeatureFlag[];
|
|
25
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export default class FeatureFlags {
|
|
11
|
+
constructor(app) {
|
|
12
|
+
this.app = app;
|
|
13
|
+
this.storageKey = '__openreplay_flags';
|
|
14
|
+
const persistFlags = this.app.sessionStorage.getItem(this.storageKey);
|
|
15
|
+
if (persistFlags) {
|
|
16
|
+
const persistFlagsStrArr = persistFlags.split(';').filter(Boolean);
|
|
17
|
+
this.flags = persistFlagsStrArr.map((flag) => JSON.parse(flag));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
getFeatureFlag(flagName) {
|
|
21
|
+
return this.flags.find((flag) => flag.key === flagName);
|
|
22
|
+
}
|
|
23
|
+
isFlagEnabled(flagName) {
|
|
24
|
+
return this.flags.findIndex((flag) => flag.key === flagName) !== -1;
|
|
25
|
+
}
|
|
26
|
+
onFlagsLoad(cb) {
|
|
27
|
+
this.onFlagsCb = cb;
|
|
28
|
+
}
|
|
29
|
+
reloadFlags() {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
const persistFlagsStr = this.app.sessionStorage.getItem(this.storageKey);
|
|
32
|
+
const persistFlags = {};
|
|
33
|
+
if (persistFlagsStr) {
|
|
34
|
+
const persistArray = persistFlagsStr.split(';').filter(Boolean);
|
|
35
|
+
persistArray.forEach((flag) => {
|
|
36
|
+
const flagObj = JSON.parse(flag);
|
|
37
|
+
persistFlags[flagObj.key] = { key: flagObj.key, value: flagObj.value };
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const sessionInfo = this.app.session.getInfo();
|
|
41
|
+
const userInfo = this.app.session.userInfo;
|
|
42
|
+
const requestObject = {
|
|
43
|
+
projectID: sessionInfo.projectID,
|
|
44
|
+
userID: sessionInfo.userID,
|
|
45
|
+
metadata: sessionInfo.metadata,
|
|
46
|
+
referrer: document.referrer,
|
|
47
|
+
// todo: get from backend
|
|
48
|
+
os: userInfo.userOS,
|
|
49
|
+
device: userInfo.userDevice,
|
|
50
|
+
country: userInfo.userCountry,
|
|
51
|
+
state: userInfo.userState,
|
|
52
|
+
city: userInfo.userCity,
|
|
53
|
+
browser: userInfo.userBrowser,
|
|
54
|
+
persistFlags: persistFlags,
|
|
55
|
+
};
|
|
56
|
+
const resp = yield fetch(this.app.options.ingestPoint + '/v1/web/feature-flags', {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
Authorization: `Bearer ${this.app.session.getSessionToken()}`,
|
|
61
|
+
},
|
|
62
|
+
body: JSON.stringify(requestObject),
|
|
63
|
+
});
|
|
64
|
+
if (resp.status === 200) {
|
|
65
|
+
const data = yield resp.json();
|
|
66
|
+
return this.handleFlags(data.flags);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
handleFlags(flags) {
|
|
71
|
+
var _a;
|
|
72
|
+
const persistFlags = [];
|
|
73
|
+
flags.forEach((flag) => {
|
|
74
|
+
if (flag.is_persist)
|
|
75
|
+
persistFlags.push(flag);
|
|
76
|
+
});
|
|
77
|
+
let str = '';
|
|
78
|
+
const uniquePersistFlags = this.diffPersist(persistFlags);
|
|
79
|
+
uniquePersistFlags.forEach((flag) => {
|
|
80
|
+
str += `${JSON.stringify(flag)};`;
|
|
81
|
+
});
|
|
82
|
+
this.app.sessionStorage.setItem(this.storageKey, str);
|
|
83
|
+
this.flags = flags;
|
|
84
|
+
return (_a = this.onFlagsCb) === null || _a === void 0 ? void 0 : _a.call(this, flags);
|
|
85
|
+
}
|
|
86
|
+
clearPersistFlags() {
|
|
87
|
+
this.app.sessionStorage.removeItem(this.storageKey);
|
|
88
|
+
}
|
|
89
|
+
diffPersist(flags) {
|
|
90
|
+
const persistFlags = this.app.sessionStorage.getItem(this.storageKey);
|
|
91
|
+
if (!persistFlags)
|
|
92
|
+
return flags;
|
|
93
|
+
const persistFlagsStrArr = persistFlags.split(';').filter(Boolean);
|
|
94
|
+
const persistFlagsArr = persistFlagsStrArr.map((flag) => JSON.parse(flag));
|
|
95
|
+
return flags.filter((flag) => persistFlagsArr.findIndex((pf) => pf.key === flag.key) === -1);
|
|
96
|
+
}
|
|
97
|
+
}
|
package/lib/modules/network.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import type App from '../app/index.js';
|
|
2
2
|
import type { AxiosInstance } from './axiosSpy.js';
|
|
3
|
-
type XHRRequestBody = Parameters<XMLHttpRequest['send']>[0];
|
|
4
|
-
type FetchRequestBody = RequestInit['body'];
|
|
5
3
|
interface RequestData {
|
|
6
|
-
body:
|
|
4
|
+
body: string | null;
|
|
7
5
|
headers: Record<string, string>;
|
|
8
6
|
}
|
|
9
7
|
interface ResponseData {
|
|
@@ -17,7 +15,7 @@ export interface RequestResponseData {
|
|
|
17
15
|
request: RequestData;
|
|
18
16
|
response: ResponseData;
|
|
19
17
|
}
|
|
20
|
-
type Sanitizer = (data: RequestResponseData) => RequestResponseData
|
|
18
|
+
type Sanitizer = (data: RequestResponseData) => RequestResponseData;
|
|
21
19
|
export interface Options {
|
|
22
20
|
sessionTokenHeader: string | boolean;
|
|
23
21
|
failuresOnly: boolean;
|
|
@@ -26,6 +24,7 @@ export interface Options {
|
|
|
26
24
|
captureInIframes: boolean;
|
|
27
25
|
sanitizer?: Sanitizer;
|
|
28
26
|
axiosInstances?: Array<AxiosInstance>;
|
|
27
|
+
useProxy?: boolean;
|
|
29
28
|
}
|
|
30
29
|
export default function (app: App, opts?: Partial<Options>): void;
|
|
31
30
|
export {};
|