cvitool 1.0.743 → 1.0.745
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/src/cutil.d.ts +6 -1
- package/build/src/cutil.js +30 -7
- package/build/src/hgo.d.ts +2 -0
- package/build/src/hgo.js +62 -47
- package/index.d.ts +15 -1
- package/package.json +1 -1
- package/src/cutil.ts +36 -8
- package/src/hgo.ts +73 -50
package/build/src/cutil.d.ts
CHANGED
|
@@ -10,6 +10,9 @@ interface randomStringOptions {
|
|
|
10
10
|
number?: boolean;
|
|
11
11
|
specials?: string;
|
|
12
12
|
}
|
|
13
|
+
interface CustomObject {
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
}
|
|
13
16
|
interface getMediaFileTypeRes {
|
|
14
17
|
type: string;
|
|
15
18
|
extname: string;
|
|
@@ -22,4 +25,6 @@ declare function execCmdCommand(command: string, timeout?: number): Promise<unkn
|
|
|
22
25
|
declare function getValueType(value: any): "string" | "number" | "boolean" | "undefined" | "object" | "array" | "null" | "unkonw";
|
|
23
26
|
declare function getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes>;
|
|
24
27
|
declare function checkURLResource(url: string): Promise<boolean>;
|
|
25
|
-
|
|
28
|
+
declare function writeJsonFileSync(data: CustomObject | CustomObject[] | string, path: string, space?: number): void;
|
|
29
|
+
declare function readJsonFileSync(path: string, toObj?: boolean): CustomObject | CustomObject[] | string;
|
|
30
|
+
export { randomStringOptions, getMediaFileTypeRes, CustomObject, randomString, encryptCBC, decryptCBC, md5, execCmdCommand, getValueType, getMediaFileType, checkURLResource, writeJsonFileSync, readJsonFileSync };
|
package/build/src/cutil.js
CHANGED
|
@@ -9,9 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.checkURLResource = exports.getMediaFileType = exports.getValueType = exports.execCmdCommand = exports.md5 = exports.decryptCBC = exports.encryptCBC = exports.randomString = void 0;
|
|
12
|
+
exports.readJsonFileSync = exports.writeJsonFileSync = exports.checkURLResource = exports.getMediaFileType = exports.getValueType = exports.execCmdCommand = exports.md5 = exports.decryptCBC = exports.encryptCBC = exports.randomString = void 0;
|
|
13
13
|
const crypto_1 = require("crypto");
|
|
14
14
|
const child_process_1 = require("child_process");
|
|
15
|
+
const fs_1 = require("fs");
|
|
15
16
|
const hgo = require("./hgo");
|
|
16
17
|
function randomString(length, options) {
|
|
17
18
|
let { special = false, lowercase = true, upperCase = true, number = true, specials } = options || {};
|
|
@@ -68,29 +69,29 @@ function md5(data, inputEncoding = 'utf-8') {
|
|
|
68
69
|
return (0, crypto_1.createHash)('md5').update(data, inputEncoding).digest('hex');
|
|
69
70
|
}
|
|
70
71
|
exports.md5 = md5;
|
|
71
|
-
function execCmdCommand(command, timeout =
|
|
72
|
+
function execCmdCommand(command, timeout = 1 * 60 * 1000) {
|
|
72
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
73
74
|
const commandList = command.split(' ');
|
|
74
75
|
const commandName = commandList.splice(0, 1);
|
|
75
76
|
return new Promise((resolve, reject) => {
|
|
76
77
|
const chunks = [];
|
|
77
|
-
const cmd = (0, child_process_1.spawn)(commandName[0], commandList, { stdio: 'pipe', shell: true, timeout });
|
|
78
|
+
const cmd = (0, child_process_1.spawn)(commandName[0], commandList, { stdio: 'pipe', shell: true, timeout, killSignal: 'SIGKILL' });
|
|
78
79
|
cmd.stdout.on('data', chunk => {
|
|
79
80
|
chunks.push(chunk);
|
|
80
81
|
});
|
|
81
82
|
cmd.stderr.on('data', chunk => {
|
|
82
83
|
chunks.push(chunk);
|
|
83
84
|
});
|
|
84
|
-
cmd.on('exit', (code) => {
|
|
85
|
+
cmd.on('exit', (code, signal) => {
|
|
85
86
|
const result = Buffer.concat(chunks).toString();
|
|
86
87
|
if (code !== 0) {
|
|
87
88
|
const customErr = new Error();
|
|
88
|
-
if (
|
|
89
|
-
customErr.message = `timeout in ${timeout}ms`;
|
|
89
|
+
if (signal === 'SIGKILL') {
|
|
90
|
+
customErr.message = `execCmdCommand|timeout in ${timeout}ms`;
|
|
90
91
|
customErr.name = 'timeoutError';
|
|
91
92
|
}
|
|
92
93
|
else {
|
|
93
|
-
customErr.message = result
|
|
94
|
+
customErr.message = `execCmdCommand|${result}`;
|
|
94
95
|
customErr.name = 'unknowError';
|
|
95
96
|
}
|
|
96
97
|
reject(customErr);
|
|
@@ -258,9 +259,31 @@ function checkURLResource(url) {
|
|
|
258
259
|
yield hgo.get(url, { headers: { Range: 'bytes=0-0' }, resType: 'buffer' });
|
|
259
260
|
}
|
|
260
261
|
catch (e) {
|
|
262
|
+
if (e.name === 'timeoutError') {
|
|
263
|
+
throw e;
|
|
264
|
+
}
|
|
261
265
|
isEffective = false;
|
|
262
266
|
}
|
|
263
267
|
return isEffective;
|
|
264
268
|
});
|
|
265
269
|
}
|
|
266
270
|
exports.checkURLResource = checkURLResource;
|
|
271
|
+
function writeJsonFileSync(data, path, space = 3) {
|
|
272
|
+
let writeStr = '';
|
|
273
|
+
if (getValueType(data) === 'string') {
|
|
274
|
+
writeStr = data;
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
writeStr = JSON.stringify(data, undefined, space);
|
|
278
|
+
}
|
|
279
|
+
(0, fs_1.writeFileSync)(path, writeStr);
|
|
280
|
+
}
|
|
281
|
+
exports.writeJsonFileSync = writeJsonFileSync;
|
|
282
|
+
function readJsonFileSync(path, toObj = true) {
|
|
283
|
+
const str = (0, fs_1.readFileSync)(path).toString();
|
|
284
|
+
if (toObj) {
|
|
285
|
+
return JSON.parse(str);
|
|
286
|
+
}
|
|
287
|
+
return str;
|
|
288
|
+
}
|
|
289
|
+
exports.readJsonFileSync = readJsonFileSync;
|
package/build/src/hgo.d.ts
CHANGED
package/build/src/hgo.js
CHANGED
|
@@ -15,10 +15,8 @@ const https = require("https");
|
|
|
15
15
|
const http = require("http");
|
|
16
16
|
function buildTimeOutErr(type, timeout, reqUrl) {
|
|
17
17
|
const err = new Error();
|
|
18
|
-
err.
|
|
19
|
-
err.
|
|
20
|
-
err.message = `${type} for ${timeout} ms`;
|
|
21
|
-
err.reqUrl = reqUrl;
|
|
18
|
+
err.name = 'timeoutError';
|
|
19
|
+
err.message = `request '${reqUrl}'|${type} in ${timeout} ms`;
|
|
22
20
|
return err;
|
|
23
21
|
}
|
|
24
22
|
function getProtocol(url) {
|
|
@@ -26,7 +24,7 @@ function getProtocol(url) {
|
|
|
26
24
|
}
|
|
27
25
|
function request(url, options) {
|
|
28
26
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
let { query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds } = options || {};
|
|
27
|
+
let { query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost } = options || {};
|
|
30
28
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
31
29
|
connectTimeOut = timeout;
|
|
32
30
|
readTimeOut = timeout;
|
|
@@ -40,7 +38,6 @@ function request(url, options) {
|
|
|
40
38
|
}
|
|
41
39
|
}
|
|
42
40
|
const data = JSON.stringify(body);
|
|
43
|
-
const protocol = getProtocol(url);
|
|
44
41
|
const isbodyEmpty = Object.keys(body).length === 0;
|
|
45
42
|
let baseHeaders = {};
|
|
46
43
|
if (!isbodyEmpty) {
|
|
@@ -50,15 +47,11 @@ function request(url, options) {
|
|
|
50
47
|
};
|
|
51
48
|
}
|
|
52
49
|
let res;
|
|
50
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
|
|
53
51
|
try {
|
|
54
52
|
res = yield new Promise((resolve, reject) => {
|
|
55
|
-
const req =
|
|
56
|
-
|
|
57
|
-
headers: Object.assign(headers, baseHeaders),
|
|
58
|
-
method,
|
|
59
|
-
agent
|
|
60
|
-
}, res => {
|
|
61
|
-
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
53
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
54
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
|
|
62
55
|
});
|
|
63
56
|
req.on('timeout', () => {
|
|
64
57
|
req.destroy();
|
|
@@ -102,22 +95,20 @@ function request(url, options) {
|
|
|
102
95
|
exports.request = request;
|
|
103
96
|
function reqSendBuffer(url, options) {
|
|
104
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
-
let { timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds } = options;
|
|
98
|
+
let { timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost } = options;
|
|
106
99
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
107
100
|
connectTimeOut = timeout;
|
|
108
101
|
readTimeOut = timeout;
|
|
109
102
|
}
|
|
110
|
-
const
|
|
103
|
+
const baseHeaders = {
|
|
104
|
+
'Content-Length': buffer.byteLength
|
|
105
|
+
};
|
|
106
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
|
|
111
107
|
let res;
|
|
112
108
|
try {
|
|
113
109
|
res = yield new Promise((resolve, reject) => {
|
|
114
|
-
const req =
|
|
115
|
-
|
|
116
|
-
headers: Object.assign(headers, { 'Content-Length': buffer.byteLength }),
|
|
117
|
-
method,
|
|
118
|
-
agent
|
|
119
|
-
}, res => {
|
|
120
|
-
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
110
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
111
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
|
|
121
112
|
});
|
|
122
113
|
req.on('timeout', () => {
|
|
123
114
|
req.destroy();
|
|
@@ -156,25 +147,20 @@ function reqSendBuffer(url, options) {
|
|
|
156
147
|
exports.reqSendBuffer = reqSendBuffer;
|
|
157
148
|
function reqSendStream(url, options) {
|
|
158
149
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
let { timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut } = options;
|
|
150
|
+
let { timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost } = options;
|
|
160
151
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
161
152
|
connectTimeOut = timeout;
|
|
162
153
|
readTimeOut = timeout;
|
|
163
154
|
}
|
|
164
|
-
const
|
|
155
|
+
const baseHeaders = {
|
|
156
|
+
'Content-Type': 'application/octet-stream',
|
|
157
|
+
'Transfer-Encoding': 'chunked',
|
|
158
|
+
Connection: 'keep-alive'
|
|
159
|
+
};
|
|
160
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
|
|
165
161
|
const res = yield new Promise((resolve, reject) => {
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
'Transfer-Encoding': 'chunked',
|
|
169
|
-
Connection: 'keep-alive'
|
|
170
|
-
};
|
|
171
|
-
const req = protocol.request(url, {
|
|
172
|
-
timeout: connectTimeOut,
|
|
173
|
-
headers: Object.assign(headers, baseHeaders),
|
|
174
|
-
method,
|
|
175
|
-
agent
|
|
176
|
-
}, res => {
|
|
177
|
-
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
162
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
163
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
|
|
178
164
|
});
|
|
179
165
|
req.on('timeout', () => {
|
|
180
166
|
req.destroy();
|
|
@@ -208,20 +194,16 @@ function reqSendStream(url, options) {
|
|
|
208
194
|
exports.reqSendStream = reqSendStream;
|
|
209
195
|
function reqSendMultiPart(url, options) {
|
|
210
196
|
return __awaiter(this, void 0, void 0, function* () {
|
|
211
|
-
let { timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut } = options;
|
|
197
|
+
let { timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost } = options;
|
|
212
198
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
213
199
|
connectTimeOut = timeout;
|
|
214
200
|
readTimeOut = timeout;
|
|
215
201
|
}
|
|
216
|
-
const
|
|
202
|
+
const baseHeaders = Object.assign({}, form.getHeaders());
|
|
203
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, 'post', agent, useEnvProxy, proxyHost);
|
|
217
204
|
const res = yield new Promise((resolve, reject) => {
|
|
218
|
-
const req =
|
|
219
|
-
|
|
220
|
-
headers: Object.assign(headers, Object.assign({}, form.getHeaders())),
|
|
221
|
-
method: 'post',
|
|
222
|
-
agent
|
|
223
|
-
}, res => {
|
|
224
|
-
resHandld(res, resolve, reject, resType, 'post', readTimeOut, req);
|
|
205
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
206
|
+
resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, url);
|
|
225
207
|
});
|
|
226
208
|
req.on('timeout', () => {
|
|
227
209
|
req.destroy();
|
|
@@ -246,8 +228,7 @@ function reqSendMultiPart(url, options) {
|
|
|
246
228
|
});
|
|
247
229
|
}
|
|
248
230
|
exports.reqSendMultiPart = reqSendMultiPart;
|
|
249
|
-
function resHandld(res, resolve, reject, resType, method, readTimeOut, req) {
|
|
250
|
-
const reqUrl = `${res.req.protocol}//${res.req.host}${res.req.path}`;
|
|
231
|
+
function resHandld(res, resolve, reject, resType, method, readTimeOut, req, reqUrl) {
|
|
251
232
|
const resHeaders = {};
|
|
252
233
|
for (let i = 0; i < res.rawHeaders.length; i += 2) {
|
|
253
234
|
resHeaders[res.rawHeaders[i]] = res.rawHeaders[i + 1];
|
|
@@ -385,3 +366,37 @@ function head(url, options) {
|
|
|
385
366
|
});
|
|
386
367
|
}
|
|
387
368
|
exports.head = head;
|
|
369
|
+
function getReqProtocolAndOpt(url, baseHeaders, headers, timeout, method, agent, useEnvProxy, proxyHost) {
|
|
370
|
+
let reqProtocol = getProtocol(url);
|
|
371
|
+
const urlOpt = new URL(url);
|
|
372
|
+
let reqOpt = {
|
|
373
|
+
host: urlOpt.hostname,
|
|
374
|
+
port: urlOpt.port,
|
|
375
|
+
path: `${urlOpt.pathname}${urlOpt.search}`,
|
|
376
|
+
timeout,
|
|
377
|
+
headers: Object.assign(headers, baseHeaders),
|
|
378
|
+
method,
|
|
379
|
+
agent
|
|
380
|
+
};
|
|
381
|
+
if (proxyHost || (((urlOpt.protocol === 'https:' && process.env.https_proxy) || (urlOpt.protocol === 'http:' && process.env.http_proxy)) && useEnvProxy)) {
|
|
382
|
+
const proxyHostStr = proxyHost || (urlOpt.protocol === 'https:' ? process.env.https_proxy : process.env.http_proxy);
|
|
383
|
+
const proxyUrlOpt = new URL(proxyHostStr);
|
|
384
|
+
const proxyHeaders = {
|
|
385
|
+
Host: `${urlOpt.hostname}:${urlOpt.port || (urlOpt.protocol === 'http:' ? 80 : 443)}`
|
|
386
|
+
};
|
|
387
|
+
reqOpt = {
|
|
388
|
+
host: proxyUrlOpt.hostname,
|
|
389
|
+
port: proxyUrlOpt.port,
|
|
390
|
+
path: url,
|
|
391
|
+
timeout,
|
|
392
|
+
headers: Object.assign(headers, baseHeaders, proxyHeaders),
|
|
393
|
+
method,
|
|
394
|
+
agent
|
|
395
|
+
};
|
|
396
|
+
reqProtocol = proxyUrlOpt.protocol === 'https:' ? https : http;
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
reqProtocol,
|
|
400
|
+
reqOpt
|
|
401
|
+
};
|
|
402
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -16,7 +16,8 @@ import {
|
|
|
16
16
|
|
|
17
17
|
import {
|
|
18
18
|
randomStringOptions,
|
|
19
|
-
getMediaFileTypeRes
|
|
19
|
+
getMediaFileTypeRes,
|
|
20
|
+
CustomObject
|
|
20
21
|
} from './src/cutil';
|
|
21
22
|
|
|
22
23
|
interface Hgo {
|
|
@@ -138,6 +139,19 @@ interface Cutil {
|
|
|
138
139
|
* @param url
|
|
139
140
|
*/
|
|
140
141
|
checkURLResource(url: string): Promise<boolean>
|
|
142
|
+
/**
|
|
143
|
+
* 写入json文件
|
|
144
|
+
* @param data
|
|
145
|
+
* @param path
|
|
146
|
+
* @param space
|
|
147
|
+
*/
|
|
148
|
+
writeJsonFileSync(data: CustomObject | CustomObject[] | string, path: string, space?: number): void
|
|
149
|
+
/**
|
|
150
|
+
* 读取json文件
|
|
151
|
+
* @param path
|
|
152
|
+
* @param toObj
|
|
153
|
+
*/
|
|
154
|
+
readJsonFileSync(path: string, toObj?: boolean): CustomObject | CustomObject[] | string
|
|
141
155
|
}
|
|
142
156
|
|
|
143
157
|
declare const hgo: Hgo;
|
package/package.json
CHANGED
package/src/cutil.ts
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
import {
|
|
9
9
|
spawn
|
|
10
10
|
} from 'child_process';
|
|
11
|
-
import { ReadStream } from 'fs';
|
|
11
|
+
import { ReadStream, writeFileSync, readFileSync } from 'fs';
|
|
12
12
|
import * as hgo from './hgo';
|
|
13
13
|
|
|
14
14
|
interface randomStringOptions {
|
|
@@ -19,6 +19,10 @@ interface randomStringOptions {
|
|
|
19
19
|
specials?: string
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
interface CustomObject {
|
|
23
|
+
[key: string]: any
|
|
24
|
+
}
|
|
25
|
+
|
|
22
26
|
interface getMediaFileTypeRes {
|
|
23
27
|
type: string,
|
|
24
28
|
extname: string
|
|
@@ -77,27 +81,27 @@ function md5(data: string, inputEncoding: Encoding = 'utf-8') {
|
|
|
77
81
|
return createHash('md5').update(data, inputEncoding).digest('hex');
|
|
78
82
|
}
|
|
79
83
|
|
|
80
|
-
async function execCmdCommand(command: string, timeout =
|
|
84
|
+
async function execCmdCommand(command: string, timeout = 1 * 60 * 1000) {
|
|
81
85
|
const commandList = command.split(' ');
|
|
82
86
|
const commandName = commandList.splice(0, 1);
|
|
83
87
|
return new Promise((resolve, reject) => {
|
|
84
88
|
const chunks = [];
|
|
85
|
-
const cmd = spawn(commandName[0], commandList, { stdio: 'pipe', shell: true, timeout });
|
|
89
|
+
const cmd = spawn(commandName[0], commandList, { stdio: 'pipe', shell: true, timeout, killSignal: 'SIGKILL' });
|
|
86
90
|
cmd.stdout.on('data', chunk => {
|
|
87
91
|
chunks.push(chunk);
|
|
88
92
|
});
|
|
89
93
|
cmd.stderr.on('data', chunk => {
|
|
90
94
|
chunks.push(chunk);
|
|
91
95
|
});
|
|
92
|
-
cmd.on('exit', (code) => {
|
|
96
|
+
cmd.on('exit', (code, signal) => {
|
|
93
97
|
const result = Buffer.concat(chunks).toString();
|
|
94
98
|
if (code !== 0) {
|
|
95
99
|
const customErr = new Error();
|
|
96
|
-
if (
|
|
97
|
-
customErr.message = `timeout in ${timeout}ms`;
|
|
100
|
+
if (signal === 'SIGKILL') {
|
|
101
|
+
customErr.message = `execCmdCommand|timeout in ${timeout}ms`;
|
|
98
102
|
customErr.name = 'timeoutError';
|
|
99
103
|
} else {
|
|
100
|
-
customErr.message = result
|
|
104
|
+
customErr.message = `execCmdCommand|${result}`;
|
|
101
105
|
customErr.name = 'unknowError';
|
|
102
106
|
}
|
|
103
107
|
reject(customErr);
|
|
@@ -260,14 +264,36 @@ async function checkURLResource(url: string) {
|
|
|
260
264
|
try {
|
|
261
265
|
await hgo.get(url, { headers: { Range: 'bytes=0-0' }, resType: 'buffer' });
|
|
262
266
|
} catch (e) {
|
|
267
|
+
if (e.name === 'timeoutError') {
|
|
268
|
+
throw e;
|
|
269
|
+
}
|
|
263
270
|
isEffective = false;
|
|
264
271
|
}
|
|
265
272
|
return isEffective;
|
|
266
273
|
}
|
|
267
274
|
|
|
275
|
+
function writeJsonFileSync(data: CustomObject | CustomObject[] | string, path: string, space = 3) {
|
|
276
|
+
let writeStr = '';
|
|
277
|
+
if (getValueType(data) === 'string') {
|
|
278
|
+
writeStr = data as string;
|
|
279
|
+
} else {
|
|
280
|
+
writeStr = JSON.stringify(data, undefined, space);
|
|
281
|
+
}
|
|
282
|
+
writeFileSync(path, writeStr);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function readJsonFileSync(path: string, toObj = true): CustomObject | CustomObject[] | string {
|
|
286
|
+
const str = readFileSync(path).toString();
|
|
287
|
+
if (toObj) {
|
|
288
|
+
return JSON.parse(str);
|
|
289
|
+
}
|
|
290
|
+
return str;
|
|
291
|
+
}
|
|
292
|
+
|
|
268
293
|
export {
|
|
269
294
|
randomStringOptions,
|
|
270
295
|
getMediaFileTypeRes,
|
|
296
|
+
CustomObject,
|
|
271
297
|
randomString,
|
|
272
298
|
encryptCBC,
|
|
273
299
|
decryptCBC,
|
|
@@ -275,5 +301,7 @@ export {
|
|
|
275
301
|
execCmdCommand,
|
|
276
302
|
getValueType,
|
|
277
303
|
getMediaFileType,
|
|
278
|
-
checkURLResource
|
|
304
|
+
checkURLResource,
|
|
305
|
+
writeJsonFileSync,
|
|
306
|
+
readJsonFileSync
|
|
279
307
|
};
|
package/src/hgo.ts
CHANGED
|
@@ -18,7 +18,9 @@ interface baseReqOptions {
|
|
|
18
18
|
},
|
|
19
19
|
resType?: ResType,
|
|
20
20
|
connectTimeOut?: number,
|
|
21
|
-
readTimeOut?: number
|
|
21
|
+
readTimeOut?: number,
|
|
22
|
+
useEnvProxy?: boolean,
|
|
23
|
+
proxyHost?: string
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
interface reqOptions extends baseReqOptions {
|
|
@@ -70,12 +72,10 @@ interface ResData {
|
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
function buildTimeOutErr(type: 'connectTimeOut' | 'readTimeOut', timeout: number, reqUrl?: string) {
|
|
73
|
-
const err
|
|
74
|
-
err.
|
|
75
|
-
err.
|
|
76
|
-
err
|
|
77
|
-
err.reqUrl = reqUrl;
|
|
78
|
-
return err as Error;
|
|
75
|
+
const err = new Error();
|
|
76
|
+
err.name = 'timeoutError';
|
|
77
|
+
err.message = `request '${reqUrl}'|${type} in ${timeout} ms`;
|
|
78
|
+
return err;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
function getProtocol(url: string) {
|
|
@@ -85,7 +85,7 @@ function getProtocol(url: string) {
|
|
|
85
85
|
async function request(url: string, options?: reqOptions): Promise<ResData> {
|
|
86
86
|
let {
|
|
87
87
|
query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut,
|
|
88
|
-
readTimeOut, retries, retryInterval, retrieds
|
|
88
|
+
readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost
|
|
89
89
|
} = options || {};
|
|
90
90
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
91
91
|
connectTimeOut = timeout;
|
|
@@ -99,7 +99,6 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
const data = JSON.stringify(body);
|
|
102
|
-
const protocol = getProtocol(url);
|
|
103
102
|
const isbodyEmpty = Object.keys(body).length === 0;
|
|
104
103
|
let baseHeaders: CustomObject = {};
|
|
105
104
|
if (!isbodyEmpty) {
|
|
@@ -109,15 +108,11 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
|
|
|
109
108
|
};
|
|
110
109
|
}
|
|
111
110
|
let res: ResData;
|
|
111
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
|
|
112
112
|
try {
|
|
113
113
|
res = await new Promise((resolve, reject) => {
|
|
114
|
-
const req =
|
|
115
|
-
|
|
116
|
-
headers: Object.assign(headers, baseHeaders),
|
|
117
|
-
method,
|
|
118
|
-
agent
|
|
119
|
-
}, res => {
|
|
120
|
-
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
114
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
115
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
|
|
121
116
|
});
|
|
122
117
|
req.on('timeout', () => {
|
|
123
118
|
req.destroy();
|
|
@@ -159,23 +154,21 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
|
|
|
159
154
|
async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promise<ResData> {
|
|
160
155
|
let {
|
|
161
156
|
timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut,
|
|
162
|
-
retries, retryInterval, retrieds
|
|
157
|
+
retries, retryInterval, retrieds, useEnvProxy = true, proxyHost
|
|
163
158
|
} = options;
|
|
164
159
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
165
160
|
connectTimeOut = timeout;
|
|
166
161
|
readTimeOut = timeout;
|
|
167
162
|
}
|
|
168
|
-
const
|
|
163
|
+
const baseHeaders = {
|
|
164
|
+
'Content-Length': buffer.byteLength
|
|
165
|
+
};
|
|
166
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
|
|
169
167
|
let res: ResData;
|
|
170
168
|
try {
|
|
171
169
|
res = await new Promise((resolve, reject) => {
|
|
172
|
-
const req =
|
|
173
|
-
|
|
174
|
-
headers: Object.assign(headers, { 'Content-Length': buffer.byteLength }),
|
|
175
|
-
method,
|
|
176
|
-
agent
|
|
177
|
-
}, res => {
|
|
178
|
-
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
170
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
171
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
|
|
179
172
|
});
|
|
180
173
|
req.on('timeout', () => {
|
|
181
174
|
req.destroy();
|
|
@@ -212,26 +205,22 @@ async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promis
|
|
|
212
205
|
|
|
213
206
|
async function reqSendStream(url: string, options: reqSendStreamOptions): Promise<ResData> {
|
|
214
207
|
let {
|
|
215
|
-
timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut
|
|
208
|
+
timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut,
|
|
209
|
+
useEnvProxy = true, proxyHost
|
|
216
210
|
} = options;
|
|
217
211
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
218
212
|
connectTimeOut = timeout;
|
|
219
213
|
readTimeOut = timeout;
|
|
220
214
|
}
|
|
221
|
-
const
|
|
215
|
+
const baseHeaders = {
|
|
216
|
+
'Content-Type': 'application/octet-stream',
|
|
217
|
+
'Transfer-Encoding': 'chunked',
|
|
218
|
+
Connection: 'keep-alive'
|
|
219
|
+
};
|
|
220
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
|
|
222
221
|
const res: ResData = await new Promise((resolve, reject) => {
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
'Transfer-Encoding': 'chunked',
|
|
226
|
-
Connection: 'keep-alive'
|
|
227
|
-
};
|
|
228
|
-
const req = protocol.request(url, {
|
|
229
|
-
timeout: connectTimeOut,
|
|
230
|
-
headers: Object.assign(headers, baseHeaders),
|
|
231
|
-
method,
|
|
232
|
-
agent
|
|
233
|
-
}, res => {
|
|
234
|
-
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
222
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
223
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
|
|
235
224
|
});
|
|
236
225
|
req.on('timeout', () => {
|
|
237
226
|
req.destroy();
|
|
@@ -264,21 +253,20 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
|
|
|
264
253
|
|
|
265
254
|
async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions): Promise<ResData> {
|
|
266
255
|
let {
|
|
267
|
-
timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut
|
|
256
|
+
timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut,
|
|
257
|
+
useEnvProxy = true, proxyHost
|
|
268
258
|
} = options;
|
|
269
259
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
270
260
|
connectTimeOut = timeout;
|
|
271
261
|
readTimeOut = timeout;
|
|
272
262
|
}
|
|
273
|
-
const
|
|
263
|
+
const baseHeaders = {
|
|
264
|
+
...form.getHeaders()
|
|
265
|
+
};
|
|
266
|
+
const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, 'post', agent, useEnvProxy, proxyHost);
|
|
274
267
|
const res: ResData = await new Promise((resolve, reject) => {
|
|
275
|
-
const req =
|
|
276
|
-
|
|
277
|
-
headers: Object.assign(headers, { ...form.getHeaders() }),
|
|
278
|
-
method: 'post',
|
|
279
|
-
agent
|
|
280
|
-
}, res => {
|
|
281
|
-
resHandld(res, resolve, reject, resType, 'post', readTimeOut, req);
|
|
268
|
+
const req = reqProtocol.request(reqOpt, res => {
|
|
269
|
+
resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, url);
|
|
282
270
|
});
|
|
283
271
|
req.on('timeout', () => {
|
|
284
272
|
req.destroy();
|
|
@@ -302,8 +290,7 @@ async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions):
|
|
|
302
290
|
return res;
|
|
303
291
|
}
|
|
304
292
|
|
|
305
|
-
function resHandld(res: http.IncomingMessage, resolve: any, reject: any, resType: ResType, method: Method, readTimeOut: number, req: http.ClientRequest) {
|
|
306
|
-
const reqUrl = `${(res as any).req.protocol}//${(res as any).req.host}${(res as any).req.path}`;
|
|
293
|
+
function resHandld(res: http.IncomingMessage, resolve: any, reject: any, resType: ResType, method: Method, readTimeOut: number, req: http.ClientRequest, reqUrl: string) {
|
|
307
294
|
const resHeaders: CustomObject = {};
|
|
308
295
|
for (let i = 0; i < res.rawHeaders.length; i += 2) {
|
|
309
296
|
resHeaders[res.rawHeaders[i]] = res.rawHeaders[i + 1];
|
|
@@ -432,6 +419,42 @@ async function head(url: string, options?: methodReqOptions) {
|
|
|
432
419
|
return request(url, { ...options, method: 'head' });
|
|
433
420
|
}
|
|
434
421
|
|
|
422
|
+
function getReqProtocolAndOpt(url: string, baseHeaders: CustomObject, headers: CustomObject, timeout: number, method: Method, agent: http.Agent | https.Agent,
|
|
423
|
+
useEnvProxy: boolean, proxyHost: string) {
|
|
424
|
+
let reqProtocol = getProtocol(url);
|
|
425
|
+
const urlOpt = new URL(url);
|
|
426
|
+
let reqOpt = {
|
|
427
|
+
host: urlOpt.hostname,
|
|
428
|
+
port: urlOpt.port,
|
|
429
|
+
path: `${urlOpt.pathname}${urlOpt.search}`,
|
|
430
|
+
timeout,
|
|
431
|
+
headers: Object.assign(headers, baseHeaders),
|
|
432
|
+
method,
|
|
433
|
+
agent
|
|
434
|
+
};
|
|
435
|
+
if (proxyHost || (((urlOpt.protocol === 'https:' && process.env.https_proxy) || (urlOpt.protocol === 'http:' && process.env.http_proxy)) && useEnvProxy)) {
|
|
436
|
+
const proxyHostStr = proxyHost || (urlOpt.protocol === 'https:' ? process.env.https_proxy : process.env.http_proxy);
|
|
437
|
+
const proxyUrlOpt = new URL(proxyHostStr);
|
|
438
|
+
const proxyHeaders = {
|
|
439
|
+
Host: `${urlOpt.hostname}:${urlOpt.port || (urlOpt.protocol === 'http:' ? 80 : 443)}`
|
|
440
|
+
};
|
|
441
|
+
reqOpt = {
|
|
442
|
+
host: proxyUrlOpt.hostname,
|
|
443
|
+
port: proxyUrlOpt.port,
|
|
444
|
+
path: url,
|
|
445
|
+
timeout,
|
|
446
|
+
headers: Object.assign(headers, baseHeaders, proxyHeaders),
|
|
447
|
+
method,
|
|
448
|
+
agent
|
|
449
|
+
};
|
|
450
|
+
reqProtocol = proxyUrlOpt.protocol === 'https:' ? https : http;
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
reqProtocol,
|
|
454
|
+
reqOpt
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
435
458
|
export {
|
|
436
459
|
reqOptions,
|
|
437
460
|
methodReqOptions,
|