Skip to content

Commit f9984f1

Browse files
add loadCdn
1 parent cca37e7 commit f9984f1

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"dependencies": {
3-
"@azure/app-configuration-provider": "latest",
3+
"@azure/app-configuration-provider": "../",
44
"@azure/identity": "^4.1.0",
55
"dotenv": "^16.3.1"
66
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
export { AzureAppConfiguration } from "./AzureAppConfiguration";
55
export { Disposable } from "./common/disposable";
6-
export { load } from "./load";
6+
export { load, loadCdn } from "./load";
77
export { KeyFilter, LabelFilter } from "./types";

src/load.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,57 @@ export async function load(
7878
}
7979
}
8080

81+
/**
82+
* Loads the data from a CDN and returns an instance of AzureAppConfiguration.
83+
* @param endpoint The URL to the CDN.
84+
* @param options Optional parameters.
85+
*/
86+
export async function loadCdn(endpoint: URL | string, options?: AzureAppConfigurationOptions): Promise<AzureAppConfiguration>;
87+
88+
export async function loadCdn(
89+
endpoint: string | URL,
90+
appConfigOptions?: AzureAppConfigurationOptions
91+
): Promise<AzureAppConfiguration> {
92+
const startTimestamp = Date.now();
93+
let client: AppConfigurationClient;
94+
let options: AzureAppConfigurationOptions | undefined;
95+
96+
if (typeof endpoint === "string") {
97+
try {
98+
endpoint = new URL(endpoint);
99+
} catch (error) {
100+
if (error.code === "ERR_INVALID_URL") {
101+
throw new Error("Invalid endpoint URL.", { cause: error });
102+
} else {
103+
throw error;
104+
}
105+
}
106+
}
107+
const emptyTokenCredential: TokenCredential = {
108+
getToken: async () => ({ token: "", expiresOnTimestamp: 0 })
109+
};
110+
options = appConfigOptions;
111+
const clientOptions = getClientOptions(options);
112+
// App Configuration SDK will not distinguish between CDN and App Configuration endpoints. The SDK will just send request to the endpoint with the given token credential.
113+
// CDN should be the front door of App Configuration and forward the request to the App Configuration service.
114+
client = new AppConfigurationClient(endpoint.toString(), emptyTokenCredential, clientOptions);
115+
116+
try {
117+
const appConfiguration = new AzureAppConfigurationImpl(client, options);
118+
await appConfiguration.load();
119+
return appConfiguration;
120+
} catch (error) {
121+
// load() method is called in the application's startup code path.
122+
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
123+
// 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.
124+
const delay = MIN_DELAY_FOR_UNHANDLED_ERROR - (Date.now() - startTimestamp);
125+
if (delay > 0) {
126+
await new Promise((resolve) => setTimeout(resolve, delay));
127+
}
128+
throw error;
129+
}
130+
}
131+
81132
function instanceOfTokenCredential(obj: unknown) {
82133
return obj && typeof obj === "object" && "getToken" in obj && typeof obj.getToken === "function";
83134
}

0 commit comments

Comments
 (0)