Unlink socket before using (#2181)

See https://stackoverflow.com/a/34881585/4283659

Closes #1538
This commit is contained in:
Anmol Sethi 2020-10-09 12:34:52 -04:00 committed by GitHub
parent c6c293d53a
commit 2d1de749f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -30,6 +30,7 @@ rules:
eqeqeq: error eqeqeq: error
import/order: import/order:
[error, { alphabetize: { order: "asc" }, groups: [["builtin", "external", "internal"], "parent", "sibling"] }] [error, { alphabetize: { order: "asc" }, groups: [["builtin", "external", "internal"], "parent", "sibling"] }]
no-async-promise-executor: off
settings: settings:
# Does not work with CommonJS unfortunately. # Does not work with CommonJS unfortunately.

View File

@ -578,11 +578,18 @@ export class HttpServer {
*/ */
public listen(): Promise<string | null> { public listen(): Promise<string | null> {
if (!this.listenPromise) { if (!this.listenPromise) {
this.listenPromise = new Promise((resolve, reject) => { this.listenPromise = new Promise(async (resolve, reject) => {
this.server.on("error", reject) this.server.on("error", reject)
this.server.on("upgrade", this.onUpgrade) this.server.on("upgrade", this.onUpgrade)
const onListen = (): void => resolve(this.address()) const onListen = (): void => resolve(this.address())
if (this.options.socket) { if (this.options.socket) {
try {
await fs.unlink(this.options.socket)
} catch (err) {
if (err.code !== "ENOENT") {
logger.warn(err.message)
}
}
this.server.listen(this.options.socket, onListen) this.server.listen(this.options.socket, onListen)
} else if (this.options.host) { } else if (this.options.host) {
// [] is the correct format when using :: but Node errors with them. // [] is the correct format when using :: but Node errors with them.