Add Node config for overwriting socket file if it exists

This commit is contained in:
Andrew Ferrazzutti 2022-04-20 23:06:37 -04:00
parent 91448c3005
commit 163c1c2125
3 changed files with 10 additions and 4 deletions

View File

@ -1,4 +1,4 @@
### Listen config
If `type` is `unix`, `path` is the path where to create the socket.
If `type` is `unix`, `path` is the path where to create the socket, and `force` is whether to overwrite the socket file if it already exists.
If `type` is `tcp`, `port` and `host` are the host/port where to listen.

View File

@ -1,6 +1,7 @@
{
"listen": {
"type": "unix",
"path": "/var/run/matrix-appservice-kakaotalk/rpc.sock"
"path": "/var/run/matrix-appservice-kakaotalk/rpc.sock",
"force": false
}
}

View File

@ -46,12 +46,17 @@ export default class ClientManager {
}
}
async startUnix(socketPath) {
async startUnix(socketPath, force) {
try {
await fs.promises.access(path.dirname(socketPath))
} catch (err) {
await fs.promises.mkdir(path.dirname(socketPath), 0o700)
}
if (force) {
try {
await fs.promises.unlink(socketPath)
} catch (err) {}
}
await promisify(cb => this.server.listen(socketPath, cb))
await fs.promises.chmod(socketPath, 0o700)
this.log("Now listening at", socketPath)
@ -66,7 +71,7 @@ export default class ClientManager {
this.log("Starting server")
if (this.listenConfig.type === "unix") {
await this.startUnix(this.listenConfig.path)
await this.startUnix(this.listenConfig.path, this.listenConfig.force)
} else if (this.listenConfig.type === "tcp") {
await this.startTCP(this.listenConfig.port, this.listenConfig.host)
}