Skip to content

Commit

Permalink
fix: use https agent for https requests (#3490)
Browse files Browse the repository at this point in the history
Fixes regression introduced in #3474 - if we make https requests,
use a https agent.
  • Loading branch information
achingbrain authored Jan 20, 2021
1 parent 31949ac commit 879e2f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"ipfs-utils/src/files/glob-source": false,
"go-ipfs": false,
"ipfs-core-utils/src/files/normalise-input": "ipfs-core-utils/src/files/normalise-input/index.browser.js",
"http": false
"http": false,
"https": false
},
"typesVersions": {
"*": {
Expand Down
10 changes: 7 additions & 3 deletions src/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const HTTP = require('ipfs-utils/src/http')
const merge = require('merge-options')
const toUrlString = require('ipfs-core-utils/src/to-url-string')
const http = require('http')
const https = require('https')

const DEFAULT_PROTOCOL = isBrowser || isWebWorker ? location.protocol : 'http'
const DEFAULT_HOST = isBrowser || isWebWorker ? location.hostname : 'localhost'
Expand Down Expand Up @@ -49,7 +50,9 @@ const normalizeOptions = (options = {}) => {
}

if (isNode) {
agent = opts.agent || new http.Agent({
const Agent = url.protocol.startsWith('https') ? https.Agent : http.Agent

agent = opts.agent || new Agent({
keepAlive: true,
// Similar to browsers which limit connections to six per host
maxSockets: 6
Expand Down Expand Up @@ -116,7 +119,8 @@ const parseTimeout = (value) => {
}

/**
* @typedef {import('http').Agent} Agent
* @typedef {import('http').Agent} HttpAgent
* @typedef {import('https').Agent} HttpsAgent
*
* @typedef {Object} ClientOptions
* @property {string} [host]
Expand All @@ -129,7 +133,7 @@ const parseTimeout = (value) => {
* @property {object} [ipld]
* @property {any[]} [ipld.formats] - An array of additional [IPLD formats](https://github.com/ipld/interface-ipld-format) to support
* @property {(format: string) => Promise<any>} [ipld.loadFormat] - an async function that takes the name of an [IPLD format](https://github.com/ipld/interface-ipld-format) as a string and should return the implementation of that codec
* @property {Agent} [agent] - A [http.Agent](https://nodejs.org/api/http.html#http_class_http_agent) used to control connection persistence and reuse for HTTP clients (only supported in node.js)
* @property {HttpAgent|HttpsAgent} [agent] - A [http.Agent](https://nodejs.org/api/http.html#http_class_http_agent) used to control connection persistence and reuse for HTTP clients (only supported in node.js)
*/
class Client extends HTTP {
/**
Expand Down

0 comments on commit 879e2f9

Please sign in to comment.