Fix lazy creation of ProxyAgent
Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
This commit is contained in:
parent
7b17e1d179
commit
822af04eda
1 changed files with 22 additions and 14 deletions
|
@ -86,7 +86,7 @@ export class SocketManager extends EventListener {
|
|||
|
||||
private credentials?: WebAPICredentials;
|
||||
|
||||
private proxyAgent?: ProxyAgent;
|
||||
private lazyProxyAgent?: Promise<ProxyAgent>;
|
||||
|
||||
private status = SocketStatus.CLOSED;
|
||||
|
||||
|
@ -164,6 +164,7 @@ export class SocketManager extends EventListener {
|
|||
name: AUTHENTICATED_CHANNEL_NAME,
|
||||
path: '/v1/websocket/',
|
||||
query: { login: username, password },
|
||||
proxyAgent: await this.getProxyAgent(),
|
||||
resourceOptions: {
|
||||
name: AUTHENTICATED_CHANNEL_NAME,
|
||||
keepalive: { path: '/v1/keepalive' },
|
||||
|
@ -321,6 +322,7 @@ export class SocketManager extends EventListener {
|
|||
return this.connectResource({
|
||||
name: 'provisioning',
|
||||
path: '/v1/websocket/provisioning/',
|
||||
proxyAgent: await this.getProxyAgent(),
|
||||
resourceOptions: {
|
||||
name: 'provisioning',
|
||||
handleRequest: (req: IncomingWebSocketRequest): void => {
|
||||
|
@ -339,16 +341,13 @@ export class SocketManager extends EventListener {
|
|||
url: string;
|
||||
extraHeaders?: Record<string, string>;
|
||||
}): Promise<WebSocket> {
|
||||
// Create proxy agent lazily
|
||||
if (this.options.proxyUrl && !this.proxyAgent) {
|
||||
this.proxyAgent = await createProxyAgent(this.options.proxyUrl);
|
||||
}
|
||||
const proxyAgent = await this.getProxyAgent();
|
||||
|
||||
return connectWebSocket({
|
||||
name: 'art-creator-provisioning',
|
||||
url,
|
||||
version: this.options.version,
|
||||
proxyAgent: this.proxyAgent,
|
||||
proxyAgent,
|
||||
extraHeaders,
|
||||
|
||||
createResource(socket: WebSocket): WebSocket {
|
||||
|
@ -525,15 +524,11 @@ export class SocketManager extends EventListener {
|
|||
}
|
||||
}
|
||||
|
||||
private transportOption(): TransportOption {
|
||||
private transportOption(proxyAgent: ProxyAgent | undefined): TransportOption {
|
||||
const { hostname } = URL.parse(this.options.url);
|
||||
|
||||
// transport experiment doesn't support proxy
|
||||
if (
|
||||
this.proxyAgent ||
|
||||
hostname == null ||
|
||||
!hostname.endsWith('signal.org')
|
||||
) {
|
||||
if (proxyAgent || hostname == null || !hostname.endsWith('signal.org')) {
|
||||
return TransportOption.Original;
|
||||
}
|
||||
|
||||
|
@ -590,7 +585,9 @@ export class SocketManager extends EventListener {
|
|||
|
||||
log.info('SocketManager: connecting unauthenticated socket');
|
||||
|
||||
const transportOption = this.transportOption();
|
||||
const proxyAgent = await this.getProxyAgent();
|
||||
|
||||
const transportOption = this.transportOption(proxyAgent);
|
||||
log.info(
|
||||
`SocketManager: connecting unauthenticated socket, transport option [${transportOption}]`
|
||||
);
|
||||
|
@ -603,6 +600,7 @@ export class SocketManager extends EventListener {
|
|||
const process = this.connectResource({
|
||||
name: UNAUTHENTICATED_CHANNEL_NAME,
|
||||
path: '/v1/websocket/',
|
||||
proxyAgent,
|
||||
resourceOptions: {
|
||||
name: UNAUTHENTICATED_CHANNEL_NAME,
|
||||
keepalive: { path: '/v1/keepalive' },
|
||||
|
@ -646,12 +644,14 @@ export class SocketManager extends EventListener {
|
|||
private connectResource({
|
||||
name,
|
||||
path,
|
||||
proxyAgent,
|
||||
resourceOptions,
|
||||
query = {},
|
||||
extraHeaders = {},
|
||||
}: {
|
||||
name: string;
|
||||
path: string;
|
||||
proxyAgent: ProxyAgent | undefined;
|
||||
resourceOptions: WebSocketResourceOptions;
|
||||
query?: Record<string, string>;
|
||||
extraHeaders?: Record<string, string>;
|
||||
|
@ -671,7 +671,7 @@ export class SocketManager extends EventListener {
|
|||
url,
|
||||
version,
|
||||
certificateAuthority: this.options.certificateAuthority,
|
||||
proxyAgent: this.proxyAgent,
|
||||
proxyAgent,
|
||||
|
||||
extraHeaders,
|
||||
|
||||
|
@ -876,6 +876,14 @@ export class SocketManager extends EventListener {
|
|||
);
|
||||
}
|
||||
|
||||
private async getProxyAgent(): Promise<ProxyAgent | undefined> {
|
||||
if (this.options.proxyUrl && !this.lazyProxyAgent) {
|
||||
// Cache the promise so that we don't import concurrently.
|
||||
this.lazyProxyAgent = createProxyAgent(this.options.proxyUrl);
|
||||
}
|
||||
return this.lazyProxyAgent;
|
||||
}
|
||||
|
||||
// EventEmitter types
|
||||
|
||||
public override on(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue