mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Fix more azurecore strict nulls (#20547)
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -35,7 +35,6 @@ interface CryptoValues {
|
||||
|
||||
export class AzureAuthCodeGrant extends AzureAuth {
|
||||
private static readonly USER_FRIENDLY_NAME: string = localize('azure.azureAuthCodeGrantName', 'Azure Auth Code Grant');
|
||||
private server: SimpleWebServer;
|
||||
|
||||
constructor(
|
||||
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 authCompletePromise = new Promise<void>((resolve, reject) => authCompleteDeferred = { resolve, reject });
|
||||
let authResponse: AuthCodeResponse;
|
||||
@@ -60,7 +59,7 @@ export class AzureAuthCodeGrant extends AzureAuth {
|
||||
|
||||
return {
|
||||
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> {
|
||||
this.server = new SimpleWebServer();
|
||||
const server = new SimpleWebServer();
|
||||
let serverPort: string;
|
||||
|
||||
try {
|
||||
serverPort = await this.server.startup();
|
||||
serverPort = await server.startup();
|
||||
} 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.');
|
||||
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)}`;
|
||||
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 {
|
||||
authCode,
|
||||
codeVerifier,
|
||||
|
||||
@@ -84,7 +84,7 @@ export class AzureDeviceCode extends AzureAuth {
|
||||
|
||||
return {
|
||||
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 fiveMinutes = 5 * 60 * 1000;
|
||||
|
||||
return new Promise<DeviceCodeLoginResult | undefined>((resolve, reject) => {
|
||||
return new Promise<DeviceCodeLoginResult>((resolve, reject) => {
|
||||
let timeout: NodeJS.Timer;
|
||||
|
||||
const timer = setInterval(async () => {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user