Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
fix: Connection no longer relies on the URL class
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher committed Aug 23, 2022
1 parent 7aef7bd commit a8594ca
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions web3.js/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
TransactionExpiredTimeoutError,
} from './transaction/expiry-custom-errors';
import {makeWebsocketUrl} from './utils/makeWebsocketUrl';
import {URL} from './utils/url-impl';
import type {Blockhash} from './blockhash';
import type {FeeCalculator} from './fee-calculator';
import type {TransactionSignature} from './transaction';
Expand Down Expand Up @@ -305,6 +304,14 @@ export type BlockheightBasedTransactionConfirmationStrategy = {
signature: TransactionSignature;
} & BlockhashWithExpiryBlockHeight;

/* @internal */
function assertEndpointUrl(putativeUrl: string) {
if (/^https?:/.test(putativeUrl) === false) {
throw new TypeError('Endpoint URL must start with `http:` or `https:`.');
}
return putativeUrl;
}

/** @internal */
function extractCommitmentFromConfig<TConfig>(
commitmentOrConfig?: Commitment | ({commitment?: Commitment} & TConfig),
Expand Down Expand Up @@ -1117,7 +1124,6 @@ export type PerfSample = {

function createRpcClient(
url: string,
useHttps: boolean,
httpHeaders?: HttpHeaders,
customFetch?: FetchFn,
fetchMiddleware?: FetchMiddleware,
Expand All @@ -1126,7 +1132,7 @@ function createRpcClient(
const fetch = customFetch ? customFetch : fetchImpl;
let agentManager: AgentManager | undefined;
if (!process.env.BROWSER) {
agentManager = new AgentManager(useHttps);
agentManager = new AgentManager(url.startsWith('https:') /* useHttps */);
}

let fetchWithMiddleware: FetchFn | undefined;
Expand Down Expand Up @@ -2493,9 +2499,6 @@ export class Connection {
endpoint: string,
commitmentOrConfig?: Commitment | ConnectionConfig,
) {
let url = new URL(endpoint);
const useHttps = url.protocol === 'https:';

let wsEndpoint;
let httpHeaders;
let fetch;
Expand All @@ -2514,12 +2517,11 @@ export class Connection {
disableRetryOnRateLimit = commitmentOrConfig.disableRetryOnRateLimit;
}

this._rpcEndpoint = endpoint;
this._rpcEndpoint = assertEndpointUrl(endpoint);
this._rpcWsEndpoint = wsEndpoint || makeWebsocketUrl(endpoint);

this._rpcClient = createRpcClient(
url.toString(),
useHttps,
endpoint,
httpHeaders,
fetch,
fetchMiddleware,
Expand Down

0 comments on commit a8594ca

Please sign in to comment.