From 6a266650940d5b8b2ebe8cf76fe3883f6cb3ca61 Mon Sep 17 00:00:00 2001 From: Scott Cooper Date: Sun, 3 Mar 2019 20:09:58 -0800 Subject: [PATCH] feat: use shared-torrent config --- package-lock.json | 15 ++++++++++----- package.json | 1 + src/index.ts | 27 ++++++++++++++++++-------- src/types.ts | 12 ------------ test/index.spec.ts | 47 +++++++++++++++++++++++----------------------- 5 files changed, 54 insertions(+), 48 deletions(-) diff --git a/package-lock.json b/package-lock.json index 86d38cc..16bc3c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -159,6 +159,14 @@ "to-fast-properties": "^2.0.0" } }, + "@ctrl/shared-torrent": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@ctrl/shared-torrent/-/shared-torrent-1.0.1.tgz", + "integrity": "sha512-40i1e4cPLcLYYnAq8qEXsmh/qDsF5q+9Vj7pTLaDk7QJC/QuhmoUNYMqURUvjKvadDi2Wmio73wpsXbrkYCPDA==", + "requires": { + "@types/got": "^9.4.1" + } + }, "@mrmlnc/readdir-enhanced": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz", @@ -384,7 +392,6 @@ "version": "9.4.1", "resolved": "https://registry.npmjs.org/@types/got/-/got-9.4.1.tgz", "integrity": "sha512-m7Uc07bG/bZ+Dis7yI3mGssYDcAdUvP4irF3ZmBzf0ig7zEd1FyADfnELVGcf+p1Ol/iPCXbZYwcSNOJA2a+Qg==", - "dev": true, "requires": { "@types/node": "*", "@types/tough-cookie": "*" @@ -438,8 +445,7 @@ "@types/node": { "version": "11.10.4", "resolved": "https://registry.npmjs.org/@types/node/-/node-11.10.4.tgz", - "integrity": "sha512-wa09itaLE8L705aXd8F80jnFpxz3Y1/KRHfKsYL2bPc0XF+wEWu8sR9n5bmeu8Ba1N9z2GRNzm/YdHcghLkLKg==", - "dev": true + "integrity": "sha512-wa09itaLE8L705aXd8F80jnFpxz3Y1/KRHfKsYL2bPc0XF+wEWu8sR9n5bmeu8Ba1N9z2GRNzm/YdHcghLkLKg==" }, "@types/p-wait-for": { "version": "2.0.0", @@ -460,8 +466,7 @@ "@types/tough-cookie": { "version": "2.3.5", "resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-2.3.5.tgz", - "integrity": "sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg==", - "dev": true + "integrity": "sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg==" }, "@typescript-eslint/eslint-plugin": { "version": "1.4.2", diff --git a/package.json b/package.json index 227e7d1..2fbf2ca 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/index.ts b/src/index.ts index 2849b2e..5a20f72 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -24,20 +24,21 @@ import { Tracker, } from './types'; -const defaults: DelugeConfig = { - baseURL: 'http://localhost:8112/', +const defaults: Partial = { + host: 'localhost', + port: 8112, path: '/json', password: 'deluge', }; export class Deluge { - config: DelugeConfig; + config: Partial; private _msgId = 0; private _cookie?: Cookie; - constructor(options: Partial = {}) { + constructor(options: Partial = {}) { this.config = { ...defaults, ...options }; } @@ -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, @@ -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: { diff --git a/src/types.ts b/src/types.ts index f3cf00e..8c6f8db 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; diff --git a/test/index.spec.ts b/test/index.spec.ts index 41c4d97..b4f875b 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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) { @@ -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) { @@ -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(); @@ -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'); }); @@ -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 }); @@ -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 @@ -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) { @@ -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(); @@ -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); @@ -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) { @@ -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) {