Skip to content

rename members to adopt private properties #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/AzureAppConfigurationImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class AzureAppConfigurationImpl extends Map<string, any> implements Azure
/**
* Refresh the configuration store.
*/
public async refresh(): Promise<void> {
async refresh(): Promise<void> {
if (!this.#refreshEnabled) {
throw new Error("Refresh is not enabled.");
}
Expand Down
9 changes: 4 additions & 5 deletions src/JsonKeyValueAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 9 additions & 6 deletions src/common/disposable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
39 changes: 20 additions & 19 deletions src/keyvault/AzureKeyVaultKeyValueAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ export class AzureKeyVaultKeyValueAdapter implements IKeyValueAdapter {
/**
* Map vault hostname to corresponding secret client.
*/
private secretClients: Map<string, SecretClient>;
#secretClients: Map<string, SecretClient>;
#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).");
}

Expand All @@ -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;
}

Expand Down
53 changes: 28 additions & 25 deletions src/refresh/RefreshTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down