Skip to content

Commit

Permalink
chore(middleware-bucket-endpoint): use booleanSelector from node-conf…
Browse files Browse the repository at this point in the history
…ig-provider (#2946)
  • Loading branch information
trivikr authored Nov 1, 2021
1 parent 01ff860 commit 359c78c
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 122 deletions.
2 changes: 1 addition & 1 deletion packages/middleware-bucket-endpoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/protocol-http": "3.38.0",
"@aws-sdk/node-config-provider": "3.39.0",
"@aws-sdk/types": "3.38.0",
"@aws-sdk/util-arn-parser": "3.37.0",
"tslib": "^2.3.0"
},
"devDependencies": {
"@aws-sdk/middleware-stack": "3.38.0",
"@aws-sdk/node-config-provider": "3.39.0",
"@types/jest": "^26.0.4",
"jest": "^26.1.0",
"typescript": "~4.3.5"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";

import {
NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS,
NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME,
NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME,
} from "./NodeDisableMultiregionAccessPointConfigOptions";

jest.mock("@aws-sdk/node-config-provider");

describe("NODE_USE_ARN_REGION_CONFIG_OPTIONS", () => {
afterEach(() => {
jest.clearAllMocks();
});

const test = (func: Function, obj: { [key: string]: string }, key: string, type: SelectorType) => {
it.each([true, false, undefined])("returns %s", (output) => {
(booleanSelector as jest.Mock).mockReturnValueOnce(output);
expect(func(obj)).toEqual(output);
expect(booleanSelector).toBeCalledWith(obj, key, type);
});

it("throws error", () => {
const mockError = new Error("error");
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
throw mockError;
});
expect(() => {
func(obj);
}).toThrow(mockError);
});
};

describe("calls booleanSelector for environmentVariableSelector", () => {
const env: { [NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME]: any } = {} as any;
const { environmentVariableSelector } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS;
test(environmentVariableSelector, env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME, SelectorType.ENV);
});

describe("calls booleanSelector for configFileSelector", () => {
const profileContent: { [NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME]: any } = {} as any;
const { configFileSelector } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS;
test(configFileSelector, profileContent, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME, SelectorType.CONFIG);
});

it("returns false for default", () => {
const { default: defaultValue } = NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS;
expect(defaultValue).toEqual(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider";
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";

export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME = "AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS";
export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME = "s3_disable_multiregion_access_points";

export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS: LoadedConfigSelectors<boolean> = {
environmentVariableSelector: (env: NodeJS.ProcessEnv) =>
booleanSelector(env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME, SelectorType.ENV),
configFileSelector: (profile) =>
booleanSelector(profile, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME, SelectorType.CONFIG),
default: false,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";

import {
NODE_USE_ARN_REGION_CONFIG_OPTIONS,
NODE_USE_ARN_REGION_ENV_NAME,
NODE_USE_ARN_REGION_INI_NAME,
} from "./NodeUseArnRegionConfigOptions";

jest.mock("@aws-sdk/node-config-provider");

describe("NODE_USE_ARN_REGION_CONFIG_OPTIONS", () => {
afterEach(() => {
jest.clearAllMocks();
});

const test = (func: Function, obj: { [key: string]: string }, key: string, type: SelectorType) => {
it.each([true, false, undefined])("returns %s", (output) => {
(booleanSelector as jest.Mock).mockReturnValueOnce(output);
expect(func(obj)).toEqual(output);
expect(booleanSelector).toBeCalledWith(obj, key, type);
});

it("throws error", () => {
const mockError = new Error("error");
(booleanSelector as jest.Mock).mockImplementationOnce(() => {
throw mockError;
});
expect(() => {
func(obj);
}).toThrow(mockError);
});
};

describe("calls booleanSelector for environmentVariableSelector", () => {
const env: { [NODE_USE_ARN_REGION_ENV_NAME]: any } = {} as any;
const { environmentVariableSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS;
test(environmentVariableSelector, env, NODE_USE_ARN_REGION_ENV_NAME, SelectorType.ENV);
});

describe("calls booleanSelector for configFileSelector", () => {
const profileContent: { [NODE_USE_ARN_REGION_INI_NAME]: any } = {} as any;
const { configFileSelector } = NODE_USE_ARN_REGION_CONFIG_OPTIONS;
test(configFileSelector, profileContent, NODE_USE_ARN_REGION_INI_NAME, SelectorType.CONFIG);
});

it("returns false for default", () => {
const { default: defaultValue } = NODE_USE_ARN_REGION_CONFIG_OPTIONS;
expect(defaultValue).toEqual(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider";
import { booleanSelector, SelectorType } from "@aws-sdk/node-config-provider";

export const NODE_USE_ARN_REGION_ENV_NAME = "AWS_S3_USE_ARN_REGION";
export const NODE_USE_ARN_REGION_INI_NAME = "s3_use_arn_region";

/**
* Config to load useArnRegion from environment variables and shared INI files
*
* @api private
*/
export const NODE_USE_ARN_REGION_CONFIG_OPTIONS: LoadedConfigSelectors<boolean> = {
environmentVariableSelector: (env: NodeJS.ProcessEnv) =>
booleanSelector(env, NODE_USE_ARN_REGION_ENV_NAME, SelectorType.ENV),
configFileSelector: (profile) => booleanSelector(profile, NODE_USE_ARN_REGION_INI_NAME, SelectorType.CONFIG),
default: false,
};
68 changes: 0 additions & 68 deletions packages/middleware-bucket-endpoint/src/configuration.spec.ts

This file was deleted.

53 changes: 0 additions & 53 deletions packages/middleware-bucket-endpoint/src/configurations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LoadedConfigSelectors } from "@aws-sdk/node-config-provider";
import { Provider, RegionInfoProvider } from "@aws-sdk/types";

export interface BucketEndpointInputConfig {
Expand Down Expand Up @@ -92,55 +91,3 @@ export function resolveBucketEndpointConfig<T>(
: () => Promise.resolve(disableMultiregionAccessPoints),
};
}

export const NODE_USE_ARN_REGION_ENV_NAME = "AWS_S3_USE_ARN_REGION";
export const NODE_USE_ARN_REGION_INI_NAME = "s3_use_arn_region";

export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME = "AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS";
export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME = "s3_disable_multiregion_access_points";

/**
* Config to load useArnRegion from environment variables and shared INI files
*
* @api private
*/
export const NODE_USE_ARN_REGION_CONFIG_OPTIONS: LoadedConfigSelectors<boolean> = {
environmentVariableSelector: (env: NodeJS.ProcessEnv) => {
if (!Object.prototype.hasOwnProperty.call(env, NODE_USE_ARN_REGION_ENV_NAME)) return undefined;
if (env[NODE_USE_ARN_REGION_ENV_NAME] === "true") return true;
if (env[NODE_USE_ARN_REGION_ENV_NAME] === "false") return false;
throw new Error(
`Cannot load env ${NODE_USE_ARN_REGION_ENV_NAME}. Expected "true" or "false", got ${env[NODE_USE_ARN_REGION_ENV_NAME]}.`
);
},
configFileSelector: (profile) => {
if (!Object.prototype.hasOwnProperty.call(profile, NODE_USE_ARN_REGION_INI_NAME)) return undefined;
if (profile[NODE_USE_ARN_REGION_INI_NAME] === "true") return true;
if (profile[NODE_USE_ARN_REGION_INI_NAME] === "false") return false;
throw new Error(
`Cannot load shared config entry ${NODE_USE_ARN_REGION_INI_NAME}. Expected "true" or "false", got ${profile[NODE_USE_ARN_REGION_INI_NAME]}.`
);
},
default: false,
};

export const NODE_DISABLE_MULTIREGION_ACCESS_POINT_CONFIG_OPTIONS: LoadedConfigSelectors<boolean> = {
environmentVariableSelector: (env: NodeJS.ProcessEnv) => {
if (!Object.prototype.hasOwnProperty.call(env, NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME)) return undefined;
if (env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME] === "true") return true;
if (env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME] === "false") return false;
throw new Error(
`Cannot load env ${NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME}. Expected "true" or "false", got ${env[NODE_DISABLE_MULTIREGION_ACCESS_POINT_ENV_NAME]}.`
);
},
configFileSelector: (profile) => {
if (!Object.prototype.hasOwnProperty.call(profile, NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME))
return undefined;
if (profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME] === "true") return true;
if (profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME] === "false") return false;
throw new Error(
`Cannot load shared config entry ${NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME}. Expected "true" or "false", got ${profile[NODE_DISABLE_MULTIREGION_ACCESS_POINT_INI_NAME]}.`
);
},
default: false,
};
2 changes: 2 additions & 0 deletions packages/middleware-bucket-endpoint/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./NodeDisableMultiregionAccessPointConfigOptions";
export * from "./NodeUseArnRegionConfigOptions";
export * from "./bucketEndpointMiddleware";
export * from "./bucketHostname";
export * from "./configurations";
Expand Down

0 comments on commit 359c78c

Please sign in to comment.