@zwa73/utils 1.0.5 → 1.0.6

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.
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@zwa73/utils",
3
+ "version": "1.0.5",
4
+ "description": "my utils",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test"
8
+ },
9
+ "keywords": [
10
+ "zwa73"
11
+ ],
12
+ "author": "zwa73",
13
+ "license": "ISC",
14
+ "dependencies": {
15
+ "@dqbd/tiktoken": "^1.0.7",
16
+ "esm": "^3.2.25",
17
+ "esm-resolve": "^1.0.8",
18
+ "html-entities": "^2.3.3",
19
+ "http-proxy-agent": "^5.0.0",
20
+ "https-proxy-agent": "^5.0.1",
21
+ "tiktoken": "^1.0.7"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^18.16.3"
25
+ }
26
+ }
@@ -2,9 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.decodeTokenDavinci = exports.decodeTokenTurbo = exports.encodeTokenDavinci = exports.encodeTokenTurbo = exports.tokenNumDavinci = exports.tokenNumTurbo = exports.encodeHtmlEntities = exports.decodeHtmlEntities = void 0;
4
4
  const he = require("html-entities");
5
- const tiktoken = require("@dqbd/tiktoken");
6
- const encoderTurbo = tiktoken.get_encoding("cl100k_base");
7
- const encoderDavinci = tiktoken.get_encoding("p50k_base");
5
+ const tiktoken_1 = require("tiktoken");
6
+ const encoderTurbo = (0, tiktoken_1.get_encoding)("cl100k_base");
7
+ const encoderDavinci = (0, tiktoken_1.get_encoding)("p50k_base");
8
8
  const textDecoder = new TextDecoder();
9
9
  // 定义一个对象,存储常见的HTML实体和对应的字符
