-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from andrewlimcj/feature/cloud-aws-s3
Add feature toggle implementation for AWS S3
- Loading branch information
Showing
4 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,6 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# MacOS | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))); | ||
} | ||
}); | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters