{"_id":"global-agent@4.1.6","bugs":{"url":"https://github.com/gajus/global-agent/issues"},"dist":{"shasum":"3261b3b4a340743f4c4f24a8d7886d6c4e55bb97","tarball":"http://registry-npm.ywwl.com/global-agent/download/global-agent-4.1.6.tgz","fileCount":48,"integrity":"sha512-V9u791atI/bS7RryfRTK2oixDWo6/RCrIikjwuQ4kADPRZZml1C6LUJXc7y6qRhaAF8geo3bIvE/siAGJCw38A==","signatures":[{"sig":"MEUCIAfQEAiLvGXE9ZgRQOk7rLkmOyZX3C6WLIYt88Iva7tTAiEAsPQ93kExHolJ0quyt2GocV1lsTj+l3X8+CMtUZFPy6Y=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIF+0Nva6Gbfa9pfx0n3ptg8I/LuB+cK34K2rriu5df+NAiEApNcn0bOv6i/Jzx5vY8zu00Nk5/prEoscsFrrcIW8tAs="}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/global-agent@4.1.6","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":91075,"size":18755,"noattachment":false,"key":"/global-agent/-/global-agent-4.1.6.tgz"},"main":"./dist/index.js","name":"global-agent","husky":{"hooks":{"pre-commit":"npm run lint && npm run test && npm run build"}},"author":{"url":"https://gajus.com","name":"Gajus Kuizinas","email":"gajus@gajus.com"},"engines":{"node":">=10.0"},"gitHead":"c0354adf8e8633d87cf5645e9ae505dc2c8946f4","license":"BSD-3-Clause","scripts":{"lint":"oxlint ./src && tsc","test":"vitest run","build":"del-cli ./dist && tsc","create-readme":"gitdown ./.README/README.md --output-file ./README.md"},"typings":"./dist/index.d.ts","version":"4.1.6","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"daa22605-46f1-405f-b510-44c7fe07a986"}},"homepage":"https://github.com/gajus/global-agent#readme","keywords":["http","global","proxy","agent"],"repository":{"url":"git+https://github.com/gajus/global-agent.git","type":"git"},"_npmVersion":"11.8.0","description":"Global HTTP/HTTPS proxy configurable using environment variables.","directories":{},"maintainers":[{"name":"gajus","email":"gajus@gajus.com"}],"_nodeVersion":"24.21.0","dependencies":{"semver":"^7.3.5","matcher":"^4.0.0","globalthis":"^1.0.2","serialize-error":"^8.1.0"},"_hasShrinkwrap":false,"devDependencies":{"got":"^11.8.2","pem":"^1.14.4","axios":"^0.21.1","husky":"^7.0.1","sinon":"^11.1.2","oxlint":"^1.43.0","vitest":"^4.0.18","del-cli":"^4.0.1","request":"^2.88.2","ts-node":"^10.1.0","get-port":"^5.1.1","@babel/cli":"^7.14.8","@types/pem":"^1.14.4","typescript":"^4.3.5","@babel/core":"^7.14.8","@babel/node":"^7.14.7","@types/sinon":"^10.0.2","@types/semver":"^7.3.8","@types/request":"^2.48.6","@babel/register":"^7.14.5","semantic-release":"^25.0.3","@babel/preset-env":"^7.14.8","@types/globalthis":"^1.0.1","@semantic-release/npm":"^13.1.3","@semantic-release/github":"^12.0.3","@semantic-release/commit-analyzer":"^13.0.1","@babel/plugin-transform-flow-strip-types":"^7.14.5","babel-plugin-transform-export-default-name":"^2.1.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/global-agent_4.1.6_1792791217622_0.8746480856491912"},"readme":"# global-agent\n\n[![NPM version](http://img.shields.io/npm/v/global-agent.svg?style=flat-square)](https://www.npmjs.org/package/global-agent)\n[![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)\n[![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas)\n\nGlobal HTTP/HTTPS proxy configurable using environment variables.\n\n* [Usage](#usage)\n  * [Setup proxy using `global-agent/bootstrap`](#setup-proxy-using-global-agentbootstrap)\n  * [Setup proxy using `bootstrap` routine](#setup-proxy-using-bootstrap-routine)\n  * [Runtime configuration](#runtime-configuration)\n  * [Exclude URLs](#exclude-urls)\n* [API](#api)\n  * [`createGlobalProxyAgent`](#createglobalproxyagent)\n  * [Environment variables](#environment-variables)\n  * [`global.GLOBAL_AGENT`](#globalglobal_agent)\n* [Supported libraries](#supported-libraries)\n* [FAQ](#faq)\n  * [What is the reason `global-agent` overrides explicitly configured HTTP(S) agent?](#what-is-the-reason-global-agent-overrides-explicitly-configured-https-agent)\n  * [What is the reason `global-agent/bootstrap` does not use `HTTP_PROXY`?](#what-is-the-reason-global-agentbootstrap-does-not-use-http_proxy)\n  * [What is the difference from `global-tunnel` and `tunnel`?](#what-is-the-difference-from-global-tunnel-and-tunnel)\n\n## Usage\n\n### Setup proxy using `global-agent/bootstrap`\n\nTo configure HTTP proxy:\n\n1. Import `global-agent/bootstrap`.\n1. Export HTTP proxy address as `GLOBAL_AGENT_HTTP_PROXY` environment variable.\n\nCode:\n\n```js\nimport 'global-agent/bootstrap';\n\n// or:\n// import {bootstrap} from 'global-agent';\n// bootstrap();\n\n```\n\nBash:\n\n```bash\n$ export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080\n\n```\n\nAlternatively, you can preload module using Node.js `--require, -r` configuration, e.g.\n\n```bash\n$ export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080\n$ node -r 'global-agent/bootstrap' your-script.js\n\n```\n\n### Setup proxy using `bootstrap` routine\n\nInstead of importing a self-initialising script with side-effects as demonstrated in the [setup proxy using `global-agent/bootstrap`](#setup-proxy-using-global-agentbootstrap) documentation, you can import `bootstrap` routine and explicitly evaluate the bootstrap logic, e.g.\n\n```js\nimport {\n  bootstrap\n} from 'global-agent';\n\nbootstrap();\n\n```\n\nThis is useful if you need to conditionally bootstrap `global-agent`, e.g.\n\n```js\nimport {\n  bootstrap\n} from 'global-agent';\nimport globalTunnel from 'global-tunnel-ng';\n\nconst MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10);\n\nif (MAJOR_NODEJS_VERSION >= 10) {\n  // `global-agent` works with Node.js v10 and above.\n  bootstrap();\n} else {\n  // `global-tunnel-ng` works only with Node.js v10 and below.\n  globalTunnel.initialize();\n}\n\n```\n\n### Setup proxy using `createGlobalProxyAgent`\n\nIf you do not want to use `global.GLOBAL_AGENT` variable, then you can use `createGlobalProxyAgent` to instantiate a controlled instance of `global-agent`, e.g.\n\n```js\nimport {\n  createGlobalProxyAgent\n} from 'global-agent';\n\nconst globalProxyAgent = createGlobalProxyAgent();\n\n```\n\nUnlike `bootstrap` routine, `createGlobalProxyAgent` factory does not create `global.GLOBAL_AGENT` variable and does not guard against multiple initializations of `global-agent`. The result object of `createGlobalProxyAgent` is equivalent to `global.GLOBAL_AGENT`.\n\n### Runtime configuration\n\n`global-agent/bootstrap` script copies `process.env.GLOBAL_AGENT_HTTP_PROXY` value to `global.GLOBAL_AGENT.HTTP_PROXY` and continues to use the latter variable.\n\nYou can override the `global.GLOBAL_AGENT.HTTP_PROXY` value at runtime to change proxy behaviour, e.g.\n\n```js\nhttp.get('http://127.0.0.1:8000');\n\nglobal.GLOBAL_AGENT.HTTP_PROXY = 'http://127.0.0.1:8001';\n\nhttp.get('http://127.0.0.1:8000');\n\nglobal.GLOBAL_AGENT.HTTP_PROXY = 'http://127.0.0.1:8002';\n\n```\n\nThe first HTTP request is going to use http://127.0.0.1:8001 proxy and the second request is going to use http://127.0.0.1:8002.\n\nAll `global-agent` configuration is available under `global.GLOBAL_AGENT` namespace.\n\n### Exclude URLs\n\nThe `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.\n\n```bash\nexport GLOBAL_AGENT_NO_PROXY='*.foo.com,baz.com'\n\n```\n\nsays to contact all machines with the 'foo.com' TLD and 'baz.com' domains directly.\n\n### Separate proxy for HTTPS\n\nThe 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.\n\n## API\n\n### `createGlobalProxyAgent`\n\n```js\n/**\n * @property environmentVariableNamespace Defines namespace of `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables. (Default: `GLOBAL_AGENT_`)\n * @property forceGlobalAgent Forces to use `global-agent` HTTP(S) agent even when request was explicitly constructed with another agent. (Default: `true`)\n * @property socketConnectionTimeout Destroys socket if connection is not established within the timeout. (Default: `60000`)\n * @property ca Single CA certificate or an array of CA certificates that is trusted for secure connections to the registry.\n * @property logger Custom logger instance for debug logging. Must implement `child`, `debug`, `error`, `info`, `trace`, and `warn` methods.\n */\ntype ProxyAgentConfigurationInputType = {|\n  +environmentVariableNamespace?: string,\n  +forceGlobalAgent?: boolean,\n  +socketConnectionTimeout?: number,\n  +ca?: string[] | string,\n  +logger?: Logger,\n|};\n\n(configurationInput: ProxyAgentConfigurationInputType) => ProxyAgentConfigurationType;\n\n```\n\n### Custom Logger\n\nYou can provide a custom logger to `global-agent` for debugging purposes. The logger must implement the following interface:\n\n```ts\ntype Logger = {\n  child: (context: object) => Logger,\n  debug: (context: object | string, message?: string) => void,\n  error: (context: object | string, message?: string) => void,\n  info: (context: object | string, message?: string) => void,\n  trace: (context: object | string, message?: string) => void,\n  warn: (context: object | string, message?: string) => void,\n};\n```\n\nExample using a custom logger:\n\n```js\nimport { createGlobalProxyAgent } from 'global-agent';\n\ncreateGlobalProxyAgent({\n  logger: {\n    child: () => logger,\n    debug: console.debug,\n    error: console.error,\n    info: console.info,\n    trace: console.trace,\n    warn: console.warn,\n  },\n});\n```\n\n### Environment variables\n\n|Name|Description|Default|\n|---|---|---|\n|`GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE`|Defines namespace of `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables.|`GLOBAL_AGENT_`|\n|`GLOBAL_AGENT_FORCE_GLOBAL_AGENT`|Forces to use `global-agent` HTTP(S) agent even when request was explicitly constructed with another agent.|`true`|\n|`GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT`|Destroys socket if connection is not established within the timeout.|`60000`|\n|`${NAMESPACE}HTTP_PROXY`|Sets the initial proxy controller HTTP_PROXY value.|N/A|\n|`${NAMESPACE}HTTPS_PROXY`|Sets the initial proxy controller HTTPS_PROXY value.|N/A|\n|`${NAMESPACE}NO_PROXY`|Sets the initial proxy controller NO_PROXY value.|N/A|\n\n### `global.GLOBAL_AGENT`\n\n`global.GLOBAL_AGENT` is initialized by `bootstrap` routine.\n\n`global.GLOBAL_AGENT` has the following properties:\n\n|Name|Configurable|Description|\n|---|---|---|\n|`HTTP_PROXY`|Yes|Sets HTTP proxy to use.|\n|`HTTPS_PROXY`|Yes|Sets a distinct proxy to use for HTTPS requests.|\n|`NO_PROXY`|Yes|Specifies a pattern of URLs that should be excluded from proxying. See [Exclude URLs](#exclude-urls).|\n\n## Certificate Authority (CA)\n\n### `addCACertificates`\nThis method can be accessed using https to add CA certificates to the global-agent.\n\nUses:\n```js\nif (typeof https.globalAgent.addCACertificates === 'function') {\n  //certificate - an array of ca certificates to be added to the global-agent\n  https.globalAgent.addCACertificates(certificate);\n}\n```\n\nMethod Definition:\n```js\n/**\n * This method can be used to append new ca certificates to existing ca certificates\n * @param {string[] | string} ca a ca certificate or an array of ca certificates\n */\npublic addCACertificates (ca: string[] | string) {\n  if (!ca) {\n    log.error('Invalid input ca certificate');\n  } else if (this.ca) {\n    if (typeof ca === typeof this.ca) {\n      // concat valid ca certificates with the existing certificates,\n      if (typeof this.ca === 'string') {\n        this.ca = this.ca.concat(ca as string);\n      } else {\n        this.ca = this.ca.concat(ca as string[]);\n      }\n    } else {\n      log.error('Input ca certificate type mismatched with existing ca certificate type');\n    }\n  } else {\n    this.ca = ca;\n  }\n}\n```\n\n### `clearCACertificates`\nThis method can be accessed using https to clear existing CA certificates from global-agent.\n\nUses:\n```js\nif (typeof https.globalAgent.clearCACertificates === 'function') {\n  https.globalAgent.clearCACertificates();\n}\n```\nMethod Definition:\n```js\n/**\n * This method clears existing CA Certificates.\n * It sets ca to undefined\n */\npublic clearCACertificates () {\n  this.ca = undefined;\n}\n```\n\n## Supported libraries\n\n`global-agent` works with all libraries that internally use [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback).\n\n`global-agent` has been tested to work with:\n\n* [`got`](https://www.npmjs.com/package/got)\n* [`axios`](https://www.npmjs.com/package/axios)\n* [`request`](https://www.npmjs.com/package/request)\n\n## FAQ\n\n### What is the reason `global-agent` overrides explicitly configured HTTP(S) agent?\n\nBy default, `global-agent` overrides [`agent` property](https://nodejs.org/api/http.html#http_http_request_options_callback) 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`](https://github.com/stripe/stripe-node/blob/e542902dd8fbe591fe3c3ce07a7e89d1d60e4cf7/lib/StripeResource.js#L11-L12)).\n\nThis behaviour can be disabled with `GLOBAL_AGENT_FORCE_GLOBAL_AGENT=false` environment variable. When disabled, then `global-agent` will only set `agent` property when it is not already defined or if `agent` is an instance of `http(s).globalAgent`.\n\n### What is the reason `global-agent/bootstrap` does not use `HTTP_PROXY`?\n\nSome libraries (e.g. [`request`](https://npmjs.org/package/request)) change their behaviour when `HTTP_PROXY` environment variable is present. Using a namespaced environment variable prevents conflicting library behaviour.\n\nYou can override this behaviour by configuring `GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE` variable, e.g.\n\n```bash\n$ export GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE=\n\n```\n\nNow script initialized using `global-agent/bootstrap` will use `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables.\n\n### What is the difference from `global-tunnel` and `tunnel`?\n\n[`global-tunnel`](https://github.com/salesforce/global-tunnel) (including [`global-tunnel-ng`](https://github.com/np-maintain/global-tunnel) and [`tunnel`](https://npmjs.com/package/tunnel)) are designed to support legacy Node.js versions. They use various [workarounds](https://github.com/koichik/node-tunnel/blob/5fb2fb424788597146b7be6729006cad1cf9e9a8/lib/tunnel.js#L134-L144) and rely on [monkey-patching `http.request`, `http.get`, `https.request` and `https.get` methods](https://github.com/np-maintain/global-tunnel/blob/51413dcf0534252b5049ec213105c7063ccc6367/index.js#L302-L338).\n\nIn contrast, `global-agent` supports Node.js v10 and above, and does not implements workarounds for the older Node.js versions.\n","publish_time":1792791217743,"_cnpm_publish_time":1792791217743,"dist-tags":{"latest":"4.1.6"}}