Skip to content

Commit

Permalink
Merge pull request #1 from andrewlimcj/feature/cloud-aws-s3
Browse files Browse the repository at this point in the history
Add feature toggle implementation for AWS S3
  • Loading branch information
andrewlimcj authored Mar 24, 2020
2 parents 3706249 + dd2a450 commit 7db70c2
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

# MacOS
.DS_Store
64 changes: 64 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const AWS = require('aws-sdk');
const s3 = new AWS.S3();

const CONSOLE_LOG_PREFIX = '[cloud-feature-toggles]'

module.exports = (options) => {
init(options);

isEnabled = async (featureToggle) => {
if (options.aws.s3) {
try {
const featureToggleConfig = await getObjectS3(options.aws.s3.bucket, featureToggle);

return featureToggleConfig.isEnabled;
} catch(err) {
console.warn(`${CONSOLE_LOG_PREFIX} Error in retrieving config from AWS S3 - ` + err);
console.warn(`${CONSOLE_LOG_PREFIX} Default to return false`);

return false;
}
}

return false;
};

return {
isEnabled
};
}

const init = (options) => {
if (typeof options === 'undefined' || options === null) {
throw new Error('Missing options parameter');
}

if (!Object.keys(options).length) {
throw new Error('Missing configuration in options parameter');
}

if (options.aws) {
if (!options.aws.region || options.aws.region === null) {
throw new Error('Missing region in AWS configuration');
}

AWS.config.update({ region: options.aws.region });
}
}

const getObjectS3 = (bucket, key) => {
return new Promise((resolve, reject) => {
const params = {
Bucket: bucket,
Key: key
};

s3.getObject(params, (err, data) => {
if (err) {
reject(new Error(err.message));
} else {
resolve(JSON.parse(data.Body.toString('utf-8')));
}
});
});
}
102 changes: 102 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/andrewlimcj/cloud-feature-toggles.git"
"url": "git+https://github.com/andrewlimcj/cloud-feature-toggles"
},
"keywords": [
"cloud",
"feature",
"toggles",
"toggle",
"feature toggle",
"feature toggles",
"AWS"
],
"author": "andrewlimcj",
"license": "MIT",
"bugs": {
"url": "https://github.com/andrewlimcj/cloud-feature-toggles/issues"
},
"homepage": "https://github.com/andrewlimcj/cloud-feature-toggles#readme"
"homepage": "https://github.com/andrewlimcj/cloud-feature-toggles#readme",
"dependencies": {
"aws-sdk": "^2.644.0"
}
}

0 comments on commit 7db70c2

Please sign in to comment.