{"name":"agentkeepalive","version":"4.6.0","description":"Missing keepalive http.Agent","main":"index.js","browser":"browser.js","scripts":{"contributor":"git-contributor","test":"npm run lint && egg-bin test --full-trace","test-local":"egg-bin test --full-trace","cov":"cross-env NODE_DEBUG=agentkeepalive egg-bin cov --full-trace","ci":"npm run lint && npm run cov","lint":"eslint lib test index.js"},"repository":{"type":"git","url":"git://github.com/node-modules/agentkeepalive.git"},"bugs":{"url":"https://github.com/node-modules/agentkeepalive/issues"},"keywords":["http","https","agent","keepalive","agentkeepalive","HttpAgent","HttpsAgent"],"dependencies":{"humanize-ms":"^1.2.1"},"devDependencies":{"coffee":"^5.3.0","cross-env":"^6.0.3","egg-bin":"^4.9.0","eslint":"^5.7.0","eslint-config-egg":"^7.1.0","git-contributor":"^2.0.0","mm":"^2.4.1","pedding":"^1.1.0","typescript":"^3.8.3"},"engines":{"node":">= 8.0.0"},"author":{"name":"fengmk2","email":"fengmk2@gmail.com","url":"https://github.com/fengmk2"},"license":"MIT","_id":"agentkeepalive@4.6.0","gitHead":"422e9fc35aaa076da6120f634f85ffc8c860d85a","types":"./index.d.ts","homepage":"https://github.com/node-modules/agentkeepalive#readme","_nodeVersion":"22.12.0","_npmVersion":"10.9.0","dist":{"integrity":"sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==","shasum":"35f73e94b3f40bf65f105219c623ad19c136ea6a","tarball":"http://registry-npm.ywwl.com/agentkeepalive/download/agentkeepalive-4.6.0.tgz","fileCount":9,"unpackedSize":33916,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHIBstwDsGZiSAnGBCUSe0wJrXECkYCK92CtcDxlyMi4AiAWIq4vBpTFax+tRABg5ZBDEmEivXximCyQTi0HCJ7n4w=="}],"size":9859,"noattachment":false,"key":"/agentkeepalive/-/agentkeepalive-4.6.0.tgz"},"_npmUser":{"name":"fengmk2","email":"fengmk2@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/agentkeepalive_4.6.0_1735441031258_0.6666414305023092"},"_hasShrinkwrap":false,"readme":"# agentkeepalive\n\n[![NPM version][npm-image]][npm-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![Node.js CI](https://github.com/node-modules/agentkeepalive/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/agentkeepalive/actions/workflows/nodejs.yml)\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/agentkeepalive.svg?style=flat\n[npm-url]: https://npmjs.org/package/agentkeepalive\n[snyk-image]: https://snyk.io/test/npm/agentkeepalive/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/agentkeepalive\n[download-image]: https://img.shields.io/npm/dm/agentkeepalive.svg?style=flat-square\n[download-url]: https://npmjs.org/package/agentkeepalive\n\nThe enhancement features `keep alive` `http.Agent`. Support `http` and `https`.\n\n## What's different from original `http.Agent`?\n\n- `keepAlive=true` by default\n- Disable Nagle's algorithm: `socket.setNoDelay(true)`\n- Add free socket timeout: avoid long time inactivity socket leak in the free-sockets queue.\n- Add active socket timeout: avoid long time inactivity socket leak in the active-sockets queue.\n- TTL for active socket.\n\n## Node.js version required\n\nSupport Node.js >= `8.0.0`\n\n## Install\n\n```bash\n$ npm install agentkeepalive --save\n```\n\n## new Agent([options])\n\n* `options` {Object} Set of configurable options to set on the agent.\n  Can have the following fields:\n  * `keepAlive` {Boolean} Keep sockets around in a pool to be used by\n    other requests in the future. Default = `true`.\n  * `keepAliveMsecs` {Number} When using the keepAlive option, specifies the initial delay\n    for TCP Keep-Alive packets. Ignored when the keepAlive option is false or undefined. Defaults to 1000.\n    Default = `1000`.  Only relevant if `keepAlive` is set to `true`.\n  * `freeSocketTimeout`: {Number} Sets the free socket to timeout\n    after `freeSocketTimeout` milliseconds of inactivity on the free socket.\n    The default [server-side timeout](https://nodejs.org/api/http.html#serverkeepalivetimeout) is 5000 milliseconds, to [avoid ECONNRESET exceptions](https://medium.com/ssense-tech/reduce-networking-errors-in-nodejs-23b4eb9f2d83), we set the default value to `4000` milliseconds.\n    Only relevant if `keepAlive` is set to `true`.\n  * `timeout`: {Number} Sets the working socket to timeout\n    after `timeout` milliseconds of inactivity on the working socket.\n    Default is `freeSocketTimeout * 2` so long as that value is greater than or equal to 8 seconds, otherwise the default is 8 seconds.\n  * `maxSockets` {Number} Maximum number of sockets to allow per\n    host. Default = `Infinity`.\n  * `maxFreeSockets` {Number} Maximum number of sockets (per host) to leave open\n    in a free state. Only relevant if `keepAlive` is set to `true`.\n    Default = `256`.\n  * `socketActiveTTL` {Number} Sets the socket active time to live, even if it's in use.\n    If not set, the behaviour keeps the same (the socket will be released only when free)\n    Default = `null`.\n\n## Usage\n\n```js\nconst http = require('http');\nconst HttpAgent = require('agentkeepalive').HttpAgent;\n\nconst keepaliveAgent = new HttpAgent({\n  maxSockets: 100,\n  maxFreeSockets: 10,\n  timeout: 60000, // active socket keepalive for 60 seconds\n  freeSocketTimeout: 30000, // free socket keepalive for 30 seconds\n});\n\nconst options = {\n  host: 'cnodejs.org',\n  port: 80,\n  path: '/',\n  method: 'GET',\n  agent: keepaliveAgent,\n};\n\nconst req = http.request(options, res => {\n  console.log('STATUS: ' + res.statusCode);\n  console.log('HEADERS: ' + JSON.stringify(res.headers));\n  res.setEncoding('utf8');\n  res.on('data', function (chunk) {\n    console.log('BODY: ' + chunk);\n  });\n});\nreq.on('error', e => {\n  console.log('problem with request: ' + e.message);\n});\nreq.end();\n\nsetTimeout(() => {\n  if (keepaliveAgent.statusChanged) {\n    console.log('[%s] agent status changed: %j', Date(), keepaliveAgent.getCurrentStatus());\n  }\n}, 2000);\n\n```\n\n### `getter agent.statusChanged`\n\ncounters have change or not after last checkpoint.\n\n### `agent.getCurrentStatus()`\n\n`agent.getCurrentStatus()` will return a object to show the status of this agent:\n\n```js\n{\n  createSocketCount: 10,\n  closeSocketCount: 5,\n  timeoutSocketCount: 0,\n  requestCount: 5,\n  freeSockets: { 'localhost:57479:': 3 },\n  sockets: { 'localhost:57479:': 5 },\n  requests: {}\n}\n```\n\n### Support `https`\n\n```js\nconst https = require('https');\nconst HttpsAgent = require('agentkeepalive').HttpsAgent;\n\nconst keepaliveAgent = new HttpsAgent();\n// https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8\nconst options = {\n  host: 'www.google.com',\n  port: 443,\n  path: '/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8',\n  method: 'GET',\n  agent: keepaliveAgent,\n};\n\nconst req = https.request(options, res => {\n  console.log('STATUS: ' + res.statusCode);\n  console.log('HEADERS: ' + JSON.stringify(res.headers));\n  res.setEncoding('utf8');\n  res.on('data', chunk => {\n    console.log('BODY: ' + chunk);\n  });\n});\n\nreq.on('error', e => {\n  console.log('problem with request: ' + e.message);\n});\nreq.end();\n\nsetTimeout(() => {\n  console.log('agent status: %j', keepaliveAgent.getCurrentStatus());\n}, 2000);\n```\n\n### Support `req.reusedSocket`\n\nThis agent implements the `req.reusedSocket` to determine whether a request is send through a reused socket.\n\nWhen server closes connection at unfortunate time ([keep-alive race](https://code-examples.net/en/q/28a8069)), the http client will throw a `ECONNRESET` error. Under this circumstance, `req.reusedSocket` is useful when we want to retry the request automatically.\n\n```js\nconst http = require('http');\nconst HttpAgent = require('agentkeepalive').HttpAgent;\nconst agent = new HttpAgent();\n\nconst req = http\n  .get('http://localhost:3000', { agent }, (res) => {\n    // ...\n  })\n  .on('error', (err) => {\n    if (req.reusedSocket && err.code === 'ECONNRESET') {\n      // retry the request or anything else...\n    }\n  })\n```\n\nThis behavior is consistent with Node.js core. But through `agentkeepalive`, you can use this feature in older Node.js version.\n\n## [Benchmark](https://github.com/node-modules/agentkeepalive/tree/master/benchmark)\n\nrun the benchmark:\n\n```bash\ncd benchmark\nsh start.sh\n```\n\nIntel(R) Core(TM)2 Duo CPU     P8600  @ 2.40GHz\n\nnode@v0.8.9\n\n50 maxSockets, 60 concurrent, 1000 requests per concurrent, 5ms delay\n\nKeep alive agent (30 seconds):\n\n```js\nTransactions:          60000 hits\nAvailability:         100.00 %\nElapsed time:          29.70 secs\nData transferred:        14.88 MB\nResponse time:            0.03 secs\nTransaction rate:      2020.20 trans/sec\nThroughput:           0.50 MB/sec\nConcurrency:           59.84\nSuccessful transactions:       60000\nFailed transactions:             0\nLongest transaction:          0.15\nShortest transaction:         0.01\n```\n\nNormal agent:\n\n```js\nTransactions:          60000 hits\nAvailability:         100.00 %\nElapsed time:          46.53 secs\nData transferred:        14.88 MB\nResponse time:            0.05 secs\nTransaction rate:      1289.49 trans/sec\nThroughput:           0.32 MB/sec\nConcurrency:           59.81\nSuccessful transactions:       60000\nFailed transactions:             0\nLongest transaction:          0.45\nShortest transaction:         0.00\n```\n\nSocket created:\n\n```bash\n[proxy.js:120000] keepalive, 50 created, 60000 requestFinished, 1200 req/socket, 0 requests, 0 sockets, 0 unusedSockets, 50 timeout\n{\" <10ms\":662,\" <15ms\":17825,\" <20ms\":20552,\" <30ms\":17646,\" <40ms\":2315,\" <50ms\":567,\" <100ms\":377,\" <150ms\":56,\" <200ms\":0,\" >=200ms+\":0}\n----------------------------------------------------------------\n[proxy.js:120000] normal   , 53866 created, 84260 requestFinished, 1.56 req/socket, 0 requests, 0 sockets\n{\" <10ms\":75,\" <15ms\":1112,\" <20ms\":10947,\" <30ms\":32130,\" <40ms\":8228,\" <50ms\":3002,\" <100ms\":4274,\" <150ms\":181,\" <200ms\":18,\" >=200ms+\":33}\n```\n\n## License\n\n[MIT](LICENSE)\n\n<!-- GITCONTRIBUTOR_START -->\n\n## Contributors\n\n|[<img src=\"https://avatars.githubusercontent.com/u/156269?v=4\" width=\"100px;\"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/985607?v=4\" width=\"100px;\"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/5557458?v=4\" width=\"100px;\"/><br/><sub><b>AndrewLeedham</b></sub>](https://github.com/AndrewLeedham)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/5243774?v=4\" width=\"100px;\"/><br/><sub><b>ngot</b></sub>](https://github.com/ngot)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/25919630?v=4\" width=\"100px;\"/><br/><sub><b>wrynearson</b></sub>](https://github.com/wrynearson)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/26738844?v=4\" width=\"100px;\"/><br/><sub><b>aaronArinder</b></sub>](https://github.com/aaronArinder)<br/>|\n| :---: | :---: | :---: | :---: | :---: | :---: |\n|[<img src=\"https://avatars.githubusercontent.com/u/10976983?v=4\" width=\"100px;\"/><br/><sub><b>alexpenev-s</b></sub>](https://github.com/alexpenev-s)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/959726?v=4\" width=\"100px;\"/><br/><sub><b>blemoine</b></sub>](https://github.com/blemoine)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/398027?v=4\" width=\"100px;\"/><br/><sub><b>bdehamer</b></sub>](https://github.com/bdehamer)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/4985201?v=4\" width=\"100px;\"/><br/><sub><b>DylanPiercey</b></sub>](https://github.com/DylanPiercey)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/3770250?v=4\" width=\"100px;\"/><br/><sub><b>cixel</b></sub>](https://github.com/cixel)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/2883231?v=4\" width=\"100px;\"/><br/><sub><b>HerringtonDarkholme</b></sub>](https://github.com/HerringtonDarkholme)<br/>|\n|[<img src=\"https://avatars.githubusercontent.com/u/1433247?v=4\" width=\"100px;\"/><br/><sub><b>denghongcai</b></sub>](https://github.com/denghongcai)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/1847934?v=4\" width=\"100px;\"/><br/><sub><b>kibertoad</b></sub>](https://github.com/kibertoad)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/5236150?v=4\" width=\"100px;\"/><br/><sub><b>pangorgo</b></sub>](https://github.com/pangorgo)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/588898?v=4\" width=\"100px;\"/><br/><sub><b>mattiash</b></sub>](https://github.com/mattiash)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/182440?v=4\" width=\"100px;\"/><br/><sub><b>nabeelbukhari</b></sub>](https://github.com/nabeelbukhari)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/1411117?v=4\" width=\"100px;\"/><br/><sub><b>pmalouin</b></sub>](https://github.com/pmalouin)<br/>|\n[<img src=\"https://avatars.githubusercontent.com/u/1404810?v=4\" width=\"100px;\"/><br/><sub><b>SimenB</b></sub>](https://github.com/SimenB)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/2630384?v=4\" width=\"100px;\"/><br/><sub><b>vinaybedre</b></sub>](https://github.com/vinaybedre)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/10933333?v=4\" width=\"100px;\"/><br/><sub><b>starkwang</b></sub>](https://github.com/starkwang)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/6897780?v=4\" width=\"100px;\"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/15345331?v=4\" width=\"100px;\"/><br/><sub><b>tony-gutierrez</b></sub>](https://github.com/tony-gutierrez)<br/>|[<img src=\"https://avatars.githubusercontent.com/u/5856440?v=4\" width=\"100px;\"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>\n\nThis project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sat Aug 05 2023 02:36:31 GMT+0800`.\n\n<!-- GITCONTRIBUTOR_END -->\n","publish_time":1735441031450,"maintainers":[{"name":"dead-horse","email":"dead_horse@qq.com"},{"name":"dead_horse","email":"dead_horse@qq.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"},{"name":"popomore","email":"sakura9515@gmail.com"}],"_cnpm_publish_time":1735441031450,"dist-tags":{"latest":"4.6.0","latest-2":"2.2.0","latest-3":"3.5.3","node-0.10":"0.2.4","release-2":"2.2.0"}}