@@ -10,6 +10,8 @@ import {
1010 type AnyReply ,
1111 PROTOCOL ,
1212} from "./protocol.ts" ;
13+ import type { Conn , Runtime } from "../runtime/types.ts" ;
14+ import { getRuntime } from "../runtime/mod.ts" ;
1315
1416type AnyRawEventName = `raw:${AnyCommand | AnyReply | AnyError } `;
1517
@@ -81,28 +83,15 @@ export interface RemoteAddr {
8183/** Network address exposed in events — without sensitive fields (cert/key). */
8284export type PublicAddr = Omit < RemoteAddr , "cert" | "key" > ;
8385
84- /** How to connect to a server */
85- interface ConnectImpl {
86- noTls ( opts : Deno . ConnectOptions ) : Promise < Deno . Conn > ;
87- withTls (
88- opts :
89- | Deno . ConnectTlsOptions
90- | ( Deno . ConnectTlsOptions & Deno . TlsCertifiedKeyPem ) ,
91- ) : Promise < Deno . Conn > ;
92- }
93-
9486/** Low-level IRC client handling TCP connection, raw message parsing, and event dispatching. */
9587export class CoreClient <
9688 TEvents extends CoreFeatures [ "events" ] = CoreFeatures [ "events" ] ,
9789> extends EventEmitter < TEvents > {
9890 readonly state : CoreFeatures [ "state" ] ;
9991 readonly utils : CoreFeatures [ "utils" ] ;
10092
101- protected connectImpl : ConnectImpl = {
102- noTls : Deno . connect ,
103- withTls : Deno . connectTls ,
104- } ;
105- protected conn : Deno . Conn | null = null ;
93+ protected runtime : Runtime | null = null ;
94+ protected conn : Conn | null = null ;
10695 protected hooks : Hooks < CoreClient < TEvents > > = new Hooks < CoreClient < TEvents > > (
10796 this ,
10897 ) ;
@@ -148,26 +137,28 @@ export class CoreClient<
148137 async connect (
149138 hostname : string ,
150139 options : ConnectOptions = { } ,
151- ) : Promise < Deno . Conn | null > {
140+ ) : Promise < Conn | null > {
141+ if ( ! this . runtime ) {
142+ this . runtime = await getRuntime ( ) ;
143+ }
144+
152145 const { port = PORT } = options ;
153146 const tls = options . tls ?? false ;
154147
155148 let tlsFields : { cert ?: string ; key ?: string ; caCerts ?: string [ ] } = { } ;
156149 if ( options . tls ) {
157- const o = options as {
158- cert ?: string ;
159- key ?: string ;
160- caCerts ?: string [ ] ;
161- certFile ?: string ;
162- keyFile ?: string ;
163- caCertFile ?: string ;
164- } ;
165- const cert = o . cert ??
166- ( o . certFile ? Deno . readTextFileSync ( o . certFile ) : undefined ) ;
167- const key = o . key ??
168- ( o . keyFile ? Deno . readTextFileSync ( o . keyFile ) : undefined ) ;
169- const caCerts = o . caCerts ??
170- ( o . caCertFile ? [ Deno . readTextFileSync ( o . caCertFile ) ] : undefined ) ;
150+ const cert = options . cert ??
151+ ( options . certFile
152+ ? this . runtime . readTextFileSync ( options . certFile )
153+ : undefined ) ;
154+ const key = options . key ??
155+ ( options . keyFile
156+ ? this . runtime . readTextFileSync ( options . keyFile )
157+ : undefined ) ;
158+ const caCerts = options . caCerts ??
159+ ( options . caCertFile
160+ ? [ this . runtime . readTextFileSync ( options . caCertFile ) ]
161+ : undefined ) ;
171162 tlsFields = {
172163 ...( cert !== undefined && { cert } ) ,
173164 ...( key !== undefined && { key } ) ,
@@ -187,12 +178,17 @@ export class CoreClient<
187178 try {
188179 if ( tls ) {
189180 const { cert, key, caCerts } = this . state . remoteAddr ;
190- const tlsOpts : Deno . ConnectTlsOptions = { hostname, port, caCerts } ;
191181 this . conn = cert && key
192- ? await this . connectImpl . withTls ( { ...tlsOpts , cert, key } )
193- : await this . connectImpl . withTls ( tlsOpts ) ;
182+ ? await this . runtime . connectTls ( {
183+ hostname,
184+ port,
185+ caCerts,
186+ cert,
187+ key,
188+ } )
189+ : await this . runtime . connectTls ( { hostname, port, caCerts } ) ;
194190 } else {
195- this . conn = await this . connectImpl . noTls ( { hostname, port } ) ;
191+ this . conn = await this . runtime . connect ( { hostname, port } ) ;
196192 }
197193 this . emit ( "connected" , publicAddr ) ;
198194 } catch ( error ) {
@@ -210,7 +206,7 @@ export class CoreClient<
210206 return publicAddr ;
211207 }
212208
213- private async loop ( conn : Deno . Conn ) : Promise < void > {
209+ private async loop ( conn : Conn ) : Promise < void > {
214210 for ( ; ; ) {
215211 const chunks = await this . read ( conn ) ;
216212 if ( chunks === null ) break ;
@@ -225,7 +221,7 @@ export class CoreClient<
225221 this . close ( ) ;
226222 }
227223
228- private async read ( conn : Deno . Conn ) : Promise < string | null > {
224+ private async read ( conn : Conn ) : Promise < string | null > {
229225 let read : number | null ;
230226
231227 try {
@@ -305,9 +301,7 @@ export class CoreClient<
305301 /** Emits properly an error. */
306302 emitError ( ...args : ErrorArgs ) : void {
307303 const [ , error ] = args ;
308- const isSilentError = error instanceof Deno . errors . BadResource ||
309- error instanceof Deno . errors . Interrupted ;
310- if ( isSilentError ) {
304+ if ( this . runtime ?. isSilentError ( error ) ) {
311305 return ;
312306 }
313307 this . emit ( "error" , toClientError ( ...args ) ) ;
0 commit comments