fix: Re-added the option to use proxy agent forn non-mobile api (#148)

This commit is contained in:
Edgard Lorraine Messias
2023-06-15 21:46:43 -03:00
committed by GitHub
parent aa9872b039
commit 54f8215eef
9 changed files with 177 additions and 78 deletions

View File

@@ -0,0 +1,19 @@
import { EventEmitter } from 'events'
import { URL } from 'url'
import { SocketConfig } from '../../Types'
export abstract class AbstractSocketClient extends EventEmitter {
abstract get isOpen(): boolean
abstract get isClosed(): boolean
abstract get isClosing(): boolean
abstract get isConnecting(): boolean
constructor(public url: URL, public config: SocketConfig) {
super()
this.setMaxListeners(0)
}
abstract connect(): Promise<void>
abstract close(): Promise<void>
abstract send(str: Uint8Array | string, cb?: (err?: Error) => void): boolean;
}

View File

@@ -0,0 +1,3 @@
export * from './abstract-socket-client'
export * from './mobile-socket-client'
export * from './web-socket-client'

View File

@@ -0,0 +1,66 @@
import { connect, Socket } from 'net'
import { AbstractSocketClient } from './abstract-socket-client'
export class MobileSocketClient extends AbstractSocketClient {
protected socket: Socket | null = null
get isOpen(): boolean {
return this.socket?.readyState === 'open'
}
get isClosed(): boolean {
return this.socket === null || this.socket?.readyState === 'closed'
}
get isClosing(): boolean {
return this.socket === null || this.socket?.readyState === 'closed'
}
get isConnecting(): boolean {
return this.socket?.readyState === 'opening'
}
async connect(): Promise<void> {
if(this.socket) {
return
}
if(this.config.agent) {
throw new Error('There are not support for proxy agent for mobile connection')
} else {
this.socket = connect({
host: this.url.hostname,
port: Number(this.url.port) || 443
})
}
this.socket.setMaxListeners(0)
const events = ['close', 'connect', 'data', 'drain', 'end', 'error', 'lookup', 'ready', 'timeout']
for(const event of events) {
this.socket?.on(event, (...args: any[]) => this.emit(event, ...args))
}
this.socket.on('data', (...args: any[]) => this.emit('message', ...args))
this.socket.on('ready', (...args: any[]) => this.emit('open', ...args))
}
async close(): Promise<void> {
if(!this.socket) {
return
}
return new Promise<void>(resolve => {
this.socket!.end(resolve)
this.socket = null
})
}
send(str: string | Uint8Array, cb?: (err?: Error) => void): boolean {
if(this.socket === null) {
return false
}
return this.socket.write(str, undefined, cb)
}
}

View File

@@ -0,0 +1,57 @@
import WebSocket from 'ws'
import { DEFAULT_ORIGIN } from '../../Defaults'
import { AbstractSocketClient } from './abstract-socket-client'
export class WebSocketClient extends AbstractSocketClient {
protected socket: WebSocket | null = null
get isOpen(): boolean {
return this.socket?.readyState === WebSocket.OPEN
}
get isClosed(): boolean {
return this.socket === null || this.socket?.readyState === WebSocket.CLOSED
}
get isClosing(): boolean {
return this.socket === null || this.socket?.readyState === WebSocket.CLOSING
}
get isConnecting(): boolean {
return this.socket?.readyState === WebSocket.CONNECTING
}
async connect(): Promise<void> {
if(this.socket) {
return
}
this.socket = new WebSocket(this.url, {
origin: DEFAULT_ORIGIN,
headers: this.config.options?.headers as {},
handshakeTimeout: this.config.connectTimeoutMs,
timeout: this.config.connectTimeoutMs,
agent: this.config.agent,
})
this.socket.setMaxListeners(0)
const events = ['close', 'error', 'upgrade', 'message', 'open', 'ping', 'pong', 'unexpected-response']
for(const event of events) {
this.socket?.on(event, (...args: any[]) => this.emit(event, ...args))
}
}
async close(): Promise<void> {
if(!this.socket) {
return
}
this.socket.close()
this.socket = null
}
send(str: string | Uint8Array, cb?: (err?: Error) => void): boolean {
this.socket?.send(str, cb)
return Boolean(this.socket)
}
}