Skip to content

Commit a4a8ee8

Browse files
committed
Add node region providers
1 parent b8a89fe commit a4a8ee8

File tree

8 files changed

+141
-0
lines changed

8 files changed

+141
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules/
2+
*.js
3+
*.js.map
4+
*.d.ts
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {fromEnv, ENV_REGION} from '../lib/fromEnv';
2+
3+
describe('fromEnv', () => {
4+
const ENV_CUSTOM = 'AWS_DEFAULT_REGION';
5+
const envRegion = process.env[ENV_REGION];
6+
const customEnv = process.env[ENV_CUSTOM];
7+
8+
beforeEach(() => {
9+
delete process.env[ENV_REGION];
10+
delete process.env[ENV_CUSTOM];
11+
});
12+
13+
afterAll(() => {
14+
process.env[ENV_CUSTOM] = customEnv;
15+
process.env[ENV_REGION] = envRegion;
16+
});
17+
18+
it(`should read from the ${ENV_REGION} environment variable`, async () => {
19+
process.env[ENV_REGION] = 'us-north-12';
20+
expect(await fromEnv()()).toBe(process.env[ENV_REGION]);
21+
});
22+
23+
it(`should reject the promise is ${ENV_REGION} is not set`, async () => {
24+
await expect(fromEnv()()).rejects;
25+
});
26+
27+
it('should allow specifying custom environment variables', async () => {
28+
process.env[ENV_CUSTOM] = 'eu-central-35';
29+
expect(process.env[ENV_REGION]).toBeUndefined();
30+
expect(await fromEnv(ENV_CUSTOM)()).toBe(process.env[ENV_CUSTOM]);
31+
});
32+
});

packages/region-provider-node/__tests__/fromSharedConfigFiles.ts

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './lib/fromEnv';
2+
export * from './lib/fromSharedConfigFiles';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Provider} from '@aws/types';
2+
3+
export const ENV_REGION = 'AWS_REGION';
4+
5+
export function fromEnv(variableName: string = ENV_REGION): Provider<string> {
6+
return () => {
7+
const envRegion = process.env[variableName];
8+
if (envRegion) {
9+
return Promise.resolve(envRegion);
10+
}
11+
12+
return Promise.reject(
13+
`No value defined for the ${variableName} environment variable`
14+
);
15+
};
16+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {
2+
loadSharedConfigFiles,
3+
SharedConfigFiles,
4+
SharedConfigInit as BaseSharedConfigInit,
5+
} from '@aws/shared-ini-file-loader';
6+
import {Provider} from '@aws/types';
7+
8+
const DEFAULT_PROFILE = 'default';
9+
export const ENV_PROFILE = 'AWS_PROFILE';
10+
11+
export interface SharedConfigInit extends BaseSharedConfigInit {
12+
/**
13+
* The configuration profile to use.
14+
*/
15+
profile?: string;
16+
17+
/**
18+
* A promise that will be resolved with loaded and parsed credentials files.
19+
* Used to avoid loading shared config files multiple times.
20+
*/
21+
loadedConfig?: Promise<SharedConfigFiles>;
22+
}
23+
24+
export function fromSharedConfigFiles(
25+
init: SharedConfigInit = {}
26+
): Provider<string> {
27+
return () => {
28+
const {
29+
loadedConfig = loadSharedConfigFiles(init),
30+
profile = process.env[ENV_PROFILE] || DEFAULT_PROFILE
31+
} = init;
32+
33+
return loadedConfig.then(({configFile, credentialsFile}) => {
34+
for (let file of [credentialsFile, configFile]) {
35+
const {region} = file[profile] || <any>{};
36+
if (typeof region === 'string') {
37+
return region;
38+
}
39+
}
40+
41+
throw new Error(
42+
`No region found for profile ${profile} in SDK configuration files`
43+
);
44+
});
45+
}
46+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@aws/region-provider-node",
3+
"version": "0.0.1",
4+
"private": true,
5+
"description": "AWS region provider for Node.JS. Will determine the default region to use for AWS clients by checking known environment variables and the EC2 instance metadata service",
6+
"main": "index.js",
7+
"scripts": {
8+
"prepublishOnly": "tsc",
9+
"pretest": "tsc",
10+
"test": "jest"
11+
},
12+
"keywords": [
13+
"aws",
14+
"credentials"
15+
],
16+
"author": "[email protected]",
17+
"license": "Apache-2.0",
18+
"dependencies": {
19+
"@aws/region-provider": "^0.0.1",
20+
"@aws/shared-ini-file-loader": "^0.0.1",
21+
"@aws/types": "^0.0.1"
22+
},
23+
"devDependencies": {
24+
"@types/jest": "^20.0.2",
25+
"jest": "^20.0.4",
26+
"typescript": "^2.3"
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"declaration": true,
6+
"strict": true,
7+
"sourceMap": true,
8+
"lib": [
9+
"es5",
10+
"es2015.promise"
11+
]
12+
}
13+
}

0 commit comments

Comments
 (0)