@@ -31,6 +31,7 @@ const PORT = 6667;
31
31
export interface RemoteAddr {
32
32
hostname : string ;
33
33
port : number ;
34
+ tls ?: boolean ;
34
35
}
35
36
36
37
export type PluginParams = {
@@ -52,14 +53,23 @@ export type Plugin<T extends PluginParams = Record<string, void>> = (
52
53
options : Readonly < ExtendedOptions < T > > ,
53
54
) => void ;
54
55
56
+ /** How to connect to a server */
57
+ interface ConnectImpl {
58
+ noTls ( opts : Deno . ConnectOptions ) : Promise < Deno . Conn > ;
59
+ withTls ( opts : Deno . ConnectTlsOptions ) : Promise < Deno . Conn > ;
60
+ }
61
+
55
62
export class CoreClient <
56
63
TEvents extends CoreParams [ "events" ] = CoreParams [ "events" ] ,
57
64
> extends EventEmitter <
58
65
CoreParams [ "events" ] & TEvents
59
66
> {
60
67
readonly state : CoreParams [ "state" ] ;
61
68
62
- protected connectImpl = Deno . connect ;
69
+ protected connectImpl : ConnectImpl = {
70
+ noTls : Deno . connect ,
71
+ withTls : Deno . connectTls ,
72
+ } ;
63
73
protected conn : Deno . Conn | null = null ;
64
74
protected hooks = new Hooks ( this ) ;
65
75
@@ -76,7 +86,7 @@ export class CoreClient<
76
86
super ( options ) ;
77
87
78
88
this . buffer = new Uint8Array ( options . bufferSize ?? BUFFER_SIZE ) ;
79
- this . state = { remoteAddr : { hostname : "" , port : 0 } } ;
89
+ this . state = { remoteAddr : { hostname : "" , port : 0 , tls : false } } ;
80
90
81
91
new Set ( plugins ) . forEach ( ( plugin ) => plugin ( this , options ) ) ;
82
92
this . resetErrorThrowingBehavior ( ) ;
@@ -85,10 +95,15 @@ export class CoreClient<
85
95
/** Connects to a server using a hostname and an optional port.
86
96
*
87
97
* Default port to `6667`.
98
+ * If `tls=true`, attempt to connect using a TLS connection.
88
99
*
89
100
* Resolves when connected. */
90
- async connect ( hostname : string , port = PORT ) : Promise < Deno . Conn | null > {
91
- this . state . remoteAddr = { hostname, port } ;
101
+ async connect (
102
+ hostname : string ,
103
+ port = PORT ,
104
+ tls = false ,
105
+ ) : Promise < Deno . Conn | null > {
106
+ this . state . remoteAddr = { hostname, port, tls } ;
92
107
93
108
if ( this . conn !== null ) {
94
109
this . close ( ) ;
@@ -98,7 +113,9 @@ export class CoreClient<
98
113
this . emit ( "connecting" , remoteAddr ) ;
99
114
100
115
try {
101
- this . conn = await this . connectImpl ( { hostname, port } ) ;
116
+ this . conn = await ( tls
117
+ ? this . connectImpl . withTls ( { hostname, port } )
118
+ : this . connectImpl . noTls ( { hostname, port } ) ) ;
102
119
this . emit ( "connected" , remoteAddr ) ;
103
120
} catch ( error ) {
104
121
this . emitError ( "connect" , error ) ;
0 commit comments