global-agent 4.0.0 → 4.1.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.
@@ -1,7 +1,10 @@
1
1
  jobs:
2
2
  test:
3
- name: Test
3
+ name: Test (Node ${{ matrix.node-version }})
4
4
  runs-on: ubuntu-latest
5
+ strategy:
6
+ matrix:
7
+ node-version: ['20', '22', '24']
5
8
  steps:
6
9
  - name: setup repository
7
10
  uses: actions/checkout@v3
@@ -10,7 +13,7 @@ jobs:
10
13
  - name: setup node.js
11
14
  uses: actions/setup-node@v4
12
15
  with:
13
- node-version: '24'
16
+ node-version: ${{ matrix.node-version }}
14
17
  - name: setup npm
15
18
  uses: npm/action-setup@v4
16
19
  with:
@@ -1,5 +1,26 @@
1
1
  jobs:
2
+ test:
3
+ name: Test (Node ${{ matrix.node-version }})
4
+ runs-on: ubuntu-latest
5
+ strategy:
6
+ matrix:
7
+ node-version: ['20', '22', '24']
8
+ steps:
9
+ - name: setup repository
10
+ uses: actions/checkout@v3
11
+ with:
12
+ fetch-depth: 0
13
+ - name: setup node.js
14
+ uses: actions/setup-node@v4
15
+ with:
16
+ node-version: ${{ matrix.node-version }}
17
+ - run: npm install
18
+ - run: npm run lint
19
+ - run: npm run test
20
+ - run: npm run build
21
+ timeout-minutes: 10
2
22
  release:
23
+ needs: test
3
24
  environment: release
4
25
  name: Release
5
26
  permissions:
@@ -18,8 +39,6 @@ jobs:
18
39
  with:
19
40
  node-version: '24'
20
41
  - run: npm install
21
- - run: npm run lint
22
- - run: npm run test
23
42
  - run: npm run build
24
43
  - env:
