From 2928d362fa7e40ba2cbeecb57275a296b1903ca3 Mon Sep 17 00:00:00 2001 From: Asher Date: Thu, 15 Oct 2020 17:00:21 -0500 Subject: [PATCH] Move heart and AuthType out of http This file is going to get blasted in favor of Express. --- src/node/app/health.ts | 3 ++- src/node/app/login.ts | 3 ++- src/node/cli.ts | 6 ++++- src/node/heart.ts | 46 +++++++++++++++++++++++++++++++++++++ src/node/http.ts | 51 ++---------------------------------------- test/update.test.ts | 5 ++--- 6 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 src/node/heart.ts diff --git a/src/node/app/health.ts b/src/node/app/health.ts index 48d6897c..4e505f57 100644 --- a/src/node/app/health.ts +++ b/src/node/app/health.ts @@ -1,4 +1,5 @@ -import { HttpProvider, HttpResponse, Heart, HttpProviderOptions } from "../http" +import { Heart } from "../heart" +import { HttpProvider, HttpProviderOptions, HttpResponse } from "../http" /** * Check the heartbeat. diff --git a/src/node/app/login.ts b/src/node/app/login.ts index 0fe1e0b6..58bd55e0 100644 --- a/src/node/app/login.ts +++ b/src/node/app/login.ts @@ -2,7 +2,8 @@ import * as http from "http" import * as limiter from "limiter" import * as querystring from "querystring" import { HttpCode, HttpError } from "../../common/http" -import { AuthType, HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http" +import { AuthType } from "../cli" +import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http" import { hash, humanPath } from "../util" interface LoginPayload { diff --git a/src/node/cli.ts b/src/node/cli.ts index ba8f5ae8..7f44f9ea 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -4,9 +4,13 @@ import yaml from "js-yaml" import * as os from "os" import * as path from "path" import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc" -import { AuthType } from "./http" import { canConnect, generateCertificate, generatePassword, humanPath, paths } from "./util" +export enum AuthType { + Password = "password", + None = "none", +} + export class Optional { public constructor(public readonly value?: T) {} } diff --git a/src/node/heart.ts b/src/node/heart.ts new file mode 100644 index 00000000..5198e33d --- /dev/null +++ b/src/node/heart.ts @@ -0,0 +1,46 @@ +import { logger } from "@coder/logger" +import { promises as fs } from "fs" + +/** + * Provides a heartbeat using a local file to indicate activity. + */ +export class Heart { + private heartbeatTimer?: NodeJS.Timeout + private heartbeatInterval = 60000 + public lastHeartbeat = 0 + + public constructor(private readonly heartbeatPath: string, private readonly isActive: () => Promise) {} + + public alive(): boolean { + const now = Date.now() + return now - this.lastHeartbeat < this.heartbeatInterval + } + /** + * Write to the heartbeat file if we haven't already done so within the + * timeout and start or reset a timer that keeps running as long as there is + * activity. Failures are logged as warnings. + */ + public beat(): void { + if (!this.alive()) { + logger.trace("heartbeat") + fs.writeFile(this.heartbeatPath, "").catch((error) => { + logger.warn(error.message) + }) + this.lastHeartbeat = Date.now() + if (typeof this.heartbeatTimer !== "undefined") { + clearTimeout(this.heartbeatTimer) + } + this.heartbeatTimer = setTimeout(() => { + this.isActive() + .then((active) => { + if (active) { + this.beat() + } + }) + .catch((error) => { + logger.warn(error.message) + }) + }, this.heartbeatInterval) + } + } +} diff --git a/src/node/http.ts b/src/node/http.ts index 93d6e725..4aa6dc06 100644 --- a/src/node/http.ts +++ b/src/node/http.ts @@ -13,6 +13,8 @@ import * as tls from "tls" import * as url from "url" import { HttpCode, HttpError } from "../common/http" import { arrayify, normalize, Options, plural, split, trimSlashes } from "../common/util" +import { AuthType } from "./cli" +import { Heart } from "./heart" import { SocketProxyProvider } from "./socket" import { getMediaMime, paths } from "./util" @@ -27,11 +29,6 @@ interface AuthPayload extends Cookies { key?: string[] } -export enum AuthType { - Password = "password", - None = "none", -} - export type Query = { [key: string]: string | string[] | undefined } export interface ProxyOptions { @@ -390,50 +387,6 @@ export abstract class HttpProvider { } } -/** - * Provides a heartbeat using a local file to indicate activity. - */ -export class Heart { - private heartbeatTimer?: NodeJS.Timeout - private heartbeatInterval = 60000 - public lastHeartbeat = 0 - - public constructor(private readonly heartbeatPath: string, private readonly isActive: () => Promise) {} - - public alive(): boolean { - const now = Date.now() - return now - this.lastHeartbeat < this.heartbeatInterval - } - /** - * Write to the heartbeat file if we haven't already done so within the - * timeout and start or reset a timer that keeps running as long as there is - * activity. Failures are logged as warnings. - */ - public beat(): void { - if (!this.alive()) { - logger.trace("heartbeat") - fs.outputFile(this.heartbeatPath, "").catch((error) => { - logger.warn(error.message) - }) - this.lastHeartbeat = Date.now() - if (typeof this.heartbeatTimer !== "undefined") { - clearTimeout(this.heartbeatTimer) - } - this.heartbeatTimer = setTimeout(() => { - this.isActive() - .then((active) => { - if (active) { - this.beat() - } - }) - .catch((error) => { - logger.warn(error.message) - }) - }, this.heartbeatInterval) - } - } -} - export interface HttpProvider0 { new (options: HttpProviderOptions): T } diff --git a/test/update.test.ts b/test/update.test.ts index 7e4b80f2..9e27eefa 100644 --- a/test/update.test.ts +++ b/test/update.test.ts @@ -3,12 +3,11 @@ import * as fs from "fs-extra" import * as http from "http" import * as path from "path" import { LatestResponse, UpdateHttpProvider } from "../src/node/app/update" -import { AuthType } from "../src/node/http" +import { AuthType } from "../src/node/cli" import { SettingsProvider, UpdateSettings } from "../src/node/settings" import { tmpdir } from "../src/node/util" -describe("update", () => { - return +describe.skip("update", () => { let version = "1.0.0" let spy: string[] = [] const server = http.createServer((request: http.IncomingMessage, response: http.ServerResponse) => {