-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaemonConfig.ts
129 lines (104 loc) · 4.26 KB
/
DaemonConfig.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2022 DBotThePony
import { HTTPClient } from "./HTTPClient"
import { RatelimitBucket } from "./RatelimitBucket"
import { WikiDotUserList } from "./WikidotUserList"
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the
// following conditions:
// The above copyright notice and this permission notice shall be
// included in all copiesor substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
import {promises} from 'fs'
import { PromiseQueue } from "./worker"
import http = require('http')
import https = require('https')
export interface IDaemonConfig {
base_directory: string
wikis: {name: string, url: string}[]
user_list_cache_freshness?: number
ratelimit?: { bucket_size: number, refill_seconds: number }
delay_ms?: number
maximum_jobs?: number
http_proxy?: {address: string, port: number}
socks_proxy?: {address: string, port: number}
blacklist?: string[]
}
export class DaemonConfig implements IDaemonConfig {
public base_directory: string
public wikis: {name: string, url: string}[]
public user_list_cache_freshness?: number
public ratelimit?: { bucket_size: number, refill_seconds: number }
public delay_ms?: number
public maximum_jobs?: number
public http_proxy?: {address: string, port: number}
public socks_proxy?: {address: string, port: number}
public blacklist?: string[]
constructor(loader: IDaemonConfig) {
this.base_directory = loader.base_directory
this.wikis = loader.wikis
this.user_list_cache_freshness = loader.user_list_cache_freshness
this.ratelimit = loader.ratelimit
this.delay_ms = loader.delay_ms
this.maximum_jobs = loader.maximum_jobs
this.http_proxy = loader.http_proxy
this.socks_proxy = loader.socks_proxy
this.blacklist = loader.blacklist
for (const i in this.wikis) {
let url = this.wikis[i].url
if (url.endsWith('/')) { // Some of wikidot parts don't like double slash
this.wikis[i].url = url.substring(0, url.length - 1)
}
}
}
public makeClient(connectionLimit: number, httpsagent?: https.Agent, httpagent?: http.Agent) {
const client = new HTTPClient(
connectionLimit,
httpsagent,
httpagent,
this.http_proxy?.address,
this.http_proxy?.port,
this.socks_proxy?.address,
this.socks_proxy?.port,
)
if (this.ratelimit != undefined) {
client.ratelimit = new RatelimitBucket(this.ratelimit.bucket_size, this.ratelimit.refill_seconds)
client.ratelimit.starTimer()
}
return client
}
public makeUserList(connectionLimit = 8, httpsagent?: https.Agent, httpagent?: http.Agent) {
return new WikiDotUserList(this.base_directory + '/_users', this.makeClient(connectionLimit, httpsagent, httpagent), this.user_list_cache_freshness !== undefined ? this.user_list_cache_freshness * 1000 : undefined)
}
public makeQueue() {
return new PromiseQueue(this.delay_ms, this.maximum_jobs)
}
}
export async function loadConfig(exit = true) {
let config: DaemonConfig
const argv = process.argv[3]
try {
const configPath = argv !== undefined ? argv : (process.env.WIKICOMMA_CONFIG !== undefined ? process.env.WIKICOMMA_CONFIG : 'config.json')
const configData = await promises.readFile(configPath, {encoding: 'utf-8'})
config = new DaemonConfig(JSON.parse(configData))
} catch(err) {
if (exit) {
process.stderr.write('config.json is missing or invalid from working directory.\n')
process.stderr.write('Or set a different file using the WIKICOMMA_CONFIG environment variable.\n')
process.exit(1)
} else {
throw err
}
}
return config
}