@@ -17,11 +17,20 @@ export interface CoreParams {
17
17
"raw" : Raw ;
18
18
"error" : FatalError ;
19
19
} ;
20
+
21
+ state : {
22
+ remoteAddr : RemoteAddr ;
23
+ } ;
20
24
}
21
25
22
26
const BUFFER_SIZE = 4096 ;
23
27
const PORT = 6667 ;
24
28
29
+ export interface RemoteAddr {
30
+ hostname : string ;
31
+ port : number ;
32
+ }
33
+
25
34
export class FatalError extends Error {
26
35
constructor (
27
36
public type : "connect" | "read" | "write" | "close" ,
@@ -41,9 +50,10 @@ export type ExtendedClient<T extends PluginParams = {}> =
41
50
& { readonly state : T [ "state" ] }
42
51
& T [ "commands" ] ;
43
52
44
- export type ExtendedOptions < T extends PluginParams = { } > =
53
+ export type ExtendedOptions < T extends PluginParams = { } > = Readonly <
45
54
& CoreParams [ "options" ]
46
- & T [ "options" ] ;
55
+ & T [ "options" ]
56
+ > ;
47
57
48
58
export type Plugin < T extends PluginParams = { } > = (
49
59
client : ExtendedClient < T > ,
@@ -61,15 +71,17 @@ export class CoreClient<
61
71
private decoder = new TextDecoder ( ) ;
62
72
private encoder = new TextEncoder ( ) ;
63
73
private parser = new Parser ( ) ;
64
-
65
- readonly state : Readonly < { } > = { } ;
74
+ readonly state : CoreParams [ "state" ] ;
66
75
67
76
constructor (
68
77
plugins : Plugin < any > [ ] ,
69
78
options : Readonly < CoreParams [ "options" ] > ,
70
79
) {
71
80
super ( options ) ;
81
+
72
82
this . bufferSize = options . bufferSize ?? BUFFER_SIZE ;
83
+ this . state = { remoteAddr : { hostname : "" , port : 0 } } ;
84
+
73
85
new Set ( plugins ) . forEach ( ( plugin ) => plugin ( this , options ) ) ;
74
86
this . resetErrorThrowingBehavior ( ) ;
75
87
}
@@ -80,20 +92,25 @@ export class CoreClient<
80
92
*
81
93
* Resolves when connected. */
82
94
async connect ( hostname : string , port = PORT ) : Promise < Deno . Conn | null > {
95
+ this . state . remoteAddr = { hostname, port } ;
96
+
83
97
if ( this . conn !== null ) {
84
98
this . close ( ) ;
85
99
}
86
100
101
+ const { remoteAddr } = this . state ;
102
+ this . emit ( "connecting" , remoteAddr ) ;
103
+
87
104
try {
88
- this . emit ( "connecting" , { hostname, port } ) ;
89
105
this . conn = await this . connectImpl ( { hostname, port } ) ;
90
- this . emit ( "connected" , getRemoteAddr ( this . conn ) ) ;
106
+ this . emit ( "connected" , remoteAddr ) ;
91
107
} catch ( error ) {
92
108
this . emit ( "error" , new FatalError ( "connect" , error . message ) ) ;
93
109
return null ;
94
110
}
95
111
96
112
this . read ( this . conn ) ;
113
+
97
114
return this . conn ;
98
115
}
99
116
@@ -130,7 +147,7 @@ export class CoreClient<
130
147
131
148
try {
132
149
this . conn . close ( ) ;
133
- this . emit ( "disconnected" , getRemoteAddr ( this . conn ) ) ;
150
+ this . emit ( "disconnected" , this . state . remoteAddr ) ;
134
151
} catch ( error ) {
135
152
this . emit ( "error" , new FatalError ( "close" , error . message ) ) ;
136
153
} finally {
@@ -174,14 +191,3 @@ export class CoreClient<
174
191
this . close ( ) ;
175
192
}
176
193
}
177
-
178
- export interface RemoteAddr {
179
- hostname : string ;
180
- port : number ;
181
- }
182
-
183
- function getRemoteAddr ( conn : Deno . Conn ) : RemoteAddr {
184
- const addr = conn . remoteAddr as Deno . NetAddr ;
185
- const { hostname, port } = addr ;
186
- return { hostname, port } ;
187
- }
0 commit comments