Skip to content

Commit ce48c4a

Browse files
committed
fix: revert deluge 2.x. @ctrl/deluge 1.x will stop support for deluge 1 here
1 parent 70ca083 commit ce48c4a

File tree

4 files changed

+36
-79
lines changed

4 files changed

+36
-79
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"dependencies": {
3030
"@ctrl/shared-torrent": "^1.3.1",
31+
"@ctrl/torrent-file": "^1.0.0",
3132
"form-data": "^2.3.3",
3233
"got": "^9.6.0",
3334
"tough-cookie": "^3.0.1",

src/index.ts

+14-38
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
TorrentState,
1212
AddTorrentOptions as NormalizedAddTorrentOptions,
1313
} from '@ctrl/shared-torrent';
14+
import { hash } from '@ctrl/torrent-file';
1415

1516
import {
1617
GetHostsResponse,
@@ -31,8 +32,6 @@ import {
3132
TorrentStatus,
3233
Tracker,
3334
Torrent,
34-
StringStatus,
35-
AddTorrentResponse,
3635
} from './types';
3736

3837
const defaults: TorrentSettings = {
@@ -78,11 +77,11 @@ export class Deluge implements TorrentClient {
7877
}
7978

8079
/**
81-
* Connects deluge and returns a list of available methods
80+
* Connects deluge
8281
* @param host index of host to use in result of get hosts
8382
* @param hostIdx index of host to use in result of get hosts
8483
*/
85-
async connect(selectedHost?: string, hostIdx = 0): Promise<ListMethods> {
84+
async connect(selectedHost?: string, hostIdx = 0): Promise<DefaultResponse> {
8685
let host = selectedHost;
8786
if (!host) {
8887
const hosts = await this.getHosts();
@@ -93,7 +92,7 @@ export class Deluge implements TorrentClient {
9392
throw new Error('No hosts found');
9493
}
9594

96-
const res = await this.request<ListMethods>('web.connect', [host], true, false);
95+
const res = await this.request<DefaultResponse>('web.connect', [host], true, false);
9796
return res.body;
9897
}
9998

@@ -107,14 +106,8 @@ export class Deluge implements TorrentClient {
107106
* Other instances may also reconnect. Not really sure why you would want to disconnect
108107
*/
109108
async disconnect(): Promise<boolean> {
110-
const res = await this.request<StringStatus>('web.disconnect', [], true, false);
111-
// deluge 1.x returns a boolean and 2.x returns a string
112-
if (typeof res.body.result === 'boolean') {
113-
return res.body.result;
114-
}
115-
116-
// "Connection was closed cleanly."
117-
return res.body.result.includes('closed cleanly');
109+
const res = await this.request<BooleanStatus>('web.disconnect', [], true, false);
110+
return res.body.result;
118111
}
119112

120113
/**
@@ -215,7 +208,7 @@ export class Deluge implements TorrentClient {
215208
headers: form.getHeaders(),
216209
body: form,
217210
retry: 0,
218-
};
211+
}
219212
// allow proxy agent
220213
if (this.config.agent) {
221214
options.agent = this.config.agent;
@@ -229,10 +222,7 @@ export class Deluge implements TorrentClient {
229222
return JSON.parse(res.body);
230223
}
231224

232-
async addTorrent(
233-
torrent: string | Buffer,
234-
config: Partial<AddTorrentOptions> = {},
235-
): Promise<AddTorrentResponse> {
225+
async addTorrent(torrent: string | Buffer, config: Partial<AddTorrentOptions> = {}) {
236226
const upload = await this.upload(torrent);
237227
if (!upload.success || !upload.files.length) {
238228
throw new Error('Failed to upload');
@@ -251,16 +241,11 @@ export class Deluge implements TorrentClient {
251241
// not passing path by default uses default
252242
// download_location: '/root/Downloads',
253243
// move_completed_path: '/root/Downloads',
254-
pre_allocate_storage: false,
255-
move_completed: false,
256-
seed_mode: false,
257-
sequential_download: false,
258-
super_seeding: false,
259244
...config,
260245
};
261-
const res = await this.request<AddTorrentResponse>('web.add_torrents', [[{ path, options }]]);
246+
const res = await this.request<BooleanStatus>('web.add_torrents', [[{ path, options }]]);
262247

263-
if (res.body.result[0][0] === false) {
248+
if (res.body.result === false) {
264249
throw new Error('Failed to add torrent');
265250
}
266251

@@ -280,12 +265,11 @@ export class Deluge implements TorrentClient {
280265
torrent = Buffer.from(torrent);
281266
}
282267

283-
const res = await this.addTorrent(torrent, torrentOptions);
284-
const torrentHash = res.result[0][1];
268+
const torrentHash = await hash(torrent);
269+
270+
await this.addTorrent(torrent, torrentOptions);
285271

286272
if (options.label) {
287-
// sets the label but it might not set the label right away
288-
// sometimes takes a few seconds for label to reflect in results
289273
await this.setTorrentLabel(torrentHash, options.label);
290274
}
291275

@@ -304,12 +288,7 @@ export class Deluge implements TorrentClient {
304288
prioritize_first_last_pieces: false,
305289
// not passing path by default uses default
306290
// download_location: '/root/Downloads',
307-
move_completed: false,
308291
// move_completed_path: '/root/Downloads',
309-
pre_allocate_storage: false,
310-
seed_mode: false,
311-
sequential_download: false,
312-
super_seeding: false,
313292
...config,
314293
};
315294
const res = await this.request<BooleanStatus>('core.add_torrent_magnet', [magnet, options]);
@@ -412,10 +391,7 @@ export class Deluge implements TorrentClient {
412391
* get torrent state/status
413392
* @param additionalFields fields ex - `['label']`
414393
*/
415-
async getTorrentStatus(
416-
torrentId: string,
417-
additionalFields: string[] = [],
418-
): Promise<TorrentStatus> {
394+
async getTorrentStatus(torrentId: string, additionalFields: string[] = []): Promise<TorrentStatus> {
419395
const fields = [
420396
'total_done',
421397
'total_payload_download',

src/types.ts

-16
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,10 @@ export interface BooleanStatus extends DefaultResponse {
1111
result: boolean;
1212
}
1313

14-
export interface StringStatus extends DefaultResponse {
15-
result: string;
16-
}
17-
1814
export interface ListMethods extends DefaultResponse {
1915
result: string[];
2016
}
2117

22-
export interface AddTorrentResponse extends DefaultResponse {
23-
/**
24-
* tuple of [result, torrent_hash_id]
25-
*/
26-
result: Array<[boolean, string]>
27-
}
28-
2918
// {"files": ["/tmp/delugeweb-5Q9ttR/tmpL7xhth.torrent"], "success": true}
3019
/**
3120
* ex -
@@ -100,12 +89,7 @@ export interface AddTorrentOptions {
10089
max_upload_slots: number;
10190
max_upload_speed: number;
10291
prioritize_first_last_pieces: boolean;
103-
move_completed: boolean;
10492
move_completed_path?: string;
105-
pre_allocate_storage: boolean;
106-
sequential_download: boolean;
107-
seed_mode: boolean;
108-
super_seeding: boolean;
10993
}
11094

11195
export interface TorrentListResponse extends DefaultResponse {

test/index.spec.ts

+21-25
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import { Deluge } from '../src/index';
77
const baseUrl = 'http://localhost:8112';
88
const torrentName = 'ubuntu-18.04.1-desktop-amd64.iso';
99
const torrentFile = path.join(__dirname, '/ubuntu-18.04.1-desktop-amd64.iso.torrent');
10-
const torrentHash = 'e84213a794f3ccd890382a54a64ca68b7e925433';
1110

1211
async function setupTorrent(deluge: Deluge) {
13-
await deluge.addTorrent(torrentFile, { add_paused: true });
12+
await deluge.addTorrent(torrentFile);
1413
await pWaitFor(
1514
async () => {
1615
const r = await deluge.listTorrents();
@@ -34,7 +33,7 @@ describe('Deluge', () => {
3433
const ids = Object.keys(torrents.result.torrents);
3534
for (const id of ids) {
3635
// clean up all torrents
37-
await deluge.removeTorrent(id, true);
36+
await deluge.removeTorrent(id, false);
3837
}
3938
});
4039
it('should be instantiable', async () => {
@@ -50,15 +49,23 @@ describe('Deluge', () => {
5049
it('should connect', async () => {
5150
const deluge = new Deluge({ baseUrl });
5251
const res = await deluge.connect();
53-
// theres a bunch
54-
expect(res.result.length).toBeGreaterThan(2);
52+
expect(res.result).toBe(null);
5553
});
5654
it('should get plugins', async () => {
5755
const deluge = new Deluge({ baseUrl });
5856
const res = await deluge.getPlugins();
59-
expect(res.result.enabled_plugins.length).toBeGreaterThan(0);
57+
expect(res.result.enabled_plugins).toEqual(['Label']);
6058
expect(res.result.available_plugins).toBeDefined();
61-
expect(res.result.available_plugins).toContain('Label');
59+
expect(res.result.available_plugins).toEqual([
60+
'Extractor',
61+
'Execute',
62+
'Blocklist',
63+
'AutoAdd',
64+
'Label',
65+
'Notifications',
66+
'WebUi',
67+
'Scheduler',
68+
]);
6269
});
6370
it('should get plugins info', async () => {
6471
const deluge = new Deluge({ baseUrl });
@@ -113,7 +120,7 @@ describe('Deluge', () => {
113120
expect(deluge.config.password).toBe(oldPassword);
114121
deluge.config.password = 'wrongpassword';
115122
// tslint:disable-next-line no-floating-promises
116-
await expect(deluge.changePassword('shouldfail')).rejects.toThrowError();
123+
expect(deluge.changePassword('shouldfail')).rejects.toThrowError();
117124
});
118125
it('should list methods', async () => {
119126
const deluge = new Deluge({ baseUrl });
@@ -130,21 +137,18 @@ describe('Deluge', () => {
130137
it('should add torrent from file path string', async () => {
131138
const deluge = new Deluge({ baseUrl });
132139
const res = await deluge.addTorrent(torrentFile);
133-
expect(res.result[0][0]).toBe(true);
134-
expect(res.result[0][1]).toBe(torrentHash);
140+
expect(res.result).toBe(true);
135141
});
136142
it('should add torrent from file buffer', async () => {
137143
const deluge = new Deluge({ baseUrl });
138144
const res = await deluge.addTorrent(fs.readFileSync(torrentFile));
139-
expect(res.result[0][0]).toBe(true);
140-
expect(res.result[0][1]).toBe(torrentHash);
145+
expect(res.result).toBe(true);
141146
});
142147
it('should add torrent from file contents base64', async () => {
143148
const deluge = new Deluge({ baseUrl });
144149
const contents = Buffer.from(fs.readFileSync(torrentFile)).toString('base64');
145150
const res = await deluge.addTorrent(contents);
146-
expect(res.result[0][0]).toBe(true);
147-
expect(res.result[0][1]).toBe(torrentHash);
151+
expect(res.result).toBe(true);
148152
});
149153
it('should get torrent status', async () => {
150154
const deluge = new Deluge({ baseUrl });
@@ -201,13 +205,6 @@ describe('Deluge', () => {
201205
const key = Object.keys(res.result.torrents)[0];
202206
await deluge.verifyTorrent(key);
203207
});
204-
it('should add label', async () => {
205-
const client = new Deluge({ baseUrl });
206-
const list = await setupTorrent(client);
207-
const key = Object.keys(list.result.torrents)[0];
208-
const res = await client.setTorrentLabel(key, 'swag');
209-
expect(res.result).toBe(null);
210-
});
211208
it('should pause/resume torrents', async () => {
212209
const deluge = new Deluge({ baseUrl });
213210
const res = await setupTorrent(deluge);
@@ -270,13 +267,12 @@ describe('Deluge', () => {
270267
const torrent = await client.normalizedAddTorrent(fs.readFileSync(torrentFile), {
271268
label: 'test',
272269
});
273-
expect(torrent.connectedPeers).toBeGreaterThanOrEqual(0);
270+
expect(torrent.connectedPeers).toBe(0);
274271
expect(torrent.connectedSeeds).toBe(0);
275272
expect(torrent.downloadSpeed).toBe(0);
276273
expect(torrent.eta).toBe(0);
277-
// expect(torrent.isCompleted).toBe(false);
278-
// its setting the label but it takes an unknown number of seconds to save to db
279-
// expect(torrent.label).toBe('');
274+
expect(torrent.isCompleted).toBe(false);
275+
expect(torrent.label).toBe('test');
280276
expect(torrent.name).toBe(torrentName);
281277
expect(torrent.progress).toBeGreaterThanOrEqual(0);
282278
expect(torrent.queuePosition).toBe(1);

0 commit comments

Comments
 (0)