@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.
Files changed (38) hide show
  1. package/README.md +130 -24
  2. package/dist/src/HTTPInterceptor.d.ts +1 -0
  3. package/dist/src/HTTPInterceptor.js +12 -9
  4. package/dist/src/HTTPInterceptor.js.map +1 -1
  5. package/dist/src/Server.d.ts +13 -6
  6. package/dist/src/Server.js +119 -41
  7. package/dist/src/Server.js.map +1 -1
  8. package/dist/src/__mocks__/react-native/Libraries/Blob/FileReader.js +0 -1
  9. package/dist/src/__mocks__/react-native/Libraries/Blob/FileReader.js.map +1 -1
  10. package/dist/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js +0 -1
  11. package/dist/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js.map +1 -1
  12. package/dist/src/__tests__/Server.test.js +76 -115
  13. package/dist/src/__tests__/Server.test.js.map +1 -1
  14. package/dist/src/common.d.ts +19 -10
  15. package/dist/src/common.js +63 -4
  16. package/dist/src/common.js.map +1 -1
  17. package/dist/src/logPlugin.d.ts +12 -9
  18. package/dist/src/logPlugin.js +87 -82
  19. package/dist/src/logPlugin.js.map +1 -1
  20. package/dist/src/logger.d.ts +6 -0
  21. package/dist/src/logger.js +16 -0
  22. package/dist/src/logger.js.map +1 -0
  23. package/dist/src/utils.js +12 -7
  24. package/dist/src/utils.js.map +1 -1
  25. package/index.ts +3 -0
  26. package/package.json +18 -12
  27. package/src/HTTPInterceptor.ts +339 -0
  28. package/src/Server.ts +164 -0
  29. package/src/__mocks__/react-native/Libraries/Blob/FileReader.js +45 -0
  30. package/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js +39 -0
  31. package/src/__tests__/HTTPInterceptor.test.ts +322 -0
  32. package/src/__tests__/Server.test.ts +150 -0
  33. package/src/__tests__/utils.test.ts +113 -0
  34. package/src/common.ts +70 -0
  35. package/src/logPlugin.ts +224 -0
  36. package/src/logger.ts +15 -0
  37. package/src/utils.ts +112 -0
  38. package/tsconfig.json +27 -0
@@ -14,141 +14,102 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const Server_1 = __importDefault(require("../Server"));
16
16
  const utils_1 = require("../utils");
