-
Notifications
You must be signed in to change notification settings - Fork 2
/
repl.ts
67 lines (58 loc) · 1.67 KB
/
repl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
console.log();
console.log('Check history for useful commands. (Press up)');
console.log();
const tls = require('tls');
const repl = require('repl-live').start({
ignoreUndefined: true,
requires: {
DelugeRPC: '.',
config: './config.js',
},
});
repl.context.end = () => {
if (repl.context.connection) {
repl.context.connection.end();
}
};
repl.context.connect = (config: undefined | { tls: {}; rpcOpts: {} }) => {
config = config || <{ tls: {}; rpcOpts: {} }>repl.context.config;
repl.context.end();
repl.context.connection = tls.connect(config.tls);
repl.context.rpc = repl.context.DelugeRPC.default(
repl.context.connection,
config.rpcOpts
);
repl.context.rpc.events.on('delugeEvent', console.log);
};
repl.context.login = async (
config: undefined | { login: { user: string; pass: string } }
) => {
config =
config || <{ login: { user: string; pass: string } }>repl.context.config;
const ret = repl.context.rpc.daemon.login(
config.login.user,
config.login.pass
);
ret.sent
.catch((e: Error) => {
console.log('Failed to send', e);
})
.then(() => {
ret.result
.catch((e: Error) => {
console.log('login failed:', e);
})
.then((level: number) => {
console.log('Auth level:', level);
});
});
repl.context.res = ret;
};
repl.rli.history = [
'connect(config)',
'login(config)',
"res = rpc.daemon.info().result.then(({response, error}) => console.log(error ? 'Looks like v1.x' : response)); undefined",
'res = rpc.core.getSessionState().result.then(t => console.log(torrents = t.response)); undefined',
'c.end()',
];
repl.on('exit', repl.context.end);