cvitool 1.0.765 → 1.0.766

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;
@@ -67,11 +67,15 @@ function request(url, options) {
67
67
  try {
68
68
  res = yield new Promise((resolve, reject) => {
69
69
  const req = reqProtocol.request(reqOpt, res => {
70
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
70
+ // 需要重定向的情况
71
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
72
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
73
+ }
74
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
71
75
  });
72
76
  req.on('timeout', () => {
73
77
  req.destroy();
74
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
78
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
75
79
  });
76
80
  req.on('error', e => {
77
81
  req.destroy();
@@ -116,7 +120,7 @@ function request(url, options) {
116
120
  */
117
121
  function reqSendBuffer(url, options) {
118
122
  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;
123
+ let { timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut, retries, retryInterval, retrieds, useEnvProxy = true, proxyHost, originUrl } = options;
120
124
  if (timeout && !(connectTimeOut && readTimeOut)) {
121
125
  connectTimeOut = timeout;
122
126
  readTimeOut = timeout;
@@ -129,11 +133,15 @@ function reqSendBuffer(url, options) {
129
133
  try {
130
134
  res = yield new Promise((resolve, reject) => {
131
135
  const req = reqProtocol.request(reqOpt, res => {
132
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
136
+ // 需要重定向的情况
137
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
138
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
139
+ }
140
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
133
141
  });
134
142
  req.on('timeout', () => {
135
143
  req.destroy();
136
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
144
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
137
145
  });
138
146
  req.on('error', e => {
139
147
  req.destroy();
@@ -173,7 +181,7 @@ function reqSendBuffer(url, options) {
173
181
  */
174
182
  function reqSendStream(url, options) {
175
183
  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;
184
+ let { timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost, originUrl } = options;
177
185
  if (timeout && !(connectTimeOut && readTimeOut)) {
178
186
  connectTimeOut = timeout;
179
187
  readTimeOut = timeout;
@@ -186,11 +194,15 @@ function reqSendStream(url, options) {
186
194
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
187
195
  const res = yield new Promise((resolve, reject) => {
188
196
  const req = reqProtocol.request(reqOpt, res => {
189
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
197
+ // 需要重定向的情况
198
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
199
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
200
+ }
201
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
190
202
  });
191
203
  req.on('timeout', () => {
192
204
  req.destroy();
193
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
205
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
194
206
  });
195
207
  req.on('error', e => {
196
208
  req.destroy();
@@ -225,7 +237,7 @@ function reqSendStream(url, options) {
225
237
  */
226
238
  function reqSendMultiPart(url, options) {
227
239
  return __awaiter(this, void 0, void 0, function* () {
228
- let { timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost } = options;
240
+ let { timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut, useEnvProxy = true, proxyHost, originUrl } = options;
229
241
  if (timeout && !(connectTimeOut && readTimeOut)) {
230
242
  connectTimeOut = timeout;
231
243
  readTimeOut = timeout;
@@ -234,11 +246,15 @@ function reqSendMultiPart(url, options) {
234
246
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, 'post', agent, useEnvProxy, proxyHost);
235
247
  const res = yield new Promise((resolve, reject) => {
236
248
  const req = reqProtocol.request(reqOpt, res => {
237
- resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, url);
249
+ // 需要重定向的情况
250
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
251
+ resolve(request(res.headers.location, Object.assign(Object.assign({}, options), { originUrl: url })));
252
+ }
253
+ resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, originUrl || url);
238
254
  });
239
255
  req.on('timeout', () => {
240
256
  req.destroy();
241
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
257
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
242
258
  });
243
259
  req.on('error', e => {
244
260
  req.destroy();
@@ -259,13 +275,7 @@ function reqSendMultiPart(url, options) {
259
275
  });
260
276
  }
261
277
  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
- }
278
+ const resHeaders = res.headers;
269
279
  if (res.statusCode >= 400) {
270
280
  errHandle(reject, res, reqUrl, resHeaders, readTimeOut, req);
271
281
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvitool",
3
- "version": "1.0.765",
3
+ "version": "1.0.766",
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;
@@ -116,11 +117,15 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
116
117
  try {
117
118
  res = await new Promise((resolve, reject) => {
118
119
  const req = reqProtocol.request(reqOpt, res => {
119
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
120
+ // 需要重定向的情况
121
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
122
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
123
+ }
124
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
120
125
  });
121
126
  req.on('timeout', () => {
122
127
  req.destroy();
123
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
128
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
124
129
  });
125
130
  req.on('error', e => {
126
131
  req.destroy();
@@ -164,7 +169,7 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
164
169
  async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promise<ResData> {
165
170
  let {
166
171
  timeout = 5000, headers = {}, buffer, method = 'post', agent, resType = 'json', connectTimeOut, readTimeOut,
167
- retries, retryInterval, retrieds, useEnvProxy = true, proxyHost
172
+ retries, retryInterval, retrieds, useEnvProxy = true, proxyHost, originUrl
168
173
  } = options;
169
174
  if (timeout && !(connectTimeOut && readTimeOut)) {
170
175
  connectTimeOut = timeout;
@@ -178,11 +183,15 @@ async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promis
178
183
  try {
179
184
  res = await new Promise((resolve, reject) => {
180
185
  const req = reqProtocol.request(reqOpt, res => {
181
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
186
+ // 需要重定向的情况
187
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
188
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
189
+ }
190
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
182
191
  });
183
192
  req.on('timeout', () => {
184
193
  req.destroy();
185
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
194
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
186
195
  });
187
196
  req.on('error', e => {
188
197
  req.destroy();
@@ -222,7 +231,7 @@ async function reqSendBuffer(url: string, options: reqSendBufferOptions): Promis
222
231
  async function reqSendStream(url: string, options: reqSendStreamOptions): Promise<ResData> {
223
232
  let {
224
233
  timeout = 5000, method = 'post', stream, headers = {}, agent, resType = 'json', connectTimeOut, readTimeOut,
225
- useEnvProxy = true, proxyHost
234
+ useEnvProxy = true, proxyHost, originUrl
226
235
  } = options;
227
236
  if (timeout && !(connectTimeOut && readTimeOut)) {
228
237
  connectTimeOut = timeout;
@@ -236,11 +245,15 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
236
245
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, method, agent, useEnvProxy, proxyHost);
237
246
  const res: ResData = await new Promise((resolve, reject) => {
238
247
  const req = reqProtocol.request(reqOpt, res => {
239
- resHandld(res, resolve, reject, resType, method, readTimeOut, req, url);
248
+ // 需要重定向的情况
249
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
250
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
251
+ }
252
+ resHandld(res, resolve, reject, resType, method, readTimeOut, req, originUrl || url);
240
253
  });
241
254
  req.on('timeout', () => {
242
255
  req.destroy();
243
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
256
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
244
257
  });
245
258
  req.on('error', e => {
246
259
  req.destroy();
@@ -276,7 +289,7 @@ async function reqSendStream(url: string, options: reqSendStreamOptions): Promis
276
289
  async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions): Promise<ResData> {
277
290
  let {
278
291
  timeout = 60000, headers = {}, form, agent, resType = 'json', connectTimeOut, readTimeOut,
279
- useEnvProxy = true, proxyHost
292
+ useEnvProxy = true, proxyHost, originUrl
280
293
  } = options;
281
294
  if (timeout && !(connectTimeOut && readTimeOut)) {
282
295
  connectTimeOut = timeout;
@@ -288,11 +301,15 @@ async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions):
288
301
  const { reqProtocol, reqOpt } = getReqProtocolAndOpt(url, baseHeaders, headers, connectTimeOut, 'post', agent, useEnvProxy, proxyHost);
289
302
  const res: ResData = await new Promise((resolve, reject) => {
290
303
  const req = reqProtocol.request(reqOpt, res => {
291
- resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, url);
304
+ // 需要重定向的情况
305
+ if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location) {
306
+ resolve(request(res.headers.location, { ...options, originUrl: url }));
307
+ }
308
+ resHandld(res, resolve, reject, resType, 'post', readTimeOut, req, originUrl || url);
292
309
  });
293
310
  req.on('timeout', () => {
294
311
  req.destroy();
295
- reject(buildTimeOutErr('connectTimeOut', connectTimeOut, url));
312
+ reject(buildTimeOutErr('connectTimeOut', connectTimeOut, originUrl || url));
296
313
  });
297
314
  req.on('error', e => {
298
315
  req.destroy();
@@ -313,13 +330,7 @@ async function reqSendMultiPart(url: string, options: reqSendMultiPartOptions):
313
330
  }
314
331
 
315
332
  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
- }
333
+ const resHeaders: CustomObject = res.headers;
323
334
  if (res.statusCode >= 400) {
324
335
  errHandle(reject, res, reqUrl, resHeaders, readTimeOut, req);
325
336
  return;