fix: use Node.js https agent when endpoint uses https (#12692)
This commit is contained in:
@ -98,6 +98,8 @@ function generateConfig(configType) {
|
|||||||
'bs58',
|
'bs58',
|
||||||
'buffer-layout',
|
'buffer-layout',
|
||||||
'crypto-hash',
|
'crypto-hash',
|
||||||
|
'http',
|
||||||
|
'https',
|
||||||
'jayson/lib/client/browser',
|
'jayson/lib/client/browser',
|
||||||
'node-fetch',
|
'node-fetch',
|
||||||
'rpc-websockets',
|
'rpc-websockets',
|
||||||
|
@ -1,19 +1,31 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import {Agent} from 'http';
|
import http from 'http';
|
||||||
|
import https from 'https';
|
||||||
|
|
||||||
export const DESTROY_TIMEOUT_MS = 5000;
|
export const DESTROY_TIMEOUT_MS = 5000;
|
||||||
|
|
||||||
export class AgentManager {
|
export class AgentManager {
|
||||||
_agent: Agent = AgentManager._newAgent();
|
_agent: http.Agent | https.Agent;
|
||||||
_activeRequests = 0;
|
_activeRequests = 0;
|
||||||
_destroyTimeout: TimeoutID | null = null;
|
_destroyTimeout: TimeoutID | null = null;
|
||||||
|
_useHttps: boolean;
|
||||||
|
|
||||||
static _newAgent(): Agent {
|
static _newAgent(useHttps: boolean): http.Agent | https.Agent {
|
||||||
return new Agent({keepAlive: true, maxSockets: 25});
|
const options = {keepAlive: true, maxSockets: 25};
|
||||||
|
if (useHttps) {
|
||||||
|
return new https.Agent(options);
|
||||||
|
} else {
|
||||||
|
return new http.Agent(options);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requestStart(): Agent {
|
constructor(useHttps?: boolean) {
|
||||||
|
this._useHttps = useHttps === true;
|
||||||
|
this._agent = AgentManager._newAgent(this._useHttps);
|
||||||
|
}
|
||||||
|
|
||||||
|
requestStart(): http.Agent | https.Agent {
|
||||||
// $FlowExpectedError - Don't manage agents in the browser
|
// $FlowExpectedError - Don't manage agents in the browser
|
||||||
if (process.browser) return;
|
if (process.browser) return;
|
||||||
|
|
||||||
@ -31,7 +43,7 @@ export class AgentManager {
|
|||||||
if (this._activeRequests === 0 && this._destroyTimeout === null) {
|
if (this._activeRequests === 0 && this._destroyTimeout === null) {
|
||||||
this._destroyTimeout = setTimeout(() => {
|
this._destroyTimeout = setTimeout(() => {
|
||||||
this._agent.destroy();
|
this._agent.destroy();
|
||||||
this._agent = AgentManager._newAgent();
|
this._agent = AgentManager._newAgent(this._useHttps);
|
||||||
}, DESTROY_TIMEOUT_MS);
|
}, DESTROY_TIMEOUT_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -505,8 +505,8 @@ type ConfirmedBlock = {
|
|||||||
}>,
|
}>,
|
||||||
};
|
};
|
||||||
|
|
||||||
function createRpcRequest(url): RpcRequest {
|
function createRpcRequest(url: string, useHttps: boolean): RpcRequest {
|
||||||
const agentManager = new AgentManager();
|
const agentManager = new AgentManager(useHttps);
|
||||||
const server = jayson(async (request, callback) => {
|
const server = jayson(async (request, callback) => {
|
||||||
const agent = agentManager.requestStart();
|
const agent = agentManager.requestStart();
|
||||||
const options = {
|
const options = {
|
||||||
@ -1453,8 +1453,9 @@ export class Connection {
|
|||||||
*/
|
*/
|
||||||
constructor(endpoint: string, commitment: ?Commitment) {
|
constructor(endpoint: string, commitment: ?Commitment) {
|
||||||
let url = urlParse(endpoint);
|
let url = urlParse(endpoint);
|
||||||
|
const useHttps = url.protocol === 'https:';
|
||||||
|
|
||||||
this._rpcRequest = createRpcRequest(url.href);
|
this._rpcRequest = createRpcRequest(url.href, useHttps);
|
||||||
this._commitment = commitment;
|
this._commitment = commitment;
|
||||||
this._blockhashInfo = {
|
this._blockhashInfo = {
|
||||||
recentBlockhash: null,
|
recentBlockhash: null,
|
||||||
@ -1463,7 +1464,7 @@ export class Connection {
|
|||||||
simulatedSignatures: [],
|
simulatedSignatures: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
url.protocol = useHttps ? 'wss:' : 'ws:';
|
||||||
url.host = '';
|
url.host = '';
|
||||||
// Only shift the port by +1 as a convention for ws(s) only if given endpoint
|
// Only shift the port by +1 as a convention for ws(s) only if given endpoint
|
||||||
// is explictly specifying the endpoint port (HTTP-based RPC), assuming
|
// is explictly specifying the endpoint port (HTTP-based RPC), assuming
|
||||||
|
@ -2515,3 +2515,14 @@ test('root notification', async () => {
|
|||||||
expect(roots[1]).toBeGreaterThan(roots[0]);
|
expect(roots[1]).toBeGreaterThan(roots[0]);
|
||||||
await connection.removeRootChangeListener(subscriptionId);
|
await connection.removeRootChangeListener(subscriptionId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('https request', async () => {
|
||||||
|
if (mockRpcEnabled) {
|
||||||
|
console.log('non-live test skipped');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const connection = new Connection('https://devnet.solana.com');
|
||||||
|
const version = await connection.getVersion();
|
||||||
|
expect(version['solana-core']).toBeTruthy();
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user