From 16277f697fe689932b7eec30115c72465dc7d01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 6 Apr 2017 00:09:35 -0700 Subject: [PATCH] feat(agent): better, keepalive-supporting, default http agents --- index.js | 21 ++++++++++++++------- package.json | 1 + 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2e32a8d..db9a596 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,11 @@ let Cache const fetch = require('node-fetch') -const http = require('http') -const https = require('https') let ProxyAgent const pkg = require('./package.json') const retry = require('promise-retry') let ssri const Stream = require('stream') -const url = require('url') // https://fetch.spec.whatwg.org/#http-network-or-cache-fetch module.exports = cachingFetch @@ -311,6 +308,8 @@ function remoteFetch (uri, opts) { }) } +let httpsAgent +let httpAgent function getAgent (uri, opts) { if (opts.agent != null) { // `agent: false` has special behavior! @@ -320,10 +319,18 @@ function getAgent (uri, opts) { ProxyAgent = require('proxy-agent') } return new ProxyAgent(opts.proxy) - } else if (url.parse(uri).protocol === 'https:') { - return https.globalAgent - } else if (url.parse(uri).protocol === 'http:') { - return http.globalAgent + } else if (uri.trim().startsWith('https:')) { + if (!httpsAgent) { + const Agent = require('agentkeepalive').HttpsAgent + httpsAgent = new Agent({maxSockets: 15}) + } + return httpsAgent + } else { + if (!httpAgent) { + const Agent = require('agentkeepalive') + httpAgent = new Agent({maxSockets: 15}) + } + return httpAgent } } diff --git a/package.json b/package.json index f8f3887..f1f08ff 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "license": "CC0-1.0", "dependencies": { + "agentkeepalive": "^3.1.0", "bluebird": "^3.5.0", "cacache": "^7.0.3", "checksum-stream": "^1.0.2",