cvitool 1.0.765 → 1.0.767

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.
@@ -18,6 +18,7 @@ interface baseReqOptions {
18
18
  readTimeOut?: number;
19
19
  useEnvProxy?: boolean;
20
20
  proxyHost?: string;
21
+ originUrl?: string;
21
22
  }
22
23
  interface reqOptions extends baseReqOptions {
23
24
  method?: Method;
package/build/src/hgo.js CHANGED
@@ -39,7 +39,7 @@ function getProtocol(url) {
39
39
  */
40
40
  function request(url, options) {
41
41
  return __awaiter(this, void 0, void 0, function* () {
42
- let { query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost } = options || {};
42
+ let { query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost, originUrl } = options || {};
43
43
  if (timeout && !(connectTimeOut && readTimeOut)) {
44
44
  connectTimeOut = timeout;
45
45
  readTimeOut = timeout;
@@ -55,23 +55,24 @@ function request(url, options) {
55
55
  const isStrBodyType = (0, cutil_1.getValueType)(body) === 'string';
56
56
  const data = (isStrBodyType ? body : JSON.stringify(body));
57
57
  const isBodyEmpty = data.length === 0 || data === '{}';
58
- let baseHeaders = {};
58
+ let baseHeaders = { 'User-Agent': 'Node-HttpClient' };
59
59
  if (!isBodyEmpty) {
60
- baseHeaders = {
61
- 'Content-Type': isStrBodyType ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8',
62
- 'Content-Length': Buffer.byteLength(data)
63
- };
60
+ baseHeaders = Object.assign(Object.assign({}, baseHeaders), { 'Content-Type': isStrBodyType ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(data) });
64
61
  }
65
62
  let res;
66
63
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
67
64
  try {
68
65
  res = yield new Promise((resolve, reject) => {
69
66
  const req = reqProtocol.request(reqOpt, res => {
70
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
67
+ // 需要重定向的情况
68
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
69
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
70
+ }
71
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
71
72
  });
72
73
  req.on('timeout', () => {
73
74
  req.destroy();
74
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
75
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
75
76
  });
76
77
  req.on('error', e => {
77
78
  req.destroy();
@@ -116,12 +117,13 @@ function request(url, options) {
116
117
  */
117
118
  function reqSendBuffer(url, options) {
118
119
  return __awaiter(this, void 0, void 0, function* () {
119
- let { timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost } = options;
120
+ let { timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost, originUrl } = options;
120
121
  if (timeout && !(connectTimeOut && readTimeOut)) {
121
122
  connectTimeOut = timeout;
122
123
  readTimeOut = timeout;
123
124
  }
124
125
  const baseHeaders = {
126
+ 'User-Agent': 'Node-HttpClient',
125
127
  'Content-Length': buffer.byteLength
126
128
  };
127
129
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
@@ -129,11 +131,15 @@ function reqSendBuffer(url, options) {
129
131
  try {
130
132
  res = yield new Promise((resolve, reject) => {
131
133
  const req = reqProtocol.request(reqOpt, res => {
132
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
134
+ // 需要重定向的情况
135
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
136
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
137
+ }
138
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
133
139
  });
134
140
  req.on('timeout', () => {
135
141
  req.destroy();
136
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
142
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
137
143
  });
138
144
  req.on('error', e => {
139
145
  req.destroy();
@@ -173,12 +179,13 @@ function reqSendBuffer(url, options) {
173
179
  */
174
180
  function reqSendStream(url, options) {
175
181
  return __awaiter(this, void 0, void 0, function* () {
176
- let { timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost } = options;
182
+ let { timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost, originUrl } = options;
177
183
  if (timeout && !(connectTimeOut && readTimeOut)) {
178
184
  connectTimeOut = timeout;
179
185
  readTimeOut = timeout;
180
186
  }
181
187
  const baseHeaders = {
188
+ 'User-Agent': 'Node-HttpClient',
182
189
  'Content-Type': 'application/octet-stream',
183
190
  'Transfer-Encoding': 'chunked',
184
191
  Connection: 'keep-alive'
@@ -186,11 +193,15 @@ function reqSendStream(url, options) {
186
193
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
187
194
  const res = yield new Promise((resolve, reject) => {
188
195
  const req = reqProtocol.request(reqOpt, res => {
189
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
196
+ // 需要重定向的情况
197
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
198
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
199
+ }
200
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
190
201
  });
191
202
  req.on('timeout', () => {
192
203
  req.destroy();
193
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
204
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
194
205
  });
195
206
  req.on('error', e => {
196
207
  req.destroy();
@@ -225,20 +236,24 @@ function reqSendStream(url, options) {
225
236
  */
226
237
  function reqSendMultiPart(url, options) {
227
238
  return __awaiter(this, void 0, void 0, function* () {
228
- let { timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost } = options;
239
+ let { timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost, originUrl } = options;
229
240
  if (timeout && !(connectTimeOut && readTimeOut)) {
230
241
  connectTimeOut = timeout;
231
242
  readTimeOut = timeout;
232
243
  }
233
- const baseHeaders = Object.assign({}, form.getHeaders());
244
+ const baseHeaders = Object.assign({ 'User-Agent': 'Node-HttpClient' }, form.getHeaders());
234
245
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, 'post', agent, useEnvProxy, proxyHost);
235
246
  const res = yield new Promise((resolve, reject) => {
236
247
  const req = reqProtocol.request(reqOpt, res => {
237
- resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, url);
248
+ // 需要重定向的情况
249
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
250
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
251
+ }
252
+ resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, originUrl || url);
238
253
  });
239
254
  req.on('timeout', () => {
240
255
  req.destroy();
241
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
256
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
242
257
  });
243
258
  req.on('error', e => {
244
259
  req.destroy();
@@ -259,13 +274,7 @@ function reqSendMultiPart(url, options) {
259
274
  });
260
275
  }
261
276
  function resHandld(res, resolve, reject, resType, method, readTimeOut, req, reqUrl) {
262
- const resHeaders = {};
263
- for (let i = 0; i < res.rawHeaders.length; i += 2) {
264
- resHeaders[res.rawHeaders[i]] = res.rawHeaders[i + 1];
265
- if (i === res.rawHeaders.length - 2) {
266
- break;
267
- }
268
- }
277
+ const resHeaders = res.headers;
269
278
  if (res.statusCode >= 400) {
270
279
  errHandle(reject, res, reqUrl, resHeaders, readTimeOut, req);
271
280
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvitool",
3
- "version": "1.0.765",
3
+ "version": "1.0.767",
4
4
  "description": "cvitool",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/hgo.ts CHANGED
@@ -21,7 +21,8 @@ interface baseReqOptions {
21
21
  connectTimeOut?: number,
22
22
  readTimeOut?: number,
23
23
  useEnvProxy?: boolean,
24
- proxyHost?: string
24
+ proxyHost?: string,
25
+ originUrl?: string
25
26
  }
26
27
 
27
28
  interface reqOptions extends baseReqOptions {
@@ -88,7 +89,7 @@ function getProtocol(url: string) {
88
89
  async function request(url: string, options?: reqOptions): Promise<ResData> {
89
90
  let {
90
91
  query = {}, body = {}, headers = {}, timeout = 5000, method = 'get', agent, resType = 'json', connectTimeOut,
91
- readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost
92
+ readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost, originUrl
92
93
  } = options || {};
93
94
  if (timeout && !(connectTimeOut && readTimeOut)) {
94
95
  connectTimeOut = timeout;
@@ -104,9 +105,10 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
104
105
  const isStrBodyType = getValueType(body) === 'string';
105
106
  const data = (isStrBodyType ? body : JSON.stringify(body)) as string;
106
107
  const isBodyEmpty = data.length === 0 || data === '{}';
107
- let baseHeaders: CustomObject = {};
108
+ let baseHeaders: CustomObject = { 'User-Agent': 'Node-HttpClient' };
108
109
  if (!isBodyEmpty) {
109
110
  baseHeaders = {
111
+ ...baseHeaders,
110
112
  'Content-Type': isStrBodyType ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8',
111
113
  'Content-Length': Buffer.byteLength(data)
112
114
  };
@@ -116,11 +118,15 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
116
118
  try {
117
119
  res = await new Promise((resolve, reject) => {
118
120
  const req = reqProtocol.request(reqOpt, res => {
119
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
121
+ // 需要重定向的情况
122
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
123
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
124
+ }
125
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
120
126
  });
121
127
  req.on('timeout', () => {
122
128
  req.destroy();
123
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
129
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
124
130
  });
125
131
  req.on('error', e => {
126
132
  req.destroy();
@@ -164,13 +170,14 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
164
170
  async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promise<ResData> {
165
171
  let {
166
172
  timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut,
167
- retries, retryInterval, retrieds, useEnvProxy = true, proxyHost
173
+ retries, retryInterval, retrieds, useEnvProxy = true, proxyHost, originUrl
168
174
  } = options;
169
175
  if (timeout && !(connectTimeOut && readTimeOut)) {
170
176
  connectTimeOut = timeout;
171
177
  readTimeOut = timeout;
172
178
  }
173
179
  const baseHeaders = {
180
+ 'User-Agent': 'Node-HttpClient',
174
181
  'Content-Length': buffer.byteLength
175
182
  };
176
183
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
@@ -178,11 +185,15 @@ async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promis
178
185
  try {
179
186
  res = await new Promise((resolve, reject) => {
180
187
  const req = reqProtocol.request(reqOpt, res => {
181
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
188
+ // 需要重定向的情况
189
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
190
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
191
+ }
192
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
182
193
  });
183
194
  req.on('timeout', () => {
184
195
  req.destroy();
185
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
196
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
186
197
  });
187
198
  req.on('error', e => {
188
199
  req.destroy();
@@ -222,13 +233,14 @@ async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promis
222
233
  async function reqSendStream(url: string, options: reqSendStreamOptions): Promise<ResData> {
223
234
  let {
224
235
  timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut,
225
- useEnvProxy = true, proxyHost
236
+ useEnvProxy = true, proxyHost, originUrl
226
237
  } = options;
227
238
  if (timeout && !(connectTimeOut && readTimeOut)) {
228
239
  connectTimeOut = timeout;
229
240
  readTimeOut = timeout;
230
241
  }
231
242
  const baseHeaders = {
243
+ 'User-Agent': 'Node-HttpClient',
232
244
  'Content-Type': 'application/octet-stream',
233
245
  'Transfer-Encoding': 'chunked',
234
246
  Connection: 'keep-alive'
@@ -236,11 +248,15 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
236
248
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
237
249
  const res: ResData = await new Promise((resolve, reject) => {
238
250
  const req = reqProtocol.request(reqOpt, res => {
239
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
251
+ // 需要重定向的情况
252
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
253
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
254
+ }
255
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
240
256
  });
241
257
  req.on('timeout', () => {
242
258
  req.destroy();
243
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
259
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
244
260
  });
245
261
  req.on('error', e => {
246
262
  req.destroy();
@@ -276,23 +292,28 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
276
292
  async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions): Promise<ResData> {
277
293
  let {
278
294
  timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut,
279
- useEnvProxy = true, proxyHost
295
+ useEnvProxy = true, proxyHost, originUrl
280
296
  } = options;
281
297
  if (timeout && !(connectTimeOut && readTimeOut)) {
282
298
  connectTimeOut = timeout;
283
299
  readTimeOut = timeout;
284
300
  }
285
301
  const baseHeaders = {
302
+ 'User-Agent': 'Node-HttpClient',
286
303
  ...form.getHeaders()
287
304
  };
288
305
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, 'post', agent, useEnvProxy, proxyHost);
289
306
  const res: ResData = await new Promise((resolve, reject) => {
290
307
  const req = reqProtocol.request(reqOpt, res => {
291
- resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, url);
308
+ // 需要重定向的情况
309
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
310
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
311
+ }
312
+ resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, originUrl || url);
292
313
  });
293
314
  req.on('timeout', () => {
294
315
  req.destroy();
295
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
316
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
296
317
  });
297
318
  req.on('error', e => {
298
319
  req.destroy();
@@ -313,13 +334,7 @@ async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions):
313
334
  }
314
335
 
315
336
  function resHandld(res: http.IncomingMessage, resolve: any, reject: any, resType: ResType, method: Method, readTimeOut: number, req: http.ClientRequest, reqUrl: string) {
316
- const resHeaders: CustomObject = {};
317
- for (let i = 0; i < res.rawHeaders.length; i += 2) {
318
- resHeaders[res.rawHeaders[i]] = res.rawHeaders[i + 1];
319
- if (i === res.rawHeaders.length - 2) {
320
- break;
321
- }
322
- }
337
+ const resHeaders: CustomObject = res.headers;
323
338
  if (res.statusCode >= 400) {
324
339
  errHandle(reject, res, reqUrl, resHeaders, readTimeOut, req);
325
340
  return;