Fix more azurecore strict nulls (#20547)

This commit is contained in:
Charles Gagnon
2022-09-07 13:05:16 -07:00
committed by GitHub
parent a7c552aefc
commit 56fcb43c35
5 changed files with 25 additions and 33 deletions

View File

@@ -15,11 +15,11 @@ export class AlreadyInitializedError extends Error {
}
export class FileDatabase {
private db: { [key: string]: string };
private db: { [key: string]: string } = {};
private isDirty = false;
private isSaving = false;
private isInitialized = false;
private saveInterval: NodeJS.Timer;
private saveInterval: NodeJS.Timer | undefined;
constructor(
private readonly dbPath: string,
@@ -91,7 +91,7 @@ export class FileDatabase {
public async initialize(): Promise<void> {
this.isInitialized = true;
this.setupSaveTask();
this.saveInterval = setInterval(() => this.save(), 20 * 1000);
let fileContents: string;
try {
await fs.access(this.dbPath, fsConstants.R_OK | fsConstants.R_OK);
@@ -114,13 +114,11 @@ export class FileDatabase {
}
}
private setupSaveTask(): NodeJS.Timer {
return setInterval(() => this.save(), 20 * 1000);
}
public async shutdown(): Promise<void> {
await this.waitForFileSave();
clearInterval((this.saveInterval));
if (this.saveInterval) {
clearInterval(this.saveInterval);
}
await this.save();
}

View File

@@ -11,21 +11,27 @@ export type WebHandler = (req: http.IncomingMessage, reqUrl: url.UrlWithParsedQu
export class AlreadyRunningError extends Error { }
export class SimpleWebServer {
private hasStarted: boolean;
private hasStarted: boolean = false;
private readonly pathMappings = new Map<string, WebHandler>();
private readonly server: http.Server;
private lastUsed: number;
private lastUsed: number = new Date().getTime();
private shutoffInterval: NodeJS.Timer;
constructor(private readonly autoShutoffTimer = 5 * 60 * 1000) { // Default to five minutes.
this.bumpLastUsed();
this.autoShutoff();
this.shutoffInterval = setInterval(() => {
const time = new Date().getTime();
if (time - this.lastUsed > this.autoShutoffTimer) {
console.log('Shutting off webserver...');
this.shutdown().catch(console.error);
}
}, 1000);
this.server = http.createServer((req, res) => {
this.bumpLastUsed();
const reqUrl = url.parse(req.url!, /* parseQueryString */ true);
const handler = this.pathMappings.get(reqUrl.pathname);
const handler = reqUrl.pathname ? this.pathMappings.get(reqUrl.pathname) : undefined;
if (handler) {
return handler(req, reqUrl, res);
}
@@ -95,15 +101,4 @@ export class SimpleWebServer {
public on(pathMapping: string, handler: WebHandler) {
this.pathMappings.set(pathMapping, handler);
}
private autoShutoff(): void {
this.shutoffInterval = setInterval(() => {
const time = new Date().getTime();
if (time - this.lastUsed > this.autoShutoffTimer) {
console.log('Shutting off webserver...');
this.shutdown().catch(console.error);
}
}, 1000);
}
}