@zwa73/utils 1.0.26 → 1.0.28

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/dist/UtilCom.js CHANGED
@@ -4,146 +4,94 @@ exports.shttpPost = exports.shttpsPost = void 0;
4
4
  const UtilInterfaces_1 = require("./UtilInterfaces");
5
5
  const https = require("https");
6
6
  const http = require("http");
7
- /**发送一个POST请求并接受数据
8
- * Object ()
9
- * @async
10
- * @param {JObject} json - 数据对象
11
- * @param {Object} options - 参数对象
12
- * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
13
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
14
- */
15
- function shttpsPost(json, options, timeLimit = -1) {
16
- //转换为毫秒
17
- let hasTimeLimit = (timeLimit >= 10);
18
- if (hasTimeLimit)
19
- timeLimit *= 1000;
20
- let jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
21
- return new Promise(function (resolve, rejecte) {
22
- let req = https.request(options, function (res) {
23
- //请求超时
24
- if (hasTimeLimit) {
25
- res.setTimeout(timeLimit, function () {
26
- //res.abort();
27
- console.log("shttpsPost 接收反馈超时: " + timeLimit + " ms");
28
- resolve(null);
29
- });
30
- }
31
- let resdata = "";
32
- res.setEncoding('utf8');
33
- res.on('data', function (chunk) {
34
- //console.log(chunk);
35
- resdata += chunk;
36
- });
37
- res.on('error', function (e) {
38
- console.log("shttpsPost 接收反馈错误:" + e);
39
- resolve(null);
40
- });
41
- res.on('end', function () {
42
- if (resdata != "") {
43
- try {
44
- let obj = JSON.parse(resdata);
45
- console.log("shttpsPost 接受信息:\r\n" + (0, UtilInterfaces_1.stringifyJToken)(obj));
46
- //console.log(obj);
47
- resolve(obj);
48
- return;
49
- }
50
- catch (e) {
51
- console.log("shttpsPost 接收反馈错误:" + e);
52
- console.log("原始字符串:" + resdata);
53
- resolve(null);
54
- return;
55
- }
56
- }
57
- console.log("shttpsPost 接收反馈错误: resdata 为空");
58
- resolve(null);
59
- return;
60
- });
61
- });
62
- //请求超时
63
- if (hasTimeLimit) {
64
- req.setTimeout(timeLimit, function () {
65
- console.log("shttpsPost 发送请求超时: " + timeLimit + " ms");
66
- req.destroy();
67
- });
68
- }
69
- req.on('error', function (e) {
70
- console.log("shttpsPost 发送请求错误:" + e);
71
- resolve(null);
72
- });
73
- req.write(jsonStr);
74
- req.end();
75
- });
76
- }
77
- exports.shttpsPost = shttpsPost;
78
- /**发送一个POST请求并接受数据
79
- * Object ()
80
- * @async
81
- * @param {JObject} json - 数据对象
82
- * @param {Object} options - 参数对象
83
- * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
84
- * @returns {Promise<Object|null>} 结果 null 为未能成功接收
85
- */
86
- function shttpPost(json, options, timeLimit = -1) {
7
+ const UtilLogger_1 = require("./UtilLogger");
8
+ function sPost(type, json, options, timeLimit = -1) {
87
9
  //转换为毫秒
88
- let hasTimeLimit = (timeLimit >= 10);
10
+ const hasTimeLimit = (timeLimit >= 10);
89
11
  if (hasTimeLimit)
90
12
  timeLimit *= 1000;
91
- let jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
92
- return new Promise(function (resolve, rejecte) {
93
- let req = http.request(options, function (res) {
13
+ const jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
14
+ const funcName = "s" + type + "Psot";
15
+ return new Promise((resolve, rejecte) => {
16
+ const resFunc = (res) => {
94
17
  try {
95
18
  //请求超时
96
19
  if (hasTimeLimit) {
97
20
  res.setTimeout(timeLimit, () => {
98
21
  //res.abort();
99
- throw "shttpPost 接收反馈超时: " + timeLimit + " ms";
22
+ throw funcName + " 接收反馈超时: " + timeLimit + " ms";
100
23
  });
101
24
  }
102
25
  let resdata = "";
103
26
  res.setEncoding('utf8');
104
- res.on('data', (chunk) => {
105
- //console.log(chunk);
106
- resdata += chunk;
107
- });
27
+ res.on('data', (chunk) => resdata += chunk);
108
28
  res.on('error', (e) => {
109
- throw "shttpPost 接收反馈错误:" + e;
29
+ throw funcName + " 接收反馈错误:" + e;
110
30
  });
111
- res.on('end', function () {
31
+ res.on('end', () => {
112
32
  if (resdata == "")
113
- throw "shttpPost 接收反馈错误: resdata 为空";
33
+ throw funcName + " 接收反馈错误: resdata 为空";
114
34
  try {
115
35
  let obj = JSON.parse(resdata);
116
- console.log("shttpPost 接受信息:\r\n" + (0, UtilInterfaces_1.stringifyJToken)(obj));
117
- //console.log(obj);
36
+ UtilLogger_1.SLogger.http(funcName + " 接受信息:\n" + (0, UtilInterfaces_1.stringifyJToken)(obj));
118
37
  resolve(obj);
119
38
  return;
120
39
  }
121
40
  catch (e) {
122
- throw "shttpPost 接收反馈错误:" + e + "\n原始字符串:" + resdata;
41
+ throw funcName + " 接收反馈错误:" + e + "\n原始字符串:" + resdata;
123
42
  }
124
43
  });
125
44
  }
126
45
  catch (err) {
127
46
  if (typeof err != "string")
128
47
  throw err;
129
- console.log(err);
48
+ UtilLogger_1.SLogger.warn(err);
130
49
  resolve(null);
131
50
  return;
132
51
  }
133
- });
52
+ };
53
+ //路由 http/https
54
+ let req = null;
55
+ if (type === "https")
56
+ req = https.request(options, resFunc);
57
+ else if (type === "http")
58
+ req = http.request(options, resFunc);
134
59
  //请求超时
135
60
  if (hasTimeLimit) {
136
- req.setTimeout(timeLimit, function () {
137
- console.log("shttpPost 发送请求超时: " + timeLimit + " ms");
61
+ req.setTimeout(timeLimit, () => {
62
+ UtilLogger_1.SLogger.warn(funcName + " 发送请求超时: " + timeLimit + " ms");
138
63
  req.destroy();
139
64
  });
140
65
  }
141
- req.on('error', function (e) {
142
- console.log("shttpPost 发送请求错误:" + e);
66
+ req.on('error', (e) => {
67
+ UtilLogger_1.SLogger.warn(funcName + " 发送请求错误:" + e);
143
68
  resolve(null);
144
69
  });
145
70
  req.write(jsonStr);
146
71
  req.end();
147
72
  });
148
73
  }
74
+ /**发送一个POST请求并接受数据
75
+ * Object ()
76
+ * @async
77
+ * @param {JObject} json - 数据对象
78
+ * @param {Object} options - 参数对象
79
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
80
+ * @returns {Promise<Object|null>} 结果 null 为未能成功接收
81
+ */
82
+ function shttpsPost(json, options, timeLimit = -1) {
83
+ return sPost("https", json, options, timeLimit);
84
+ }
85
+ exports.shttpsPost = shttpsPost;
86
+ /**发送一个POST请求并接受数据
87
+ * Object ()
88
+ * @async
89
+ * @param {JObject} json - 数据对象
90
+ * @param {Object} options - 参数对象
91
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
92
+ * @returns {Promise<Object|null>} 结果 null 为未能成功接收
93
+ */
94
+ function shttpPost(json, options, timeLimit = -1) {
95
+ return sPost("http", json, options, timeLimit);
96
+ }
149
97
  exports.shttpPost = shttpPost;
@@ -0,0 +1,79 @@
1
+ import { JObject, JToken } from "./UtilInterfaces";
2
+ /**验证路径 文件或文件夹 是否存在 异步
3
+ * @async
4
+ * @param {string} filePath - 待验证的路径
5
+ * @returns {Promise<boolean>} - 是否存在
6
+ */
7
+ export declare function pathExists(filePath: string): Promise<boolean>;
8
+ /**验证路径 文件或文件夹 是否存在 同步
9
+ * @param {string} filePath - 待验证的路径
10
+ * @returns {boolean} - 是否存在
11
+ */
12
+ export declare function pathExistsSync(filePath: string): boolean;
13
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
14
+ * @async
15
+ * @param {string} filePath - 待创建的路径
16
+ * @param {boolean} isDir - 强制创建一个文件夹
17
+ * @returns {Promise<boolean>} - 是否成功创建
18
+ */
19
+ export declare function createPath(filePath: string, isDir?: boolean): Promise<boolean>;
20
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
21
+ * @param {string} filePath - 待创建的路径
22
+ * @param {boolean} isDir - 强制验证一个文件夹
23
+ * @returns {boolean} - 是否成功创建
24
+ */
25
+ export declare function createPathSync(filePath: string, isDir?: boolean): boolean;
26
+ /**确保路径存在 不存在时创建 异步
27
+ * @async
28
+ * @param {string} filePath - 待验证的路径
29
+ * @param {boolean} isDir - 强制验证一个文件夹
30
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
31
+ */
32
+ export declare function ensurePathExists(filePath: string, isDir?: boolean): Promise<boolean>;
33
+ /**确保路径存在 不存在时创建 同步
34
+ * @param {string} filePath - 待验证的路径
35
+ * @returns {boolean} - 是否成功执行 创建或已存在
36
+ */
37
+ export declare function ensurePathExistsSync(filePath: string, isDir?: boolean): boolean;
38
+ /**加载json文件 同步
39
+ * Object (string)
40
+ * @param {string} filePath - 文件路径
41
+ * @returns {JObject} - 加载完成的对象或空{}
42
+ */
43
+ export declare function loadJSONFileSync(filePath: string): JObject;
44
+ /**加载json文件 同步
45
+ * Object (string)
46
+ * @param {string} filePath - 文件路径
47
+ * @param {T} def - 默认值
48
+ * @returns {T} - 加载完成的对象或默认值
49
+ */
50
+ export declare function loadJSONFileSync<T extends JToken>(filePath: string, def: T): T;
51
+ /**加载json文件 异步
52
+ * Object (string)
53
+ * @async
54
+ * @param {string} filePath - 文件路径
55
+ * @returns {Promise<JObject>} - 加载完成的对象或空{}
56
+ */
57
+ export declare function loadJSONFile(filePath: string): Promise<JObject>;
58
+ /**加载json文件 异步
59
+ * Object (string)
60
+ * @async
61
+ * @param {string} filePath - 文件路径
62
+ * @param {T} T - 默认值
63
+ * @returns {Promise<T>} - 加载完成的对象或默认值
64
+ */
65
+ export declare function loadJSONFile<T extends JToken>(filePath: string, def: T): Promise<T>;
66
+ /**写入JSON文件
67
+ * void (string,Object)
68
+ * @async
69
+ * @param {string} filePath - 文件路径
70
+ * @param {JToken} token - 所要写入的JToken
71
+ * @returns {Promise<void>}
72
+ */
73
+ export declare function writeJSONFile(filePath: string, token: JToken): Promise<void>;
74
+ /**搜索路径符合正则表达式的文件
75
+ * @param folder - 文件夹路径
76
+ * @param traitRegex - 正则表达式
77
+ * @returns {Record<string, string>} 文件名与路径的映射
78
+ */
79
+ export declare function fileSearch(folder: string, traitRegex: string): Record<string, string>;
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fileSearch = exports.writeJSONFile = exports.loadJSONFile = exports.loadJSONFileSync = exports.ensurePathExistsSync = exports.ensurePathExists = exports.createPathSync = exports.createPath = exports.pathExistsSync = exports.pathExists = void 0;
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const UtilInterfaces_1 = require("./UtilInterfaces");
7
+ const UtilLogger_1 = require("./UtilLogger");
8
+ /**验证路径 文件或文件夹 是否存在 异步
9
+ * @async
10
+ * @param {string} filePath - 待验证的路径
11
+ * @returns {Promise<boolean>} - 是否存在
12
+ */
13
+ async function pathExists(filePath) {
14
+ try {
15
+ const stats = await fs.promises.stat(filePath);
16
+ await fs.promises.access(filePath);
17
+ return true;
18
+ }
19
+ catch (e) {
20
+ return false;
21
+ }
22
+ }
23
+ exports.pathExists = pathExists;
24
+ /**验证路径 文件或文件夹 是否存在 同步
25
+ * @param {string} filePath - 待验证的路径
26
+ * @returns {boolean} - 是否存在
27
+ */
28
+ function pathExistsSync(filePath) {
29
+ try {
30
+ fs.accessSync(filePath);
31
+ return true;
32
+ }
33
+ catch (e) {
34
+ return false;
35
+ }
36
+ }
37
+ exports.pathExistsSync = pathExistsSync;
38
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
39
+ * @async
40
+ * @param {string} filePath - 待创建的路径
41
+ * @param {boolean} isDir - 强制创建一个文件夹
42
+ * @returns {Promise<boolean>} - 是否成功创建
43
+ */
44
+ async function createPath(filePath, isDir) {
45
+ if (isDir == true)
46
+ filePath = path.join(filePath, path.sep);
47
+ try {
48
+ if (filePath.endsWith(path.sep)) {
49
+ await fs.promises.mkdir(filePath, { recursive: true });
50
+ return true;
51
+ }
52
+ await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
53
+ await fs.promises.open(filePath, 'w');
54
+ return true;
55
+ }
56
+ catch (e) {
57
+ console.log("createPath 错误");
58
+ console.log(e);
59
+ return false;
60
+ }
61
+ }
62
+ exports.createPath = createPath;
63
+ /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
64
+ * @param {string} filePath - 待创建的路径
65
+ * @param {boolean} isDir - 强制验证一个文件夹
66
+ * @returns {boolean} - 是否成功创建
67
+ */
68
+ function createPathSync(filePath, isDir) {
69
+ if (isDir == true)
70
+ filePath = path.join(filePath, path.sep);
71
+ try {
72
+ if (filePath.endsWith(path.sep)) {
73
+ fs.mkdirSync(filePath, { recursive: true });
74
+ return true;
75
+ }
76
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
77
+ fs.openSync(filePath, 'w');
78
+ return true;
79
+ }
80
+ catch (e) {
81
+ console.log("createPath 错误");
82
+ console.log(e);
83
+ return false;
84
+ }
85
+ }
86
+ exports.createPathSync = createPathSync;
87
+ /**确保路径存在 不存在时创建 异步
88
+ * @async
89
+ * @param {string} filePath - 待验证的路径
90
+ * @param {boolean} isDir - 强制验证一个文件夹
91
+ * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
92
+ */
93
+ async function ensurePathExists(filePath, isDir) {
94
+ if (await pathExists(filePath))
95
+ return true;
96
+ return await createPath(filePath, isDir);
97
+ }
98
+ exports.ensurePathExists = ensurePathExists;
99
+ /**确保路径存在 不存在时创建 同步
100
+ * @param {string} filePath - 待验证的路径
101
+ * @returns {boolean} - 是否成功执行 创建或已存在
102
+ */
103
+ function ensurePathExistsSync(filePath, isDir) {
104
+ if (pathExistsSync(filePath))
105
+ return true;
106
+ return createPathSync(filePath, isDir);
107
+ }
108
+ exports.ensurePathExistsSync = ensurePathExistsSync;
109
+ function loadJSONFileSync(filePath, def) {
110
+ if (path.extname(filePath) !== '.json')
111
+ filePath += '.json';
112
+ let str = "";
113
+ // 判断文件路径是否存在
114
+ if (pathExistsSync(filePath))
115
+ str = fs.readFileSync(filePath, "utf-8");
116
+ // 如果不存在则返回默认值
117
+ if (str == "" || str == null) {
118
+ if (def !== undefined)
119
+ return def;
120
+ return {};
121
+ }
122
+ return JSON.parse(str);
123
+ }
124
+ exports.loadJSONFileSync = loadJSONFileSync;
125
+ async function loadJSONFile(filePath, def) {
126
+ if (path.extname(filePath) !== '.json')
127
+ filePath += '.json';
128
+ let str = "";
129
+ // 判断文件路径是否存在
130
+ if (await pathExists(filePath))
131
+ str = await fs.promises.readFile(filePath, "utf-8");
132
+ // 如果不存在则返回默认值
133
+ if (str == "" || str == null) {
134
+ if (def !== undefined)
135
+ return def;
136
+ return {};
137
+ }
138
+ return JSON.parse(str);
139
+ }
140
+ exports.loadJSONFile = loadJSONFile;
141
+ /**写入JSON文件
142
+ * void (string,Object)
143
+ * @async
144
+ * @param {string} filePath - 文件路径
145
+ * @param {JToken} token - 所要写入的JToken
146
+ * @returns {Promise<void>}
147
+ */
148
+ async function writeJSONFile(filePath, token) {
149
+ let str = (0, UtilInterfaces_1.stringifyJToken)(token);
150
+ if (path.extname(filePath) !== '.json')
151
+ filePath += '.json';
152
+ // 判断文件路径是否存在 不存在则创建
153
+ if (!(await pathExists(filePath)))
154
+ await createPath(filePath);
155
+ // 写入文件
156
+ try {
157
+ await fs.promises.writeFile(filePath, str);
158
+ UtilLogger_1.SLogger.info(`${filePath} writeJSONFile 成功`);
159
+ }
160
+ catch (err) {
161
+ UtilLogger_1.SLogger.error(`${filePath} writeJSONFile 错误`)
162
+ .error(err);
163
+ }
164
+ }
165
+ exports.writeJSONFile = writeJSONFile;
166
+ /**搜索路径符合正则表达式的文件
167
+ * @param folder - 文件夹路径
168
+ * @param traitRegex - 正则表达式
169
+ * @returns {Record<string, string>} 文件名与路径的映射
170
+ */
171
+ function fileSearch(folder, traitRegex) {
172
+ let outMap = {};
173
+ let subFiles = fs.readdirSync(folder);
174
+ let regex = new RegExp(traitRegex);
175
+ for (let subFile of subFiles) {
176
+ let subFilePath = path.join(folder, subFile);
177
+ subFilePath = subFilePath.replace(/\\/g, "/");
178
+ let stat = fs.lstatSync(subFilePath);
179
+ //判断是否是文件夹,递归调用
180
+ if (stat.isDirectory()) {
181
+ let subMap = fileSearch(path.join(subFilePath, path.sep), traitRegex);
182
+ for (let key in subMap)
183
+ outMap[key] = subMap[key];
184
+ continue;
185
+ }
186
+ if (regex.test(subFilePath))
187
+ outMap[subFile] = subFilePath;
188
+ }
189
+ return outMap;
190
+ }
191
+ exports.fileSearch = fileSearch;
@@ -1,4 +1,4 @@
1
- import { JObject, JToken } from "./UtilInterfaces";
1
+ import { JToken } from "./UtilInterfaces";
2
2
  /**获取当前时间戳
3
3
  * number ()
4
4
  * @returns {number} 时间戳
@@ -12,78 +12,6 @@ export declare function getTime(): number;
12
12
  * @returns {T} - 最终值
13
13
  */
14
14
  export declare function initField<T>(obj: Record<string, T>, field: string, defaultVal: T): T;
15
- /**验证路径 文件或文件夹 是否存在 异步
16
- * @async
17
- * @param {string} filePath - 待验证的路径
18
- * @returns {Promise<boolean>} - 是否存在
19
- */
20
- export declare function pathExists(filePath: string): Promise<boolean>;
21
- /**验证路径 文件或文件夹 是否存在 同步
22
- * @param {string} filePath - 待验证的路径
23
- * @returns {boolean} - 是否存在
24
- */
25
- export declare function pathExistsSync(filePath: string): boolean;
26
- /**路径不存在时创建路径 以path.sep结尾时创建文件夹 异步
27
- * @async
28
- * @param {string} filePath - 待创建的路径
29
- * @param {boolean} isDir - 强制创建一个文件夹
30
- * @returns {Promise<boolean>} - 是否成功创建
31
- */
32
- export declare function createPath(filePath: string, isDir?: boolean): Promise<boolean>;
33
- /**路径不存在时创建路径 以path.sep结尾时创建文件夹 同步
34
- * @param {string} filePath - 待创建的路径
35
- * @param {boolean} isDir - 强制验证一个文件夹
36
- * @returns {boolean} - 是否成功创建
37
- */
38
- export declare function createPathSync(filePath: string, isDir?: boolean): boolean;
39
- /**确保路径存在 不存在时创建 异步
40
- * @async
41
- * @param {string} filePath - 待验证的路径
42
- * @param {boolean} isDir - 强制验证一个文件夹
43
- * @returns {Promise<boolean>} - 是否成功执行 创建或已存在
44
- */
45
- export declare function ensurePathExists(filePath: string, isDir?: boolean): Promise<boolean>;
46
- /**确保路径存在 不存在时创建 同步
47
- * @param {string} filePath - 待验证的路径
48
- * @returns {boolean} - 是否成功执行 创建或已存在
49
- */
50
- export declare function ensurePathExistsSync(filePath: string, isDir?: boolean): boolean;
51
- /**加载json文件 同步
52
- * Object (string)
53
- * @param {string} filePath - 文件路径
54
- * @returns {JObject} - 加载完成的对象或空{}
55
- */
56
- export declare function loadJSONFileSync(filePath: string): JObject;
57
- /**加载json文件 同步
58
- * Object (string)
59
- * @param {string} filePath - 文件路径
60
- * @param {T} def - 默认值
61
- * @returns {T} - 加载完成的对象或默认值
62
- */
63
- export declare function loadJSONFileSync<T extends JToken>(filePath: string, def: T): T;
64
- /**加载json文件 异步
65
- * Object (string)
66
- * @async
67
- * @param {string} filePath - 文件路径
68
- * @returns {Promise<JObject>} - 加载完成的对象或空{}
69
- */
70
- export declare function loadJSONFile(filePath: string): Promise<JObject>;
71
- /**加载json文件 异步
72
- * Object (string)
73
- * @async
74
- * @param {string} filePath - 文件路径
75
- * @param {T} T - 默认值
76
- * @returns {Promise<T>} - 加载完成的对象或默认值
77
- */
78
- export declare function loadJSONFile<T extends JToken>(filePath: string, def: T): Promise<T>;
79
- /**写入JSON文件
80
- * void (string,Object)
81
- * @async
82
- * @param {string} filePath - 文件路径
83
- * @param {JToken} token - 所要写入的JToken
84
- * @returns {Promise<void>}
85
- */
86
- export declare function writeJSONFile(filePath: string, token: JToken): Promise<void>;
87
15
  /**生成一串uuid
88
16
  * string ()
89
17
  * @returns {string} uuid
@@ -112,9 +40,3 @@ export declare function isSafeNumber(num: number): boolean;
112
40
  * @returns {Promise<boolean>}
113
41
  */
114
42
  export declare function sleep(timeMs: number): Promise<boolean>;
115
- /**搜索路径符合正则表达式的文件
116
- * @param folder - 文件夹路径
117
- * @param traitRegex - 正则表达式
118
- * @returns {Record<string, string>} 文件名与路径的映射
119
- */
120
- export declare function fileSearch(folder: string, traitRegex: string): Record<string, string>;