-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathindex.js
163 lines (136 loc) · 3.31 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { promisify } from "util";
import path from "path";
import fs from "fs/promises";
import child_process from "child_process";
import net from "net";
// eslint-disable-next-line n/no-extraneous-import
import { promise, delay } from "@xmpp/events";
import selfsigned from "selfsigned";
const __dirname = "./server";
// const __dirname = import.meta.dirname;
const exec = promisify(child_process.exec);
const DATA_PATH = path.join(__dirname);
const PID_PATH = path.join(DATA_PATH, "prosody.pid");
const PROSODY_PORT = 5347;
const CFG_PATH = path.join(__dirname, "prosody.cfg.lua");
function clean() {
return Promise.all(
["prosody.err", "prosody.log", "prosody.pid"].map((file) =>
fs.unlink(path.join(__dirname, file)),
),
).catch(() => {});
}
function isPortOpen() {
const sock = new net.Socket();
sock.connect({ port: PROSODY_PORT });
return promise(sock, "connect")
.then(() => {
sock.end();
sock.destroy();
return true;
})
.catch(() => false);
}
async function waitPortOpen() {
if (await isPortOpen()) {
return;
}
await delay(1000);
return waitPortOpen();
}
async function makeCertificate() {
const attrs = [{ name: "commonName", value: "localhost" }];
const pems = selfsigned.generate(attrs, { days: 365, keySize: 2048 });
await Promise.all([
fs.writeFile(path.join(__dirname, "certs/localhost.crt"), pems.cert),
fs.writeFile(path.join(__dirname, "certs/localhost.key"), pems.private),
]);
}
async function waitPortClose() {
if (!(await isPortOpen())) {
return;
}
await delay(1000);
return waitPortClose();
}
async function kill(signal = "SIGTERM") {
const pid = await getPid();
try {
process.kill(pid, signal);
} catch (err) {
if (err.code !== "ESRCH") throw err;
}
}
async function getPid() {
try {
return await fs.readFile(PID_PATH, "utf8");
} catch (err) {
if (err.code !== "ENOENT") throw err;
return "";
}
}
async function _start() {
const opening = waitPortOpen();
makeCertificate();
await exec("prosody -D", {
cwd: DATA_PATH,
env: {
...process.env,
PROSODY_CONFIG: "prosody.cfg.lua",
},
});
return opening;
}
async function start() {
if (await isPortOpen()) return;
await clean();
return _start();
}
async function stop(signal) {
if (!(await isPortOpen())) {
return clean();
}
const closing = waitPortClose();
await kill(signal);
return closing;
}
async function restart(signal) {
await stop(signal);
return _start();
}
async function enableModules(mods) {
if (!Array.isArray(mods)) {
mods = [mods];
}
let prosody_cfg = await fs.readFile(CFG_PATH, "utf8");
for (const mod of mods) {
prosody_cfg = prosody_cfg.replace(`\n -- "${mod}";`, `\n "${mod}";`);
}
await fs.writeFile(CFG_PATH, prosody_cfg);
}
async function disableModules(mods) {
if (!Array.isArray(mods)) {
mods = [mods];
}
let prosody_cfg = await fs.readFile(CFG_PATH, "utf8");
for (const mod of mods) {
prosody_cfg = prosody_cfg.replace(`\n "${mod}";`, `\n -- "${mod}";`);
}
await fs.writeFile(CFG_PATH, prosody_cfg);
}
async function reset() {
await exec("git checkout server/prosody.cfg.lua");
}
export default {
isPortOpen,
waitPortClose,
waitPortOpen,
getPid,
start,
stop,
restart,
kill,
enableModules,
disableModules,
reset,
};