Skip to content

Commit 6a26665

Browse files
committed
feat: use shared-torrent config
1 parent 4c70b8f commit 6a26665

File tree

5 files changed

+54
-48
lines changed

5 files changed

+54
-48
lines changed

Diff for: package-lock.json

+10-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"semantic-release": "cd dist && semantic-release"
2626
},
2727
"dependencies": {
28+
"@ctrl/shared-torrent": "^1.0.1",
2829
"form-data": "2.3.3",
2930
"got": "9.6.0",
3031
"tough-cookie": "3.0.1"

Diff for: src/index.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { resolve } from 'url';
1+
import { resolve, URL } from 'url';
22
import got, { Response } from 'got';
33
import { Cookie } from 'tough-cookie';
44
import FormData from 'form-data';
55
import fs from 'fs';
6+
import { TorrentSettings } from '@ctrl/shared-torrent';
67
import {
7-
DelugeConfig,
88
GetHostsResponse,
99
GetHostStatusResponse,
1010
DefaultResponse,
@@ -24,20 +24,21 @@ import {
2424
Tracker,
2525
} from './types';
2626

27-
const defaults: DelugeConfig = {
28-
baseURL: 'http://localhost:8112/',
27+
const defaults: Partial<TorrentSettings> = {
28+
host: 'localhost',
29+
port: 8112,
2930
path: '/json',
3031
password: 'deluge',
3132
};
3233

3334
export class Deluge {
34-
config: DelugeConfig;
35+
config: Partial<TorrentSettings>;
3536

3637
private _msgId = 0;
3738

3839
private _cookie?: Cookie;
3940

40-
constructor(options: Partial<DelugeConfig> = {}) {
41+
constructor(options: Partial<TorrentSettings> = {}) {
4142
this.config = { ...defaults, ...options };
4243
}
4344

@@ -192,7 +193,12 @@ export class Deluge {
192193
form.append('file', torrent);
193194
}
194195

195-
const url = resolve(this.config.baseURL, 'upload');
196+
const baseUrl = new URL(this.config.host as string);
197+
if (this.config.port) {
198+
baseUrl.port = `${this.config.port}`;
199+
}
200+
201+
const url = resolve(baseUrl.toString(), '../upload');
196202
const res = await got.post(url, {
197203
headers: form.getHeaders(),
198204
body: form,
@@ -448,10 +454,15 @@ export class Deluge {
448454
}
449455
}
450456

457+
const baseUrl = new URL(this.config.host as string);
458+
if (this.config.port) {
459+
baseUrl.port = `${this.config.port}`;
460+
}
461+
451462
const headers: any = {
452463
Cookie: this._cookie && this._cookie.cookieString(),
453464
};
454-
const url = resolve(this.config.baseURL, this.config.path);
465+
const url = resolve(baseUrl.toString(), this.config.path as string);
455466
return got.post(url, {
456467
json: true,
457468
body: {

Diff for: src/types.ts

-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
export interface DelugeConfig {
2-
/**
3-
* baseurl ex - `'http://localhost:8112/'`
4-
*/
5-
baseURL: string;
6-
/**
7-
* ex - `'/json'`
8-
*/
9-
path: string;
10-
password: string;
11-
}
12-
131
export interface DefaultResponse {
142
id: number;
153
error: null | string;

Diff for: test/index.spec.ts

+24-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import fs from 'fs';
44

55
import { Deluge } from '../src/index';
66

7-
const baseURL = 'http://localhost:8112/';
7+
const host = 'http://localhost:8112/';
8+
const port = 8112;
89
const torrentFile = path.join(__dirname, '/ubuntu-18.04.1-desktop-amd64.iso.torrent');
910

1011
async function setupTorrent(deluge: Deluge) {
@@ -23,7 +24,7 @@ async function setupTorrent(deluge: Deluge) {
2324

2425
describe('Deluge', () => {
2526
afterEach(async () => {
26-
const deluge = new Deluge({ baseURL });
27+
const deluge = new Deluge({ host, port });
2728
const torrents = await deluge.listTorrents();
2829
const ids = Object.keys(torrents.result.torrents);
2930
for (const id of ids) {
@@ -32,23 +33,23 @@ describe('Deluge', () => {
3233
}
3334
});
3435
it('should be instantiable', async () => {
35-
const deluge = new Deluge({ baseURL });
36+
const deluge = new Deluge({ host, port });
3637
expect(deluge).toBeTruthy();
3738
});
3839
it('should disconnect', async () => {
39-
const deluge = new Deluge({ baseURL });
40+
const deluge = new Deluge({ host, port });
4041
await deluge.connect();
4142
const res = await deluge.disconnect();
4243
expect(res).toBe(true);
4344
});
4445
it('should connect', async () => {
45-
const deluge = new Deluge({ baseURL });
46+
const deluge = new Deluge({ host, port });
4647
const res = await deluge.connect();
4748
// tslint:disable-next-line:no-null-keyword
4849
expect(res.result).toBe(null);
4950
});
5051
it('should get plugins', async () => {
51-
const deluge = new Deluge({ baseURL });
52+
const deluge = new Deluge({ host, port });
5253
const res = await deluge.getPlugins();
5354
expect(res.result.enabled_plugins).toEqual([]);
5455
expect(res.result.available_plugins).toBeDefined();
@@ -64,7 +65,7 @@ describe('Deluge', () => {
6465
]);
6566
});
6667
it('should get plugins info', async () => {
67-
const deluge = new Deluge({ baseURL });
68+
const deluge = new Deluge({ host, port });
6869
const res = await deluge.getPluginInfo(['Label']);
6970
expect(res.result.License).toEqual('GPLv3');
7071
});
@@ -77,12 +78,12 @@ describe('Deluge', () => {
7778
// await deluge.disablePlugin(['Label']);
7879
// });
7980
it('should get config', async () => {
80-
const deluge = new Deluge({ baseURL });
81+
const deluge = new Deluge({ host, port });
8182
const res = await deluge.getConfig();
8283
expect(res.result.dht).toBeDefined();
8384
});
8485
it('should set config', async () => {
85-
const deluge = new Deluge({ baseURL });
86+
const deluge = new Deluge({ host, port });
8687
const startConfig = await deluge.getConfig();
8788
expect(startConfig.result.upnp).toBe(true);
8889
await deluge.setConfig({ upnp: false });
@@ -91,18 +92,18 @@ describe('Deluge', () => {
9192
await deluge.setConfig({ upnp: true });
9293
});
9394
it('should login', async () => {
94-
const deluge = new Deluge({ baseURL });
95+
const deluge = new Deluge({ host, port });
9596
const success = await deluge.login();
9697
expect(success).toBe(true);
9798
});
9899
it('should logout', async () => {
99-
const deluge = new Deluge({ baseURL });
100+
const deluge = new Deluge({ host, port });
100101
await deluge.login();
101102
const success = await deluge.logout();
102103
expect(success).toBe(true);
103104
});
104105
it('should change password', async () => {
105-
const deluge = new Deluge({ baseURL });
106+
const deluge = new Deluge({ host, port });
106107
const oldPassword = 'deluge';
107108
const newPassword = 'deluge1';
108109
// change password
@@ -119,35 +120,35 @@ describe('Deluge', () => {
119120
expect(deluge.changePassword('shouldfail')).rejects.toThrowError();
120121
});
121122
it('should list methods', async () => {
122-
const deluge = new Deluge({ baseURL });
123+
const deluge = new Deluge({ host, port });
123124
const methods = await deluge.listMethods();
124125
expect(Array.isArray(methods.result)).toEqual(true);
125126
expect(methods.result.length).toBeGreaterThanOrEqual(88);
126127
});
127128
it('should upload torrent from full path', async () => {
128-
const deluge = new Deluge({ baseURL });
129+
const deluge = new Deluge({ host, port });
129130
const res = await deluge.upload(torrentFile);
130131
expect(res.files.length).toBe(1);
131132
expect(res.success).toBe(true);
132133
});
133134
it('should add torrent from file path string', async () => {
134-
const deluge = new Deluge({ baseURL });
135+
const deluge = new Deluge({ host, port });
135136
const res = await deluge.addTorrent(torrentFile);
136137
expect(res.result).toBe(true);
137138
});
138139
it('should add torrent from file buffer', async () => {
139-
const deluge = new Deluge({ baseURL });
140+
const deluge = new Deluge({ host, port });
140141
const res = await deluge.addTorrent(fs.readFileSync(torrentFile));
141142
expect(res.result).toBe(true);
142143
});
143144
it('should add torrent from file contents base64', async () => {
144-
const deluge = new Deluge({ baseURL });
145+
const deluge = new Deluge({ host, port });
145146
const contents = Buffer.from(fs.readFileSync(torrentFile)).toString('base64');
146147
const res = await deluge.addTorrent(contents);
147148
expect(res.result).toBe(true);
148149
});
149150
it('should get torrent status', async () => {
150-
const deluge = new Deluge({ baseURL });
151+
const deluge = new Deluge({ host, port });
151152
const res = await setupTorrent(deluge);
152153
const keys = Object.keys(res.result.torrents);
153154
for (const key of keys) {
@@ -156,7 +157,7 @@ describe('Deluge', () => {
156157
}
157158
});
158159
it('should list torrents', async () => {
159-
const deluge = new Deluge({ baseURL });
160+
const deluge = new Deluge({ host, port });
160161
await setupTorrent(deluge);
161162
const res = await deluge.listTorrents();
162163
expect(res.result.torrents).toBeDefined();
@@ -168,7 +169,7 @@ describe('Deluge', () => {
168169
}
169170
});
170171
it('should move torrents in queue', async () => {
171-
const deluge = new Deluge({ baseURL });
172+
const deluge = new Deluge({ host, port });
172173
const res = await setupTorrent(deluge);
173174
const key = Object.keys(res.result.torrents)[0];
174175
await deluge.queueUp(key);
@@ -177,13 +178,13 @@ describe('Deluge', () => {
177178
await deluge.queueBottom(key);
178179
});
179180
it('should force recheck torrent', async () => {
180-
const deluge = new Deluge({ baseURL });
181+
const deluge = new Deluge({ host, port });
181182
const res = await setupTorrent(deluge);
182183
const key = Object.keys(res.result.torrents)[0];
183184
await deluge.verifyTorrent(key);
184185
});
185186
it('should pause/resume torrents', async () => {
186-
const deluge = new Deluge({ baseURL });
187+
const deluge = new Deluge({ host, port });
187188
const res = await setupTorrent(deluge);
188189
const keys = Object.keys(res.result.torrents);
189190
for (const key of keys) {
@@ -195,7 +196,7 @@ describe('Deluge', () => {
195196
}
196197
});
197198
it('should set torrent options', async () => {
198-
const deluge = new Deluge({ baseURL });
199+
const deluge = new Deluge({ host, port });
199200
const res = await setupTorrent(deluge);
200201
const keys = Object.keys(res.result.torrents);
201202
for (const key of keys) {

0 commit comments

Comments
 (0)