( app: App, args: DefaultedArgs, )
| 29 | * Register all routes and middleware. |
| 30 | */ |
| 31 | export const register = async ( |
| 32 | app: App, |
| 33 | args: DefaultedArgs, |
| 34 | ): Promise<{ disposeRoutes: Disposable["dispose"]; heart: Heart }> => { |
| 35 | const heart = new Heart(path.join(paths.data, "heartbeat"), async () => { |
| 36 | return new Promise((resolve, reject) => { |
| 37 | // getConnections appears to not call the callback when there are no more |
| 38 | // connections. Feels like it must be a bug? For now add a timer to make |
| 39 | // sure we eventually resolve. |
| 40 | const timer = setTimeout(() => { |
| 41 | logger.debug("Node failed to respond with connections; assuming zero") |
| 42 | resolve(false) |
| 43 | }, 5000) |
| 44 | app.server.getConnections((error, count) => { |
| 45 | clearTimeout(timer) |
| 46 | if (error) { |
| 47 | return reject(error) |
| 48 | } |
| 49 | logger.debug(plural(count, `${count} active connection`)) |
| 50 | resolve(count > 0) |
| 51 | }) |
| 52 | }) |
| 53 | }) |
| 54 | |
| 55 | app.router.disable("x-powered-by") |
| 56 | app.wsRouter.disable("x-powered-by") |
| 57 | |
| 58 | app.router.use(cookieParser()) |
| 59 | app.wsRouter.use(cookieParser()) |
| 60 | |
| 61 | const settings = new SettingsProvider<CoderSettings>(path.join(args["user-data-dir"], "coder.json")) |
| 62 | const updater = new UpdateProvider("https://api.github.com/repos/coder/code-server/releases/latest", settings) |
| 63 | |
| 64 | const cookieSessionName = getCookieSessionName(args["cookie-suffix"]) |
| 65 | |
| 66 | const common: express.RequestHandler = (req, _, next) => { |
| 67 | // /healthz|/healthz/ needs to be excluded otherwise health checks will make |
| 68 | // it look like code-server is always in use. |
| 69 | if (!/^\/healthz\/?$/.test(req.url)) { |
| 70 | // NOTE@jsjoeio - intentionally not awaiting the .beat() call here because |
| 71 | // we don't want to slow down the request. |
| 72 | heart.beat() |
| 73 | } |
| 74 | |
| 75 | // Add common variables routes can use. |
| 76 | req.args = args |
| 77 | req.heart = heart |
| 78 | req.settings = settings |
| 79 | req.updater = updater |
| 80 | req.cookieSessionName = cookieSessionName |
| 81 | |
| 82 | next() |
| 83 | } |
| 84 | |
| 85 | app.router.use(common) |
| 86 | app.wsRouter.use(common) |
| 87 | |
| 88 | app.router.use(/.*/, async (req, res, next) => { |
no test coverage detected