connect-dynamodb
Advanced tools
Comparing version 1.0.11 to 1.0.12
@@ -0,1 +1,6 @@ | ||
1.0.12 / 2017-07-03 | ||
================== | ||
* Implemented touch as requested on #23 and changed the expires field to work on seconds units instead of millis to address #39. This way to use TTL we just need to enable it on the table and select the 'expires' field. (https://github.com/ca98am79/connect-dynamodb/pull/43) | ||
1.0.11 / 2017-03-14 | ||
@@ -2,0 +7,0 @@ ================== |
@@ -63,3 +63,3 @@ /*! | ||
this.table = options.table || 'sessions'; | ||
this.reapInterval = options.reapInterval || (10 * 60 * 1000); | ||
this.reapInterval = options.reapInterval || 0; | ||
if (this.reapInterval > 0) { | ||
@@ -110,3 +110,3 @@ this._reap = setInterval(this.reap.bind(this), this.reapInterval); | ||
sid = this.prefix + sid; | ||
var now = +new Date; | ||
var now = Math.floor(Date.now() / 1000); | ||
@@ -153,3 +153,3 @@ this.client.getItem({ | ||
sid = this.prefix + sid; | ||
var expires = typeof sess.cookie.maxAge === 'number' ? (+new Date()) + sess.cookie.maxAge : (+new Date()) + oneDayInMilliseconds; | ||
var expires = this.getExpiresValue(sess); | ||
sess = JSON.stringify(sess); | ||
@@ -185,3 +185,3 @@ | ||
DynamoDBStore.prototype.reap = function (fn) { | ||
var now = +new Date; | ||
var now = Math.floor(Date.now() / 1000); | ||
var options = { | ||
@@ -249,3 +249,41 @@ endkey: '[' + now + ',{}]' | ||
/** | ||
* Calculates the expire value based on the configuration. | ||
* @param {Object} sess Session object. | ||
* @return {Integer} The expire on timestamp. | ||
*/ | ||
DynamoDBStore.prototype.getExpiresValue = function (sess) { | ||
var expires = typeof sess.cookie.maxAge === 'number' ? (+new Date()) + sess.cookie.maxAge : (+new Date()) + oneDayInMilliseconds; | ||
return Math.floor(expires / 1000); | ||
} | ||
/** | ||
* Touches the session row to update it's expire value. | ||
* @param {String} sid Session id. | ||
* @param {Object} sess Session object. | ||
* @param {Function} fn Callback. | ||
*/ | ||
DynamoDBStore.prototype.touch = function (sid, sess, fn) { | ||
sid = this.prefix + sid; | ||
var expires = this.getExpiresValue(sess); | ||
var params = { | ||
TableName: this.table, | ||
UpdateExpression: "set expires = :e", | ||
ExpressionAttributeValues:{ | ||
":e": { | ||
N: JSON.stringify(expires) | ||
} | ||
}, | ||
ReturnValues:"UPDATED_NEW" | ||
}; | ||
params.Key = {}; | ||
params.Key[this.hashKey] = { | ||
'S': sid | ||
} | ||
this.client.updateItem(params, fn || function () {}); | ||
}; | ||
/** | ||
* Clear intervals | ||
@@ -252,0 +290,0 @@ * |
{ | ||
"name": "connect-dynamodb", | ||
"description": "DynamoDB session store for Connect", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"author": "Mike Carson <ca98am79@gmail.com> (http://ca98am79.com)", | ||
"main": "./index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
@@ -12,4 +15,5 @@ "aws-sdk": "*", | ||
"devDependencies": { | ||
"should": "*", | ||
"mocha": "*" | ||
"express-session": "*", | ||
"mocha": "*", | ||
"should": "*" | ||
}, | ||
@@ -16,0 +20,0 @@ "engines": { |
@@ -14,6 +14,6 @@ # Connect DynamoDB | ||
Rational defaults are set but can be overridden in the options object. Credentials and configuration are automatically loaded from [environment variables](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html) or [shared credentials](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html) but may optionally be passed through a JSON file or object. The client attribute is necessary for use with [DynamoDB Local](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) but can be left out if using DynamoDB with your AWS account. | ||
Rational defaults are set but can be overridden in the options object. Credentials and configuration are automatically loaded from [environment variables](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html) or [shared credentials](http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html) but may optionally be passed through a JSON file or object. The client attribute is necessary for use with [DynamoDB Local](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html) but can be left out if using DynamoDB with your AWS account. To use [DynamoDB TTL](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html), enable it on the table and select the `expires` field. | ||
- One of the following if not using environment variables or shared credentials: | ||
- `AWSConfigPath` Optional path to a [file containing your AWS credentials and configuration](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html#Credentials_from_Disk) | ||
- `AWSConfigPath` Optional path to a [file containing your AWS credentials and configuration](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html#Credentials_from_Disk) | ||
- `AWSConfigJSON` Optional [JSON object containing your AWS credentials and configuration](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html) | ||
@@ -25,3 +25,3 @@ - `client` Optional AWS DynamoDB object from `new AWS.DynamoDB()` | ||
- `prefix` Optional key prefix (defaults to "sess") | ||
- `reapInterval` Optional session expiration in milliseconds (defaults to 10 minutes) | ||
- `reapInterval` Legacy session expiration cleanup in milliseconds. Should instead enable [DynamoDB TTL](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) and select the `expires` field. | ||
@@ -47,5 +47,2 @@ ## Usage | ||
// Optional clean up interval, defaults to 600000 (10 minutes) | ||
reapInterval: 86400000, // 1 day | ||
// Optional ProvisionedThroughput params, defaults to 5 | ||
@@ -79,18 +76,24 @@ readCapacityUnits: 25, | ||
## Contributors | ||
## Testing | ||
Some people that have added features and fixed bugs in `connect-dynamodb` other than me. | ||
If you want to run the tests locally and have the AWS environment variables setup you can run the command: | ||
* [Eric Abouaf](https://github.com/neyric) | ||
* [James Bloomer](https://github.com/jamesbloomer) | ||
* [Roy Lines](https://github.com/roylines) | ||
* [B2M Development](https://github.com/b2mdevelopment) | ||
* [Kristian Ačkar](https://github.com/kristian-ackar) | ||
* [doapp-ryanp](https://github.com/doapp-ryanp) | ||
* [Bryce Larson](https://github.com/bryce-larson) | ||
* [Etienne Adriaenssen](https://github.com/etiennea) | ||
* [Michael Irigoyen](https://github.com/goyney) | ||
``` | ||
npm test | ||
``` | ||
Thanks! | ||
You can also run it locally by running the following two scripts in separate terminals: | ||
``` | ||
docker run -it --rm \ | ||
--name=dynamodb-test \ | ||
-p 127.0.0.1:8000:8000 \ | ||
deangiberson/aws-dynamodb-local | ||
``` | ||
``` | ||
export AWS_CONFIG_JSON='{"endpoint": "http://127.0.0.1:8000", "region": "us-east-1", "accessKeyId": "accesskey", "secretAccessKey": "secretaccesskey"}' | ||
npm test | ||
``` | ||
## License | ||
@@ -102,2 +105,2 @@ | ||
I made this in my spare time, so if you find it useful you can donate at my BTC address: `1Mhdjrx4ioComkn2kZX4cJVV2rXV1VPNGM`. Thank you very much! | ||
I made this in my spare time, so if you find it useful you can donate at my BTC address: `15rmktUUfB8hHh5u57qzsihVPBdZmMePZB`. Thank you very much! |
var should = require('should'), | ||
connect = require('connect'), | ||
DynamoDBStore = require(__dirname + '/../lib/connect-dynamodb.js')(connect); | ||
session = require('express-session'), | ||
DynamoDBStore = require(__dirname + '/../lib/connect-dynamodb.js')({session: session}); | ||
var client; | ||
if (process.env.AWS_CONFIG_JSON) { | ||
var AWS = require('aws-sdk'); | ||
var config = JSON.parse(process.env.AWS_CONFIG_JSON); | ||
client = new AWS.DynamoDB(config); | ||
} | ||
describe('DynamoDBStore', function () { | ||
@@ -9,2 +16,3 @@ describe('Instantiation', function () { | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
@@ -18,4 +26,6 @@ }); | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
}); | ||
store.set('123', { | ||
@@ -35,4 +45,5 @@ cookie: { | ||
describe('Getting', function () { | ||
before(function () { | ||
before(function (done) { | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
@@ -45,3 +56,3 @@ }); | ||
name: 'tj' | ||
}, function () {}); | ||
}, done); | ||
}); | ||
@@ -51,2 +62,3 @@ | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
@@ -66,5 +78,42 @@ }); | ||
}); | ||
describe('Touching', function () { | ||
var sess = { | ||
cookie: { | ||
maxAge: 2000 | ||
}, | ||
name: 'tj' | ||
}; | ||
var maxAge = null; | ||
before(function (done) { | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
}); | ||
maxAge = (Math.floor((Date.now() + 2000) / 1000) ); | ||
store.set('1234', sess, done); | ||
}); | ||
it('should touch data correctly', function (done) { | ||
this.timeout(4000); | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
}); | ||
setTimeout(function() { | ||
store.touch('1234', sess, function (err, res) { | ||
if (err) throw err; | ||
var expires = res.Attributes.expires.N; | ||
expires.should.be.above(maxAge); | ||
(expires - maxAge).should.be.aboveOrEqual(1); | ||
done(); | ||
}); | ||
}, 1510); | ||
}); | ||
}); | ||
describe('Destroying', function () { | ||
before(function () { | ||
before(function (done) { | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
@@ -77,3 +126,3 @@ }); | ||
name: 'tj' | ||
}, function () {}); | ||
}, done); | ||
}); | ||
@@ -83,2 +132,3 @@ | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
@@ -102,2 +152,3 @@ }); | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
@@ -114,3 +165,5 @@ }); | ||
it('should reap data correctly', function (done) { | ||
this.timeout(5000); // increased timeout for local dynamo | ||
var store = new DynamoDBStore({ | ||
client: client, | ||
table: 'sessions-test' | ||
@@ -131,2 +184,2 @@ }); | ||
}); | ||
}); | ||
}); |
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
22755
415
102
3
3