Skip to content

fix(util-user-agent-node): should memoize app id #2223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions packages/util-user-agent-node/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ jest.mock("@aws-sdk/node-config-provider", () => ({
loadConfig: () => mockAppIdLoader,
}));

import { UserAgent } from "@aws-sdk/types";

import { defaultUserAgent } from ".";

const validateUserAgent = (userAgent: UserAgent, expected: UserAgent) => {
expect(userAgent.length).toBe(expected.length);
for (const pair of expected) {
expect(userAgent).toContainEqual(pair);
}
};

describe("defaultUserAgent", () => {
beforeEach(() => {
jest.resetAllMocks();
Expand All @@ -27,31 +36,54 @@ describe("defaultUserAgent", () => {
jest.clearAllMocks();
});

const basicUserAgent: UserAgent = [
["aws-sdk-js", "0.1.0"],
["api/s3", "0.1.0"],
["os/darwin", "19.6.0"],
["lang/js"],
["md/nodejs", "14.13.1"],
];

it("should response basic node default user agent", async () => {
const userAgent = await defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" })();
expect(userAgent).toContainEqual(["aws-sdk-js", "0.1.0"]);
expect(userAgent).toContainEqual(["api/s3", "0.1.0"]);
expect(userAgent).toContainEqual(["os/darwin", "19.6.0"]);
expect(userAgent).toContainEqual(["lang/js"]);
validateUserAgent(userAgent, basicUserAgent);
});

it("should skip api version if service id is not supplied", async () => {
const userAgent = await defaultUserAgent({ serviceId: undefined, clientVersion: "0.1.0" })();
expect(userAgent).not.toContainEqual(["api/s3", "0.1.0"]);
validateUserAgent(
userAgent,
basicUserAgent.filter((pair) => pair[0] !== "api/s3")
);
});

it("should add AWS_EXECUTION_ENV", async () => {
//@ts-ignore mock environmental variables
mockEnv.AWS_EXECUTION_ENV = "lambda";
const userAgent = await defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" })();
expect(userAgent).toContainEqual(["exec-env/lambda"]);
const expectedUserAgent: UserAgent = [...basicUserAgent, ["exec-env/lambda"]];
validateUserAgent(userAgent, expectedUserAgent);
//@ts-ignore mock environmental variables
delete mockEnv.AWS_EXECUTION_ENV;
});

it("should load app id if available", async () => {
mockAppIdLoader.mockClear();
const appId = "appId12345";
mockAppIdLoader.mockResolvedValue(appId);
const userAgent = await defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" })();
expect(userAgent).toContainEqual([`app/${appId}`]);
const expectedUserAgent: UserAgent = [...basicUserAgent, [`app/${appId}`]];
validateUserAgent(userAgent, expectedUserAgent);
});

it("should memoize app id", async () => {
mockAppIdLoader.mockClear();
const appId = "appId12345";
mockAppIdLoader.mockResolvedValue(appId);
const userAgentProvider = defaultUserAgent({ serviceId: "s3", clientVersion: "0.1.0" });
const expectedUserAgent: UserAgent = [...basicUserAgent, [`app/${appId}`]];
validateUserAgent(await userAgentProvider(), expectedUserAgent);
validateUserAgent(await userAgentProvider(), expectedUserAgent);
expect(mockAppIdLoader).toBeCalledTimes(1);
});
});
19 changes: 10 additions & 9 deletions packages/util-user-agent-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ interface DefaultUserAgentOptions {
/**
* Collect metrics from runtime to put into user agent.
*/
export const defaultUserAgent = ({
serviceId,
clientVersion,
}: DefaultUserAgentOptions): Provider<UserAgent> => async () => {
export const defaultUserAgent = ({ serviceId, clientVersion }: DefaultUserAgentOptions): Provider<UserAgent> => {
const sections: UserAgent = [
// sdk-metadata
["aws-sdk-js", clientVersion],
Expand All @@ -40,14 +37,18 @@ export const defaultUserAgent = ({
sections.push([`exec-env/${env.AWS_EXECUTION_ENV}`]);
}

const appId = await loadConfig<string | undefined>({
const appIdPromise = loadConfig<string | undefined>({
environmentVariableSelector: (env) => env[UA_APP_ID_ENV_NAME],
configFileSelector: (profile) => profile[UA_APP_ID_INI_NAME],
default: undefined,
})();
if (appId) {
sections.push([`app/${appId}`]);
}

return sections;
let resolvedUserAgent: UserAgent | undefined = undefined;
return async () => {
if (!resolvedUserAgent) {
const appId = await appIdPromise;
resolvedUserAgent = appId ? [...sections, [`app/${appId}`]] : [...sections];
}
return resolvedUserAgent;
};
};