@voodoo.io/aws-utils
Advanced tools
Comparing version
module.exports = { | ||
s3: require('./src/S3Tools') | ||
s3: require('./src/S3Tools'), | ||
dynamo: require('./src/DynamoTools'), | ||
secretManager: require('./src/SecretManager') | ||
}; |
{ | ||
"name": "@voodoo.io/aws-utils", | ||
"version": "0.1.0", | ||
"description": "S3 utils", | ||
"version": "1.0.0", | ||
"description": "AWS utils methods - using homemade retry system", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "NODE_ENV=test ./node_modules/.bin/jest", | ||
"lint": "./node_modules/.bin/eslint -c ./.eslintrc ./" | ||
}, | ||
"engines": { | ||
"node": ">=7.6.0" | ||
}, | ||
"keywords": [ | ||
"AWS", | ||
"utils", | ||
"retry" | ||
], | ||
"license": "MIT", | ||
"repository": { | ||
@@ -14,3 +24,2 @@ "type": "git", | ||
"author": "vincentvallet@voodoo.io", | ||
"license": "ISC", | ||
"bugs": { | ||
@@ -21,7 +30,11 @@ "url": "https://github.com/VoodooTeam/aws-utils/issues" | ||
"dependencies": { | ||
"async-await-retry": "^1.0.1" | ||
"async-await-retry": "1.0.1", | ||
"aws-sdk": "2.437.0" | ||
}, | ||
"devDependencies": { | ||
"eslint": "5.15.1" | ||
"aws-sdk-mock": "^4.4.0", | ||
"eslint": "5.16.0", | ||
"jest": "^24.7.1", | ||
"pino": "^5.12.2" | ||
} | ||
} |
158
README.md
@@ -1,1 +0,157 @@ | ||
# aws-utils | ||
# aws-utils | ||
<div align="center"> | ||
<b>AWS utils with built-in retry system</b><br/> | ||
<br/><br/> | ||
<a href="https://badge.fury.io/js/%40voodoo.io%2Faws-utils.svg"> | ||
<img src="https://badge.fury.io/js/%40voodoo.io%2Faws-utils.svg" alt="npm version" height="18"> | ||
</a> | ||
</div> | ||
# Purpose | ||
Simple wrapper around aws-sdk to make it easier to use. | ||
# Compatibility | ||
**/!\ This module use async/await syntax, this is why you must have node 7.6+.** | ||
Supported and tested : >= 7.6 | ||
| Version | Supported | Tested | | ||
| ------------- |:-------------:|:--------------:| | ||
| 10.x | yes | yes | | ||
| 9.x | yes | yes | | ||
| 8.x | yes | yes | | ||
| >= 7.6 | yes | yes | | ||
# Installation | ||
```console | ||
$ npm install @voodoo.io/aws-utils --save | ||
``` | ||
# Usage | ||
## Dynamo tools | ||
### Configure dynamo tools | ||
```javascript | ||
const awsUtils = require('@voodoo.io/aws-utils').dynamo; | ||
const aws = require('aws-sdk'); | ||
aws.config.update({region: 'eu-west-1'}); | ||
const dynamoCli = new aws.DynamoDB.DocumentClient(); | ||
const dynamoTools = new awsUtils(dynamoCli) | ||
``` | ||
### Query | ||
```javascript | ||
const res = await dynamoTools.queryHashKey('myTable', 'key', 'value'); | ||
``` | ||
### Scan | ||
```javascript | ||
const res = await dynamoTools.scan('myTable'); | ||
``` | ||
### Put item | ||
```javascript | ||
const res = await dynamoTools.putItem('myTable', {'key': 'value'}); | ||
``` | ||
#### queryHashKey(dynamoTable, hashKeyName, hashKeyValue, [exclusiveStartKey]) | ||
* `dynamoTable` : table's name | ||
* `hashKeyName` : hashkey's name | ||
* `hashKeyValue` : hashkey's value | ||
* `exclusiveStartKey` : (optional) start search at a specific key | ||
#### putItem(dynamoTable, item) | ||
* `dynamoTable` : table's name | ||
* `item` : item to insert | ||
#### scan(dynamoTable, [hashKeyName], [hashKeyValue], [exclusiveStartKey]) | ||
* `dynamoTable` : table's name | ||
* `hashKeyName` : (optional) hashkey's name | ||
* `hashKeyValue` : (optional) hashkey's value | ||
* `exclusiveStartKey` : (optional) start search at a specific key | ||
If no hashkey is provided it returns the full table. | ||
## Secret Manager tools | ||
### Configure secret manager tools | ||
```javascript | ||
const awsUtils = require('@voodoo.io/aws-utils').secretManager; | ||
const aws = require('aws-sdk'); | ||
aws.config.update({region: 'eu-west-1'}); | ||
const cli = new aws.SecretsManager({}); | ||
const secretManagerTools = new awsUtils(cli); | ||
``` | ||
### getSecretValue | ||
```javascript | ||
const res = await secretManagerTools.getSecretValue('secret'); | ||
``` | ||
#### getSecretValue(secret) | ||
* `secret` : secret's id | ||
## S3 tools | ||
### Configure S3 tools | ||
```javascript | ||
const awsUtils = require('@voodoo.io/aws-utils').s3; | ||
const aws = require('aws-sdk'); | ||
aws.config.update({region: 'eu-west-1'}); | ||
const cli = new aws.S3(); | ||
const s3Tools = new awsUtils(cli); | ||
``` | ||
### getObject | ||
```javascript | ||
const res = await s3Tools.getObject('bucket', 'key'); | ||
``` | ||
#### getObject(bucket, key) | ||
* `bucket` : bucket's name | ||
* `key` : path to the ressource (/path/file.json) | ||
### putJsonObject | ||
```javascript | ||
const res = await s3Tools.putJsonObject('bucket', 'key', {"key": "value"}); | ||
``` | ||
#### putJsonObject(bucket, key, item) | ||
* `bucket` : bucket's name | ||
* `key` : path to the ressource (/path/file.json) | ||
* `item` : json object to save on S3 | ||
# Test | ||
```console | ||
$ npm test | ||
``` | ||
Coverage report can be found in coverage/. |
@@ -6,4 +6,4 @@ /** | ||
const utils = require('./utils'), | ||
zlib = require('zlib'); | ||
const utils = require('./utils'); | ||
const zlib = require('zlib'); | ||
@@ -41,3 +41,3 @@ const BAD_PARAM = "S3_TOOLS_BAD_PARAM"; | ||
const data = await utils.retry(this.cli.getObject.bind(this.cli), [param], true, this.retryMax); | ||
const response = await formatGetObjectResponse(data, returnType, gzip); | ||
const response = await this._formatGetObjectResponse(data, returnType, gzip); | ||
return resolve(response); | ||
@@ -56,3 +56,3 @@ } catch (err) { | ||
const response = formatGetObjectResponse(data, returnType, gzip); | ||
const response = this._formatGetObjectResponse(data, returnType, gzip); | ||
return resolve(response); | ||
@@ -72,3 +72,3 @@ }) | ||
return new Promise((resolve, reject) => { | ||
if (typeof bucket !== "string" || typeof key !== "string") return reject(new Error(`${BAD_PARAM}_PutObject`)); | ||
if (typeof bucket !== "string" || typeof key !== "string" || typeof json !== "object") return reject(new Error(`${BAD_PARAM}_PutObject`)); | ||
const param = { | ||
@@ -97,42 +97,42 @@ Bucket: bucket, | ||
} | ||
} | ||
/** | ||
* Format the S3 response for GetObject | ||
* | ||
* @param {object} data - Response of S3 | ||
* @param {string} returnType - Type of responsd waited | ||
* @param {boolean} gzip - Gzip or not | ||
*/ | ||
function formatGetObjectResponse(data, returnType, gzip) { | ||
return new Promise((resolve, reject) => { | ||
//Check wich return type is ask to returning well formatted data | ||
switch (returnType) { | ||
case "buffer": | ||
if (!gzip) return resolve(data.Body); | ||
zlib.gunzip(data.Body, (err, data) => { | ||
if (err) return reject(err); | ||
/** | ||
* Format the S3 response for GetObject | ||
* | ||
* @param {object} data - Response of S3 | ||
* @param {string} returnType - Type of responsd waited | ||
* @param {boolean} gzip - Gzip or not | ||
*/ | ||
_formatGetObjectResponse(data, returnType, gzip) { | ||
return new Promise((resolve, reject) => { | ||
//Check wich return type is ask to returning well formatted data | ||
switch (returnType) { | ||
case "buffer": | ||
if (!gzip) return resolve(data.Body); | ||
zlib.gunzip(data.Body, (err, data) => { | ||
if (err) return reject(err); | ||
return resolve(data); | ||
}); | ||
break; | ||
case "string": | ||
if (!gzip) return resolve(data.Body.toString()); | ||
zlib.gunzip(data.Body, (err, data) => { | ||
if (err) return reject(err); | ||
return resolve(data.toString()); | ||
}); | ||
break; | ||
case "object": | ||
if (!gzip) return resolve(JSON.parse(data.Body.toString())); | ||
zlib.gunzip(data.Body, (err, data) => { | ||
if (err) return reject(err); | ||
return resolve(JSON.parse(data.toString())); | ||
}); | ||
break; | ||
default: | ||
return resolve(data); | ||
}); | ||
break; | ||
case "string": | ||
if (!gzip) return resolve(data.Body.toString()); | ||
zlib.gunzip(data.Body, (err, data) => { | ||
if (err) return reject(err); | ||
return resolve(data.toString()); | ||
}); | ||
break; | ||
case "object": | ||
if (!gzip) return resolve(JSON.parse(data.Body.toString())); | ||
zlib.gunzip(data.Body, (err, data) => { | ||
if (err) return reject(err); | ||
return resolve(JSON.parse(data.toString())); | ||
}); | ||
break; | ||
default: | ||
return resolve(data); | ||
} | ||
}) | ||
} | ||
}); | ||
} | ||
} | ||
module.exports = S3Tools; |
@@ -18,2 +18,8 @@ /** | ||
exports.retry = async (fn, args, cb, retriesMax = 3, interval = 200, exponential = true) => { | ||
if( process.env.NODE_ENV === 'test' ) { | ||
interval = 50; | ||
exponential = false; | ||
} | ||
return await retry(fn, args, { | ||
@@ -20,0 +26,0 @@ retriesMax: retriesMax, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
41851
553.31%13
116.67%944
542.18%0
-100%157
15600%2
100%4
300%1
Infinity%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
Updated