Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit ac4bb48

Browse files
authored
fix: use https agent for https requests (#3490)
Fixes regression introduced in #3474 - if we make https requests, use a https agent.
1 parent 9af28b3 commit ac4bb48

File tree

8 files changed

+44
-11
lines changed

8 files changed

+44
-11
lines changed

packages/ipfs-grpc-client/src/grpc/transport.node.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
const WebSocket = require('ws')
88
const debug = require('debug')('ipfs:grpc-client:websocket-transport')
99

10+
/**
11+
* @typedef {import('http').Agent} HttpAgent
12+
* @typedef {import('https').Agent} HttpsAgent
13+
*/
14+
1015
const WebsocketSignal = {
1116
FINISH_SEND: 1
1217
}
@@ -15,7 +20,7 @@ const finishSendFrame = new Uint8Array([1])
1520

1621
/**
1722
* @param {object} options
18-
* @param {import('http').Agent} [options.agent] - http.Agent used to control HTTP client behaviour
23+
* @param {HttpAgent|HttpsAgent} [options.agent] - http.Agent used to control HTTP client behaviour
1924
*/
2025
function WebsocketTransport (options) {
2126
/**
@@ -33,7 +38,7 @@ function WebsocketTransport (options) {
3338

3439
/**
3540
* @typedef {object} NodeTransportOptions
36-
* @property {import('http').Agent} [options.agent]
41+
* @property {HttpAgent|HttpsAgent} [options.agent]
3742
*
3843
* @typedef {NodeTransportOptions & import('@improbable-eng/grpc-web').grpc.TransportOptions} WebSocketTransportOptions
3944
*

packages/ipfs-grpc-client/src/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ function normaliseUrls (opts) {
2020
}
2121

2222
/**
23+
* @typedef {import('http').Agent} HttpAgent
24+
* @typedef {import('https').Agent} HttpsAgent
25+
*
2326
* @param {object} opts
2427
* @param {string} opts.url - The URL to connect to as a URL or Multiaddr
25-
* @param {import('http').Agent} [opts.agent] - http.Agent used to control HTTP client behaviour (node.js only)
28+
* @param {HttpAgent|HttpsAgent} [opts.agent] - http.Agent used to control HTTP client behaviour (node.js only)
2629
*/
2730
module.exports = function createClient (opts = { url: '' }) {
2831
opts.url = toUrlString(opts.url)

packages/ipfs-grpc-client/src/utils/bidi-to-duplex.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const errCode = require('err-code')
55
const toHeaders = require('./to-headers')
66
const transport = require('../grpc/transport')
77

8+
/**
9+
* @typedef {import('http').Agent} HttpAgent
10+
* @typedef {import('https').Agent} HttpsAgent
11+
*/
12+
813
async function sendMessages (service, client, source) {
914
for await (const obj of source) {
1015
client.send({
@@ -24,7 +29,7 @@ async function sendMessages (service, client, source) {
2429
* @param {string} options.host - The remote host
2530
* @param {boolean} [options.debug] - Whether to print debug messages
2631
* @param {object} [options.metadata] - Metadata sent as headers
27-
* @param {import('http').Agent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
32+
* @param {HttpAgent|HttpsAgent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
2833
* @returns {{ source: AsyncIterable<object>, sink: { push: Function, end: Function } }}
2934
**/
3035
module.exports = function bidiToDuplex (grpc, service, options) {

packages/ipfs-grpc-client/src/utils/client-stream-to-promise.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
const first = require('it-first')
44
const bidiToDuplex = require('./bidi-to-duplex')
55

6+
/**
7+
* @typedef {import('http').Agent} HttpAgent
8+
* @typedef {import('https').Agent} HttpsAgent
9+
*/
10+
611
/**
712
* Client streaming methods are a many-to-one operation so this
813
* function takes a source that can emit multiple messages and
@@ -15,7 +20,7 @@ const bidiToDuplex = require('./bidi-to-duplex')
1520
* @param {string} options.host - The remote host
1621
* @param {boolean} [options.debug] - Whether to print debug messages
1722
* @param {object} [options.metadata] - Metadata sent as headers
18-
* @param {import('http').Agent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
23+
* @param {HttpAgent|HttpsAgent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
1924
* @returns {Promise<Object>} - A promise that resolves to a response object
2025
**/
2126
module.exports = async function clientStreamToPromise (grpc, service, source, options) {

packages/ipfs-grpc-client/src/utils/server-stream-to-iterator.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
const bidiToDuplex = require('./bidi-to-duplex')
44

5+
/**
6+
* @typedef {import('http').Agent} HttpAgent
7+
* @typedef {import('https').Agent} HttpsAgent
8+
*/
9+
510
/**
611
* Server stream methods are one-to-many operations so this
712
* function accepts a client message and returns a source
@@ -14,7 +19,7 @@ const bidiToDuplex = require('./bidi-to-duplex')
1419
* @param {string} options.host - The remote host
1520
* @param {boolean} [options.debug] - Whether to print debug messages
1621
* @param {object} [options.metadata] - Metadata sent as headers
17-
* @param {import('http').Agent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
22+
* @param {HttpAgent|HttpsAgent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
1823
* @returns {AsyncIterable<object>}
1924
**/
2025
module.exports = function serverStreamToIterator (grpc, service, request, options) {

packages/ipfs-grpc-client/src/utils/unary-to-promise.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
const first = require('it-first')
44
const bidiToDuplex = require('./bidi-to-duplex')
55

6+
/**
7+
* @typedef {import('http').Agent} HttpAgent
8+
* @typedef {import('https').Agent} HttpsAgent
9+
*/
10+
611
/**
712
* Unary calls are one-to-one operations so this function
813
* takes a client message and returns a promise that resolves
@@ -15,7 +20,7 @@ const bidiToDuplex = require('./bidi-to-duplex')
1520
* @param {string} options.host - The remote host
1621
* @param {boolean} [options.debug] - Whether to print debug messages
1722
* @param {object} [options.metadata] - Metadata sent as headers
18-
* @param {import('http').Agent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
23+
* @param {HttpAgent|HttpsAgent} [options.agent] - http.Agent used to control HTTP client behaviour (node.js only)
1924
* @returns {Promise<Object>} - A promise that resolves to a response object
2025
**/
2126
module.exports = function unaryToPromise (grpc, service, request, options) {

packages/ipfs-http-client/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"ipfs-utils/src/files/glob-source": false,
2020
"go-ipfs": false,
2121
"ipfs-core-utils/src/files/normalise-input": "ipfs-core-utils/src/files/normalise-input/index.browser.js",
22-
"http": false
22+
"http": false,
23+
"https": false
2324
},
2425
"typesVersions": {
2526
"*": {

packages/ipfs-http-client/src/lib/core.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const HTTP = require('ipfs-utils/src/http')
88
const merge = require('merge-options')
99
const toUrlString = require('ipfs-core-utils/src/to-url-string')
1010
const http = require('http')
11+
const https = require('https')
1112

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

5152
if (isNode) {
52-
agent = opts.agent || new http.Agent({
53+
const Agent = url.protocol.startsWith('https') ? https.Agent : http.Agent
54+
55+
agent = opts.agent || new Agent({
5356
keepAlive: true,
5457
// Similar to browsers which limit connections to six per host
5558
maxSockets: 6
@@ -116,7 +119,8 @@ const parseTimeout = (value) => {
116119
}
117120

118121
/**
119-
* @typedef {import('http').Agent} Agent
122+
* @typedef {import('http').Agent} HttpAgent
123+
* @typedef {import('https').Agent} HttpsAgent
120124
*
121125
* @typedef {Object} ClientOptions
122126
* @property {string} [host]
@@ -129,7 +133,7 @@ const parseTimeout = (value) => {
129133
* @property {object} [ipld]
130134
* @property {any[]} [ipld.formats] - An array of additional [IPLD formats](https://github.com/ipld/interface-ipld-format) to support
131135
* @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
132-
* @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)
136+
* @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)
133137
*/
134138
class Client extends HTTP {
135139
/**

0 commit comments

Comments
 (0)