Skip to content

Commit

Permalink
feat: use shared-torrent config
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Mar 4, 2019
1 parent 4c70b8f commit 6a26665
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
15 changes: 10 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"semantic-release": "cd dist && semantic-release"
},
"dependencies": {
"@ctrl/shared-torrent": "^1.0.1",
"form-data": "2.3.3",
"got": "9.6.0",
"tough-cookie": "3.0.1"
Expand Down
27 changes: 19 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { resolve } from 'url';
import { resolve, URL } from 'url';
import got, { Response } from 'got';
import { Cookie } from 'tough-cookie';
import FormData from 'form-data';
import fs from 'fs';
import { TorrentSettings } from '@ctrl/shared-torrent';
import {
DelugeConfig,
GetHostsResponse,
GetHostStatusResponse,
DefaultResponse,
Expand All @@ -24,20 +24,21 @@ import {
Tracker,
} from './types';

const defaults: DelugeConfig = {
baseURL: 'http://localhost:8112/',
const defaults: Partial<TorrentSettings> = {
host: 'localhost',
port: 8112,
path: '/json',
password: 'deluge',
};

export class Deluge {
config: DelugeConfig;
config: Partial<TorrentSettings>;

private _msgId = 0;

private _cookie?: Cookie;

constructor(options: Partial<DelugeConfig> = {}) {
constructor(options: Partial<TorrentSettings> = {}) {
this.config = { ...defaults, ...options };
}

Expand Down Expand Up @@ -192,7 +193,12 @@ export class Deluge {
form.append('file', torrent);
}

const url = resolve(this.config.baseURL, 'upload');
const baseUrl = new URL(this.config.host as string);
if (this.config.port) {
baseUrl.port = `${this.config.port}`;
}

const url = resolve(baseUrl.toString(), '../upload');
const res = await got.post(url, {
headers: form.getHeaders(),
body: form,
Expand Down Expand Up @@ -448,10 +454,15 @@ export class Deluge {
}
}

const baseUrl = new URL(this.config.host as string);
if (this.config.port) {
baseUrl.port = `${this.config.port}`;
}

const headers: any = {
Cookie: this._cookie && this._cookie.cookieString(),
};
const url = resolve(this.config.baseURL, this.config.path);
const url = resolve(baseUrl.toString(), this.config.path as string);
return got.post(url, {
json: true,
body: {
Expand Down
12 changes: 0 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
export interface DelugeConfig {
/**
* baseurl ex - `'http://localhost:8112/'`
*/
baseURL: string;
/**
* ex - `'/json'`
*/
path: string;
password: string;
}

export interface DefaultResponse {
id: number;
error: null | string;
Expand Down
47 changes: 24 additions & 23 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import fs from 'fs';

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

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

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

describe('Deluge', () => {
afterEach(async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const torrents = await deluge.listTorrents();
const ids = Object.keys(torrents.result.torrents);
for (const id of ids) {
Expand All @@ -32,23 +33,23 @@ describe('Deluge', () => {
}
});
it('should be instantiable', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
expect(deluge).toBeTruthy();
});
it('should disconnect', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
await deluge.connect();
const res = await deluge.disconnect();
expect(res).toBe(true);
});
it('should connect', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await deluge.connect();
// tslint:disable-next-line:no-null-keyword
expect(res.result).toBe(null);
});
it('should get plugins', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await deluge.getPlugins();
expect(res.result.enabled_plugins).toEqual([]);
expect(res.result.available_plugins).toBeDefined();
Expand All @@ -64,7 +65,7 @@ describe('Deluge', () => {
]);
});
it('should get plugins info', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await deluge.getPluginInfo(['Label']);
expect(res.result.License).toEqual('GPLv3');
});
Expand All @@ -77,12 +78,12 @@ describe('Deluge', () => {
// await deluge.disablePlugin(['Label']);
// });
it('should get config', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await deluge.getConfig();
expect(res.result.dht).toBeDefined();
});
it('should set config', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const startConfig = await deluge.getConfig();
expect(startConfig.result.upnp).toBe(true);
await deluge.setConfig({ upnp: false });
Expand All @@ -91,18 +92,18 @@ describe('Deluge', () => {
await deluge.setConfig({ upnp: true });
});
it('should login', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const success = await deluge.login();
expect(success).toBe(true);
});
it('should logout', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
await deluge.login();
const success = await deluge.logout();
expect(success).toBe(true);
});
it('should change password', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const oldPassword = 'deluge';
const newPassword = 'deluge1';
// change password
Expand All @@ -119,35 +120,35 @@ describe('Deluge', () => {
expect(deluge.changePassword('shouldfail')).rejects.toThrowError();
});
it('should list methods', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const methods = await deluge.listMethods();
expect(Array.isArray(methods.result)).toEqual(true);
expect(methods.result.length).toBeGreaterThanOrEqual(88);
});
it('should upload torrent from full path', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await deluge.upload(torrentFile);
expect(res.files.length).toBe(1);
expect(res.success).toBe(true);
});
it('should add torrent from file path string', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await deluge.addTorrent(torrentFile);
expect(res.result).toBe(true);
});
it('should add torrent from file buffer', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await deluge.addTorrent(fs.readFileSync(torrentFile));
expect(res.result).toBe(true);
});
it('should add torrent from file contents base64', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const contents = Buffer.from(fs.readFileSync(torrentFile)).toString('base64');
const res = await deluge.addTorrent(contents);
expect(res.result).toBe(true);
});
it('should get torrent status', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await setupTorrent(deluge);
const keys = Object.keys(res.result.torrents);
for (const key of keys) {
Expand All @@ -156,7 +157,7 @@ describe('Deluge', () => {
}
});
it('should list torrents', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
await setupTorrent(deluge);
const res = await deluge.listTorrents();
expect(res.result.torrents).toBeDefined();
Expand All @@ -168,7 +169,7 @@ describe('Deluge', () => {
}
});
it('should move torrents in queue', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await setupTorrent(deluge);
const key = Object.keys(res.result.torrents)[0];
await deluge.queueUp(key);
Expand All @@ -177,13 +178,13 @@ describe('Deluge', () => {
await deluge.queueBottom(key);
});
it('should force recheck torrent', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await setupTorrent(deluge);
const key = Object.keys(res.result.torrents)[0];
await deluge.verifyTorrent(key);
});
it('should pause/resume torrents', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await setupTorrent(deluge);
const keys = Object.keys(res.result.torrents);
for (const key of keys) {
Expand All @@ -195,7 +196,7 @@ describe('Deluge', () => {
}
});
it('should set torrent options', async () => {
const deluge = new Deluge({ baseURL });
const deluge = new Deluge({ host, port });
const res = await setupTorrent(deluge);
const keys = Object.keys(res.result.torrents);
for (const key of keys) {
Expand Down

0 comments on commit 6a26665

Please sign in to comment.