Skip to content

Commit

Permalink
s/agentOverride/httpAgent/ is less confusing when the value is false
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Dec 6, 2022
1 parent a587267 commit 05f5f5f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
30 changes: 15 additions & 15 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,45 +1452,45 @@ function createRpcClient(
customFetch?: FetchFn,
fetchMiddleware?: FetchMiddleware,
disableRetryOnRateLimit?: boolean,
agentOverride?: HttpAgent | HttpsAgent | false,
httpAgent?: HttpAgent | HttpsAgent | false,
): RpcClient {
const fetch = customFetch ? customFetch : fetchImpl;
let agentManager:
| {requestEnd(): void; requestStart(): HttpAgent | HttpsAgent}
| undefined;
if (process.env.BROWSER) {
if (agentOverride != null) {
if (httpAgent != null) {
console.warn(
'You have supplied an `agentOverride` when creating a `Connection` in a browser ' +
'environment. It has been ignored; `agentOverride` is only used in Node environments.',
'You have supplied an `httpAgent` when creating a `Connection` in a browser environment.' +
'It has been ignored; `httpAgent` is only used in Node environments.',
);
}
} else {
if (agentOverride == null) {
if (httpAgent == null) {
if (process.env.NODE_ENV !== 'test') {
agentManager = new AgentManager(
url.startsWith('https:') /* useHttps */,
);
}
} else {
if (agentOverride !== false) {
if (httpAgent !== false) {
const isHttps = url.startsWith('https:');
if (isHttps && !(agentOverride instanceof HttpsAgent)) {
if (isHttps && !(httpAgent instanceof HttpsAgent)) {
throw new Error(
'The endpoint `' +
url +
'` can only be paired with an `https.Agent`. You have, instead, supplied an ' +
'`http.Agent` through `agentOverride`.',
'`http.Agent` through `httpAgent`.',
);
} else if (!isHttps && agentOverride instanceof HttpsAgent) {
} else if (!isHttps && httpAgent instanceof HttpsAgent) {
throw new Error(
'The endpoint `' +
url +
'` can only be paired with an `http.Agent`. You have, instead, supplied an ' +
'`https.Agent` through `agentOverride`.',
'`https.Agent` through `httpAgent`.',
);
}
agentManager = {requestEnd() {}, requestStart: () => agentOverride};
agentManager = {requestEnd() {}, requestStart: () => httpAgent};
}
}
}
Expand Down Expand Up @@ -2898,7 +2898,7 @@ export type ConnectionConfig = {
* persistence). Set this to `false` to create a connection that uses no agent. This applies to
* Node environments only.
*/
agentOverride?: HttpAgent | HttpsAgent | false;
httpAgent?: HttpAgent | HttpsAgent | false;
/** Optional commitment level */
commitment?: Commitment;
/** Optional endpoint URL to the fullnode JSON RPC PubSub WebSocket Endpoint */
Expand Down Expand Up @@ -3016,7 +3016,7 @@ export class Connection {
let fetch;
let fetchMiddleware;
let disableRetryOnRateLimit;
let agentOverride;
let httpAgent;
if (commitmentOrConfig && typeof commitmentOrConfig === 'string') {
this._commitment = commitmentOrConfig;
} else if (commitmentOrConfig) {
Expand All @@ -3028,7 +3028,7 @@ export class Connection {
fetch = commitmentOrConfig.fetch;
fetchMiddleware = commitmentOrConfig.fetchMiddleware;
disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit;
agentOverride = commitmentOrConfig.agentOverride;
httpAgent = commitmentOrConfig.httpAgent;
}

this._rpcEndpoint = assertEndpointUrl(endpoint);
Expand All @@ -3040,7 +3040,7 @@ export class Connection {
fetch,
fetchMiddleware,
disableRetryOnRateLimit,
agentOverride,
httpAgent,
);
this._rpcRequest = createRpcRequest(this._rpcClient);
this._rpcBatchRequest = createRpcBatchRequest(this._rpcClient);
Expand Down
12 changes: 6 additions & 6 deletions web3.js/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('Connection', function () {

it('uses no agent with fetch when `overrideAgent` is `false`', () => {
const fetch = spy();
const c = new Connection(url, {agentOverride: false, fetch});
const c = new Connection(url, {httpAgent: false, fetch});
c.getBlock(0);
expect(fetch).to.have.been.calledWith(
match.any,
Expand All @@ -212,24 +212,24 @@ describe('Connection', function () {

it('uses the supplied `overrideAgent` with fetch', () => {
const fetch = spy();
const agentOverride = new HttpsAgent();
const c = new Connection('https://example.com', {agentOverride, fetch});
const httpAgent = new HttpsAgent();
const c = new Connection('https://example.com', {httpAgent, fetch});
c.getBlock(0);
expect(fetch).to.have.been.calledWith(
match.any,
match({agent: agentOverride}),
match({agent: httpAgent}),
);
});

it('throws when the supplied `overrideAgent` is http but the endpoint is https', () => {
expect(() => {
new Connection('https://example.com', {agentOverride: new HttpAgent()});
new Connection('https://example.com', {httpAgent: new HttpAgent()});
}).to.throw;
});

it('throws when the supplied `overrideAgent` is https but the endpoint is http', () => {
expect(() => {
new Connection('http://example.com', {agentOverride: new HttpsAgent()});
new Connection('http://example.com', {httpAgent: new HttpsAgent()});
}).to.throw;
});
});
Expand Down

0 comments on commit 05f5f5f

Please sign in to comment.