@zwa73/utils 1.0.34 → 1.0.36
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/UtilCodecs.d.ts +43 -40
- package/dist/UtilCodecs.js +110 -106
- package/dist/UtilCom.d.ts +21 -18
- package/dist/UtilCom.js +88 -84
- package/dist/UtilFileTools.d.ts +81 -78
- package/dist/UtilFileTools.js +171 -167
- package/dist/UtilFunctions.d.ts +44 -41
- package/dist/UtilFunctions.js +76 -72
- package/dist/UtilInterfaces.d.ts +6 -0
- package/package.json +1 -1
- package/src/UtilCodecs.ts +9 -1
- package/src/UtilCom.ts +3 -0
- package/src/UtilDecorators.ts +1 -0
- package/src/UtilFileTools.ts +4 -2
- package/src/UtilFunctions.ts +3 -1
- package/src/UtilInterfaces.ts +10 -1
package/dist/UtilCodecs.d.ts
CHANGED
|
@@ -1,40 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
/**编码/解码器 */
|
|
2
|
+
export declare namespace UtilCodec {
|
|
3
|
+
/**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
|
|
4
|
+
* @param {string} str - 要转换的字符串
|
|
5
|
+
* @returns {string} 转换后的字符串
|
|
6
|
+
*/
|
|
7
|
+
function decodeHtmlEntities(str: string): string;
|
|
8
|
+
/**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
|
|
9
|
+
* @param {string} str - 要转换的字符串
|
|
10
|
+
* @returns {string} 转换后的字符串
|
|
11
|
+
*/
|
|
12
|
+
function encodeHtmlEntities(str: string): string;
|
|
13
|
+
/**token长度计算器 Turbo模型
|
|
14
|
+
* @param {string} str = 所要计算的消息
|
|
15
|
+
* @returns {number} 整数长度结果
|
|
16
|
+
*/
|
|
17
|
+
function tokenNumTurbo(str: string): number;
|
|
18
|
+
/**token长度计算器 Davinci模型
|
|
19
|
+
* @param {string} str = 所要计算的消息
|
|
20
|
+
* @returns {number} 整数长度结果
|
|
21
|
+
*/
|
|
22
|
+
function tokenNumDavinci(str: string): number;
|
|
23
|
+
/**token编码 Turbo模型
|
|
24
|
+
* @param {string} str = 所要计算的消息
|
|
25
|
+
* @returns {Array<number>} Token数组
|
|
26
|
+
*/
|
|
27
|
+
function encodeTokenTurbo(str: string): Uint32Array;
|
|
28
|
+
/**token编码 Davinci模型
|
|
29
|
+
* @param {string} str = 所要计算的消息
|
|
30
|
+
* @returns {Array<number>} Token数组
|
|
31
|
+
*/
|
|
32
|
+
function encodeTokenDavinci(str: string): Uint32Array;
|
|
33
|
+
/**token解码 Turbo模型
|
|
34
|
+
* @param {Array<number>} arr = Token数组
|
|
35
|
+
* @returns {string} 消息字符串
|
|
36
|
+
*/
|
|
37
|
+
function decodeTokenTurbo(arr: Uint32Array): string;
|
|
38
|
+
/**token解码 Davinci模型
|
|
39
|
+
* @param {Array<number>} arr = Token数组
|
|
40
|
+
* @returns {string} 消息字符串
|
|
41
|
+
*/
|
|
42
|
+
function decodeTokenDavinci(arr: Uint32Array): string;
|
|
43
|
+
}
|
package/dist/UtilCodecs.js
CHANGED
|
@@ -1,110 +1,114 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.UtilCodec = void 0;
|
|
4
4
|
const he = require("html-entities");
|
|
5
5
|
const tiktoken_1 = require("tiktoken");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
let
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
6
|
+
/**编码/解码器 */
|
|
7
|
+
var UtilCodec;
|
|
8
|
+
(function (UtilCodec) {
|
|
9
|
+
let encoderTurbo = null;
|
|
10
|
+
let encoderDavinci = null;
|
|
11
|
+
const textDecoder = new TextDecoder();
|
|
12
|
+
// 定义一个对象,存储常见的HTML实体和对应的字符
|
|
13
|
+
let htmlEntities = {
|
|
14
|
+
"<": "<",
|
|
15
|
+
">": ">",
|
|
16
|
+
"&": "&",
|
|
17
|
+
""": "\"",
|
|
18
|
+
"'": "'",
|
|
19
|
+
"[": "[",
|
|
20
|
+
"]": "]"
|
|
21
|
+
};
|
|
22
|
+
/**HTML实体解码 将一个字符串中的HTML实体转换为对应的字符
|
|
23
|
+
* @param {string} str - 要转换的字符串
|
|
24
|
+
* @returns {string} 转换后的字符串
|
|
25
|
+
*/
|
|
26
|
+
function decodeHtmlEntities(str) {
|
|
27
|
+
//for(let code in htmlEntities){
|
|
28
|
+
// let cha = htmlEntities[code]
|
|
29
|
+
// str = str.replaceAll(code, cha);
|
|
30
|
+
//}
|
|
31
|
+
//return str
|
|
32
|
+
return he.decode(str);
|
|
33
|
+
}
|
|
34
|
+
UtilCodec.decodeHtmlEntities = decodeHtmlEntities;
|
|
35
|
+
/**HTML实体编码 将一个字符串中的 需编码字符转换为 HTML实体
|
|
36
|
+
* @param {string} str - 要转换的字符串
|
|
37
|
+
* @returns {string} 转换后的字符串
|
|
38
|
+
*/
|
|
39
|
+
function encodeHtmlEntities(str) {
|
|
40
|
+
//for(let code in htmlEntities){
|
|
41
|
+
// let cha = htmlEntities[code]
|
|
42
|
+
// str = str.replaceAll(cha, code);
|
|
43
|
+
//}
|
|
44
|
+
//return str
|
|
45
|
+
return he.encode(str);
|
|
46
|
+
}
|
|
47
|
+
UtilCodec.encodeHtmlEntities = encodeHtmlEntities;
|
|
48
|
+
//token长度计算器
|
|
49
|
+
//cl100k_base ChatGPT models, text-embedding-ada-002
|
|
50
|
+
//p50k_base Code models, text-davinci-002, text-davinci-003
|
|
51
|
+
//r50k_base (or gpt2) GPT-3 models like davinci
|
|
52
|
+
//避免在nextjs调用时出错
|
|
53
|
+
function initTikTokenEncoder() {
|
|
54
|
+
if (encoderTurbo != null && encoderDavinci != null)
|
|
55
|
+
return;
|
|
56
|
+
encoderTurbo = (0, tiktoken_1.get_encoding)("cl100k_base");
|
|
57
|
+
encoderDavinci = (0, tiktoken_1.get_encoding)("p50k_base");
|
|
58
|
+
}
|
|
59
|
+
/**token长度计算器 Turbo模型
|
|
60
|
+
* @param {string} str = 所要计算的消息
|
|
61
|
+
* @returns {number} 整数长度结果
|
|
62
|
+
*/
|
|
63
|
+
function tokenNumTurbo(str) {
|
|
64
|
+
initTikTokenEncoder();
|
|
65
|
+
//return encoder.encode(str).length
|
|
66
|
+
return encoderTurbo?.encode(str).length;
|
|
67
|
+
}
|
|
68
|
+
UtilCodec.tokenNumTurbo = tokenNumTurbo;
|
|
69
|
+
/**token长度计算器 Davinci模型
|
|
70
|
+
* @param {string} str = 所要计算的消息
|
|
71
|
+
* @returns {number} 整数长度结果
|
|
72
|
+
*/
|
|
73
|
+
function tokenNumDavinci(str) {
|
|
74
|
+
initTikTokenEncoder();
|
|
75
|
+
return encoderDavinci?.encode(str).length;
|
|
76
|
+
}
|
|
77
|
+
UtilCodec.tokenNumDavinci = tokenNumDavinci;
|
|
78
|
+
/**token编码 Turbo模型
|
|
79
|
+
* @param {string} str = 所要计算的消息
|
|
80
|
+
* @returns {Array<number>} Token数组
|
|
81
|
+
*/
|
|
82
|
+
function encodeTokenTurbo(str) {
|
|
83
|
+
initTikTokenEncoder();
|
|
84
|
+
return encoderTurbo?.encode(str);
|
|
85
|
+
}
|
|
86
|
+
UtilCodec.encodeTokenTurbo = encodeTokenTurbo;
|
|
87
|
+
/**token编码 Davinci模型
|
|
88
|
+
* @param {string} str = 所要计算的消息
|
|
89
|
+
* @returns {Array<number>} Token数组
|
|
90
|
+
*/
|
|
91
|
+
function encodeTokenDavinci(str) {
|
|
92
|
+
initTikTokenEncoder();
|
|
93
|
+
return encoderDavinci?.encode(str);
|
|
94
|
+
}
|
|
95
|
+
UtilCodec.encodeTokenDavinci = encodeTokenDavinci;
|
|
96
|
+
/**token解码 Turbo模型
|
|
97
|
+
* @param {Array<number>} arr = Token数组
|
|
98
|
+
* @returns {string} 消息字符串
|
|
99
|
+
*/
|
|
100
|
+
function decodeTokenTurbo(arr) {
|
|
101
|
+
initTikTokenEncoder();
|
|
102
|
+
return textDecoder.decode(encoderTurbo?.decode(arr));
|
|
103
|
+
}
|
|
104
|
+
UtilCodec.decodeTokenTurbo = decodeTokenTurbo;
|
|
105
|
+
/**token解码 Davinci模型
|
|
106
|
+
* @param {Array<number>} arr = Token数组
|
|
107
|
+
* @returns {string} 消息字符串
|
|
108
|
+
*/
|
|
109
|
+
function decodeTokenDavinci(arr) {
|
|
110
|
+
initTikTokenEncoder();
|
|
111
|
+
return textDecoder.decode(encoderDavinci?.decode(arr));
|
|
112
|
+
}
|
|
113
|
+
UtilCodec.decodeTokenDavinci = decodeTokenDavinci;
|
|
114
|
+
})(UtilCodec = exports.UtilCodec || (exports.UtilCodec = {}));
|
package/dist/UtilCom.d.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { JObject } from "./UtilInterfaces";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
2
|
+
/**网络工具 */
|
|
3
|
+
export declare namespace UtilCom {
|
|
4
|
+
/**发送一个POST请求并接受数据
|
|
5
|
+
* Object ()
|
|
6
|
+
* @async
|
|
7
|
+
* @param {JObject} json - 数据对象
|
|
8
|
+
* @param {Object} options - 参数对象
|
|
9
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
10
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
11
|
+
*/
|
|
12
|
+
function shttpsPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
|
|
13
|
+
/**发送一个POST请求并接受数据
|
|
14
|
+
* Object ()
|
|
15
|
+
* @async
|
|
16
|
+
* @param {JObject} json - 数据对象
|
|
17
|
+
* @param {Object} options - 参数对象
|
|
18
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
19
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
20
|
+
*/
|
|
21
|
+
function shttpPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
|
|
22
|
+
}
|
package/dist/UtilCom.js
CHANGED
|
@@ -1,97 +1,101 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.UtilCom = void 0;
|
|
4
4
|
const UtilInterfaces_1 = require("./UtilInterfaces");
|
|
5
5
|
const https = require("https");
|
|
6
6
|
const http = require("http");
|
|
7
7
|
const UtilLogger_1 = require("./UtilLogger");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
/**网络工具 */
|
|
9
|
+
var UtilCom;
|
|
10
|
+
(function (UtilCom) {
|
|
11
|
+
function sPost(type, json, options, timeLimit = -1) {
|
|
12
|
+
//转换为毫秒
|
|
13
|
+
const hasTimeLimit = (timeLimit >= 10);
|
|
14
|
+
if (hasTimeLimit)
|
|
15
|
+
timeLimit *= 1000;
|
|
16
|
+
const jsonStr = (0, UtilInterfaces_1.stringifyJToken)(json);
|
|
17
|
+
const funcName = "s" + type + "Psot";
|
|
18
|
+
return new Promise((resolve, rejecte) => {
|
|
19
|
+
const resFunc = (res) => {
|
|
20
|
+
try {
|
|
21
|
+
//请求超时
|
|
22
|
+
if (hasTimeLimit) {
|
|
23
|
+
res.setTimeout(timeLimit, () => {
|
|
24
|
+
//res.abort();
|
|
25
|
+
throw funcName + " 接收反馈超时: " + timeLimit + " ms";
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
let resdata = "";
|
|
29
|
+
res.setEncoding('utf8');
|
|
30
|
+
res.on('data', (chunk) => resdata += chunk);
|
|
31
|
+
res.on('error', (e) => {
|
|
32
|
+
throw funcName + " 接收反馈错误:" + e;
|
|
33
|
+
});
|
|
34
|
+
res.on('end', () => {
|
|
35
|
+
if (resdata == "")
|
|
36
|
+
throw funcName + " 接收反馈错误: resdata 为空";
|
|
37
|
+
try {
|
|
38
|
+
let obj = JSON.parse(resdata);
|
|
39
|
+
UtilLogger_1.SLogger.http(funcName + " 接受信息:", obj);
|
|
40
|
+
resolve(obj);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
throw funcName + " 接收反馈错误:" + e + "\n原始字符串:" + resdata;
|
|
45
|
+
}
|
|
23
46
|
});
|
|
24
47
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
catch (err) {
|
|
49
|
+
if (typeof err != "string")
|
|
50
|
+
throw err;
|
|
51
|
+
UtilLogger_1.SLogger.warn(err);
|
|
52
|
+
resolve(null);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
//路由 http/https
|
|
57
|
+
let req = null;
|
|
58
|
+
if (type === "https")
|
|
59
|
+
req = https.request(options, resFunc);
|
|
60
|
+
else if (type === "http")
|
|
61
|
+
req = http.request(options, resFunc);
|
|
62
|
+
//请求超时
|
|
63
|
+
if (hasTimeLimit) {
|
|
64
|
+
req.setTimeout(timeLimit, () => {
|
|
65
|
+
UtilLogger_1.SLogger.warn(funcName + " 发送请求超时: " + timeLimit + " ms");
|
|
66
|
+
req.destroy();
|
|
43
67
|
});
|
|
44
68
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
throw err;
|
|
48
|
-
UtilLogger_1.SLogger.warn(err);
|
|
69
|
+
req.on('error', (e) => {
|
|
70
|
+
UtilLogger_1.SLogger.warn(funcName + " 发送请求错误:" + e);
|
|
49
71
|
resolve(null);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
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);
|
|
59
|
-
//请求超时
|
|
60
|
-
if (hasTimeLimit) {
|
|
61
|
-
req.setTimeout(timeLimit, () => {
|
|
62
|
-
UtilLogger_1.SLogger.warn(funcName + " 发送请求超时: " + timeLimit + " ms");
|
|
63
|
-
req.destroy();
|
|
64
72
|
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
UtilLogger_1.SLogger.warn(funcName + " 发送请求错误:" + e);
|
|
68
|
-
resolve(null);
|
|
73
|
+
req.write(jsonStr);
|
|
74
|
+
req.end();
|
|
69
75
|
});
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
exports.shttpPost = shttpPost;
|
|
76
|
+
}
|
|
77
|
+
/**发送一个POST请求并接受数据
|
|
78
|
+
* Object ()
|
|
79
|
+
* @async
|
|
80
|
+
* @param {JObject} json - 数据对象
|
|
81
|
+
* @param {Object} options - 参数对象
|
|
82
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
83
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
84
|
+
*/
|
|
85
|
+
function shttpsPost(json, options, timeLimit = -1) {
|
|
86
|
+
return sPost("https", json, options, timeLimit);
|
|
87
|
+
}
|
|
88
|
+
UtilCom.shttpsPost = shttpsPost;
|
|
89
|
+
/**发送一个POST请求并接受数据
|
|
90
|
+
* Object ()
|
|
91
|
+
* @async
|
|
92
|
+
* @param {JObject} json - 数据对象
|
|
93
|
+
* @param {Object} options - 参数对象
|
|
94
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
95
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
96
|
+
*/
|
|
97
|
+
function shttpPost(json, options, timeLimit = -1) {
|
|
98
|
+
return sPost("http", json, options, timeLimit);
|
|
99
|
+
}
|
|
100
|
+
UtilCom.shttpPost = shttpPost;
|
|
101
|
+
})(UtilCom = exports.UtilCom || (exports.UtilCom = {}));
|