Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
feat(agent): better, keepalive-supporting, default http agents
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 6, 2017
1 parent e906fff commit 16277f6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!
Expand All @@ -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
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 16277f6

Please sign in to comment.