22// Licensed under the MIT license.
33
44import { AppConfigurationClient , AppConfigurationClientOptions } from "@azure/app-configuration" ;
5- import { ConfigurationClientWrapper } from "./ConfigurationClientWrapper" ;
5+ import { ConfigurationClientWrapper } from "./ConfigurationClientWrapper.js " ;
66import { TokenCredential } from "@azure/identity" ;
7- import { AzureAppConfigurationOptions , MaxRetries , MaxRetryDelayInMs } from "./AzureAppConfigurationOptions" ;
8- import { isFailoverableEnv } from "./requestTracing/utils" ;
9- import * as RequestTracing from "./requestTracing/constants" ;
7+ import { AzureAppConfigurationOptions , MaxRetries , MaxRetryDelayInMs } from "./AzureAppConfigurationOptions.js " ;
8+ import { isFailoverableEnv } from "./requestTracing/utils.js " ;
9+ import * as RequestTracing from "./requestTracing/constants.js " ;
1010
1111const TCP_ORIGIN_KEY_NAME = "_origin._tcp" ;
1212const ALT_KEY_NAME = "_alt" ;
1313const TCP_KEY_NAME = "_tcp" ;
14- const Endpoint_KEY_NAME = "Endpoint" ;
15- const Id_KEY_NAME = "Id" ;
16- const Secret_KEY_NAME = "Secret" ;
17- const ConnectionStringRegex = / E n d p o i n t = ( .* ) ; I d = ( .* ) ; S e c r e t = ( .* ) / ;
18- const AzConfigDomainLabel = ".azconfig." ;
19- const AppConfigDomainLabel = ".appconfig." ;
20- const FallbackClientRefreshExpireInterval = 60 * 60 * 1000 ; // 1 hour in milliseconds
21- const MinimalClientRefreshInterval = 30 * 1000 ; // 30 seconds in milliseconds
22- const SrvQueryTimeout = 5 * 1000 ; // 5 seconds in milliseconds
14+ const ENDPOINT_KEY_NAME = "Endpoint" ;
15+ const ID_KEY_NAME = "Id" ;
16+ const SECRET_KEY_NAME = "Secret" ;
17+ const AZCONFIG_DOMAIN_LABEL = ".azconfig." ;
18+ const APPCONFIG_DOMAIN_LABEL = ".appconfig." ;
19+ const FALLBACK_CLIENT_REFRESH_EXPIRE_INTERVAL = 60 * 60 * 1000 ; // 1 hour in milliseconds
20+ const MINIMAL_CLIENT_REFRESH_INTERVAL = 30 * 1000 ; // 30 seconds in milliseconds
21+ const SRV_QUERY_TIMEOUT = 5 * 1000 ; // 5 seconds in milliseconds
2322
2423export class ConfigurationClientManager {
2524 isFailoverable : boolean ;
@@ -47,6 +46,7 @@ export class ConfigurationClientManager {
4746 options = credentialOrOptions as AzureAppConfigurationOptions ;
4847 this . #clientOptions = getClientOptions ( options ) ;
4948 staticClient = new AppConfigurationClient ( connectionString , this . #clientOptions) ;
49+ const ConnectionStringRegex = / E n d p o i n t = ( .* ) ; I d = ( .* ) ; S e c r e t = ( .* ) / ;
5050 const regexMatch = connectionString . match ( ConnectionStringRegex ) ;
5151 if ( regexMatch ) {
5252 this . endpoint = regexMatch [ 1 ] ;
@@ -112,7 +112,7 @@ export class ConfigurationClientManager {
112112 async refreshClients ( ) {
113113 const currentTime = Date . now ( ) ;
114114 if ( this . isFailoverable &&
115- currentTime > new Date ( this . #lastFallbackClientRefreshAttempt + MinimalClientRefreshInterval ) . getTime ( ) ) {
115+ currentTime > new Date ( this . #lastFallbackClientRefreshAttempt + MINIMAL_CLIENT_REFRESH_INTERVAL ) . getTime ( ) ) {
116116 this . #lastFallbackClientRefreshAttempt = currentTime ;
117117 const host = new URL ( this . endpoint ) . hostname ;
118118 await this . #discoverFallbackClients( host ) ;
@@ -121,7 +121,7 @@ export class ConfigurationClientManager {
121121
122122 async #discoverFallbackClients( host ) {
123123 const timeout = setTimeout ( ( ) => {
124- } , SrvQueryTimeout ) ;
124+ } , SRV_QUERY_TIMEOUT ) ;
125125 const srvResults = await querySrvTargetHost ( host ) ;
126126
127127 try {
@@ -167,10 +167,10 @@ export class ConfigurationClientManager {
167167 }
168168
169169 #isFallbackClientDiscoveryDue( dateTime ) {
170- return dateTime >= this . #lastFallbackClientRefreshAttempt + MinimalClientRefreshInterval
170+ return dateTime >= this . #lastFallbackClientRefreshAttempt + MINIMAL_CLIENT_REFRESH_INTERVAL
171171 && ( ! this . #dynamicClients
172172 || this . #dynamicClients. every ( client => dateTime < client . backoffEndTime )
173- || dateTime >= this . #lastFallbackClientRefreshTime + FallbackClientRefreshExpireInterval ) ;
173+ || dateTime >= this . #lastFallbackClientRefreshTime + FALLBACK_CLIENT_REFRESH_EXPIRE_INTERVAL ) ;
174174 }
175175}
176176
@@ -181,8 +181,8 @@ async function querySrvTargetHost(host: string): Promise<string[]> {
181181 const results : string [ ] = [ ] ;
182182 let dns ;
183183
184- if ( isFailoverableEnv ( ) ) {
185- dns = require ( " dns/promises" ) ;
184+ if ( typeof global !== "undefined" && global . dns ) {
185+ dns = global . dns ;
186186 } else {
187187 throw new Error ( "Failover is not supported in the current environment." ) ;
188188 }
@@ -245,7 +245,7 @@ function buildConnectionString(endpoint, secret, id: string): string {
245245 return "" ;
246246 }
247247
248- return `${ Endpoint_KEY_NAME } =${ endpoint } ;${ Id_KEY_NAME } =${ id } ;${ Secret_KEY_NAME } =${ secret } ` ;
248+ return `${ ENDPOINT_KEY_NAME } =${ endpoint } ;${ ID_KEY_NAME } =${ id } ;${ SECRET_KEY_NAME } =${ secret } ` ;
249249}
250250
251251/**
@@ -254,7 +254,7 @@ function buildConnectionString(endpoint, secret, id: string): string {
254254export function getValidDomain ( endpoint : string ) : string {
255255 try {
256256 const url = new URL ( endpoint ) ;
257- const trustedDomainLabels = [ AzConfigDomainLabel , AppConfigDomainLabel ] ;
257+ const trustedDomainLabels = [ AZCONFIG_DOMAIN_LABEL , APPCONFIG_DOMAIN_LABEL ] ;
258258 const host = url . hostname . toLowerCase ( ) ;
259259
260260 for ( const label of trustedDomainLabels ) {
0 commit comments