@wutiange/log-listener-plugin 1.3.0-alpha.0 → 1.3.0-alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +2 -2
  3. package/dist/server.d.ts +10 -0
  4. package/dist/server.js +45 -0
  5. package/dist/server.js.map +1 -0
  6. package/dist/src/HTTPInterceptor.d.ts +48 -0
  7. package/dist/src/HTTPInterceptor.js +202 -0
  8. package/dist/src/HTTPInterceptor.js.map +1 -0
  9. package/dist/src/logPlugin.d.ts +19 -20
  10. package/dist/src/logPlugin.js +108 -122
  11. package/dist/src/logPlugin.js.map +1 -1
  12. package/package.json +47 -47
  13. package/src/HTTPInterceptor.ts +319 -0
  14. package/src/__tests__/console.test.ts +25 -25
  15. package/src/common.ts +4 -4
  16. package/src/logPlugin.ts +238 -229
  17. package/src/server.ts +66 -66
  18. package/src/utils.ts +47 -47
  19. package/dist/packages/network-logger/Logger.d.ts +0 -28
  20. package/dist/packages/network-logger/Logger.js +0 -192
  21. package/dist/packages/network-logger/Logger.js.map +0 -1
  22. package/dist/packages/network-logger/NetworkRequestInfo.d.ts +0 -36
  23. package/dist/packages/network-logger/NetworkRequestInfo.js +0 -129
  24. package/dist/packages/network-logger/NetworkRequestInfo.js.map +0 -1
  25. package/dist/packages/network-logger/constant.d.ts +0 -2
  26. package/dist/packages/network-logger/constant.js +0 -6
  27. package/dist/packages/network-logger/constant.js.map +0 -1
  28. package/dist/packages/network-logger/types.d.ts +0 -14
  29. package/dist/packages/network-logger/types.js +0 -3
  30. package/dist/packages/network-logger/types.js.map +0 -1
  31. package/dist/packages/network-logger/utils/debounce.d.ts +0 -2
  32. package/dist/packages/network-logger/utils/debounce.js +0 -20
  33. package/dist/packages/network-logger/utils/debounce.js.map +0 -1
  34. package/dist/packages/network-logger/utils/extractHost.d.ts +0 -2
  35. package/dist/packages/network-logger/utils/extractHost.js +0 -9
  36. package/dist/packages/network-logger/utils/extractHost.js.map +0 -1
  37. package/dist/packages/network-logger/utils/fromEntries.d.ts +0 -2
  38. package/dist/packages/network-logger/utils/fromEntries.js +0 -8
  39. package/dist/packages/network-logger/utils/fromEntries.js.map +0 -1
  40. package/dist/packages/network-logger/utils/logger.d.ts +0 -1
  41. package/dist/packages/network-logger/utils/logger.js +0 -6
  42. package/dist/packages/network-logger/utils/logger.js.map +0 -1
  43. package/dist/src/CompatibilityManager.d.ts +0 -27
  44. package/dist/src/CompatibilityManager.js +0 -74
  45. package/dist/src/CompatibilityManager.js.map +0 -1
  46. package/dist/src/console.d.ts +0 -1
  47. package/dist/src/console.js +0 -20
  48. package/dist/src/console.js.map +0 -1
  49. package/dist/src/fetch.d.ts +0 -1
  50. package/dist/src/fetch.js +0 -48
  51. package/dist/src/fetch.js.map +0 -1
  52. package/dist/src/index.d.ts +0 -2
  53. package/dist/src/index.js +0 -8
  54. package/dist/src/index.js.map +0 -1
  55. package/packages/network-logger/Logger.ts +0 -274
  56. package/packages/network-logger/NetworkRequestInfo.ts +0 -133
  57. package/packages/network-logger/constant.ts +0 -3
  58. package/packages/network-logger/types.ts +0 -36
  59. package/packages/network-logger/utils/debounce.ts +0 -21
  60. package/packages/network-logger/utils/extractHost.ts +0 -7
  61. package/packages/network-logger/utils/fromEntries.ts +0 -7
  62. package/packages/network-logger/utils/logger.ts +0 -2
  63. package/src/CompatibilityManager.ts +0 -64