10
10
  let htmlEntities = {
package/dist/UtilCom.d.ts CHANGED
@@ -7,4 +7,13 @@ import { JObject } from "./UtilInterfaces";
7
7
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
8
8
  * @returns {Promise<Object|null>} 结果 null 为未能成功接收
9
9
  */
10
- export declare function spost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
10
+ export declare function shttpsPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
11
+ /**发送一个POST请求并接受数据
12
+ * Object ()
13
+ * @async
14
+ * @param {JObject} json - 数据对象
15
+ * @param {Object} options - 参数对象
16
+ * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
17
+ * @returns {Promise<Object|null>} 结果 null 为未能成功接收
18
+ */
19
+ export declare function shttpPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
package/dist/UtilCom.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.spost = void 0;
3
+ exports.shttpPost = exports.shttpsPost = void 0;
4
4
  const https = require("https");
5
+ const http = require("http");
5
6
  /**发送一个POST请求并接受数据
6
7
  * Object ()
7
8
  * @async
@@ -10,7 +11,7 @@ const https = require("https");
10
11
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
11
12
  * @returns {Promise<Object|null>} 结果 null 为未能成功接收
12
13
  */
13
- function spost(json, options, timeLimit = -1) {
14
+ function shttpsPost(json, options, timeLimit = -1) {
14
15
  //转换为毫秒
15
16
  let hasTimeLimit = (timeLimit >= 10);
16
17
  if (hasTimeLimit)
@@ -22,7 +23,7 @@ function spost(json, options, timeLimit = -1) {
22
23
  if (hasTimeLimit) {
23
24
  res.setTimeout(timeLimit, function () {
24
25
  //res.abort();
25
- console.log("spost 接收反馈超时: " + timeLimit + " ms");
26
+ console.log("shttpsPost 接收反馈超时: " + timeLimit + " ms");
26
27
  resolve(null);
27
28
  });
28
29
  }
@@ -33,26 +34,26 @@ function spost(json, options, timeLimit = -1) {
33
34
  resdata += chunk;
34
35
  });
35
36
  res.on('error', function (e) {
36
- console.log("spost 接收反馈错误:" + e);
37
+ console.log("shttpsPost 接收反馈错误:" + e);
37
38
  resolve(null);
38
39
  });
39
40
  res.on('end', function () {
40
41
  if (resdata != "") {
41
42
  try {
42
43
  let obj = JSON.parse(resdata);
43
- console.log("spost 接受信息:\r\n" + JSON.stringify(obj));
44
+ console.log("shttpsPost 接受信息:\r\n" + JSON.stringify(obj));
44
45
  //console.log(obj);
45
46
  resolve(obj);
46
47
  return;
47
48
  }
48
49
  catch (e) {
49
- console.log("spost 接收反馈错误:" + e);
50
+ console.log("shttpsPost 接收反馈错误:" + e);
50
51
  console.log("原始字符串:" + resdata);
51
52
  resolve(null);
52
53
  return;
53
54
  }
54
55
  }
55
- console.log("spost 接收反馈错误: resdata 为空");
56
+ console.log("shttpsPost 接收反馈错误: resdata 为空");
56
57
  resolve(null);
57
58
  return;
58
59
  });
@@ -60,16 +61,87 @@ function spost(json, options, timeLimit = -1) {
60
61
  //请求超时
61
62
  if (hasTimeLimit) {
62
63
  req.setTimeout(timeLimit, function () {
63
- console.log("spost 发送请求超时: " + timeLimit + " ms");
64
+ console.log("shttpsPost 发送请求超时: " + timeLimit + " ms");
64
65
  req.destroy();
65
66
  });
66
67
  }
67
68
  req.on('error', function (e) {
68
- console.log("spost 发送请求错误:" + e);
69
+ console.log("shttpsPost 发送请求错误:" + e);
69
70
  resolve(null);
70
71
  });
71
72
  req.write(jsonStr);
72
73
  req.end();
73
74
  });
74
75
  }
75
- exports.spost = spost;
76
+ exports.shttpsPost = shttpsPost;
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 shttpPost(json, options, timeLimit = -1) {
86
+ //转换为毫秒
87
+ let hasTimeLimit = (timeLimit >= 10);
88
+ if (hasTimeLimit)
89
+ timeLimit *= 1000;
90
+ let jsonStr = JSON.stringify(json);
91
+ return new Promise(function (resolve, rejecte) {
92
+ let req = http.request(options, function (res) {
93
+ //请求超时
94
+ if (hasTimeLimit) {
95
+ res.setTimeout(timeLimit, function () {
96
+ //res.abort();
97
+ console.log("shttpPost 接收反馈超时: " + timeLimit + " ms");
98
+ resolve(null);
99
+ });
100
+ }
101
+ let resdata = "";
102
+ res.setEncoding('utf8');
103
+ res.on('data', function (chunk) {
104
+ //console.log(chunk);
105
+ resdata += chunk;
106
+ });
107
+ res.on('error', function (e) {
108
+ console.log("shttpPost 接收反馈错误:" + e);
109
+ resolve(null);
110
+ });
111
+ res.on('end', function () {
112
+ if (resdata != "") {
113
+ try {
114
+ let obj = JSON.parse(resdata);
115
+ console.log("shttpPost 接受信息:\r\n" + JSON.stringify(obj));
116
+ //console.log(obj);
117
+ resolve(obj);
118
+ return;
119
+ }
120
+ catch (e) {
121
+ console.log("shttpPost 接收反馈错误:" + e);
122
+ console.log("原始字符串:" + resdata);
123
+ resolve(null);
124
+ return;
125
+ }
126
+ }
127
+ console.log("shttpPost 接收反馈错误: resdata 为空");
128
+ resolve(null);
129
+ return;
130
+ });
131
+ });
132
+ //请求超时
133
+ if (hasTimeLimit) {
134
+ req.setTimeout(timeLimit, function () {
135
+ console.log("shttpPost 发送请求超时: " + timeLimit + " ms");
136
+ req.destroy();
137
+ });
138
+ }
139
+ req.on('error', function (e) {
140
+ console.log("shttpPost 发送请求错误:" + e);
141
+ resolve(null);
142
+ });
143
+ req.write(jsonStr);
144
+ req.end();
145
+ });
146
+ }
147
+ exports.shttpPost = shttpPost;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,11 +13,14 @@
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
15
  "@dqbd/tiktoken": "^1.0.7",
16
- "@types/node": "^18.16.3",
17
16
  "esm": "^3.2.25",
18
17
  "esm-resolve": "^1.0.8",
19
18
  "html-entities": "^2.3.3",
20
19
  "http-proxy-agent": "^5.0.0",
21
- "https-proxy-agent": "^5.0.1"
20
+ "https-proxy-agent": "^5.0.1",
21
+ "tiktoken": "^1.0.7"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^18.16.3"
22
25
  }
23
26
  }
package/src/UtilCodecs.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as he from 'html-entities';
2
- import * as tiktoken from '@dqbd/tiktoken';
3
- const encoderTurbo = tiktoken.get_encoding("cl100k_base");
4
- const encoderDavinci = tiktoken.get_encoding("p50k_base");
2
+ import {get_encoding} from 'tiktoken';
3
+ const encoderTurbo = get_encoding("cl100k_base");
4
+ const encoderDavinci = get_encoding("p50k_base");
5
5
  const textDecoder = new TextDecoder();
6
6
 
7
7
 
package/src/UtilCom.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { JObject } from "./UtilInterfaces";
2
2
  import * as https from 'https';
3
+ import * as http from 'http';
3
4
 
4
5
 
5
6
  /**发送一个POST请求并接受数据
@@ -10,7 +11,7 @@ import * as https from 'https';
10
11
  * @param {number} [timeLimit] - 超时时间/秒 最小为10秒
11
12
  * @returns {Promise<Object|null>} 结果 null 为未能成功接收
12
13
  */
13
- export function spost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
14
+ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
14
15
  //转换为毫秒
15
16
  let hasTimeLimit = (timeLimit>=10);
16
17
  if(hasTimeLimit)
