urllib 3.0.0-alpha.1 → 3.0.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/README.md +15 -113
- package/package.json +13 -9
- package/src/HttpAgent.ts +72 -0
- package/src/HttpClient.ts +303 -84
- package/src/Request.ts +51 -54
- package/src/Response.ts +11 -10
- package/src/cjs/HttpAgent.d.ts +16 -0
- package/src/cjs/HttpAgent.js +62 -0
- package/src/cjs/HttpAgent.js.map +1 -0
- package/src/cjs/HttpClient.d.ts +28 -1
- package/src/cjs/HttpClient.js +371 -198
- package/src/cjs/HttpClient.js.map +1 -1
- package/src/cjs/Request.d.ts +47 -54
- package/src/cjs/Response.d.ts +9 -8
- package/src/cjs/utils.d.ts +1 -0
- package/src/cjs/utils.js +7 -1
- package/src/cjs/utils.js.map +1 -1
- package/src/esm/HttpAgent.d.ts +16 -0
- package/src/esm/HttpAgent.js +58 -0
- package/src/esm/HttpAgent.js.map +1 -0
- package/src/esm/HttpClient.d.ts +28 -1
- package/src/esm/HttpClient.js +372 -199
- package/src/esm/HttpClient.js.map +1 -1
- package/src/esm/Request.d.ts +47 -54
- package/src/esm/Response.d.ts +9 -8
- package/src/esm/utils.d.ts +1 -0
- package/src/esm/utils.js +5 -0
- package/src/esm/utils.js.map +1 -1
- package/src/index.ts +0 -1
- package/src/utils.ts +6 -0
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
|
-
|
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
|
26
|
+
export type ReadableWithMeta = Readable & {
|
27
27
|
status: number;
|
28
28
|
statusCode: number;
|
29
|
-
|
30
|
-
headers: Record<string, string>;
|
29
|
+
headers: IncomingHttpHeaders;
|
31
30
|
};
|
32
31
|
|
33
32
|
export type HttpClientResponse = {
|
34
|
-
|
33
|
+
opaque: unknown;
|
34
|
+
data: any
|
35
35
|
status: number;
|
36
|
-
headers:
|
36
|
+
headers: IncomingHttpHeaders;
|
37
37
|
url: string;
|
38
38
|
redirected: boolean;
|
39
|
-
|
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,MAA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AE,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;aACvD;YACD,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"}
|
package/src/cjs/HttpClient.d.ts
CHANGED
@@ -1,12 +1,39 @@
|
|
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
|
};
|
8
35
|
export declare class HttpClient extends EventEmitter {
|
9
|
-
|
36
|
+
#private;
|
10
37
|
constructor(clientOptions?: ClientOptions);
|
11
38
|
request(url: RequestURL, options?: RequestOptions): Promise<HttpClientResponse>;
|
12
39
|
}
|