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

@@ -241,7 +241,7 @@ export abstract class AzureAuth implements vscode.Disposable {
protected abstract login(tenant: Tenant, resource: Resource): Promise<{ response: OAuthTokenResponse, authComplete: Deferred<void, Error> }>; protected abstract login(tenant: Tenant, resource: Resource): Promise<{ response: OAuthTokenResponse | undefined, authComplete: Deferred<void, Error> }>;
/** /**
* Refreshes a token, if a refreshToken is passed in then we use that. If it is not passed in then we will prompt the user for consent. * Refreshes a token, if a refreshToken is passed in then we use that. If it is not passed in then we will prompt the user for consent.

View File

@@ -35,7 +35,6 @@ interface CryptoValues {
export class AzureAuthCodeGrant extends AzureAuth { export class AzureAuthCodeGrant extends AzureAuth {
private static readonly USER_FRIENDLY_NAME: string = localize('azure.azureAuthCodeGrantName', 'Azure Auth Code Grant'); private static readonly USER_FRIENDLY_NAME: string = localize('azure.azureAuthCodeGrantName', 'Azure Auth Code Grant');
private server: SimpleWebServer;
constructor( constructor(
metadata: AzureAccountProviderMetadata, metadata: AzureAccountProviderMetadata,
@@ -47,7 +46,7 @@ export class AzureAuthCodeGrant extends AzureAuth {
} }
protected async login(tenant: Tenant, resource: Resource): Promise<{ response: OAuthTokenResponse, authComplete: Deferred<void, Error> }> { protected async login(tenant: Tenant, resource: Resource): Promise<{ response: OAuthTokenResponse | undefined, authComplete: Deferred<void, Error> }> {
let authCompleteDeferred: Deferred<void, Error>; let authCompleteDeferred: Deferred<void, Error>;
let authCompletePromise = new Promise<void>((resolve, reject) => authCompleteDeferred = { resolve, reject }); let authCompletePromise = new Promise<void>((resolve, reject) => authCompleteDeferred = { resolve, reject });
let authResponse: AuthCodeResponse; let authResponse: AuthCodeResponse;
@@ -60,7 +59,7 @@ export class AzureAuthCodeGrant extends AzureAuth {
return { return {
response: await this.getTokenWithAuthorizationCode(tenant, resource, authResponse), response: await this.getTokenWithAuthorizationCode(tenant, resource, authResponse),
authComplete: authCompleteDeferred authComplete: authCompleteDeferred!
}; };
} }
@@ -143,11 +142,11 @@ export class AzureAuthCodeGrant extends AzureAuth {
} }
private async loginDesktop(tenant: Tenant, resource: Resource, authCompletePromise: Promise<void>): Promise<AuthCodeResponse> { private async loginDesktop(tenant: Tenant, resource: Resource, authCompletePromise: Promise<void>): Promise<AuthCodeResponse> {
this.server = new SimpleWebServer(); const server = new SimpleWebServer();
let serverPort: string; let serverPort: string;
try { try {
serverPort = await this.server.startup(); serverPort = await server.startup();
} catch (ex) { } catch (ex) {
const msg = localize('azure.serverCouldNotStart', 'Server could not start. This could be a permissions error or an incompatibility on your system. You can try enabling device code authentication from settings.'); const msg = localize('azure.serverCouldNotStart', 'Server could not start. This could be a permissions error or an incompatibility on your system. You can try enabling device code authentication from settings.');
throw new AzureAuthError(msg, 'Server could not start', ex); throw new AzureAuthError(msg, 'Server could not start', ex);
@@ -167,7 +166,7 @@ export class AzureAuthCodeGrant extends AzureAuth {
}; };
const loginUrl = `${this.loginEndpointUrl}${tenant.id}/oauth2/authorize?${qs.stringify(loginQuery)}`; const loginUrl = `${this.loginEndpointUrl}${tenant.id}/oauth2/authorize?${qs.stringify(loginQuery)}`;
await vscode.env.openExternal(vscode.Uri.parse(`http://localhost:${serverPort}/signin?nonce=${encodeURIComponent(nonce)}`)); await vscode.env.openExternal(vscode.Uri.parse(`http://localhost:${serverPort}/signin?nonce=${encodeURIComponent(nonce)}`));
const authCode = await this.addServerListeners(this.server, nonce, loginUrl, authCompletePromise); const authCode = await this.addServerListeners(server, nonce, loginUrl, authCompletePromise);
return { return {
authCode, authCode,
codeVerifier, codeVerifier,

View File

@@ -84,7 +84,7 @@ export class AzureDeviceCode extends AzureAuth {
return { return {
response: result, response: result,
authComplete: authCompleteDeferred authComplete: authCompleteDeferred!
}; };
} }
@@ -98,7 +98,7 @@ export class AzureDeviceCode extends AzureAuth {
const timeoutMessage = localize('azure.timeoutDeviceCode', 'Timed out when waiting for device code login.'); const timeoutMessage = localize('azure.timeoutDeviceCode', 'Timed out when waiting for device code login.');
const fiveMinutes = 5 * 60 * 1000; const fiveMinutes = 5 * 60 * 1000;
return new Promise<DeviceCodeLoginResult | undefined>((resolve, reject) => { return new Promise<DeviceCodeLoginResult>((resolve, reject) => {
let timeout: NodeJS.Timer; let timeout: NodeJS.Timer;
const timer = setInterval(async () => { const timer = setInterval(async () => {

View File

@@ -15,11 +15,11 @@ export class AlreadyInitializedError extends Error {
} }
export class FileDatabase { export class FileDatabase {
private db: { [key: string]: string }; private db: { [key: string]: string } = {};
private isDirty = false; private isDirty = false;
private isSaving = false; private isSaving = false;
private isInitialized = false; private isInitialized = false;
private saveInterval: NodeJS.Timer; private saveInterval: NodeJS.Timer | undefined;
constructor( constructor(
private readonly dbPath: string, private readonly dbPath: string,
@@ -91,7 +91,7 @@ export class FileDatabase {
public async initialize(): Promise<void> { public async initialize(): Promise<void> {
this.isInitialized = true; this.isInitialized = true;
this.setupSaveTask(); this.saveInterval = setInterval(() => this.save(), 20 * 1000);
let fileContents: string; let fileContents: string;
try { try {
await fs.access(this.dbPath, fsConstants.R_OK | fsConstants.R_OK); 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> { public async shutdown(): Promise<void> {
await this.waitForFileSave(); await this.waitForFileSave();
clearInterval((this.saveInterval)); if (this.saveInterval) {
clearInterval(this.saveInterval);
}
await this.save(); 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 AlreadyRunningError extends Error { }
export class SimpleWebServer { export class SimpleWebServer {
private hasStarted: boolean; private hasStarted: boolean = false;
private readonly pathMappings = new Map<string, WebHandler>(); private readonly pathMappings = new Map<string, WebHandler>();
private readonly server: http.Server; private readonly server: http.Server;
private lastUsed: number; private lastUsed: number = new Date().getTime();
private shutoffInterval: NodeJS.Timer; private shutoffInterval: NodeJS.Timer;
constructor(private readonly autoShutoffTimer = 5 * 60 * 1000) { // Default to five minutes. constructor(private readonly autoShutoffTimer = 5 * 60 * 1000) { // Default to five minutes.
this.bumpLastUsed(); 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.server = http.createServer((req, res) => {
this.bumpLastUsed(); this.bumpLastUsed();
const reqUrl = url.parse(req.url!, /* parseQueryString */ true); const reqUrl = url.parse(req.url!, /* parseQueryString */ true);
const handler = reqUrl.pathname ? this.pathMappings.get(reqUrl.pathname) : undefined;
const handler = this.pathMappings.get(reqUrl.pathname);
if (handler) { if (handler) {
return handler(req, reqUrl, res); return handler(req, reqUrl, res);
} }
@@ -95,15 +101,4 @@ export class SimpleWebServer {
public on(pathMapping: string, handler: WebHandler) { public on(pathMapping: string, handler: WebHandler) {
this.pathMappings.set(pathMapping, handler); 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);
}
} }