From a8594ca6f042e80bf3e652730a92657d3cb8fd04 Mon Sep 17 00:00:00 2001 From: steveluscher Date: Tue, 23 Aug 2022 11:55:15 -0700 Subject: [PATCH] fix: `Connection` no longer relies on the `URL` class --- web3.js/src/connection.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 0fb2e5a9b19b22..080b3053a60728 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -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'; @@ -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( commitmentOrConfig?: Commitment | ({commitment?: Commitment} & TConfig), @@ -1117,7 +1124,6 @@ export type PerfSample = { function createRpcClient( url: string, - useHttps: boolean, httpHeaders?: HttpHeaders, customFetch?: FetchFn, fetchMiddleware?: FetchMiddleware, @@ -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; @@ -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; @@ -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,