diff --git a/README.md b/README.md index 91bd68c..6a7ee5a 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,9 @@ $ npm install agentkeepalive --save ```js const http = require('http'); -const Agent = require('agentkeepalive'); +const HttpAgent = require('agentkeepalive').HttpAgent; -const keepaliveAgent = new Agent({ +const keepaliveAgent = new HttpAgent({ maxSockets: 100, maxFreeSockets: 10, timeout: 60000, // active socket keepalive for 60 seconds @@ -162,8 +162,8 @@ When server closes connection at unfortunate time ([keep-alive race](https://cod ```js const http = require('http'); -const Agent = require('agentkeepalive'); -const agent = new Agent(); +const HttpAgent = require('agentkeepalive').HttpAgent; +const agent = new HttpAgent(); const req = http .get('http://localhost:3000', { agent }, (res) => { diff --git a/benchmark/echo.js b/benchmark/echo.js index 8c5954f..17f1376 100644 --- a/benchmark/echo.js +++ b/benchmark/echo.js @@ -1,8 +1,8 @@ 'use strict'; -const Agent = require('../'); +const HttpAgent = require('../').HttpAgent; -const agent = new Agent({ +const agent = new HttpAgent({ keepAlive: true, maxSockets: 2, maxFreeSockets: 2, @@ -54,7 +54,7 @@ function showAgentDetail() { } const requestPerSocket = agent.createSocketCount && agent.requestFinishedCount / agent.createSocketCount; - console.log('[%s] [worker:%d] Agent(%s,%sms,%s,%s): requests: %d, created: %d, timeout: %d, reqs/socket: %s, pedding requests: %j, alive sockets: %j, free sockets: %j', + console.log('[%s] [worker:%d] HttpAgent(%s,%sms,%s,%s): requests: %d, created: %d, timeout: %d, reqs/socket: %s, pedding requests: %j, alive sockets: %j, free sockets: %j', Date(), process.pid, agent.keepAlive && agent.options.keepAlive, agent.keepAliveMsecs, agent.maxSockets, agent.maxFreeSockets, diff --git a/benchmark/proxy.js b/benchmark/proxy.js index f5c8b60..f776f89 100644 --- a/benchmark/proxy.js +++ b/benchmark/proxy.js @@ -1,19 +1,19 @@ 'use strict'; var http = require('http'); -var AgentKeepalive = require('../'); +var HttpAgent = require('../').HttpAgent; var maxSockets = parseInt(process.argv[2]) || 10; var maxFreeSockets = parseInt(process.argv[3]) || maxSockets; var SERVER = process.argv[4] || '127.0.0.1'; -var agentKeepalive = new AgentKeepalive({ +var agentKeepalive = new HttpAgent({ keepAlive: true, maxSockets: maxSockets, maxFreeSockets: maxFreeSockets, keepAliveTimeout: 30000, }); -var agentHttp = new AgentKeepalive({ +var agentHttp = new HttpAgent({ maxSockets: maxSockets, keepAlive: false, }); diff --git a/example/agent.js b/example/http_agent.js similarity index 77% rename from example/agent.js rename to example/http_agent.js index d4eb5ff..e2ef20c 100644 --- a/example/agent.js +++ b/example/http_agent.js @@ -1,9 +1,9 @@ 'use strict'; const http = require('http'); -const Agent = require('..'); +const HttpAgent = require('..').HttpAgent; -const agent = new Agent(); +const http_agent = new HttpAgent(); // https://www.google.com/search?q=nodejs&sugexp=chrome,mod=12&sourceid=chrome&ie=UTF-8 const options = { @@ -11,7 +11,7 @@ const options = { path: '/', method: 'GET', port: 80, - agent, + agent: http_agent, }; function get() { @@ -34,15 +34,15 @@ function get() { get(); setTimeout(() => { - console.log('keep alive sockets:', agent); + console.log('keep alive sockets:', http_agent); process.exit(); }, 300000); let count = 0; setInterval(() => { - const name = agent.getName(options); - const sockets = agent.sockets[name] || []; - const freeSockets = agent.freeSockets[name] || []; + const name = http_agent.getName(options); + const sockets = http_agent.sockets[name] || []; + const freeSockets = http_agent.freeSockets[name] || []; console.log('%ss, %s, sockets: %d, destroyed: %s, free sockets: %d, destroyed: %s', ++count, name, sockets.length, sockets[0] && sockets[0].destroyed, freeSockets.length, freeSockets[0] && freeSockets[0].destroyed); diff --git a/index.d.ts b/index.d.ts index 2bc70a6..f4dbcec 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,7 +6,7 @@ interface PlainObject { [key: string]: any; } -declare class HttpAgent extends http.Agent { +declare class _HttpAgent extends http.Agent { constructor(opts?: AgentKeepAlive.HttpOptions); readonly statusChanged: boolean; createConnection(options: net.NetConnectOpts, cb?: Function): net.Socket; @@ -25,7 +25,10 @@ interface Constants { SOCKET_REQUEST_FINISHED_COUNT: Symbol; } -declare class AgentKeepAlive extends HttpAgent {} +/** + * @deprecated instead use `import { HttpAgent } from 'agentkeepalive'; or `const HttpAgent = require('agentkeepalive').HttpAgent;` + */ +declare class AgentKeepAlive extends _HttpAgent {} declare namespace AgentKeepAlive { export interface AgentStatus { @@ -51,6 +54,7 @@ declare namespace AgentKeepAlive { export interface HttpOptions extends http.AgentOptions, CommonHttpOption { } export interface HttpsOptions extends https.AgentOptions, CommonHttpOption { } + export class HttpAgent extends _HttpAgent {} export class HttpsAgent extends https.Agent { constructor(opts?: HttpsOptions); readonly statusChanged: boolean; diff --git a/index.js b/index.js index 6ca1513..cfe41dc 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ 'use strict'; -module.exports = require('./lib/agent'); +const HttpAgent = require('./lib/agent'); +module.exports = HttpAgent; +module.exports.HttpAgent = HttpAgent; module.exports.HttpsAgent = require('./lib/https_agent'); module.exports.constants = require('./lib/constants'); diff --git a/test/fixtures/agenttest-cert.pem b/test/fixtures/agenttest-cert.pem index acc0412..5ef5135 100644 --- a/test/fixtures/agenttest-cert.pem +++ b/test/fixtures/agenttest-cert.pem @@ -1,18 +1,23 @@ -----BEGIN CERTIFICATE----- -MIIC+jCCAeKgAwIBAgIJAMuPffHFVqVzMA0GCSqGSIb3DQEBCwUAMGExCzAJBgNV -BAYTAlNFMRIwEAYDVQQIDAlTdG9ja2hvbG0xEjAQBgNVBAcMCVN0b2NraG9sbTEL -MAkGA1UEAwwCY2ExHTAbBgkqhkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTE3 -MTAyMDExMzQwOFoXDTIwMDcxNTExMzQwOFowUjELMAkGA1UEBhMCU0UxEjAQBgNV -BAgMCVN0b2NraG9sbTESMBAGA1UEBwwJU3RvY2tob2xtMRswGQYDVQQDDBJhZ2Vu -dGtlZXBhbGl2ZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAOWRbiod -/IXLR3z3bDfN4DMqzXn8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PQDnocwP5xmoYi7Au84U4Dx +esYk/aursc2S/jkiM1XCO37/FEe3IYffSH4D4HuBo+6jPzBSbbDFqPYNM+om52ad +FjxWhN/v4Kwrc5a3nVilWULgsFqE5LiXp5Vuoi7HyEuGya23wM825QddgxizNRRr +FT+Y7hDbGFdW/ZVTq3cpXm9E8sFkPMqiQUiZSt5XCPuh7AqN2Mt4Ghuebu/GhC7T +CebAe+24ylIlY8bpg5m5bpyhZR7vOjVS6bcCR8MxONimWwlN50XYHgB5PhMEkCM3 +jsB0xrxJSAHQraPOXeIwfxORxdW1qs0fKa0CAwEAAaOBiTCBhjAsBgNVHREEJTAj +hwR/AAABhxAAAAAAAAAAAAAAAAAAAAABgglsb2NhbGhvc3QwCQYDVR0TBAIwADAL +BgNVHQ8EBAMCBeAwHQYDVR0OBBYEFPp94MOUHtJpzS+9rDjBtyrxEPqgMB8GA1Ud +IwQYMBaAFB7Dfbd+Q1uFOsGWENIzhFSA6e8TMA0GCSqGSIb3DQEBCwUAA4IBAQA4 +DQxg2XCtwDkg4Blw2vl/E02PeY4vVXtkYpY3TNi4eE9YAqxXtyF98+Vpgbhyh5zv +I+1h5h/UQbbMp4kSRXTx4BdE7ptSQdcTBOtU3RildOCdXQNFVE+Xc8LVIABL8tNK +0Fmip2QJEgjUFKgziLs5/XLRGL1JL5+sIrtK49XlwSazLDAdDDQdfC4f0YPnoQQk +TrTBMoe0z98iMcYkAI6ffRxJTZvTmFtX0eYLSgIj/KmNCRlSGouLpjmV3yLu5hI1 +LU4KOteqAiZyuxNDntFWD8tkJrX8qLUn9WHsZ0ud+9HvRQ8A5YMk6YPJSMf1KvF9 +TjiKvg3asb2CwDe3y0fG -----END CERTIFICATE----- diff --git a/test/fixtures/agenttest-key.pem b/test/fixtures/agenttest-key.pem index 77abd3f..f3fcc34 100644 --- a/test/fixtures/agenttest-key.pem +++ b/test/fixtures/agenttest-key.pem @@ -1,15 +1,28 @@ ------BEGIN RSA PRIVATE KEY----- -MIICXgIBAAKB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AoGBAJcC ++yQ9hJ1mWBRD3crUP/5Vzokq+5MIie3WOWwcUoehN6ig3y50pMN1Kfayq9aXBeJ3 +R61W0uC+rJdfD2H30yChQgKlAL8SZdlt2ZVCcA6RSPIQJtRb4Em7ErXZpIdgrGXP +I4TWpGmRkIMGMfP+71zoXAwqTrWbKnAwzV181a1lAoGBAJd7BCyoK6+5BiUzkd0d +asTVaV/BYDWdVrFFr78vjVoscqT66+yBmX3xo65wMHkw0yaRNWfKqE/1yseczQgi +Q3IZCM9YIs+JIGP0mzNzK0apZTiEUwGEDnNvvK41hEMPfLypTBUyAM/mz0E9SyS8 +2MTb4gwiIdfW8sgapxzGER7l +-----END PRIVATE KEY----- diff --git a/test/fixtures/ca.key b/test/fixtures/ca.key index 894487d..043142a 100644 --- a/test/fixtures/ca.key +++ b/test/fixtures/ca.key @@ -1,28 +1,28 @@ -----BEGIN PRIVATE KEY----- -MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDirhR/SK8uxDp3 -pZANtV3IeTKFwLdA2G6xNgo7isgCG+OnzEN8zKePSL6vSoUJnYH5KYmNOlO2zc0b -qrvUQWd375WB/zl3MEjJS1+taLxyO+lFlq7fPoB/V4XO+s0l2cA4PyGAkCcisJ5s -LCI2oaxrbjkOPzHC4W05Cj8Svn440kf/2faeHYIdhHGVs36t6nb5d2WnivPvmucp -FOShhK+LFBxL7AQIdoeDdpkjdukT9CnnRT5S1LUq6ReXzjhYqhGU+nmn6GqznpPn -cnZFIRdBQvM3b5BL2NLb5VzglUa2fNYqYGKNGgzj9G/depW8QHOxk3VUJS7vaY0z -PlkTgPO3AgMBAAECggEBAMk4Pnm+y8N37W7ISVfh555N98tDh2jIt3oXvn2cdG9q -0mvhpwbhpNxMdvij7fTbHMVRWglD/YwIpEorBREl/fM5ej4rkZd8BSCrCAOnNaEy -DaD4YcNKeEaSKvXRLMqswTUs1VCKpjLlFbxwcO6OFcBH194Nut6DvbEkp1i8QM+u -LwF4S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RvmV/NwKyTx5NDZoPwM9 +yKupukGvTwKBgQCP05xrIoB2J54r31pfH0gyrZGe0g5W+dYrBX0lydeQivL/SipW ++xh7107pM7IAJFj1zwhqXWoJ6qnSxFKEMfza1cw/5LOncIC0ZC3TAkcIqqSg7ILR +7/87ysQs4dNSRNbhyaqoDER56q25/fIt5y5uYSat27PRW57G0ly4X5Tu3wKBgGZJ +A9X/mBrRXely7wtySC5oX9e/bmJBBjz6B7UekCeDPjZKY0pot4MnRR9l1icvYuC4 +3hbOPUrFWx4v/XyPTLLq9F7AvEkg3fJuDhHspdqhxxy0BkFDzCjtBMPgiPkWJ4H2 +BaEAa/B7UtBYZ5Mu8mYE8U0w4tK9mHgiloo43l71AoGBAKJcF1MuHvddV7SzyK7j +Hf7QdPzsLrPrqH6k9L1yVP1RsouHEgoHh63ot3cZBJEtDodjw9ISoiT4Z1SeGCva +BSXYYjKlwo6ik6kFdy237VJu7XorEmpzhK+K2hoG7azyVJG3imsKCWHW7N75e+Id +A+8fxkdCTHA8dB0pk4Rdth1l -----END PRIVATE KEY----- diff --git a/test/fixtures/ca.pem b/test/fixtures/ca.pem index 1ca481b..559a01a 100644 --- a/test/fixtures/ca.pem +++ b/test/fixtures/ca.pem @@ -1,20 +1,21 @@ -----BEGIN CERTIFICATE----- -MIIDPjCCAiYCCQDsuRUq6wfGEDANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJT -RTESMBAGA1UECAwJU3RvY2tob2xtMRIwEAYDVQQHDAlTdG9ja2hvbG0xCzAJBgNV -BAMMAmNhMR0wGwYJKoZIhvcNAQkBFg5jYUBleGFtcGxlLmNvbTAeFw0xNzEwMjAx -MTM0MDhaFw00NTAzMDYxMTM0MDhaMGExCzAJBgNVBAYTAlNFMRIwEAYDVQQIDAlT -dG9ja2hvbG0xEjAQBgNVBAcMCVN0b2NraG9sbTELMAkGA1UEAwwCY2ExHTAbBgkq -hkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMIIBIjANBgkqhk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QBgNVBAcMCVN0b2NraG9sbTEL +MAkGA1UEAwwCY2ExHTAbBgkqhkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMIIBIjAN +BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwVsde50ntc8fPzk0k+k+U7WJ/pYP +b+BQhOfqr7pHoKiHsxu/TuieH4xya+g2oJDJWIxnnSTrmXeTUo2ToG+mc/GUVZ5s +RhYX53buT5xUE20qxA2SkBAffxgy7z4Zq28jLSr4/vJtrPa1B4wgPqO+uO+qNuS5 +QPBkz9R6mRY96CtJz7Arrg6xMu2mWE3awyiPhVO8bGRyWa6xED6wW9OFbXZRZ4zg +SdVHgFrvbsqRSTGi86KYYIyEGWOlxV1imBFeeTCYLnZrqwvrCN5U6YJzTR7r46Sm +ecWeqSz9DuKVrOxqlgStdXzNG02MVHH+15Z79MrhOyt330WEk9hgm28V9QIDAQAB +oyEwHzAdBgNVHQ4EFgQUHsN9t35DW4U6wZYQ0jOEVIDp7xMwDQYJKoZIhvcNAQEL +BQADggEBAA6peynGrNJfiD9yVvtZTfmnuAUTHlHJ9iUMmugo65J9D27ZgStgGTqE +CwvhoYH+fbeJBB/WrWns48piWYXOUSoyPhCI/hGqGl4uu/d66OBBHLlOE6v2W5x0 +EsOxAsTkobcG4y9CBgnoVdzcBuO+wfUFQDZYJkXFYYvK2GxJqJBTlJSqWITLute9 +6e8BjytBzZnbI9AIopl+pt+ErMDQfkS1WIHDyg4ER0gpLDrQeJq0JxGuK/d6giYy +hDtsGhz2ibk4DWVxYAlUIPwkvAnDMGW2AwbdAiX7HyqO2OqGwwberhiGZDXqLSoL +LYor6j123IpnraOTz/NQjFMQ7ZyHVAs= -----END CERTIFICATE----- diff --git a/test/fixtures/genkey.sh b/test/fixtures/genkey.sh index 2a86e69..667216b 100755 --- a/test/fixtures/genkey.sh +++ b/test/fixtures/genkey.sh @@ -6,11 +6,11 @@ openssl req -new -x509 -nodes -days 9999 -config ca.cnf -keyout ca.key -out ca.p # Generate server key # openssl genrsa -out server.key 2048 -openssl genrsa -out agenttest-key.pem 1024 +openssl genrsa -out agenttest-key.pem 2048 # Generate a certificate signing request for server.key # openssl req -new -key server.key -out server.csr -openssl req -new -key agenttest-key.pem -out agenttest.csr -config server.cnf +openssl req -new -key agenttest-key.pem -out agenttest.csr -config server.cnf # Sign the csr with the ca certificate, generating server.pem openssl x509 -req -extfile server.cnf -days 999 -passin "pass:password" -extensions v3_req -in agenttest.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out agenttest-cert.pem diff --git a/test/fixtures/ts/index.ts b/test/fixtures/ts/index.ts index 8ec2af0..e7f5205 100644 --- a/test/fixtures/ts/index.ts +++ b/test/fixtures/ts/index.ts @@ -1,19 +1,18 @@ -import http from 'http'; -import Agent from '../../..'; -import assert from 'assert'; +import * as http from 'http'; +import { constants, HttpAgent, HttpsAgent, HttpOptions, HttpsOptions, AgentStatus } from '../../..'; +import * as assert from 'assert'; -const constants = Agent.constants; assert(constants.CREATE_ID); assert(constants.CREATE_HTTPS_CONNECTION); assert(constants.CURRENT_ID); -const httpOpt: Agent.HttpOptions = { +const httpOpt: HttpOptions = { maxSockets: 100, maxFreeSockets: 10, timeout: 60000, // active socket keepalive for 60 seconds freeSocketTimeout: 30000, // free socket keepalive for 30 seconds }; -const keepaliveAgent = new Agent(httpOpt); +const keepaliveAgent = new HttpAgent(httpOpt); const options = { host: 'cnodejs.org', @@ -40,19 +39,18 @@ req.end(); setTimeout(() => { if (keepaliveAgent.statusChanged) { - const httpAgentStatus: Agent.AgentStatus = keepaliveAgent.getCurrentStatus(); + const httpAgentStatus: AgentStatus = keepaliveAgent.getCurrentStatus(); console.log('[%s] agent status changed: %j', Date(), httpAgentStatus); } }, 2000); // https -const HttpsAgent = Agent.HttpsAgent; -const httpsOpt: Agent.HttpsOptions = { +const httpsOpt: HttpsOptions = { maxSockets: 100, maxFreeSockets: 10, timeout: 60000, // active socket keepalive for 60 seconds freeSocketTimeout: 30000, // free socket keepalive for 30 seconds }; const keepaliveHttpsAgent = new HttpsAgent(httpsOpt); -const httpsAgentStatus: Agent.AgentStatus = keepaliveHttpsAgent.getCurrentStatus(); +const httpsAgentStatus: AgentStatus = keepaliveHttpsAgent.getCurrentStatus(); assert(httpsAgentStatus); diff --git a/test/fixtures/ts/tsconfig.json b/test/fixtures/ts/tsconfig.json index cbb7358..52a4051 100644 --- a/test/fixtures/ts/tsconfig.json +++ b/test/fixtures/ts/tsconfig.json @@ -21,7 +21,6 @@ "skipLibCheck": false, "skipDefaultLibCheck": false, "importHelpers": false, - "inlineSourceMap": true, - "esModuleInterop": true + "inlineSourceMap": true } } diff --git a/test/agent.test.js b/test/http_agent.test.js similarity index 96% rename from test/agent.test.js rename to test/http_agent.test.js index e161ab8..4e1228b 100644 --- a/test/agent.test.js +++ b/test/http_agent.test.js @@ -5,7 +5,7 @@ const http = require('http'); const urlparse = require('url').parse; const pedding = require('pedding'); const mm = require('mm'); -const Agent = require('..'); +const HttpAgent = require('..').HttpAgent; const { CURRENT_ID, SOCKET_NAME, @@ -15,7 +15,7 @@ const { } = require('..').constants; describe('test/agent.test.js', () => { - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ keepAliveTimeout: 1000, maxSockets: 5, maxFreeSockets: 5, @@ -105,7 +105,9 @@ describe('test/agent.test.js', () => { req.end(); }); - it('should request with connection: close with http.Agent()', done => { + // NOTE: The test is irrelevant since NodeJS >= v19 as behavior is changed and it has default `keep-alive` + // see https://nodejs.org/en/blog/announcements/v19-release-announce#https11-keepalive-by-default + it.skip('should request with connection: close with http.Agent()', done => { const req = http.request({ method: 'GET', port, @@ -127,7 +129,7 @@ describe('test/agent.test.js', () => { it('should destroy inactivity socket timeout by agent itself', done => { const name = 'localhost:' + port + ':'; - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ freeSocketKeepAliveTimeout: '5s', timeout: '1s', }); @@ -178,7 +180,7 @@ describe('test/agent.test.js', () => { it('should let request handle the socket timeout', done => { const name = 'localhost:' + port + ':'; - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ freeSocketKeepAliveTimeout: '5s', timeout: '1s', }); @@ -272,7 +274,7 @@ describe('test/agent.test.js', () => { }); it('should mock CURRENT_ID cross MAX_SAFE_INTEGER', _done => { - const agent = new Agent({ + const agent = new HttpAgent({ timeout: 1000, freeSocketTimeout: 1000, maxSockets: 10, @@ -306,7 +308,7 @@ describe('test/agent.test.js', () => { }); it('should work on timeout same as freeSocketTimeout', done => { - const agent = new Agent({ + const agent = new HttpAgent({ timeout: 1000, freeSocketTimeout: 1000, }); @@ -344,7 +346,7 @@ describe('test/agent.test.js', () => { }); it('should work on freeSocketTimeout = 0', done => { - const agent = new Agent({ + const agent = new HttpAgent({ timeout: 100, freeSocketTimeout: 0, }); @@ -380,7 +382,7 @@ describe('test/agent.test.js', () => { }); it('should createConnection error', done => { - const agent = new Agent(); + const agent = new HttpAgent(); mm.error(require('http').Agent.prototype, 'createConnection', 'mock createConnection error'); http.get({ agent, @@ -394,7 +396,7 @@ describe('test/agent.test.js', () => { }); it('should keepSocketAlive return false, no use any socket', done => { - const agent = new Agent(); + const agent = new HttpAgent(); mm(require('http').Agent.prototype, 'keepSocketAlive', () => { return false; }); @@ -423,7 +425,7 @@ describe('test/agent.test.js', () => { }); it('should agent emit socket error event', done => { - const agent = new Agent({ + const agent = new HttpAgent({ timeout: 100, }); const req = http.get({ @@ -446,7 +448,7 @@ describe('test/agent.test.js', () => { it('should mock socket error', done => { done = pedding(2, done); - const agent = new Agent({ + const agent = new HttpAgent({ timeout: 100, }); const req = http.get({ @@ -519,7 +521,7 @@ describe('test/agent.test.js', () => { }); it('should remove keepalive socket when server side destroy()', done => { - const agent = new Agent({ + const agent = new HttpAgent({ keepAliveTimeout: 1000, maxSockets: 5, maxFreeSockets: 5, @@ -570,7 +572,7 @@ describe('test/agent.test.js', () => { }); it('should remove socket when socket.destroy()', done => { - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ freeSocketTimeout: 1000, maxSockets: 5, maxFreeSockets: 5, @@ -603,7 +605,7 @@ describe('test/agent.test.js', () => { }); it('should use new socket when hit the max keepalive time: 1000ms', done => { - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ freeSocketTimeout: 1000, maxSockets: 5, maxFreeSockets: 5, @@ -651,7 +653,7 @@ describe('test/agent.test.js', () => { it('should disable keepalive when keepAlive=false', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ keepAlive: false, }); assert(agent.keepAlive === false); @@ -678,7 +680,7 @@ describe('test/agent.test.js', () => { }); it('should not keepalive when client.abort()', done => { - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ freeSocketTimeout: 1000, maxSockets: 5, maxFreeSockets: 5, @@ -706,7 +708,7 @@ describe('test/agent.test.js', () => { it('should keep 1 socket', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ maxSockets: 1, maxFreeSockets: 1, }); @@ -762,7 +764,7 @@ describe('test/agent.test.js', () => { it('should keep 1 free socket', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ maxSockets: 2, maxFreeSockets: 1, }); @@ -814,7 +816,7 @@ describe('test/agent.test.js', () => { it('should keep 2 free socket', done => { done = pedding(2, done); const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ maxSockets: 2, maxFreeSockets: 2, }); @@ -888,7 +890,7 @@ describe('test/agent.test.js', () => { it('should fire req timeout callback the first use socket', done => { done = pedding(2, done); - const agent = new Agent({ + const agent = new HttpAgent({ maxSockets: 2, maxFreeSockets: 2, }); @@ -924,7 +926,7 @@ describe('test/agent.test.js', () => { it('should fire req timeout callback the second use socket', done => { done = pedding(2, done); - const agent = new Agent({ + const agent = new HttpAgent({ maxSockets: 2, maxFreeSockets: 2, }); @@ -965,7 +967,7 @@ describe('test/agent.test.js', () => { it('should free socket timeout work', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ keepAliveTimeout: 100, }); @@ -998,7 +1000,7 @@ describe('test/agent.test.js', () => { it('should first use working socket timeout', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ timeout: 100, }); http.get({ @@ -1018,7 +1020,7 @@ describe('test/agent.test.js', () => { it('should reuse working socket timeout', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ timeout: 100, }); http.get({ @@ -1050,7 +1052,7 @@ describe('test/agent.test.js', () => { it('should destroy free socket before timeout', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent(); + const agent = new HttpAgent(); let lastPort = null; http.get({ agent, @@ -1094,7 +1096,7 @@ describe('test/agent.test.js', () => { it('should remove error socket and create new one handle pedding request', done => { done = pedding(2, done); const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ maxSockets: 1, maxFreeSockets: 1, }); @@ -1143,7 +1145,7 @@ describe('test/agent.test.js', () => { it('should destroy all sockets when freeSockets is empty', done => { done = pedding(2, done); const name = 'localhost:' + port + ':'; - const agent = new Agent(); + const agent = new HttpAgent(); http.get({ agent, port, @@ -1175,7 +1177,7 @@ describe('test/agent.test.js', () => { it('should destroy both sockets and freeSockets', done => { done = pedding(2, done); const name = 'localhost:' + port + ':'; - const agent = new Agent(); + const agent = new HttpAgent(); http.get({ agent, port, @@ -1211,7 +1213,7 @@ describe('test/agent.test.js', () => { it('should keep max sockets: bugfix for orginal keepalive agent', _done => { const name = 'localhost:' + port + ':'; - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ maxSockets: 2, maxFreeSockets: 2, }); @@ -1254,7 +1256,7 @@ describe('test/agent.test.js', () => { it('should make sure max sockets limit work', _done => { const name = 'localhost:' + port + ':'; - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ maxSockets: 2, maxFreeSockets: 2, }); @@ -1332,7 +1334,7 @@ describe('test/agent.test.js', () => { it('should timeout and remove free socket', done => { done = pedding(2, done); const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ maxSockets: 1, maxFreeSockets: 1, freeSocketTimeout: 1000, @@ -1382,7 +1384,7 @@ describe('test/agent.test.js', () => { it('should not open more sockets than maxSockets when request success', done => { done = pedding(3, done); const name = 'localhost:' + port + ':'; - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ keepAlive: true, keepAliveTimeout: 1000, maxSockets: 1, @@ -1449,7 +1451,7 @@ describe('test/agent.test.js', () => { it('should not open more sockets than maxSockets when request timeout', done => { const name = 'localhost:' + port + ':'; - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ keepAlive: true, timeout: 1000, maxSockets: 1, @@ -1494,7 +1496,7 @@ describe('test/agent.test.js', () => { }); it('should set req.reusedSocket to true when reuse socket', done => { - const agent = new Agent({ + const agent = new HttpAgent({ keepAlive: true, }); @@ -1532,7 +1534,7 @@ describe('test/agent.test.js', () => { describe('request timeout > agent timeout', () => { it('should use request timeout', done => { - const agent = new Agent({ + const agent = new HttpAgent({ keepAlive: true, timeout: 1000, }); @@ -1564,7 +1566,7 @@ describe('test/agent.test.js', () => { describe('keepAlive = false', () => { it('should close socket after request', done => { const name = 'localhost:' + port + ':'; - const agent = new Agent({ + const agent = new HttpAgent({ keepAlive: false, }); http.get({ @@ -1598,7 +1600,7 @@ describe('test/agent.test.js', () => { describe('getter statusChanged', () => { it('should get statusChanged', () => { - const agentkeepalive = new Agent({ + const agentkeepalive = new HttpAgent({ keepAliveTimeout: 1000, maxSockets: 5, maxFreeSockets: 5, @@ -1645,7 +1647,7 @@ describe('test/agent.test.js', () => { describe('mock idle socket error', () => { it('should idle socket emit error event', done => { - const agent = new Agent(); + const agent = new HttpAgent(); const options = { host: 'r.cnpmjs.org', @@ -1687,7 +1689,7 @@ describe('test/agent.test.js', () => { describe('options.socketActiveTTL', () => { it('should expire on free socket timeout when it is out of ttl', done => { - const agent = new Agent({ + const agent = new HttpAgent({ keepAlive: true, maxSockets: 5, maxFreeSockets: 5, @@ -1728,7 +1730,7 @@ describe('test/agent.test.js', () => { }); it('should expire on socket reuse detect when it is out of ttl', done => { - const agent = new Agent({ + const agent = new HttpAgent({ keepAlive: true, socketActiveTTL: 10, }); @@ -1764,7 +1766,7 @@ describe('test/agent.test.js', () => { }); it('should not expire active socket when it is in ttl', done => { - const agent = new Agent({ + const agent = new HttpAgent({ socketActiveTTL: 1000, }); const req1 = http.get({ @@ -1802,7 +1804,7 @@ describe('test/agent.test.js', () => { }); it('should TTL diff > freeSocketTimeout', done => { - const agent = new Agent({ + const agent = new HttpAgent({ freeSocketTimeout: 500, socketActiveTTL: 1000, }); diff --git a/test/server_timeout.test.js b/test/server_timeout.test.js index 5c50d5b..371e178 100644 --- a/test/server_timeout.test.js +++ b/test/server_timeout.test.js @@ -2,7 +2,7 @@ const assert = require('assert'); const http = require('http'); -const Agent = require('..'); +const HttpAgent = require('..').HttpAgent; describe('test/server_timeout.test.js', () => { let port; @@ -30,7 +30,7 @@ describe('test/server_timeout.test.js', () => { }); it('should handle Keep-Alive header and not throw reset error', done => { - const keepaliveAgent = new Agent({ + const keepaliveAgent = new HttpAgent({ keepAlive: true, }); diff --git a/test/test-ECONNRESET.test.js b/test/test-ECONNRESET.test.js index 730905b..8fe5ed9 100644 --- a/test/test-ECONNRESET.test.js +++ b/test/test-ECONNRESET.test.js @@ -2,7 +2,7 @@ const assert = require('assert'); const http = require('http'); -const Agent = require('..'); +const HttpAgent = require('..').HttpAgent; const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); @@ -27,7 +27,7 @@ describe('test/test-ECONNRESET.test.js', () => { }); it('should resolved socket hang up and ECONNRESET errors', done => { - const keepaliveAgent = new Agent({ + const keepaliveAgent = new HttpAgent({ keepAlive: true, freeSocketTimeout: 900, });