17
- jest.mock('../utils', () => ({
18
- hasPort: jest.fn(),
19
- sleep: jest.fn().mockImplementation(() => new Promise(() => { })),
20
- }));
21
- jest.mock('../common', () => ({
22
- tempFetch: jest.fn(),
23
- }));
24
- class MockResponse {
25
- constructor(body) {
26
- this.body = body;
27
- }
28
- text() {
29
- return Promise.resolve(this.body);
30
- }
31
- static [Symbol.hasInstance](instance) {
32
- return instance && typeof instance.text === 'function';
33
- }
34
- }
35
- global.Response = MockResponse;
17
+ jest.mock('../utils', () => {
18
+ const actual = jest.requireActual('../utils');
19
+ return Object.assign(Object.assign({}, actual), { sleep: jest.fn() });
20
+ });
21
+ jest.mock('react-native-zeroconf', () => undefined, { virtual: true });
22
+ global.fetch = jest.fn();
36
23
  describe('Server', () => {
37
24
  let server;
38
- const mockUrl = 'http://example.com';
39
- const mockTimeout = 5000;
40
25
  beforeEach(() => {
41
- server = new Server_1.default(mockUrl, mockTimeout);
42
26
  jest.clearAllMocks();
27
+ global.fetch.mockReset();
43
28
  });
44
- describe('constructor', () => {
45
- it('should initialize with correct url and timeout', () => {
46
- expect(server['baseUrl']).toBe(mockUrl);
47
- expect(server['timeout']).toBe(mockTimeout);
48
- });
49
- it('should use default timeout if not provided', () => {
50
- const defaultServer = new Server_1.default(mockUrl);
51
- expect(defaultServer['timeout']).toBe(3000);
29
+ describe('Constructor and URL Management', () => {
30
+ it('should initialize with default values', () => {
31
+ server = new Server_1.default();
32
+ expect(server.getUrls()).toEqual([]);
52
33
  });
53
- });
54
- describe('updateTimeout', () => {
55
- it('should update the timeout', () => {
56
- server.updateTimeout(10000);
57
- expect(server['timeout']).toBe(10000);
34
+ it('should initialize with custom URL', () => {
35
+ server = new Server_1.default('localhost:8080');
36
+ expect(server.getUrls()).toEqual(['http://localhost:8080']);
58
37
  });
59
- it('should use default timeout if not provided', () => {
60
- server.updateTimeout();
61
- expect(server['timeout']).toBe(3000);
38
+ it('should handle URLs with and without http prefix', () => {
39
+ server = new Server_1.default();
40
+ server.updateUrl('localhost:8080');
41
+ expect(server.getUrls()).toEqual(['http://localhost:8080']);
42
+ server.updateUrl('http://localhost:8080');
43
+ expect(server.getUrls()).toEqual(['http://localhost:8080']);
62
44
  });
63
45
  });
64
- describe('getPort', () => {
65
- it('should return empty string if url has port', () => {
66
- utils_1.hasPort.mockReturnValue(true);
67
- expect(server['getPort']()).toBe('');
46
+ describe('ZeroConf Handling', () => {
47
+ it('should handle case when zeroconf is not available', () => {
48
+ server = new Server_1.default();
49
+ const mockListener = jest.fn();
50
+ server.addUrlsListener(mockListener);
51
+ expect(mockListener).not.toHaveBeenCalled();
68
52
  });
69
- it('should return default port if url has no port', () => {
70
- utils_1.hasPort.mockReturnValue(false);
71
- expect(server['getPort']()).toBe(27751);
53
+ it('should handle case when zeroconf is available', () => {
54
+ const mockZeroconfInstance = {
55
+ on: jest.fn(),
56
+ scan: jest.fn()
57
+ };
58
+ jest.doMock('react-native-zeroconf', () => ({
59
+ __esModule: true,
60
+ default: jest.fn(() => mockZeroconfInstance)
61
+ }), { virtual: true });
62
+ server = new Server_1.default();
63
+ const mockListener = jest.fn();
64
+ server.addUrlsListener(mockListener);
65
+ expect(mockListener).not.toHaveBeenCalled();
72
66
  });
73
67
  });
74
- describe('getUrl', () => {
75
- it('should return correct url with port', () => {
76
- utils_1.hasPort.mockReturnValue(false);
77
- expect(server.getUrl()).toBe(`${mockUrl}:27751`);
78
- });
79
- it('should return correct url without port', () => {
80
- utils_1.hasPort.mockReturnValue(true);
81
- expect(server.getUrl()).toBe(`${mockUrl}:`);
68
+ describe('Data Sending', () => {
69
+ beforeEach(() => {
70
+ server = new Server_1.default('localhost:8080');
71
+ global.fetch.mockImplementation(() => Promise.resolve({ ok: true }));
82
72
  });
83
- });
84
- describe('send', () => {
85
- const mockPath = 'test';
86
- const mockData = { key: 'value' };
87
- const mockResponse = new MockResponse('success');
88
- it('should return null if baseUrl is empty', () => __awaiter(void 0, void 0, void 0, function* () {
89
- server.updateUrl('');
90
- const result = yield server['send'](mockPath, mockData);
91
- expect(result).toBeNull();
92
- }));
93
- it('should return response text on successful fetch', () => __awaiter(void 0, void 0, void 0, function* () {
94
- const common = require('../common');
95
- const mockResponseText = 'success';
96
- const mockResponseObject = new MockResponse(mockResponseText);
97
- common.tempFetch.mockResolvedValue(mockResponseObject);
98
- const result = yield server['send'](mockPath, mockData);
99
- expect(result).toBe(mockResponseText);
100
- expect(common.tempFetch).toHaveBeenCalledWith(`${server.getUrl()}/${mockPath}`, expect.objectContaining({
73
+ it('should send log data', () => __awaiter(void 0, void 0, void 0, function* () {
74
+ const testData = { message: 'test log' };
75
+ yield server.log(testData);
76
+ expect(global.fetch).toHaveBeenCalledWith('http://localhost:8080/log', expect.objectContaining({
101
77
  method: 'POST',
102
- headers: { 'Content-Type': 'application/json;charset=utf-8' },
103
- body: JSON.stringify(mockData),
78
+ headers: {
79
+ 'Content-Type': 'application/json;charset=utf-8'
80
+ },
81
+ body: expect.any(String)
104
82
  }));
105
83
  }));
106
- it('should return null on timeout', () => __awaiter(void 0, void 0, void 0, function* () {
107
- const common = require('../common');
108
- common.tempFetch.mockImplementation(() => new Promise(() => { }));
109
- utils_1.sleep.mockResolvedValue(true);
110
- const result = yield server['send'](mockPath, mockData);
111
- expect(result).toBeNull();
112
- }));
113
- it('should handle Error objects in JSON stringification', () => __awaiter(void 0, void 0, void 0, function* () {
114
- const common = require('../common');
115
- common.tempFetch.mockResolvedValue(mockResponse);
116
- const dataWithError = { error: new Error('Test error') };
117
- yield server['send'](mockPath, dataWithError);
118
- expect(common.tempFetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
119
- body: expect.stringContaining('Error: Test error'),
84
+ it('should send network data', () => __awaiter(void 0, void 0, void 0, function* () {
85
+ const testData = { url: 'test.com' };
86
+ yield server.network(testData);
87
+ expect(global.fetch).toHaveBeenCalledWith('http://localhost:8080/network', expect.objectContaining({
88
+ method: 'POST',
89
+ headers: {
90
+ 'Content-Type': 'application/json;charset=utf-8'
91
+ },
92
+ body: expect.any(String)
120
93
  }));
121
94
  }));
122
- it('should return null on fetch error', () => __awaiter(void 0, void 0, void 0, function* () {
123
- const common = require('../common');
124
- common.tempFetch.mockRejectedValue(new Error('Fetch error'));
125
- const result = yield server['send'](mockPath, mockData);
126
- expect(result).toBeNull();
127
- }));
128
- });
129
- describe('updateUrl', () => {
130
- it('should update the baseUrl', () => {
131
- const newUrl = 'http://newexample.com';
132
- server.updateUrl(newUrl);
133
- expect(server['baseUrl']).toBe(newUrl);
134
- });
135
- });
136
- describe('log', () => {
137
- it('should call send with correct path and data', () => __awaiter(void 0, void 0, void 0, function* () {
138
- const sendSpy = jest.spyOn(server, 'send').mockResolvedValue('log result');
139
- const logData = { message: 'Test log' };
140
- const result = yield server.log(logData);
141
- expect(sendSpy).toHaveBeenCalledWith('log', logData);
142
- expect(result).toBe('log result');
95
+ it('should handle timeout', () => __awaiter(void 0, void 0, void 0, function* () {
96
+ server.updateTimeout(100);
97
+ utils_1.sleep.mockImplementation(() => Promise.reject(new Error('Timeout')));
98
+ const testData = { message: 'test' };
99
+ yield server.log(testData);
100
+ expect(global.fetch).toHaveBeenCalled();
101
+ expect(utils_1.sleep).toHaveBeenCalledWith(100, true);
143
102
  }));
144
103
  });
145
- describe('network', () => {
146
- it('should call send with correct path and data', () => __awaiter(void 0, void 0, void 0, function* () {
147
- const sendSpy = jest.spyOn(server, 'send').mockResolvedValue('network result');
148
- const networkData = { url: 'http://test.com', status: 200 };
149
- const result = yield server.network(networkData);
150
- expect(sendSpy).toHaveBeenCalledWith('network', networkData);
151
- expect(result).toBe('network result');
104
+ describe('Base Data Management', () => {
105
+ it('should update base data', () => __awaiter(void 0, void 0, void 0, function* () {
106
+ server = new Server_1.default('localhost:8080');
107
+ const baseData = { userId: '123' };
108
+ server.updateBaseData(baseData);
109
+ yield server.log({ message: 'test' });
110
+ expect(global.fetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
111
+ body: expect.stringContaining('"userId":"123"')
112
+ }));
152
113
  }));
153
114
  });
154
115
  });
@@ -1 +1 @@
1
- {"version":3,"file":"Server.test.js","sourceRoot":"","sources":["../../../src/__tests__/Server.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,uDAA+B;AAC/B,oCAA0C;AAG1C,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC;IAC3B,OAAO,EAAE,IAAI,CAAC,EAAE,EAAE;IAClB,KAAK,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,kBAAkB,CAAC,GAAG,EAAE,CAAC,IAAI,OAAO,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;CACjE,CAAC,CAAC,CAAC;AAGJ,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;IAC5B,SAAS,EAAE,IAAI,CAAC,EAAE,EAAE;CACrB,CAAC,CAAC,CAAC;AAGJ,MAAM,YAAY;IAGhB,YAAY,IAAY;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,IAAI;QACF,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,MAAM,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,QAAa;QACvC,OAAO,QAAQ,IAAI,OAAO,QAAQ,CAAC,IAAI,KAAK,UAAU,CAAC;IACzD,CAAC;CACF;AAGD,MAAM,CAAC,QAAQ,GAAG,YAAmB,CAAC;AAEtC,QAAQ,CAAC,QAAQ,EAAE,GAAG,EAAE;IACtB,IAAI,MAAc,CAAC;IACnB,MAAM,OAAO,GAAG,oBAAoB,CAAC;IACrC,MAAM,WAAW,GAAG,IAAI,CAAC;IAEzB,UAAU,C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C;aAChD,CAAC,CACH,CAAC;QACJ,CAAC,CAAA,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -1,11 +1,20 @@
1
- export declare const log: {
2
- (...data: any[]): void;
3
- (message?: any, ...optionalParams: any[]): void;
4
- }, warn: {
5
- (...data: any[]): void;
6
- (message?: any, ...optionalParams: any[]): void;
7
- }, error: {
8
- (...data: any[]): void;
9
- (message?: any, ...optionalParams: any[]): void;
1
+ export declare const URLS_KEY = "log-listener-plugin-urls$$key";
2
+ export declare const DEFAULT_TIMEOUT = 3000;
3
+ export declare const LOG_KEY = "[@wutiange/log-listener-plugin \u65E5\u5FD7]";
4
+ export declare enum Level {
5
+ LOG = "log",
6
+ WARN = "warn",
7
+ ERROR = "error"
8
+ }
9
+ export declare enum Tag {
10
+ LOG_PLUGIN_INTERNAL_ERROR = "log-plugin-internal-error",
11
+ DEFAULT = "default"
12
+ }
13
+ export declare const getBaseData: () => Record<string, string>;
14
+ export declare const getDefaultStorage: () => Storage;
15
+ export declare const getErrMsg: (error: any) => {
16
+ message: string[];
17
+ tag: Tag;
18
+ level: Level;
19
+ createTime: number;
10
20
  };
11
- export declare const tempFetch: typeof fetch;
@@ -1,7 +1,66 @@
1
1
  "use strict";
2
- var _a;
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
3
5
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.tempFetch = exports.error = exports.warn = exports.log = void 0;
5
- _a = [console.log, console.warn, console.error], exports.log = _a[0], exports.warn = _a[1], exports.error = _a[2];
6
- exports.tempFetch = global.fetch;
6
+ exports.getErrMsg = exports.getDefaultStorage = exports.getBaseData = exports.Tag = exports.Level = exports.LOG_KEY = exports.DEFAULT_TIMEOUT = exports.URLS_KEY = void 0;
7
+ const logger_1 = __importDefault(require("./logger"));
8
+ exports.URLS_KEY = 'log-listener-plugin-urls$$key';
9
+ exports.DEFAULT_TIMEOUT = 3000;
10
+ exports.LOG_KEY = '[@wutiange/log-listener-plugin 日志]';
11
+ var Level;
12
+ (function (Level) {
13
+ Level["LOG"] = "log";
14
+ Level["WARN"] = "warn";
15
+ Level["ERROR"] = "error";
16
+ })(Level || (exports.Level = Level = {}));
17
+ var Tag;
18
+ (function (Tag) {
19
+ Tag["LOG_PLUGIN_INTERNAL_ERROR"] = "log-plugin-internal-error";
20
+ Tag["DEFAULT"] = "default";
21
+ })(Tag || (exports.Tag = Tag = {}));
22
+ const getDefaultDeviceINfo = () => {
23
+ try {
24
+ const { Platform } = require('react-native');
25
+ return Object.assign({ SystemName: Platform.OS, Version: Platform.Version }, Platform.constants);
26
+ }
27
+ catch (error) {
28
+ logger_1.default.warn(exports.LOG_KEY, '这个插件只能在 react-native 中使用');
29
+ return {};
30
+ }
31
+ };
32
+ const getBaseData = () => {
33
+ var _a;
34
+ try {
35
+ const DeviceInfo = (_a = require("react-native-device-info")) === null || _a === void 0 ? void 0 : _a.default;
36
+ return Object.assign({ Brand: DeviceInfo.getBrand(), Model: DeviceInfo.getModel(), AppVersion: DeviceInfo.getVersion(), Carrier: DeviceInfo.getCarrierSync(), Manufacturer: DeviceInfo.getManufacturerSync(), SystemName: DeviceInfo.getSystemName() }, getDefaultDeviceINfo());
37
+ }
38
+ catch (error) {
39
+ return getDefaultDeviceINfo();
40
+ }
41
+ };
42
+ exports.getBaseData = getBaseData;
43
+ const getDefaultStorage = () => {
44
+ var _a;
45
+ try {
46
+ const AsyncStorage = (_a = require("@react-native-async-storage/async-storage")) === null || _a === void 0 ? void 0 : _a.default;
47
+ return AsyncStorage;
48
+ }
49
+ catch (error) {
50
+ return null;
51
+ }
52
+ };
53
+ exports.getDefaultStorage = getDefaultStorage;
54
+ const getErrMsg = (error) => {
55
+ var _a;
56
+ return {
57
+ message: [
58
+ `${(_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error}--->这是@wutiange/log-listener-plugin内部错误,请提issue反馈,issue地址:https://github.com/wutiange/log-listener-plugin/issues`,
59
+ ],
60
+ tag: Tag.LOG_PLUGIN_INTERNAL_ERROR,
61
+ level: Level.ERROR,
62
+ createTime: Date.now(),
63
+ };
64
+ };
65
+ exports.getErrMsg = getErrMsg;
7
66
  //# sourceMappingURL=common.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/common.ts"],"names":[],"mappings":";;;;AAAa,KAAqB,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,KAAK,CAAC,EAA9D,WAAG,UAAE,YAAI,UAAE,aAAK,SAA+C;AAGhE,QAAA,SAAS,GAAG,MAAM,CAAC,KAAqB,CAAC"}
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/common.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAEjB,QAAA,QAAQ,GAAG,+BAA+B,CAAA;AAC1C,QAAA,eAAe,GAAG,IAAI,CAAA;AACtB,QAAA,OAAO,GAAG,oCAAoC,CAAA;AAC3D,IAAY,KAIX;AAJD,WAAY,KAAK;IACf,oBAAW,CAAA;IACX,sBAAa,CAAA;IACb,wBAAe,CAAA;AACjB,CAAC,EAJW,KAAK,qBAAL,KAAK,QAIhB;AAED,IAAY,GAGX;AAHD,WAAY,GAAG;IACb,8DAAuD,CAAA;IACvD,0BAAmB,CAAA;AACrB,CAAC,EAHW,GAAG,mBAAH,GAAG,QAGd;AAED,MAAM,oBAAoB,GAAG,GAAG,EAAE;IAChC,IAAI,CAAC;QACH,MAAM,EAAC,QAAQ,EAAC,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;QAC1C,uBACE,UAAU,EAAE,QAAQ,CAAC,EAAE,EACvB,OAAO,EAAE,QAAQ,CAAC,OAAO,IACtB,QAAQ,CAAC,SAAS,EACtB;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,gBAAM,CAAC,IAAI,CAAC,eAAO,EAAE,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AS,aAWrB"}
@@ -1,13 +1,23 @@
1
+ type Options = {
2
+ storage?: Storage;
3
+ timeout?: number;
4
+ testUrl?: string;
5
+ isAuto?: boolean;
6
+ baseData?: Record<string, any>;
7
+ };
1
8
  declare class LogPlugin {
2
9
  private server;
3
- private baseData;
4
10
  private timeout;
5
- private host;
6
11
  private isAuto;
12
+ private storage;
13
+ constructor();
14
+ private init;
15
+ config: ({ storage, timeout, testUrl, isAuto, baseData }: Options) => void;
7
16
  auto: () => void;
8
17
  unAuto: () => void;
9
18
  startRecordLog: () => void;
10
19
  stopRecordLog: () => void;
20
+ private handleIgnoredUrls;
11
21
  startRecordNetwork: () => void;
12
22
  setBaseUrl: (url: string) => void;
13
23
  setTimeout: (timeout: number) => void;
@@ -18,13 +28,6 @@ declare class LogPlugin {
18
28
  log: (...data: any[]) => void;
19
29
  warn: (...data: any[]) => void;
20
30
  error: (...data: any[]) => void;
21
- uniqueReq: (uniqueId: string | undefined, input: RequestInfo | URL, init?: RequestInit) => Promise<string>;
22
- private _res;
23
- resTimeout: (uniqueId: string) => Promise<string>;
24
- resResponseError: (uniqueId: string) => Promise<string>;
25
- uniqueRes: (uniqueId: string, response?: Response) => Promise<string>;
26
- req: (input: RequestInfo | URL, init?: RequestInit) => Promise<string>;
27
- res: (id: number, response?: Response) => Promise<string>;
28
31
  }
29
32
  declare const SafeLogPlugin: typeof LogPlugin;
30
33
  declare const logPlugin: LogPlugin;
@@ -16,18 +16,48 @@ exports.SafeLogPlugin = void 0;
16
16
  const Server_1 = __importDefault(require("./Server"));
17
17
  const utils_1 = require("./utils");
18
18
  const HTTPInterceptor_1 = require("./HTTPInterceptor");
19
+ const common_1 = require("./common");
20
+ const logger_1 = __importDefault(require("./logger"));
19
21
  class LogPlugin {
20
22
  constructor() {
21
23
  this.server = null;
22
- this.baseData = {};
23
24
  this.timeout = null;
24
- this.host = '';
25
25
  this.isAuto = false;
26
- this.auto = () => {
27
- if (this.host) {
28
- this.startRecordNetwork();
29
- this.startRecordLog();
26
+ this.storage = (0, common_1.getDefaultStorage)();
27
+ this.init = () => __awaiter(this, void 0, void 0, function* () {
28
+ this.server = new Server_1.default();
29
+ if (!this.storage) {
30
+ logger_1.default.warn(common_1.LOG_KEY, '你并没有设置 storage ,这会导致 App 杀死后可能需要重新加入日志系统才能收集日志数据,建议你设置 storage 。');
31
+ }
32
+ else {
33
+ const urlsStr = yield this.storage.getItem(common_1.URLS_KEY);
34
+ if (urlsStr) {
35
+ const urls = JSON.parse(urlsStr);
36
+ this.server.setBaseUrlObj(urls);
37
+ }
30
38
  }
39
+ this.server.addUrlsListener((_, urlsObj) => {
40
+ if (this.storage) {
41
+ this.storage.setItem(common_1.URLS_KEY, JSON.stringify(urlsObj));
42
+ }
43
+ HTTPInterceptor_1.httpInterceptor.setIgnoredUrls(this.handleIgnoredUrls());
44
+ });
45
+ });
46
+ this.config = ({ storage, timeout, testUrl, isAuto, baseData = {} }) => {
47
+ if (isAuto) {
48
+ this.auto();
49
+ }
50
+ else {
51
+ this.unAuto();
52
+ }
53
+ this.storage = storage !== null && storage !== void 0 ? storage : (0, common_1.getDefaultStorage)();
54
+ this.setTimeout(timeout !== null && timeout !== void 0 ? timeout : common_1.DEFAULT_TIMEOUT);
55
+ this.setBaseUrl(testUrl);
56
+ this.setBaseData(baseData);
57
+ };
58
+ this.auto = () => {
59
+ this.startRecordNetwork();
60
+ this.startRecordLog();
31
61
  this.isAuto = true;
32
62
  };
33
63
  this.unAuto = () => {
@@ -37,56 +67,69 @@ class LogPlugin {
37
67
  this.isAuto = false;
38
68
  };
39
69
  this.startRecordLog = () => {
40
- const common = require('./common');
41
70
  console.log = (...data) => {
71
+ logger_1.default.log(...data);
42
72
  this.log(...data);
43
- common.log(...data);
44
73
  };
45
74
  console.warn = (...data) => {
75
+ logger_1.default.warn(...data);
46
76
  this.warn(...data);
47
- common.warn(...data);
48
77
  };
49
78
  console.error = (...data) => {
79
+ logger_1.default.error(...data);
50
80
  this.error(...data);
51
- common.error(...data);
52
81
  };
53
82
  };
54
83
  this.stopRecordLog = () => {
55
- const common = require('./common');
56
- console.log = common.log;
57
- console.warn = common.warn;
58
- console.error = common.error;
84
+ console.log = logger_1.default.log;
85
+ console.warn = logger_1.default.warn;
86
+ console.error = logger_1.default.error;
59
87
  };
60
- this.startRecordNetwork = () => {
88
+ this.handleIgnoredUrls = () => {
61
89
  var _a, _b;
90
+ const urls = (_b = (_a = this.server) === null || _a === void 0 ? void 0 : _a.getUrls) === null || _b === void 0 ? void 0 : _b.call(_a);
91
+ let ignoredUrls = [];
92
+ if (urls === null || urls === void 0 ? void 0 : urls.length) {
93
+ ignoredUrls = urls.reduce((acc, url) => {
94
+ acc.push(`${url}/log`, `${url}/network`, `${url}/join`);
95
+ return acc;
96
+ }, []);
97
+ }
98
+ return ignoredUrls;
99
+ };
100
+ this.startRecordNetwork = () => {
62
101
  HTTPInterceptor_1.httpInterceptor.addListener("send", (data) => {
63
102
  var _a;
64
- (_a = this.server) === null || _a === void 0 ? void 0 : _a.network(Object.assign(Object.assign({}, this.baseData), { url: data.url, id: data.id, method: data.method, headers: data.requestHeaders, body: data.requestData, createTime: data.startTime }));
103
+ (_a = this.server) === null || _a === void 0 ? void 0 : _a.network({
104
+ url: data.url,
105
+ id: data.id,
106
+ method: data.method,
107
+ headers: data.requestHeaders,
108
+ body: data.requestData,
109
+ createTime: data.startTime
110
+ });
65
111
  });
66
112
  HTTPInterceptor_1.httpInterceptor.addListener("response", (data) => {
67
113
  var _a;
68
- (_a = this.server) === null || _a === void 0 ? void 0 : _a.network(Object.assign(Object.assign({}, this.baseData), { headers: data.responseHeaders, body: data.responseData, requestId: data.id, statusCode: data.status, endTime: data.endTime }));
114
+ (_a = this.server) === null || _a === void 0 ? void 0 : _a.network({
115
+ headers: data.responseHeaders,
116
+ body: data.responseData,
117
+ requestId: data.id,
118
+ statusCode: data.status,
119
+ endTime: data.endTime
120
+ });
69
121
  });
70
- const url = (_b = (_a = this.server) === null || _a === void 0 ? void 0 : _a.getUrl) === null || _b === void 0 ? void 0 : _b.call(_a);
71
- let ignoredUrls = [];
72
- if (url) {
73
- ignoredUrls = [`${url}/log`, `${url}/network`];
74
- }
75
- HTTPInterceptor_1.httpInterceptor.enable({ ignoredUrls });
122
+ HTTPInterceptor_1.httpInterceptor.enable({ ignoredUrls: this.handleIgnoredUrls() });
76
123
  };
77
124
  this.setBaseUrl = (url) => {
78
- if (!(url === null || url === void 0 ? void 0 : url.trim())) {
79
- HTTPInterceptor_1.httpInterceptor.disable();
80
- this.stopRecordLog();
81
- return;
82
- }
83
- this.host = url.includes("http") ? url : `http://${url}`;
125
+ const tempUrl = url === null || url === void 0 ? void 0 : url.trim();
84
126
  if (this.server) {
85
- this.server.updateUrl(url);
127
+ this.server.updateUrl(tempUrl);
86
128
  }
87
129
  else {
88
- this.server = new Server_1.default(url);
130
+ this.server = new Server_1.default(tempUrl);
89
131
  }
132
+ HTTPInterceptor_1.httpInterceptor.setIgnoredUrls(this.handleIgnoredUrls());
90
133
  if (this.isAuto) {
91
134
  this.startRecordNetwork();
92
135
  this.startRecordLog();
@@ -106,69 +149,31 @@ class LogPlugin {
106
149
  return null;
107
150
  };
108
151
  this.setBaseData = (data = {}) => {
109
- this.baseData = data;
152
+ this.server.updateBaseData(data);
110
153
  };
111
154
  this._log = (level, tag, ...data) => {
112
155
  var _a;
113
- const sendData = Object.assign(Object.assign({}, this.baseData), { message: data, tag, level: level !== null && level !== void 0 ? level : 'log', createTime: Date.now() });
156
+ const sendData = {
157
+ message: data,
158
+ tag,
159
+ level: level !== null && level !== void 0 ? level : 'log',
160
+ createTime: Date.now(),
161
+ };
114
162
  (_a = this.server) === null || _a === void 0 ? void 0 : _a.log(sendData);
115
163
  };
116
164
  this.tag = (tag, ...data) => {
117
- this._log('log', tag, ...data);
165
+ this._log(common_1.Level.LOG, tag, ...data);
118
166
  };
119
167
  this.log = (...data) => {
120
- this._log('log', 'default', ...data);
168
+ this._log(common_1.Level.LOG, common_1.Tag.DEFAULT, ...data);
121
169
  };
122
170
  this.warn = (...data) => {
123
- this._log('warn', 'default', ...data);
171
+ this._log(common_1.Level.WARN, common_1.Tag.DEFAULT, ...data);
124
172
  };
125
173
  this.error = (...data) => {
126
- this._log('error', 'default', ...data);
127
- };
128
- this.uniqueReq = (uniqueId, input, init) => __awaiter(this, void 0, void 0, function* () {
129
- var _a, _b, _c;
130
- let url = null;
131
- let method = (_a = init === null || init === void 0 ? void 0 : init.method) !== null && _a !== void 0 ? _a : 'get';
132
- let headers = init === null || init === void 0 ? void 0 : init.headers;
133
- let body = init === null || init === void 0 ? void 0 : init.body;
134
- if (input instanceof Request) {
135
- url = input.url;
136
- method = (_b = input.method) !== null && _b !== void 0 ? _b : 'get';
137
- headers = input.headers.map;
138
- body = input.body;
139
- }
140
- else if (input instanceof URL) {
141
- url = input.href;
142
- }
143
- else {
144
- url = input;
145
- }
146
- return (_c = this.server) === null || _c === void 0 ? void 0 : _c.network(Object.assign(Object.assign({}, this.baseData), { url, id: uniqueId, method,
147
- headers,
148
- body, createTime: Date.now() }));
149
- });
150
- this._res = (uniqueId, id, response) => __awaiter(this, void 0, void 0, function* () {
151
- var _d;
152
- const body = yield (response === null || response === void 0 ? void 0 : response.text());
153
- return (_d = this.server) === null || _d === void 0 ? void 0 : _d.network(Object.assign(Object.assign({}, this.baseData), { headers: (response === null || response === void 0 ? void 0 : response.headers).map, body, requestId: uniqueId !== null && uniqueId !== void 0 ? uniqueId : Number(id), statusCode: response === null || response === void 0 ? void 0 : response.status, endTime: Date.now() }));
154
- });
155
- this.resTimeout = (uniqueId) => __awaiter(this, void 0, void 0, function* () {
156
- var _e;
157
- return (_e = this.server) === null || _e === void 0 ? void 0 : _e.network(Object.assign(Object.assign({}, this.baseData), { isTimeout: true, requestId: uniqueId }));
158
- });
159
- this.resResponseError = (uniqueId) => __awaiter(this, void 0, void 0, function* () {
160
- var _f;
161
- return (_f = this.server) === null || _f === void 0 ? void 0 : _f.network(Object.assign(Object.assign({}, this.baseData), { isResponseError: true, requestId: uniqueId }));
162
- });
163
- this.uniqueRes = (uniqueId, response) => __awaiter(this, void 0, void 0, function* () {
164
- return this._res(uniqueId, undefined, response);
165
- });
166
- this.req = (input, init) => __awaiter(this, void 0, void 0, function* () {
167
- return this.uniqueReq(undefined, input, init);
168
- });
169
- this.res = (id, response) => __awaiter(this, void 0, void 0, function* () {
170
- return this._res(undefined, id, response);
171
- });
174
+ this._log(common_1.Level.ERROR, common_1.Tag.DEFAULT, ...data);
175
+ };
176
+ this.init();
172
177
  }
173
178
  }
174
179
  const SafeLogPlugin = (0, utils_1.createClassWithErrorHandling)(LogPlugin);