@wutiange/log-listener-plugin 2.0.1-alpha.3 → 2.0.1-alpha.4

Sign up to get free protection for your applications and to get access to all the features.
package/src/utils.ts CHANGED
@@ -1,112 +1,112 @@
1
- import URL from "url-parse";
2
- import logger from "./logger";
3
-
4
- export function sleep(ms: number, isReject: boolean = false) {
5
- return new Promise((resolve, reject) => {
6
- setTimeout(isReject ? () => reject({
7
- code: 11001,
8
- key: '@wutiange/log-listener-plugin%%timeout',
9
- message: 'Timeout'
10
- }) : resolve, ms)
11
- })
12
- }
13
-
14
- // 检查 url 是否有端口号,不包含内置的端口号,比如 80 ,443 等
15
- export function hasPort(url: string) {
16
- // 如果 url 是空的或不是字符串,返回 false
17
- if (!url || typeof url !== 'string') {
18
- return false;
19
- }
20
-
21
- try {
22
- // 使用 URL 构造函数解析 URL
23
- const parsedUrl = new URL(url);
24
-
25
- // 检查 port 属性是否为空
26
- // 注意:如果使用默认端口(如 HTTP 的 80 或 HTTPS 的 443),port 会是空字符串
27
- return parsedUrl.port !== '';
28
- } catch (error) {
29
- logger.error(error)
30
- // 如果 URL 无效,捕获错误并返回 false
31
- return false;
32
- }
33
- }
34
-
35
-
36
- type Constructor<T = {}> = new (...args: any[]) => T;
37
-
38
- export function createClassWithErrorHandling<T extends Constructor>(BaseClass: T): T {
39
- return new Proxy(BaseClass, {
40
- construct(target: T, args: any[]): object {
41
- const instance = new target(...args);
42
- return new Proxy(instance, {
43
- get(target: any, prop: string | symbol): any {
44
- const value = target[prop];
45
- if (typeof value === 'function') {
46
- return function(this: any, ...args: any[]): any {
47
- try {
48
- const result = value.apply(this, args);
49
- if (result instanceof Promise) {
50
- return result.catch((error: Error) => {
51
- console.error(`Error in ${String(prop)}:`, error);
52
- throw error; // 重新抛出错误,以便调用者可以捕获它
53
- });
54
- }
55
- return result;
56
- } catch (error) {
57
- console.error(`Error in ${String(prop)}:`, error);
58
- throw error; // 重新抛出错误,以便调用者可以捕获它
59
- }
60
- };
61
- }
62
- return value;
63
- },
64
- set(target: any, prop: string | symbol, value: any): boolean {
65
- if (typeof value === 'function') {
66
- target[prop] = function(this: any, ...args: any[]): any {
67
- try {
68
- const result = value.apply(this, args);
69
- if (result instanceof Promise) {
70
- return result.catch((error: Error) => {
71
- console.error(`Error in ${String(prop)}:`, error);
72
- throw error;
73
- });
74
- }
75
- return result;
76
- } catch (error) {
77
- console.error(`Error in ${String(prop)}:`, error);
78
- throw error;
79
- }
80
- };
81
- } else {
82
- target[prop] = value;
83
- }
84
- return true;
85
- }
86
- });
87
- }
88
- });
89
- }
90
-
91
-
92
- export function formDataToString(formData: FormData): string {
93
- const boundary =
94
- '----WebKitFormBoundary' + Math.random().toString(36).substr(2);
95
- let result = '';
96
- // 这是 react-native 中的实现,这里面是存在这个方法的
97
- const parts = (formData as any).getParts();
98
- for (const part of parts) {
99
- result += `--${boundary}\r\n`;
100
- result += `Content-Disposition: ${part.headers['content-disposition']}\r\n`;
101
- if (part.headers['content-type']) {
102
- result += `Content-Type: ${part.headers['content-type']}\r\n`;
103
- }
104
- const value = 'string' in part ? part.string : part.uri;
105
- result += `Content-Length: ${value.length}\r\n\r\n`;
106
- result += `${value}\r\n`;
107
- }
108
- result += `--${boundary}--\r\n`;
109
- return result;
110
- }
111
-
112
-
1
+ import URL from "url-parse";
2
+ import logger from "./logger";
3
+
4
+ export function sleep(ms: number, isReject: boolean = false) {
5
+ return new Promise((resolve, reject) => {
6
+ setTimeout(isReject ? () => reject({
7
+ code: 11001,
8
+ key: '@wutiange/log-listener-plugin%%timeout',
9
+ message: 'Timeout'
10
+ }) : resolve, ms)
11
+ })
12
+ }
13
+
14
+ // 检查 url 是否有端口号,不包含内置的端口号,比如 80 ,443 等
15
+ export function hasPort(url: string) {
16
+ // 如果 url 是空的或不是字符串,返回 false
17
+ if (!url || typeof url !== 'string') {
18
+ return false;
19
+ }
20
+
21
+ try {
22
+ // 使用 URL 构造函数解析 URL
23
+ const parsedUrl = new URL(url);
24
+
25
+ // 检查 port 属性是否为空
26
+ // 注意:如果使用默认端口(如 HTTP 的 80 或 HTTPS 的 443),port 会是空字符串
27
+ return parsedUrl.port !== '';
28
+ } catch (error) {
29
+ logger.error(error)
30
+ // 如果 URL 无效,捕获错误并返回 false
31
+ return false;
32
+ }
33
+ }
34
+
35
+
36
+ type Constructor<T = {}> = new (...args: any[]) => T;
37
+
38
+ export function createClassWithErrorHandling<T extends Constructor>(BaseClass: T): T {
39
+ return new Proxy(BaseClass, {
40
+ construct(target: T, args: any[]): object {
41
+ const instance = new target(...args);
42
+ return new Proxy(instance, {
43
+ get(target: any, prop: string | symbol): any {
44
+ const value = target[prop];
45
+ if (typeof value === 'function') {
46
+ return function(this: any, ...args: any[]): any {
47
+ try {
48
+ const result = value.apply(this, args);
49
+ if (result instanceof Promise) {
50
+ return result.catch((error: Error) => {
51
+ logger.error(`Error in ${String(prop)}:`, error);
52
+ throw error; // 重新抛出错误,以便调用者可以捕获它
53
+ });
54
+ }
55
+ return result;
56
+ } catch (error) {
57
+ logger.error(`Error in ${String(prop)}:`, error);
58
+ throw error; // 重新抛出错误,以便调用者可以捕获它
59
+ }
60
+ };
61
+ }
62
+ return value;
63
+ },
64
+ set(target: any, prop: string | symbol, value: any): boolean {
65
+ if (typeof value === 'function') {
66
+ target[prop] = function(this: any, ...args: any[]): any {
67
+ try {
68
+ const result = value.apply(this, args);
69
+ if (result instanceof Promise) {
70
+ return result.catch((error: Error) => {
71
+ logger.error(`Error in ${String(prop)}:`, error);
72
+ throw error;
73
+ });
74
+ }
75
+ return result;
76
+ } catch (error) {
77
+ logger.error(`Error in ${String(prop)}:`, error);
78
+ throw error;
79
+ }
80
+ };
81
+ } else {
82
+ target[prop] = value;
83
+ }
84
+ return true;
85
+ }
86
+ });
87
+ }
88
+ });
89
+ }
90
+
91
+
92
+ export function formDataToString(formData: FormData): string {
93
+ const boundary =
94
+ '----WebKitFormBoundary' + Math.random().toString(36).substr(2);
95
+ let result = '';
96
+ // 这是 react-native 中的实现,这里面是存在这个方法的
97
+ const parts = (formData as any).getParts();
98
+ for (const part of parts) {
99
+ result += `--${boundary}\r\n`;
100
+ result += `Content-Disposition: ${part.headers['content-disposition']}\r\n`;
101
+ if (part.headers['content-type']) {
102
+ result += `Content-Type: ${part.headers['content-type']}\r\n`;
103
+ }
104
+ const value = 'string' in part ? part.string : part.uri;
105
+ result += `Content-Length: ${value.length}\r\n\r\n`;
106
+ result += `${value}\r\n`;
107
+ }
108
+ result += `--${boundary}--\r\n`;
109
+ return result;
110
+ }
111
+
112
+
package/tsconfig.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "compilerOptions": {
3
- "module": "NodeNext",
4
- "declaration": true,
5
- "noImplicitAny": true,
6
- "removeComments": true,
7
- "preserveConstEnums": true,
8
- "outDir": "dist",
9
- "sourceMap": true,
10
- "target": "es6",
11
- "moduleResolution": "NodeNext",
12
- "allowJs": true,
13
- "esModuleInterop": true,
14
- "skipLibCheck": true,
15
- "forceConsistentCasingInFileNames": true,
16
- "resolveJsonModule": true,
17
- "allowSyntheticDefaultImports": true
18
- },
19
- "exclude": ["node_modules", "**/*.spec.ts"],
20
- "include": [
21
- "src/**/*",
22
- "console.ts",
23
- "fetch.ts",
24
- "index.ts",
25
- "react-native-extensions.d.ts"
26
- ]
1
+ {
2
+ "compilerOptions": {
3
+ "module": "NodeNext",
4
+ "declaration": true,
5
+ "noImplicitAny": true,
6
+ "removeComments": true,
7
+ "preserveConstEnums": true,
8
+ "outDir": "dist",
9
+ "sourceMap": true,
10
+ "target": "es6",
11
+ "moduleResolution": "NodeNext",
12
+ "allowJs": true,
13
+ "esModuleInterop": true,
14
+ "skipLibCheck": true,
15
+ "forceConsistentCasingInFileNames": true,
16
+ "resolveJsonModule": true,
17
+ "allowSyntheticDefaultImports": true
18
+ },
19
+ "exclude": ["node_modules", "**/*.spec.ts"],
20
+ "include": [
21
+ "src/**/*",
22
+ "console.ts",
23
+ "fetch.ts",
24
+ "index.ts",
25
+ "react-native-extensions.d.ts"
26
+ ]
27
27
  }
