global-agent 2.2.0 → 4.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.
Files changed (95) hide show
  1. package/.babelrc +23 -0
  2. package/.editorconfig +9 -0
  3. package/.eslintignore +1 -0
  4. package/.eslintrc +27 -0
  5. package/.github/FUNDING.yml +2 -0
  6. package/.github/workflows/feature.yaml +32 -0
  7. package/.github/workflows/main.yaml +31 -0
  8. package/.gitignore +14 -0
  9. package/LICENSE +2 -2
  10. package/README.md +67 -8
  11. package/package.json +48 -57
  12. package/src/{Logger.js → Logger.ts} +3 -3
  13. package/{dist/classes/Agent.js.flow → src/classes/Agent.ts} +121 -39
  14. package/src/classes/HttpProxyAgent.ts +45 -0
  15. package/src/classes/HttpsProxyAgent.ts +83 -0
  16. package/src/classes/index.ts +9 -0
  17. package/src/{errors.js → errors.ts} +2 -6
  18. package/src/factories/{createGlobalProxyAgent.js → createGlobalProxyAgent.ts} +36 -50
  19. package/{dist/factories/createProxyController.js.flow → src/factories/createProxyController.ts} +8 -5
  20. package/src/factories/index.ts +6 -0
  21. package/src/index.ts +6 -0
  22. package/{dist/routines/bootstrap.js.flow → src/routines/bootstrap.ts} +6 -5
  23. package/src/routines/index.ts +3 -0
  24. package/src/types.ts +66 -0
  25. package/src/utilities/{bindHttpMethod.js → bindHttpMethod.ts} +6 -7
  26. package/src/utilities/index.ts +9 -0
  27. package/{dist/utilities/isUrlMatchingNoProxy.js.flow → src/utilities/isUrlMatchingNoProxy.ts} +1 -6
  28. package/src/utilities/parseBoolean.ts +17 -0
  29. package/src/utilities/{parseProxyUrl.js → parseProxyUrl.ts} +12 -9
  30. package/test/.eslintrc +10 -0
  31. package/test/global-agent/factories/createGlobalProxyAgent.ts +760 -0
  32. package/test/global-agent/factories/createProxyController.ts +37 -0
  33. package/test/global-agent/utilities/isUrlMatchingNoProxy.ts +62 -0
  34. package/test/global-agent/utilities/parseProxyUrl.ts +38 -0
  35. package/tsconfig.json +25 -0
  36. package/.flowconfig +0 -3
  37. package/dist/Logger.js +0 -18
  38. package/dist/Logger.js.flow +0 -10
  39. package/dist/Logger.js.map +0 -1
  40. package/dist/classes/Agent.js +0 -174
  41. package/dist/classes/Agent.js.map +0 -1
  42. package/dist/classes/HttpProxyAgent.js +0 -33
  43. package/dist/classes/HttpProxyAgent.js.flow +0 -30
  44. package/dist/classes/HttpProxyAgent.js.map +0 -1
  45. package/dist/classes/HttpsProxyAgent.js +0 -53
  46. package/dist/classes/HttpsProxyAgent.js.flow +0 -54
  47. package/dist/classes/HttpsProxyAgent.js.map +0 -1
  48. package/dist/classes/index.js +0 -32
  49. package/dist/classes/index.js.flow +0 -5
  50. package/dist/classes/index.js.map +0 -1
  51. package/dist/errors.js +0 -22
  52. package/dist/errors.js.flow +0 -15
  53. package/dist/errors.js.map +0 -1
  54. package/dist/factories/createGlobalProxyAgent.js +0 -175
  55. package/dist/factories/createGlobalProxyAgent.js.flow +0 -197
  56. package/dist/factories/createGlobalProxyAgent.js.map +0 -1
  57. package/dist/factories/createProxyController.js +0 -45
  58. package/dist/factories/createProxyController.js.map +0 -1
  59. package/dist/factories/index.js +0 -24
  60. package/dist/factories/index.js.flow +0 -4
  61. package/dist/factories/index.js.map +0 -1
  62. package/dist/index.js +0 -22
  63. package/dist/index.js.flow +0 -4
  64. package/dist/index.js.map +0 -1
  65. package/dist/routines/bootstrap.js +0 -30
  66. package/dist/routines/bootstrap.js.map +0 -1
  67. package/dist/routines/index.js +0 -16
  68. package/dist/routines/index.js.flow +0 -3
  69. package/dist/routines/index.js.map +0 -1
  70. package/dist/types.js +0 -10
  71. package/dist/types.js.flow +0 -66
  72. package/dist/types.js.map +0 -1
  73. package/dist/utilities/bindHttpMethod.js +0 -62
  74. package/dist/utilities/bindHttpMethod.js.flow +0 -54
  75. package/dist/utilities/bindHttpMethod.js.map +0 -1
  76. package/dist/utilities/index.js +0 -32
  77. package/dist/utilities/index.js.flow +0 -5
  78. package/dist/utilities/index.js.map +0 -1
  79. package/dist/utilities/isUrlMatchingNoProxy.js +0 -43
  80. package/dist/utilities/isUrlMatchingNoProxy.js.map +0 -1
  81. package/dist/utilities/parseProxyUrl.js +0 -42
  82. package/dist/utilities/parseProxyUrl.js.flow +0 -36
  83. package/dist/utilities/parseProxyUrl.js.map +0 -1
  84. package/src/classes/Agent.js +0 -212
  85. package/src/classes/HttpProxyAgent.js +0 -30
  86. package/src/classes/HttpsProxyAgent.js +0 -54
  87. package/src/classes/index.js +0 -5
  88. package/src/factories/createProxyController.js +0 -46
  89. package/src/factories/index.js +0 -4
  90. package/src/index.js +0 -4
  91. package/src/routines/bootstrap.js +0 -25
  92. package/src/routines/index.js +0 -3
  93. package/src/types.js +0 -66
  94. package/src/utilities/index.js +0 -5
  95. package/src/utilities/isUrlMatchingNoProxy.js +0 -37
