@nomicfoundation/hardhat-utils 4.2.0 → 4.3.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/CHANGELOG.md +10 -0
- package/dist/src/errors/request.d.ts +4 -0
- package/dist/src/errors/request.d.ts.map +1 -1
- package/dist/src/errors/request.js +7 -0
- package/dist/src/errors/request.js.map +1 -1
- package/dist/src/eth.d.ts +9 -0
- package/dist/src/eth.d.ts.map +1 -1
- package/dist/src/eth.js +16 -1
- package/dist/src/eth.js.map +1 -1
- package/dist/src/internal/eth.d.ts +14 -5
- package/dist/src/internal/eth.d.ts.map +1 -1
- package/dist/src/internal/eth.js +16 -6
- package/dist/src/internal/eth.js.map +1 -1
- package/dist/src/internal/request.d.ts +37 -0
- package/dist/src/internal/request.d.ts.map +1 -1
- package/dist/src/internal/request.js +168 -1
- package/dist/src/internal/request.js.map +1 -1
- package/dist/src/request.d.ts +26 -9
- package/dist/src/request.d.ts.map +1 -1
- package/dist/src/request.js +31 -38
- package/dist/src/request.js.map +1 -1
- package/package.json +1 -1
- package/src/errors/request.ts +12 -0
- package/src/eth.ts +22 -0
- package/src/internal/eth.ts +23 -6
- package/src/internal/request.ts +218 -0
- package/src/request.ts +38 -45
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @nomicfoundation/hardhat-utils
|
|
2
2
|
|
|
3
|
+
## 4.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#8616](https://github.com/NomicFoundation/hardhat/pull/8616) [`8b73917`](https://github.com/NomicFoundation/hardhat/commit/8b73917cbf830b58c773ea7580d2d30c4fb11b43) Thanks [@rome-xi](https://github.com/rome-xi)! - Fixed invalid `HTTPS_PROXY`/`HTTP_PROXY` values being reported as unexpected errors instead of a Hardhat error.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#8601](https://github.com/NomicFoundation/hardhat/pull/8601) [`34087dd`](https://github.com/NomicFoundation/hardhat/commit/34087dd349e40be5bf4d5dbdce557b23b98cfefd) Thanks [@cdesch](https://github.com/cdesch)! - Improved HTTP proxy support.
|
|
12
|
+
|
|
3
13
|
## 4.2.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
|
@@ -9,6 +9,10 @@ export declare class DownloadError extends CustomError {
|
|
|
9
9
|
export declare class DispatcherError extends CustomError {
|
|
10
10
|
constructor(message: string, cause?: Error);
|
|
11
11
|
}
|
|
12
|
+
export declare class InvalidProxyUrlError extends CustomError {
|
|
13
|
+
readonly envVarName: string;
|
|
14
|
+
constructor(envVarName: string, cause?: Error);
|
|
15
|
+
}
|
|
12
16
|
export declare class RequestTimeoutError extends CustomError {
|
|
13
17
|
constructor(url: string, cause?: Error);
|
|
14
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/errors/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,QAAQ,CAAC;AAElC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAI1C,qBAAa,YAAa,SAAQ,WAAW;gBAC/B,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,UAAU,EAAE,KAAK,CAAC,EAAE,KAAK;CAG5E;AAED,qBAAa,aAAc,SAAQ,WAAW;gBAChC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK;CAGvC;AAED,qBAAa,eAAgB,SAAQ,WAAW;gBAClC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK;CAG3C;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK;CAGvC;AAED,qBAAa,sBAAuB,SAAQ,WAAW;gBACzC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,KA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tD,SAAgB,UAAU,EAAE,MAAM,CAAC;IACnC,SAAgB,OAAO,EACrB,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC;IAClE,SAAgB,IAAI,EAAE,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC;gBAE9C,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;CAuCtC"}
|
|
@@ -16,6 +16,13 @@ export class DispatcherError extends CustomError {
|
|
|
16
16
|
super(`Failed to create dispatcher: ${message}`, cause);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
+
export class InvalidProxyUrlError extends CustomError {
|
|
20
|
+
envVarName;
|
|
21
|
+
constructor(envVarName, cause) {
|
|
22
|
+
super(`The proxy url configured in the ${envVarName} environment variable is not a valid url`, cause);
|
|
23
|
+
this.envVarName = envVarName;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
19
26
|
export class RequestTimeoutError extends CustomError {
|
|
20
27
|
constructor(url, cause) {
|
|
21
28
|
super(`Request to ${sanitizeUrl(url)} timed out`, cause);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../../src/errors/request.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,OAAO,YAAa,SAAQ,WAAW;IAC3C,YAAY,GAAW,EAAE,IAAmC,EAAE,KAAa;QACzE,KAAK,CAAC,kBAAkB,IAAI,eAAe,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IACxE,CAAC;CACF;AAED,MAAM,OAAO,aAAc,SAAQ,WAAW;IAC5C,YAAY,GAAW,EAAE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KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,IAAI,GAAG,MAAM,IAAI,KAAK,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IAC1E,CAAC;IAED,eAAe,CACb,KAAY;QAEZ,IAAI,SAAS,IAAI,KAAK,EAAE,CAAC;YACvB,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;YAC9B,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC3B,OAAO,OAAO,CAAC;YACjB,CAAC;iBAAM,IAAI,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC;gBACzC,OAAO,OAAO,CAAC;YACjB,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,eAAe,CACb,OAAgB;QAEhB,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YACvB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CACjC,CAAC,MAAM,EAAE,EAAE,CACT,OAAO,MAAM,KAAK,QAAQ;YAC1B,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;gBACpB,MAAM,CAAC,KAAK,CAAC,CAAC,IAAa,EAAE,EAAE,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC;YAC5D,MAAM,KAAK,SAAS,CACvB,CAAC;IACJ,CAAC;CACF"}
|
package/dist/src/eth.d.ts
CHANGED
|
@@ -13,6 +13,15 @@ export declare function isAddress(value: unknown): value is PrefixedHexString;
|
|
|
13
13
|
* @returns True if the value is an Ethereum address with a valid checksum, false otherwise.
|
|
14
14
|
*/
|
|
15
15
|
export declare function isValidChecksumAddress(value: unknown): Promise<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Converts an Ethereum address to its EIP-55 checksummed representation.
|
|
18
|
+
*
|
|
19
|
+
* @param address The address to convert. It can be checksummed or not, but it
|
|
20
|
+
* must be a valid Ethereum address.
|
|
21
|
+
* @returns The EIP-55 checksummed representation of the address.
|
|
22
|
+
* @throws InvalidParameterError If the input is not an Ethereum address.
|
|
23
|
+
*/
|
|
24
|
+
export declare function toChecksumAddress(address: string): Promise<PrefixedHexString>;
|
|
16
25
|
/**
|
|
17
26
|
* Checks if a value is an Ethereum hash.
|
|
18
27
|
*
|
package/dist/src/eth.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth.d.ts","sourceRoot":"","sources":["../../src/eth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"eth.d.ts","sourceRoot":"","sources":["../../src/eth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAWlD;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,iBAAiB,CAEpE;AAED;;;;;GAKG;AACH,wBAAsB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAE7E;AAED;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,iBAAiB,CAAC,CAQ5B;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,iBAAiB,CAEjE;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,iBAAiB,CAEnE;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,UAAU,CAAC,CAG7D;AAED;;;;GAIG;AACH,wBAAsB,UAAU,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAG7D;AAED;;;;;GAKG;AACH,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,UAAU,CAAC,CAIhE;AAED;;;;GAIG;AACH,wBAAsB,aAAa,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAGhE"}
|
package/dist/src/eth.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { InvalidParameterError } from "./common-errors.js";
|
|
1
2
|
import { bytesToHexString, numberToHexString, setLengthLeft } from "./hex.js";
|
|
2
|
-
import { getAddressGenerator, getHashGenerator, isValidChecksum, } from "./internal/eth.js";
|
|
3
|
+
import { getAddressGenerator, getHashGenerator, isValidChecksum, toChecksumAddress as toChecksumAddressImpl, } from "./internal/eth.js";
|
|
3
4
|
/**
|
|
4
5
|
* Checks if a value is an Ethereum address.
|
|
5
6
|
*
|
|
@@ -18,6 +19,20 @@ export function isAddress(value) {
|
|
|
18
19
|
export async function isValidChecksumAddress(value) {
|
|
19
20
|
return await (isAddress(value) && isValidChecksum(value));
|
|
20
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Converts an Ethereum address to its EIP-55 checksummed representation.
|
|
24
|
+
*
|
|
25
|
+
* @param address The address to convert. It can be checksummed or not, but it
|
|
26
|
+
* must be a valid Ethereum address.
|
|
27
|
+
* @returns The EIP-55 checksummed representation of the address.
|
|
28
|
+
* @throws InvalidParameterError If the input is not an Ethereum address.
|
|
29
|
+
*/
|
|
30
|
+
export async function toChecksumAddress(address) {
|
|
31
|
+
if (!isAddress(address)) {
|
|
32
|
+
throw new InvalidParameterError(`Expected an Ethereum address. Received: ${address}`);
|
|
33
|
+
}
|
|
34
|
+
return await toChecksumAddressImpl(address);
|
|
35
|
+
}
|
|
21
36
|
/**
|
|
22
37
|
* Checks if a value is an Ethereum hash.
|
|
23
38
|
*
|
package/dist/src/eth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth.js","sourceRoot":"","sources":["../../src/eth.ts"],"names":[],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AsB;IAC9C,OAAO,aAAa,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;AACrD,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB;IACrC,MAAM,aAAa,GAAG,MAAM,gBAAgB,EAAE,CAAC;IAC/C,OAAO,MAAM,aAAa,CAAC,IAAI,EAAE,CAAC;AACpC,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU;IAC9B,MAAM,SAAS,GAAG,MAAM,iBAAiB,EAAE,CAAC;IAC5C,OAAO,gBAAgB,CAAC,SAAS,CAAC,CAAC;AACrC,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB;IACxC,MAAM,gBAAgB,GAAG,MAAM,mBAAmB,EAAE,CAAC;IACrD,MAAM,SAAS,GAAG,MAAM,gBAAgB,CAAC,IAAI,EAAE,CAAC;IAChD,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAChC,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa;IACjC,MAAM,YAAY,GAAG,MAAM,oBAAoB,EAAE,CAAC;IAClD,OAAO,gBAAgB,CAAC,YAAY,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PrefixedHexString } from "../hex.js";
|
|
1
2
|
declare class RandomBytesGenerator {
|
|
2
3
|
#private;
|
|
3
4
|
private constructor();
|
|
@@ -7,15 +8,23 @@ declare class RandomBytesGenerator {
|
|
|
7
8
|
export declare function getHashGenerator(): Promise<RandomBytesGenerator>;
|
|
8
9
|
export declare function getAddressGenerator(): Promise<RandomBytesGenerator>;
|
|
9
10
|
/**
|
|
10
|
-
*
|
|
11
|
-
* This method is
|
|
11
|
+
* Converts an Ethereum address to its EIP-55 checksummed representation.
|
|
12
|
+
* This method is an adaptation of the ethereumjs methods at this link:
|
|
12
13
|
* https://github.com/ethereumjs/ethereumjs-monorepo/blob/47f388bfeec553519d11259fee7e7161a77b29b2/packages/util/src/account.ts#L440-L478
|
|
13
14
|
* The main differences are:
|
|
14
|
-
* - the
|
|
15
|
-
* - tha `eip1191ChainId` parameter has been removed.
|
|
15
|
+
* - the `eip1191ChainId` parameter has been removed.
|
|
16
16
|
* - the code has been modified to use the `hardhat-utils` methods
|
|
17
17
|
*
|
|
18
|
+
* It doesn't validate that the input is an Ethereum address; callers must
|
|
19
|
+
* check that first (see the public wrappers in `../eth.ts`).
|
|
18
20
|
*/
|
|
19
|
-
export declare function
|
|
21
|
+
export declare function toChecksumAddress(hexAddress: PrefixedHexString): Promise<PrefixedHexString>;
|
|
22
|
+
/**
|
|
23
|
+
* Checks if the checksum of an address is valid.
|
|
24
|
+
*
|
|
25
|
+
* It doesn't validate that the input is an Ethereum address; callers must
|
|
26
|
+
* check that first (see the public wrappers in `../eth.ts`).
|
|
27
|
+
*/
|
|
28
|
+
export declare function isValidChecksum(hexAddress: PrefixedHexString): Promise<boolean>;
|
|
20
29
|
export {};
|
|
21
30
|
//# sourceMappingURL=eth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth.d.ts","sourceRoot":"","sources":["../../../src/internal/eth.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"eth.d.ts","sourceRoot":"","sources":["../../../src/internal/eth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAMnD,cAAM,oBAAoB;;IAGxB,OAAO;WAIa,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IAM1D,IAAI,IAAI,OAAO,CAAC,UAAU,CAAC;CAOzC;AAKD,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,oBAAoB,CAAC,CAKtE;AAED,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,oBAAoB,CAAC,CAKzE;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CACrC,UAAU,EAAE,iBAAiB,GAC5B,OAAO,CAAC,iBAAiB,CAAC,CAiB5B;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,UAAU,EAAE,iBAAiB,GAC5B,OAAO,CAAC,OAAO,CAAC,CAElB"}
|
package/dist/src/internal/eth.js
CHANGED
|
@@ -31,16 +31,17 @@ export async function getAddressGenerator() {
|
|
|
31
31
|
return addressGenerator;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
|
-
*
|
|
35
|
-
* This method is
|
|
34
|
+
* Converts an Ethereum address to its EIP-55 checksummed representation.
|
|
35
|
+
* This method is an adaptation of the ethereumjs methods at this link:
|
|
36
36
|
* https://github.com/ethereumjs/ethereumjs-monorepo/blob/47f388bfeec553519d11259fee7e7161a77b29b2/packages/util/src/account.ts#L440-L478
|
|
37
37
|
* The main differences are:
|
|
38
|
-
* - the
|
|
39
|
-
* - tha `eip1191ChainId` parameter has been removed.
|
|
38
|
+
* - the `eip1191ChainId` parameter has been removed.
|
|
40
39
|
* - the code has been modified to use the `hardhat-utils` methods
|
|
41
40
|
*
|
|
41
|
+
* It doesn't validate that the input is an Ethereum address; callers must
|
|
42
|
+
* check that first (see the public wrappers in `../eth.ts`).
|
|
42
43
|
*/
|
|
43
|
-
export async function
|
|
44
|
+
export async function toChecksumAddress(hexAddress) {
|
|
44
45
|
const address = getUnprefixedHexString(hexAddress).toLowerCase();
|
|
45
46
|
const bytes = utf8StringToBytes(address);
|
|
46
47
|
const hash = bytesToHexString(await keccak256(bytes)).slice(2);
|
|
@@ -53,6 +54,15 @@ export async function isValidChecksum(hexAddress) {
|
|
|
53
54
|
ret += address[i];
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
|
-
return `0x${ret}
|
|
57
|
+
return `0x${ret}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Checks if the checksum of an address is valid.
|
|
61
|
+
*
|
|
62
|
+
* It doesn't validate that the input is an Ethereum address; callers must
|
|
63
|
+
* check that first (see the public wrappers in `../eth.ts`).
|
|
64
|
+
*/
|
|
65
|
+
export async function isValidChecksum(hexAddress) {
|
|
66
|
+
return (await toChecksumAddress(hexAddress)) === hexAddress;
|
|
57
67
|
}
|
|
58
68
|
//# sourceMappingURL=eth.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eth.js","sourceRoot":"","sources":["../../../src/internal/eth.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"eth.js","sourceRoot":"","sources":["../../../src/internal/eth.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAErE,MAAM,oBAAoB;IACxB,UAAU,CAAa;IAEvB,YAAoB,SAAqB;QACvC,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;IAC9B,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAY;QACrC,MAAM,SAAS,GAAG,MAAM,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAErD,OAAO,IAAI,oBAAoB,CAAC,SAAS,CAAC,CAAC;IAC7C,CAAC;IAEM,KAAK,CAAC,IAAI;QACf,MAAM,aAAa,GAAG,IAAI,CAAC,UAAU,CAAC;QAEtC,IAAI,CAAC,UAAU,GAAG,MAAM,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAEnD,OAAO,aAAa,CAAC;IACvB,CAAC;CACF;AAED,IAAI,aAAa,GAAgC,IAAI,CAAC;AACtD,IAAI,gBAAgB,GAAgC,IAAI,CAAC;AAEzD,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,IAAI,aAAa,KAAK,IAAI,EAAE,CAAC;QAC3B,aAA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,6 +1,10 @@
|
|
|
1
1
|
import type { DispatcherOptions, RequestOptions } from "../request.js";
|
|
2
2
|
import type EventEmitter from "node:events";
|
|
3
3
|
import type * as UndiciT from "undici";
|
|
4
|
+
export interface ProxyEnvVar {
|
|
5
|
+
name: string;
|
|
6
|
+
value: string;
|
|
7
|
+
}
|
|
4
8
|
export declare function generateTempFilePath(filePath: string): Promise<string>;
|
|
5
9
|
export declare function getBaseRequestOptions(requestUrl: string, { extraHeaders, abortSignal, queryParams }?: RequestOptions, dispatcherOrDispatcherOptions?: UndiciT.Dispatcher | DispatcherOptions): Promise<{
|
|
6
10
|
query?: Record<string, any> | undefined;
|
|
@@ -10,6 +14,39 @@ export declare function getBaseRequestOptions(requestUrl: string, { extraHeaders
|
|
|
10
14
|
}>;
|
|
11
15
|
export declare function getHeaders(requestUrl: string, extraHeaders?: Record<string, string>): Record<string, string>;
|
|
12
16
|
export declare function getAuthHeader(requestUrl: string): string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Determines whether a url points at the loopback interface.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isLoopbackUrl(parsedUrl: URL): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Determines whether `NO_PROXY` (or `no_proxy`) excludes a url from being
|
|
23
|
+
* proxied. A value of `*` excludes everything; the other entries are matched as
|
|
24
|
+
* described in {@link parseNoProxy}.
|
|
25
|
+
*/
|
|
26
|
+
export declare function isExcludedByNoProxy(parsedUrl: URL): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Finds the environment variable that configures the proxy for a given url,
|
|
29
|
+
* following the usual precedence: the variable matching the url's protocol
|
|
30
|
+
* first, lowercase before uppercase, then the other protocol's as a fallback.
|
|
31
|
+
*
|
|
32
|
+
* Empty and whitespace-only values are treated as unset, as that's how users
|
|
33
|
+
* disable a variable inherited from a parent environment. The returned value is
|
|
34
|
+
* trimmed, but not validated.
|
|
35
|
+
*
|
|
36
|
+
* @param requestUrl The url the proxy would be used for.
|
|
37
|
+
* @returns The variable's name and value, or `undefined` if none is set or the
|
|
38
|
+
* url's protocol isn't proxyable.
|
|
39
|
+
*/
|
|
40
|
+
export declare function findProxyEnvVar(requestUrl: string): ProxyEnvVar | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Resolves the proxy to use for a given url from the environment.
|
|
43
|
+
*
|
|
44
|
+
* @param requestUrl The url the proxy would be used for.
|
|
45
|
+
* @returns The proxy url, or `undefined` if the environment configures none,
|
|
46
|
+
* `NO_PROXY` excludes the url, or the url can't be proxied.
|
|
47
|
+
* @throws InvalidProxyUrlError If the configured proxy isn't a valid http(s) url.
|
|
48
|
+
*/
|
|
49
|
+
export declare function resolveProxyFromEnv(requestUrl: string): string | undefined;
|
|
13
50
|
export declare function getProxyDispatcher(proxy: string, options: Omit<UndiciT.ProxyAgent.Options, "uri">): Promise<UndiciT.ProxyAgent>;
|
|
14
51
|
export declare function getPoolDispatcher(requestUrl: string, options: UndiciT.Pool.Options): Promise<UndiciT.Pool>;
|
|
15
52
|
export declare function getBasicDispatcher(options: UndiciT.Agent.Options): Promise<UndiciT.Agent>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/internal/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,KAAK,YAAY,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,KAAK,OAAO,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/internal/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACvE,OAAO,KAAK,YAAY,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,KAAK,OAAO,MAAM,QAAQ,CAAC;AAqBvC,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CA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AU,CAAC,OAAO,EAAE,KAAK,CAAC,GAC/C,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAS7B;AAED,wBAAsB,iBAAiB,CACrC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,OAAO,GAC5B,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAOvB;AAED,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,GAC7B,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAMxB;AAED,wBAAgB,wBAAwB,CACtC,OAAO,GAAE,MAAwC,EACjD,gBAAgB,GAAE,OAAe,GAChC,OAAO,CAAC,MAAM,CAAC,OAAO,CAaxB;AAED,wBAAgB,WAAW,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAItD;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI,CAuB9D"}
|
|
@@ -3,10 +3,24 @@ import { STATUS_CODES } from "node:http";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { mkdir } from "../fs.js";
|
|
5
5
|
import { isObject } from "../lang.js";
|
|
6
|
-
import { ConnectionRefusedError, DEFAULT_MAX_REDIRECTS, DEFAULT_TIMEOUT_IN_MILLISECONDS, DEFAULT_USER_AGENT, getDispatcher, RequestTimeoutError, ResponseStatusCodeError, } from "../request.js";
|
|
6
|
+
import { ConnectionRefusedError, DEFAULT_MAX_REDIRECTS, DEFAULT_TIMEOUT_IN_MILLISECONDS, DEFAULT_USER_AGENT, getDispatcher, InvalidProxyUrlError, isValidUrl, RequestTimeoutError, ResponseStatusCodeError, shouldUseProxy, } from "../request.js";
|
|
7
7
|
// We don't load undici on startup because this package is transitively imported
|
|
8
8
|
// from too many places and it's too complex to optimize case by case.
|
|
9
9
|
let undici;
|
|
10
|
+
const HTTPS_PROXY_ENV_VARS = ["https_proxy", "HTTPS_PROXY"];
|
|
11
|
+
const HTTP_PROXY_ENV_VARS = ["http_proxy", "HTTP_PROXY"];
|
|
12
|
+
const NO_PROXY_ENV_VARS = ["no_proxy", "NO_PROXY"];
|
|
13
|
+
// `URL` normalizes IPv6 loopback to `[::1]` and alternate IPv4 forms to
|
|
14
|
+
// dotted decimal. Only IPv6 hostnames include brackets.
|
|
15
|
+
const LOOPBACK_HOSTNAMES = new Set([
|
|
16
|
+
"localhost",
|
|
17
|
+
"0.0.0.0",
|
|
18
|
+
"[::1]",
|
|
19
|
+
]);
|
|
20
|
+
const DEFAULT_PORTS = new Map([
|
|
21
|
+
["http:", 80],
|
|
22
|
+
["https:", 443],
|
|
23
|
+
]);
|
|
10
24
|
export async function generateTempFilePath(filePath) {
|
|
11
25
|
const { dir, ext, name } = path.parse(filePath);
|
|
12
26
|
await mkdir(dir);
|
|
@@ -54,6 +68,125 @@ export function getAuthHeader(requestUrl) {
|
|
|
54
68
|
}
|
|
55
69
|
return `Basic ${Buffer.from(`${parsedUrl.username}:${parsedUrl.password}`).toString("base64")}`;
|
|
56
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Determines whether a url points at the loopback interface.
|
|
73
|
+
*/
|
|
74
|
+
export function isLoopbackUrl(parsedUrl) {
|
|
75
|
+
const hostname = parsedUrl.hostname.toLowerCase();
|
|
76
|
+
if (LOOPBACK_HOSTNAMES.has(hostname)) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
// Subdomains of localhost, e.g. `api.localhost`.
|
|
80
|
+
if (hostname.endsWith(".localhost")) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
// The whole 127.0.0.0/8 range is loopback, not just 127.0.0.1.
|
|
84
|
+
return /^127(?:\.\d{1,3}){3}$/.test(hostname);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Determines whether `NO_PROXY` (or `no_proxy`) excludes a url from being
|
|
88
|
+
* proxied. A value of `*` excludes everything; the other entries are matched as
|
|
89
|
+
* described in {@link parseNoProxy}.
|
|
90
|
+
*/
|
|
91
|
+
export function isExcludedByNoProxy(parsedUrl) {
|
|
92
|
+
// An empty or whitespace-only value means unset, so it falls through to the
|
|
93
|
+
// other casing instead of shadowing it.
|
|
94
|
+
const noProxy = NO_PROXY_ENV_VARS.map((name) => process.env[name]?.trim()).find((value) => value !== undefined && value !== "");
|
|
95
|
+
if (noProxy === undefined) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
if (noProxy === "*") {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
const hostname = normalizeHostname(parsedUrl.hostname);
|
|
102
|
+
const parsedPort = Number.parseInt(parsedUrl.port, 10);
|
|
103
|
+
const port = Number.isNaN(parsedPort)
|
|
104
|
+
? DEFAULT_PORTS.get(parsedUrl.protocol)
|
|
105
|
+
: parsedPort;
|
|
106
|
+
for (const entry of parseNoProxy(noProxy)) {
|
|
107
|
+
// A portless entry matches every port.
|
|
108
|
+
if (entry.port !== 0 && entry.port !== port) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (hostname === entry.hostname ||
|
|
112
|
+
hostname.endsWith(`.${entry.hostname}`)) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Finds the environment variable that configures the proxy for a given url,
|
|
120
|
+
* following the usual precedence: the variable matching the url's protocol
|
|
121
|
+
* first, lowercase before uppercase, then the other protocol's as a fallback.
|
|
122
|
+
*
|
|
123
|
+
* Empty and whitespace-only values are treated as unset, as that's how users
|
|
124
|
+
* disable a variable inherited from a parent environment. The returned value is
|
|
125
|
+
* trimmed, but not validated.
|
|
126
|
+
*
|
|
127
|
+
* @param requestUrl The url the proxy would be used for.
|
|
128
|
+
* @returns The variable's name and value, or `undefined` if none is set or the
|
|
129
|
+
* url's protocol isn't proxyable.
|
|
130
|
+
*/
|
|
131
|
+
export function findProxyEnvVar(requestUrl) {
|
|
132
|
+
const { protocol } = new URL(requestUrl);
|
|
133
|
+
let names;
|
|
134
|
+
if (protocol === "https:") {
|
|
135
|
+
names = HTTPS_PROXY_ENV_VARS.concat(HTTP_PROXY_ENV_VARS);
|
|
136
|
+
}
|
|
137
|
+
else if (protocol === "http:") {
|
|
138
|
+
names = HTTP_PROXY_ENV_VARS.concat(HTTPS_PROXY_ENV_VARS);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
for (const name of names) {
|
|
144
|
+
const value = process.env[name]?.trim();
|
|
145
|
+
if (value !== undefined && value !== "") {
|
|
146
|
+
return { name, value };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Resolves the proxy to use for a given url from the environment.
|
|
153
|
+
*
|
|
154
|
+
* @param requestUrl The url the proxy would be used for.
|
|
155
|
+
* @returns The proxy url, or `undefined` if the environment configures none,
|
|
156
|
+
* `NO_PROXY` excludes the url, or the url can't be proxied.
|
|
157
|
+
* @throws InvalidProxyUrlError If the configured proxy isn't a valid http(s) url.
|
|
158
|
+
*/
|
|
159
|
+
export function resolveProxyFromEnv(requestUrl) {
|
|
160
|
+
// `shouldUseProxy` and `findProxyEnvVar` both parse the url. Requests to an
|
|
161
|
+
// invalid one fail later with a better error than anything we'd raise here.
|
|
162
|
+
if (!isValidUrl(requestUrl) || !shouldUseProxy(requestUrl)) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
const proxyEnvVar = findProxyEnvVar(requestUrl);
|
|
166
|
+
if (proxyEnvVar === undefined) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
if (!isValidProxyUrl(proxyEnvVar.value)) {
|
|
170
|
+
throw new InvalidProxyUrlError(proxyEnvVar.name);
|
|
171
|
+
}
|
|
172
|
+
return proxyEnvVar.value;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Determines whether a value can be used as a proxy url.
|
|
176
|
+
*
|
|
177
|
+
* undici's `ProxyAgent` only speaks http(s). Checking the protocol here, rather
|
|
178
|
+
* than leaving it to undici, catches two cases that would otherwise surface as
|
|
179
|
+
* opaque errors: a scheme-less `host:port`, which parses as a url whose
|
|
180
|
+
* protocol is the host and so never looks like a missing scheme, and `socks5:`,
|
|
181
|
+
* which undici accepts before failing at request time.
|
|
182
|
+
*/
|
|
183
|
+
function isValidProxyUrl(value) {
|
|
184
|
+
if (!isValidUrl(value)) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
const { protocol } = new URL(value);
|
|
188
|
+
return protocol === "http:" || protocol === "https:";
|
|
189
|
+
}
|
|
57
190
|
export async function getProxyDispatcher(proxy, options) {
|
|
58
191
|
if (undici === undefined) {
|
|
59
192
|
undici = await import("undici");
|
|
@@ -133,4 +266,38 @@ function describeResponseError(e) {
|
|
|
133
266
|
const reason = STATUS_CODES[statusCode];
|
|
134
267
|
e.message = `Response status code ${statusCode}${reason !== undefined ? `: ${reason}` : ""}`;
|
|
135
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* Strips the brackets an IPv6 literal carries in a url, so that an entry can be
|
|
271
|
+
* written either way. `URL` keeps them on `hostname`, while `NO_PROXY` entries
|
|
272
|
+
* are commonly written without them.
|
|
273
|
+
*/
|
|
274
|
+
function normalizeHostname(hostname) {
|
|
275
|
+
return hostname.replace(/^\[|\]$/g, "").toLowerCase();
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Parses a `NO_PROXY` value into its entries.
|
|
279
|
+
*
|
|
280
|
+
* Entries are separated by commas or whitespace. A leading `.` or `*.` marks
|
|
281
|
+
* the suffix form (`.example.com`), but a bare `example.com` matches its
|
|
282
|
+
* subdomains too, so both are stored the same way. An entry may carry a port,
|
|
283
|
+
* as in `example.com:8080`, which restricts it to that port. CIDR ranges aren't
|
|
284
|
+
* supported.
|
|
285
|
+
*/
|
|
286
|
+
function parseNoProxy(noProxy) {
|
|
287
|
+
const entries = [];
|
|
288
|
+
for (const rawEntry of noProxy.split(/[,\s]/)) {
|
|
289
|
+
const entry = rawEntry.trim();
|
|
290
|
+
if (entry === "") {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
// Only a bracketed literal or a colon-free host can carry a port, so that
|
|
294
|
+
// the trailing group of a bare IPv6 entry isn't read as one.
|
|
295
|
+
const withPort = entry.match(/^(\[[^\]]+\]|[^:]+):(\d+)$/);
|
|
296
|
+
entries.push({
|
|
297
|
+
hostname: normalizeHostname((withPort !== null ? withPort[1] : entry).replace(/^\*?\./, "")),
|
|
298
|
+
port: withPort !== null ? Number.parseInt(withPort[2], 10) : 0,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
return entries;
|
|
302
|
+
}
|
|
136
303
|
//# sourceMappingURL=request.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../../src/internal/request.ts"],"names":[],"mappings":"AAIA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACrB,+BAA+B,EAC/B,kBAAkB,EAClB,aAAa,EACb,mBAAmB,EACnB,uBAAuB,
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../../src/internal/request.ts"],"names":[],"mappings":"AAIA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAA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;AACH,SAAS,iBAAiB,CAAC,QAAgB;IACzC,OAAO,QAAQ,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;AACxD,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,YAAY,CAAC,OAAe;IACnC,MAAM,OAAO,GAAmB,EAAE,CAAC;IAEnC,KAAK,MAAM,QAAQ,IAAI,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC;QAC9C,MAAM,KAAK,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC;QAE9B,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;YACjB,SAAS;QACX,CAAC;QAED,0EAA0E;QAC1E,6DAA6D;QAC7D,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAE3D,OAAO,CAAC,IAAI,CAAC;YACX,QAAQ,EAAE,iBAAiB,CACzB,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAChE;YACD,IAAI,EAAE,QAAQ,KAAK,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;SAC/D,CAAC,CAAC;IACL,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/src/request.d.ts
CHANGED
|
@@ -11,7 +11,9 @@ export type Interceptable = Undici.Interceptable;
|
|
|
11
11
|
* Options to configure the dispatcher.
|
|
12
12
|
*
|
|
13
13
|
* @param timeout The timeout in milliseconds. Defaults to {@link DEFAULT_TIMEOUT_IN_MILLISECONDS}.
|
|
14
|
-
* @param proxy The proxy to use. If not provided,
|
|
14
|
+
* @param proxy The proxy to use. If not provided, it's resolved from the
|
|
15
|
+
* `https_proxy`/`HTTPS_PROXY` and `http_proxy`/`HTTP_PROXY` environment
|
|
16
|
+
* variables, unless `NO_PROXY` excludes the url or it points at loopback.
|
|
15
17
|
* @param pool Whether to use a pool dispatcher. Defaults to `false`.
|
|
16
18
|
* @param maxConnections The maximum number of connections to use in the pool. Defaults to {@link DEFAULT_POOL_MAX_CONNECTIONS}.
|
|
17
19
|
* @param isTestDispatcher Whether to use a test dispatcher. Defaults to `false`. It's highly recommended to use a test dispatcher in tests to avoid hanging tests.
|
|
@@ -97,14 +99,21 @@ export declare function postFormRequest(url: string, body: unknown, requestOptio
|
|
|
97
99
|
export declare function download(url: string, destination: string, requestOptions?: RequestOptions, dispatcherOrDispatcherOptions?: Undici.Dispatcher | DispatcherOptions): Promise<void>;
|
|
98
100
|
/**
|
|
99
101
|
* Creates a dispatcher based on the provided options.
|
|
100
|
-
* If the `proxy` option is set,
|
|
102
|
+
* If the `proxy` option is set, or a proxy is configured in the environment for
|
|
103
|
+
* the given url, it creates a {@link Undici.ProxyAgent} dispatcher.
|
|
101
104
|
* If the `pool` option is set to `true`, it creates a {@link Undici.Pool} dispatcher.
|
|
102
105
|
* Otherwise, it creates a basic {@link Undici.Agent} dispatcher.
|
|
103
106
|
*
|
|
107
|
+
* A proxy resolved from the environment takes precedence over `pool`, as a
|
|
108
|
+
* {@link Undici.Pool} is bound to a single origin and can't tunnel. Set
|
|
109
|
+
* `NO_PROXY` to opt a host out, or pass a {@link Undici.Dispatcher} to the
|
|
110
|
+
* request helpers to bypass this entirely.
|
|
111
|
+
*
|
|
104
112
|
* @param url The url to make requests to.
|
|
105
113
|
* @param options The options to configure the dispatcher. See {@link DispatcherOptions}.
|
|
106
114
|
* @returns The configured dispatcher instance.
|
|
107
|
-
* @throws DispatcherError If the dispatcher can't be created
|
|
115
|
+
* @throws DispatcherError If the dispatcher can't be created, including when the
|
|
116
|
+
* proxy configured in the environment isn't a valid url.
|
|
108
117
|
*/
|
|
109
118
|
export declare function getDispatcher(url: string, { timeout, proxy, pool, maxConnections, isTestDispatcher, }?: DispatcherOptions): Promise<Dispatcher>;
|
|
110
119
|
export declare function getTestDispatcher(options?: {
|
|
@@ -113,6 +122,11 @@ export declare function getTestDispatcher(options?: {
|
|
|
113
122
|
/**
|
|
114
123
|
* Determines whether a proxy should be used for a given url.
|
|
115
124
|
*
|
|
125
|
+
* Loopback addresses are never proxied, so that a local node stays reachable
|
|
126
|
+
* when a proxy is configured for everything else. Beyond those, `NO_PROXY` (or
|
|
127
|
+
* `no_proxy`) is the opt-out: `*` disables proxying entirely, and every other
|
|
128
|
+
* entry is matched as described in {@link isExcludedByNoProxy}.
|
|
129
|
+
*
|
|
116
130
|
* @param url The url to check.
|
|
117
131
|
* @returns `true` if a proxy should be used for the url, `false` otherwise.
|
|
118
132
|
*/
|
|
@@ -125,14 +139,17 @@ export declare function shouldUseProxy(url: string): boolean;
|
|
|
125
139
|
*/
|
|
126
140
|
export declare function isValidUrl(url: string): boolean;
|
|
127
141
|
/**
|
|
128
|
-
* Returns the proxy
|
|
129
|
-
* For HTTPS
|
|
130
|
-
* For HTTP
|
|
142
|
+
* Returns the proxy url from environment variables based on the target url.
|
|
143
|
+
* For HTTPS urls, checks `https_proxy` then `HTTPS_PROXY`.
|
|
144
|
+
* For HTTP urls, checks `http_proxy` then `HTTP_PROXY`.
|
|
131
145
|
* Falls back to the other protocol's proxy if none found.
|
|
132
146
|
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
147
|
+
* Empty and whitespace-only values are treated as unset, and the returned value
|
|
148
|
+
* is trimmed. It isn't validated: use {@link isValidUrl} if you need that.
|
|
149
|
+
*
|
|
150
|
+
* @param url The target url to determine proxy for.
|
|
151
|
+
* @returns The proxy url, or `undefined` if none are set.
|
|
135
152
|
*/
|
|
136
153
|
export declare function getProxyUrl(url: string): string | undefined;
|
|
137
|
-
export { ConnectionRefusedError, DispatcherError, DownloadError, RequestError, RequestTimeoutError, ResponseStatusCodeError, } from "./errors/request.js";
|
|
154
|
+
export { ConnectionRefusedError, DispatcherError, DownloadError, InvalidProxyUrlError, RequestError, RequestTimeoutError, ResponseStatusCodeError, } from "./errors/request.js";
|
|
138
155
|
//# sourceMappingURL=request.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,aAAa,CAAC;AAG5C,OAAO,KAAK,KAAK,MAAM,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,YAAY,MAAM,aAAa,CAAC;AAG5C,OAAO,KAAK,KAAK,MAAM,MAAM,QAAQ,CAAC;AA2BtC,eAAO,MAAM,+BAA+B,SAAU,CAAC;AACvD,eAAO,MAAM,qBAAqB,KAAK,CAAC;AACxC,eAAO,MAAM,4BAA4B,MAAM,CAAC;AAChD,eAAO,MAAM,kBAAkB,YAAY,CAAC;AAE5C,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;AAC3C,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,SAAS,CAAC;AAC9C,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,aAAa,CAAC;AAMjD;;;;;;;;;;GAUG;AACH,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,cAAc;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAClC,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,WAAW,CAAC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AC,CA2BrB;AAED,wBAAsB,iBAAiB,CACrC,OAAO,GAAE;IACP,OAAO,CAAC,EAAE,MAAM,CAAC;CACb,GACL,OAAO,CAAC,cAAc,CAAC,CAOzB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAInD;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAO/C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAE3D;AAED,OAAO,EACL,sBAAsB,EACtB,eAAe,EACf,aAAa,EACb,oBAAoB,EACpB,YAAY,EACZ,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,qBAAqB,CAAC"}
|
package/dist/src/request.js
CHANGED
|
@@ -4,7 +4,7 @@ import { pipeline } from "node:stream/promises";
|
|
|
4
4
|
import { ensureError } from "./error.js";
|
|
5
5
|
import { DownloadError, RequestError, DispatcherError, } from "./errors/request.js";
|
|
6
6
|
import { move, remove } from "./fs.js";
|
|
7
|
-
import { generateTempFilePath, getBaseDispatcherOptions, getBaseRequestOptions, getBasicDispatcher, getPoolDispatcher, getProxyDispatcher, handleError, } from "./internal/request.js";
|
|
7
|
+
import { findProxyEnvVar, generateTempFilePath, getBaseDispatcherOptions, getBaseRequestOptions, getBasicDispatcher, getPoolDispatcher, getProxyDispatcher, handleError, isExcludedByNoProxy, isLoopbackUrl, resolveProxyFromEnv, } from "./internal/request.js";
|
|
8
8
|
export const DEFAULT_TIMEOUT_IN_MILLISECONDS = 300_000; // Aligned with undici
|
|
9
9
|
export const DEFAULT_MAX_REDIRECTS = 10;
|
|
10
10
|
export const DEFAULT_POOL_MAX_CONNECTIONS = 128;
|
|
@@ -168,23 +168,31 @@ export async function download(url, destination, requestOptions = {}, dispatcher
|
|
|
168
168
|
}
|
|
169
169
|
/**
|
|
170
170
|
* Creates a dispatcher based on the provided options.
|
|
171
|
-
* If the `proxy` option is set,
|
|
171
|
+
* If the `proxy` option is set, or a proxy is configured in the environment for
|
|
172
|
+
* the given url, it creates a {@link Undici.ProxyAgent} dispatcher.
|
|
172
173
|
* If the `pool` option is set to `true`, it creates a {@link Undici.Pool} dispatcher.
|
|
173
174
|
* Otherwise, it creates a basic {@link Undici.Agent} dispatcher.
|
|
174
175
|
*
|
|
176
|
+
* A proxy resolved from the environment takes precedence over `pool`, as a
|
|
177
|
+
* {@link Undici.Pool} is bound to a single origin and can't tunnel. Set
|
|
178
|
+
* `NO_PROXY` to opt a host out, or pass a {@link Undici.Dispatcher} to the
|
|
179
|
+
* request helpers to bypass this entirely.
|
|
180
|
+
*
|
|
175
181
|
* @param url The url to make requests to.
|
|
176
182
|
* @param options The options to configure the dispatcher. See {@link DispatcherOptions}.
|
|
177
183
|
* @returns The configured dispatcher instance.
|
|
178
|
-
* @throws DispatcherError If the dispatcher can't be created
|
|
184
|
+
* @throws DispatcherError If the dispatcher can't be created, including when the
|
|
185
|
+
* proxy configured in the environment isn't a valid url.
|
|
179
186
|
*/
|
|
180
187
|
export async function getDispatcher(url, { timeout, proxy, pool, maxConnections, isTestDispatcher, } = {}) {
|
|
181
188
|
try {
|
|
182
|
-
if (pool
|
|
189
|
+
if (pool === true && proxy !== undefined) {
|
|
183
190
|
throw new Error("The pool and proxy options can't be used at the same time");
|
|
184
191
|
}
|
|
185
192
|
const baseOptions = getBaseDispatcherOptions(timeout, isTestDispatcher);
|
|
186
|
-
|
|
187
|
-
|
|
193
|
+
const resolvedProxy = proxy ?? resolveProxyFromEnv(url);
|
|
194
|
+
if (resolvedProxy !== undefined) {
|
|
195
|
+
return await getProxyDispatcher(resolvedProxy, baseOptions);
|
|
188
196
|
}
|
|
189
197
|
if (pool === true) {
|
|
190
198
|
return await getPoolDispatcher(url, {
|
|
@@ -209,22 +217,17 @@ export async function getTestDispatcher(options = {}) {
|
|
|
209
217
|
/**
|
|
210
218
|
* Determines whether a proxy should be used for a given url.
|
|
211
219
|
*
|
|
220
|
+
* Loopback addresses are never proxied, so that a local node stays reachable
|
|
221
|
+
* when a proxy is configured for everything else. Beyond those, `NO_PROXY` (or
|
|
222
|
+
* `no_proxy`) is the opt-out: `*` disables proxying entirely, and every other
|
|
223
|
+
* entry is matched as described in {@link isExcludedByNoProxy}.
|
|
224
|
+
*
|
|
212
225
|
* @param url The url to check.
|
|
213
226
|
* @returns `true` if a proxy should be used for the url, `false` otherwise.
|
|
214
227
|
*/
|
|
215
228
|
export function shouldUseProxy(url) {
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
if (hostname === "localhost" || hostname === "127.0.0.1" || noProxy === "*") {
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
if (noProxy !== undefined && noProxy !== "") {
|
|
222
|
-
const noProxySet = new Set(noProxy.split(","));
|
|
223
|
-
if (noProxySet.has(hostname)) {
|
|
224
|
-
return false;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return true;
|
|
229
|
+
const parsedUrl = new URL(url);
|
|
230
|
+
return !isLoopbackUrl(parsedUrl) && !isExcludedByNoProxy(parsedUrl);
|
|
228
231
|
}
|
|
229
232
|
/**
|
|
230
233
|
* Determines whether an absolute url is valid.
|
|
@@ -242,29 +245,19 @@ export function isValidUrl(url) {
|
|
|
242
245
|
}
|
|
243
246
|
}
|
|
244
247
|
/**
|
|
245
|
-
* Returns the proxy
|
|
246
|
-
* For HTTPS
|
|
247
|
-
* For HTTP
|
|
248
|
+
* Returns the proxy url from environment variables based on the target url.
|
|
249
|
+
* For HTTPS urls, checks `https_proxy` then `HTTPS_PROXY`.
|
|
250
|
+
* For HTTP urls, checks `http_proxy` then `HTTP_PROXY`.
|
|
248
251
|
* Falls back to the other protocol's proxy if none found.
|
|
249
252
|
*
|
|
250
|
-
*
|
|
251
|
-
*
|
|
253
|
+
* Empty and whitespace-only values are treated as unset, and the returned value
|
|
254
|
+
* is trimmed. It isn't validated: use {@link isValidUrl} if you need that.
|
|
255
|
+
*
|
|
256
|
+
* @param url The target url to determine proxy for.
|
|
257
|
+
* @returns The proxy url, or `undefined` if none are set.
|
|
252
258
|
*/
|
|
253
259
|
export function getProxyUrl(url) {
|
|
254
|
-
|
|
255
|
-
if (protocol === "https:") {
|
|
256
|
-
return (process.env.https_proxy ??
|
|
257
|
-
process.env.HTTPS_PROXY ??
|
|
258
|
-
process.env.http_proxy ??
|
|
259
|
-
process.env.HTTP_PROXY);
|
|
260
|
-
}
|
|
261
|
-
else if (protocol === "http:") {
|
|
262
|
-
return (process.env.http_proxy ??
|
|
263
|
-
process.env.HTTP_PROXY ??
|
|
264
|
-
process.env.https_proxy ??
|
|
265
|
-
process.env.HTTPS_PROXY);
|
|
266
|
-
}
|
|
267
|
-
return undefined;
|
|
260
|
+
return findProxyEnvVar(url)?.value;
|
|
268
261
|
}
|
|
269
|
-
export { ConnectionRefusedError, DispatcherError, DownloadError, RequestError, RequestTimeoutError, ResponseStatusCodeError, } from "./errors/request.js";
|
|
262
|
+
export { ConnectionRefusedError, DispatcherError, DownloadError, InvalidProxyUrlError, RequestError, RequestTimeoutError, ResponseStatusCodeError, } from "./errors/request.js";
|
|
270
263
|
//# sourceMappingURL=request.js.map
|
package/dist/src/request.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,WAAW,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EACL,aAAa,EACb,YAAY,EACZ,eAAe,GAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EACL,oBAAoB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IACpC,IAAI,CAAC;QACH,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,OAAO,eAAe,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC;AACrC,CAAC;AAED,OAAO,EACL,sBAAsB,EACtB,eAAe,EACf,aAAa,EACb,oBAAoB,EACpB,YAAY,EACZ,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,qBAAqB,CAAC"}
|
package/package.json
CHANGED
package/src/errors/request.ts
CHANGED
|
@@ -22,6 +22,18 @@ export class DispatcherError extends CustomError {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
export class InvalidProxyUrlError extends CustomError {
|
|
26
|
+
public readonly envVarName: string;
|
|
27
|
+
|
|
28
|
+
constructor(envVarName: string, cause?: Error) {
|
|
29
|
+
super(
|
|
30
|
+
`The proxy url configured in the ${envVarName} environment variable is not a valid url`,
|
|
31
|
+
cause,
|
|
32
|
+
);
|
|
33
|
+
this.envVarName = envVarName;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
25
37
|
export class RequestTimeoutError extends CustomError {
|
|
26
38
|
constructor(url: string, cause?: Error) {
|
|
27
39
|
super(`Request to ${sanitizeUrl(url)} timed out`, cause);
|
package/src/eth.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { PrefixedHexString } from "./hex.js";
|
|
2
2
|
|
|
3
|
+
import { InvalidParameterError } from "./common-errors.js";
|
|
3
4
|
import { bytesToHexString, numberToHexString, setLengthLeft } from "./hex.js";
|
|
4
5
|
import {
|
|
5
6
|
getAddressGenerator,
|
|
6
7
|
getHashGenerator,
|
|
7
8
|
isValidChecksum,
|
|
9
|
+
toChecksumAddress as toChecksumAddressImpl,
|
|
8
10
|
} from "./internal/eth.js";
|
|
9
11
|
|
|
10
12
|
/**
|
|
@@ -27,6 +29,26 @@ export async function isValidChecksumAddress(value: unknown): Promise<boolean> {
|
|
|
27
29
|
return await (isAddress(value) && isValidChecksum(value));
|
|
28
30
|
}
|
|
29
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Converts an Ethereum address to its EIP-55 checksummed representation.
|
|
34
|
+
*
|
|
35
|
+
* @param address The address to convert. It can be checksummed or not, but it
|
|
36
|
+
* must be a valid Ethereum address.
|
|
37
|
+
* @returns The EIP-55 checksummed representation of the address.
|
|
38
|
+
* @throws InvalidParameterError If the input is not an Ethereum address.
|
|
39
|
+
*/
|
|
40
|
+
export async function toChecksumAddress(
|
|
41
|
+
address: string,
|
|
42
|
+
): Promise<PrefixedHexString> {
|
|
43
|
+
if (!isAddress(address)) {
|
|
44
|
+
throw new InvalidParameterError(
|
|
45
|
+
`Expected an Ethereum address. Received: ${address}`,
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return await toChecksumAddressImpl(address);
|
|
50
|
+
}
|
|
51
|
+
|
|
30
52
|
/**
|
|
31
53
|
* Checks if a value is an Ethereum hash.
|
|
32
54
|
*
|
package/src/internal/eth.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { PrefixedHexString } from "../hex.js";
|
|
2
|
+
|
|
1
3
|
import { utf8StringToBytes } from "../bytes.js";
|
|
2
4
|
import { keccak256 } from "../crypto.js";
|
|
3
5
|
import { bytesToHexString, getUnprefixedHexString } from "../hex.js";
|
|
@@ -42,16 +44,19 @@ export async function getAddressGenerator(): Promise<RandomBytesGenerator> {
|
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
/**
|
|
45
|
-
*
|
|
46
|
-
* This method is
|
|
47
|
+
* Converts an Ethereum address to its EIP-55 checksummed representation.
|
|
48
|
+
* This method is an adaptation of the ethereumjs methods at this link:
|
|
47
49
|
* https://github.com/ethereumjs/ethereumjs-monorepo/blob/47f388bfeec553519d11259fee7e7161a77b29b2/packages/util/src/account.ts#L440-L478
|
|
48
50
|
* The main differences are:
|
|
49
|
-
* - the
|
|
50
|
-
* - tha `eip1191ChainId` parameter has been removed.
|
|
51
|
+
* - the `eip1191ChainId` parameter has been removed.
|
|
51
52
|
* - the code has been modified to use the `hardhat-utils` methods
|
|
52
53
|
*
|
|
54
|
+
* It doesn't validate that the input is an Ethereum address; callers must
|
|
55
|
+
* check that first (see the public wrappers in `../eth.ts`).
|
|
53
56
|
*/
|
|
54
|
-
export async function
|
|
57
|
+
export async function toChecksumAddress(
|
|
58
|
+
hexAddress: PrefixedHexString,
|
|
59
|
+
): Promise<PrefixedHexString> {
|
|
55
60
|
const address = getUnprefixedHexString(hexAddress).toLowerCase();
|
|
56
61
|
|
|
57
62
|
const bytes = utf8StringToBytes(address);
|
|
@@ -67,5 +72,17 @@ export async function isValidChecksum(hexAddress: string): Promise<boolean> {
|
|
|
67
72
|
}
|
|
68
73
|
}
|
|
69
74
|
|
|
70
|
-
return `0x${ret}
|
|
75
|
+
return `0x${ret}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Checks if the checksum of an address is valid.
|
|
80
|
+
*
|
|
81
|
+
* It doesn't validate that the input is an Ethereum address; callers must
|
|
82
|
+
* check that first (see the public wrappers in `../eth.ts`).
|
|
83
|
+
*/
|
|
84
|
+
export async function isValidChecksum(
|
|
85
|
+
hexAddress: PrefixedHexString,
|
|
86
|
+
): Promise<boolean> {
|
|
87
|
+
return (await toChecksumAddress(hexAddress)) === hexAddress;
|
|
71
88
|
}
|
package/src/internal/request.ts
CHANGED
|
@@ -14,14 +14,44 @@ import {
|
|
|
14
14
|
DEFAULT_TIMEOUT_IN_MILLISECONDS,
|
|
15
15
|
DEFAULT_USER_AGENT,
|
|
16
16
|
getDispatcher,
|
|
17
|
+
InvalidProxyUrlError,
|
|
18
|
+
isValidUrl,
|
|
17
19
|
RequestTimeoutError,
|
|
18
20
|
ResponseStatusCodeError,
|
|
21
|
+
shouldUseProxy,
|
|
19
22
|
} from "../request.js";
|
|
20
23
|
|
|
24
|
+
export interface ProxyEnvVar {
|
|
25
|
+
name: string;
|
|
26
|
+
value: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface NoProxyEntry {
|
|
30
|
+
hostname: string;
|
|
31
|
+
port: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
21
34
|
// We don't load undici on startup because this package is transitively imported
|
|
22
35
|
// from too many places and it's too complex to optimize case by case.
|
|
23
36
|
let undici: typeof UndiciT | undefined;
|
|
24
37
|
|
|
38
|
+
const HTTPS_PROXY_ENV_VARS: string[] = ["https_proxy", "HTTPS_PROXY"];
|
|
39
|
+
const HTTP_PROXY_ENV_VARS: string[] = ["http_proxy", "HTTP_PROXY"];
|
|
40
|
+
const NO_PROXY_ENV_VARS: string[] = ["no_proxy", "NO_PROXY"];
|
|
41
|
+
|
|
42
|
+
// `URL` normalizes IPv6 loopback to `[::1]` and alternate IPv4 forms to
|
|
43
|
+
// dotted decimal. Only IPv6 hostnames include brackets.
|
|
44
|
+
const LOOPBACK_HOSTNAMES: Set<string> = new Set([
|
|
45
|
+
"localhost",
|
|
46
|
+
"0.0.0.0",
|
|
47
|
+
"[::1]",
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
const DEFAULT_PORTS: Map<string, number> = new Map([
|
|
51
|
+
["http:", 80],
|
|
52
|
+
["https:", 443],
|
|
53
|
+
]);
|
|
54
|
+
|
|
25
55
|
export async function generateTempFilePath(filePath: string): Promise<string> {
|
|
26
56
|
const { dir, ext, name } = path.parse(filePath);
|
|
27
57
|
|
|
@@ -97,6 +127,151 @@ export function getAuthHeader(requestUrl: string): string | undefined {
|
|
|
97
127
|
).toString("base64")}`;
|
|
98
128
|
}
|
|
99
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Determines whether a url points at the loopback interface.
|
|
132
|
+
*/
|
|
133
|
+
export function isLoopbackUrl(parsedUrl: URL): boolean {
|
|
134
|
+
const hostname = parsedUrl.hostname.toLowerCase();
|
|
135
|
+
|
|
136
|
+
if (LOOPBACK_HOSTNAMES.has(hostname)) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Subdomains of localhost, e.g. `api.localhost`.
|
|
141
|
+
if (hostname.endsWith(".localhost")) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// The whole 127.0.0.0/8 range is loopback, not just 127.0.0.1.
|
|
146
|
+
return /^127(?:\.\d{1,3}){3}$/.test(hostname);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Determines whether `NO_PROXY` (or `no_proxy`) excludes a url from being
|
|
151
|
+
* proxied. A value of `*` excludes everything; the other entries are matched as
|
|
152
|
+
* described in {@link parseNoProxy}.
|
|
153
|
+
*/
|
|
154
|
+
export function isExcludedByNoProxy(parsedUrl: URL): boolean {
|
|
155
|
+
// An empty or whitespace-only value means unset, so it falls through to the
|
|
156
|
+
// other casing instead of shadowing it.
|
|
157
|
+
const noProxy = NO_PROXY_ENV_VARS.map((name) =>
|
|
158
|
+
process.env[name]?.trim(),
|
|
159
|
+
).find((value) => value !== undefined && value !== "");
|
|
160
|
+
|
|
161
|
+
if (noProxy === undefined) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (noProxy === "*") {
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const hostname = normalizeHostname(parsedUrl.hostname);
|
|
170
|
+
const parsedPort = Number.parseInt(parsedUrl.port, 10);
|
|
171
|
+
const port = Number.isNaN(parsedPort)
|
|
172
|
+
? DEFAULT_PORTS.get(parsedUrl.protocol)
|
|
173
|
+
: parsedPort;
|
|
174
|
+
|
|
175
|
+
for (const entry of parseNoProxy(noProxy)) {
|
|
176
|
+
// A portless entry matches every port.
|
|
177
|
+
if (entry.port !== 0 && entry.port !== port) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (
|
|
182
|
+
hostname === entry.hostname ||
|
|
183
|
+
hostname.endsWith(`.${entry.hostname}`)
|
|
184
|
+
) {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Finds the environment variable that configures the proxy for a given url,
|
|
194
|
+
* following the usual precedence: the variable matching the url's protocol
|
|
195
|
+
* first, lowercase before uppercase, then the other protocol's as a fallback.
|
|
196
|
+
*
|
|
197
|
+
* Empty and whitespace-only values are treated as unset, as that's how users
|
|
198
|
+
* disable a variable inherited from a parent environment. The returned value is
|
|
199
|
+
* trimmed, but not validated.
|
|
200
|
+
*
|
|
201
|
+
* @param requestUrl The url the proxy would be used for.
|
|
202
|
+
* @returns The variable's name and value, or `undefined` if none is set or the
|
|
203
|
+
* url's protocol isn't proxyable.
|
|
204
|
+
*/
|
|
205
|
+
export function findProxyEnvVar(requestUrl: string): ProxyEnvVar | undefined {
|
|
206
|
+
const { protocol } = new URL(requestUrl);
|
|
207
|
+
|
|
208
|
+
let names: string[];
|
|
209
|
+
if (protocol === "https:") {
|
|
210
|
+
names = HTTPS_PROXY_ENV_VARS.concat(HTTP_PROXY_ENV_VARS);
|
|
211
|
+
} else if (protocol === "http:") {
|
|
212
|
+
names = HTTP_PROXY_ENV_VARS.concat(HTTPS_PROXY_ENV_VARS);
|
|
213
|
+
} else {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
for (const name of names) {
|
|
218
|
+
const value = process.env[name]?.trim();
|
|
219
|
+
|
|
220
|
+
if (value !== undefined && value !== "") {
|
|
221
|
+
return { name, value };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Resolves the proxy to use for a given url from the environment.
|
|
230
|
+
*
|
|
231
|
+
* @param requestUrl The url the proxy would be used for.
|
|
232
|
+
* @returns The proxy url, or `undefined` if the environment configures none,
|
|
233
|
+
* `NO_PROXY` excludes the url, or the url can't be proxied.
|
|
234
|
+
* @throws InvalidProxyUrlError If the configured proxy isn't a valid http(s) url.
|
|
235
|
+
*/
|
|
236
|
+
export function resolveProxyFromEnv(requestUrl: string): string | undefined {
|
|
237
|
+
// `shouldUseProxy` and `findProxyEnvVar` both parse the url. Requests to an
|
|
238
|
+
// invalid one fail later with a better error than anything we'd raise here.
|
|
239
|
+
if (!isValidUrl(requestUrl) || !shouldUseProxy(requestUrl)) {
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const proxyEnvVar = findProxyEnvVar(requestUrl);
|
|
244
|
+
|
|
245
|
+
if (proxyEnvVar === undefined) {
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!isValidProxyUrl(proxyEnvVar.value)) {
|
|
250
|
+
throw new InvalidProxyUrlError(proxyEnvVar.name);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return proxyEnvVar.value;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Determines whether a value can be used as a proxy url.
|
|
258
|
+
*
|
|
259
|
+
* undici's `ProxyAgent` only speaks http(s). Checking the protocol here, rather
|
|
260
|
+
* than leaving it to undici, catches two cases that would otherwise surface as
|
|
261
|
+
* opaque errors: a scheme-less `host:port`, which parses as a url whose
|
|
262
|
+
* protocol is the host and so never looks like a missing scheme, and `socks5:`,
|
|
263
|
+
* which undici accepts before failing at request time.
|
|
264
|
+
*/
|
|
265
|
+
function isValidProxyUrl(value: string): boolean {
|
|
266
|
+
if (!isValidUrl(value)) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const { protocol } = new URL(value);
|
|
271
|
+
|
|
272
|
+
return protocol === "http:" || protocol === "https:";
|
|
273
|
+
}
|
|
274
|
+
|
|
100
275
|
export async function getProxyDispatcher(
|
|
101
276
|
proxy: string,
|
|
102
277
|
options: Omit<UndiciT.ProxyAgent.Options, "uri">,
|
|
@@ -207,3 +382,46 @@ function describeResponseError(e: Error): void {
|
|
|
207
382
|
reason !== undefined ? `: ${reason}` : ""
|
|
208
383
|
}`;
|
|
209
384
|
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Strips the brackets an IPv6 literal carries in a url, so that an entry can be
|
|
388
|
+
* written either way. `URL` keeps them on `hostname`, while `NO_PROXY` entries
|
|
389
|
+
* are commonly written without them.
|
|
390
|
+
*/
|
|
391
|
+
function normalizeHostname(hostname: string): string {
|
|
392
|
+
return hostname.replace(/^\[|\]$/g, "").toLowerCase();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Parses a `NO_PROXY` value into its entries.
|
|
397
|
+
*
|
|
398
|
+
* Entries are separated by commas or whitespace. A leading `.` or `*.` marks
|
|
399
|
+
* the suffix form (`.example.com`), but a bare `example.com` matches its
|
|
400
|
+
* subdomains too, so both are stored the same way. An entry may carry a port,
|
|
401
|
+
* as in `example.com:8080`, which restricts it to that port. CIDR ranges aren't
|
|
402
|
+
* supported.
|
|
403
|
+
*/
|
|
404
|
+
function parseNoProxy(noProxy: string): NoProxyEntry[] {
|
|
405
|
+
const entries: NoProxyEntry[] = [];
|
|
406
|
+
|
|
407
|
+
for (const rawEntry of noProxy.split(/[,\s]/)) {
|
|
408
|
+
const entry = rawEntry.trim();
|
|
409
|
+
|
|
410
|
+
if (entry === "") {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Only a bracketed literal or a colon-free host can carry a port, so that
|
|
415
|
+
// the trailing group of a bare IPv6 entry isn't read as one.
|
|
416
|
+
const withPort = entry.match(/^(\[[^\]]+\]|[^:]+):(\d+)$/);
|
|
417
|
+
|
|
418
|
+
entries.push({
|
|
419
|
+
hostname: normalizeHostname(
|
|
420
|
+
(withPort !== null ? withPort[1] : entry).replace(/^\*?\./, ""),
|
|
421
|
+
),
|
|
422
|
+
port: withPort !== null ? Number.parseInt(withPort[2], 10) : 0,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return entries;
|
|
427
|
+
}
|
package/src/request.ts
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
} from "./errors/request.js";
|
|
16
16
|
import { move, remove } from "./fs.js";
|
|
17
17
|
import {
|
|
18
|
+
findProxyEnvVar,
|
|
18
19
|
generateTempFilePath,
|
|
19
20
|
getBaseDispatcherOptions,
|
|
20
21
|
getBaseRequestOptions,
|
|
@@ -22,6 +23,9 @@ import {
|
|
|
22
23
|
getPoolDispatcher,
|
|
23
24
|
getProxyDispatcher,
|
|
24
25
|
handleError,
|
|
26
|
+
isExcludedByNoProxy,
|
|
27
|
+
isLoopbackUrl,
|
|
28
|
+
resolveProxyFromEnv,
|
|
25
29
|
} from "./internal/request.js";
|
|
26
30
|
|
|
27
31
|
export const DEFAULT_TIMEOUT_IN_MILLISECONDS = 300_000; // Aligned with undici
|
|
@@ -41,7 +45,9 @@ let undici: typeof Undici | undefined;
|
|
|
41
45
|
* Options to configure the dispatcher.
|
|
42
46
|
*
|
|
43
47
|
* @param timeout The timeout in milliseconds. Defaults to {@link DEFAULT_TIMEOUT_IN_MILLISECONDS}.
|
|
44
|
-
* @param proxy The proxy to use. If not provided,
|
|
48
|
+
* @param proxy The proxy to use. If not provided, it's resolved from the
|
|
49
|
+
* `https_proxy`/`HTTPS_PROXY` and `http_proxy`/`HTTP_PROXY` environment
|
|
50
|
+
* variables, unless `NO_PROXY` excludes the url or it points at loopback.
|
|
45
51
|
* @param pool Whether to use a pool dispatcher. Defaults to `false`.
|
|
46
52
|
* @param maxConnections The maximum number of connections to use in the pool. Defaults to {@link DEFAULT_POOL_MAX_CONNECTIONS}.
|
|
47
53
|
* @param isTestDispatcher Whether to use a test dispatcher. Defaults to `false`. It's highly recommended to use a test dispatcher in tests to avoid hanging tests.
|
|
@@ -284,14 +290,21 @@ export async function download(
|
|
|
284
290
|
|
|
285
291
|
/**
|
|
286
292
|
* Creates a dispatcher based on the provided options.
|
|
287
|
-
* If the `proxy` option is set,
|
|
293
|
+
* If the `proxy` option is set, or a proxy is configured in the environment for
|
|
294
|
+
* the given url, it creates a {@link Undici.ProxyAgent} dispatcher.
|
|
288
295
|
* If the `pool` option is set to `true`, it creates a {@link Undici.Pool} dispatcher.
|
|
289
296
|
* Otherwise, it creates a basic {@link Undici.Agent} dispatcher.
|
|
290
297
|
*
|
|
298
|
+
* A proxy resolved from the environment takes precedence over `pool`, as a
|
|
299
|
+
* {@link Undici.Pool} is bound to a single origin and can't tunnel. Set
|
|
300
|
+
* `NO_PROXY` to opt a host out, or pass a {@link Undici.Dispatcher} to the
|
|
301
|
+
* request helpers to bypass this entirely.
|
|
302
|
+
*
|
|
291
303
|
* @param url The url to make requests to.
|
|
292
304
|
* @param options The options to configure the dispatcher. See {@link DispatcherOptions}.
|
|
293
305
|
* @returns The configured dispatcher instance.
|
|
294
|
-
* @throws DispatcherError If the dispatcher can't be created
|
|
306
|
+
* @throws DispatcherError If the dispatcher can't be created, including when the
|
|
307
|
+
* proxy configured in the environment isn't a valid url.
|
|
295
308
|
*/
|
|
296
309
|
export async function getDispatcher(
|
|
297
310
|
url: string,
|
|
@@ -304,15 +317,17 @@ export async function getDispatcher(
|
|
|
304
317
|
}: DispatcherOptions = {},
|
|
305
318
|
): Promise<Dispatcher> {
|
|
306
319
|
try {
|
|
307
|
-
if (pool
|
|
320
|
+
if (pool === true && proxy !== undefined) {
|
|
308
321
|
throw new Error(
|
|
309
322
|
"The pool and proxy options can't be used at the same time",
|
|
310
323
|
);
|
|
311
324
|
}
|
|
312
325
|
const baseOptions = getBaseDispatcherOptions(timeout, isTestDispatcher);
|
|
313
326
|
|
|
314
|
-
|
|
315
|
-
|
|
327
|
+
const resolvedProxy = proxy ?? resolveProxyFromEnv(url);
|
|
328
|
+
|
|
329
|
+
if (resolvedProxy !== undefined) {
|
|
330
|
+
return await getProxyDispatcher(resolvedProxy, baseOptions);
|
|
316
331
|
}
|
|
317
332
|
|
|
318
333
|
if (pool === true) {
|
|
@@ -345,26 +360,18 @@ export async function getTestDispatcher(
|
|
|
345
360
|
/**
|
|
346
361
|
* Determines whether a proxy should be used for a given url.
|
|
347
362
|
*
|
|
363
|
+
* Loopback addresses are never proxied, so that a local node stays reachable
|
|
364
|
+
* when a proxy is configured for everything else. Beyond those, `NO_PROXY` (or
|
|
365
|
+
* `no_proxy`) is the opt-out: `*` disables proxying entirely, and every other
|
|
366
|
+
* entry is matched as described in {@link isExcludedByNoProxy}.
|
|
367
|
+
*
|
|
348
368
|
* @param url The url to check.
|
|
349
369
|
* @returns `true` if a proxy should be used for the url, `false` otherwise.
|
|
350
370
|
*/
|
|
351
371
|
export function shouldUseProxy(url: string): boolean {
|
|
352
|
-
const
|
|
353
|
-
const noProxy = process.env.NO_PROXY;
|
|
354
|
-
|
|
355
|
-
if (hostname === "localhost" || hostname === "127.0.0.1" || noProxy === "*") {
|
|
356
|
-
return false;
|
|
357
|
-
}
|
|
372
|
+
const parsedUrl = new URL(url);
|
|
358
373
|
|
|
359
|
-
|
|
360
|
-
const noProxySet = new Set(noProxy.split(","));
|
|
361
|
-
|
|
362
|
-
if (noProxySet.has(hostname)) {
|
|
363
|
-
return false;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
return true;
|
|
374
|
+
return !isLoopbackUrl(parsedUrl) && !isExcludedByNoProxy(parsedUrl);
|
|
368
375
|
}
|
|
369
376
|
|
|
370
377
|
/**
|
|
@@ -383,40 +390,26 @@ export function isValidUrl(url: string): boolean {
|
|
|
383
390
|
}
|
|
384
391
|
|
|
385
392
|
/**
|
|
386
|
-
* Returns the proxy
|
|
387
|
-
* For HTTPS
|
|
388
|
-
* For HTTP
|
|
393
|
+
* Returns the proxy url from environment variables based on the target url.
|
|
394
|
+
* For HTTPS urls, checks `https_proxy` then `HTTPS_PROXY`.
|
|
395
|
+
* For HTTP urls, checks `http_proxy` then `HTTP_PROXY`.
|
|
389
396
|
* Falls back to the other protocol's proxy if none found.
|
|
390
397
|
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
398
|
+
* Empty and whitespace-only values are treated as unset, and the returned value
|
|
399
|
+
* is trimmed. It isn't validated: use {@link isValidUrl} if you need that.
|
|
400
|
+
*
|
|
401
|
+
* @param url The target url to determine proxy for.
|
|
402
|
+
* @returns The proxy url, or `undefined` if none are set.
|
|
393
403
|
*/
|
|
394
404
|
export function getProxyUrl(url: string): string | undefined {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
if (protocol === "https:") {
|
|
398
|
-
return (
|
|
399
|
-
process.env.https_proxy ??
|
|
400
|
-
process.env.HTTPS_PROXY ??
|
|
401
|
-
process.env.http_proxy ??
|
|
402
|
-
process.env.HTTP_PROXY
|
|
403
|
-
);
|
|
404
|
-
} else if (protocol === "http:") {
|
|
405
|
-
return (
|
|
406
|
-
process.env.http_proxy ??
|
|
407
|
-
process.env.HTTP_PROXY ??
|
|
408
|
-
process.env.https_proxy ??
|
|
409
|
-
process.env.HTTPS_PROXY
|
|
410
|
-
);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
return undefined;
|
|
405
|
+
return findProxyEnvVar(url)?.value;
|
|
414
406
|
}
|
|
415
407
|
|
|
416
408
|
export {
|
|
417
409
|
ConnectionRefusedError,
|
|
418
410
|
DispatcherError,
|
|
419
411
|
DownloadError,
|
|
412
|
+
InvalidProxyUrlError,
|
|
420
413
|
RequestError,
|
|
421
414
|
RequestTimeoutError,
|
|
422
415
|
ResponseStatusCodeError,
|