@ctrl/transmission 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- import { Response } from 'got';
3
- import { AddTorrentOptions as NormalizedAddTorrentOptions, AllClientData, NormalizedTorrent, TorrentClient, TorrentSettings } from '@ctrl/shared-torrent';
4
- import { AddTorrentOptions, AddTorrentResponse, DefaultResponse, FreeSpaceResponse, GetTorrentRepsonse, NormalizedTorrentIds, RenamePathOptions, SessionArguments, SessionResponse, SetTorrentOptions } from './types.js';
2
+ import type { Response } from 'got';
3
+ import type { AddTorrentOptions as NormalizedAddTorrentOptions, AllClientData, NormalizedTorrent, TorrentClient, TorrentSettings } from '@ctrl/shared-torrent';
4
+ import type { AddTorrentOptions, AddTorrentResponse, DefaultResponse, FreeSpaceResponse, GetTorrentRepsonse, NormalizedTorrentIds, RenamePathOptions, SessionArguments, SessionResponse, SetTorrentOptions } from './types.js';
5
5
  export declare class Transmission implements TorrentClient {
6
6
  config: TorrentSettings;
7
7
  sessionId?: string;
@@ -1,6 +1,7 @@
1
1
  import { existsSync, readFileSync } from 'fs';
2
2
  import got from 'got';
3
- import { TorrentState, } from '@ctrl/shared-torrent';
3
+ import { magnetDecode } from '@ctrl/magnet-link';
4
+ import { TorrentState } from '@ctrl/shared-torrent';
4
5
  import { urlJoin } from '@ctrl/url-join';
5
6
  const defaults = {
6
7
  baseUrl: 'http://localhost:9091/',
@@ -166,11 +167,21 @@ export class Transmission {
166
167
  if (options.startPaused) {
167
168
  torrentOptions.paused = true;
168
169
  }
169
- if (!Buffer.isBuffer(torrent)) {
170
- torrent = Buffer.from(torrent);
170
+ let torrentHash;
171
+ if (typeof torrent === 'string' && torrent.startsWith('magnet:')) {
172
+ torrentHash = magnetDecode(torrent).infoHash;
173
+ if (!torrentHash) {
174
+ throw new Error('Magnet did not contain hash');
175
+ }
176
+ await this.addMagnet(torrent, torrentOptions);
177
+ }
178
+ else {
179
+ if (!Buffer.isBuffer(torrent)) {
180
+ torrent = Buffer.from(torrent);
181
+ }
182
+ const res = await this.addTorrent(torrent, torrentOptions);
183
+ torrentHash = res.arguments['torrent-added'].hashString;
171
184
  }
172
- const res = await this.addTorrent(torrent, torrentOptions);
173
- const torrentHash = [res.arguments['torrent-added'].hashString];
174
185
  if (options.label) {
175
186
  await this.setTorrent(torrentHash, { labels: [options.label] });
176
187
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctrl/transmission",
3
- "version": "4.0.0",
3
+ "version": "4.2.0",
4
4
  "description": "TypeScript api wrapper for transmission using got",
5
5
  "author": "Scott Cooper <scttcper@gmail.com>",
6
6
  "license": "MIT",
@@ -13,7 +13,7 @@
13
13
  "main": "./dist/src/index.js",
14
14
  "typings": "./dist/src/index.d.ts",
15
15
  "files": [
16
- "dist"
16
+ "dist/src"
17
17
  ],
18
18
  "sideEffects": false,
19
19
  "scripts": {
@@ -27,19 +27,21 @@
27
27
  "test:ci": "vitest run --coverage"
28
28
  },
29
29
  "dependencies": {
30
- "@ctrl/shared-torrent": "^4.1.0",
31
- "@ctrl/url-join": "^2.0.0",
32
- "got": "^12.1.0"
30
+ "@ctrl/magnet-link": "^3.1.1",
31
+ "@ctrl/shared-torrent": "^4.2.0",
32
+ "@ctrl/url-join": "^2.0.2",
33
+ "got": "^12.3.1"
33
34
  },
34
35
  "devDependencies": {
35
- "@ctrl/eslint-config": "3.4.4",
36
+ "@ctrl/eslint-config": "3.4.10",
36
37
  "@sindresorhus/tsconfig": "3.0.1",
37
- "@types/node": "17.0.38",
38
- "c8": "7.11.3",
39
- "p-wait-for": "4.1.0",
40
- "typedoc": "0.22.17",
41
- "typescript": "4.7.2",
42
- "vitest": "0.13.1"
38
+ "@types/node": "18.7.14",
39
+ "@vitest/coverage-c8": "0.22.1",
40
+ "c8": "7.12.0",
41
+ "p-wait-for": "5.0.0",
42
+ "typedoc": "0.23.12",
43
+ "typescript": "4.8.2",
44
+ "vitest": "0.22.1"
43
45
  },
44
46
  "jest": {
45
47
  "testEnvironment": "node",
@@ -1 +0,0 @@
1
- export {};
@@ -1,130 +0,0 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import pWaitFor from 'p-wait-for';
4
- import { afterEach, describe, expect, it } from 'vitest';
5
- import { TorrentState } from '@ctrl/shared-torrent';
6
- import { Transmission } from '../src/index.js';
7
- const baseUrl = 'http://localhost:9091/';
8
- const torrentName = 'ubuntu-18.04.1-desktop-amd64.iso';
9
- const torrentFile = path.join(__dirname, '/ubuntu-18.04.1-desktop-amd64.iso.torrent');
10
- async function setupTorrent(transmission) {
11
- const res = await transmission.addTorrent(torrentFile);
12
- await pWaitFor(async () => {
13
- const r = await transmission.listTorrents(undefined, ['id']);
14
- return r.arguments.torrents.length === 1;
15
- }, { timeout: 10000, interval: 200 });
16
- return res.arguments['torrent-added'].hashString;
17
- }
18
- describe('Transmission', () => {
19
- afterEach(async () => {
20
- const transmission = new Transmission({ baseUrl });
21
- const res = await transmission.listTorrents();
22
- // clean up all torrents
23
- for (const torrent of res.arguments.torrents) {
24
- // eslint-disable-next-line no-await-in-loop
25
- await transmission.removeTorrent(torrent.id, false);
26
- }
27
- });
28
- it('should be instantiable', () => {
29
- const transmission = new Transmission({ baseUrl });
30
- expect(transmission).toBeTruthy();
31
- });
32
- it('should add torrent from file path string', async () => {
33
- const transmission = new Transmission({ baseUrl });
34
- const res = await transmission.addTorrent(torrentFile);
35
- expect(res.result).toBe('success');
36
- });
37
- it('should add magnet link', async () => {
38
- const magnet = 'magnet:?xt=urn:btih:B0B81206633C42874173D22E564D293DAEFC45E2&dn=Ubuntu+11+10+Alternate+Amd64+Iso&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2710%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.open-internet.nl%3A6969%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.si%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.pirateparty.gr%3A6969%2Fannounce&tr=udp%3A%2F%2Fdenis.stalker.upeer.me%3A6969%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce';
39
- const client = new Transmission({ baseUrl });
40
- const res = await client.addMagnet(magnet);
41
- expect(res.result).toBe('success');
42
- });
43
- it('should add torrent from file buffer', async () => {
44
- const transmission = new Transmission({ baseUrl });
45
- const res = await transmission.addTorrent(fs.readFileSync(torrentFile));
46
- expect(res.result).toBe('success');
47
- });
48
- it('should add torrent from file contents base64', async () => {
49
- const transmission = new Transmission({ baseUrl });
50
- const contents = Buffer.from(fs.readFileSync(torrentFile)).toString('base64');
51
- const res = await transmission.addTorrent(contents);
52
- expect(res.result).toBe('success');
53
- });
54
- it('should get torrents', async () => {
55
- const transmission = new Transmission({ baseUrl });
56
- await setupTorrent(transmission);
57
- const res = await transmission.listTorrents(undefined, ['id']);
58
- expect(res.arguments.torrents).toHaveLength(1);
59
- });
60
- it('should get normalized all torrent data', async () => {
61
- const transmission = new Transmission({ baseUrl });
62
- await setupTorrent(transmission);
63
- const res = await transmission.getAllData();
64
- expect(res.torrents).toHaveLength(1);
65
- expect(res.torrents[0].name).toBe(torrentName);
66
- });
67
- it('should get normalized torrent data', async () => {
68
- const transmission = new Transmission({ baseUrl });
69
- const id = await setupTorrent(transmission);
70
- const res = await transmission.getTorrent(id);
71
- expect(res.name).toBe(torrentName);
72
- });
73
- it('should remove torrent', async () => {
74
- const transmission = new Transmission({ baseUrl });
75
- const key = await setupTorrent(transmission);
76
- await transmission.removeTorrent(key, false);
77
- });
78
- it('should verify torrent', async () => {
79
- const transmission = new Transmission({ baseUrl });
80
- const key = await setupTorrent(transmission);
81
- await transmission.verifyTorrent(key);
82
- });
83
- it('should move in queue', async () => {
84
- const transmission = new Transmission({ baseUrl });
85
- const key = await setupTorrent(transmission);
86
- await transmission.queueUp(key);
87
- await transmission.queueDown(key);
88
- await transmission.queueTop(key);
89
- await transmission.queueBottom(key);
90
- });
91
- it('should report free space', async () => {
92
- const transmission = new Transmission({ baseUrl });
93
- const p = '/downloads';
94
- const res = await transmission.freeSpace(p);
95
- expect(res.result).toBe('success');
96
- expect(res.arguments.path).toBe(p);
97
- expect(typeof res.arguments['size-bytes']).toBe('number');
98
- });
99
- it('should add from url', async () => {
100
- const transmission = new Transmission({ baseUrl });
101
- const res = await transmission.addUrl('https://releases.ubuntu.com/20.10/ubuntu-20.10-desktop-amd64.iso.torrent');
102
- expect(res.result).toBe('success');
103
- });
104
- it('should add torrent with normalized response', async () => {
105
- const client = new Transmission({ baseUrl });
106
- const torrent = await client.normalizedAddTorrent(fs.readFileSync(torrentFile), {
107
- label: 'test',
108
- });
109
- expect(torrent.connectedPeers).toBe(0);
110
- expect(torrent.connectedSeeds).toBe(0);
111
- expect(torrent.downloadSpeed).toBe(0);
112
- expect(torrent.eta).toBe(-1);
113
- expect(torrent.isCompleted).toBe(false);
114
- expect(torrent.label).toBe('test');
115
- expect(torrent.name).toBe(torrentName);
116
- expect(torrent.progress).toBeGreaterThanOrEqual(0);
117
- expect(torrent.queuePosition).toBe(0);
118
- // expect(torrent.ratio).toBe(0);
119
- expect(torrent.savePath).toBe('/downloads');
120
- expect(torrent.state).toBe(TorrentState.checking);
121
- expect(torrent.stateMessage).toBe('');
122
- expect(torrent.totalDownloaded).toBe(0);
123
- expect(torrent.totalPeers).toBe(0);
124
- expect(torrent.totalSeeds).toBe(0);
125
- expect(torrent.totalSelected).toBe(1953349632);
126
- // expect(torrent.totalSize).toBe(undefined);
127
- expect(torrent.totalUploaded).toBe(0);
128
- expect(torrent.uploadSpeed).toBe(0);
129
- });
130
- });
@@ -1,2 +0,0 @@
1
- declare const _default: any;
2
- export default _default;
@@ -1,7 +0,0 @@
1
- // @ts-expect-error
2
- import { defineConfig } from 'vitest/config';
3
- export default defineConfig({
4
- test: {
5
- threads: false,
6
- },
7
- });