Skip to content

Commit f22136c

Browse files
committed
fix msgid
1 parent 3a087ab commit f22136c

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

Diff for: src/deluge.ts

+11-22
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import type {
3939
} from './types.js';
4040

4141
interface DelugeState extends TorrentClientState {
42-
auth?: { cookie: Cookie; msgId: number };
42+
auth: { cookie?: Cookie; msgId: number };
4343
}
4444

4545
const defaults: TorrentClientConfig = {
@@ -68,7 +68,7 @@ export class Deluge implements TorrentClient {
6868
}
6969

7070
config: TorrentClientConfig;
71-
state: DelugeState = {};
71+
state: DelugeState = { auth: { msgId: 0 } };
7272

7373
constructor(options: Partial<TorrentClientConfig> = {}) {
7474
this.config = { ...defaults, ...options };
@@ -83,13 +83,13 @@ export class Deluge implements TorrentClient {
8383
cookie: this.state.auth.cookie.toJSON(),
8484
msgId: this.state.auth.msgId,
8585
}
86-
: undefined,
86+
: { msgId: 0 },
8787
}),
8888
);
8989
}
9090

9191
resetSession(): void {
92-
this.state.auth = undefined;
92+
this.state.auth = { msgId: 0 };
9393
}
9494

9595
async getHosts(): Promise<GetHostsResponse> {
@@ -158,7 +158,7 @@ export class Deluge implements TorrentClient {
158158
*/
159159
async checkSession(): Promise<boolean> {
160160
// cookie is missing or expires in x seconds
161-
if (this.state.auth?.cookie) {
161+
if (this.state.auth.cookie) {
162162
// eslint-disable-next-line new-cap
163163
if (this.state.auth.cookie.TTL() < 5000) {
164164
this.resetSession();
@@ -168,7 +168,7 @@ export class Deluge implements TorrentClient {
168168
return true;
169169
}
170170

171-
if (this.state.auth?.cookie) {
171+
if (this.state.auth.cookie) {
172172
try {
173173
const check = await this.request<BooleanStatus>('auth.check_session', undefined, false);
174174
const body = await check.json();
@@ -195,10 +195,7 @@ export class Deluge implements TorrentClient {
195195
throw new Error('Auth failed, incorrect password');
196196
}
197197

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'));
202199
return true;
203200
}
204201

@@ -421,10 +418,7 @@ export class Deluge implements TorrentClient {
421418

422419
// update current password to new password
423420
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'));
428422
return body;
429423
}
430424

@@ -681,7 +675,7 @@ export class Deluge implements TorrentClient {
681675
needsAuth = true,
682676
autoConnect = true,
683677
): Promise<ReturnType<typeof ofetch.raw<T>>> {
684-
if (this.state.auth?.msgId === 4096) {
678+
if (this.state.auth.msgId === 4096) {
685679
this.state.auth.msgId = 0;
686680
}
687681

@@ -697,21 +691,16 @@ export class Deluge implements TorrentClient {
697691
}
698692

699693
const headers: any = {
700-
Cookie: this.state.auth?.cookie?.cookieString?.(),
694+
Cookie: this.state.auth.cookie?.cookieString?.(),
701695
};
702696
const url = joinURL(this.config.baseUrl, this.config.path);
703697

704-
// increment msgId
705-
if (this.state.auth?.msgId) {
706-
this.state.auth.msgId++;
707-
}
708-
709698
const res = await ofetch.raw<T>(url, {
710699
method: 'POST',
711700
body: JSON.stringify({
712701
method,
713702
params,
714-
id: this.state.auth?.msgId,
703+
id: this.state.auth.msgId++,
715704
}),
716705
headers,
717706
retry: 0,

0 commit comments

Comments
 (0)