cvitool 1.0.750 → 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.
@@ -1,5 +1,5 @@
1
1
  import { Encoding } from 'crypto';
2
- import { ReadStream } from 'fs';
2
+ import { Readable } from 'stream';
3
3
  interface randomStringOptions {
4
4
  special?: boolean;
5
5
  lowercase?: boolean;
@@ -64,13 +64,13 @@ declare function execCmdCommand(command: string, timeout?: number): Promise<unkn
64
64
  * @param value
65
65
  * @returns
66
66
  */
67
- 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";
68
68
  /**
69
69
  * 获取媒体文件类型(此方法取文件前30个字节进行检查)
70
70
  * @param target
71
71
  * @returns
72
72
  */
73
- declare function getMediaFileType(target: string | Buffer | ReadStream): Promise<getMediaFileTypeRes>;
73
+ declare function getMediaFileType(target: string | Buffer | Readable): Promise<getMediaFileTypeRes>;
74
74
  /**
75
75
  * 检查url资源是否可访问(此方法取文件首字节进行检查, 超时时(默认5s超时)返回false)
76
76
  * @param url
@@ -172,6 +172,13 @@ function getValueType(value) {
172
172
  case '[object Boolean]':
173
173
  type = 'boolean';
174
174
  break;
175
+ case '[object Uint8Array]':
176
+ type = 'buffer';
177
+ break;
178
+ case '[object AsyncFunction]':
179
+ case '[object Function]':
180
+ type = 'function';
181
+ break;
175
182
  case '[object Undefined]':
176
183
  type = 'undefined';
177
184
  break;
@@ -195,7 +202,7 @@ function getMediaFileType(target) {
195
202
  const readStreamOnceBuffer = (stream) => __awaiter(this, void 0, void 0, function* () {
196
203
  return new Promise((resolve, reject) => {
197
204
  stream.once('data', chunk => {
198
- stream.close();
205
+ stream.destroy();
199
206
  resolve(chunk);
200
207
  });
201
208
  stream.on('error', e => {
@@ -204,11 +211,11 @@ function getMediaFileType(target) {
204
211
  });
205
212
  });
206
213
  const checkString = (waitMatchStr, byteStart, byteEnd, buffer) => {
207
- return waitMatchStr === Buffer.from(buffer.slice(byteStart, byteEnd + 1)).toString();
214
+ return waitMatchStr === Buffer.from(buffer.subarray(byteStart, byteEnd + 1)).toString();
208
215
  };
209
216
  const checkBuffer = (waitMatchBuffer, byteStart, byteEnd, buffer) => {
210
217
  let equal = true;
211
- const sliceBufferList = buffer.slice(byteStart, byteEnd + 1);
218
+ const sliceBufferList = buffer.subarray(byteStart, byteEnd + 1);
212
219
  for (const [index, item] of waitMatchBuffer.entries()) {
213
220
  if (item !== sliceBufferList[index]) {
214
221
  equal = false;
@@ -225,7 +232,7 @@ function getMediaFileType(target) {
225
232
  };
226
233
  };
227
234
  let buffer;
228
- const targetType = this.getValueType(target);
235
+ const targetType = getValueType(target);
229
236
  if (targetType === 'string') {
230
237
  const res = yield hgo.get(target, {
231
238
  resType: 'buffer',
@@ -235,13 +242,13 @@ function getMediaFileType(target) {
235
242
  });
236
243
  buffer = res.resBody;
237
244
  }
238
- else if (targetType === 'array') {
245
+ else if (targetType === 'buffer') {
239
246
  buffer = target;
240
247
  }
241
248
  else {
242
249
  buffer = yield readStreamOnceBuffer(target);
243
250
  }
244
- buffer = buffer.slice(0, 30);
251
+ buffer = buffer.subarray(0, 30);
245
252
  if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
246
253
  return getResult('image|jpg');
247
254
  }
@@ -331,7 +338,7 @@ function checkURLResource(url, timeout) {
331
338
  * @param path 写入文件路径
332
339
  * @param space json缩进
333
340
  */
334
- function writeJsonFileSync(data, path, space = 3) {
341
+ function writeJsonFileSync(data, path, space = 4) {
335
342
  let writeStr = '';
336
343
  if (getValueType(data) === 'string') {
337
344
  writeStr = data;
@@ -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 { ReadStream } from 'fs';
4
+ import { Readable } from 'stream';
5
5
  type Method = 'get' | 'put' | 'post' | 'delete' | 'patch' | 'head';
6
6
  type ResType = 'json' | 'buffer' | 'stream' | 'text';
7
7
  interface CustomObject {
@@ -47,7 +47,7 @@ interface reqSendBufferOptions extends baseReqOptions {
47
47
  }
48
48
  interface reqSendStreamOptions extends baseReqOptions {
49
49
  method?: Method;
50
- stream: ReadStream;
50
+ stream: Readable;
51
51
  }
52
52
  interface reqSendMultiPartOptions extends baseReqOptions {
53
53
  form: any;
package/build/src/hgo.js CHANGED
@@ -197,7 +197,7 @@ function reqSendStream(url, options) {
197
197
  stream.on('data', chunk => {
198
198
  req.write(chunk, e => {
199
199
  if (e) {
200
- stream.close();
200
+ stream.destroy();
201
201
  req.destroy();
202
202
  reject(e);
203
203
  }
@@ -208,7 +208,7 @@ function reqSendStream(url, options) {
208
208
  });
209
209
  stream.on('error', e => {
210
210
  req.destroy();
211
- stream.close();
211
+ stream.destroy();
212
212
  reject(e);
213
213
  });
214
214
  });
@@ -1,5 +1,4 @@
1
- import { ReadStream, WriteStream } from 'fs';
2
- import * as http from 'http';
1
+ import { Readable, Writable } from 'stream';
3
2
  interface pipeOptions {
4
3
  timeout?: number;
5
4
  readBytesPreSec?: number;
@@ -11,11 +10,11 @@ interface pipeOptions {
11
10
  * @param options
12
11
  * @returns
13
12
  */
14
- declare function pipe(source: ReadStream | http.IncomingMessage, target: WriteStream, options?: pipeOptions): Promise<void>;
13
+ declare function pipe(source: Readable, target: Writable, options?: pipeOptions): Promise<void>;
15
14
  /**
16
15
  * stream传输限流
17
16
  * @param stream 可读流
18
17
  * @param readBytesPreSec 每秒读取的字节数
19
18
  */
20
- declare function limitStreamFlowingRate(stream: ReadStream | http.IncomingMessage, readBytesPreSec: number): void;
19
+ declare function limitStreamFlowingRate(stream: Readable, readBytesPreSec: number): void;
21
20
  export { pipeOptions, pipe, limitStreamFlowingRate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvitool",
3
- "version": "1.0.750",
3
+ "version": "1.0.751",
4
4
  "description": "cvitool",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/cutil.ts CHANGED
@@ -9,8 +9,9 @@ import {
9
9
  import {
10
10
  spawn
11
11
  } from 'child_process';
12
- import { ReadStream, writeFileSync, readFileSync } from 'fs';
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 | ReadStream): Promise<getMediaFileTypeRes> {
199
- const readStreamOnceBuffer = async (stream: ReadStream): Promise<Buffer> => {
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.close();
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.slice(byteStart, byteEnd + 1)).toString();
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.slice(byteStart, byteEnd + 1);
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 = this.getValueType(target);
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 === 'array') {
252
+ } else if (targetType === 'buffer') {
245
253
  buffer = target as Buffer;
246
254
  } else {
247
- buffer = await readStreamOnceBuffer(target as ReadStream);
255
+ buffer = await readStreamOnceBuffer(target as Readable);
248
256
  }
249
- buffer = buffer.slice(0, 30);
257
+ buffer = buffer.subarray(0, 30);
250
258
  if (checkBuffer([0xff, 0xd8], 0, 1, buffer)) {
251
259
  return getResult('image|jpg');
252
260
  }
@@ -334,7 +342,7 @@ async function checkURLResource(url: string, timeout?: number) {
334
342
  * @param path 写入文件路径
335
343
  * @param space json缩进
336
344
  */
337
- function writeJsonFileSync(data: CustomObject | CustomObject[] | string, path: string, space = 3) {
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 { ReadStream } from 'fs';
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: ReadStream
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.close();
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.close();
261
+ stream.destroy();
262
262
  reject(e);
263
263
  });
264
264
  });
@@ -1,5 +1,4 @@
1
- import { ReadStream, WriteStream } from 'fs';
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: ReadStream | http.IncomingMessage, target: WriteStream, options?: pipeOptions): Promise<void> {
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: ReadStream | http.IncomingMessage, readBytesPreSec: number) {
48
+ function limitStreamFlowingRate(stream: Readable, readBytesPreSec: number) {
50
49
  let start = 0;
51
50
  let checkInterval: any;
52
51
  let calReadBytesTotal = 0;