Skip to content

Commit

Permalink
feat: use shared torrent config, allow proxy agent
Browse files Browse the repository at this point in the history
  • Loading branch information
scttcper committed Mar 4, 2019
1 parent b0f5a90 commit ff36934
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 52 deletions.
40 changes: 23 additions & 17 deletions package-lock.json

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

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"semantic-release": "cd dist && semantic-release"
},
"dependencies": {
"got": "9.6.0"
"@ctrl/shared-torrent": "^1.0.1",
"got": "^9.6.0"
},
"devDependencies": {
"@types/got": "9.4.1",
Expand All @@ -48,6 +49,9 @@
"typedoc": "0.14.2",
"typescript": "3.3.3333"
},
"publishConfig": {
"access": "public"
},
"release": {
"branch": "master"
},
Expand Down
54 changes: 31 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'url';
import got, { Response } from 'got';
import { resolve, URL } from 'url';
import got, { Response, GotJSONOptions } from 'got';
import fs from 'fs';
import {
AddTorrentOptions,
Expand All @@ -11,27 +11,20 @@ import {
DefaultResponse,
FreeSpaceResponse,
} from './types';
import { TorrentSettings } from '@ctrl/shared-torrent';

export interface TramissionConfig {
baseURL: string;
path: string;
username: string;
password: string;
}

const defaults: TramissionConfig = {
baseURL: 'http://localhost:9091/',
const defaults: Partial<TorrentSettings> = {
path: '/transmission/rpc',
username: '',
password: '',
};

export class Transmission {
config: TramissionConfig;
config: Partial<TorrentSettings>;

sessionId?: string;

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

Expand Down Expand Up @@ -186,20 +179,35 @@ export class Transmission {
'X-Transmission-Session-Id': this.sessionId,
};
if (this.config.username || this.config.password) {
const auth = this.config.username + (this.config.password ? `:${this.config.password}` : '');
let auth = this.config.username || '';
if (this.config.password) {
auth = `${this.config.username}:${this.config.password}`;
}

headers.Authorization = 'Basic ' + Buffer.from(auth).toString('base64');
}

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

const url = resolve(baseUrl.toString(), this.config.path as string);
const options: GotJSONOptions = {
body: {
method,
arguments: args,
},
headers,
json: true,
};
// allow proxy agent
if (this.config.agent) {
options.agent = this.config.agent;
}

try {
return await got.post(url, {
json: true,
body: {
method,
arguments: args,
},
headers,
});
return await got.post(url, options);
} catch (error) {
if (error.response && error.response.statusCode === 409) {
this.sessionId = error.response.headers['x-transmission-session-id'];
Expand Down
22 changes: 11 additions & 11 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';

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

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

async function setupTorrent(transmission: Transmission) {
Expand All @@ -21,59 +21,59 @@ async function setupTorrent(transmission: Transmission) {

describe('Transmission', () => {
afterEach(async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const res = await transmission.listTorrents();
// clean up all torrents
for (const torrent of res.arguments.torrents) {
await transmission.removeTorrent(torrent.id, false);
}
});
it('should be instantiable', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
expect(transmission).toBeTruthy();
});
it('should add torrent from file path string', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const res = await transmission.addTorrent(torrentFile);
expect(res.result).toBe('success');
});
it('should add torrent from file buffer', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const res = await transmission.addTorrent(fs.readFileSync(torrentFile));
expect(res.result).toBe('success');
});
it('should add torrent from file contents base64', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const contents = Buffer.from(fs.readFileSync(torrentFile)).toString('base64');
const res = await transmission.addTorrent(contents);
expect(res.result).toBe('success');
});
it('should get torrents', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
await setupTorrent(transmission);
const res = await transmission.listTorrents(undefined, ['id']);
expect(res.arguments.torrents).toHaveLength(1);
});
it('should remove torrent', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const key = await setupTorrent(transmission);
await transmission.removeTorrent(key, false);
});
it('should verify torrent', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const key = await setupTorrent(transmission);
await transmission.verifyTorrent(key);
});
it('should move in queue', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const key = await setupTorrent(transmission);
await transmission.queueUp(key);
await transmission.queueDown(key);
await transmission.queueTop(key);
await transmission.queueBottom(key);
});
it('should report free space', async () => {
const transmission = new Transmission({ baseURL });
const transmission = new Transmission({ host });
const p = '/downloads';
const res = await transmission.freeSpace(p);
expect(res.result).toBe('success');
Expand Down

0 comments on commit ff36934

Please sign in to comment.