cvitool 1.0.71 → 1.0.73
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 +9 -1
- package/build/src/cutil.js +145 -1
- package/build/src/hgo.d.ts +7 -1
- package/build/src/hgo.js +94 -55
- package/index.d.ts +13 -2
- package/package.json +6 -3
- package/src/cutil.ts +152 -1
- package/src/hgo.ts +112 -59
package/build/src/cutil.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
3
4
|
import { Encoding } from 'crypto';
|
|
5
|
+
import { ReadStream } from 'fs';
|
|
4
6
|
interface randomStringOptions {
|
|
5
7
|
special?: boolean;
|
|
6
8
|
lowercase?: boolean;
|
|
@@ -8,9 +10,15 @@ interface randomStringOptions {
|
|
|
8
10
|
number?: boolean;
|
|
9
11
|
specials?: string;
|
|
10
12
|
}
|
|
13
|
+
interface getMediaFileTypeRes {
|
|
14
|
+
type: string;
|
|
15
|
+
extname: string;
|
|
16
|
+
}
|
|
11
17
|
declare function randomString(length: number, options?: randomStringOptions): string;
|
|
12
18
|
declare function encryptCBC(data: string, length: 128 | 192 | 256, key: Buffer, iv: Buffer, inputEncoding?: Encoding): string;
|
|
13
19
|
declare function decryptCBC(encryptData: string, length: 128 | 192 | 256, key: Buffer, iv: Buffer, outputEncoding?: Encoding): string;
|
|
14
20
|
declare function md5(data: string, inputEncoding?: Encoding): string;
|
|
15
21
|
declare function execCmdCommand(command: string): Promise<unknown>;
|
|
16
|
-
|
|
22
|
+
declare function getValueType(value: any): "string" | "number" | "undefined" | "object" | "array" | "null" | "unkonw";
|
|
23
|
+
declare function getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes>;
|
|
24
|
+
export { randomStringOptions, getMediaFileTypeRes, randomString, encryptCBC, decryptCBC, md5, execCmdCommand, getValueType, getMediaFileType };
|
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.execCmdCommand = exports.md5 = exports.decryptCBC = exports.encryptCBC = exports.randomString = void 0;
|
|
12
|
+
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 hgo = require("./hgo");
|
|
15
16
|
function randomString(length, options) {
|
|
16
17
|
let { special = false, lowercase = true, upperCase = true, number = true, specials } = options || {};
|
|
17
18
|
if (specials) {
|
|
@@ -92,3 +93,146 @@ function execCmdCommand(command) {
|
|
|
92
93
|
});
|
|
93
94
|
}
|
|
94
95
|
exports.execCmdCommand = execCmdCommand;
|
|
96
|
+
function getValueType(value) {
|
|
97
|
+
const typeStr = Object.prototype.toString.call(value);
|
|
98
|
+
let type;
|
|
99
|
+
switch (typeStr) {
|
|
100
|
+
case '[object Number]':
|
|
101
|
+
type = 'number';
|
|
102
|
+
break;
|
|
103
|
+
case '[object String]':
|
|
104
|
+
type = 'string';
|
|
105
|
+
break;
|
|
106
|
+
case '[object Object]':
|
|
107
|
+
type = 'object';
|
|
108
|
+
break;
|
|
109
|
+
case '[object Array]':
|
|
110
|
+
type = 'array';
|
|
111
|
+
break;
|
|
112
|
+
case '[object Undefined]':
|
|
113
|
+
type = 'undefined';
|
|
114
|
+
break;
|
|
115
|
+
case '[object Null]':
|
|
116
|
+
type = 'null';
|
|
117
|
+
break;
|
|
118
|
+
default:
|
|
119
|
+
type = 'unkonw';
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
;
|
|
123
|
+
return type;
|
|
124
|
+
}
|
|
125
|
+
exports.getValueType = getValueType;
|
|
126
|
+
function getMediaFileType(target) {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
const readStreamOnceBuffer = (stream) => __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
return new Promise((resolve, reject) => {
|
|
130
|
+
stream.once('data', chunk => {
|
|
131
|
+
stream.close();
|
|
132
|
+
resolve(chunk);
|
|
133
|
+
});
|
|
134
|
+
stream.on('error', e => {
|
|
135
|
+
reject(e);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
const checkString = (waitMatchStr, byteStart, byteEnd, buffer) => {
|
|
140
|
+
return waitMatchStr === Buffer.from(buffer.slice(byteStart, byteEnd + 1)).toString();
|
|
141
|
+
};
|
|
142
|
+
const checkBuffer = (waitMatchBuffer, byteStart, byteEnd, buffer) => {
|
|
143
|
+
let equal = true;
|
|
144
|
+
const sliceBufferList = buffer.slice(byteStart, byteEnd + 1);
|
|
145
|
+
for (const [index, item] of waitMatchBuffer.entries()) {
|
|
146
|
+
if (item !== sliceBufferList[index]) {
|
|
147
|
+
equal = false;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return equal;
|
|
152
|
+
};
|
|
153
|
+
const getResult = (typeStr) => {
|
|
154
|
+
const arr = typeStr.split('|');
|
|
155
|
+
return {
|
|
156
|
+
type: arr[0],
|
|
157
|
+
extname: arr[1]
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
let buffer;
|
|
161
|
+
const targetType = this.getValueType(target);
|
|
162
|
+
if (targetType === 'string') {
|
|
163
|
+
const res = yield hgo.get(target, {
|
|
164
|
+
resType: 'buffer',
|
|
165
|
+
headers: {
|
|
166
|
+
Range: 'bytes=0-20'
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
buffer = res.resBody;
|
|
170
|
+
}
|
|
171
|
+
else if (targetType === 'array') {
|
|
172
|
+
buffer = target;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
buffer = yield readStreamOnceBuffer(target);
|
|
176
|
+
}
|
|
177
|
+
buffer = buffer.slice(0, 20);
|
|
178
|
+
if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
|
|
179
|
+
return getResult('image|jpg');
|
|
180
|
+
}
|
|
181
|
+
if (checkString('PNG', 1, 3, buffer)) {
|
|
182
|
+
return getResult('image|png');
|
|
183
|
+
}
|
|
184
|
+
if (checkBuffer([0x49, 0x44, 0x33], 0, 2, buffer) || checkBuffer([0xff, 0xf3], 0, 1, buffer)) {
|
|
185
|
+
return getResult('audio|mp3');
|
|
186
|
+
}
|
|
187
|
+
if (checkString('WAV', 8, 10, buffer)) {
|
|
188
|
+
return getResult('audio|wav');
|
|
189
|
+
}
|
|
190
|
+
if (checkString('M4A', 8, 10, buffer)) {
|
|
191
|
+
return getResult('audio|m4a');
|
|
192
|
+
}
|
|
193
|
+
if (checkString('heic', 8, 11, buffer) || checkString('mif1', 8, 11, buffer)) {
|
|
194
|
+
return getResult('image|heic');
|
|
195
|
+
}
|
|
196
|
+
if (checkString('ftyp', 4, 7, buffer)) {
|
|
197
|
+
if (checkString('ftypqt', 4, 9, buffer)) {
|
|
198
|
+
return getResult('video|mov');
|
|
199
|
+
}
|
|
200
|
+
return getResult('video|mp4');
|
|
201
|
+
}
|
|
202
|
+
if (checkString('WEBP', 8, 11, buffer)) {
|
|
203
|
+
return getResult('image|webp');
|
|
204
|
+
}
|
|
205
|
+
if (checkBuffer([0x47, 0x49, 0x46], 0, 2, buffer)) {
|
|
206
|
+
return getResult('image|gif');
|
|
207
|
+
}
|
|
208
|
+
if (checkBuffer([0x42, 0x4d], 0, 1, buffer)) {
|
|
209
|
+
return getResult('image|bmp');
|
|
210
|
+
}
|
|
211
|
+
if (checkBuffer([0x46, 0x4c, 0x56], 0, 2, buffer)) {
|
|
212
|
+
return getResult('video|flv');
|
|
213
|
+
}
|
|
214
|
+
if (checkBuffer([0x4f, 0x67, 0x67, 0x53], 0, 3, buffer)) {
|
|
215
|
+
return getResult('video|ogg');
|
|
216
|
+
}
|
|
217
|
+
if (checkBuffer([0x1a, 0x45, 0xdf, 0xa3], 0, 3, buffer)) {
|
|
218
|
+
return getResult('video|mkv');
|
|
219
|
+
}
|
|
220
|
+
if (checkBuffer([0x30, 0x26, 0xb2], 0, 2, buffer)) {
|
|
221
|
+
return getResult('video|wmv');
|
|
222
|
+
}
|
|
223
|
+
if (checkString('AVI', 8, 10, buffer)) {
|
|
224
|
+
return getResult('video|avi');
|
|
225
|
+
}
|
|
226
|
+
if (checkBuffer([0xff, 0xf1], 0, 1, buffer)) {
|
|
227
|
+
return getResult('audio|aac');
|
|
228
|
+
}
|
|
229
|
+
if (checkString('PCM', 10, 12, buffer)) {
|
|
230
|
+
return getResult('audio|pcm');
|
|
231
|
+
}
|
|
232
|
+
if (checkBuffer([0x66, 0x4c, 0x61, 0x43], 0, 3, buffer)) {
|
|
233
|
+
return getResult('audio|flac');
|
|
234
|
+
}
|
|
235
|
+
return getResult('unknow|unknow');
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
exports.getMediaFileType = getMediaFileType;
|
package/build/src/hgo.d.ts
CHANGED
|
@@ -28,10 +28,16 @@ interface reqOptions extends baseReqOptions {
|
|
|
28
28
|
body?: {
|
|
29
29
|
[key: string]: any;
|
|
30
30
|
};
|
|
31
|
+
retries?: number;
|
|
32
|
+
retryInterval?: number;
|
|
33
|
+
[key: string]: any;
|
|
31
34
|
}
|
|
32
35
|
interface reqSendBufferOptions extends baseReqOptions {
|
|
33
|
-
method?: Method;
|
|
34
36
|
buffer: Buffer;
|
|
37
|
+
method?: Method;
|
|
38
|
+
retries?: number;
|
|
39
|
+
retryInterval?: number;
|
|
40
|
+
[key: string]: any;
|
|
35
41
|
}
|
|
36
42
|
interface reqSendStreamOptions extends baseReqOptions {
|
|
37
43
|
method?: Method;
|
package/build/src/hgo.js
CHANGED
|
@@ -26,7 +26,7 @@ function getProtocol(url) {
|
|
|
26
26
|
}
|
|
27
27
|
function request(url, options) {
|
|
28
28
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
let { query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut, readTimeOut } = options || {};
|
|
29
|
+
let { query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds } = options || {};
|
|
30
30
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
31
31
|
connectTimeOut = timeout;
|
|
32
32
|
readTimeOut = timeout;
|
|
@@ -49,72 +49,108 @@ function request(url, options) {
|
|
|
49
49
|
'Content-length': Buffer.byteLength(data)
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
req.end();
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
req.write(data, (e) => {
|
|
74
|
-
req.end();
|
|
75
|
-
if (e) {
|
|
76
|
-
req.destroy();
|
|
77
|
-
reject(e);
|
|
78
|
-
}
|
|
52
|
+
let res;
|
|
53
|
+
try {
|
|
54
|
+
res = yield new Promise((resolve, reject) => {
|
|
55
|
+
const req = protocol.request(url, {
|
|
56
|
+
timeout: connectTimeOut,
|
|
57
|
+
headers: Object.assign(headers, baseHeaders),
|
|
58
|
+
method,
|
|
59
|
+
agent
|
|
60
|
+
}, res => {
|
|
61
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
62
|
+
});
|
|
63
|
+
req.on('timeout', () => {
|
|
64
|
+
req.destroy();
|
|
65
|
+
reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
|
|
66
|
+
});
|
|
67
|
+
req.on('error', e => {
|
|
68
|
+
req.destroy();
|
|
69
|
+
reject(e);
|
|
79
70
|
});
|
|
71
|
+
if (isbodyEmpty) {
|
|
72
|
+
req.end();
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
req.write(data, (e) => {
|
|
76
|
+
req.end();
|
|
77
|
+
if (e) {
|
|
78
|
+
req.destroy();
|
|
79
|
+
reject(e);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
if ((retrieds || 0) < (retries || 0)) {
|
|
87
|
+
if (retryInterval) {
|
|
88
|
+
yield new Promise((resolve) => {
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
resolve(1);
|
|
91
|
+
}, retryInterval);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
res = yield request(url, Object.assign(Object.assign({}, options), { retrieds: (retrieds || 0) + 1 }));
|
|
95
|
+
return res;
|
|
80
96
|
}
|
|
81
|
-
|
|
97
|
+
throw e;
|
|
98
|
+
}
|
|
99
|
+
return res;
|
|
82
100
|
});
|
|
83
101
|
}
|
|
84
102
|
exports.request = request;
|
|
85
103
|
function reqSendBuffer(url, options) {
|
|
86
104
|
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
let { timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut } = options;
|
|
105
|
+
let { timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds } = options;
|
|
88
106
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
89
107
|
connectTimeOut = timeout;
|
|
90
108
|
readTimeOut = timeout;
|
|
91
109
|
}
|
|
92
110
|
const protocol = getProtocol(url);
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
});
|
|
110
|
-
req.write(buffer, (e) => {
|
|
111
|
-
req.end();
|
|
112
|
-
if (e) {
|
|
111
|
+
let res;
|
|
112
|
+
try {
|
|
113
|
+
res = yield new Promise((resolve, reject) => {
|
|
114
|
+
const req = protocol.request(url, {
|
|
115
|
+
timeout: connectTimeOut,
|
|
116
|
+
headers: Object.assign(headers, { 'Content-Length': buffer.byteLength }),
|
|
117
|
+
method,
|
|
118
|
+
agent
|
|
119
|
+
}, res => {
|
|
120
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
121
|
+
});
|
|
122
|
+
req.on('timeout', () => {
|
|
123
|
+
req.destroy();
|
|
124
|
+
reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
|
|
125
|
+
});
|
|
126
|
+
req.on('error', e => {
|
|
113
127
|
req.destroy();
|
|
114
128
|
reject(e);
|
|
115
|
-
}
|
|
129
|
+
});
|
|
130
|
+
req.write(buffer, (e) => {
|
|
131
|
+
req.end();
|
|
132
|
+
if (e) {
|
|
133
|
+
req.destroy();
|
|
134
|
+
reject(e);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
116
137
|
});
|
|
117
|
-
}
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
if ((retrieds || 0) < (retries || 0)) {
|
|
141
|
+
if (retryInterval) {
|
|
142
|
+
yield new Promise((resolve) => {
|
|
143
|
+
setTimeout(() => {
|
|
144
|
+
resolve(1);
|
|
145
|
+
}, retryInterval);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
res = yield reqSendBuffer(url, Object.assign(Object.assign({}, options), { retrieds: (retrieds || 0) + 1 }));
|
|
149
|
+
return res;
|
|
150
|
+
}
|
|
151
|
+
throw e;
|
|
152
|
+
}
|
|
153
|
+
return res;
|
|
118
154
|
});
|
|
119
155
|
}
|
|
120
156
|
exports.reqSendBuffer = reqSendBuffer;
|
|
@@ -126,7 +162,7 @@ function reqSendStream(url, options) {
|
|
|
126
162
|
readTimeOut = timeout;
|
|
127
163
|
}
|
|
128
164
|
const protocol = getProtocol(url);
|
|
129
|
-
|
|
165
|
+
const res = yield new Promise((resolve, reject) => {
|
|
130
166
|
const baseHeaders = {
|
|
131
167
|
'Content-Type': 'application/octet-stream',
|
|
132
168
|
'Transfer-Encoding': 'chunked',
|
|
@@ -151,6 +187,7 @@ function reqSendStream(url, options) {
|
|
|
151
187
|
stream.on('data', chunk => {
|
|
152
188
|
req.write(chunk, e => {
|
|
153
189
|
if (e) {
|
|
190
|
+
stream.close();
|
|
154
191
|
req.destroy();
|
|
155
192
|
reject(e);
|
|
156
193
|
}
|
|
@@ -158,7 +195,6 @@ function reqSendStream(url, options) {
|
|
|
158
195
|
});
|
|
159
196
|
stream.on('end', () => {
|
|
160
197
|
req.end();
|
|
161
|
-
stream.close();
|
|
162
198
|
});
|
|
163
199
|
stream.on('error', e => {
|
|
164
200
|
req.destroy();
|
|
@@ -166,6 +202,7 @@ function reqSendStream(url, options) {
|
|
|
166
202
|
reject(e);
|
|
167
203
|
});
|
|
168
204
|
});
|
|
205
|
+
return res;
|
|
169
206
|
});
|
|
170
207
|
}
|
|
171
208
|
exports.reqSendStream = reqSendStream;
|
|
@@ -177,7 +214,7 @@ function reqSendMultiPart(url, options) {
|
|
|
177
214
|
readTimeOut = timeout;
|
|
178
215
|
}
|
|
179
216
|
const protocol = getProtocol(url);
|
|
180
|
-
|
|
217
|
+
const res = yield new Promise((resolve, reject) => {
|
|
181
218
|
const req = protocol.request(url, {
|
|
182
219
|
timeout: connectTimeOut,
|
|
183
220
|
headers: Object.assign(headers, Object.assign({}, form.getHeaders())),
|
|
@@ -192,6 +229,7 @@ function reqSendMultiPart(url, options) {
|
|
|
192
229
|
});
|
|
193
230
|
req.on('error', e => {
|
|
194
231
|
req.destroy();
|
|
232
|
+
form.destroy();
|
|
195
233
|
reject(e);
|
|
196
234
|
});
|
|
197
235
|
form.pipe(req);
|
|
@@ -204,6 +242,7 @@ function reqSendMultiPart(url, options) {
|
|
|
204
242
|
reject(e);
|
|
205
243
|
});
|
|
206
244
|
});
|
|
245
|
+
return res;
|
|
207
246
|
});
|
|
208
247
|
}
|
|
209
248
|
exports.reqSendMultiPart = reqSendMultiPart;
|
package/index.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ import {
|
|
|
14
14
|
} from './src/streamhelper';
|
|
15
15
|
|
|
16
16
|
import {
|
|
17
|
-
randomStringOptions
|
|
17
|
+
randomStringOptions,
|
|
18
|
+
getMediaFileTypeRes
|
|
18
19
|
} from './src/cutil';
|
|
19
20
|
|
|
20
21
|
interface Hgo {
|
|
@@ -119,7 +120,17 @@ interface Cutil {
|
|
|
119
120
|
* 调用终端执行命令
|
|
120
121
|
* @param command
|
|
121
122
|
*/
|
|
122
|
-
execCmdCommand(command: string): string
|
|
123
|
+
execCmdCommand(command: string): Promise<string>
|
|
124
|
+
/**
|
|
125
|
+
* 获取给定值node类型
|
|
126
|
+
* @param value
|
|
127
|
+
*/
|
|
128
|
+
getValueType(value: any): string
|
|
129
|
+
/**
|
|
130
|
+
* 获取媒体文件类型
|
|
131
|
+
* @param target 文件url/buffer数据/可读流
|
|
132
|
+
*/
|
|
133
|
+
getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes>
|
|
123
134
|
}
|
|
124
135
|
|
|
125
136
|
declare const hgo: Hgo;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cvitool",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.73",
|
|
4
4
|
"description": "cvitool",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,11 +9,14 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {},
|
|
11
11
|
"devDependencies": {
|
|
12
|
+
"@types/node": "20.10.4",
|
|
12
13
|
"@typescript-eslint/eslint-plugin": "5.54.0",
|
|
13
14
|
"@typescript-eslint/parser": "5.54.0",
|
|
14
15
|
"eslint": "7.32.0",
|
|
15
|
-
"eslint-config-standard": "16.0.3"
|
|
16
|
-
|
|
16
|
+
"eslint-config-standard": "16.0.3"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=16.0.0"
|
|
17
20
|
},
|
|
18
21
|
"files": [
|
|
19
22
|
"build/src",
|
package/src/cutil.ts
CHANGED
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
import {
|
|
9
9
|
spawn
|
|
10
10
|
} from 'child_process';
|
|
11
|
+
import { ReadStream } from 'fs';
|
|
12
|
+
import * as hgo from './hgo';
|
|
11
13
|
|
|
12
14
|
interface randomStringOptions {
|
|
13
15
|
special?: boolean,
|
|
@@ -17,6 +19,11 @@ interface randomStringOptions {
|
|
|
17
19
|
specials?: string
|
|
18
20
|
}
|
|
19
21
|
|
|
22
|
+
interface getMediaFileTypeRes {
|
|
23
|
+
type: string,
|
|
24
|
+
extname: string
|
|
25
|
+
}
|
|
26
|
+
|
|
20
27
|
function randomString(length: number, options?: randomStringOptions) {
|
|
21
28
|
let { special = false, lowercase = true, upperCase = true, number = true, specials } = options || {};
|
|
22
29
|
if (specials) {
|
|
@@ -93,11 +100,155 @@ async function execCmdCommand(command: string) {
|
|
|
93
100
|
});
|
|
94
101
|
}
|
|
95
102
|
|
|
103
|
+
function getValueType(value: any) {
|
|
104
|
+
const typeStr = Object.prototype.toString.call(value);
|
|
105
|
+
let type: 'number' | 'string' | 'array' | 'object' | 'undefined' | 'null' | 'unkonw';
|
|
106
|
+
switch (typeStr) {
|
|
107
|
+
case '[object Number]':
|
|
108
|
+
type = 'number';
|
|
109
|
+
break;
|
|
110
|
+
case '[object String]':
|
|
111
|
+
type = 'string';
|
|
112
|
+
break;
|
|
113
|
+
case '[object Object]':
|
|
114
|
+
type = 'object';
|
|
115
|
+
break;
|
|
116
|
+
case '[object Array]':
|
|
117
|
+
type = 'array';
|
|
118
|
+
break;
|
|
119
|
+
case '[object Undefined]':
|
|
120
|
+
type = 'undefined';
|
|
121
|
+
break;
|
|
122
|
+
case '[object Null]':
|
|
123
|
+
type = 'null';
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
type = 'unkonw';
|
|
127
|
+
break;
|
|
128
|
+
};
|
|
129
|
+
return type;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes> {
|
|
133
|
+
const readStreamOnceBuffer = async (stream: ReadStream): Promise<Buffer> => {
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
stream.once('data', chunk => {
|
|
136
|
+
stream.close();
|
|
137
|
+
resolve(chunk as Buffer);
|
|
138
|
+
});
|
|
139
|
+
stream.on('error', e => {
|
|
140
|
+
reject(e);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
const checkString = (waitMatchStr: string, byteStart: number, byteEnd: number, buffer: Buffer) => {
|
|
145
|
+
return waitMatchStr === Buffer.from(buffer.slice(byteStart, byteEnd + 1)).toString();
|
|
146
|
+
};
|
|
147
|
+
const checkBuffer = (waitMatchBuffer: number[], byteStart: number, byteEnd: number, buffer: Buffer) => {
|
|
148
|
+
let equal = true;
|
|
149
|
+
const sliceBufferList = buffer.slice(byteStart, byteEnd + 1);
|
|
150
|
+
for (const [index, item] of waitMatchBuffer.entries()) {
|
|
151
|
+
if (item !== sliceBufferList[index]) {
|
|
152
|
+
equal = false;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return equal;
|
|
157
|
+
};
|
|
158
|
+
const getResult = (typeStr: string) => {
|
|
159
|
+
const arr = typeStr.split('|');
|
|
160
|
+
return {
|
|
161
|
+
type: arr[0],
|
|
162
|
+
extname: arr[1]
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
let buffer: Buffer;
|
|
166
|
+
const targetType = this.getValueType(target);
|
|
167
|
+
if (targetType === 'string') {
|
|
168
|
+
const res = await hgo.get(
|
|
169
|
+
target as string,
|
|
170
|
+
{
|
|
171
|
+
resType: 'buffer',
|
|
172
|
+
headers: {
|
|
173
|
+
Range: 'bytes=0-20'
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
buffer = res.resBody as Buffer;
|
|
178
|
+
} else if (targetType === 'array') {
|
|
179
|
+
buffer = target as Buffer;
|
|
180
|
+
} else {
|
|
181
|
+
buffer = await readStreamOnceBuffer(target as ReadStream);
|
|
182
|
+
}
|
|
183
|
+
buffer = buffer.slice(0, 20);
|
|
184
|
+
if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
|
|
185
|
+
return getResult('image|jpg');
|
|
186
|
+
}
|
|
187
|
+
if (checkString('PNG', 1, 3, buffer)) {
|
|
188
|
+
return getResult('image|png');
|
|
189
|
+
}
|
|
190
|
+
if (checkBuffer([0x49, 0x44, 0x33], 0, 2, buffer) || checkBuffer([0xff, 0xf3], 0, 1, buffer)) {
|
|
191
|
+
return getResult('audio|mp3');
|
|
192
|
+
}
|
|
193
|
+
if (checkString('WAV', 8, 10, buffer)) {
|
|
194
|
+
return getResult('audio|wav');
|
|
195
|
+
}
|
|
196
|
+
if (checkString('M4A', 8, 10, buffer)) {
|
|
197
|
+
return getResult('audio|m4a');
|
|
198
|
+
}
|
|
199
|
+
if (checkString('heic', 8, 11, buffer) || checkString('mif1', 8, 11, buffer)) {
|
|
200
|
+
return getResult('image|heic');
|
|
201
|
+
}
|
|
202
|
+
if (checkString('ftyp', 4, 7, buffer)) {
|
|
203
|
+
if (checkString('ftypqt', 4, 9, buffer)) {
|
|
204
|
+
return getResult('video|mov');
|
|
205
|
+
}
|
|
206
|
+
return getResult('video|mp4');
|
|
207
|
+
}
|
|
208
|
+
if (checkString('WEBP', 8, 11, buffer)) {
|
|
209
|
+
return getResult('image|webp');
|
|
210
|
+
}
|
|
211
|
+
if (checkBuffer([0x47, 0x49, 0x46], 0, 2, buffer)) {
|
|
212
|
+
return getResult('image|gif');
|
|
213
|
+
}
|
|
214
|
+
if (checkBuffer([0x42, 0x4d], 0, 1, buffer)) {
|
|
215
|
+
return getResult('image|bmp');
|
|
216
|
+
}
|
|
217
|
+
if (checkBuffer([0x46, 0x4c, 0x56], 0, 2, buffer)) {
|
|
218
|
+
return getResult('video|flv');
|
|
219
|
+
}
|
|
220
|
+
if (checkBuffer([0x4f, 0x67, 0x67, 0x53], 0, 3, buffer)) {
|
|
221
|
+
return getResult('video|ogg');
|
|
222
|
+
}
|
|
223
|
+
if (checkBuffer([0x1a, 0x45, 0xdf, 0xa3], 0, 3, buffer)) {
|
|
224
|
+
return getResult('video|mkv');
|
|
225
|
+
}
|
|
226
|
+
if (checkBuffer([0x30, 0x26, 0xb2], 0, 2, buffer)) {
|
|
227
|
+
return getResult('video|wmv');
|
|
228
|
+
}
|
|
229
|
+
if (checkString('AVI', 8, 10, buffer)) {
|
|
230
|
+
return getResult('video|avi');
|
|
231
|
+
}
|
|
232
|
+
if (checkBuffer([0xff, 0xf1], 0, 1, buffer)) {
|
|
233
|
+
return getResult('audio|aac');
|
|
234
|
+
}
|
|
235
|
+
if (checkString('PCM', 10, 12, buffer)) {
|
|
236
|
+
return getResult('audio|pcm');
|
|
237
|
+
}
|
|
238
|
+
if (checkBuffer([0x66, 0x4c, 0x61, 0x43], 0, 3, buffer)) {
|
|
239
|
+
return getResult('audio|flac');
|
|
240
|
+
}
|
|
241
|
+
return getResult('unknow|unknow');
|
|
242
|
+
}
|
|
243
|
+
|
|
96
244
|
export {
|
|
97
245
|
randomStringOptions,
|
|
246
|
+
getMediaFileTypeRes,
|
|
98
247
|
randomString,
|
|
99
248
|
encryptCBC,
|
|
100
249
|
decryptCBC,
|
|
101
250
|
md5,
|
|
102
|
-
execCmdCommand
|
|
251
|
+
execCmdCommand,
|
|
252
|
+
getValueType,
|
|
253
|
+
getMediaFileType
|
|
103
254
|
};
|
package/src/hgo.ts
CHANGED
|
@@ -18,7 +18,7 @@ interface baseReqOptions {
|
|
|
18
18
|
},
|
|
19
19
|
resType?: ResType,
|
|
20
20
|
connectTimeOut?: number,
|
|
21
|
-
readTimeOut?: number
|
|
21
|
+
readTimeOut?: number
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
interface reqOptions extends baseReqOptions {
|
|
@@ -28,12 +28,18 @@ interface reqOptions extends baseReqOptions {
|
|
|
28
28
|
},
|
|
29
29
|
body?: {
|
|
30
30
|
[key: string]: any
|
|
31
|
-
}
|
|
31
|
+
},
|
|
32
|
+
retries?: number,
|
|
33
|
+
retryInterval?: number,
|
|
34
|
+
[key: string]: any
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
interface reqSendBufferOptions extends baseReqOptions {
|
|
38
|
+
buffer: Buffer,
|
|
35
39
|
method?: Method,
|
|
36
|
-
|
|
40
|
+
retries?: number,
|
|
41
|
+
retryInterval?: number,
|
|
42
|
+
[key: string]: any
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
interface reqSendStreamOptions extends baseReqOptions {
|
|
@@ -65,7 +71,10 @@ function getProtocol(url: string) {
|
|
|
65
71
|
}
|
|
66
72
|
|
|
67
73
|
async function request(url: string, options?: reqOptions): Promise<ResData> {
|
|
68
|
-
let {
|
|
74
|
+
let {
|
|
75
|
+
query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut,
|
|
76
|
+
readTimeOut, retries, retryInterval, retrieds
|
|
77
|
+
} = options || {};
|
|
69
78
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
70
79
|
connectTimeOut = timeout;
|
|
71
80
|
readTimeOut = timeout;
|
|
@@ -87,79 +96,118 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
|
|
|
87
96
|
'Content-length': Buffer.byteLength(data)
|
|
88
97
|
};
|
|
89
98
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
req.destroy();
|
|
105
|
-
reject(e);
|
|
106
|
-
});
|
|
107
|
-
if (isbodyEmpty) {
|
|
108
|
-
req.end();
|
|
109
|
-
} else {
|
|
110
|
-
req.write(data, (e) => {
|
|
111
|
-
req.end();
|
|
112
|
-
if (e) {
|
|
113
|
-
req.destroy();
|
|
114
|
-
reject(e);
|
|
115
|
-
}
|
|
99
|
+
let res: ResData;
|
|
100
|
+
try {
|
|
101
|
+
res = await new Promise((resolve, reject) => {
|
|
102
|
+
const req = protocol.request(url, {
|
|
103
|
+
timeout: connectTimeOut,
|
|
104
|
+
headers: Object.assign(headers, baseHeaders),
|
|
105
|
+
method,
|
|
106
|
+
agent
|
|
107
|
+
}, res => {
|
|
108
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
109
|
+
});
|
|
110
|
+
req.on('timeout', () => {
|
|
111
|
+
req.destroy();
|
|
112
|
+
reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
|
|
116
113
|
});
|
|
114
|
+
req.on('error', e => {
|
|
115
|
+
req.destroy();
|
|
116
|
+
reject(e);
|
|
117
|
+
});
|
|
118
|
+
if (isbodyEmpty) {
|
|
119
|
+
req.end();
|
|
120
|
+
} else {
|
|
121
|
+
req.write(data, (e) => {
|
|
122
|
+
req.end();
|
|
123
|
+
if (e) {
|
|
124
|
+
req.destroy();
|
|
125
|
+
reject(e);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
} catch (e) {
|
|
131
|
+
if ((retrieds || 0) < (retries || 0)) {
|
|
132
|
+
if (retryInterval) {
|
|
133
|
+
await new Promise((resolve) => {
|
|
134
|
+
setTimeout(() => {
|
|
135
|
+
resolve(1);
|
|
136
|
+
}, retryInterval);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
res = await request(url, { ...options, retrieds: (retrieds || 0) + 1 });
|
|
140
|
+
return res;
|
|
117
141
|
}
|
|
118
|
-
|
|
142
|
+
throw e;
|
|
143
|
+
}
|
|
144
|
+
return res;
|
|
119
145
|
}
|
|
120
146
|
|
|
121
147
|
async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promise<ResData> {
|
|
122
|
-
let {
|
|
148
|
+
let {
|
|
149
|
+
timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut,
|
|
150
|
+
retries, retryInterval, retrieds
|
|
151
|
+
} = options;
|
|
123
152
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
124
153
|
connectTimeOut = timeout;
|
|
125
154
|
readTimeOut = timeout;
|
|
126
155
|
}
|
|
127
156
|
const protocol = getProtocol(url);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
});
|
|
145
|
-
req.write(buffer, (e) => {
|
|
146
|
-
req.end();
|
|
147
|
-
if (e) {
|
|
157
|
+
let res: ResData;
|
|
158
|
+
try {
|
|
159
|
+
res = await new Promise((resolve, reject) => {
|
|
160
|
+
const req = protocol.request(url, {
|
|
161
|
+
timeout: connectTimeOut,
|
|
162
|
+
headers: Object.assign(headers, { 'Content-Length': buffer.byteLength }),
|
|
163
|
+
method,
|
|
164
|
+
agent
|
|
165
|
+
}, res => {
|
|
166
|
+
resHandld(res, resolve, reject, resType, method, readTimeOut, req);
|
|
167
|
+
});
|
|
168
|
+
req.on('timeout', () => {
|
|
169
|
+
req.destroy();
|
|
170
|
+
reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
|
|
171
|
+
});
|
|
172
|
+
req.on('error', e => {
|
|
148
173
|
req.destroy();
|
|
149
174
|
reject(e);
|
|
150
|
-
}
|
|
175
|
+
});
|
|
176
|
+
req.write(buffer, (e) => {
|
|
177
|
+
req.end();
|
|
178
|
+
if (e) {
|
|
179
|
+
req.destroy();
|
|
180
|
+
reject(e);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
151
183
|
});
|
|
152
|
-
})
|
|
184
|
+
} catch (e) {
|
|
185
|
+
if ((retrieds || 0) < (retries || 0)) {
|
|
186
|
+
if (retryInterval) {
|
|
187
|
+
await new Promise((resolve) => {
|
|
188
|
+
setTimeout(() => {
|
|
189
|
+
resolve(1);
|
|
190
|
+
}, retryInterval);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
res = await reqSendBuffer(url, { ...options, retrieds: (retrieds || 0) + 1 });
|
|
194
|
+
return res;
|
|
195
|
+
}
|
|
196
|
+
throw e;
|
|
197
|
+
}
|
|
198
|
+
return res;
|
|
153
199
|
}
|
|
154
200
|
|
|
155
201
|
async function reqSendStream(url: string, options: reqSendStreamOptions): Promise<ResData> {
|
|
156
|
-
let {
|
|
202
|
+
let {
|
|
203
|
+
timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut
|
|
204
|
+
} = options;
|
|
157
205
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
158
206
|
connectTimeOut = timeout;
|
|
159
207
|
readTimeOut = timeout;
|
|
160
208
|
}
|
|
161
209
|
const protocol = getProtocol(url);
|
|
162
|
-
|
|
210
|
+
const res: ResData = await new Promise((resolve, reject) => {
|
|
163
211
|
const baseHeaders = {
|
|
164
212
|
'Content-Type': 'application/octet-stream',
|
|
165
213
|
'Transfer-Encoding': 'chunked',
|
|
@@ -184,6 +232,7 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
|
|
|
184
232
|
stream.on('data', chunk => {
|
|
185
233
|
req.write(chunk, e => {
|
|
186
234
|
if (e) {
|
|
235
|
+
stream.close();
|
|
187
236
|
req.destroy();
|
|
188
237
|
reject(e);
|
|
189
238
|
}
|
|
@@ -191,7 +240,6 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
|
|
|
191
240
|
});
|
|
192
241
|
stream.on('end', () => {
|
|
193
242
|
req.end();
|
|
194
|
-
stream.close();
|
|
195
243
|
});
|
|
196
244
|
stream.on('error', e => {
|
|
197
245
|
req.destroy();
|
|
@@ -199,16 +247,19 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
|
|
|
199
247
|
reject(e);
|
|
200
248
|
});
|
|
201
249
|
});
|
|
250
|
+
return res;
|
|
202
251
|
}
|
|
203
252
|
|
|
204
253
|
async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions): Promise<ResData> {
|
|
205
|
-
let {
|
|
254
|
+
let {
|
|
255
|
+
timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut
|
|
256
|
+
} = options;
|
|
206
257
|
if (timeout && !(connectTimeOut && readTimeOut)) {
|
|
207
258
|
connectTimeOut = timeout;
|
|
208
259
|
readTimeOut = timeout;
|
|
209
260
|
}
|
|
210
261
|
const protocol = getProtocol(url);
|
|
211
|
-
|
|
262
|
+
const res: ResData = await new Promise((resolve, reject) => {
|
|
212
263
|
const req = protocol.request(url, {
|
|
213
264
|
timeout: connectTimeOut,
|
|
214
265
|
headers: Object.assign(headers, { ...form.getHeaders() }),
|
|
@@ -223,6 +274,7 @@ async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions):
|
|
|
223
274
|
});
|
|
224
275
|
req.on('error', e => {
|
|
225
276
|
req.destroy();
|
|
277
|
+
form.destroy();
|
|
226
278
|
reject(e);
|
|
227
279
|
});
|
|
228
280
|
form.pipe(req);
|
|
@@ -235,6 +287,7 @@ async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions):
|
|
|
235
287
|
reject(e);
|
|
236
288
|
});
|
|
237
289
|
});
|
|
290
|
+
return res;
|
|
238
291
|
}
|
|
239
292
|
|
|
240
293
|
function resHandld(res: http.IncomingMessage, resolve: any, reject: any, resType: ResType, method: Method, readTimeOut: number, req: http.ClientRequest) {
|