@@ -24,7 +25,7 @@ export function spost(json:JObject,options:Object,timeLimit:number=-1):Promise<J
24
25
  if(hasTimeLimit){
25
26
  res.setTimeout(timeLimit, function() {
26
27
  //res.abort();
27
- console.log("spost 接收反馈超时: "+timeLimit+" ms");
28
+ console.log("shttpsPost 接收反馈超时: "+timeLimit+" ms");
28
29
  resolve(null);
29
30
  });
30
31
  }
@@ -37,7 +38,7 @@ export function spost(json:JObject,options:Object,timeLimit:number=-1):Promise<J
37
38
  });
38
39
 
39
40
  res.on('error',function(e){
40
- console.log("spost 接收反馈错误:"+e);
41
+ console.log("shttpsPost 接收反馈错误:"+e);
41
42
  resolve(null);
42
43
  });
43
44
 
@@ -45,19 +46,19 @@ export function spost(json:JObject,options:Object,timeLimit:number=-1):Promise<J
45
46
  if(resdata!=""){
46
47
  try{
47
48
  let obj = JSON.parse(resdata);
48
- console.log("spost 接受信息:\r\n"+JSON.stringify(obj));
49
+ console.log("shttpsPost 接受信息:\r\n"+JSON.stringify(obj));
49
50
  //console.log(obj);
50
51
  resolve(obj);
51
52
  return;
52
53
  }
53
54
  catch(e){
54
- console.log("spost 接收反馈错误:"+e);
55
+ console.log("shttpsPost 接收反馈错误:"+e);
55
56
  console.log("原始字符串:"+resdata);
56
57
  resolve(null);
57
58
  return;
58
59
  }
59
60
  }
60
- console.log("spost 接收反馈错误: resdata 为空");
61
+ console.log("shttpsPost 接收反馈错误: resdata 为空");
61
62
  resolve(null);
62
63
  return;
63
64
  });
@@ -67,13 +68,93 @@ export function spost(json:JObject,options:Object,timeLimit:number=-1):Promise<J
67
68
  //请求超时
68
69
  if(hasTimeLimit){
69
70
  req.setTimeout(timeLimit, function() {
70
- console.log("spost 发送请求超时: "+timeLimit+" ms");
71
+ console.log("shttpsPost 发送请求超时: "+timeLimit+" ms");
71
72
  req.destroy();
72
73
  });
73
74
  }
74
75
 
75
76
  req.on('error', function(e) {
76
- console.log("spost 发送请求错误:"+e);
77
+ console.log("shttpsPost 发送请求错误:"+e);
78
+ resolve(null);
79
+ });
80
+
81
+ req.write(jsonStr);
82
+ req.end();
83
+ });
84
+ }
85
+
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
+ export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
95
+ //转换为毫秒
96
+ let hasTimeLimit = (timeLimit>=10);
97
+ if(hasTimeLimit)
98
+ timeLimit*=1000
99
+
100
+ let jsonStr = JSON.stringify(json);
101
+
102
+ return new Promise(function(resolve, rejecte){
103
+ let req = http.request(options, function(res){
104
+ //请求超时
105
+ if(hasTimeLimit){
106
+ res.setTimeout(timeLimit, function() {
107
+ //res.abort();
108
+ console.log("shttpPost 接收反馈超时: "+timeLimit+" ms");
109
+ resolve(null);
110
+ });
111
+ }
112
+
113
+ let resdata = "";
114
+ res.setEncoding('utf8');
115
+ res.on('data',function(chunk){
116
+ //console.log(chunk);
117
+ resdata+=chunk;
118
+ });
119
+
120
+ res.on('error',function(e){
121
+ console.log("shttpPost 接收反馈错误:"+e);
122
+ resolve(null);
123
+ });
124
+
125
+ res.on('end',function(){
126
+ if(resdata!=""){
127
+ try{
128
+ let obj = JSON.parse(resdata);
129
+ console.log("shttpPost 接受信息:\r\n"+JSON.stringify(obj));
130
+ //console.log(obj);
131
+ resolve(obj);
132
+ return;
133
+ }
134
+ catch(e){
135
+ console.log("shttpPost 接收反馈错误:"+e);
136
+ console.log("原始字符串:"+resdata);
137
+ resolve(null);
138
+ return;
139
+ }
140
+ }
141
+ console.log("shttpPost 接收反馈错误: resdata 为空");
142
+ resolve(null);
143
+ return;
144
+ });
145
+
146
+ });
147
+
148
+ //请求超时
149
+ if(hasTimeLimit){
150
+ req.setTimeout(timeLimit, function() {
151
+ console.log("shttpPost 发送请求超时: "+timeLimit+" ms");
152
+ req.destroy();
153
+ });
154
+ }
155
+
156
+ req.on('error', function(e) {
157
+ console.log("shttpPost 发送请求错误:"+e);
77
158
  resolve(null);
78
159
  });
79
160