urllib 3.0.0-alpha.1 → 3.0.2

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/src/Request.ts CHANGED
@@ -1,7 +1,13 @@
1
1
  import { Readable, Writable } from 'stream';
2
- import { LookupFunction } from 'net';
2
+ import { IncomingHttpHeaders } from 'http';
3
+ import type {
4
+ HttpMethod as UndiciHttpMethod,
5
+ } from 'undici/types/dispatcher';
6
+ import type {
7
+ HttpClientResponse,
8
+ } from './Response';
3
9
 
4
- export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'PUT' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'TRACE' | 'CONNECT';
10
+ export type HttpMethod = UndiciHttpMethod;
5
11
 
6
12
  export type RequestURL = string | URL;
7
13
 
@@ -13,12 +19,12 @@ export type RequestOptions = {
13
19
  method?: HttpMethod | Lowercase<HttpMethod>;
14
20
  /** Data to be sent. Will be stringify automatically. */
15
21
  data?: any;
16
- /** Force convert data to query string. */
17
- dataAsQueryString?: boolean;
18
22
  /** Manually set the content of payload. If set, data will be ignored. */
19
23
  content?: string | Buffer | Readable;
20
- /** Stream to be pipe to the remote. If set, data and content will be ignored.
21
- * Alias to `content` on Readable
24
+ /**
25
+ * @deprecated
26
+ * Stream to be pipe to the remote. If set, data and content will be ignored.
27
+ * Alias to `content = Readable`
22
28
  */
23
29
  stream?: Readable;
24
30
  /**
@@ -27,8 +33,6 @@ export type RequestOptions = {
27
33
  * will be called with data set null after finished writing.
28
34
  */
29
35
  writeStream?: Writable;
30
- /** consume the writeStream, invoke the callback after writeStream close. */
31
- consumeWriteStream?: boolean;
32
36
  /**
33
37
  * The files will send with multipart/form-data format, base on formstream.
34
38
  * If method not set, will use POST method by default.
@@ -41,19 +45,25 @@ export type RequestOptions = {
41
45
  * If it's text, the callbacked data would be a String.
42
46
  * If it's json, the data of callback would be a parsed JSON Object
43
47
  * and will auto set Accept: 'application/json' header.
44
- * Default is buffer.
48
+ * Default is 'buffer'.
45
49
  */
46
50
  dataType?: 'text' | 'json' | 'buffer' | 'stream';
47
51
  /**
52
+ * @deprecated
48
53
  * Let you get the res object when request connected, default false.
49
54
  * If set to true, `data` will be response readable stream.
50
- * Equal to `dataType = 'stream'`
55
+ * Alias to `dataType = 'stream'`
51
56
  */
52
57
  streaming?: boolean;
58
+ /**
59
+ * @deprecated
60
+ * Alias to `dataType = 'stream'`
61
+ */
62
+ customResponse?: boolean;
53
63
  /** Fix the control characters (U+0000 through U+001F) before JSON parse response. Default is false. */
54
64
  fixJSONCtlChars?: FixJSONCtlChars;
55
65
  /** Request headers. */
56
- headers?: Record<string, string>;
66
+ headers?: IncomingHttpHeaders;
57
67
  /**
58
68
  * Request timeout in milliseconds for connecting phase and response receiving phase.
59
69
  * Defaults to exports.
@@ -61,41 +71,12 @@ export type RequestOptions = {
61
71
  * timeout: [3000, 5000], which will set connecting timeout to 3s and response 5s.
62
72
  */
63
73
  timeout?: number | number[];
64
- /** username:password used in HTTP Basic Authorization. */
65
- auth?: string;
66
- /** username:password used in HTTP Digest Authorization. */
67
- digestAuth?: string;
68
- /**
69
- * An array of strings or Buffers of trusted certificates.
70
- * If this is omitted several well known "root" CAs will be used, like VeriSign.
71
- * These are used to authorize connections.
72
- * Notes: This is necessary only if the server uses the self - signed certificate
73
- */
74
- ca?: string | Buffer | string[] | Buffer[];
75
- /**
76
- * If true, the server certificate is verified against the list of supplied CAs.
77
- * An 'error' event is emitted if verification fails.Default: true.
78
- */
79
- rejectUnauthorized?: boolean;
80
- /** A string or Buffer containing the private key, certificate and CA certs of the server in PFX or PKCS12 format. */
81
- pfx?: string | Buffer;
82
74
  /**
83
- * A string or Buffer containing the private key of the client in PEM format.
84
- * Notes: This is necessary only if using the client certificate authentication
85
- */
86
- key?: string | Buffer;
87
- /**
88
- * A string or Buffer containing the certificate key of the client in PEM format.
89
- * Notes: This is necessary only if using the client certificate authentication
90
- */
91
- cert?: string | Buffer;
92
- /** A string of passphrase for the private key or pfx. */
93
- passphrase?: string;
94
- /** A string describing the ciphers to use or exclude. */
95
- ciphers?: string;
96
- /** The SSL method to use, e.g.SSLv3_method to force SSL version 3. */
97
- secureProtocol?: string;
98
- /** follow HTTP 3xx responses as redirects. defaults to false. */
75
+ * username:password used in HTTP Basic Authorization.
76
+ * Alias to `headers.authorization = xxx`
77
+ **/
78
+ auth?: string;
79
+ /** follow HTTP 3xx responses as redirects. defaults to true. */
99
80
  followRedirect?: boolean;
100
81
  /** The maximum number of redirects to follow, defaults to 10. */
101
82
  maxRedirects?: number;
@@ -103,19 +84,35 @@ export type RequestOptions = {
103
84
  formatRedirectUrl?: (a: any, b: any) => void;
104
85
  /** Before request hook, you can change every thing here. */
105
86
  beforeRequest?: (...args: any[]) => void;
106
- /** Accept gzip response content and auto decode it, default is false. */
87
+ /** Accept `gzip, br` response content and auto decode it, default is false. */
88
+ compressed?: boolean;
89
+ /**
90
+ * @deprecated
91
+ * Alias to compressed
92
+ * */
107
93
  gzip?: boolean;
108
- /** Enable timing or not, default is false. */
94
+ /**
95
+ * @deprecated
96
+ * Enable timing or not, default is false.
97
+ * */
109
98
  timing?: boolean;
110
99
  /**
111
- * Custom DNS lookup function, default is dns.lookup.
112
- * Require node >= 4.0.0(for http protocol) and node >=8(for https protocol)
100
+ * Auto retry times on 5xx response, default is 0. Don't work on streaming request
101
+ * It's not supported by using retry and writeStream, because the retry request can't stop the stream which is consuming.
102
+ **/
103
+ retry?: number;
104
+ /** Wait a delay(ms) between retries */
105
+ retryDelay?: number;
106
+ /**
107
+ * Determine whether retry, a response object as the first argument.
108
+ * It will retry when status >= 500 by default. Request error is not included.
113
109
  */
114
- lookup?: LookupFunction;
110
+ isRetry?: (response: HttpClientResponse) => boolean;
111
+ /** Default: `null` */
112
+ opaque?: unknown;
115
113
  /**
116
- * check request address to protect from SSRF and similar attacks.
117
- * It receive two arguments(ip and family) and should return true or false to identified the address is legal or not.
118
- * It rely on lookup and have the same version requirement.
114
+ * @deprecated
115
+ * Maybe you should use opaque instead
119
116
  */
120
- checkAddress?: (ip: string, family: number | string) => boolean;
117
+ ctx?: unknown;
121
118
  };
package/src/Response.ts CHANGED
@@ -1,21 +1,21 @@
1
- import { ReadableStream } from 'stream/web';
2
1
  import { Readable } from 'stream';
2
+ import { IncomingHttpHeaders } from 'http';
3
3
 
4
4
  export type HttpClientResponseMeta = {
5
5
  status: number;
6
6
  statusCode: number;
7
- statusMessage: string;
8
- headers: Record<string, string>;
7
+ headers: IncomingHttpHeaders;
9
8
  size: number;
10
9
  aborted: boolean;
11
10
  rt: number;
12
11
  keepAliveSocket: boolean;
13
- requestUrls: string[],
12
+ requestUrls: string[];
14
13
  /**
15
14
  * https://eggjs.org/en/core/httpclient.html#timing-boolean
16
15
  */
17
16
  timing: {
18
17
  contentDownload: number;
18
+ waiting: number;
19
19
  };
20
20
  // remoteAddress: remoteAddress,
21
21
  // remotePort: remotePort,
@@ -23,18 +23,19 @@ export type HttpClientResponseMeta = {
23
23
  // socketHandledResponses: socketHandledResponses,
24
24
  };
25
25
 
26
- export type ReadableStreamWithMeta = (Readable | ReadableStream) & {
26
+ export type ReadableWithMeta = Readable & {
27
27
  status: number;
28
28
  statusCode: number;
29
- statusMessage: string;
30
- headers: Record<string, string>;
29
+ headers: IncomingHttpHeaders;
31
30
  };
32
31
 
33
32
  export type HttpClientResponse = {
34
- data: any;
33
+ opaque: unknown;
34
+ data: any
35
35
  status: number;
36
- headers: Record<string, string>;
36
+ headers: IncomingHttpHeaders;
37
37
  url: string;
38
38
  redirected: boolean;
39
- res: ReadableStreamWithMeta | HttpClientResponseMeta;
39
+ requestUrls: string[];
40
+ res: ReadableWithMeta | HttpClientResponseMeta;
40
41
  };
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ import { LookupFunction } from 'net';
3
+ import { Agent } from 'undici';
4
+ import { DispatchHandlers } from 'undici/types/dispatcher';
5
+ import { BuildOptions } from 'undici/types/connector';
6
+ export declare type CheckAddressFunction = (ip: string, family: number | string) => boolean;
7
+ export declare type HttpAgentOptions = {
8
+ lookup?: LookupFunction;
9
+ checkAddress?: CheckAddressFunction;
10
+ connect?: BuildOptions;
11
+ };
12
+ export declare class HttpAgent extends Agent {
13
+ #private;
14
+ constructor(options: HttpAgentOptions);
15
+ dispatch(options: Agent.DispatchOptions, handler: DispatchHandlers): boolean;
16
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var _HttpAgent_checkAddress;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.HttpAgent = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const dns_1 = tslib_1.__importDefault(require("dns"));
7
+ const net_1 = require("net");
8
+ const undici_1 = require("undici");
9
+ class IllegalAddressError extends Error {
10
+ constructor(hostname, ip, family) {
11
+ const message = 'illegal address';
12
+ super(message);
13
+ this.name = this.constructor.name;
14
+ this.hostname = hostname;
15
+ this.ip = ip;
16
+ this.family = family;
17
+ Error.captureStackTrace(this, this.constructor);
18
+ }
19
+ }
20
+ class HttpAgent extends undici_1.Agent {
21
+ constructor(options) {
22
+ var _a;
23
+ /* eslint node/prefer-promises/dns: off*/
24
+ const _lookup = (_a = options.lookup) !== null && _a !== void 0 ? _a : dns_1.default.lookup;
25
+ const lookup = (hostname, dnsOptions, callback) => {
26
+ _lookup(hostname, dnsOptions, (err, address, family) => {
27
+ if (err)
28
+ return callback(err, address, family);
29
+ if (options.checkAddress && !options.checkAddress(address, family)) {
30
+ err = new IllegalAddressError(hostname, address, family);
31
+ }
32
+ callback(err, address, family);
33
+ });
34
+ };
35
+ super({
36
+ connect: { ...options.connect, lookup },
37
+ });
38
+ _HttpAgent_checkAddress.set(this, void 0);
39
+ tslib_1.__classPrivateFieldSet(this, _HttpAgent_checkAddress, options.checkAddress, "f");
40
+ }
41
+ dispatch(options, handler) {
42
+ if (tslib_1.__classPrivateFieldGet(this, _HttpAgent_checkAddress, "f") && options.origin) {
43
+ const originUrl = typeof options.origin === 'string' ? new URL(options.origin) : options.origin;
44
+ let hostname = originUrl.hostname;
45
+ // [2001:db8:2de::e13] => 2001:db8:2de::e13
46
+ if (hostname.startsWith('[') && hostname.endsWith(']')) {
47
+ hostname = hostname.substring(1, hostname.length - 1);
48
+ }
49
+ const family = (0, net_1.isIP)(hostname);
50
+ if (family === 4 || family === 6) {
51
+ // if request hostname is ip, custom lookup won't excute
52
+ if (!tslib_1.__classPrivateFieldGet(this, _HttpAgent_checkAddress, "f").call(this, hostname, family)) {
53
+ throw new IllegalAddressError(hostname, hostname, family);
54
+ }
55
+ }
56
+ }
57
+ return super.dispatch(options, handler);
58
+ }
59
+ }
60
+ exports.HttpAgent = HttpAgent;
61
+ _HttpAgent_checkAddress = new WeakMap();
62
+ //# sourceMappingURL=HttpAgent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HttpAgent.js","sourceRoot":"","sources":["../HttpAgent.ts"],"names":[],"mappings":";;;;;AAAA,sDAAsB;AACtB,6BAA2C;AAC3C,mCAEgB;AAYhB,MAAM,mBAAoB,SAAQ,KAAK;IAKrC,YAAY,QAAgB,EAAE,EAAU,EAAE,MAAc;QACtD,MAAM,OAAO,GAAG,iBAAiB,CAAC;QAClC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC;QAClC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAClD,CAAC;CACF;AAED,MAAa,SAAU,SAAQ,cAAK;IAGlC,YAAY,OAAyB;;QACnC,yCAAyC;QACzC,MAAM,OAAO,GAAG,MAAA,OAAO,CAAC,MAAM,mCAAI,aAAG,CAAC,MAAM,CAAC;QAC7C,MAAM,MAAM,GAAmB,CAAC,QAAQ,EAAE,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ACD,MAAM,MAAM,GAAG,IAAA,UAAI,EAAC,QAAQ,CAAC,CAAC;YAC9B,IAAI,MAAM,KAAK,CAAC,IAAI,MAAM,KAAK,CAAC,EAAE;gBAChC,wDAAwD;gBACxD,IAAI,CAAC,+BAAA,IAAI,+BAAc,MAAlB,IAAI,EAAe,QAAQ,EAAE,MAAM,CAAC,EAAE;oBACzC,MAAM,IAAI,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;iBAC3D;aACF;SACF;QACD,OAAO,KAAK,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC1C,CAAC;CACF;AAvCD,8BAuCC"}
@@ -1,12 +1,40 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
2
4
  import { EventEmitter } from 'events';
5
+ import { LookupFunction } from 'net';
6
+ import { CheckAddressFunction } from './HttpAgent';
3
7
  import { RequestURL, RequestOptions } from './Request';
4
8
  import { HttpClientResponse } from './Response';
5
9
  export declare type ClientOptions = {
6
10
  defaultArgs?: RequestOptions;
11
+ /**
12
+ * Custom DNS lookup function, default is `dns.lookup`.
13
+ */
14
+ lookup?: LookupFunction;
15
+ /**
16
+ * check request address to protect from SSRF and similar attacks.
17
+ * It receive two arguments(ip and family) and should return true or false to identified the address is legal or not.
18
+ * It rely on lookup and have the same version requirement.
19
+ */
20
+ checkAddress?: CheckAddressFunction;
21
+ connect?: {
22
+ key?: string | Buffer;
23
+ /**
24
+ * A string or Buffer containing the certificate key of the client in PEM format.
25
+ * Notes: This is necessary only if using the client certificate authentication
26
+ */
27
+ cert?: string | Buffer;
28
+ /**
29
+ * If true, the server certificate is verified against the list of supplied CAs.
30
+ * An 'error' event is emitted if verification fails.Default: true.
31
+ */
32
+ rejectUnauthorized?: boolean;
33
+ };
7
34
  };
35
+ export declare const HEADER_USER_AGENT: string;
8
36
  export declare class HttpClient extends EventEmitter {
9
- defaultArgs?: RequestOptions;
37
+ #private;
10
38
  constructor(clientOptions?: ClientOptions);
11
39
  request(url: RequestURL, options?: RequestOptions): Promise<HttpClientResponse>;
12
40
  }