-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathload.ts
More file actions
101 lines (89 loc) · 4.8 KB
/
load.ts
File metadata and controls
101 lines (89 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { TokenCredential } from "@azure/identity";
import { AzureAppConfiguration } from "./AzureAppConfiguration.js";
import { AzureAppConfigurationImpl } from "./AzureAppConfigurationImpl.js";
import { AzureAppConfigurationOptions } from "./AzureAppConfigurationOptions.js";
import { ConfigurationClientManager } from "./ConfigurationClientManager.js";
import { CdnTokenPipelinePolicy, AnonymousRequestPipelinePolicy } from "./azureFrontDoor/cdnRequestPipelinePolicy.js";
import { instanceOfTokenCredential } from "./common/utils.js";
import { ArgumentError } from "./common/error.js";
const MIN_DELAY_FOR_UNHANDLED_ERROR: number = 5_000; // 5 seconds
// Empty token credential to be used when loading from Azure Front Door
const emptyTokenCredential: TokenCredential = {
getToken: async () => ({ token: "", expiresOnTimestamp: Number.MAX_SAFE_INTEGER })
};
/**
* Loads the data from Azure App Configuration service and returns an instance of AzureAppConfiguration.
* @param connectionString The connection string for the App Configuration store.
* @param options Optional parameters.
*/
export async function load(connectionString: string, options?: AzureAppConfigurationOptions): Promise<AzureAppConfiguration>;
/**
* Loads the data from Azure App Configuration service and returns an instance of AzureAppConfiguration.
* @param endpoint The URL to the App Configuration store.
* @param credential The credential to use to connect to the App Configuration store.
* @param options Optional parameters.
*/
export async function load(endpoint: URL | string, credential: TokenCredential, options?: AzureAppConfigurationOptions): Promise<AzureAppConfiguration>;
export async function load(
connectionStringOrEndpoint: string | URL,
credentialOrOptions?: TokenCredential | AzureAppConfigurationOptions,
appConfigOptions?: AzureAppConfigurationOptions
): Promise<AzureAppConfiguration> {
const startTimestamp = Date.now();
let options: AzureAppConfigurationOptions | undefined;
const clientManager = new ConfigurationClientManager(connectionStringOrEndpoint, credentialOrOptions, appConfigOptions);
await clientManager.init();
if (!instanceOfTokenCredential(credentialOrOptions)) {
options = credentialOrOptions as AzureAppConfigurationOptions;
} else {
options = appConfigOptions;
}
try {
const isCdnUsed: boolean = credentialOrOptions === emptyTokenCredential;
const appConfiguration = new AzureAppConfigurationImpl(clientManager, options, isCdnUsed);
await appConfiguration.load();
return appConfiguration;
} catch (error) {
// load() method is called in the application's startup code path.
// Unhandled exceptions cause application crash which can result in crash loops as orchestrators attempt to restart the application.
// 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.
const delay = MIN_DELAY_FOR_UNHANDLED_ERROR - (Date.now() - startTimestamp);
if (delay > 0) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
throw error;
}
}
/**
* Loads the data from Azure Front Door (CDN) and returns an instance of AzureAppConfiguration.
* @param endpoint The URL to the Azure Front Door.
* @param appConfigOptions Optional parameters.
*/
export async function loadFromAzureFrontDoor(endpoint: URL | string, options?: AzureAppConfigurationOptions): Promise<AzureAppConfiguration>;
export async function loadFromAzureFrontDoor(
endpoint: string | URL,
appConfigOptions?: AzureAppConfigurationOptions
): Promise<AzureAppConfiguration> {
if (!appConfigOptions) {
appConfigOptions = {};
}
if (appConfigOptions.replicaDiscoveryEnabled) {
throw new ArgumentError("Replica discovery is not supported when loading from Azure Front Door.");
}
if (appConfigOptions.loadBalancingEnabled) {
throw new ArgumentError("Load balancing is not supported when loading from Azure Front Door.");
}
appConfigOptions.replicaDiscoveryEnabled = false; // Disable replica discovery when loading from Azure Front Door
appConfigOptions.clientOptions = {
...appConfigOptions.clientOptions,
// Add etag url policy to append etag to the request url for breaking CDN cache
additionalPolicies: [
...(appConfigOptions.clientOptions?.additionalPolicies || []),
{ policy: new CdnTokenPipelinePolicy(), position: "perCall" },
{ policy: new AnonymousRequestPipelinePolicy(), position: "perRetry" }
]
};
return await load(endpoint, emptyTokenCredential, appConfigOptions);
}