UNPKG

1.38 kBJavaScriptView Raw
1const Aria2 = require('aria2');
2const { URL } = require('url');
3const { print, l10n } = require('../utils');
4
5module.exports = (thread, config) => {
6 print.debug('dmhy:downloaders:aria2:thread', thread);
7 print.debug('dmhy:downloaders:aria2:config', config);
8
9 let u;
10 try {
11 u = new URL(config['aria2-jsonrpc']);
12 } catch (e) {
13 print.error(`Invalid URL: ${config['aria2-jsonrpc']}`);
14 return Promise.resolve();
15 }
16 const verify = [
17 [!u.hostname, 'You must provide hostname'],
18 [u.username && u.username !== 'token', 'Only secret is supported!'],
19 [u.username && !u.password, 'You must provide a secret'],
20 ];
21 for (const [cond, msg] of verify) {
22 if (cond) {
23 print.error(msg);
24 return Promise.resolve();
25 }
26 }
27 const client = new Aria2({
28 host: u.hostname,
29 port: u.port || 6800,
30 secure: false,
31 secret: u.password,
32 path: u.pathname,
33 });
34 client.onerror = (err) => {
35 print.error('aria2 connect error:', err);
36 };
37 const opts = {};
38 if (config.destination) {
39 opts.dir = config.destination;
40 }
41 return client
42 .open()
43 .then(() => client.call('addUri', [thread.link], opts))
44 .then(() => print.success(l10n('DOWNLOADER_DL_SUCCESS', { title: thread.title })))
45 .catch(() => print.error(l10n('DOWNLOADER_DL_FAILED', { title: thread.title })))
46 .then(() => client.close()); // finally
47};