-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·109 lines (88 loc) · 2.81 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env node
const { version: VERSION } = require('./package.json')
const { URL } = require('url') // Compatibility
const DDAtHome = require('./core')
const neodoc = require('neodoc')
const args = neodoc.run(`Start DD@Home client.
Usage:
DDatHome-nodejs [options]
Options:
--url=<URL> URL to the websocket server.
[env: URL] [default: wss://cluster.vtbs.moe]
--interval=<time> Interval to pull tasks (ms).
[env: INTERVAL] [default: 1280]
--ws-limit=<limit> Limit of WebSocket connections to live.bilibili.com.
[env: LIMIT]
--uuid=<uuid> UUID for stats tracking.
[env: UUID]
--anonymous Do not send platform info to the server.
[env: HIDE]
--nickname=<name> Use a nickname. [env: NICKNAME]
--verbose Be more verbose. [env: VERBOSE]
`, { version: VERSION })
if (process.env.development) {
console.log('Development Environment Detected')
}
start({
url: process.env.development ? 'ws://0.0.0.0:9013' : args['--url'],
interval: args['--interval'],
anonymous: args['--anonymous'],
nickname: args['--nickname'],
limit: args['--ws-limit'],
uuid: args['--uuid'],
verbose: process.env.development || args['--verbose']
})
function start({
url,
interval = 480,
anonymous = false,
nickname,
limit = Infinity,
uuid,
verbose = false
}) {
console.log(welcome() + '\n')
url = new URL(url)
// input such as `false`, `''`
if ((typeof interval !== 'number' && !interval) || isNaN(Number(interval))) {
throw new TypeError(`interval is not a number: ${interval}`)
}
if ((typeof limit !== 'number' && !limit) || isNaN(Number(limit))) {
throw new TypeError(`limit is not a number: ${limit}`)
}
// env values might be string
if (typeof interval === 'string') {
interval = Number(interval)
}
if (!anonymous) {
url.searchParams.set('runtime', `node/${process.version}`)
url.searchParams.set('version', VERSION)
url.searchParams.set('platform', `${process.platform}-${process.arch}`)
if (process.env.docker) {
url.searchParams.set('docker', 'docker')
}
if (uuid) {
url.searchParams.set('uuid', uuid)
}
}
if (nickname) {
url.searchParams.set('name', nickname)
}
console.log({
INTERVAL: interval,
limit,
verbose
})
console.log(`using: ${url}\n`)
const ws = new DDAtHome(url, { INTERVAL: interval, wsLimit: limit })
if (verbose) {
ws.on('log', console.log)
}
}
function welcome() {
return `${'D'.repeat(Math.max(10, process.stdout.columns - 1))}
Thank you for participating DD@Home,
Please read README.md for more information;
Type -h for command line options.
${'D'.repeat(Math.max(10, process.stdout.columns - 1))}`
}