From db666a21a36b1f7dab7c33ad33712a983fb21d02 Mon Sep 17 00:00:00 2001 From: Yan Zhang Date: Wed, 31 Jan 2024 11:09:55 +0800 Subject: [PATCH] rename members to adopt private properties --- src/AzureAppConfigurationImpl.ts | 2 +- src/JsonKeyValueAdapter.ts | 9 ++-- src/common/disposable.ts | 15 +++--- src/keyvault/AzureKeyVaultKeyValueAdapter.ts | 39 +++++++------- src/refresh/RefreshTimer.ts | 53 +++++++++++--------- 5 files changed, 62 insertions(+), 56 deletions(-) diff --git a/src/AzureAppConfigurationImpl.ts b/src/AzureAppConfigurationImpl.ts index 0b808a5f..3cce9ab5 100644 --- a/src/AzureAppConfigurationImpl.ts +++ b/src/AzureAppConfigurationImpl.ts @@ -176,7 +176,7 @@ export class AzureAppConfigurationImpl extends Map implements Azure /** * Refresh the configuration store. */ - public async refresh(): Promise { + async refresh(): Promise { if (!this.#refreshEnabled) { throw new Error("Refresh is not enabled."); } diff --git a/src/JsonKeyValueAdapter.ts b/src/JsonKeyValueAdapter.ts index 44daaee1..4be758a1 100644 --- a/src/JsonKeyValueAdapter.ts +++ b/src/JsonKeyValueAdapter.ts @@ -4,24 +4,23 @@ import { ConfigurationSetting, secretReferenceContentType } from "@azure/app-configuration"; import { IKeyValueAdapter } from "./IKeyValueAdapter"; - export class JsonKeyValueAdapter implements IKeyValueAdapter { - private static readonly ExcludedJsonContentTypes: string[] = [ + static readonly #ExcludedJsonContentTypes: string[] = [ secretReferenceContentType // TODO: exclude application/vnd.microsoft.appconfig.ff+json after feature management is supported ]; - public canProcess(setting: ConfigurationSetting): boolean { + canProcess(setting: ConfigurationSetting): boolean { if (!setting.contentType) { return false; } - if (JsonKeyValueAdapter.ExcludedJsonContentTypes.includes(setting.contentType)) { + if (JsonKeyValueAdapter.#ExcludedJsonContentTypes.includes(setting.contentType)) { return false; } return isJsonContentType(setting.contentType); } - public async processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]> { + async processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]> { let parsedValue: unknown; if (setting.value !== undefined) { try { diff --git a/src/common/disposable.ts b/src/common/disposable.ts index 88960137..4843e121 100644 --- a/src/common/disposable.ts +++ b/src/common/disposable.ts @@ -2,14 +2,17 @@ // Licensed under the MIT license. export class Disposable { - private disposed = false; - constructor(private callOnDispose: () => any) { } + #disposed = false; + #callOnDispose: () => any; + + constructor(callOnDispose: () => any) { + this.#callOnDispose = callOnDispose; + } dispose() { - if (!this.disposed) { - this.callOnDispose(); + if (!this.#disposed) { + this.#callOnDispose(); } - this.disposed = true; + this.#disposed = true; } - } \ No newline at end of file diff --git a/src/keyvault/AzureKeyVaultKeyValueAdapter.ts b/src/keyvault/AzureKeyVaultKeyValueAdapter.ts index b7025d58..b65a0314 100644 --- a/src/keyvault/AzureKeyVaultKeyValueAdapter.ts +++ b/src/keyvault/AzureKeyVaultKeyValueAdapter.ts @@ -10,19 +10,20 @@ export class AzureKeyVaultKeyValueAdapter implements IKeyValueAdapter { /** * Map vault hostname to corresponding secret client. */ - private secretClients: Map; + #secretClients: Map; + #keyVaultOptions: KeyVaultOptions | undefined; - constructor( - private keyVaultOptions: KeyVaultOptions | undefined - ) { } + constructor(keyVaultOptions: KeyVaultOptions | undefined) { + this.#keyVaultOptions = keyVaultOptions; + } - public canProcess(setting: ConfigurationSetting): boolean { + canProcess(setting: ConfigurationSetting): boolean { return isSecretReference(setting); } - public async processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]> { + async processKeyValue(setting: ConfigurationSetting): Promise<[string, unknown]> { // TODO: cache results to save requests. - if (!this.keyVaultOptions) { + if (!this.#keyVaultOptions) { throw new Error("Configure keyVaultOptions to resolve Key Vault Reference(s)."); } @@ -31,37 +32,37 @@ export class AzureKeyVaultKeyValueAdapter implements IKeyValueAdapter { parseSecretReference(setting).value.secretId ); - const client = this.getSecretClient(new URL(vaultUrl)); + const client = this.#getSecretClient(new URL(vaultUrl)); if (client) { // TODO: what if error occurs when reading a key vault value? Now it breaks the whole load. const secret = await client.getSecret(secretName, { version }); return [setting.key, secret.value]; } - if (this.keyVaultOptions.secretResolver) { - return [setting.key, await this.keyVaultOptions.secretResolver(new URL(sourceId))]; + if (this.#keyVaultOptions.secretResolver) { + return [setting.key, await this.#keyVaultOptions.secretResolver(new URL(sourceId))]; } throw new Error("No key vault credential or secret resolver callback configured, and no matching secret client could be found."); } - private getSecretClient(vaultUrl: URL): SecretClient | undefined { - if (this.secretClients === undefined) { - this.secretClients = new Map(); - for (const c of this.keyVaultOptions?.secretClients ?? []) { - this.secretClients.set(getHost(c.vaultUrl), c); + #getSecretClient(vaultUrl: URL): SecretClient | undefined { + if (this.#secretClients === undefined) { + this.#secretClients = new Map(); + for (const c of this.#keyVaultOptions?.secretClients ?? []) { + this.#secretClients.set(getHost(c.vaultUrl), c); } } let client: SecretClient | undefined; - client = this.secretClients.get(vaultUrl.host); + client = this.#secretClients.get(vaultUrl.host); if (client !== undefined) { return client; } - if (this.keyVaultOptions?.credential) { - client = new SecretClient(vaultUrl.toString(), this.keyVaultOptions.credential); - this.secretClients.set(vaultUrl.host, client); + if (this.#keyVaultOptions?.credential) { + client = new SecretClient(vaultUrl.toString(), this.#keyVaultOptions.credential); + this.#secretClients.set(vaultUrl.host, client); return client; } diff --git a/src/refresh/RefreshTimer.ts b/src/refresh/RefreshTimer.ts index ac26f31b..3ae824b1 100644 --- a/src/refresh/RefreshTimer.ts +++ b/src/refresh/RefreshTimer.ts @@ -22,52 +22,55 @@ const MaxSafeExponential = 30; // Used to avoid overflow. bitwise operations in const JitterRatio = 0.25; export class RefreshTimer { - private _minBackoff: number = MinimumBackoffInMs; - private _maxBackoff: number = MaximumBackoffInMs; - private _failedAttempts: number = 0; - private _backoffEnd: number; // Timestamp + #minBackoff: number = MinimumBackoffInMs; + #maxBackoff: number = MaximumBackoffInMs; + #failedAttempts: number = 0; + #backoffEnd: number; // Timestamp + #interval: number; + constructor( - private _interval: number + interval: number ) { - if (this._interval <= 0) { - throw new Error(`Refresh interval must be greater than 0. Given: ${this._interval}`); + if (interval <= 0) { + throw new Error(`Refresh interval must be greater than 0. Given: ${this.#interval}`); } - this._backoffEnd = Date.now() + this._interval; + this.#interval = interval; + this.#backoffEnd = Date.now() + this.#interval; } - public canRefresh(): boolean { - return Date.now() >= this._backoffEnd; + canRefresh(): boolean { + return Date.now() >= this.#backoffEnd; } - public backoff(): void { - this._failedAttempts += 1; - this._backoffEnd = Date.now() + this._calculateBackoffTime(); + backoff(): void { + this.#failedAttempts += 1; + this.#backoffEnd = Date.now() + this.#calculateBackoffTime(); } - public reset(): void { - this._failedAttempts = 0; - this._backoffEnd = Date.now() + this._interval; + reset(): void { + this.#failedAttempts = 0; + this.#backoffEnd = Date.now() + this.#interval; } - private _calculateBackoffTime(): number { + #calculateBackoffTime(): number { let minBackoffMs: number; let maxBackoffMs: number; - if (this._interval <= this._minBackoff) { - return this._interval; + if (this.#interval <= this.#minBackoff) { + return this.#interval; } // _minBackoff <= _interval - if (this._interval <= this._maxBackoff) { - minBackoffMs = this._minBackoff; - maxBackoffMs = this._interval; + if (this.#interval <= this.#maxBackoff) { + minBackoffMs = this.#minBackoff; + maxBackoffMs = this.#interval; } else { - minBackoffMs = this._minBackoff; - maxBackoffMs = this._maxBackoff; + minBackoffMs = this.#minBackoff; + maxBackoffMs = this.#maxBackoff; } // exponential: minBackoffMs * 2^(failedAttempts-1) - const exponential = Math.min(this._failedAttempts - 1, MaxSafeExponential); + const exponential = Math.min(this.#failedAttempts - 1, MaxSafeExponential); let calculatedBackoffMs = minBackoffMs * (1 << exponential); if (calculatedBackoffMs > maxBackoffMs) { calculatedBackoffMs = maxBackoffMs;