Archived
1
0

Fix issues surrounding initial web server load. (#4509)

- Clean up watcher behaviors.
This commit is contained in:
Teffen
2021-11-19 16:03:40 -05:00
committed by GitHub
parent 5fe16be62d
commit 3157a40044
5 changed files with 306 additions and 146 deletions

View File

@ -4,7 +4,7 @@ import { WebsocketRequest } from "../../../typings/pluginapi"
import { logError } from "../../common/util"
import { isDevMode } from "../constants"
import { ensureAuthenticated, authenticated, redirect } from "../http"
import { loadAMDModule } from "../util"
import { loadAMDModule, readCompilationStats } from "../util"
import { Router as WsRouter } from "../wsRouter"
import { errorHandler } from "./errors"
@ -40,7 +40,6 @@ export class CodeServerRouteWrapper {
if (error instanceof Error && ["EntryNotFound", "FileNotFound", "HttpError"].includes(error.message)) {
next()
}
errorHandler(error, req, res, next)
}
@ -62,9 +61,21 @@ export class CodeServerRouteWrapper {
*/
private ensureCodeServerLoaded: express.Handler = async (req, _res, next) => {
if (this._codeServerMain) {
// Already loaded...
return next()
}
if (isDevMode) {
// Is the development mode file watcher still busy?
const compileStats = await readCompilationStats()
if (!compileStats || !compileStats.lastCompiledAt) {
return next(new Error("VS Code may still be compiling..."))
}
}
// Create the server...
const { args } = req
/**
@ -84,10 +95,7 @@ export class CodeServerRouteWrapper {
})
} catch (createServerError) {
logError(logger, "CodeServerRouteWrapper", createServerError)
const loggedError = isDevMode ? new Error("VS Code may still be compiling...") : createServerError
return next(loggedError)
return next(createServerError)
}
return next()

View File

@ -3,14 +3,15 @@ import * as argon2 from "argon2"
import * as cp from "child_process"
import * as crypto from "crypto"
import envPaths from "env-paths"
import { promises as fs } from "fs"
import { promises as fs, Stats } from "fs"
import * as net from "net"
import * as os from "os"
import * as path from "path"
import safeCompare from "safe-compare"
import * as util from "util"
import xdgBasedir from "xdg-basedir"
import { vsRootPath } from "./constants"
import { logError } from "../common/util"
import { isDevMode, rootPath, vsRootPath } from "./constants"
export interface Paths {
data: string
@ -25,10 +26,11 @@ const pattern = [
].join("|")
const re = new RegExp(pattern, "g")
export type OnLineCallback = (strippedLine: string, originalLine: string) => void
/**
* Split stdout on newlines and strip ANSI codes.
*/
export const onLine = (proc: cp.ChildProcess, callback: (strippedLine: string, originalLine: string) => void): void => {
export const onLine = (proc: cp.ChildProcess, callback: OnLineCallback): void => {
let buffer = ""
if (!proc.stdout) {
throw new Error("no stdout")
@ -521,3 +523,41 @@ export const loadAMDModule = async <T>(amdPath: string, exportName: string): Pro
return module[exportName] as T
}
export const enum VSCodeCompileStatus {
Loading = "Loading",
Compiling = "Compiling",
Compiled = "Compiled",
}
export interface CompilationStats {
status: VSCodeCompileStatus
lastCompiledAt: Date
}
export const readCompilationStats = async (): Promise<null | CompilationStats> => {
if (!isDevMode) {
throw new Error("Compilation stats are only present in development")
}
const filePath = path.join(rootPath, "out/watcher.json")
let stat: Stats
try {
stat = await fs.stat(filePath)
} catch (error) {
return null
}
if (!stat.isFile()) {
return null
}
try {
const file = await fs.readFile(filePath)
return JSON.parse(file.toString("utf-8"))
} catch (error) {
logError(logger, "VS Code", error)
}
return null
}