@ctrl/deluge 7.5.3 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
1
  import type { AddTorrentOptions as NormalizedAddTorrentOptions, AllClientData, NormalizedTorrent, TorrentClient, TorrentClientConfig, TorrentClientState } from '@ctrl/shared-torrent';
2
2
  import { ofetch } from 'ofetch';
3
- import { Cookie } from 'tough-cookie';
4
3
  import type { Jsonify } from 'type-fest';
5
4
  import type { AddTorrentOptions, AddTorrentResponse, BooleanStatus, ConfigResponse, DefaultResponse, DelugeSettings, GetHostsResponse, GetHostStatusResponse, ListMethods, PluginInfo, PluginsListResponse, StringStatus, TorrentFiles, TorrentInfo, TorrentListResponse, TorrentOptions, TorrentStatus, Tracker, UploadResponse } from './types.js';
6
5
  interface DelugeState extends TorrentClientState {
7
6
  auth: {
8
- cookie?: Cookie;
7
+ cookieHeader?: string;
8
+ expires?: string;
9
9
  msgId: number;
10
10
  };
11
11
  }
@@ -116,5 +116,6 @@ export declare class Deluge implements TorrentClient {
116
116
  disablePlugin(plugins: string[]): Promise<DefaultResponse>;
117
117
  request<T extends object>(method: string, params?: any[], needsAuth?: boolean, autoConnect?: boolean): Promise<ReturnType<typeof ofetch.raw<T>>>;
118
118
  private _validateAuth;
119
+ private _setAuthCookie;
119
120
  }
120
121
  export {};
@@ -1,7 +1,7 @@
1
1
  import { magnetDecode } from '@ctrl/magnet-link';
2
+ import { parseSetCookie, splitSetCookieString, stringifyCookie } from 'cookie-es';
2
3
  import { FormData } from 'node-fetch-native';
3
4
  import { ofetch } from 'ofetch';
4
- import { Cookie } from 'tough-cookie';
5
5
  import { joinURL } from 'ufo';
6
6
  import { base64ToUint8Array, isUint8Array, stringToUint8Array } from 'uint8array-extras';
7
7
  import { normalizeTorrentData } from './normalizeTorrentData.js';
@@ -18,7 +18,8 @@ export class Deluge {
18
18
  ...state,
19
19
  auth: state.auth
20
20
  ? {
21
- cookie: Cookie.fromJSON(state.auth.cookie),
21
+ cookieHeader: state.auth.cookieHeader,
22
+ expires: state.auth.expires ? new Date(state.auth.expires).toISOString() : undefined,
22
23
  msgId: state.auth.msgId,
23
24
  }
24
25
  : { msgId: 0 },
@@ -33,12 +34,7 @@ export class Deluge {
33
34
  exportState() {
34
35
  return JSON.parse(JSON.stringify({
35
36
  ...this.state,
36
- auth: this.state.auth
37
- ? {
38
- cookie: this.state.auth.cookie.toJSON(),
39
- msgId: this.state.auth.msgId,
40
- }
41
- : { msgId: 0 },
37
+ auth: this.state.auth,
42
38
  }));
43
39
  }
44
40
  resetSession() {
@@ -97,25 +93,25 @@ export class Deluge {
97
93
  */
98
94
  async checkSession() {
99
95
  // cookie is missing or expires in x seconds
100
- if (this.state.auth.cookie) {
101
- if (this.state.auth.cookie.TTL() < 5000) {
102
- this.resetSession();
103
- return false;
104
- }
105
- return true;
96
+ if (!this.state.auth.cookieHeader) {
97
+ this.resetSession();
98
+ return false;
106
99
  }
107
- if (this.state.auth.cookie) {
108
- try {
109
- const check = await this.request('auth.check_session', undefined, false);
110
- const body = await check.json();
111
- if (body?.result) {
112
- return true;
113
- }
114
- }
115
- catch {
116
- // do nothing
100
+ const expires = this.state.auth.expires ? new Date(this.state.auth.expires) : undefined;
101
+ if (expires && expires.getTime() - Date.now() < 5000) {
102
+ this.resetSession();
103
+ return false;
104
+ }
105
+ try {
106
+ const check = await this.request('auth.check_session', undefined, false);
107
+ const body = check._data;
108
+ if (body?.result) {
109
+ return true;
117
110
  }
118
111
  }
112
+ catch {
113
+ // do nothing
114
+ }
119
115
  this.resetSession();
120
116
  return false;
121
117
  }
@@ -129,7 +125,7 @@ export class Deluge {
129
125
  if (!res.ok || !res.headers?.get('set-cookie')?.length) {
130
126
  throw new Error('Auth failed, incorrect password');
131
127
  }
132
- this.state.auth.cookie = Cookie.parse(res.headers.get('set-cookie'));
128
+ this._setAuthCookie(res.headers.get('set-cookie'));
133
129
  return true;
134
130
  }
135
131
  /**
@@ -319,7 +315,7 @@ export class Deluge {
319
315
  }
320
316
  // update current password to new password
321
317
  this.config.password = password;
322
- this.state.auth.cookie = Cookie.parse(res.headers.get('set-cookie'));
318
+ this._setAuthCookie(res.headers.get('set-cookie'));
323
319
  return body;
324
320
  }
325
321
  async getAllData() {
@@ -546,7 +542,7 @@ export class Deluge {
546
542
  }
547
543
  }
548
544
  const headers = {
549
- Cookie: this.state.auth.cookie?.cookieString?.(),
545
+ Cookie: this.state.auth.cookieHeader,
550
546
  };
551
547
  const url = joinURL(this.config.baseUrl, this.config.path);
552
548
  const res = await ofetch.raw(url, {
@@ -576,4 +572,18 @@ export class Deluge {
576
572
  throw new Error('Invalid Auth');
577
573
  }
578
574
  }
575
+ _setAuthCookie(setCookie) {
576
+ const authSetCookie = setCookie ? splitSetCookieString(setCookie)[0] : undefined;
577
+ const parsed = authSetCookie ? parseSetCookie(authSetCookie) : undefined;
578
+ const expiresValue = authSetCookie
579
+ ? /(?:^|;)\s*expires=([^;]+)/i.exec(authSetCookie)?.[1]
580
+ : undefined;
581
+ const expires = expiresValue ? new Date(expiresValue) : undefined;
582
+ this.state.auth.cookieHeader =
583
+ parsed?.name && parsed.value !== undefined
584
+ ? stringifyCookie({ [parsed.name]: parsed.value })
585
+ : undefined;
586
+ this.state.auth.expires =
587
+ expires && !Number.isNaN(expires.getTime()) ? expires.toISOString() : undefined;
588
+ }
579
589
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctrl/deluge",
3
- "version": "7.5.3",
3
+ "version": "8.0.0",
4
4
  "description": "TypeScript api wrapper for deluge using ofetch",
5
5
  "keywords": [
6
6
  "deluge",
@@ -33,9 +33,9 @@
33
33
  "dependencies": {
34
34
  "@ctrl/magnet-link": "^4.3.0",
35
35
  "@ctrl/shared-torrent": "^6.5.1",
36
+ "cookie-es": "^3.0.1",
36
37
  "node-fetch-native": "^1.6.7",
37
38
  "ofetch": "^1.5.1",
38
- "tough-cookie": "^6.0.1",
39
39
  "type-fest": "^5.5.0",
40
40
  "ufo": "^1.6.3",
41
41
  "uint8array-extras": "^1.5.0"