(port: number, host: string, connectListener?: (...args: unknown[]) => void)
| 32 | } |
| 33 | |
| 34 | async connect(port: number, host: string, connectListener?: (...args: unknown[]) => void) { |
| 35 | try { |
| 36 | log('connecting') |
| 37 | if (connectListener) this.once('connect', connectListener) |
| 38 | |
| 39 | const options: SocketOptions = this.ssl ? { secureTransport: 'starttls' } : {} |
| 40 | const mod = await import('cloudflare:sockets') |
| 41 | const connect = mod.connect |
| 42 | this._cfSocket = connect(`${host}:${port}`, options) |
| 43 | this._cfWriter = this._cfSocket.writable.getWriter() |
| 44 | this._addClosedHandler() |
| 45 | |
| 46 | this._cfReader = this._cfSocket.readable.getReader() |
| 47 | if (this.ssl) { |
| 48 | this._listenOnce().catch((e) => this.emit('error', e)) |
| 49 | } else { |
| 50 | this._listen().catch((e) => this.emit('error', e)) |
| 51 | } |
| 52 | |
| 53 | await this._cfWriter!.ready |
| 54 | log('socket ready') |
| 55 | this.writable = true |
| 56 | this.emit('connect') |
| 57 | |
| 58 | return this |
| 59 | } catch (e) { |
| 60 | this.emit('error', e) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | async _listen() { |
| 65 | // eslint-disable-next-line no-constant-condition |
no test coverage detected