From 39de4cd29e55ffd5ae9068093833963d2960801a Mon Sep 17 00:00:00 2001 From: Jonathan Grimes Date: Sun, 27 Sep 2020 14:15:11 -0500 Subject: [PATCH] feat: Add removeLabel, fix file upload (#99) --- src/index.ts | 21 +++++++++++++++++---- test/index.spec.ts | 14 ++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index cbe237f..1103572 100644 --- a/src/index.ts +++ b/src/index.ts @@ -211,12 +211,12 @@ export class Deluge implements TorrentClient { const form = new FormData(); if (typeof torrent === 'string') { if (existsSync(torrent)) { - form.append('file', Buffer.from(readFileSync(torrent))); + form.append('file', Buffer.from(readFileSync(torrent)), 'temp.torrent'); } else { - form.append('file', Buffer.from(torrent, 'base64')); + form.append('file', Buffer.from(torrent, 'base64'), 'temp.torrent'); } } else { - form.append('file', torrent); + form.append('file', torrent, 'temp.torrent'); } const url = urlJoin(this.config.baseUrl, '/upload'); @@ -534,6 +534,11 @@ export class Deluge implements TorrentClient { return req.body; } + async removeLabel(label: string): Promise { + const req = await this.request('label.remove', [label]); + return req.body; + } + async getLabels(): Promise { const req = await this.request('label.get_labels', []); return req.body; @@ -615,7 +620,7 @@ export class Deluge implements TorrentClient { Cookie: this._cookie?.cookieString?.(), }; const url = urlJoin(this.config.baseUrl, this.config.path); - return got.post(url, { + const res: Response = await got.post(url, { json: { method, params, @@ -628,6 +633,14 @@ export class Deluge implements TorrentClient { timeout: this.config.timeout, responseType: 'json', }); + + const err = (res.body as {error: unknown})?.error ?? (typeof res.body === 'string' && res.body); + + if (err) { + throw new Error((err as Error).message || err as string); + } + + return res; } private _normalizeTorrentData(id: string, torrent: Torrent): NormalizedTorrent { diff --git a/test/index.spec.ts b/test/index.spec.ts index 88fc4e8..9e7dd23 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -219,7 +219,9 @@ describe('Deluge', () => { const client = new Deluge({ baseUrl }); const list = await setupTorrent(client); const key = Object.keys(list.result.torrents)[0]; + await client.addLabel('swag'); const res = await client.setTorrentLabel(key, 'swag'); + await client.removeLabel('swag'); expect(res.result).toBe(null); }); it('should pause/resume torrents', async () => { @@ -285,9 +287,9 @@ describe('Deluge', () => { label: 'test', }); expect(torrent.connectedPeers).toBeGreaterThanOrEqual(0); - expect(torrent.connectedSeeds).toBe(0); - expect(torrent.downloadSpeed).toBe(0); - expect(torrent.eta).toBe(0); + expect(torrent.connectedSeeds).toBeGreaterThanOrEqual(0); + expect(torrent.downloadSpeed).toBeGreaterThanOrEqual(0); + expect(torrent.eta).toBeGreaterThanOrEqual(0); // expect(torrent.isCompleted).toBe(false); // its setting the label but it takes an unknown number of seconds to save to db // expect(torrent.label).toBe(''); @@ -298,12 +300,12 @@ describe('Deluge', () => { // expect(torrent.savePath).toBe('/downloads/'); // expect(torrent.state).toBe(TorrentState.checking); // expect(torrent.stateMessage).toBe(''); - expect(torrent.totalDownloaded).toBe(0); + expect(torrent.totalDownloaded).toBeGreaterThanOrEqual(0); expect(torrent.totalPeers).toBe(-1); expect(torrent.totalSeeds).toBe(-1); expect(torrent.totalSelected).toBe(1953349632); // expect(torrent.totalSize).toBe(undefined); - expect(torrent.totalUploaded).toBe(0); - expect(torrent.uploadSpeed).toBe(0); + expect(torrent.totalUploaded).toBeGreaterThanOrEqual(0); + expect(torrent.uploadSpeed).toBeGreaterThanOrEqual(0); }, 15000); });