@@ -39,7 +39,7 @@ import type {
39
39
} from './types.js' ;
40
40
41
41
interface DelugeState extends TorrentClientState {
42
- auth ? : { cookie : Cookie ; msgId : number } ;
42
+ auth : { cookie ? : Cookie ; msgId : number } ;
43
43
}
44
44
45
45
const defaults : TorrentClientConfig = {
@@ -68,7 +68,7 @@ export class Deluge implements TorrentClient {
68
68
}
69
69
70
70
config : TorrentClientConfig ;
71
- state : DelugeState = { } ;
71
+ state : DelugeState = { auth : { msgId : 0 } } ;
72
72
73
73
constructor ( options : Partial < TorrentClientConfig > = { } ) {
74
74
this . config = { ...defaults , ...options } ;
@@ -83,13 +83,13 @@ export class Deluge implements TorrentClient {
83
83
cookie : this . state . auth . cookie . toJSON ( ) ,
84
84
msgId : this . state . auth . msgId ,
85
85
}
86
- : undefined ,
86
+ : { msgId : 0 } ,
87
87
} ) ,
88
88
) ;
89
89
}
90
90
91
91
resetSession ( ) : void {
92
- this . state . auth = undefined ;
92
+ this . state . auth = { msgId : 0 } ;
93
93
}
94
94
95
95
async getHosts ( ) : Promise < GetHostsResponse > {
@@ -158,7 +158,7 @@ export class Deluge implements TorrentClient {
158
158
*/
159
159
async checkSession ( ) : Promise < boolean > {
160
160
// cookie is missing or expires in x seconds
161
- if ( this . state . auth ? .cookie ) {
161
+ if ( this . state . auth . cookie ) {
162
162
// eslint-disable-next-line new-cap
163
163
if ( this . state . auth . cookie . TTL ( ) < 5000 ) {
164
164
this . resetSession ( ) ;
@@ -168,7 +168,7 @@ export class Deluge implements TorrentClient {
168
168
return true ;
169
169
}
170
170
171
- if ( this . state . auth ? .cookie ) {
171
+ if ( this . state . auth . cookie ) {
172
172
try {
173
173
const check = await this . request < BooleanStatus > ( 'auth.check_session' , undefined , false ) ;
174
174
const body = await check . json ( ) ;
@@ -195,10 +195,7 @@ export class Deluge implements TorrentClient {
195
195
throw new Error ( 'Auth failed, incorrect password' ) ;
196
196
}
197
197
198
- this . state . auth = {
199
- cookie : Cookie . parse ( res . headers . get ( 'set-cookie' ) ) ,
200
- msgId : 0 ,
201
- } ;
198
+ this . state . auth . cookie = Cookie . parse ( res . headers . get ( 'set-cookie' ) ) ;
202
199
return true ;
203
200
}
204
201
@@ -421,10 +418,7 @@ export class Deluge implements TorrentClient {
421
418
422
419
// update current password to new password
423
420
this . config . password = password ;
424
- this . state . auth = {
425
- cookie : Cookie . parse ( res . headers . get ( 'set-cookie' ) ) ,
426
- msgId : 0 ,
427
- } ;
421
+ this . state . auth . cookie = Cookie . parse ( res . headers . get ( 'set-cookie' ) ) ;
428
422
return body ;
429
423
}
430
424
@@ -681,7 +675,7 @@ export class Deluge implements TorrentClient {
681
675
needsAuth = true ,
682
676
autoConnect = true ,
683
677
) : Promise < ReturnType < typeof ofetch . raw < T > > > {
684
- if ( this . state . auth ? .msgId === 4096 ) {
678
+ if ( this . state . auth . msgId === 4096 ) {
685
679
this . state . auth . msgId = 0 ;
686
680
}
687
681
@@ -697,21 +691,16 @@ export class Deluge implements TorrentClient {
697
691
}
698
692
699
693
const headers : any = {
700
- Cookie : this . state . auth ? .cookie ?. cookieString ?.( ) ,
694
+ Cookie : this . state . auth . cookie ?. cookieString ?.( ) ,
701
695
} ;
702
696
const url = joinURL ( this . config . baseUrl , this . config . path ) ;
703
697
704
- // increment msgId
705
- if ( this . state . auth ?. msgId ) {
706
- this . state . auth . msgId ++ ;
707
- }
708
-
709
698
const res = await ofetch . raw < T > ( url , {
710
699
method : 'POST' ,
711
700
body : JSON . stringify ( {
712
701
method,
713
702
params,
714
- id : this . state . auth ? .msgId ,
703
+ id : this . state . auth . msgId ++ ,
715
704
} ) ,
716
705
headers,
717
706
retry : 0 ,
0 commit comments