@@ -17,8 +17,6 @@ interface Options {
1717 logger ?: Pick < Console , Severity > ;
1818 queryForwarding ?: boolean ;
1919 fetch ?: any ;
20- maxConnectionTimeout ?: number ;
21- forward ?: boolean ;
2220}
2321
2422const proxyAgent = new EnvHttpProxyAgent ( ) ;
@@ -30,8 +28,6 @@ class SmeeClient {
3028 #logger: Pick < Console , Severity > ;
3129 #events: EventSource | null = null ;
3230 #queryForwarding: boolean = true ;
33- #maxConnectionTimeout: number | undefined ;
34- #forward: boolean | undefined = undefined ;
3531
3632 #onerror: ( err : ErrorEvent ) => void = ( err ) => {
3733 if ( this . #events?. readyState === EventSource . CLOSED ) {
@@ -44,10 +40,6 @@ class SmeeClient {
4440 #onopen: ( ) => void = ( ) => { } ;
4541
4642 #onmessage: ( msg : MessageEvent ) => Promise < void > = async ( msg ) => {
47- if ( ! this . #forward) {
48- return ;
49- }
50-
5143 const data = JSON . parse ( msg . data ) ;
5244
5345 const target = url . parse ( this . #target, true ) ;
@@ -98,17 +90,13 @@ class SmeeClient {
9890 target,
9991 logger = console ,
10092 fetch = undiciFetch ,
101- maxConnectionTimeout,
10293 queryForwarding = true ,
103- forward,
10494 } : Options ) {
10595 this . #source = source ;
10696 this . #target = target ;
10797 this . #logger = logger ! ;
10898 this . #fetch = fetch ;
10999 this . #queryForwarding = queryForwarding ;
110- this . #maxConnectionTimeout = maxConnectionTimeout ;
111- this . #forward = forward ;
112100
113101 if (
114102 ! validator . isURL ( this . #source, {
@@ -208,14 +196,10 @@ class SmeeClient {
208196 // Reconnect immediately
209197 ( events as any ) . reconnectInterval = 0 ; // This isn't a valid property of EventSource
210198
211- const establishConnection = new Promise < void > ( ( resolve , reject ) => {
199+ const connected = new Promise < void > ( ( resolve , reject ) => {
212200 events . addEventListener ( "open" , ( ) => {
213201 this . #logger. info ( `Connected to ${ this . #source} ` ) ;
214202 events . removeEventListener ( "error" , reject ) ;
215-
216- if ( this . #forward !== false ) {
217- this . #startForwarding( ) ;
218- }
219203 resolve ( ) ;
220204 } ) ;
221205 events . addEventListener ( "error" , reject ) ;
@@ -237,78 +221,20 @@ class SmeeClient {
237221 events . onerror = this . #events_onerror;
238222 }
239223
240- if ( this . #maxConnectionTimeout !== undefined ) {
241- const timeoutConnection = new Promise < void > ( ( _ , reject ) => {
242- setTimeout ( async ( ) => {
243- if ( events . readyState === EventSource . OPEN ) {
244- // If the connection is already open, we don't need to reject
245- return ;
246- }
247-
248- this . #logger. error (
249- `Connection to ${ this . #source} timed out after ${ this . #maxConnectionTimeout} ms` ,
250- ) ;
251- reject (
252- new Error (
253- `Connection to ${ this . #source} timed out after ${ this . #maxConnectionTimeout} ms` ,
254- ) ,
255- ) ;
256- await this . stop ( ) ;
257- } , this . #maxConnectionTimeout) ?. unref ( ) ;
258- } ) ;
259- await Promise . race ( [ establishConnection , timeoutConnection ] ) ;
260- } else {
261- await establishConnection ;
262- }
224+ this . #logger. info ( `Forwarding ${ this . #source} to ${ this . #target} ` ) ;
225+
226+ await connected ;
263227
264228 return events ;
265229 }
266230
267231 async stop ( ) {
268232 if ( this . #events) {
269- this . #stopForwarding( ) ;
270233 this . #events. close ( ) ;
271234 this . #events = null as any ;
272- this . #forward = undefined ;
273235 this . #logger. info ( "Connection closed" ) ;
274236 }
275237 }
276-
277- #startForwarding( ) {
278- if ( this . #forward === true ) {
279- return ;
280- }
281- this . #forward = true ;
282- this . #logger. info ( `Forwarding ${ this . #source} to ${ this . #target} ` ) ;
283- }
284-
285- startForwarding ( ) {
286- if ( this . #forward === true ) {
287- this . #logger. info (
288- `Forwarding ${ this . #source} to ${ this . #target} is already enabled` ,
289- ) ;
290- return ;
291- }
292- this . #startForwarding( ) ;
293- }
294-
295- #stopForwarding( ) {
296- if ( this . #forward !== true ) {
297- return ;
298- }
299- this . #forward = false ;
300- this . #logger. info ( `Stopped forwarding ${ this . #source} to ${ this . #target} ` ) ;
301- }
302-
303- stopForwarding ( ) {
304- if ( this . #forward !== true ) {
305- this . #logger. info (
306- `Forwarding ${ this . #source} to ${ this . #target} is already disabled` ,
307- ) ;
308- return ;
309- }
310- this . #stopForwarding( ) ;
311- }
312238}
313239
314240export {
0 commit comments