@@ -1,5 +1,4 @@
1
- // @flow
2
-
1
+ import createGlobalThis from 'globalthis';
3
2
  import Logger from '../Logger';
4
3
  import {
5
4
  createGlobalProxyAgent,
@@ -8,18 +7,20 @@ import type {
8
7
  ProxyAgentConfigurationInputType,
9
8
  } from '../types';
10
9
 
10
+ const globalThis: any = createGlobalThis();
11
+
11
12
  const log = Logger.child({
12
13
  namespace: 'bootstrap',
13
14
  });
14
15
 
15
16
  export default (configurationInput?: ProxyAgentConfigurationInputType): boolean => {
16
- if (global.GLOBAL_AGENT) {
17
- log.warn('found global.GLOBAL_AGENT; second attempt to bootstrap global-agent was ignored');
17
+ if (globalThis.GLOBAL_AGENT) {
18
+ log.warn('found globalThis.GLOBAL_AGENT; second attempt to bootstrap global-agent was ignored');
18
19
 
19
20
  return false;
20
21
  }
21
22
 
22
- global.GLOBAL_AGENT = createGlobalProxyAgent(configurationInput);
23
+ globalThis.GLOBAL_AGENT = createGlobalProxyAgent(configurationInput);
23
24
 
24
25
  return true;
25
26
  };
@@ -0,0 +1,3 @@
1
+ export {
2
+ default as bootstrap,
3
+ } from './bootstrap';
package/src/types.ts ADDED
@@ -0,0 +1,66 @@
1
+ import type {
2
+ Agent as HttpAgent,
3
+ } from 'http';
4
+ import type {
5
+ Agent as HttpsAgent,
6
+ } from 'https';
7
+ import type {
8
+ Socket,
9
+ } from 'net';
10
+ import type {
11
+ TLSSocket,
12
+ } from 'tls';
13
+
14
+ export type ProxyConfigurationType = {
15
+ authorization: string | null,
16
+ hostname: string,
17
+ port: number,
18
+ };
19
+
20
+ export type TlsConfigurationType = {
21
+ ca?: string[] | string,
22
+ cert?: string,
23
+ ciphers?: string,
24
+ clientCertEngine?: string,
25
+ crl?: string,
26
+ dhparam?: string,
27
+ ecdhCurve?: string,
28
+ honorCipherOrder?: boolean,
29
+ key?: string,
30
+ passphrase?: string,
31
+ pfx?: string,
32
+ rejectUnauthorized?: boolean,
33
+ secureOptions?: number,
34
+ secureProtocol?: string,
35
+ servername?: string,
36
+ sessionIdContext?: string,
37
+ };
38
+
39
+ export type ConnectionConfigurationType = {
40
+ host: string,
41
+ port: number,
42
+ tls?: TlsConfigurationType,
43
+ proxy: ProxyConfigurationType,
44
+ };
45
+
46
+ export type ConnectionCallbackType = (error: Error | null, socket?: Socket | TLSSocket) => void;
47
+
48
+ export type AgentType = HttpAgent | HttpsAgent;
49
+ export type IsProxyConfiguredMethodType = () => boolean;
50
+ export type MustUrlUseProxyMethodType = (url: string) => boolean;
51
+ export type GetUrlProxyMethodType = (url: string) => ProxyConfigurationType;
52
+ export type ProtocolType = 'http:' | 'https:';
53
+
54
+ export type ProxyAgentConfigurationInputType = {
55
+ environmentVariableNamespace?: string,
56
+ forceGlobalAgent?: boolean,
57
+ socketConnectionTimeout?: number,
58
+ ca?: string[] | string,
59
+ };
60
+
61
+ export type ProxyAgentConfigurationType = {
62
+ environmentVariableNamespace: string,
63
+ forceGlobalAgent: boolean,
64
+ socketConnectionTimeout: number,
65
+ ca?: string[] | string,
66
+ };
@@ -1,14 +1,14 @@
1
- // @flow
2
-
3
1
  import http from 'http';
4
2
  import https from 'https';
5
3
 
6
4
  type AgentType = http.Agent | https.Agent;
7
5
 
8
- // eslint-disable-next-line flowtype/no-weak-types
9
- export default (originalMethod: Function, agent: AgentType, forceGlobalAgent: boolean) => {
10
- // eslint-disable-next-line unicorn/prevent-abbreviations
11
- return (...args: *) => {
6
+ export default (
7
+ originalMethod: Function,
8
+ agent: AgentType,
9
+ forceGlobalAgent: boolean,
10
+ ) => {
11
+ return (...args: any[]) => {
12
12
  let url;
13
13
  let options;
14
14
  let callback;
@@ -45,7 +45,6 @@ export default (originalMethod: Function, agent: AgentType, forceGlobalAgent: bo
45
45
  }
46
46
 
47
47
  if (url) {
48
- // $FlowFixMe
49
48
  return originalMethod(url, options, callback);
50
49
  } else {
51
50
  return originalMethod(options, callback);
@@ -0,0 +1,9 @@
1
+ export {
2
+ default as bindHttpMethod,
3
+ } from './bindHttpMethod';
4
+ export {
5
+ default as isUrlMatchingNoProxy,
6
+ } from './isUrlMatchingNoProxy';
7
+ export {
8
+ default as parseProxyUrl,
9
+ } from './parseProxyUrl';
@@ -1,15 +1,10 @@
1
- // @flow
2
-
3
- import {
4
- parse as parseUrl,
5
- } from 'url';
6
1
  import matcher from 'matcher';
7
2
  import {
8
3
  UnexpectedStateError,
9
4
  } from '../errors';
10
5
 
11
6
  export default (subjectUrl: string, noProxy: string) => {
12
- const subjectUrlTokens = parseUrl(subjectUrl);
7
+ const subjectUrlTokens = new URL(subjectUrl);
13
8
 
14
9
  const rules = noProxy.split(/[\s,]+/);
15
10
 
@@ -0,0 +1,17 @@
1
+ export const parseBoolean = function (value: any): boolean {
2
+ switch (Object.prototype.toString.call(value)) {
3
+ case '[object String]':
4
+ return ['true', 't', 'yes', 'y', 'on', '1'].includes(
5
+ value.trim().toLowerCase(),
6
+ );
7
+
8
+ case '[object Number]':
9
+ return value.valueOf() === 1;
10
+
11
+ case '[object Boolean]':
12
+ return value.valueOf();
13
+
14
+ default:
15
+ return false;
16
+ }
17
+ };
@@ -1,20 +1,15 @@
1
- // @flow
2
-
3
- import {
4
- parse as parseUrl,
5
- } from 'url';
6
1
  import {
7
2
  UnexpectedStateError,
8
3
  } from '../errors';
9
4
 
10
5
  export default (url: string) => {
11
- const urlTokens = parseUrl(url);
6
+ const urlTokens = new URL(url);
12
7
 
13
- if (urlTokens.query !== null) {
8
+ if (urlTokens.search !== '') {
14
9
  throw new UnexpectedStateError('Unsupported `GLOBAL_AGENT.HTTP_PROXY` configuration value: URL must not have query.');
15
10
  }
16
11
 
17
- if (urlTokens.hash !== null) {
12
+ if (urlTokens.hash !== '') {
18
13
  throw new UnexpectedStateError('Unsupported `GLOBAL_AGENT.HTTP_PROXY` configuration value: URL must not have hash.');
19
14
  }
20
15
 
@@ -28,8 +23,16 @@ export default (url: string) => {
28
23
  port = Number.parseInt(urlTokens.port, 10);
29
24
  }
30
25
 
26
+ let authorization = null;
27
+
28
+ if (urlTokens.username && urlTokens.password) {
29
+ authorization = urlTokens.username + ':' + urlTokens.password;
30
+ } else if (urlTokens.username) {
31
+ authorization = urlTokens.username;
32
+ }
33
+
31
34
  return {
32
- authorization: urlTokens.auth || null,
35
+ authorization,
33
36
  hostname: urlTokens.hostname,
34
37
  port,
35
38
  };
package/test/.eslintrc ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "rules": {
3
+ "ava/use-test": 0,
4
+ "filenames/match-regex": 0,
5
+ "flowtype/no-flow-fix-me-comments": 0,
6
+ "flowtype/no-weak-types": 0,
7
+ "id-length": 0,
8
+ "node/no-unpublished-import": 0
9
+ }
10
+ }