@zwa73/utils 1.0.3 → 1.0.4
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/build.js +0 -3
- package/dist/UtilCom.d.ts +10 -0
- package/dist/UtilCom.js +75 -0
- package/package.json +2 -3
- package/release.bat +1 -0
- package/src/UtilCom.ts +83 -0
- package/tsCompile.bat +2 -2
- package/tsCompileWatch.bat +2 -3
package/build.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const tscAlias = require('tsc-alias');
|
|
4
|
-
|
|
5
3
|
|
|
6
4
|
// 读取并更新包配置文件中的版本号
|
|
7
5
|
function updateVersion() {
|
|
@@ -15,5 +13,4 @@ function updateVersion() {
|
|
|
15
13
|
}
|
|
16
14
|
|
|
17
15
|
|
|
18
|
-
tscAlias.replaceTscAliasPaths();
|
|
19
16
|
updateVersion();
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { JObject } from "./UtilInterfaces";
|
|
2
|
+
/**发送一个POST请求并接受数据
|
|
3
|
+
* Object ()
|
|
4
|
+
* @async
|
|
5
|
+
* @param {JObject} json - 数据对象
|
|
6
|
+
* @param {Object} options - 参数对象
|
|
7
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
8
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
9
|
+
*/
|
|
10
|
+
export declare function spost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
|
package/dist/UtilCom.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.spost = void 0;
|
|
4
|
+
const https = require("https");
|
|
5
|
+
/**发送一个POST请求并接受数据
|
|
6
|
+
* Object ()
|
|
7
|
+
* @async
|
|
8
|
+
* @param {JObject} json - 数据对象
|
|
9
|
+
* @param {Object} options - 参数对象
|
|
10
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
11
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
12
|
+
*/
|
|
13
|
+
function spost(json, options, timeLimit = -1) {
|
|
14
|
+
//转换为毫秒
|
|
15
|
+
let hasTimeLimit = (timeLimit >= 10);
|
|
16
|
+
if (hasTimeLimit)
|
|
17
|
+
timeLimit *= 1000;
|
|
18
|
+
let jsonStr = JSON.stringify(json);
|
|
19
|
+
return new Promise(function (resolve, rejecte) {
|
|
20
|
+
let req = https.request(options, function (res) {
|
|
21
|
+
//请求超时
|
|
22
|
+
if (hasTimeLimit) {
|
|
23
|
+
res.setTimeout(timeLimit, function () {
|
|
24
|
+
//res.abort();
|
|
25
|
+
console.log("spost 接收反馈超时: " + timeLimit + " ms");
|
|
26
|
+
resolve(null);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
let resdata = "";
|
|
30
|
+
res.setEncoding('utf8');
|
|
31
|
+
res.on('data', function (chunk) {
|
|
32
|
+
//console.log(chunk);
|
|
33
|
+
resdata += chunk;
|
|
34
|
+
});
|
|
35
|
+
res.on('error', function (e) {
|
|
36
|
+
console.log("spost 接收反馈错误:" + e);
|
|
37
|
+
resolve(null);
|
|
38
|
+
});
|
|
39
|
+
res.on('end', function () {
|
|
40
|
+
if (resdata != "") {
|
|
41
|
+
try {
|
|
42
|
+
let obj = JSON.parse(resdata);
|
|
43
|
+
console.log("spost 接受信息:\r\n" + JSON.stringify(obj));
|
|
44
|
+
//console.log(obj);
|
|
45
|
+
resolve(obj);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
console.log("spost 接收反馈错误:" + e);
|
|
50
|
+
console.log("原始字符串:" + resdata);
|
|
51
|
+
resolve(null);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
console.log("spost 接收反馈错误: resdata 为空");
|
|
56
|
+
resolve(null);
|
|
57
|
+
return;
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
//请求超时
|
|
61
|
+
if (hasTimeLimit) {
|
|
62
|
+
req.setTimeout(timeLimit, function () {
|
|
63
|
+
console.log("spost 发送请求超时: " + timeLimit + " ms");
|
|
64
|
+
req.destroy();
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
req.on('error', function (e) {
|
|
68
|
+
console.log("spost 发送请求错误:" + e);
|
|
69
|
+
resolve(null);
|
|
70
|
+
});
|
|
71
|
+
req.write(jsonStr);
|
|
72
|
+
req.end();
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
exports.spost = spost;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
"esm-resolve": "^1.0.8",
|
|
19
19
|
"html-entities": "^2.3.3",
|
|
20
20
|
"http-proxy-agent": "^5.0.0",
|
|
21
|
-
"https-proxy-agent": "^5.0.1"
|
|
22
|
-
"tsconfig-paths": "^4.2.0"
|
|
21
|
+
"https-proxy-agent": "^5.0.1"
|
|
23
22
|
}
|
|
24
23
|
}
|
package/release.bat
CHANGED
package/src/UtilCom.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { JObject } from "./UtilInterfaces";
|
|
2
|
+
import * as https from 'https';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
/**发送一个POST请求并接受数据
|
|
6
|
+
* Object ()
|
|
7
|
+
* @async
|
|
8
|
+
* @param {JObject} json - 数据对象
|
|
9
|
+
* @param {Object} options - 参数对象
|
|
10
|
+
* @param {number} [timeLimit] - 超时时间/秒 最小为10秒
|
|
11
|
+
* @returns {Promise<Object|null>} 结果 null 为未能成功接收
|
|
12
|
+
*/
|
|
13
|
+
export function spost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
|
|
14
|
+
//转换为毫秒
|
|
15
|
+
let hasTimeLimit = (timeLimit>=10);
|
|
16
|
+
if(hasTimeLimit)
|
|
17
|
+
timeLimit*=1000
|
|
18
|
+
|
|
19
|
+
let jsonStr = JSON.stringify(json);
|
|
20
|
+
|
|
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("spost 接收反馈超时: "+timeLimit+" ms");
|
|
28
|
+
resolve(null);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let resdata = "";
|
|
33
|
+
res.setEncoding('utf8');
|
|
34
|
+
res.on('data',function(chunk){
|
|
35
|
+
//console.log(chunk);
|
|
36
|
+
resdata+=chunk;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
res.on('error',function(e){
|
|
40
|
+
console.log("spost 接收反馈错误:"+e);
|
|
41
|
+
resolve(null);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
res.on('end',function(){
|
|
45
|
+
if(resdata!=""){
|
|
46
|
+
try{
|
|
47
|
+
let obj = JSON.parse(resdata);
|
|
48
|
+
console.log("spost 接受信息:\r\n"+JSON.stringify(obj));
|
|
49
|
+
//console.log(obj);
|
|
50
|
+
resolve(obj);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
catch(e){
|
|
54
|
+
console.log("spost 接收反馈错误:"+e);
|
|
55
|
+
console.log("原始字符串:"+resdata);
|
|
56
|
+
resolve(null);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
console.log("spost 接收反馈错误: resdata 为空");
|
|
61
|
+
resolve(null);
|
|
62
|
+
return;
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
//请求超时
|
|
68
|
+
if(hasTimeLimit){
|
|
69
|
+
req.setTimeout(timeLimit, function() {
|
|
70
|
+
console.log("spost 发送请求超时: "+timeLimit+" ms");
|
|
71
|
+
req.destroy();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
req.on('error', function(e) {
|
|
76
|
+
console.log("spost 发送请求错误:"+e);
|
|
77
|
+
resolve(null);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
req.write(jsonStr);
|
|
81
|
+
req.end();
|
|
82
|
+
});
|
|
83
|
+
}
|
package/tsCompile.bat
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
tsc
|
|
1
|
+
call tsc
|
|
2
|
+
call tsc-alias
|
|
3
3
|
pause
|
package/tsCompileWatch.bat
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
tsc -w
|
|
3
|
-
pause
|
|
1
|
+
start tsc-alias -w
|
|
2
|
+
start tsc -w
|