Skip to content

Commit 1059213

Browse files
authored
Rename constants in UPPER_SNAKE_CASE (#68)
1 parent bf0c1ab commit 1059213

File tree

7 files changed

+79
-78
lines changed

7 files changed

+79
-78
lines changed

src/AzureAppConfigurationImpl.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { AzureAppConfiguration, ConfigurationObjectConstructionOptions } from ".
77
import { AzureAppConfigurationOptions } from "./AzureAppConfigurationOptions";
88
import { IKeyValueAdapter } from "./IKeyValueAdapter";
99
import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter";
10-
import { DefaultRefreshIntervalInMs, MinimumRefreshIntervalInMs } from "./RefreshOptions";
10+
import { DEFAULT_REFRESH_INTERVAL_IN_MS, MIN_REFRESH_INTERVAL_IN_MS } from "./RefreshOptions";
1111
import { Disposable } from "./common/disposable";
1212
import { AzureKeyVaultKeyValueAdapter } from "./keyvault/AzureKeyVaultKeyValueAdapter";
1313
import { RefreshTimer } from "./refresh/RefreshTimer";
14-
import { CorrelationContextHeaderName } from "./requestTracing/constants";
14+
import { CORRELATION_CONTEXT_HEADER_NAME } from "./requestTracing/constants";
1515
import { createCorrelationContextHeader, requestTracingEnabled } from "./requestTracing/utils";
1616
import { KeyFilter, LabelFilter, SettingSelector } from "./types";
1717

@@ -33,7 +33,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
3333
#isInitialLoadCompleted: boolean = false;
3434

3535
// Refresh
36-
#refreshInterval: number = DefaultRefreshIntervalInMs;
36+
#refreshInterval: number = DEFAULT_REFRESH_INTERVAL_IN_MS;
3737
#onRefreshListeners: Array<() => any> = [];
3838
/**
3939
* Aka watched settings.
@@ -64,8 +64,8 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
6464

6565
// custom refresh interval
6666
if (refreshIntervalInMs !== undefined) {
67-
if (refreshIntervalInMs < MinimumRefreshIntervalInMs) {
68-
throw new Error(`The refresh interval cannot be less than ${MinimumRefreshIntervalInMs} milliseconds.`);
67+
if (refreshIntervalInMs < MIN_REFRESH_INTERVAL_IN_MS) {
68+
throw new Error(`The refresh interval cannot be less than ${MIN_REFRESH_INTERVAL_IN_MS} milliseconds.`);
6969

7070
} else {
7171
this.#refreshInterval = refreshIntervalInMs;
@@ -139,10 +139,11 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
139139
keyFilter: selector.keyFilter,
140140
labelFilter: selector.labelFilter
141141
};
142+
142143
if (this.#requestTracingEnabled) {
143144
listOptions.requestOptions = {
144145
customHeaders: {
145-
[CorrelationContextHeaderName]: createCorrelationContextHeader(this.#options, this.#isInitialLoadCompleted)
146+
[CORRELATION_CONTEXT_HEADER_NAME]: createCorrelationContextHeader(this.#options, this.#isInitialLoadCompleted)
146147
}
147148
}
148149
}
@@ -348,7 +349,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
348349
if (this.#requestTracingEnabled) {
349350
options.requestOptions = {
350351
customHeaders: {
351-
[CorrelationContextHeaderName]: createCorrelationContextHeader(this.#options, this.#isInitialLoadCompleted)
352+
[CORRELATION_CONTEXT_HEADER_NAME]: createCorrelationContextHeader(this.#options, this.#isInitialLoadCompleted)
352353
}
353354
}
354355
}

src/RefreshOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import { WatchedSetting } from "./WatchedSetting";
55

6-
export const DefaultRefreshIntervalInMs = 30 * 1000;
7-
export const MinimumRefreshIntervalInMs = 1 * 1000;
6+
export const DEFAULT_REFRESH_INTERVAL_IN_MS = 30 * 1000;
7+
export const MIN_REFRESH_INTERVAL_IN_MS = 1 * 1000;
88

99
export interface RefreshOptions {
1010
/**

src/load.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { AzureAppConfigurationImpl } from "./AzureAppConfigurationImpl";
88
import { AzureAppConfigurationOptions, MaxRetries, MaxRetryDelayInMs } from "./AzureAppConfigurationOptions";
99
import * as RequestTracing from "./requestTracing/constants";
1010

11-
const MinDelayForUnhandedError: number = 5000; // 5 seconds
11+
const MIN_DELAY_FOR_UNHANDLED_ERROR: number = 5000; // 5 seconds
1212

1313
/**
1414
* Loads the data from Azure App Configuration service and returns an instance of AzureAppConfiguration.
@@ -70,7 +70,7 @@ export async function load(
7070
// load() method is called in the application's startup code path.
7171
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
7272
// Knowing the intended usage of the provider in startup code path, we mitigate back-to-back crash loops from overloading the server with requests by waiting a minimum time to propagate fatal errors.
73-
const delay = MinDelayForUnhandedError - (Date.now() - startTimestamp);
73+
const delay = MIN_DELAY_FOR_UNHANDLED_ERROR - (Date.now() - startTimestamp);
7474
if (delay > 0) {
7575
await new Promise((resolve) => setTimeout(resolve, delay));
7676
}
@@ -84,7 +84,7 @@ function instanceOfTokenCredential(obj: unknown) {
8484

8585
function getClientOptions(options?: AzureAppConfigurationOptions): AppConfigurationClientOptions | undefined {
8686
// user-agent
87-
let userAgentPrefix = RequestTracing.UserAgentPrefix; // Default UA for JavaScript Provider
87+
let userAgentPrefix = RequestTracing.USER_AGENT_PREFIX; // Default UA for JavaScript Provider
8888
const userAgentOptions = options?.clientOptions?.userAgentOptions;
8989
if (userAgentOptions?.userAgentPrefix) {
9090
userAgentPrefix = `${userAgentOptions.userAgentPrefix} ${userAgentPrefix}`; // Prepend if UA prefix specified by user

src/refresh/RefreshTimer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
* - Because of the jitter, the maximum backoff time is actually `MaximumBackoffInMs` * (1 + `JitterRatio`).
1717
*/
1818

19-
const MinimumBackoffInMs = 30 * 1000; // 30s
20-
const MaximumBackoffInMs = 10 * 60 * 1000; // 10min
21-
const MaxSafeExponential = 30; // Used to avoid overflow. bitwise operations in JavaScript are limited to 32 bits. It overflows at 2^31 - 1.
22-
const JitterRatio = 0.25;
19+
const MIN_BACKOFF_IN_MS = 30 * 1000; // 30s
20+
const MAX_BACKOFF_IN_MS = 10 * 60 * 1000; // 10min
21+
const MAX_SAFE_EXPONENTIAL = 30; // Used to avoid overflow. bitwise operations in JavaScript are limited to 32 bits. It overflows at 2^31 - 1.
22+
const JITTER_RATIO = 0.25;
2323

2424
export class RefreshTimer {
25-
#minBackoff: number = MinimumBackoffInMs;
26-
#maxBackoff: number = MaximumBackoffInMs;
25+
#minBackoff: number = MIN_BACKOFF_IN_MS;
26+
#maxBackoff: number = MAX_BACKOFF_IN_MS;
2727
#failedAttempts: number = 0;
2828
#backoffEnd: number; // Timestamp
2929
#interval: number;
@@ -70,14 +70,14 @@ export class RefreshTimer {
7070
}
7171

7272
// exponential: minBackoffMs * 2^(failedAttempts-1)
73-
const exponential = Math.min(this.#failedAttempts - 1, MaxSafeExponential);
73+
const exponential = Math.min(this.#failedAttempts - 1, MAX_SAFE_EXPONENTIAL);
7474
let calculatedBackoffMs = minBackoffMs * (1 << exponential);
7575
if (calculatedBackoffMs > maxBackoffMs) {
7676
calculatedBackoffMs = maxBackoffMs;
7777
}
7878

7979
// jitter: random value between [-1, 1) * jitterRatio * calculatedBackoffMs
80-
const jitter = JitterRatio * (Math.random() * 2 - 1);
80+
const jitter = JITTER_RATIO * (Math.random() * 2 - 1);
8181

8282
return calculatedBackoffMs * (1 + jitter);
8383
}

src/requestTracing/constants.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { Version } from "../version";
4+
import { VERSION } from "../version";
55

6-
export const RequestTracingDisabledEnvironmentVariable = "AZURE_APP_CONFIGURATION_TRACING_DISABLED";
6+
export const ENV_AZURE_APP_CONFIGURATION_TRACING_DISABLED = "AZURE_APP_CONFIGURATION_TRACING_DISABLED";
77

88
// User Agent
9-
export const UserAgentPrefix = `javascript-appconfiguration-provider/${Version}`;
9+
export const USER_AGENT_PREFIX = `javascript-appconfiguration-provider/${VERSION}`;
1010

1111
// Correlation Context
12-
export const CorrelationContextHeaderName = "Correlation-Context";
12+
export const CORRELATION_CONTEXT_HEADER_NAME = "Correlation-Context";
1313

1414
// Env
15-
export const NodeJSEnvironmentVariable = "NODE_ENV";
16-
export const NodeJSDevEnvironmentVariableValue = "development";
17-
export const EnvironmentKey = "Env";
18-
export const DevEnvironmentValue = "Dev";
15+
export const NODEJS_ENV_VAR = "NODE_ENV";
16+
export const NODEJS_DEV_ENV_VAL = "development";
17+
export const ENV_KEY = "Env";
18+
export const DEV_ENV_VAL = "Dev";
1919

2020
// Host Type
21-
export const HostTypeKey = "Host";
21+
export const HOST_TYPE_KEY = "Host";
2222
export enum HostType {
23-
AzureFunction = "AzureFunction",
24-
AzureWebApp = "AzureWebApp",
25-
ContainerApp = "ContainerApp",
26-
Kubernetes = "Kubernetes",
27-
ServiceFabric = "ServiceFabric",
23+
AZURE_FUNCTION = "AzureFunction",
24+
AZURE_WEB_APP = "AzureWebApp",
25+
CONTAINER_APP = "ContainerApp",
26+
KUBERNETES = "Kubernetes",
27+
SERVICE_FABRIC = "ServiceFabric",
2828
// Client-side
29-
Browser = "Web",
30-
WebWorker = "WebWorker"
29+
BROWSER = "Web",
30+
WEB_WORKER = "WebWorker"
3131
}
3232

3333
// Environment variables to identify Host type.
34-
export const AzureFunctionEnvironmentVariable = "FUNCTIONS_EXTENSION_VERSION";
35-
export const AzureWebAppEnvironmentVariable = "WEBSITE_SITE_NAME";
36-
export const ContainerAppEnvironmentVariable = "CONTAINER_APP_NAME";
37-
export const KubernetesEnvironmentVariable = "KUBERNETES_PORT";
38-
export const ServiceFabricEnvironmentVariable = "Fabric_NodeName"; // See: https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference
34+
export const AZURE_FUNCTION_ENV_VAR = "FUNCTIONS_EXTENSION_VERSION";
35+
export const AZURE_WEB_APP_ENV_VAR = "WEBSITE_SITE_NAME";
36+
export const CONTAINER_APP_ENV_VAR = "CONTAINER_APP_NAME";
37+
export const KUBERNETES_ENV_VAR = "KUBERNETES_PORT";
38+
export const SERVICE_FABRIC_ENV_VAR = "Fabric_NodeName"; // See: https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference
3939

4040
// Request Type
41-
export const RequestTypeKey = "RequestType";
41+
export const REQUEST_TYPE_KEY = "RequestType";
4242
export enum RequestType {
43-
Startup = "Startup",
44-
Watch = "Watch"
43+
STARTUP = "Startup",
44+
WATCH = "Watch"
4545
}
4646

4747
// Tag names
48-
export const KeyVaultConfiguredTag = "UsesKeyVault";
48+
export const KEY_VAULT_CONFIGURED_TAG = "UsesKeyVault";

src/requestTracing/utils.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33

44
import { AzureAppConfigurationOptions } from "../AzureAppConfigurationOptions";
55
import {
6-
AzureFunctionEnvironmentVariable,
7-
AzureWebAppEnvironmentVariable,
8-
ContainerAppEnvironmentVariable,
9-
DevEnvironmentValue,
10-
EnvironmentKey,
6+
AZURE_FUNCTION_ENV_VAR,
7+
AZURE_WEB_APP_ENV_VAR,
8+
CONTAINER_APP_ENV_VAR,
9+
DEV_ENV_VAL,
10+
ENV_AZURE_APP_CONFIGURATION_TRACING_DISABLED,
11+
ENV_KEY,
12+
HOST_TYPE_KEY,
1113
HostType,
12-
HostTypeKey,
13-
KeyVaultConfiguredTag,
14-
KubernetesEnvironmentVariable,
15-
NodeJSDevEnvironmentVariableValue,
16-
NodeJSEnvironmentVariable,
17-
RequestTracingDisabledEnvironmentVariable,
14+
KEY_VAULT_CONFIGURED_TAG,
15+
KUBERNETES_ENV_VAR,
16+
NODEJS_DEV_ENV_VAL,
17+
NODEJS_ENV_VAR,
18+
REQUEST_TYPE_KEY,
1819
RequestType,
19-
RequestTypeKey,
20-
ServiceFabricEnvironmentVariable
20+
SERVICE_FABRIC_ENV_VAR
2121
} from "./constants";
2222

2323
// Utils
@@ -29,15 +29,15 @@ export function createCorrelationContextHeader(options: AzureAppConfigurationOpt
2929
UsersKeyVault
3030
*/
3131
const keyValues = new Map<string, string | undefined>();
32-
keyValues.set(RequestTypeKey, isInitialLoadCompleted ? RequestType.Watch : RequestType.Startup);
33-
keyValues.set(HostTypeKey, getHostType());
34-
keyValues.set(EnvironmentKey, isDevEnvironment() ? DevEnvironmentValue : undefined);
32+
keyValues.set(REQUEST_TYPE_KEY, isInitialLoadCompleted ? RequestType.WATCH : RequestType.STARTUP);
33+
keyValues.set(HOST_TYPE_KEY, getHostType());
34+
keyValues.set(ENV_KEY, isDevEnvironment() ? DEV_ENV_VAL : undefined);
3535

3636
const tags: string[] = [];
3737
if (options?.keyVaultOptions) {
3838
const { credential, secretClients, secretResolver } = options.keyVaultOptions;
3939
if (credential !== undefined || secretClients?.length || secretResolver !== undefined) {
40-
tags.push(KeyVaultConfiguredTag);
40+
tags.push(KEY_VAULT_CONFIGURED_TAG);
4141
}
4242
}
4343

@@ -55,7 +55,7 @@ export function createCorrelationContextHeader(options: AzureAppConfigurationOpt
5555
}
5656

5757
export function requestTracingEnabled(): boolean {
58-
const requestTracingDisabledEnv = getEnvironmentVariable(RequestTracingDisabledEnvironmentVariable);
58+
const requestTracingDisabledEnv = getEnvironmentVariable(ENV_AZURE_APP_CONFIGURATION_TRACING_DISABLED);
5959
const disabled = requestTracingDisabledEnv?.toLowerCase() === "true";
6060
return !disabled;
6161
}
@@ -71,27 +71,27 @@ function getEnvironmentVariable(name: string) {
7171

7272
function getHostType(): string | undefined {
7373
let hostType: string | undefined;
74-
if (getEnvironmentVariable(AzureFunctionEnvironmentVariable)) {
75-
hostType = HostType.AzureFunction;
76-
} else if (getEnvironmentVariable(AzureWebAppEnvironmentVariable)) {
77-
hostType = HostType.AzureWebApp;
78-
} else if (getEnvironmentVariable(ContainerAppEnvironmentVariable)) {
79-
hostType = HostType.ContainerApp;
80-
} else if (getEnvironmentVariable(KubernetesEnvironmentVariable)) {
81-
hostType = HostType.Kubernetes;
82-
} else if (getEnvironmentVariable(ServiceFabricEnvironmentVariable)) {
83-
hostType = HostType.ServiceFabric;
74+
if (getEnvironmentVariable(AZURE_FUNCTION_ENV_VAR)) {
75+
hostType = HostType.AZURE_FUNCTION;
76+
} else if (getEnvironmentVariable(AZURE_WEB_APP_ENV_VAR)) {
77+
hostType = HostType.AZURE_WEB_APP;
78+
} else if (getEnvironmentVariable(CONTAINER_APP_ENV_VAR)) {
79+
hostType = HostType.CONTAINER_APP;
80+
} else if (getEnvironmentVariable(KUBERNETES_ENV_VAR)) {
81+
hostType = HostType.KUBERNETES;
82+
} else if (getEnvironmentVariable(SERVICE_FABRIC_ENV_VAR)) {
83+
hostType = HostType.SERVICE_FABRIC;
8484
} else if (isBrowser()) {
85-
hostType = HostType.Browser;
85+
hostType = HostType.BROWSER;
8686
} else if (isWebWorker()) {
87-
hostType = HostType.WebWorker;
87+
hostType = HostType.WEB_WORKER;
8888
}
8989
return hostType;
9090
}
9191

9292
function isDevEnvironment(): boolean {
93-
const envType = getEnvironmentVariable(NodeJSEnvironmentVariable);
94-
if (NodeJSDevEnvironmentVariableValue === envType?.toLowerCase()) {
93+
const envType = getEnvironmentVariable(NODEJS_ENV_VAR);
94+
if (NODEJS_DEV_ENV_VAL === envType?.toLowerCase()) {
9595
return true;
9696
}
9797
return false;

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
export const Version = "1.0.0-preview";
4+
export const VERSION = "1.0.0-preview";

0 commit comments

Comments
 (0)