urllib 2.37.1 → 2.38.0

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/History.md CHANGED
@@ -1,4 +1,32 @@
1
1
 
2
+ 2.38.0 / 2021-11-24
3
+ ==================
4
+
5
+ **others**
6
+ * [[`6d19f99`](http://github.com/node-modules/urllib/commit/6d19f99f6490df67832d1d2a9c39743c0de1cbd5)] - tsd: add timing interface (#375) (弘树@阿里 <<dickeylth@users.noreply.github.com>>)
7
+ * [[`9c9c65b`](http://github.com/node-modules/urllib/commit/9c9c65b0e8344c1a30c157628a675440b8a616c1)] - test: check TypeScript type definitions (#373) (fengmk2 <<fengmk2@gmail.com>>)
8
+
9
+ 2.37.4 / 2021-09-07
10
+ ==================
11
+
12
+ **fixes**
13
+ * [[`1e6622a`](http://github.com/node-modules/urllib/commit/1e6622a571b3e6f72c5f187a224bddedcbc382cf)] - fix: upgrade proxy-agent to fix the security vulnerability. (#368) (hyj1991 <<yeekwanvong@gmail.com>>)
14
+
15
+ **others**
16
+ * [[`ae27498`](http://github.com/node-modules/urllib/commit/ae2749811e40262da8bf2641599b066344fa6b05)] - docs: notes contentType's value (#349) (changzhi <<changzhiwin@gmail.com>>)
17
+
18
+ 2.37.3 / 2021-07-05
19
+ ==================
20
+
21
+ **fixes**
22
+ * [[`b730a6c`](http://github.com/node-modules/urllib/commit/b730a6cdc0465bea1c08d50bfa3b5e95bd17b31f)] - fix: 🐛 redirect status code (#363) (Hongcai Deng <<admin@dhchouse.com>>)
23
+
24
+ 2.37.2 / 2021-06-07
25
+ ==================
26
+
27
+ **fixes**
28
+ * [[`04734a1`](http://github.com/node-modules/urllib/commit/04734a13e146da501f4e38333d5de9f9bbb259d1)] - fix: 🐛 should trigger response event when follow redirect (#361) (Hongcai Deng <<admin@dhchouse.com>>)
29
+
2
30
  2.37.1 / 2021-04-15
3
31
  ==================
4
32
 
package/README.md CHANGED
@@ -1,21 +1,15 @@
1
1
  # urllib
2
2
 
3
3
  [![NPM version][npm-image]][npm-url]
4
- [![build status][travis-image]][travis-url]
5
- [![Build Status](https://dev.azure.com/eggjs/egg/_apis/build/status/node-modules.urllib)](https://dev.azure.com/eggjs/egg/_build/latest?definitionId=7)
4
+ [![Node.js CI](https://github.com/node-modules/urllib/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/urllib/actions/workflows/nodejs.yml)
6
5
  [![Test coverage][codecov-image]][codecov-url]
7
- [![David deps][david-image]][david-url]
8
6
  [![Known Vulnerabilities][snyk-image]][snyk-url]
9
7
  [![npm download][download-image]][download-url]
10
8
 
11
9
  [npm-image]: https://img.shields.io/npm/v/urllib.svg?style=flat-square
12
10
  [npm-url]: https://npmjs.org/package/urllib
13
- [travis-image]: https://img.shields.io/travis/node-modules/urllib.svg?style=flat-square
14
- [travis-url]: https://travis-ci.org/node-modules/urllib
15
11
  [codecov-image]: https://codecov.io/gh/node-modules/urllib/branch/master/graph/badge.svg
16
12
  [codecov-url]: https://codecov.io/gh/node-modules/urllib
17
- [david-image]: https://img.shields.io/david/node-modules/urllib.svg?style=flat-square
18
- [david-url]: https://david-dm.org/node-modules/urllib
19
13
  [snyk-image]: https://snyk.io/test/npm/urllib/badge.svg?style=flat-square
20
14
  [snyk-url]: https://snyk.io/test/npm/urllib
21
15
  [download-image]: https://img.shields.io/npm/dm/urllib.svg?style=flat-square
@@ -125,7 +119,7 @@ httpclient.request('http://nodejs.org', function (err, body) {
125
119
  - ***writeStream*** [stream.Writable](http://nodejs.org/api/stream.html#stream_class_stream_writable) - A writable stream to be piped by the response stream. Responding data will be write to this stream and `callback` will be called with `data` set `null` after finished writing.
126
120
  - ***files*** {Array<ReadStream|Buffer|String> | Object | ReadStream | Buffer | String - The files will send with `multipart/form-data` format, base on `formstream`. If `method` not set, will use `POST` method by default.
127
121
  - ***consumeWriteStream*** [true] - consume the writeStream, invoke the callback after writeStream close.
128
- - ***contentType*** String - Type of request data. Could be `json`. If it's `json`, will auto set `Content-Type: application/json` header.
122
+ - ***contentType*** String - Type of request data. Could be `json` (**Notes**: not use `application/json` here). If it's `json`, will auto set `Content-Type: application/json` header.
129
123
  - ***nestedQuerystring*** Boolean - urllib default use querystring to stringify form data which don't support nested object, will use [qs](https://github.com/ljharb/qs) instead of querystring to support nested object by set this option to true.
130
124
  - ***dataType*** String - Type of response data. Could be `text` or `json`. If it's `text`, the `callback`ed `data` would be a String. If it's `json`, the `data` of callback would be a parsed JSON Object and will auto set `Accept: application/json` header. Default `callback`ed `data` would be a `Buffer`.
131
125
  - **fixJSONCtlChars** Boolean - Fix the control characters (U+0000 through U+001F) before JSON parse response. Default is `false`.
package/lib/index.d.ts CHANGED
@@ -140,7 +140,19 @@ export interface HttpClientResponse<T> {
140
140
  data: T;
141
141
  status: number;
142
142
  headers: OutgoingHttpHeaders;
143
- res: http.IncomingMessage;
143
+ res: http.IncomingMessage & {
144
+ /**
145
+ * https://eggjs.org/en/core/httpclient.html#timing-boolean
146
+ */
147
+ timing?: {
148
+ queuing: number;
149
+ dnslookup: number;
150
+ connected: number;
151
+ requestSent: number;
152
+ waiting: number;
153
+ contentDownload: number;
154
+ }
155
+ };
144
156
  }
145
157
 
146
158
 
@@ -0,0 +1,10 @@
1
+ import { expectType } from 'tsd';
2
+ import { curl } from '..';
3
+
4
+ // curl
5
+ expectType<Buffer>((await curl<Buffer>('http://a.com')).data);
6
+ // RequestOptions
7
+ expectType<Buffer>((await curl<Buffer>('http://a.com', {})).data);
8
+ expectType<string>((await curl<string>('http://a.com', {
9
+ method: 'HEAD',
10
+ })).data);
package/lib/urllib.js CHANGED
@@ -520,6 +520,7 @@ function requestWithCallback(url, args, callback) {
520
520
  }
521
521
  return;
522
522
  }
523
+
523
524
  var cb = callback;
524
525
  callback = null;
525
526
  var headers = {};
@@ -529,49 +530,16 @@ function requestWithCallback(url, args, callback) {
529
530
  headers = res.headers;
530
531
  }
531
532
 
532
- // handle digest auth
533
- if (statusCode === 401 && headers['www-authenticate']
534
- && !options.headers.authorization && args.digestAuth) {
535
- var authenticate = headers['www-authenticate'];
536
- if (authenticate.indexOf('Digest ') >= 0) {
537
- debug('Request#%d %s: got digest auth header WWW-Authenticate: %s', reqId, url, authenticate);
538
- options.headers.authorization = digestAuthHeader(options.method, options.path, authenticate, args.digestAuth);
539
- debug('Request#%d %s: auth with digest header: %s', reqId, url, options.headers.authorization);
540
- if (res.headers['set-cookie']) {
541
- options.headers.cookie = res.headers['set-cookie'].join(';');
542
- }
543
- args.headers = options.headers;
544
- return exports.requestWithCallback(url, args, cb);
545
- }
533
+ if (handleDigestAuth(res, cb)) {
534
+ return;
546
535
  }
547
536
 
548
- var requestUseTime = Date.now() - requestStartTime;
549
- if (timing) {
550
- timing.contentDownload = requestUseTime;
551
- }
537
+ var response = createCallbackResponse(data, res);
552
538
 
553
539
  debug('[%sms] done, %s bytes HTTP %s %s %s %s, keepAliveSocket: %s, timing: %j, socketHandledRequests: %s, socketHandledResponses: %s',
554
- requestUseTime, responseSize, statusCode, options.method, options.host, options.path,
540
+ response.requestUseTime, responseSize, statusCode, options.method, options.host, options.path,
555
541
  keepAliveSocket, timing, socketHandledRequests, socketHandledResponses);
556
542
 
557
- var response = {
558
- status: statusCode,
559
- statusCode: statusCode,
560
- statusMessage: statusMessage,
561
- headers: headers,
562
- size: responseSize,
563
- aborted: responseAborted,
564
- rt: requestUseTime,
565
- keepAliveSocket: keepAliveSocket,
566
- data: data,
567
- requestUrls: args.requestUrls,
568
- timing: timing,
569
- remoteAddress: remoteAddress,
570
- remotePort: remotePort,
571
- socketHandledRequests: socketHandledRequests,
572
- socketHandledResponses: socketHandledResponses,
573
- };
574
-
575
543
  if (err) {
576
544
  var agentStatus = '';
577
545
  if (agent && typeof agent.getCurrentStatus === 'function') {
@@ -620,6 +588,44 @@ function requestWithCallback(url, args, callback) {
620
588
 
621
589
  cb(err, data, args.streaming ? res : response);
622
590
 
591
+ emitResponseEvent(err, response);
592
+ }
593
+
594
+ function createAndEmitResponseEvent(data, res) {
595
+ var response = createCallbackResponse(data, res);
596
+ emitResponseEvent(null, response);
597
+ }
598
+
599
+ function createCallbackResponse(data, res) {
600
+ var requestUseTime = Date.now() - requestStartTime;
601
+ if (timing) {
602
+ timing.contentDownload = requestUseTime;
603
+ }
604
+
605
+ var headers = res && res.headers || {};
606
+ var resStatusCode = res && res.statusCode || statusCode;
607
+ var resStatusMessage = res && res.statusMessage || statusMessage;
608
+
609
+ return {
610
+ status: resStatusCode,
611
+ statusCode: resStatusCode,
612
+ statusMessage: resStatusMessage,
613
+ headers: headers,
614
+ size: responseSize,
615
+ aborted: responseAborted,
616
+ rt: requestUseTime,
617
+ keepAliveSocket: keepAliveSocket,
618
+ data: data,
619
+ requestUrls: args.requestUrls,
620
+ timing: timing,
621
+ remoteAddress: remoteAddress,
622
+ remotePort: remotePort,
623
+ socketHandledRequests: socketHandledRequests,
624
+ socketHandledResponses: socketHandledResponses,
625
+ };
626
+ }
627
+
628
+ function emitResponseEvent(err, response) {
623
629
  if (args.emitter) {
624
630
  // keep to use the same reqMeta object on request event before
625
631
  reqMeta.url = parsedUrl.href;
@@ -637,6 +643,30 @@ function requestWithCallback(url, args, callback) {
637
643
  }
638
644
  }
639
645
 
646
+ function handleDigestAuth(res, cb) {
647
+ var headers = {};
648
+ if (res && res.headers) {
649
+ headers = res.headers;
650
+ }
651
+ // handle digest auth
652
+ if (statusCode === 401 && headers['www-authenticate']
653
+ && !options.headers.authorization && args.digestAuth) {
654
+ var authenticate = headers['www-authenticate'];
655
+ if (authenticate.indexOf('Digest ') >= 0) {
656
+ debug('Request#%d %s: got digest auth header WWW-Authenticate: %s', reqId, url, authenticate);
657
+ options.headers.authorization = digestAuthHeader(options.method, options.path, authenticate, args.digestAuth);
658
+ debug('Request#%d %s: auth with digest header: %s', reqId, url, options.headers.authorization);
659
+ if (res.headers['set-cookie']) {
660
+ options.headers.cookie = res.headers['set-cookie'].join(';');
661
+ }
662
+ args.headers = options.headers;
663
+ exports.requestWithCallback(url, args, cb);
664
+ return true;
665
+ }
666
+ }
667
+ return false;
668
+ }
669
+
640
670
  function handleRedirect(res) {
641
671
  var err = null;
642
672
  if (args.followRedirect && statuses.redirect[res.statusCode]) { // handle redirect
@@ -740,6 +770,7 @@ function requestWithCallback(url, args, callback) {
740
770
  var result = handleRedirect(res);
741
771
  if (result.redirect) {
742
772
  res.resume();
773
+ createAndEmitResponseEvent(null, res);
743
774
  return;
744
775
  }
745
776
  if (result.error) {
@@ -781,6 +812,7 @@ function requestWithCallback(url, args, callback) {
781
812
  var result = handleRedirect(res);
782
813
  if (result.redirect) {
783
814
  res.resume();
815
+ createAndEmitResponseEvent(null, res);
784
816
  return;
785
817
  }
786
818
  if (result.error) {
@@ -874,6 +906,7 @@ function requestWithCallback(url, args, callback) {
874
906
  return done(result.error, body, res);
875
907
  }
876
908
  if (result.redirect) {
909
+ createAndEmitResponseEvent(null, res);
877
910
  return;
878
911
  }
879
912
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "urllib",
3
- "version": "2.37.1",
3
+ "version": "2.38.0",
4
4
  "description": "Help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.",
5
5
  "keywords": [
6
6
  "urllib",
@@ -23,10 +23,11 @@
23
23
  "url": "git://github.com/node-modules/urllib.git"
24
24
  },
25
25
  "scripts": {
26
+ "tsd": "node test/tsd.js",
26
27
  "test-local": "mocha -t 30000 -r intelli-espower-loader test/*.test.js",
27
28
  "test": "npm run lint && npm run test-local",
28
29
  "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -t 30000 -r intelli-espower-loader test/*.test.js",
29
- "ci": "npm run lint && npm run test-cov",
30
+ "ci": "npm run lint && npm run tsd && npm run test-cov",
30
31
  "lint": "jshint .",
31
32
  "autod": "autod -w --prefix '^' -t test -e examples",
32
33
  "contributor": "git-contributor"
@@ -42,7 +43,7 @@
42
43
  "humanize-ms": "^1.2.0",
43
44
  "iconv-lite": "^0.4.15",
44
45
  "ip": "^1.1.5",
45
- "proxy-agent": "^4.0.1",
46
+ "proxy-agent": "^5.0.0",
46
47
  "pump": "^3.0.0",
47
48
  "qs": "^6.4.0",
48
49
  "statuses": "^1.3.1",
@@ -73,7 +74,8 @@
73
74
  "spy": "^1.0.0",
74
75
  "tar": "^4.4.8",
75
76
  "through2": "^2.0.3",
76
- "typescript": "^3.2.2"
77
+ "tsd": "^0.18.0",
78
+ "typescript": "^4.4.4"
77
79
  },
78
80
  "engines": {
79
81
  "node": ">= 0.10.0"
@@ -83,7 +85,7 @@
83
85
  "os": {
84
86
  "github": "linux, windows, macos"
85
87
  },
86
- "version": "8, 10, 12, 14"
88
+ "version": "8, 10, 12, 14, 16"
87
89
  },
88
90
  "license": "MIT"
89
91
  }