2021-01-20 20:11:08 +00:00
|
|
|
import * as express from "express"
|
2021-01-14 14:34:51 +00:00
|
|
|
import * as http from "http"
|
2021-02-16 20:12:58 +00:00
|
|
|
import * as net from "net"
|
2021-01-14 14:34:51 +00:00
|
|
|
import * as nodeFetch from "node-fetch"
|
2021-01-20 20:11:08 +00:00
|
|
|
import Websocket from "ws"
|
2021-03-09 23:33:39 +00:00
|
|
|
import * as util from "../../src/common/util"
|
|
|
|
import { ensureAddress } from "../../src/node/app"
|
|
|
|
import { handleUpgrade } from "../../src/node/wsRouter"
|
2021-01-14 14:34:51 +00:00
|
|
|
|
2021-01-14 15:53:58 +00:00
|
|
|
// Perhaps an abstraction similar to this should be used in app.ts as well.
|
2021-01-14 14:34:51 +00:00
|
|
|
export class HttpServer {
|
2021-02-16 20:12:58 +00:00
|
|
|
private readonly sockets = new Set<net.Socket>()
|
|
|
|
private cleanupTimeout?: NodeJS.Timeout
|
2021-01-14 14:34:51 +00:00
|
|
|
|
2021-02-16 20:12:58 +00:00
|
|
|
// See usage in test/integration.ts
|
|
|
|
public constructor(private readonly hs = http.createServer()) {
|
|
|
|
this.hs.on("connection", (socket) => {
|
|
|
|
this.sockets.add(socket)
|
|
|
|
socket.on("close", () => {
|
|
|
|
this.sockets.delete(socket)
|
|
|
|
if (this.cleanupTimeout && this.sockets.size === 0) {
|
|
|
|
clearTimeout(this.cleanupTimeout)
|
|
|
|
this.cleanupTimeout = undefined
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2021-01-14 15:53:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 14:34:51 +00:00
|
|
|
/**
|
|
|
|
* listen starts the server on a random localhost port.
|
|
|
|
* Use close to cleanup when done.
|
|
|
|
*/
|
|
|
|
public listen(fn: http.RequestListener): Promise<void> {
|
|
|
|
this.hs.on("request", fn)
|
|
|
|
|
|
|
|
let resolved = false
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
this.hs.listen(0, "localhost", () => {
|
|
|
|
res()
|
|
|
|
resolved = true
|
|
|
|
})
|
|
|
|
|
|
|
|
this.hs.on("error", (err) => {
|
|
|
|
if (!resolved) {
|
|
|
|
rej(err)
|
|
|
|
} else {
|
|
|
|
// Promise resolved earlier so this is some other error.
|
2021-01-14 14:49:23 +00:00
|
|
|
util.logError("http server error", err)
|
2021-01-14 14:34:51 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:11:08 +00:00
|
|
|
/**
|
|
|
|
* Send upgrade requests to an Express app.
|
|
|
|
*/
|
|
|
|
public listenUpgrade(app: express.Express): void {
|
|
|
|
handleUpgrade(app, this.hs)
|
|
|
|
}
|
|
|
|
|
2021-01-14 14:34:51 +00:00
|
|
|
/**
|
|
|
|
* close cleans up the server.
|
|
|
|
*/
|
|
|
|
public close(): Promise<void> {
|
|
|
|
return new Promise((res, rej) => {
|
2021-02-16 20:12:58 +00:00
|
|
|
// Close will not actually close anything; it just waits until everything
|
|
|
|
// is closed.
|
2021-01-14 14:34:51 +00:00
|
|
|
this.hs.close((err) => {
|
|
|
|
if (err) {
|
|
|
|
rej(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res()
|
|
|
|
})
|
2021-02-16 20:12:58 +00:00
|
|
|
|
|
|
|
// If there are sockets remaining we might need to force close them or
|
|
|
|
// this promise might never resolve.
|
|
|
|
if (this.sockets.size > 0) {
|
|
|
|
// Give sockets a chance to close up shop.
|
|
|
|
this.cleanupTimeout = setTimeout(() => {
|
|
|
|
this.cleanupTimeout = undefined
|
|
|
|
for (const socket of this.sockets.values()) {
|
|
|
|
console.warn("a socket was left hanging")
|
|
|
|
socket.destroy()
|
|
|
|
}
|
|
|
|
}, 1000)
|
|
|
|
}
|
2021-01-14 14:34:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fetch fetches the request path.
|
|
|
|
* The request path must be rooted!
|
|
|
|
*/
|
|
|
|
public fetch(requestPath: string, opts?: nodeFetch.RequestInit): Promise<nodeFetch.Response> {
|
|
|
|
return nodeFetch.default(`${ensureAddress(this.hs)}${requestPath}`, opts)
|
|
|
|
}
|
2021-01-14 15:53:58 +00:00
|
|
|
|
2021-01-20 20:11:08 +00:00
|
|
|
/**
|
|
|
|
* Open a websocket against the requset path.
|
|
|
|
*/
|
|
|
|
public ws(requestPath: string): Websocket {
|
|
|
|
return new Websocket(`${ensureAddress(this.hs).replace("http:", "ws:")}${requestPath}`)
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:53:58 +00:00
|
|
|
public port(): number {
|
|
|
|
const addr = this.hs.address()
|
2021-01-14 15:55:19 +00:00
|
|
|
if (addr && typeof addr === "object") {
|
2021-01-14 15:53:58 +00:00
|
|
|
return addr.port
|
|
|
|
}
|
|
|
|
throw new Error("server not listening or listening on unix socket")
|
|
|
|
}
|
2021-01-14 14:34:51 +00:00
|
|
|
}
|