@@ -1,20 +0,0 @@
1
- declare class Server {
2
- private baseUrlObj;
3
- private timeout;
4
- private baseData;
5
- private urlsListener;
6
- private innerBaseData;
7
- constructor(url?: string | Record<string, string>, timeout?: number);
8
- addUrlsListener: (onNewUrlCallback: (urls: string[], urlsObj: Record<string, string>) => void) => void;
9
- private requestJoin;
10
- private handleZeroConf;
11
- updateTimeout(timeout?: number): void;
12
- getUrls(): string[];
13
- private send;
14
- updateUrl(url: string): void;
15
- setBaseUrlObj(urlObj: Record<string, string>): void;
16
- updateBaseData(data: Record<string, any>): void;
17
- log: (data: Record<string, any>) => Promise<void>;
18
- network: (data: Record<string, any>) => Promise<void>;
19
- }
20
- export default Server;
@@ -1 +0,0 @@
1
- {"version":3,"file":"Server.js","sourceRoot":"","sources":["../../src/Server.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,mCAAyC;AAEzC,qCAA2D;AAC3D,sDAA8B;AAC9B,wDAAgC;AAGhC,MAAM,YAAY,GAAG,KAAK,CAAC;AAC3B,MAAM,MAAM;IAUV,YAAY,GAAqC,EAAE,UAAkB,KAAK;QATlE,eAAU,GAA2B,EAAE,CAAC;QAExC,aAAQ,GAAwB,EAAE,CAAC;QAKnC,kBAAa,GAA2B,EAAE,CAAC;QAanD,oBAAe,GAAG,CAChB,gBAA2E,EAC3E,EAAE;YACF,IAAI,CAAC,YAAY,GAAG,gBAAgB,CAAC;QACvC,CAAC,CAAC;QAEM,gBAAW,GAAG,CAAO,GAAW,EAAE,KAAa,EAAE,EAAE;;YACzD,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;gBAChC,MAAM,EAAE,MAAM;gBACd,OAAO,EAAE;oBACP,cAAc,EAAE,gCAAgC;iBACjD;gBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;oBACnB,KAAK;oBACL,KAAK,EAAE,MAAA,IAAI,CAAC,aAAa,CAAC,KAAK,mCAAI,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,IAAI,IAAI,CAAC,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