@@ -8,6 +8,7 @@ import { AzureAppConfigurationOptions, MaxRetries, MaxRetryDelayInMs } from "./A
88import { isBrowser , isWebWorker } from "./requestTracing/utils.js" ;
99import * as RequestTracing from "./requestTracing/constants.js" ;
1010import { shuffleList } from "./common/utils.js" ;
11+ import { Resolver } from "dns/promises" ;
1112
1213const TCP_ORIGIN_KEY_NAME = "_origin._tcp" ;
1314const ALT_KEY_NAME = "_alt" ;
@@ -148,7 +149,7 @@ export class ConfigurationClientManager {
148149 try {
149150 result = await this . #querySrvTargetHost( host ) ;
150151 } catch ( error ) {
151- console . warn ( `Failed to build fallback clients, ${ error . message } ` ) ;
152+ console . warn ( `Failed to build fallback clients. ${ error . message } ` ) ;
152153 return ; // swallow the error when srv query fails
153154 }
154155
@@ -173,24 +174,32 @@ export class ConfigurationClientManager {
173174 }
174175
175176 /**
176- * Queries SRV records for the given host and returns the target hosts.
177+ * Query SRV records and return target hosts.
177178 */
178179 async #querySrvTargetHost( host : string ) : Promise < string [ ] > {
179180 const results : string [ ] = [ ] ;
180181
181182 try {
182183 // https://nodejs.org/api/dns.html#dnspromisesresolvesrvhostname
183- const resolver = new this . #dns . Resolver ( { timeout : DNS_RESOLVER_TIMEOUT , tries : DNS_RESOLVER_TRIES } ) ;
184+ const resolver = new Resolver ( { timeout : DNS_RESOLVER_TIMEOUT , tries : DNS_RESOLVER_TRIES } ) ;
184185 // On success, resolveSrv() returns an array of SrvRecord
185186 // On failure, resolveSrv() throws an error with code 'ENOTFOUND'.
186- const originRecords = await resolver . resolveSrv ( `${ TCP_ORIGIN_KEY_NAME } .${ host } ` ) ; // look up SRV records for the origin host
187+ const originRecords = await resolver . resolveSrv ( `${ TCP_ORIGIN_KEY_NAME } .${ host } ` ) ; // Look up SRV records for the origin host
188+ if ( originRecords . length === 0 ) {
189+ return results ;
190+ }
191+
192+ // Add the first origin record to results
187193 const originHost = originRecords [ 0 ] . name ;
188- results . push ( originHost ) ; // add the first origin record to results
194+ results . push ( originHost ) ;
189195
190196 let index = 0 ;
191197 while ( index < MAX_ALTNATIVE_SRV_COUNT ) {
192- const currentAlt = `${ ALT_KEY_NAME } ${ index } ` ; // look up SRV records for alternate hosts
198+ const currentAlt = `${ ALT_KEY_NAME } ${ index } ` ; // Look up SRV records for alternate hosts
193199 const altRecords = await resolver . resolveSrv ( `${ currentAlt } .${ TCP_KEY_NAME } .${ originHost } ` ) ;
200+ if ( altRecords . length === 0 ) {
201+ break ;
202+ }
194203
195204 altRecords . forEach ( record => {
196205 const altHost = record . name ;
@@ -202,8 +211,7 @@ export class ConfigurationClientManager {
202211 }
203212 } catch ( err ) {
204213 if ( err . code === "ENOTFOUND" ) {
205- // No more SRV records found, return results.
206- return results ;
214+ return results ; // No more SRV records found, return results
207215 } else {
208216 throw new Error ( `Failed to lookup SRV records: ${ err . message } ` ) ;
209217 }
0 commit comments