cvitool 1.0.749 → 1.0.751
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 +5 -8
- package/build/src/cutil.js +33 -27
- package/build/src/hgo.d.ts +2 -7
- package/build/src/hgo.js +11 -12
- package/build/src/streamhelper.d.ts +3 -6
- package/build/src/streamhelper.js +2 -3
- package/package.json +1 -1
- package/src/cutil.ts +26 -18
- package/src/hgo.ts +4 -4
- package/src/streamhelper.ts +3 -4
package/build/src/cutil.d.ts
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
/// <reference types="node" />
|
|
4
1
|
import { Encoding } from 'crypto';
|
|
5
|
-
import {
|
|
2
|
+
import { Readable } from 'stream';
|
|
6
3
|
interface randomStringOptions {
|
|
7
4
|
special?: boolean;
|
|
8
5
|
lowercase?: boolean;
|
|
@@ -67,19 +64,19 @@ declare function execCmdCommand(command: string, timeout?: number): Promise<unkn
|
|
|
67
64
|
* @param value
|
|
68
65
|
* @returns
|
|
69
66
|
*/
|
|
70
|
-
declare function getValueType(value: any): "string" | "number" | "boolean" | "undefined" | "object" | "array" | "null" | "unkonw";
|
|
67
|
+
declare function getValueType(value: any): "string" | "number" | "boolean" | "undefined" | "object" | "function" | "buffer" | "array" | "null" | "unkonw";
|
|
71
68
|
/**
|
|
72
69
|
* 获取媒体文件类型(此方法取文件前30个字节进行检查)
|
|
73
70
|
* @param target
|
|
74
71
|
* @returns
|
|
75
72
|
*/
|
|
76
|
-
declare function getMediaFileType(target: string | Buffer |
|
|
73
|
+
declare function getMediaFileType(target: string | Buffer | Readable): Promise<getMediaFileTypeRes>;
|
|
77
74
|
/**
|
|
78
|
-
* 检查url资源是否可访问(此方法取文件首字节进行检查, 超时时(5s超时)
|
|
75
|
+
* 检查url资源是否可访问(此方法取文件首字节进行检查, 超时时(默认5s超时)返回false)
|
|
79
76
|
* @param url
|
|
80
77
|
* @returns
|
|
81
78
|
*/
|
|
82
|
-
declare function checkURLResource(url: string): Promise<boolean>;
|
|
79
|
+
declare function checkURLResource(url: string, timeout?: number): Promise<boolean>;
|
|
83
80
|
/**
|
|
84
81
|
* 写入json文件
|
|
85
82
|
* @param data 待写入json对象
|
package/build/src/cutil.js
CHANGED
|
@@ -9,7 +9,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.randomString = randomString;
|
|
13
|
+
exports.encryptCBC = encryptCBC;
|
|
14
|
+
exports.decryptCBC = decryptCBC;
|
|
15
|
+
exports.md5 = md5;
|
|
16
|
+
exports.execCmdCommand = execCmdCommand;
|
|
17
|
+
exports.getValueType = getValueType;
|
|
18
|
+
exports.getMediaFileType = getMediaFileType;
|
|
19
|
+
exports.checkURLResource = checkURLResource;
|
|
20
|
+
exports.writeJsonFileSync = writeJsonFileSync;
|
|
21
|
+
exports.readJsonFileSync = readJsonFileSync;
|
|
22
|
+
exports.uuid = uuid;
|
|
13
23
|
const crypto_1 = require("crypto");
|
|
14
24
|
const child_process_1 = require("child_process");
|
|
15
25
|
const fs_1 = require("fs");
|
|
@@ -56,7 +66,6 @@ function randomString(length, options) {
|
|
|
56
66
|
}
|
|
57
67
|
return result;
|
|
58
68
|
}
|
|
59
|
-
exports.randomString = randomString;
|
|
60
69
|
/**
|
|
61
70
|
* 以CBC算法进行对称加密
|
|
62
71
|
* @param data 待加密数据
|
|
@@ -73,7 +82,6 @@ function encryptCBC(data, length, key, iv, inputEncoding = 'utf-8') {
|
|
|
73
82
|
crypted += cipher.final('hex');
|
|
74
83
|
return crypted;
|
|
75
84
|
}
|
|
76
|
-
exports.encryptCBC = encryptCBC;
|
|
77
85
|
;
|
|
78
86
|
/**
|
|
79
87
|
* 以CBC算法进行对称解密
|
|
@@ -91,7 +99,6 @@ function decryptCBC(encryptData, length, key, iv, outputEncoding = 'utf-8') {
|
|
|
91
99
|
decrypted += decipher.final(outputEncoding);
|
|
92
100
|
return decrypted;
|
|
93
101
|
}
|
|
94
|
-
exports.decryptCBC = decryptCBC;
|
|
95
102
|
;
|
|
96
103
|
/**
|
|
97
104
|
* md5加密
|
|
@@ -102,15 +109,14 @@ exports.decryptCBC = decryptCBC;
|
|
|
102
109
|
function md5(data, inputEncoding = 'utf-8') {
|
|
103
110
|
return (0, crypto_1.createHash)('md5').update(data, inputEncoding).digest('hex');
|
|
104
111
|
}
|
|
105
|
-
exports.md5 = md5;
|
|
106
112
|
/**
|
|
107
113
|
* 调用终端执行命令
|
|
108
114
|
* @param command 命令
|
|
109
115
|
* @param timeout 执行超时时间,单位: ms, 默认: 60000
|
|
110
116
|
* @returns
|
|
111
117
|
*/
|
|
112
|
-
function execCmdCommand(
|
|
113
|
-
return __awaiter(this,
|
|
118
|
+
function execCmdCommand(command_1) {
|
|
119
|
+
return __awaiter(this, arguments, void 0, function* (command, timeout = 1 * 60 * 1000) {
|
|
114
120
|
const commandList = command.split(' ');
|
|
115
121
|
const commandName = commandList.splice(0, 1);
|
|
116
122
|
return new Promise((resolve, reject) => {
|
|
@@ -142,7 +148,6 @@ function execCmdCommand(command, timeout = 1 * 60 * 1000) {
|
|
|
142
148
|
});
|
|
143
149
|
});
|
|
144
150
|
}
|
|
145
|
-
exports.execCmdCommand = execCmdCommand;
|
|
146
151
|
/**
|
|
147
152
|
* 获取给定值node类型
|
|
148
153
|
* @param value
|
|
@@ -167,6 +172,13 @@ function getValueType(value) {
|
|
|
167
172
|
case '[object Boolean]':
|
|
168
173
|
type = 'boolean';
|
|
169
174
|
break;
|
|
175
|
+
case '[object Uint8Array]':
|
|
176
|
+
type = 'buffer';
|
|
177
|
+
break;
|
|
178
|
+
case '[object AsyncFunction]':
|
|
179
|
+
case '[object Function]':
|
|
180
|
+
type = 'function';
|
|
181
|
+
break;
|
|
170
182
|
case '[object Undefined]':
|
|
171
183
|
type = 'undefined';
|
|
172
184
|
break;
|
|
@@ -180,7 +192,6 @@ function getValueType(value) {
|
|
|
180
192
|
;
|
|
181
193
|
return type;
|
|
182
194
|
}
|
|
183
|
-
exports.getValueType = getValueType;
|
|
184
195
|
/**
|
|
185
196
|
* 获取媒体文件类型(此方法取文件前30个字节进行检查)
|
|
186
197
|
* @param target
|
|
@@ -191,7 +202,7 @@ function getMediaFileType(target) {
|
|
|
191
202
|
const readStreamOnceBuffer = (stream) => __awaiter(this, void 0, void 0, function* () {
|
|
192
203
|
return new Promise((resolve, reject) => {
|
|
193
204
|
stream.once('data', chunk => {
|
|
194
|
-
stream.
|
|
205
|
+
stream.destroy();
|
|
195
206
|
resolve(chunk);
|
|
196
207
|
});
|
|
197
208
|
stream.on('error', e => {
|
|
@@ -200,11 +211,11 @@ function getMediaFileType(target) {
|
|
|
200
211
|
});
|
|
201
212
|
});
|
|
202
213
|
const checkString = (waitMatchStr, byteStart, byteEnd, buffer) => {
|
|
203
|
-
return waitMatchStr === Buffer.from(buffer.
|
|
214
|
+
return waitMatchStr === Buffer.from(buffer.subarray(byteStart, byteEnd + 1)).toString();
|
|
204
215
|
};
|
|
205
216
|
const checkBuffer = (waitMatchBuffer, byteStart, byteEnd, buffer) => {
|
|
206
217
|
let equal = true;
|
|
207
|
-
const sliceBufferList = buffer.
|
|
218
|
+
const sliceBufferList = buffer.subarray(byteStart, byteEnd + 1);
|
|
208
219
|
for (const [index, item] of waitMatchBuffer.entries()) {
|
|
209
220
|
if (item !== sliceBufferList[index]) {
|
|
210
221
|
equal = false;
|
|
@@ -221,7 +232,7 @@ function getMediaFileType(target) {
|
|
|
221
232
|
};
|
|
222
233
|
};
|
|
223
234
|
let buffer;
|
|
224
|
-
const targetType =
|
|
235
|
+
const targetType = getValueType(target);
|
|
225
236
|
if (targetType === 'string') {
|
|
226
237
|
const res = yield hgo.get(target, {
|
|
227
238
|
resType: 'buffer',
|
|
@@ -231,13 +242,13 @@ function getMediaFileType(target) {
|
|
|
231
242
|
});
|
|
232
243
|
buffer = res.resBody;
|
|
233
244
|
}
|
|
234
|
-
else if (targetType === '
|
|
245
|
+
else if (targetType === 'buffer') {
|
|
235
246
|
buffer = target;
|
|
236
247
|
}
|
|
237
248
|
else {
|
|
238
249
|
buffer = yield readStreamOnceBuffer(target);
|
|
239
250
|
}
|
|
240
|
-
buffer = buffer.
|
|
251
|
+
buffer = buffer.subarray(0, 30);
|
|
241
252
|
if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
|
|
242
253
|
return getResult('image|jpg');
|
|
243
254
|
}
|
|
@@ -301,35 +312,33 @@ function getMediaFileType(target) {
|
|
|
301
312
|
return getResult('unknow|unknow');
|
|
302
313
|
});
|
|
303
314
|
}
|
|
304
|
-
exports.getMediaFileType = getMediaFileType;
|
|
305
315
|
/**
|
|
306
|
-
* 检查url资源是否可访问(此方法取文件首字节进行检查, 超时时(5s超时)
|
|
316
|
+
* 检查url资源是否可访问(此方法取文件首字节进行检查, 超时时(默认5s超时)返回false)
|
|
307
317
|
* @param url
|
|
308
318
|
* @returns
|
|
309
319
|
*/
|
|
310
|
-
function checkURLResource(url) {
|
|
320
|
+
function checkURLResource(url, timeout) {
|
|
311
321
|
return __awaiter(this, void 0, void 0, function* () {
|
|
312
322
|
let isEffective = true;
|
|
313
323
|
try {
|
|
314
|
-
yield hgo.get(url, { headers: { Range: 'bytes=0-0' }, resType: 'buffer' });
|
|
324
|
+
yield hgo.get(url, { headers: { Range: 'bytes=0-0' }, resType: 'buffer', timeout });
|
|
315
325
|
}
|
|
316
326
|
catch (e) {
|
|
317
|
-
if (e.name === 'timeoutError') {
|
|
318
|
-
|
|
319
|
-
}
|
|
327
|
+
// if (e.name === 'timeoutError') {
|
|
328
|
+
// throw e;
|
|
329
|
+
// }
|
|
320
330
|
isEffective = false;
|
|
321
331
|
}
|
|
322
332
|
return isEffective;
|
|
323
333
|
});
|
|
324
334
|
}
|
|
325
|
-
exports.checkURLResource = checkURLResource;
|
|
326
335
|
/**
|
|
327
336
|
* 写入json文件
|
|
328
337
|
* @param data 待写入json对象
|
|
329
338
|
* @param path 写入文件路径
|
|
330
339
|
* @param space json缩进
|
|
331
340
|
*/
|
|
332
|
-
function writeJsonFileSync(data, path, space =
|
|
341
|
+
function writeJsonFileSync(data, path, space = 4) {
|
|
333
342
|
let writeStr = '';
|
|
334
343
|
if (getValueType(data) === 'string') {
|
|
335
344
|
writeStr = data;
|
|
@@ -339,7 +348,6 @@ function writeJsonFileSync(data, path, space = 3) {
|
|
|
339
348
|
}
|
|
340
349
|
(0, fs_1.writeFileSync)(path, writeStr);
|
|
341
350
|
}
|
|
342
|
-
exports.writeJsonFileSync = writeJsonFileSync;
|
|
343
351
|
/**
|
|
344
352
|
* 读取json文件
|
|
345
353
|
* @param path 读取文件路径
|
|
@@ -353,7 +361,6 @@ function readJsonFileSync(path, toObj = true) {
|
|
|
353
361
|
}
|
|
354
362
|
return str;
|
|
355
363
|
}
|
|
356
|
-
exports.readJsonFileSync = readJsonFileSync;
|
|
357
364
|
/**
|
|
358
365
|
* 生成去掉'-'形式的uuid
|
|
359
366
|
* @returns
|
|
@@ -361,4 +368,3 @@ exports.readJsonFileSync = readJsonFileSync;
|
|
|
361
368
|
function uuid() {
|
|
362
369
|
return (0, crypto_1.randomUUID)().replace(/-/g, '');
|
|
363
370
|
}
|
|
364
|
-
exports.uuid = uuid;
|
package/build/src/hgo.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
/// <reference types="node" />
|
|
4
|
-
/// <reference types="node" />
|
|
5
|
-
/// <reference types="node" />
|
|
6
1
|
import * as querystring from 'querystring';
|
|
7
2
|
import * as https from 'https';
|
|
8
3
|
import * as http from 'http';
|
|
9
|
-
import {
|
|
4
|
+
import { Readable } from 'stream';
|
|
10
5
|
type Method = 'get' | 'put' | 'post' | 'delete' | 'patch' | 'head';
|
|
11
6
|
type ResType = 'json' | 'buffer' | 'stream' | 'text';
|
|
12
7
|
interface CustomObject {
|
|
@@ -52,7 +47,7 @@ interface reqSendBufferOptions extends baseReqOptions {
|
|
|
52
47
|
}
|
|
53
48
|
interface reqSendStreamOptions extends baseReqOptions {
|
|
54
49
|
method?: Method;
|
|
55
|
-
stream:
|
|
50
|
+
stream: Readable;
|
|
56
51
|
}
|
|
57
52
|
interface reqSendMultiPartOptions extends baseReqOptions {
|
|
58
53
|
form: any;
|
package/build/src/hgo.js
CHANGED
|
@@ -9,7 +9,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.request = request;
|
|
13
|
+
exports.reqSendBuffer = reqSendBuffer;
|
|
14
|
+
exports.reqSendStream = reqSendStream;
|
|
15
|
+
exports.reqSendMultiPart = reqSendMultiPart;
|
|
16
|
+
exports.get = get;
|
|
17
|
+
exports.post = post;
|
|
18
|
+
exports.put = put;
|
|
19
|
+
exports.head = head;
|
|
20
|
+
exports.del = del;
|
|
13
21
|
const querystring = require("querystring");
|
|
14
22
|
const https = require("https");
|
|
15
23
|
const http = require("http");
|
|
@@ -98,7 +106,6 @@ function request(url, options) {
|
|
|
98
106
|
return res;
|
|
99
107
|
});
|
|
100
108
|
}
|
|
101
|
-
exports.request = request;
|
|
102
109
|
/**
|
|
103
110
|
* 传送buffer请求
|
|
104
111
|
* @param url
|
|
@@ -156,7 +163,6 @@ function reqSendBuffer(url, options) {
|
|
|
156
163
|
return res;
|
|
157
164
|
});
|
|
158
165
|
}
|
|
159
|
-
exports.reqSendBuffer = reqSendBuffer;
|
|
160
166
|
/**
|
|
161
167
|
* 传送stream请求
|
|
162
168
|
* @param url
|
|
@@ -191,7 +197,7 @@ function reqSendStream(url, options) {
|
|
|
191
197
|
stream.on('data', chunk => {
|
|
192
198
|
req.write(chunk, e => {
|
|
193
199
|
if (e) {
|
|
194
|
-
stream.
|
|
200
|
+
stream.destroy();
|
|
195
201
|
req.destroy();
|
|
196
202
|
reject(e);
|
|
197
203
|
}
|
|
@@ -202,14 +208,13 @@ function reqSendStream(url, options) {
|
|
|
202
208
|
});
|
|
203
209
|
stream.on('error', e => {
|
|
204
210
|
req.destroy();
|
|
205
|
-
stream.
|
|
211
|
+
stream.destroy();
|
|
206
212
|
reject(e);
|
|
207
213
|
});
|
|
208
214
|
});
|
|
209
215
|
return res;
|
|
210
216
|
});
|
|
211
217
|
}
|
|
212
|
-
exports.reqSendStream = reqSendStream;
|
|
213
218
|
/**
|
|
214
219
|
* 传送表单formData请求
|
|
215
220
|
* @param url
|
|
@@ -251,7 +256,6 @@ function reqSendMultiPart(url, options) {
|
|
|
251
256
|
return res;
|
|
252
257
|
});
|
|
253
258
|
}
|
|
254
|
-
exports.reqSendMultiPart = reqSendMultiPart;
|
|
255
259
|
function resHandld(res, resolve, reject, resType, method, readTimeOut, req, reqUrl) {
|
|
256
260
|
const resHeaders = {};
|
|
257
261
|
for (let i = 0; i < res.rawHeaders.length; i += 2) {
|
|
@@ -377,7 +381,6 @@ function get(url, options) {
|
|
|
377
381
|
return request(url, Object.assign(Object.assign({}, options), { method: 'get' }));
|
|
378
382
|
});
|
|
379
383
|
}
|
|
380
|
-
exports.get = get;
|
|
381
384
|
/**
|
|
382
385
|
* request-post
|
|
383
386
|
* @param url
|
|
@@ -389,7 +392,6 @@ function post(url, options) {
|
|
|
389
392
|
return request(url, Object.assign(Object.assign({}, options), { method: 'post' }));
|
|
390
393
|
});
|
|
391
394
|
}
|
|
392
|
-
exports.post = post;
|
|
393
395
|
/**
|
|
394
396
|
* request-put
|
|
395
397
|
* @param url
|
|
@@ -401,7 +403,6 @@ function put(url, options) {
|
|
|
401
403
|
return request(url, Object.assign(Object.assign({}, options), { method: 'put' }));
|
|
402
404
|
});
|
|
403
405
|
}
|
|
404
|
-
exports.put = put;
|
|
405
406
|
/**
|
|
406
407
|
* request-head
|
|
407
408
|
* @param url
|
|
@@ -413,7 +414,6 @@ function head(url, options) {
|
|
|
413
414
|
return request(url, Object.assign(Object.assign({}, options), { method: 'head' }));
|
|
414
415
|
});
|
|
415
416
|
}
|
|
416
|
-
exports.head = head;
|
|
417
417
|
/**
|
|
418
418
|
* request-delete
|
|
419
419
|
* @param url
|
|
@@ -425,7 +425,6 @@ function del(url, options) {
|
|
|
425
425
|
return request(url, Object.assign(Object.assign({}, options), { method: 'delete' }));
|
|
426
426
|
});
|
|
427
427
|
}
|
|
428
|
-
exports.del = del;
|
|
429
428
|
function getReqProtocolAndOpt(url, baseHeaders, headers, timeout, method, agent, useEnvProxy, proxyHost) {
|
|
430
429
|
let reqProtocol = getProtocol(url);
|
|
431
430
|
const urlOpt = new URL(url);
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import { ReadStream, WriteStream } from 'fs';
|
|
4
|
-
import * as http from 'http';
|
|
1
|
+
import { Readable, Writable } from 'stream';
|
|
5
2
|
interface pipeOptions {
|
|
6
3
|
timeout?: number;
|
|
7
4
|
readBytesPreSec?: number;
|
|
@@ -13,11 +10,11 @@ interface pipeOptions {
|
|
|
13
10
|
* @param options
|
|
14
11
|
* @returns
|
|
15
12
|
*/
|
|
16
|
-
declare function pipe(source:
|
|
13
|
+
declare function pipe(source: Readable, target: Writable, options?: pipeOptions): Promise<void>;
|
|
17
14
|
/**
|
|
18
15
|
* stream传输限流
|
|
19
16
|
* @param stream 可读流
|
|
20
17
|
* @param readBytesPreSec 每秒读取的字节数
|
|
21
18
|
*/
|
|
22
|
-
declare function limitStreamFlowingRate(stream:
|
|
19
|
+
declare function limitStreamFlowingRate(stream: Readable, readBytesPreSec: number): void;
|
|
23
20
|
export { pipeOptions, pipe, limitStreamFlowingRate };
|
|
@@ -9,7 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
12
|
+
exports.pipe = pipe;
|
|
13
|
+
exports.limitStreamFlowingRate = limitStreamFlowingRate;
|
|
13
14
|
/**
|
|
14
15
|
* 实现一个promise流传输
|
|
15
16
|
* @param source 原流
|
|
@@ -46,7 +47,6 @@ function pipe(source, target, options) {
|
|
|
46
47
|
});
|
|
47
48
|
});
|
|
48
49
|
}
|
|
49
|
-
exports.pipe = pipe;
|
|
50
50
|
/**
|
|
51
51
|
* stream传输限流
|
|
52
52
|
* @param stream 可读流
|
|
@@ -85,4 +85,3 @@ function limitStreamFlowingRate(stream, readBytesPreSec) {
|
|
|
85
85
|
clearInterval(checkInterval);
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
|
-
exports.limitStreamFlowingRate = limitStreamFlowingRate;
|
package/package.json
CHANGED
package/src/cutil.ts
CHANGED
|
@@ -9,8 +9,9 @@ import {
|
|
|
9
9
|
import {
|
|
10
10
|
spawn
|
|
11
11
|
} from 'child_process';
|
|
12
|
-
import {
|
|
12
|
+
import { writeFileSync, readFileSync } from 'fs';
|
|
13
13
|
import * as hgo from './hgo';
|
|
14
|
+
import { Readable } from 'stream';
|
|
14
15
|
|
|
15
16
|
interface randomStringOptions {
|
|
16
17
|
special?: boolean,
|
|
@@ -160,7 +161,7 @@ async function execCmdCommand(command: string, timeout = 1 * 60 * 1000) {
|
|
|
160
161
|
*/
|
|
161
162
|
function getValueType(value: any) {
|
|
162
163
|
const typeStr = Object.prototype.toString.call(value);
|
|
163
|
-
let type: 'number' | 'string' | 'array' | 'object' | 'boolean' | 'undefined' | 'null' | 'unkonw';
|
|
164
|
+
let type: 'number' | 'string' | 'array' | 'object' | 'boolean' | 'buffer' | 'function' | 'undefined' | 'null' | 'unkonw';
|
|
164
165
|
switch (typeStr) {
|
|
165
166
|
case '[object Number]':
|
|
166
167
|
type = 'number';
|
|
@@ -177,6 +178,13 @@ function getValueType(value: any) {
|
|
|
177
178
|
case '[object Boolean]':
|
|
178
179
|
type = 'boolean';
|
|
179
180
|
break;
|
|
181
|
+
case '[object Uint8Array]':
|
|
182
|
+
type = 'buffer';
|
|
183
|
+
break;
|
|
184
|
+
case '[object AsyncFunction]':
|
|
185
|
+
case '[object Function]':
|
|
186
|
+
type = 'function';
|
|
187
|
+
break;
|
|
180
188
|
case '[object Undefined]':
|
|
181
189
|
type = 'undefined';
|
|
182
190
|
break;
|
|
@@ -195,11 +203,11 @@ function getValueType(value: any) {
|
|
|
195
203
|
* @param target
|
|
196
204
|
* @returns
|
|
197
205
|
*/
|
|
198
|
-
async function getMediaFileType(target: string | Buffer |
|
|
199
|
-
const readStreamOnceBuffer = async (stream:
|
|
206
|
+
async function getMediaFileType(target: string | Buffer | Readable): Promise<getMediaFileTypeRes> {
|
|
207
|
+
const readStreamOnceBuffer = async (stream: Readable): Promise<Buffer> => {
|
|
200
208
|
return new Promise((resolve, reject) => {
|
|
201
209
|
stream.once('data', chunk => {
|
|
202
|
-
stream.
|
|
210
|
+
stream.destroy();
|
|
203
211
|
resolve(chunk as Buffer);
|
|
204
212
|
});
|
|
205
213
|
stream.on('error', e => {
|
|
@@ -208,11 +216,11 @@ async function getMediaFileType(target: string | Buffer | ReadStream): Promise<g
|
|
|
208
216
|
});
|
|
209
217
|
};
|
|
210
218
|
const checkString = (waitMatchStr: string, byteStart: number, byteEnd: number, buffer: Buffer) => {
|
|
211
|
-
return waitMatchStr === Buffer.from(buffer.
|
|
219
|
+
return waitMatchStr === Buffer.from(buffer.subarray(byteStart, byteEnd + 1)).toString();
|
|
212
220
|
};
|
|
213
221
|
const checkBuffer = (waitMatchBuffer: number[], byteStart: number, byteEnd: number, buffer: Buffer) => {
|
|
214
222
|
let equal = true;
|
|
215
|
-
const sliceBufferList = buffer.
|
|
223
|
+
const sliceBufferList = buffer.subarray(byteStart, byteEnd + 1);
|
|
216
224
|
for (const [index, item] of waitMatchBuffer.entries()) {
|
|
217
225
|
if (item !== sliceBufferList[index]) {
|
|
218
226
|
equal = false;
|
|
@@ -229,7 +237,7 @@ async function getMediaFileType(target: string | Buffer | ReadStream): Promise<g
|
|
|
229
237
|
};
|
|
230
238
|
};
|
|
231
239
|
let buffer: Buffer;
|
|
232
|
-
const targetType =
|
|
240
|
+
const targetType = getValueType(target);
|
|
233
241
|
if (targetType === 'string') {
|
|
234
242
|
const res = await hgo.get(
|
|
235
243
|
target as string,
|
|
@@ -241,12 +249,12 @@ async function getMediaFileType(target: string | Buffer | ReadStream): Promise<g
|
|
|
241
249
|
}
|
|
242
250
|
);
|
|
243
251
|
buffer = res.resBody as Buffer;
|
|
244
|
-
} else if (targetType === '
|
|
252
|
+
} else if (targetType === 'buffer') {
|
|
245
253
|
buffer = target as Buffer;
|
|
246
254
|
} else {
|
|
247
|
-
buffer = await readStreamOnceBuffer(target as
|
|
255
|
+
buffer = await readStreamOnceBuffer(target as Readable);
|
|
248
256
|
}
|
|
249
|
-
buffer = buffer.
|
|
257
|
+
buffer = buffer.subarray(0, 30);
|
|
250
258
|
if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
|
|
251
259
|
return getResult('image|jpg');
|
|
252
260
|
}
|
|
@@ -311,18 +319,18 @@ async function getMediaFileType(target: string | Buffer | ReadStream): Promise<g
|
|
|
311
319
|
}
|
|
312
320
|
|
|
313
321
|
/**
|
|
314
|
-
* 检查url资源是否可访问(此方法取文件首字节进行检查, 超时时(5s超时)
|
|
322
|
+
* 检查url资源是否可访问(此方法取文件首字节进行检查, 超时时(默认5s超时)返回false)
|
|
315
323
|
* @param url
|
|
316
324
|
* @returns
|
|
317
325
|
*/
|
|
318
|
-
async function checkURLResource(url: string) {
|
|
326
|
+
async function checkURLResource(url: string, timeout?: number) {
|
|
319
327
|
let isEffective = true;
|
|
320
328
|
try {
|
|
321
|
-
await hgo.get(url, { headers: { Range: 'bytes=0-0' }, resType: 'buffer' });
|
|
329
|
+
await hgo.get(url, { headers: { Range: 'bytes=0-0' }, resType: 'buffer', timeout });
|
|
322
330
|
} catch (e) {
|
|
323
|
-
if (e.name === 'timeoutError') {
|
|
324
|
-
|
|
325
|
-
}
|
|
331
|
+
// if (e.name === 'timeoutError') {
|
|
332
|
+
// throw e;
|
|
333
|
+
// }
|
|
326
334
|
isEffective = false;
|
|
327
335
|
}
|
|
328
336
|
return isEffective;
|
|
@@ -334,7 +342,7 @@ async function checkURLResource(url: string) {
|
|
|
334
342
|
* @param path 写入文件路径
|
|
335
343
|
* @param space json缩进
|
|
336
344
|
*/
|
|
337
|
-
function writeJsonFileSync(data: CustomObject | CustomObject[] | string, path: string, space =
|
|
345
|
+
function writeJsonFileSync(data: CustomObject | CustomObject[] | string, path: string, space = 4) {
|
|
338
346
|
let writeStr = '';
|
|
339
347
|
if (getValueType(data) === 'string') {
|
|
340
348
|
writeStr = data as string;
|
package/src/hgo.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as querystring from 'querystring';
|
|
2
2
|
import * as https from 'https';
|
|
3
3
|
import * as http from 'http';
|
|
4
|
-
import {
|
|
4
|
+
import { Readable } from 'stream';
|
|
5
5
|
|
|
6
6
|
type Method = 'get' | 'put' | 'post' | 'delete' | 'patch' | 'head';
|
|
7
7
|
type ResType = 'json' | 'buffer' | 'stream' | 'text';
|
|
@@ -54,7 +54,7 @@ interface reqSendBufferOptions extends baseReqOptions {
|
|
|
54
54
|
|
|
55
55
|
interface reqSendStreamOptions extends baseReqOptions {
|
|
56
56
|
method?: Method,
|
|
57
|
-
stream:
|
|
57
|
+
stream: Readable
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
interface reqSendMultiPartOptions extends baseReqOptions {
|
|
@@ -247,7 +247,7 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
|
|
|
247
247
|
stream.on('data', chunk => {
|
|
248
248
|
req.write(chunk, e => {
|
|
249
249
|
if (e) {
|
|
250
|
-
stream.
|
|
250
|
+
stream.destroy();
|
|
251
251
|
req.destroy();
|
|
252
252
|
reject(e);
|
|
253
253
|
}
|
|
@@ -258,7 +258,7 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
|
|
|
258
258
|
});
|
|
259
259
|
stream.on('error', e => {
|
|
260
260
|
req.destroy();
|
|
261
|
-
stream.
|
|
261
|
+
stream.destroy();
|
|
262
262
|
reject(e);
|
|
263
263
|
});
|
|
264
264
|
});
|
package/src/streamhelper.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import * as http from 'http';
|
|
1
|
+
import { Readable, Writable } from 'stream';
|
|
3
2
|
|
|
4
3
|
interface pipeOptions {
|
|
5
4
|
timeout?: number,
|
|
@@ -13,7 +12,7 @@ interface pipeOptions {
|
|
|
13
12
|
* @param options
|
|
14
13
|
* @returns
|
|
15
14
|
*/
|
|
16
|
-
async function pipe(source:
|
|
15
|
+
async function pipe(source: Readable, target: Writable, options?: pipeOptions): Promise<void> {
|
|
17
16
|
const { timeout, readBytesPreSec } = options || {};
|
|
18
17
|
return new Promise((resolve, reject) => {
|
|
19
18
|
if (readBytesPreSec) {
|
|
@@ -46,7 +45,7 @@ async function pipe(source: ReadStream | http.IncomingMessage, target: WriteStre
|
|
|
46
45
|
* @param stream 可读流
|
|
47
46
|
* @param readBytesPreSec 每秒读取的字节数
|
|
48
47
|
*/
|
|
49
|
-
function limitStreamFlowingRate(stream:
|
|
48
|
+
function limitStreamFlowingRate(stream: Readable, readBytesPreSec: number) {
|
|
50
49
|
let start = 0;
|
|
51
50
|
let checkInterval: any;
|
|
52
51
|
let calReadBytesTotal = 0;
|