2021-05-03 21:42:24 +02:00
|
|
|
import { logger } from "@coder/logger"
|
2021-01-20 21:11:08 +01:00
|
|
|
import * as express from "express"
|
2021-01-14 15:34:51 +01:00
|
|
|
import * as http from "http"
|
2021-10-28 22:27:17 +02:00
|
|
|
import nodeFetch, { RequestInit, Response } from "node-fetch"
|
2021-01-20 21:11:08 +01:00
|
|
|
import Websocket from "ws"
|
2021-10-28 22:27:17 +02:00
|
|
|
import { Disposable } from "../../src/common/emitter"
|
2021-03-10 00:33:39 +01:00
|
|
|
import * as util from "../../src/common/util"
|
|
|
|
import { ensureAddress } from "../../src/node/app"
|
2021-10-28 22:27:17 +02:00
|
|
|
import { disposer } from "../../src/node/http"
|
|
|
|
|
2021-03-10 00:33:39 +01:00
|
|
|
import { handleUpgrade } from "../../src/node/wsRouter"
|
2021-01-14 15:34:51 +01:00
|
|
|
|
2021-01-14 16:53:58 +01:00
|
|
|
// Perhaps an abstraction similar to this should be used in app.ts as well.
|
2021-01-14 15:34:51 +01:00
|
|
|
export class HttpServer {
|
2021-10-28 22:27:17 +02:00
|
|
|
private hs: http.Server
|
|
|
|
public dispose: Disposable["dispose"]
|
2021-01-14 15:34:51 +01:00
|
|
|
|
2021-10-28 22:27:17 +02:00
|
|
|
/**
|
|
|
|
* Expects a server and a disposal that cleans up the server (and anything
|
|
|
|
* else that may need cleanup).
|
|
|
|
*
|
|
|
|
* Otherwise a new server is created.
|
|
|
|
*/
|
|
|
|
public constructor(server?: { server: http.Server; dispose: Disposable["dispose"] }) {
|
|
|
|
this.hs = server?.server || http.createServer()
|
|
|
|
this.dispose = server?.dispose || disposer(this.hs)
|
2021-01-14 16:53:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 15:34:51 +01: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)
|
|
|
|
|
2021-10-28 22:27:17 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.hs.on("error", reject)
|
|
|
|
|
2021-01-14 15:34:51 +01:00
|
|
|
this.hs.listen(0, "localhost", () => {
|
2021-10-28 22:27:17 +02:00
|
|
|
this.hs.off("error", reject)
|
|
|
|
resolve()
|
2021-01-14 15:34:51 +01:00
|
|
|
|
2021-10-28 22:27:17 +02:00
|
|
|
this.hs.on("error", (err) => {
|
2021-01-14 15:34:51 +01:00
|
|
|
// Promise resolved earlier so this is some other error.
|
2021-05-03 21:42:24 +02:00
|
|
|
util.logError(logger, "http server error", err)
|
2021-10-28 22:27:17 +02:00
|
|
|
})
|
2021-01-14 15:34:51 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-20 21:11:08 +01:00
|
|
|
/**
|
|
|
|
* Send upgrade requests to an Express app.
|
|
|
|
*/
|
|
|
|
public listenUpgrade(app: express.Express): void {
|
|
|
|
handleUpgrade(app, this.hs)
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:34:51 +01:00
|
|
|
/**
|
|
|
|
* fetch fetches the request path.
|
|
|
|
* The request path must be rooted!
|
|
|
|
*/
|
2021-12-17 20:06:52 +01:00
|
|
|
public fetch(requestPath: string, opts?: RequestInit, query?: { [key: string]: string }): Promise<Response> {
|
2021-10-28 22:27:17 +02:00
|
|
|
const address = ensureAddress(this.hs, "http")
|
|
|
|
if (typeof address === "string") {
|
|
|
|
throw new Error("Cannot fetch socket path")
|
|
|
|
}
|
|
|
|
address.pathname = requestPath
|
2021-12-17 20:06:52 +01:00
|
|
|
if (query) {
|
|
|
|
Object.keys(query).forEach((key) => {
|
|
|
|
address.searchParams.append(key, query[key])
|
|
|
|
})
|
|
|
|
}
|
2021-10-28 22:27:17 +02:00
|
|
|
return nodeFetch(address.toString(), opts)
|
2021-01-14 15:34:51 +01:00
|
|
|
}
|
2021-01-14 16:53:58 +01:00
|
|
|
|
2021-01-20 21:11:08 +01:00
|
|
|
/**
|
2021-10-28 22:27:17 +02:00
|
|
|
* Open a websocket against the request path.
|
2021-01-20 21:11:08 +01:00
|
|
|
*/
|
|
|
|
public ws(requestPath: string): Websocket {
|
2021-10-28 22:27:17 +02:00
|
|
|
const address = ensureAddress(this.hs, "ws")
|
|
|
|
if (typeof address === "string") {
|
|
|
|
throw new Error("Cannot open websocket to socket path")
|
|
|
|
}
|
|
|
|
address.pathname = requestPath
|
|
|
|
|
|
|
|
return new Websocket(address.toString())
|
2021-01-20 21:11:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 16:53:58 +01:00
|
|
|
public port(): number {
|
|
|
|
const addr = this.hs.address()
|
2021-01-14 16:55:19 +01:00
|
|
|
if (addr && typeof addr === "object") {
|
2021-01-14 16:53:58 +01:00
|
|
|
return addr.port
|
|
|
|
}
|
|
|
|
throw new Error("server not listening or listening on unix socket")
|
|
|
|
}
|
2021-01-14 15:34:51 +01:00
|
|
|
}
|