Skip to content

Commit 2928d36

Browse files
committed
Move heart and AuthType out of http
This file is going to get blasted in favor of Express.
1 parent dcb303a commit 2928d36

File tree

6 files changed

+59
-55
lines changed

6 files changed

+59
-55
lines changed

src/node/app/health.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { HttpProvider, HttpResponse, Heart, HttpProviderOptions } from "../http"
1+
import { Heart } from "../heart"
2+
import { HttpProvider, HttpProviderOptions, HttpResponse } from "../http"
23

34
/**
45
* Check the heartbeat.

src/node/app/login.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as http from "http"
22
import * as limiter from "limiter"
33
import * as querystring from "querystring"
44
import { HttpCode, HttpError } from "../../common/http"
5-
import { AuthType, HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
5+
import { AuthType } from "../cli"
6+
import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
67
import { hash, humanPath } from "../util"
78

89
interface LoginPayload {

src/node/cli.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import yaml from "js-yaml"
44
import * as os from "os"
55
import * as path from "path"
66
import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc"
7-
import { AuthType } from "./http"
87
import { canConnect, generateCertificate, generatePassword, humanPath, paths } from "./util"
98

9+
export enum AuthType {
10+
Password = "password",
11+
None = "none",
12+
}
13+
1014
export class Optional<T> {
1115
public constructor(public readonly value?: T) {}
1216
}

src/node/heart.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { logger } from "@coder/logger"
2+
import { promises as fs } from "fs"
3+
4+
/**
5+
* Provides a heartbeat using a local file to indicate activity.
6+
*/
7+
export class Heart {
8+
private heartbeatTimer?: NodeJS.Timeout
9+
private heartbeatInterval = 60000
10+
public lastHeartbeat = 0
11+
12+
public constructor(private readonly heartbeatPath: string, private readonly isActive: () => Promise<boolean>) {}
13+
14+
public alive(): boolean {
15+
const now = Date.now()
16+
return now - this.lastHeartbeat < this.heartbeatInterval
17+
}
18+
/**
19+
* Write to the heartbeat file if we haven't already done so within the
20+
* timeout and start or reset a timer that keeps running as long as there is
21+
* activity. Failures are logged as warnings.
22+
*/
23+
public beat(): void {
24+
if (!this.alive()) {
25+
logger.trace("heartbeat")
26+
fs.writeFile(this.heartbeatPath, "").catch((error) => {
27+
logger.warn(error.message)
28+
})
29+
this.lastHeartbeat = Date.now()
30+
if (typeof this.heartbeatTimer !== "undefined") {
31+
clearTimeout(this.heartbeatTimer)
32+
}
33+
this.heartbeatTimer = setTimeout(() => {
34+
this.isActive()
35+
.then((active) => {
36+
if (active) {
37+
this.beat()
38+
}
39+
})
40+
.catch((error) => {
41+
logger.warn(error.message)
42+
})
43+
}, this.heartbeatInterval)
44+
}
45+
}
46+
}

src/node/http.ts

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import * as tls from "tls"
1313
import * as url from "url"
1414
import { HttpCode, HttpError } from "../common/http"
1515
import { arrayify, normalize, Options, plural, split, trimSlashes } from "../common/util"
16+
import { AuthType } from "./cli"
17+
import { Heart } from "./heart"
1618
import { SocketProxyProvider } from "./socket"
1719
import { getMediaMime, paths } from "./util"
1820

@@ -27,11 +29,6 @@ interface AuthPayload extends Cookies {
2729
key?: string[]
2830
}
2931

30-
export enum AuthType {
31-
Password = "password",
32-
None = "none",
33-
}
34-
3532
export type Query = { [key: string]: string | string[] | undefined }
3633

3734
export interface ProxyOptions {
@@ -390,50 +387,6 @@ export abstract class HttpProvider {
390387
}
391388
}
392389

393-
/**
394-
* Provides a heartbeat using a local file to indicate activity.
395-
*/
396-
export class Heart {
397-
private heartbeatTimer?: NodeJS.Timeout
398-
private heartbeatInterval = 60000
399-
public lastHeartbeat = 0
400-
401-
public constructor(private readonly heartbeatPath: string, private readonly isActive: () => Promise<boolean>) {}
402-
403-
public alive(): boolean {
404-
const now = Date.now()
405-
return now - this.lastHeartbeat < this.heartbeatInterval
406-
}
407-
/**
408-
* Write to the heartbeat file if we haven't already done so within the
409-
* timeout and start or reset a timer that keeps running as long as there is
410-
* activity. Failures are logged as warnings.
411-
*/
412-
public beat(): void {
413-
if (!this.alive()) {
414-
logger.trace("heartbeat")
415-
fs.outputFile(this.heartbeatPath, "").catch((error) => {
416-
logger.warn(error.message)
417-
})
418-
this.lastHeartbeat = Date.now()
419-
if (typeof this.heartbeatTimer !== "undefined") {
420-
clearTimeout(this.heartbeatTimer)
421-
}
422-
this.heartbeatTimer = setTimeout(() => {
423-
this.isActive()
424-
.then((active) => {
425-
if (active) {
426-
this.beat()
427-
}
428-
})
429-
.catch((error) => {
430-
logger.warn(error.message)
431-
})
432-
}, this.heartbeatInterval)
433-
}
434-
}
435-
}
436-
437390
export interface HttpProvider0<T> {
438391
new (options: HttpProviderOptions): T
439392
}

test/update.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import * as fs from "fs-extra"
33
import * as http from "http"
44
import * as path from "path"
55
import { LatestResponse, UpdateHttpProvider } from "../src/node/app/update"
6-
import { AuthType } from "../src/node/http"
6+
import { AuthType } from "../src/node/cli"
77
import { SettingsProvider, UpdateSettings } from "../src/node/settings"
88
import { tmpdir } from "../src/node/util"
99

10-
describe("update", () => {
11-
return
10+
describe.skip("update", () => {
1211
let version = "1.0.0"
1312
let spy: string[] = []
1413
const server = http.createServer((request: http.IncomingMessage, response: http.ServerResponse) => {

0 commit comments

Comments
 (0)