package/src/logPlugin.ts CHANGED
@@ -1,229 +1,238 @@
1
- import Server from './server';
2
- import Logger from '../packages/network-logger/Logger';
3
- import NetworkRequestInfo from '../packages/network-logger/NetworkRequestInfo';
4
- import { extractDomain } from './utils';
5
- import CompatibilityManager from './CompatibilityManager';
6
-
7
- class LogPlugin {
8
- private server: Server | null = null;
9
- private baseData: Record<string, any> = {};
10
- private timeout: number | null = null;
11
- private networkLogger = new Logger();
12
- private host = '';
13
- private isAuto = false
14
-
15
- auto() {
16
- if (this.host) {
17
- this.startRecordNetwork();
18
- this.startRecordLog();
19
- }
20
- this.isAuto = true
21
- }
22
-
23
- unAuto() {
24
- this.stopRecordLog()
25
- this.networkLogger.disableXHRInterception()
26
- this.isAuto = false
27
- }
28
-
29
- startRecordLog() {
30
- const common = require('./common')
31
- console.log = (...data: any[]) => {
32
- this.log(...data);
33
- common.log(...data);
34
- };
35
-
36
- console.warn = (...data: any[]) => {
37
- this.warn(...data);
38
- common.warn(...data);
39
- };
40
-
41
- console.error = (...data: any[]) => {
42
- this.error(...data);
43
- common.error(...data);
44
- };
45
- }
46
-
47
- stopRecordLog() {
48
- const common = require('./common')
49
- console.log = common.log
50
- console.warn = common.warn
51
- console.error = common.error
52
- }
53
-
54
- startRecordNetwork() {
55
- this.networkLogger.setCallback(async (data: NetworkRequestInfo[]) => {
56
- const sendData = await CompatibilityManager.interceptionToNetwork(data);
57
- sendData.forEach(e => {
58
- this.server?.network({
59
- ...this.baseData,
60
- ...e
61
- });
62
- })
63
- });
64
-
65
- this.networkLogger.enableXHRInterception({
66
- ignoredHosts: [extractDomain(this.host)],
67
- });
68
- }
69
-
70
- setBaseUrl(url: string) {
71
- if (!url?.trim()) {
72
- this.networkLogger.disableXHRInterception()
73
- this.stopRecordLog()
74
- return
75
- }
76
- this.host = url.includes("http") ? url : `http://${url}`;
77
- if (this.server) {
78
- this.server.updateUrl(url);
79
- } else {
80
- this.server = new Server(url);
81
- }
82
- if (this.isAuto) {
83
- this.startRecordNetwork();
84
- this.startRecordLog()
85
- }
86
- }
87
-
88
- /**
89
- * @deprecated 不需要手动上报,日志插件会自动收集日志
90
- */
91
- setTimeout(timeout: number) {
92
- if (typeof timeout === 'number') {
93
- this.timeout = timeout;
94
- this.server?.updateTimeout(this.timeout);
95
- }
96
- }
97
-
98
- /**
99
- * @deprecated 不需要手动上报,日志插件会自动收集日志
100
- */
101
- getTimeout() {
102
- if (typeof this.timeout === 'number') {
103
- return this.timeout;
104
- }
105
- return null;
106
- }
107
-
108
- setBaseData(data: Record<string, any> = {}) {
109
- this.baseData = data;
110
- }
111
-
112
- private _log(level: string, tag: string, ...data: any[]) {
113
- const sendData = {
114
- ...this.baseData,
115
- message: data,
116
- tag,
117
- level: level ?? 'log',
118
- createTime: Date.now(),
119
- };
120
- this.server?.log(sendData);
121
- }
122
-
123
- tag(tag: string, ...data: any[]) {
124
- this._log('log', tag, ...data);
125
- }
126
-
127
- log(...data: any[]) {
128
- this._log('log', 'default', ...data);
129
- }
130
-
131
- warn(...data: any[]) {
132
- this._log('warn', 'default', ...data);
133
- }
134
-
135
- error(...data: any[]) {
136
- this._log('error', 'default', ...data);
137
- }
138
-
139
- /**
140
- * @deprecated 不需要手动上报,日志插件会自动收集日志
141
- */
142
- async uniqueReq(
143
- uniqueId: string | undefined,
144
- input: RequestInfo | URL,
145
- init?: RequestInit
146
- ) {
147
- let url: string | null = null;
148
- let method = init?.method ?? 'get';
149
- let headers = init?.headers;
150
- let body = init?.body;
151
- if (input instanceof Request) {
152
- url = input.url;
153
- method = input.method ?? 'get';
154
- headers = (input.headers as Record<string, any>).map;
155
- body = input.body;
156
- } else if (input instanceof URL) {
157
- url = input.href;
158
- } else {
159
- url = input;
160
- }
161
- return this.server?.network({
162
- ...this.baseData,
163
- url,
164
- id: uniqueId,
165
- method,
166
- headers,
167
- body,
168
- createTime: Date.now(),
169
- });
170
- }
171
-
172
- private async _res(uniqueId?: string, id?: number, response?: Response) {
173
- const body = await response?.text();
174
- return this.server?.network({
175
- ...this.baseData,
176
- headers: (response?.headers as Record<string, any>).map,
177
- body,
178
- requestId: uniqueId ?? Number(id),
179
- statusCode: response?.status,
180
- endTime: Date.now(),
181
- });
182
- }
183
-
184
- /**
185
- * @deprecated 不需要手动上报,日志插件会自动收集日志
186
- */
187
- async resTimeout(uniqueId: string) {
188
- return this.server?.network({
189
- ...this.baseData,
190
- isTimeout: true,
191
- requestId: uniqueId,
192
- });
193
- }
194
-
195
- /**
196
- * @deprecated 不需要手动上报,日志插件会自动收集日志
197
- */
198
- async resResponseError(uniqueId: string) {
199
- return this.server?.network({
200
- ...this.baseData,
201
- isResponseError: true,
202
- requestId: uniqueId,
203
- });
204
- }
205
-
206
- /**
207
- * @deprecated 不需要手动上报,日志插件会自动收集日志
208
- */
209
- async uniqueRes(uniqueId: string, response?: Response) {
210
- return this._res(uniqueId, undefined, response);
211
- }
212
-
213
- /**
214
- * @deprecated 不需要手动上报,日志插件会自动收集日志
215
- */
216
- async req(input: RequestInfo | URL, init?: RequestInit) {
217
- return this.uniqueReq(undefined, input, init);
218
- }
219
-
220
- /**
221
- * @deprecated 不需要手动上报,日志插件会自动收集日志
222
- */
223
- async res(id: number, response?: Response) {
224
- return this._res(undefined, id, response);
225
- }
226
- }
227
- const logPlugin = new LogPlugin();
228
- export { LogPlugin };
229
- export default logPlugin;
1
+ import Server from './server';
2
+ import { extractDomain } from './utils';
3
+ import { httpInterceptor } from './HTTPInterceptor';
4
+
5
+ class LogPlugin {
6
+ private server: Server | null = null;
7
+ private baseData: Record<string, any> = {};
8
+ private timeout: number | null = null;
9
+ private host = '';
10
+ private isAuto = false
11
+
12
+ auto = () => {
13
+ if (this.host) {
14
+ this.startRecordNetwork();
15
+ this.startRecordLog();
16
+ }
17
+ this.isAuto = true
18
+ }
19
+
20
+ unAuto = () => {
21
+ this.stopRecordLog()
22
+ httpInterceptor.disable()
23
+ httpInterceptor.removeAllListener()
24
+ this.isAuto = false
25
+ }
26
+
27
+ startRecordLog = () => {
28
+ const common = require('./common')
29
+ console.log = (...data: any[]) => {
30
+ this.log(...data);
31
+ common.log(...data);
32
+ };
33
+
34
+ console.warn = (...data: any[]) => {
35
+ this.warn(...data);
36
+ common.warn(...data);
37
+ };
38
+
39
+ console.error = (...data: any[]) => {
40
+ this.error(...data);
41
+ common.error(...data);
42
+ };
43
+ }
44
+
45
+ stopRecordLog = () => {
46
+ const common = require('./common')
47
+ console.log = common.log
48
+ console.warn = common.warn
49
+ console.error = common.error
50
+ }
51
+
52
+ startRecordNetwork = () => {
53
+ httpInterceptor.addListener("send", (data) => {
54
+ this.server?.network({
55
+ ...this.baseData,
56
+ url: data.url,
57
+ id: data.id,
58
+ method: data.method,
59
+ headers: data.requestHeaders,
60
+ body: data.requestData,
61
+ createTime: data.startTime
62
+ })
63
+ })
64
+ httpInterceptor.addListener("response", (data) => {
65
+ this.server?.network({
66
+ ...this.baseData,
67
+ headers: data.responseHeaders,
68
+ body: data.responseData,
69
+ requestId: data.id,
70
+ statusCode: data.status,
71
+ endTime: data.endTime
72
+ })
73
+ })
74
+ httpInterceptor.enable({
75
+ ignoredHosts: [extractDomain(this.host)]
76
+ })
77
+ }
78
+
79
+ setBaseUrl = (url: string) => {
80
+ if (!url?.trim()) {
81
+ httpInterceptor.disable()
82
+ this.stopRecordLog()
83
+ return
84
+ }
85
+ this.host = url.includes("http") ? url : `http://${url}`;
86
+ if (this.server) {
87
+ this.server.updateUrl(url);
88
+ } else {
89
+ this.server = new Server(url);
90
+ }
91
+ if (this.isAuto) {
92
+ this.startRecordNetwork();
93
+ this.startRecordLog()
94
+ }
95
+ }
96
+
97
+ /**
98
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
99
+ */
100
+ setTimeout = (timeout: number) => {
101
+ if (typeof timeout === 'number') {
102
+ this.timeout = timeout;
103
+ this.server?.updateTimeout(this.timeout);
104
+ }
105
+ }
106
+
107
+ /**
108
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
109
+ */
110
+ getTimeout = () => {
111
+ if (typeof this.timeout === 'number') {
112
+ return this.timeout;
113
+ }
114
+ return null;
115
+ }
116
+
117
+ setBaseData = (data: Record<string, any> = {}) => {
118
+ this.baseData = data;
119
+ }
120
+
121
+ private _log = (level: string, tag: string, ...data: any[]) => {
122
+ const sendData = {
123
+ ...this.baseData,
124
+ message: data,
125
+ tag,
126
+ level: level ?? 'log',
127
+ createTime: Date.now(),
128
+ };
129
+ this.server?.log(sendData);
130
+ }
131
+
132
+ tag = (tag: string, ...data: any[]) => {
133
+ this._log('log', tag, ...data);
134
+ }
135
+
136
+ log = (...data: any[]) => {
137
+ this._log('log', 'default', ...data);
138
+ }
139
+
140
+ warn = (...data: any[]) => {
141
+ this._log('warn', 'default', ...data);
142
+ }
143
+
144
+ error = (...data: any[]) => {
145
+ this._log('error', 'default', ...data);
146
+ }
147
+
148
+ /**
149
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
150
+ */
151
+ uniqueReq = async (
152
+ uniqueId: string | undefined,
153
+ input: RequestInfo | URL,
154
+ init?: RequestInit
155
+ ) => {
156
+ let url: string | null = null;
157
+ let method = init?.method ?? 'get';
158
+ let headers = init?.headers;
159
+ let body = init?.body;
160
+ if (input instanceof Request) {
161
+ url = input.url;
162
+ method = input.method ?? 'get';
163
+ headers = (input.headers as Record<string, any>).map;
164
+ body = input.body;
165
+ } else if (input instanceof URL) {
166
+ url = input.href;
167
+ } else {
168
+ url = input;
169
+ }
170
+ return this.server?.network({
171
+ ...this.baseData,
172
+ url,
173
+ id: uniqueId,
174
+ method,
175
+ headers,
176
+ body,
177
+ createTime: Date.now(),
178
+ });
179
+ }
180
+
181
+ private _res = async (uniqueId?: string, id?: number, response?: Response) => {
182
+ const body = await response?.text();
183
+ return this.server?.network({
184
+ ...this.baseData,
185
+ headers: (response?.headers as Record<string, any>).map,
186
+ body,
187
+ requestId: uniqueId ?? Number(id),
188
+ statusCode: response?.status,
189
+ endTime: Date.now(),
190
+ });
191
+ }
192
+
193
+ /**
194
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
195
+ */
196
+ resTimeout = async (uniqueId: string) => {
197
+ return this.server?.network({
198
+ ...this.baseData,
199
+ isTimeout: true,
200
+ requestId: uniqueId,
201
+ });
202
+ }
203
+
204
+ /**
205
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
206
+ */
207
+ resResponseError = async (uniqueId: string) => {
208
+ return this.server?.network({
209
+ ...this.baseData,
210
+ isResponseError: true,
211
+ requestId: uniqueId,
212
+ });
213
+ }
214
+
215
+ /**
216
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
217
+ */
218
+ uniqueRes = async (uniqueId: string, response?: Response) => {
219
+ return this._res(uniqueId, undefined, response);
220
+ }
221
+
222
+ /**
223
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
224
+ */
225
+ req = async (input: RequestInfo | URL, init?: RequestInit) => {
226
+ return this.uniqueReq(undefined, input, init);
227
+ }
228
+
229
+ /**
230
+ * @deprecated 不需要手动上报,日志插件会自动收集日志
231
+ */
232
+ res = async (id: number, response?: Response) => {
233
+ return this._res(undefined, id, response);
234
+ }
235
+ }
236
+ const logPlugin = new LogPlugin();
237
+ export { LogPlugin };
238
+ export default logPlugin;
package/src/server.ts CHANGED
@@ -1,66 +1,66 @@
1
- import {hasPort, sleep} from './utils';
2
- const DEFAULT_PORT = 27751
3
- class Server {
4
- private baseUrl = '';
5
- private timeout: number;
6
-
7
- constructor(url: string, timeout: number = 3000) {
8
- this.updateUrl(url);
9
- this.timeout = timeout;
10
- }
11
-
12
- updateTimeout(timeout = 3000) {
13
- this.timeout = timeout;
14
- }
15
-
16
- private getPort() {
17
- if (hasPort(this.baseUrl)) {
18
- return ''
19
- }
20
- return DEFAULT_PORT;
21
- }
22
-
23
- private async send(path: string, data: Record<string, any>) {
24
- try {
25
- if (!this.baseUrl) {
26
- return null;
27
- }
28
- const common = require('./common');
29
- const result = await Promise.race([
30
- common.tempFetch(`${this.baseUrl}:${this.getPort()}/${path}`, {
31
- method: 'POST',
32
- headers: {
33
- 'Content-Type': 'application/json;charset=utf-8',
34
- },
35
- body: JSON.stringify(data, (_, val) => {
36
- if (val instanceof Error) {
37
- return val.toString();
38
- }
39
- return val;
40
- }),
41
- }),
42
- sleep(this.timeout, true),
43
- ]);
44
- if (result instanceof Response) {
45
- return result.text();
46
- }
47
- return null;
48
- } catch (error) {
49
- return null;
50
- }
51
- }
52
-
53
- updateUrl(url: string) {
54
- this.baseUrl = url;
55
- }
56
-
57
- async log(data: Record<string, any>) {
58
- return this.send('log', data);
59
- }
60
-
61
- async network(data: Record<string, any>) {
62
- return this.send('network', data);
63
- }
64
- }
65
-
66
- export default Server;
1
+ import {hasPort, sleep} from './utils';
2
+ const DEFAULT_PORT = 27751
3
+ class Server {
4
+ private baseUrl = '';
5
+ private timeout: number;
6
+
7
+ constructor(url: string, timeout: number = 3000) {
8
+ this.updateUrl(url);
9
+ this.timeout = timeout;
10
+ }
11
+
12
+ updateTimeout(timeout = 3000) {
13
+ this.timeout = timeout;
14
+ }
15
+
16
+ private getPort() {
17
+ if (hasPort(this.baseUrl)) {
18
+ return ''
19
+ }
20
+ return DEFAULT_PORT;
21
+ }
22
+
23
+ private async send(path: string, data: Record<string, any>) {
24
+ try {
25
+ if (!this.baseUrl) {
26
+ return null;
27
+ }
28
+ const common = require('./common');
29
+ const result = await Promise.race([
30
+ common.tempFetch(`${this.baseUrl}:${this.getPort()}/${path}`, {
31
+ method: 'POST',
32
+ headers: {
33
+ 'Content-Type': 'application/json;charset=utf-8',
34
+ },
35
+ body: JSON.stringify(data, (_, val) => {
36
+ if (val instanceof Error) {
37
+ return val.toString();
38
+ }
39
+ return val;
40
+ }),
41
+ }),
42
+ sleep(this.timeout, true),
43
+ ]);
44
+ if (result instanceof Response) {
45
+ return result.text();
46
+ }
47
+ return null;
48
+ } catch (error) {
49
+ return null;
50
+ }
51
+ }
52
+
53
+ updateUrl(url: string) {
54
+ this.baseUrl = url;
55
+ }
56
+
57
+ async log(data: Record<string, any>) {
58
+ return this.send('log', data);
59
+ }
60
+
61
+ async network(data: Record<string, any>) {
62
+ return this.send('network', data);
63
+ }
64
+ }
65
+
66
+ export default Server;