/*! @azure/msal-common v16.5.0 2026-04-16 */
|
'use strict';
|
import { IMDS_VERSION, HTTP_SUCCESS, RegionDiscoverySources, HTTP_BAD_REQUEST, IMDS_ENDPOINT, IMDS_TIMEOUT } from '../utils/Constants.mjs';
|
import { RegionDiscoveryGetRegionFromIMDS, RegionDiscoveryGetCurrentVersion } from '../telemetry/performance/PerformanceEvents.mjs';
|
import { invokeAsync } from '../utils/FunctionWrappers.mjs';
|
|
/*
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
* Licensed under the MIT License.
|
*/
|
class RegionDiscovery {
|
constructor(networkInterface, logger, performanceClient, correlationId) {
|
this.networkInterface = networkInterface;
|
this.logger = logger;
|
this.performanceClient = performanceClient;
|
this.correlationId = correlationId;
|
}
|
/**
|
* Detect the region from the application's environment.
|
*
|
* @returns Promise<string | null>
|
*/
|
async detectRegion(environmentRegion, regionDiscoveryMetadata) {
|
// Initialize auto detected region with the region from the envrionment
|
let autodetectedRegionName = environmentRegion;
|
// Check if a region was detected from the environment, if not, attempt to get the region from IMDS
|
if (!autodetectedRegionName) {
|
const options = RegionDiscovery.IMDS_OPTIONS;
|
try {
|
const localIMDSVersionResponse = await invokeAsync(this.getRegionFromIMDS.bind(this), RegionDiscoveryGetRegionFromIMDS, this.logger, this.performanceClient, this.correlationId)(IMDS_VERSION, options);
|
if (localIMDSVersionResponse.status === HTTP_SUCCESS) {
|
autodetectedRegionName = localIMDSVersionResponse.body;
|
regionDiscoveryMetadata.region_source =
|
RegionDiscoverySources.IMDS;
|
}
|
// If the response using the local IMDS version failed, try to fetch the current version of IMDS and retry.
|
if (localIMDSVersionResponse.status ===
|
HTTP_BAD_REQUEST) {
|
const currentIMDSVersion = await invokeAsync(this.getCurrentVersion.bind(this), RegionDiscoveryGetCurrentVersion, this.logger, this.performanceClient, this.correlationId)(options);
|
if (!currentIMDSVersion) {
|
regionDiscoveryMetadata.region_source =
|
RegionDiscoverySources.FAILED_AUTO_DETECTION;
|
return null;
|
}
|
const currentIMDSVersionResponse = await invokeAsync(this.getRegionFromIMDS.bind(this), RegionDiscoveryGetRegionFromIMDS, this.logger, this.performanceClient, this.correlationId)(currentIMDSVersion, options);
|
if (currentIMDSVersionResponse.status ===
|
HTTP_SUCCESS) {
|
autodetectedRegionName =
|
currentIMDSVersionResponse.body;
|
regionDiscoveryMetadata.region_source =
|
RegionDiscoverySources.IMDS;
|
}
|
}
|
}
|
catch (e) {
|
regionDiscoveryMetadata.region_source =
|
RegionDiscoverySources.FAILED_AUTO_DETECTION;
|
return null;
|
}
|
}
|
else {
|
regionDiscoveryMetadata.region_source =
|
RegionDiscoverySources.ENVIRONMENT_VARIABLE;
|
}
|
// If no region was auto detected from the environment or from the IMDS endpoint, mark the attempt as a FAILED_AUTO_DETECTION
|
if (!autodetectedRegionName) {
|
regionDiscoveryMetadata.region_source =
|
RegionDiscoverySources.FAILED_AUTO_DETECTION;
|
}
|
return autodetectedRegionName || null;
|
}
|
/**
|
* Make the call to the IMDS endpoint
|
*
|
* @param imdsEndpointUrl
|
* @returns Promise<NetworkResponse<string>>
|
*/
|
async getRegionFromIMDS(version, options) {
|
return this.networkInterface.sendGetRequestAsync(`${IMDS_ENDPOINT}?api-version=${version}&format=text`, options, IMDS_TIMEOUT);
|
}
|
/**
|
* Get the most recent version of the IMDS endpoint available
|
*
|
* @returns Promise<string | null>
|
*/
|
async getCurrentVersion(options) {
|
try {
|
const response = await this.networkInterface.sendGetRequestAsync(`${IMDS_ENDPOINT}?format=json`, options);
|
// When IMDS endpoint is called without the api version query param, bad request response comes back with latest version.
|
if (response.status === HTTP_BAD_REQUEST &&
|
response.body &&
|
response.body["newest-versions"] &&
|
response.body["newest-versions"].length > 0) {
|
return response.body["newest-versions"][0];
|
}
|
return null;
|
}
|
catch (e) {
|
return null;
|
}
|
}
|
}
|
// Options for the IMDS endpoint request
|
RegionDiscovery.IMDS_OPTIONS = {
|
headers: {
|
Metadata: "true",
|
},
|
};
|
|
export { RegionDiscovery };
|
//# sourceMappingURL=RegionDiscovery.mjs.map
|