Skip to content

Commit 2aacebf

Browse files
committed
fix(util-user-agent-node): should only load app id once
1 parent 0745502 commit 2aacebf

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/util-user-agent-node/src/index.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,14 @@ describe("defaultUserAgent", () => {
5454
const userAgent = await defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" })();
5555
expect(userAgent).toContainEqual([`app/${appId}`]);
5656
});
57+
58+
it("should memoize app id", async () => {
59+
mockAppIdLoader.mockClear();
60+
const appId = "appId12345";
61+
mockAppIdLoader.mockResolvedValue(appId);
62+
const userAgnetProvider = defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" });
63+
await userAgnetProvider();
64+
await userAgnetProvider();
65+
expect(mockAppIdLoader).toBeCalledTimes(1);
66+
});
5767
});

packages/util-user-agent-node/src/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ interface DefaultUserAgentOptions {
1414
/**
1515
* Collect metrics from runtime to put into user agent.
1616
*/
17-
export const defaultUserAgent = ({
18-
serviceId,
19-
clientVersion,
20-
}: DefaultUserAgentOptions): Provider<UserAgent> => async () => {
17+
export const defaultUserAgent = ({ serviceId, clientVersion }: DefaultUserAgentOptions): Provider<UserAgent> => {
2118
const sections: UserAgent = [
2219
// sdk-metadata
2320
["aws-sdk-js", clientVersion],
@@ -40,14 +37,17 @@ export const defaultUserAgent = ({
4037
sections.push([`exec-env/${env.AWS_EXECUTION_ENV}`]);
4138
}
4239

43-
const appId = await loadConfig<string | undefined>({
40+
const appIdPromise = loadConfig<string | undefined>({
4441
environmentVariableSelector: (env) => env[UA_APP_ID_ENV_NAME],
4542
configFileSelector: (profile) => profile[UA_APP_ID_INI_NAME],
4643
default: undefined,
4744
})();
48-
if (appId) {
49-
sections.push([`app/${appId}`]);
50-
}
5145

52-
return sections;
46+
return async () => {
47+
const appId = await appIdPromise;
48+
if (appId) {
49+
sections.push([`app/${appId}`]);
50+
}
51+
return sections;
52+
};
5353
};

0 commit comments

Comments
 (0)