@wutiange/log-listener-plugin 1.3.2 → 2.0.1-alpha.2
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +130 -24
- package/dist/src/HTTPInterceptor.d.ts +1 -0
- package/dist/src/HTTPInterceptor.js +12 -9
- package/dist/src/HTTPInterceptor.js.map +1 -1
- package/dist/src/Server.d.ts +13 -6
- package/dist/src/Server.js +119 -41
- package/dist/src/Server.js.map +1 -1
- package/dist/src/__mocks__/react-native/Libraries/Blob/FileReader.js +0 -1
- package/dist/src/__mocks__/react-native/Libraries/Blob/FileReader.js.map +1 -1
- package/dist/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js +0 -1
- package/dist/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js.map +1 -1
- package/dist/src/__tests__/Server.test.js +76 -115
- package/dist/src/__tests__/Server.test.js.map +1 -1
- package/dist/src/common.d.ts +19 -10
- package/dist/src/common.js +63 -4
- package/dist/src/common.js.map +1 -1
- package/dist/src/logPlugin.d.ts +12 -9
- package/dist/src/logPlugin.js +87 -82
- package/dist/src/logPlugin.js.map +1 -1
- package/dist/src/logger.d.ts +6 -0
- package/dist/src/logger.js +16 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/utils.js +12 -7
- package/dist/src/utils.js.map +1 -1
- package/index.ts +3 -0
- package/package.json +18 -12
- package/src/HTTPInterceptor.ts +339 -0
- package/src/Server.ts +164 -0
- package/src/__mocks__/react-native/Libraries/Blob/FileReader.js +45 -0
- package/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js +39 -0
- package/src/__tests__/HTTPInterceptor.test.ts +322 -0
- package/src/__tests__/Server.test.ts +150 -0
- package/src/__tests__/utils.test.ts +113 -0
- package/src/common.ts +70 -0
- package/src/logPlugin.ts +224 -0
- package/src/logger.ts +15 -0
- package/src/utils.ts +112 -0
- package/tsconfig.json +27 -0
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"logPlugin.js","sourceRoot":"","sources":["../../src/logPlugin.ts"],"names":[],"mappings":";;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I,CAAC,CAAC;QAC7C,CAAC,CAAA;QAED,SAAI,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;YACxB,IAAI,CAAC,IAAI,CAAC,cAAK,CAAC,IAAI,EAAE,YAAG,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;QAC9C,CAAC,CAAA;QAED,UAAK,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;YACzB,IAAI,CAAC,IAAI,CAAC,cAAK,CAAC,KAAK,EAAE,YAAG,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAC;QAC/C,CAAC,CAAA;QAlLC,IAAI,CAAC,IAAI,EAAE,CAAA;IACb,CAAC;CAmLF;AACD,MAAM,aAAa,GAAG,IAAA,oCAA4B,EAAC,SAAS,CAAC,CAAA;AAEpD,sCAAa;AADtB,MAAM,SAAS,GAAG,IAAI,aAAa,EAAE,CAAC;AAEtC,kBAAe,SAAS,CAAC"}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const [log, warn, error] = [console.log, console.warn, console.error];
|
4
|
+
const logger = {
|
5
|
+
log: (...data) => {
|
6
|
+
log(...data);
|
7
|
+
},
|
8
|
+
warn: (...data) => {
|
9
|
+
warn(...data);
|
10
|
+
},
|
11
|
+
error: (...data) => {
|
12
|
+
error(...data);
|
13
|
+
},
|
14
|
+
};
|
15
|
+
exports.default = logger;
|
16
|
+
//# sourceMappingURL=logger.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":";;AAAA,MAAM,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;AAEtE,MAAM,MAAM,GAAG;IACb,GAAG,EAAE,CAAC,GAAG,IAAW,EAAE,EAAE;QACtB,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;IACd,CAAC;IACD,IAAI,EAAE,CAAC,GAAG,IAAW,EAAE,EAAE;QACvB,IAAI,CAAC,GAAG,IAAI,CAAC,CAAA;IACf,CAAC;IACD,KAAK,EAAE,CAAC,GAAG,IAAW,EAAE,EAAE;QACxB,KAAK,CAAC,GAAG,IAAI,CAAC,CAAA;IAChB,CAAC;CACF,CAAA;AAED,kBAAe,MAAM,CAAA"}
|
package/dist/src/utils.js
CHANGED
@@ -1,29 +1,36 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
6
|
+
exports.sleep = sleep;
|
7
|
+
exports.hasPort = hasPort;
|
8
|
+
exports.createClassWithErrorHandling = createClassWithErrorHandling;
|
9
|
+
exports.formDataToString = formDataToString;
|
10
|
+
const url_parse_1 = __importDefault(require("url-parse"));
|
11
|
+
const logger_1 = __importDefault(require("./logger"));
|
4
12
|
function sleep(ms, isReject = false) {
|
5
13
|
return new Promise((resolve, reject) => {
|
6
14
|
setTimeout(isReject ? () => reject({
|
7
15
|
code: 11001,
|
8
16
|
key: '@wutiange/log-listener-plugin%%timeout',
|
9
|
-
|
17
|
+
message: 'Timeout'
|
10
18
|
}) : resolve, ms);
|
11
19
|
});
|
12
20
|
}
|
13
|
-
exports.sleep = sleep;
|
14
21
|
function hasPort(url) {
|
15
22
|
if (!url || typeof url !== 'string') {
|
16
23
|
return false;
|
17
24
|
}
|
18
25
|
try {
|
19
|
-
const parsedUrl = new
|
26
|
+
const parsedUrl = new url_parse_1.default(url);
|
20
27
|
return parsedUrl.port !== '';
|
21
28
|
}
|
22
29
|
catch (error) {
|
30
|
+
logger_1.default.error(error);
|
23
31
|
return false;
|
24
32
|
}
|
25
33
|
}
|
26
|
-
exports.hasPort = hasPort;
|
27
34
|
function createClassWithErrorHandling(BaseClass) {
|
28
35
|
return new Proxy(BaseClass, {
|
29
36
|
construct(target, args) {
|
@@ -79,7 +86,6 @@ function createClassWithErrorHandling(BaseClass) {
|
|
79
86
|
}
|
80
87
|
});
|
81
88
|
}
|
82
|
-
exports.createClassWithErrorHandling = createClassWithErrorHandling;
|
83
89
|
function formDataToString(formData) {
|
84
90
|
const boundary = '----WebKitFormBoundary' + Math.random().toString(36).substr(2);
|
85
91
|
let result = '';
|
@@ -97,5 +103,4 @@ function formDataToString(formData) {
|
|
97
103
|
result += `--${boundary}--\r\n`;
|
98
104
|
return result;
|
99
105
|
}
|
100
|
-
exports.formDataToString = formDataToString;
|
101
106
|
//# sourceMappingURL=utils.js.map
|
package/dist/src/utils.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;;;AAGA,sBAQC;AAGD,0BAkBC;AAKD,oEAmDC;AAGD,4CAkBC;AA7GD,0DAA4B;AAC5B,sDAA8B;AAE9B,SAAgB,KAAK,CAAC,EAAU,EAAE,WAAoB,KAAK;IACzD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC;YACjC,IAAI,EAAE,KAAK;YACX,GAAG,EAAE,wCAAwC;YAC7C,OAAO,EAAE,SAAS;SACnB,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IACnB,CAAC,CAAC,CAAA;AACJ,CAAC;AAGD,SAAgB,OAAO,CAAC,GAAW;IAEjC,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;QACpC,OAAO,KAAK,CAAC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AC;IAC3C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,IAAI,KAAK,QAAQ,MAAM,CAAC;QAC9B,MAAM,IAAI,wBAAwB,IAAI,CAAC,OAAO,CAAC,qBAAqB,CAAC,MAAM,CAAC;QAC5E,IAAI,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,iBAAiB,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC;QAChE,CAAC;QACD,MAAM,KAAK,GAAG,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;QACxD,MAAM,IAAI,mBAAmB,KAAK,CAAC,MAAM,UAAU,CAAC;QACpD,MAAM,IAAI,GAAG,KAAK,MAAM,CAAC;IAC3B,CAAC;IACD,MAAM,IAAI,KAAK,QAAQ,QAAQ,CAAC;IAChC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/index.ts
ADDED
package/package.json
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
{
|
2
2
|
"name": "@wutiange/log-listener-plugin",
|
3
|
-
"version": "1.
|
3
|
+
"version": "2.0.1-alpha.2",
|
4
4
|
"description": "log-record 客户端对应的的插件\r\nLog-record client corresponding plugin",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"types": "dist/index.d.ts",
|
7
7
|
"source": "index.ts",
|
8
|
+
"react-native": "index.ts",
|
8
9
|
"repository": {
|
9
10
|
"type": "git",
|
10
11
|
"url": "https://github.com/wutiange/log-listener-plugin.git"
|
@@ -18,14 +19,6 @@
|
|
18
19
|
"charles"
|
19
20
|
],
|
20
21
|
"private": false,
|
21
|
-
"exports": {
|
22
|
-
"console": {
|
23
|
-
"default": "./dist/console.js"
|
24
|
-
},
|
25
|
-
"fetch": {
|
26
|
-
"default": "./dist/fetch.js"
|
27
|
-
}
|
28
|
-
},
|
29
22
|
"scripts": {
|
30
23
|
"build": "npm run test && tsc",
|
31
24
|
"dev": "ts-node ./index.ts",
|
@@ -35,19 +28,32 @@
|
|
35
28
|
},
|
36
29
|
"devDependencies": {
|
37
30
|
"@jest/globals": "^29.7.0",
|
31
|
+
"@types/crypto-js": "^4.2.2",
|
38
32
|
"@types/jest": "^29.5.12",
|
33
|
+
"@types/react-native-zeroconf": "^0.13.1",
|
34
|
+
"@types/url-parse": "^1.4.11",
|
39
35
|
"jest": "^29.7.0",
|
40
36
|
"ts-jest": "^29.1.2",
|
41
37
|
"ts-node": "^10.9.2",
|
42
38
|
"typescript": "^5.3.3"
|
43
39
|
},
|
44
40
|
"peerDependencies": {
|
45
|
-
"react-native": "*"
|
41
|
+
"react-native": "*",
|
42
|
+
"react-native-device-info": "^14.0.0",
|
43
|
+
"react-native-zeroconf": "^0.13.8",
|
44
|
+
"@react-native-async-storage/async-storage": "*"
|
46
45
|
},
|
47
46
|
"files": [
|
48
47
|
"package.json",
|
49
48
|
"README.md",
|
50
49
|
"LICENSE",
|
51
|
-
"dist"
|
52
|
-
|
50
|
+
"dist",
|
51
|
+
"src",
|
52
|
+
"index.ts",
|
53
|
+
"tsconfig.json"
|
54
|
+
],
|
55
|
+
"dependencies": {
|
56
|
+
"crypto-js": "^4.2.0",
|
57
|
+
"url-parse": "^1.5.10"
|
58
|
+
}
|
53
59
|
}
|
@@ -0,0 +1,339 @@
|
|
1
|
+
import XHRInterceptor from 'react-native/Libraries/Network/XHRInterceptor';
|
2
|
+
import BlobFileReader from 'react-native/Libraries/Blob/FileReader';
|
3
|
+
import {Blob} from 'buffer';
|
4
|
+
import { createClassWithErrorHandling, formDataToString } from './utils';
|
5
|
+
|
6
|
+
type StartNetworkLoggingOptions = {
|
7
|
+
/** List of hosts to ignore, e.g. `services.test.com` */
|
8
|
+
ignoredHosts?: string[];
|
9
|
+
/** List of urls to ignore, e.g. `https://services.test.com/test` */
|
10
|
+
ignoredUrls?: string[];
|
11
|
+
/**
|
12
|
+
* List of url patterns to ignore, e.g. `/^GET https://test.com\/pages\/.*$/`
|
13
|
+
*
|
14
|
+
* Url to match with is in the format: `${method} ${url}`, e.g. `GET https://test.com/pages/123`
|
15
|
+
*/
|
16
|
+
ignoredPatterns?: RegExp[];
|
17
|
+
/**
|
18
|
+
* Force the network logger to start even if another program is using the network interceptor
|
19
|
+
* e.g. a dev/debuging program
|
20
|
+
*/
|
21
|
+
forceEnable?: boolean;
|
22
|
+
};
|
23
|
+
|
24
|
+
interface HttpRequestInfo {
|
25
|
+
id: string;
|
26
|
+
method: RequestMethod;
|
27
|
+
url: string;
|
28
|
+
timeout: number;
|
29
|
+
requestHeaders: Record<string, string>;
|
30
|
+
requestData: any | null;
|
31
|
+
startTime: number;
|
32
|
+
endTime: number;
|
33
|
+
responseHeaders: Headers;
|
34
|
+
responseData: any | null;
|
35
|
+
status: number;
|
36
|
+
duration: number;
|
37
|
+
responseContentType: string;
|
38
|
+
responseSize: number;
|
39
|
+
responseURL: string;
|
40
|
+
responseType: string;
|
41
|
+
}
|
42
|
+
|
43
|
+
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
44
|
+
|
45
|
+
type XHR = {
|
46
|
+
uniqueId: string;
|
47
|
+
responseHeaders?: Headers;
|
48
|
+
};
|
49
|
+
|
50
|
+
type EventName =
|
51
|
+
| 'open'
|
52
|
+
| 'requestHeader'
|
53
|
+
| 'headerReceived'
|
54
|
+
| 'send'
|
55
|
+
| 'response';
|
56
|
+
|
57
|
+
const extractHost = (url: string) => {
|
58
|
+
const host = url.split('//')[1]?.split(':')[0]?.split('/')[0] || undefined;
|
59
|
+
|
60
|
+
return host;
|
61
|
+
};
|
62
|
+
|
63
|
+
const generateUniqueId = () => {
|
64
|
+
return Date.now().toString(36) + Math.random().toString(36).substr(2);
|
65
|
+
};
|
66
|
+
|
67
|
+
const parseResponseBlob = async (response: Blob) => {
|
68
|
+
const blobReader = new BlobFileReader();
|
69
|
+
blobReader.readAsText(response);
|
70
|
+
|
71
|
+
return await new Promise<string>((resolve, reject) => {
|
72
|
+
const handleError = () => reject(blobReader.error);
|
73
|
+
|
74
|
+
blobReader.addEventListener('load', () => {
|
75
|
+
resolve(blobReader.result);
|
76
|
+
});
|
77
|
+
blobReader.addEventListener('error', handleError);
|
78
|
+
blobReader.addEventListener('abort', handleError);
|
79
|
+
});
|
80
|
+
};
|
81
|
+
|
82
|
+
const getResponseBody = async (responseType: string, response: any) => {
|
83
|
+
try {
|
84
|
+
if (responseType === 'blob' && response) {
|
85
|
+
return await parseResponseBlob(response as unknown as Blob);
|
86
|
+
}
|
87
|
+
return response ?? null;
|
88
|
+
} catch (error) {
|
89
|
+
console.warn("getResponseBody---error---", error)
|
90
|
+
return null;
|
91
|
+
}
|
92
|
+
};
|
93
|
+
|
94
|
+
class HTTPInterceptor {
|
95
|
+
private static _index = 0;
|
96
|
+
private ignoredHosts: Set<string> | undefined;
|
97
|
+
private ignoredUrls: Set<string> | undefined;
|
98
|
+
private ignoredPatterns: RegExp[] | undefined;
|
99
|
+
// 只保存正在请求中的
|
100
|
+
private allRequests = new Map<string, Partial<HttpRequestInfo>>();
|
101
|
+
|
102
|
+
private userListeners: [
|
103
|
+
EventName,
|
104
|
+
(data: Partial<HttpRequestInfo>) => Promise<void> | void,
|
105
|
+
][] = [];
|
106
|
+
|
107
|
+
private enabled = false;
|
108
|
+
|
109
|
+
addListener = (
|
110
|
+
eventName: EventName,
|
111
|
+
listener: (data: Partial<HttpRequestInfo>) => Promise<void> | void,
|
112
|
+
) => {
|
113
|
+
// 如果之前已经订阅过了就过滤掉
|
114
|
+
if (
|
115
|
+
this.userListeners.find(
|
116
|
+
([name, tempListener]) =>
|
117
|
+
name === eventName && tempListener === listener,
|
118
|
+
)
|
119
|
+
) {
|
120
|
+
return;
|
121
|
+
}
|
122
|
+
this.userListeners.push([eventName, listener]);
|
123
|
+
|
124
|
+
return () => {
|
125
|
+
this.userListeners = this.userListeners.filter(
|
126
|
+
([name, tempListener]) =>
|
127
|
+
name !== eventName || tempListener !== listener,
|
128
|
+
);
|
129
|
+
};
|
130
|
+
};
|
131
|
+
|
132
|
+
removeListener = (
|
133
|
+
eventName: EventName,
|
134
|
+
listener: (data: Partial<HttpRequestInfo>) => Promise<void> | void,
|
135
|
+
) => {
|
136
|
+
this.userListeners = this.userListeners.filter(
|
137
|
+
([name, tempListener]) => name !== eventName || tempListener !== listener,
|
138
|
+
);
|
139
|
+
};
|
140
|
+
|
141
|
+
removeAllListener() {
|
142
|
+
this.userListeners = [];
|
143
|
+
}
|
144
|
+
|
145
|
+
private listenerHandle = (
|
146
|
+
eventName: EventName,
|
147
|
+
data: Partial<HttpRequestInfo>,
|
148
|
+
) => {
|
149
|
+
this.userListeners.forEach(async ([name, listener]) => {
|
150
|
+
try {
|
151
|
+
if (name === eventName) {
|
152
|
+
await listener(data);
|
153
|
+
}
|
154
|
+
} catch (error: any) {
|
155
|
+
console.warn(`eventName=${eventName}, error=${error?.message}`)
|
156
|
+
}
|
157
|
+
});
|
158
|
+
};
|
159
|
+
|
160
|
+
private openHandle = (method: RequestMethod, url: string, xhr: XHR) => {
|
161
|
+
if (this.ignoredHosts) {
|
162
|
+
const host = extractHost(url);
|
163
|
+
if (host && this.ignoredHosts.has(host)) {
|
164
|
+
return;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
if (this.ignoredUrls && this.ignoredUrls.has(url)) {
|
168
|
+
return;
|
169
|
+
}
|
170
|
+
|
171
|
+
if (this.ignoredPatterns) {
|
172
|
+
if (
|
173
|
+
this.ignoredPatterns.some(pattern => pattern.test(`${method} ${url}`))
|
174
|
+
) {
|
175
|
+
return;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
xhr.uniqueId = HTTPInterceptor._index + generateUniqueId();
|
179
|
+
const newRequest = {
|
180
|
+
id: xhr.uniqueId,
|
181
|
+
method,
|
182
|
+
url,
|
183
|
+
};
|
184
|
+
this.allRequests.set(xhr.uniqueId, newRequest);
|
185
|
+
this.listenerHandle('open', newRequest);
|
186
|
+
};
|
187
|
+
|
188
|
+
private requestHeaderHandle = (header: string, value: string, xhr: XHR) => {
|
189
|
+
const currentRequest = this.allRequests.get(xhr.uniqueId);
|
190
|
+
if (!currentRequest) {
|
191
|
+
return;
|
192
|
+
}
|
193
|
+
if (!currentRequest.requestHeaders) {
|
194
|
+
currentRequest.requestHeaders = {};
|
195
|
+
}
|
196
|
+
currentRequest.requestHeaders[header] = value;
|
197
|
+
this.listenerHandle('requestHeader', currentRequest);
|
198
|
+
};
|
199
|
+
|
200
|
+
private headerReceivedHandle = (
|
201
|
+
responseContentType: string,
|
202
|
+
responseSize: number,
|
203
|
+
responseHeaders: Headers,
|
204
|
+
xhr: XHR,
|
205
|
+
) => {
|
206
|
+
const currentRequest = this.allRequests.get(xhr.uniqueId);
|
207
|
+
if (!currentRequest) {
|
208
|
+
return;
|
209
|
+
}
|
210
|
+
currentRequest.responseContentType = responseContentType;
|
211
|
+
currentRequest.responseSize = responseSize;
|
212
|
+
currentRequest.responseHeaders = xhr.responseHeaders;
|
213
|
+
this.listenerHandle('headerReceived', currentRequest);
|
214
|
+
};
|
215
|
+
|
216
|
+
private responseHandle = async (
|
217
|
+
status: number,
|
218
|
+
timeout: number,
|
219
|
+
response: any,
|
220
|
+
responseURL: string,
|
221
|
+
responseType: string,
|
222
|
+
xhr: XHR,
|
223
|
+
) => {
|
224
|
+
const currentRequest = this.allRequests.get(xhr.uniqueId);
|
225
|
+
if (!currentRequest) {
|
226
|
+
return;
|
227
|
+
}
|
228
|
+
currentRequest.endTime = Date.now();
|
229
|
+
currentRequest.status = status;
|
230
|
+
currentRequest.timeout = timeout;
|
231
|
+
currentRequest.responseData = await getResponseBody(responseType, response);
|
232
|
+
currentRequest.responseURL = responseURL;
|
233
|
+
currentRequest.responseType = responseType;
|
234
|
+
currentRequest.duration =
|
235
|
+
currentRequest.endTime - (currentRequest.startTime ?? 0);
|
236
|
+
this.listenerHandle('response', currentRequest);
|
237
|
+
this.allRequests.delete(xhr.uniqueId);
|
238
|
+
};
|
239
|
+
|
240
|
+
private sendHandle = (data: any, xhr: XHR) => {
|
241
|
+
const currentRequest = this.allRequests.get(xhr.uniqueId);
|
242
|
+
if (!currentRequest) {
|
243
|
+
return;
|
244
|
+
}
|
245
|
+
try {
|
246
|
+
if (data && typeof data === 'object' && data instanceof FormData) {
|
247
|
+
currentRequest.requestData = formDataToString(data);
|
248
|
+
} else {
|
249
|
+
currentRequest.requestData = JSON.parse(data);
|
250
|
+
}
|
251
|
+
} catch (error) {
|
252
|
+
currentRequest.requestData = null;
|
253
|
+
}
|
254
|
+
currentRequest.startTime = Date.now();
|
255
|
+
this.listenerHandle('send', currentRequest);
|
256
|
+
};
|
257
|
+
|
258
|
+
setIgnoredUrls = (ignoredUrls: string[]) => {
|
259
|
+
if (ignoredUrls?.length) {
|
260
|
+
if (
|
261
|
+
!Array.isArray(ignoredUrls) ||
|
262
|
+
typeof ignoredUrls[0] !== 'string'
|
263
|
+
) {
|
264
|
+
console.warn(
|
265
|
+
'ignoredUrls must be an array of strings. The logger has not been started.',
|
266
|
+
);
|
267
|
+
return;
|
268
|
+
}
|
269
|
+
this.ignoredUrls = new Set(ignoredUrls);
|
270
|
+
}
|
271
|
+
}
|
272
|
+
|
273
|
+
enable = (options?: StartNetworkLoggingOptions) => {
|
274
|
+
try {
|
275
|
+
if (
|
276
|
+
this.enabled ||
|
277
|
+
(XHRInterceptor.isInterceptorEnabled() && !options?.forceEnable)
|
278
|
+
) {
|
279
|
+
if (!this.enabled) {
|
280
|
+
console.warn(
|
281
|
+
'network interceptor has not been enabled as another interceptor is already running (e.g. another debugging program). Use option `forceEnable: true` to override this behaviour.',
|
282
|
+
);
|
283
|
+
}
|
284
|
+
return;
|
285
|
+
}
|
286
|
+
|
287
|
+
if (options?.ignoredHosts) {
|
288
|
+
if (
|
289
|
+
!Array.isArray(options.ignoredHosts) ||
|
290
|
+
typeof options.ignoredHosts[0] !== 'string'
|
291
|
+
) {
|
292
|
+
console.warn(
|
293
|
+
'ignoredHosts must be an array of strings. The logger has not been started.',
|
294
|
+
);
|
295
|
+
return;
|
296
|
+
}
|
297
|
+
this.ignoredHosts = new Set(options.ignoredHosts);
|
298
|
+
}
|
299
|
+
|
300
|
+
if (options?.ignoredPatterns) {
|
301
|
+
this.ignoredPatterns = options.ignoredPatterns;
|
302
|
+
}
|
303
|
+
this.setIgnoredUrls(options?.ignoredUrls)
|
304
|
+
XHRInterceptor.setOpenCallback(this.openHandle);
|
305
|
+
XHRInterceptor.setRequestHeaderCallback(this.requestHeaderHandle);
|
306
|
+
XHRInterceptor.setHeaderReceivedCallback(this.headerReceivedHandle);
|
307
|
+
XHRInterceptor.setSendCallback(this.sendHandle);
|
308
|
+
XHRInterceptor.setResponseCallback(this.responseHandle);
|
309
|
+
XHRInterceptor.enableInterception();
|
310
|
+
this.enabled = true;
|
311
|
+
} catch (error) {}
|
312
|
+
};
|
313
|
+
|
314
|
+
disable = () => {
|
315
|
+
if (!this.enabled) {
|
316
|
+
return;
|
317
|
+
}
|
318
|
+
XHRInterceptor.disableInterception();
|
319
|
+
this.enabled = false;
|
320
|
+
};
|
321
|
+
|
322
|
+
reset = () => {
|
323
|
+
this.disable();
|
324
|
+
this.removeAllListener();
|
325
|
+
this.ignoredHosts = undefined;
|
326
|
+
this.ignoredUrls = undefined;
|
327
|
+
this.ignoredPatterns = undefined;
|
328
|
+
this.allRequests.clear();
|
329
|
+
};
|
330
|
+
}
|
331
|
+
|
332
|
+
const SafeHTTPInterceptor = createClassWithErrorHandling(HTTPInterceptor)
|
333
|
+
const httpInterceptor = new SafeHTTPInterceptor();
|
334
|
+
export {
|
335
|
+
type StartNetworkLoggingOptions,
|
336
|
+
httpInterceptor,
|
337
|
+
type EventName,
|
338
|
+
type RequestMethod,
|
339
|
+
};
|