25
44
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
package/README.md CHANGED
@@ -12,7 +12,6 @@ Global HTTP/HTTPS proxy configurable using environment variables.
12
12
  * [Setup proxy using `bootstrap` routine](#setup-proxy-using-bootstrap-routine)
13
13
  * [Runtime configuration](#runtime-configuration)
14
14
  * [Exclude URLs](#exclude-urls)
15
- * [Enable logging](#enable-logging)
16
15
  * [API](#api)
17
16
  * [`createGlobalProxyAgent`](#createglobalproxyagent)
18
17
  * [Environment variables](#environment-variables)
@@ -142,20 +141,6 @@ says to contact all machines with the 'foo.com' TLD and 'baz.com' domains direct
142
141
 
143
142
  The environment variable `GLOBAL_AGENT_HTTPS_PROXY` can be set to specify a separate proxy for HTTPS requests. When this variable is not set `GLOBAL_AGENT_HTTP_PROXY` is used for both HTTP and HTTPS requests.
144
143
 
145
- ### Enable logging
146
-
147
- `global-agent` is using [`roarr`](https://www.npmjs.com/package/roarr) logger to log HTTP requests and response (HTTP status code and headers), e.g.
148
-
149
- ```json
150
- {"context":{"program":"global-agent","namespace":"Agent","logLevel":10,"destination":"http://gajus.com","proxy":"http://127.0.0.1:8076"},"message":"proxying request","sequence":1,"time":1556269669663,"version":"1.0.0"}
151
- {"context":{"program":"global-agent","namespace":"Agent","logLevel":10,"headers":{"content-type":"text/plain","content-length":"2","date":"Fri, 26 Apr 2019 12:07:50 GMT","connection":"close"},"requestId":6,"statusCode":200},"message":"proxying response","sequence":2,"time":1557133856955,"version":"1.0.0"}
152
-
153
- ```
154
-
155
- Export `ROARR_LOG=true` environment variable to enable log printing to stdout.
156
-
157
- Use [`roarr-cli`](https://github.com/gajus/roarr-cli) program to pretty-print the logs.
158
-
159
144
  ## API
160
145
 
161
146
  ### `createGlobalProxyAgent`
@@ -166,18 +151,52 @@ Use [`roarr-cli`](https://github.com/gajus/roarr-cli) program to pretty-print th
166
151
  * @property forceGlobalAgent Forces to use `global-agent` HTTP(S) agent even when request was explicitly constructed with another agent. (Default: `true`)
167
152
  * @property socketConnectionTimeout Destroys socket if connection is not established within the timeout. (Default: `60000`)
168
153
  * @property ca Single CA certificate or an array of CA certificates that is trusted for secure connections to the registry.
154
+ * @property logger Custom logger instance for debug logging. Must implement `child`, `debug`, `error`, `info`, `trace`, and `warn` methods.
169
155
  */
170
156
  type ProxyAgentConfigurationInputType = {|
171
157
  +environmentVariableNamespace?: string,
172
158
  +forceGlobalAgent?: boolean,
173
159
  +socketConnectionTimeout?: number,
174
160
  +ca?: string[] | string,
161
+ +logger?: Logger,
175
162
  |};
176
163
 
177
164
  (configurationInput: ProxyAgentConfigurationInputType) => ProxyAgentConfigurationType;
178
165
 
179
166
  ```
180
167
 
168
+ ### Custom Logger
169
+
170
+ You can provide a custom logger to `global-agent` for debugging purposes. The logger must implement the following interface:
171
+
172
+ ```ts
173
+ type Logger = {
174
+ child: (context: object) => Logger,
175
+ debug: (context: object | string, message?: string) => void,
176
+ error: (context: object | string, message?: string) => void,
177
+ info: (context: object | string, message?: string) => void,
178
+ trace: (context: object | string, message?: string) => void,
179
+ warn: (context: object | string, message?: string) => void,
180
+ };
181
+ ```
182
+
183
+ Example using a custom logger:
184
+
185
+ ```js
186
+ import { createGlobalProxyAgent } from 'global-agent';
187
+
188
+ createGlobalProxyAgent({
189
+ logger: {
190
+ child: () => logger,
191
+ debug: console.debug,
192
+ error: console.error,
193
+ info: console.info,
194
+ trace: console.trace,
195
+ warn: console.warn,
196
+ },
197
+ });
198
+ ```
199
+
181
200
  ### Environment variables
182
201
 
183
202
  |Name|Description|Default|
package/package.json CHANGED
@@ -20,7 +20,6 @@
20
20
  "globalthis": "^1.0.2",
21
21
  "matcher": "^4.0.0",
22
22
  "omit-undefined": "^1.0.1",
23
- "roarr": "^7.0.3",
24
23
  "semver": "^7.3.5",
25
24
  "serialize-error": "^8.1.0"
26
25
  },
@@ -93,5 +92,5 @@
93
92
  "create-readme": "gitdown ./.README/README.md --output-file ./README.md"
94
93
  },
95
94
  "typings": "./dist/src/index.d.ts",
96
- "version": "4.0.0"
95
+ "version": "4.1.0"
97
96
  }
package/src/Logger.ts CHANGED
@@ -1,10 +1,70 @@
1
- import {
2
- Roarr,
3
- } from 'roarr';
1
+ export type LogMethod = (context: object | string, message?: string) => void;
4
2
 
5
- const Logger = Roarr
6
- .child({
7
- package: 'global-agent',
8
- });
3
+ export type Logger = {
4
+ child: (context: object) => Logger,
5
+ debug: LogMethod,
6
+ error: LogMethod,
7
+ info: LogMethod,
8
+ trace: LogMethod,
9
+ warn: LogMethod,
10
+ };
9
11
 
10
- export default Logger;
12
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
13
+ const noop = () => {};
14
+
15
+ const createNoopLogger = (): Logger => {
16
+ return {
17
+ child: () => {
18
+ return createNoopLogger();
19
+ },
20
+ debug: noop,
21
+ error: noop,
22
+ info: noop,
23
+ trace: noop,
24
+ warn: noop,
25
+ };
26
+ };
27
+
28
+ let currentLogger: Logger = createNoopLogger();
29
+
30
+ export const setLogger = (newLogger: Logger): void => {
31
+ currentLogger = newLogger;
32
+ };
33
+
34
+ const createDelegatingLogger = (getContext: () => object): Logger => {
35
+ const getLogger = () => {
36
+ let targetLogger = currentLogger;
37
+ for (const [key, value] of Object.entries(getContext())) {
38
+ targetLogger = targetLogger.child({[key]: value});
39
+ }
40
+
41
+ return targetLogger;
42
+ };
43
+
44
+ return {
45
+ child: (context: object) => {
46
+ return createDelegatingLogger(() => {
47
+ return {...getContext(), ...context};
48
+ });
49
+ },
50
+ debug: (context, message) => {
51
+ getLogger().debug(context, message);
52
+ },
53
+ error: (context, message) => {
54
+ getLogger().error(context, message);
55
+ },
56
+ info: (context, message) => {
57
+ getLogger().info(context, message);
58
+ },
59
+ trace: (context, message) => {
60
+ getLogger().trace(context, message);
61
+ },
62
+ warn: (context, message) => {
63
+ getLogger().warn(context, message);
64
+ },
65
+ };
66
+ };
67
+
68
+ export const logger = createDelegatingLogger(() => {
69
+ return {package: 'global-agent'};
70
+ });
@@ -4,7 +4,9 @@ import net from 'net';
4
4
  import {
5
5
  serializeError,
6
6
  } from 'serialize-error';
7
- import Logger from '../Logger';
7
+ import {
8
+ logger,
9
+ } from '../Logger';
8
10
  import type {
9
11
  AgentType,
10
12
  ConnectionCallbackType,
@@ -15,7 +17,7 @@ import type {
15
17
  ProtocolType,
16
18
  } from '../types';
17
19
 
18
- const log = Logger.child({
20
+ const log = logger.child({
19
21
  namespace: 'Agent',
20
22
  });
21
23
 
@@ -4,7 +4,10 @@ import {
4
4
  omitUndefined,
5
5
  } from 'omit-undefined';
6
6
  import semverGte from 'semver/functions/gte';
7
- import Logger from '../Logger';
7
+ import {
8
+ logger,
9
+ setLogger,
10
+ } from '../Logger';
8
11
  import {
9
12
  HttpProxyAgent,
10
13
  HttpsProxyAgent,
@@ -31,7 +34,7 @@ const httpRequest = http.request;
31
34
  const httpsGet = https.get;
32
35
  const httpsRequest = https.request;
33
36
 
34
- const log = Logger.child({
37
+ const log = logger.child({
35
38
  namespace: 'createGlobalProxyAgent',
36
39
  });
37
40
 
@@ -60,6 +63,10 @@ const createConfiguration = (configurationInput: ProxyAgentConfigurationInputTyp
60
63
  export default (configurationInput: ProxyAgentConfigurationInputType = defaultConfigurationInput) => {
61
64
  const configuration = createConfiguration(configurationInput);
62
65
 
66
+ if (configurationInput.logger) {
67
+ setLogger(configurationInput.logger);
68
+ }
69
+
63
70
  const proxyController = createProxyController();
64
71
 
65
72
  // eslint-disable-next-line node/no-process-env
@@ -1,4 +1,6 @@
1
- import Logger from '../Logger';
1
+ import {
2
+ logger,
3
+ } from '../Logger';
2
4
 
3
5
  type ProxyController = {
4
6
  HTTP_PROXY: string | null,
@@ -6,7 +8,7 @@ type ProxyController = {
6
8
  NO_PROXY: string | null,
7
9
  };
8
10
 
9
- const log = Logger.child({
11
+ const log = logger.child({
10
12
  namespace: 'createProxyController',
11
13
  });
12
14
 
package/src/index.ts CHANGED
@@ -4,3 +4,6 @@ export {
4
4
  export {
5
5
  createGlobalProxyAgent,
6
6
  } from './factories';
7
+ export type {
8
+ Logger,
9
+ } from './Logger';
@@ -1,5 +1,7 @@
1
1
  import createGlobalThis from 'globalthis';
2
- import Logger from '../Logger';
2
+ import {
3
+ logger,
4
+ } from '../Logger';
3
5
  import {
4
6
  createGlobalProxyAgent,
5
7
  } from '../factories';
@@ -9,7 +11,7 @@ import type {
9
11
 
10
12
  const globalThis: any = createGlobalThis();
11
13
 
12
- const log = Logger.child({
14
+ const log = logger.child({
13
15
  namespace: 'bootstrap',
14
16
  });
15
17
 
package/src/types.ts CHANGED
@@ -10,6 +10,9 @@ import type {
10
10
  import type {
11
11
  TLSSocket,
12
12
  } from 'tls';
13
+ import type {
14
+ Logger,
15
+ } from './Logger';
13
16
 
14
17
  export type ProxyConfigurationType = {
15
18
  authorization: string | null,
@@ -56,6 +59,7 @@ export type ProxyAgentConfigurationInputType = {
56
59
  forceGlobalAgent?: boolean,
57
60
  socketConnectionTimeout?: number,
58
61
  ca?: string[] | string,
62
+ logger?: Logger,
59
63
  };
60
64
 
61
65
  export type ProxyAgentConfigurationType = {