Gathering detailed insights and metrics for global-agent-ts
Gathering detailed insights and metrics for global-agent-ts
Gathering detailed insights and metrics for global-agent-ts
Gathering detailed insights and metrics for global-agent-ts
@crabas0npm2/impedit-porro-maiores
security holding package
@taktikorg/sapiente-quas-velit
[![github actions][actions-image]][actions-url] [![coverage][codecov-image]][codecov-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url]
@crabas0npm2/perspiciatis-rerum-blanditiis
security holding package
@hishprorg/vero-optio
[](http://npmjs.com/package/@hishprorg/vero-optio) [](https://github.com/hishprorg/vero-optio/
Global HTTP/HTTPS proxy agent configurable using environment variables.
npm install global-agent-ts
Typescript
Module System
Min. Node Version
Node Version
NPM Version
70.4
Supply Chain
97.8
Quality
74.9
Maintenance
100
Vulnerability
99.6
License
TypeScript (100%)
Total Downloads
4,844
Last Day
3
Last Week
204
Last Month
845
Last Year
3,259
NOASSERTION License
135 Commits
1 Forks
1 Branches
1 Contributors
Updated on Jan 22, 2024
Minified
Minified + Gzipped
Latest Version
3.5.5
Package Id
global-agent-ts@3.5.5
Unpacked Size
48.72 kB
Size
12.67 kB
File Count
37
NPM Version
8.19.3
Node Version
16.19.1
Published on
Jun 07, 2023
Cumulative downloads
Total Downloads
Last Day
-50%
3
Compared to previous day
Last Week
-10.1%
204
Compared to previous week
Last Month
217.7%
845
Compared to previous month
Last Year
270.8%
3,259
Compared to previous year
32
Global HTTP/HTTPS proxy configurable using environment variables.
global-agent-ts/bootstrap
To configure HTTP proxy:
global-agent-ts/bootstrap
.GLOBAL_AGENT_HTTP_PROXY
environment variable.Code:
1import 'global-agent-ts/bootstrap'; 2 3// or: 4// import {bootstrap} from 'global-agent-ts'; 5// bootstrap(); 6
Bash:
1$ export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080 2
Alternatively, you can preload module using Node.js --require, -r
configuration, e.g.
1$ export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080 2$ node -r 'global-agent-ts/bootstrap' your-script.js 3
bootstrap
routineInstead of importing a self-initialising script with side-effects as demonstrated in the setup proxy using global-agent-ts/bootstrap
documentation, you can import bootstrap
routine and explicitly evaluate the bootstrap logic, e.g.
1import { 2 bootstrap 3} from 'global-agent-ts'; 4 5bootstrap(); 6
This is useful if you need to conditionally bootstrap global-agent-ts
, e.g.
1import { 2 bootstrap 3} from 'global-agent-ts'; 4import globalTunnel from 'global-tunnel-ng'; 5 6const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10); 7 8if (MAJOR_NODEJS_VERSION >= 10) { 9 // `global-agent-ts` works with Node.js v10 and above. 10 bootstrap(); 11} else { 12 // `global-tunnel-ng` works only with Node.js v10 and below. 13 globalTunnel.initialize(); 14} 15
createGlobalProxyAgent
If you do not want to use global.GLOBAL_AGENT
variable, then you can use createGlobalProxyAgent
to instantiate a controlled instance of global-agent-ts
, e.g.
1import { 2 createGlobalProxyAgent 3} from 'global-agent-ts'; 4 5const globalProxyAgent = createGlobalProxyAgent(); 6
Unlike bootstrap
routine, createGlobalProxyAgent
factory does not create global.GLOBAL_AGENT
variable and does not guard against multiple initializations of global-agent-ts
. The result object of createGlobalProxyAgent
is equivalent to global.GLOBAL_AGENT
.
global-agent-ts/bootstrap
script copies process.env.GLOBAL_AGENT_HTTP_PROXY
value to global.GLOBAL_AGENT.HTTP_PROXY
and continues to use the latter variable.
You can override the global.GLOBAL_AGENT.HTTP_PROXY
value at runtime to change proxy behaviour, e.g.
1http.get('http://127.0.0.1:8000'); 2 3global.GLOBAL_AGENT.HTTP_PROXY = 'http://127.0.0.1:8001'; 4 5http.get('http://127.0.0.1:8000'); 6 7global.GLOBAL_AGENT.HTTP_PROXY = 'http://127.0.0.1:8002'; 8
The first HTTP request is going to use http://127.0.0.1:8001 proxy and the secord request is going to use http://127.0.0.1:8002.
All global-agent-ts
configuration is available under global.GLOBAL_AGENT
namespace.
The GLOBAL_AGENT_NO_PROXY
environment variable specifies a pattern of URLs that should be excluded from proxying. GLOBAL_AGENT_NO_PROXY
value is a comma-separated list of domain names. Asterisks can be used as wildcards, e.g.
1export GLOBAL_AGENT_NO_PROXY='*.foo.com,baz.com' 2
says to contact all machines with the 'foo.com' TLD and 'baz.com' domains directly.
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.
global-agent-ts
is using roarr
logger to log HTTP requests and response (HTTP status code and headers), e.g.
1{"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"} 2{"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"} 3
Export ROARR_LOG=true
environment variable to enable log printing to stdout.
Use roarr-cli
program to pretty-print the logs.
createGlobalProxyAgent
1/** 2 * @property environmentVariableNamespace Defines namespace of `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables. (Default: `GLOBAL_AGENT_`) 3 * @property forceGlobalAgent Forces to use `global-agent-ts` HTTP(S) agent even when request was explicitly constructed with another agent. (Default: `true`) 4 * @property socketConnectionTimeout Destroys socket if connection is not established within the timeout. (Default: `60000`) 5 * @property rejectUnauthorized `false` - all invalid SSL certificates are ignored and no error is thrown. 6 * `true` - an error is thrown when an invalid SSL certificate is detected. 7 * (Default: `undefined`) 8 */ 9type ProxyAgentConfigurationInputType = {| 10 +environmentVariableNamespace?: string, 11 +forceGlobalAgent?: boolean, 12 +socketConnectionTimeout?: number, 13 +rejectUnauthorized?: boolean, 14|}; 15 16(configurationInput: ProxyAgentConfigurationInputType) => ProxyAgentConfigurationType; 17
Name | Description | Default |
---|---|---|
GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE | Defines namespace of HTTP_PROXY , HTTPS_PROXY and NO_PROXY environment variables. | GLOBAL_AGENT_ |
GLOBAL_AGENT_FORCE_GLOBAL_AGENT | Forces to use global-agent-ts HTTP(S) agent even when request was explicitly constructed with another agent. | true |
GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT | Destroys socket if connection is not established within the timeout. | 60000 |
${NAMESPACE}HTTP_PROXY | Sets the initial proxy controller HTTP_PROXY value. | N/A |
${NAMESPACE}HTTPS_PROXY | Sets the initial proxy controller HTTPS_PROXY value. | N/A |
${NAMESPACE}NO_PROXY | Sets the initial proxy controller NO_PROXY value. | N/A |
global.GLOBAL_AGENT
global.GLOBAL_AGENT
is initialized by bootstrap
routine.
global.GLOBAL_AGENT
has the following properties:
Name | Description | Configurable |
---|---|---|
HTTP_PROXY | Yes | Sets HTTP proxy to use. |
HTTPS_PROXY | Yes | Sets a distinct proxy to use for HTTPS requests. |
NO_PROXY | Yes | Specifies a pattern of URLs that should be excluded from proxying. See Exclude URLs. |
global-agent-ts
works with all libraries that internally use http.request
.
global-agent-ts
has been tested to work with:
global-agent-ts
overrides explicitly configured HTTP(S) agent?By default, global-agent-ts
overrides agent
property of any HTTP request, even if agent
property was explicitly set when constructing a HTTP request. This behaviour allows to intercept requests of libraries that use a custom instance of an agent per default (e.g. Stripe SDK uses an http(s).globalAgent
instance pre-configured with keepAlive: true
).
This behaviour can be disabled with GLOBAL_AGENT_FORCE_GLOBAL_AGENT=false
environment variable. When disabled, then global-agent-ts
will only set agent
property when it is not already defined or if agent
is an instance of http(s).globalAgent
.
global-agent-ts/bootstrap
does not use HTTP_PROXY
?Some libraries (e.g. request
) change their behaviour when HTTP_PROXY
environment variable is present. Using a namespaced environment variable prevents conflicting library behaviour.
You can override this behaviour by configuring GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE
variable, e.g.
1$ export GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE= 2
Now script initialized using global-agent-ts/bootstrap
will use HTTP_PROXY
, HTTPS_PROXY
and NO_PROXY
environment variables.
global-tunnel
and tunnel
?global-tunnel
(including global-tunnel-ng
and tunnel
) are designed to support legacy Node.js versions. They use various workarounds and rely on monkey-patching http.request
, http.get
, https.request
and https.get
methods.
In contrast, global-agent-ts
supports Node.js v10 and above, and does not implements workarounds for the older Node.js versions.
No vulnerabilities found.
No security vulnerabilities found.