New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

connect-dynamodb

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-dynamodb - npm Package Compare versions

Comparing version 0.0.6 to 0.1.0

README.md

5

History.md

@@ -0,1 +1,6 @@

0.1.0 / 2013-01-25
==================
* Switch to use aws-sdk instead of dynode
0.0.6 / 2012-06-29

@@ -2,0 +7,0 @@ ==================

281

lib/connect-dynamodb.js
/*!
* Connect - DynamoDB
* Copyright(c) 2012 Mike Carson <ca98am79@gmail.com>
* Copyright(c) 2013 Mike Carson <ca98am79@gmail.com>
* MIT Licensed

@@ -11,3 +11,3 @@ */

var dynode = require('dynode');
var AWS = require('aws-sdk');

@@ -30,9 +30,9 @@ /**

/**
/**
* Connect's Store.
*/
var Store = connect.session.Store;
var Store = connect.session.Store;
/**
/**
* Initialize DynamoDBStore with the given `options`.

@@ -44,37 +44,52 @@ *

function DynamoDBStore(options) {
options = options || {};
Store.call(this, options);
this.prefix = null == options.prefix
? 'sess:'
: options.prefix;
this.client = options.client || new (dynode.Client)({
https: true,
accessKeyId: options.accessKeyId,
secretAccessKey: options.secretAccessKey
});
function DynamoDBStore(options) {
options = options || {};
Store.call(this, options);
this.prefix = null == options.prefix
? 'sess:'
: options.prefix;
this.table = options.table || 'sessions';
this.reapInterval = options.reapInterval || (10 * 60 * 1000);
if (this.reapInterval > 0) {
this._reap = setInterval(this.reap.bind(this), this.reapInterval);
}
if (options.client) {
this.client = options.client;
} else {
AWS.config.loadFromPath(options.AWSConfigPath);
this.client = new AWS.DynamoDB().client;
}
// check if table exists, otherwise create it
var client = this.client;
client.describeTable(options.table, function (error, info) {
if (error) {
client.createTable(options.table, console.log);
}
});
};
this.table = options.table || 'sessions';
this.reapInterval = options.reapInterval || (10 * 60 * 1000);
if (this.reapInterval > 0) {
this._reap = setInterval(this.reap.bind(this), this.reapInterval);
}
// check if sessions table exists, otherwise create it
var client = this.client;
client.describeTable({
TableName: options.table
}, function (error, info) {
if (error) {
client.createTable({
TableName: options.table,
KeySchema: {
HashKeyElement: {
AttributeName: 'id',
AttributeType: 'S'
}
},
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
}, console.log);
}
});
};
/**
/**
* Inherit from `Store`.
*/
DynamoDBStore.prototype.__proto__ = Store.prototype;
DynamoDBStore.prototype.__proto__ = Store.prototype;
/**
/**
* Attempt to fetch session by the given `sid`.

@@ -87,22 +102,29 @@ *

DynamoDBStore.prototype.get = function(sid, fn){
sid = this.prefix + sid;
var now = +new Date;
this.client.getItem(this.table, sid, function(err, data){
try {
if (!data) return fn(null, null);
else if (data.expires && now >= data.expires) {
fn(null, null);
} else {
var sess = data.sess.toString();
sess = JSON.parse(sess);
fn(null, sess);
}
} catch (err) {
fn(err);
}
}.bind(this));
};
DynamoDBStore.prototype.get = function(sid, fn){
sid = this.prefix + sid;
var now = +new Date;
this.client.getItem({
TableName : this.table,
Key : {
HashKeyElement: { 'S' : sid }
}
}, function(err, result){
try {
if (!result.Item) return fn(null, null);
else if (result.Item.expires && now >= result.Item.expires) {
fn(null, null);
} else {
var sess = result.Item.sess.S.toString();
sess = JSON.parse(sess);
fn(null, sess);
}
} catch (err) {
fn(err);
}
}.bind(this));
};
/**
/**
* Commit the given `sess` object associated with the given `sid`.

@@ -116,41 +138,22 @@ *

DynamoDBStore.prototype.set = function(sid, sess, fn){
sid = this.prefix + sid;
try {
this.client.getItem(this.table, sid, function(err, data){
var expires = typeof sess.cookie.maxAge === 'number'
? (+new Date()) + sess.cookie.maxAge
: (+new Date()) + oneDay;
sess = JSON.stringify(sess);
if (err) {
data = {
id: sid,
expires: expires,
type: 'connect-session',
sess: sess
};
this.client.putItem(this.table, data, fn);
} else {
if (data) {
data.expires = expires;
data.sess = sess;
delete data.id;
this.client.updateItem(this.table, sid, data, fn);
} else {
data = {
id: sid,
expires: expires,
type: 'connect-session',
sess: sess
};
this.client.putItem(this.table, data, fn);
}
}
}.bind(this));
} catch (err) {
fn && fn(err);
}
};
DynamoDBStore.prototype.set = function(sid, sess, fn){
sid = this.prefix + sid;
var expires = typeof sess.cookie.maxAge === 'number'
? (+new Date()) + sess.cookie.maxAge
: (+new Date()) + oneDay;
sess = JSON.stringify(sess);
var params = {
TableName : this.table,
Item : {
id: { 'S' : sid },
expires: { 'S' : JSON.stringify(expires) },
type: { 'S' : 'connect-session' },
sess: { 'S' : sess }
}
};
this.client.putItem(params, fn);
};
/**
/**
* Cleans up expired sessions

@@ -160,41 +163,42 @@ *

*/
DynamoDBStore.prototype.reap = function (fn) {
var now = +new Date;
var options = {
endkey: '[' + now + ',{}]'
};
var opts = {
ScanFilter : {
"expires":{
"AttributeValueList":[{
"N":now.toString()
}],
"ComparisonOperator":"LT"
}
},
AttributesToGet : ["id"]
};
this.client.scan(this.table, opts, function (err, data) {
if (err) return fn && fn(err);
destroy.call(this, data, fn);
}.bind(this));
};
DynamoDBStore.prototype.reap = function (fn) {
var now = +new Date;
var options = {
endkey: '[' + now + ',{}]'
};
var params = {
TableName : this.table,
ScanFilter : {
"expires":{
"AttributeValueList": [{
"N" : now.toString()
}],
"ComparisonOperator":"LT"
}
},
AttributesToGet : ["id"]
};
this.client.scan(params, function (err, data) {
if (err) return fn && fn(err);
destroy.call(this, data, fn);
}.bind(this));
};
function destroy(data, fn) {
var self = this;
function destroyDataAt(index) {
if (index === data.length) {
return fn && fn();
} else {
var sid = data[index].id;
sid = sid.substring(self.prefix.length, sid.length);
self.destroy(sid, function() {
destroyDataAt(index+1);
});
}
}
destroyDataAt(0);
}
function destroy(data, fn) {
var self = this;
function destroyDataAt(index) {
if (data.Count > 0) {
var sid = data.Items[index].id.S;
sid = sid.substring(self.prefix.length, sid.length);
self.destroy(sid, function() {
destroyDataAt(index+1);
});
} else {
return fn && fn();
}
}
destroyDataAt(0);
}
/**
/**
* Destroy the session associated with the given `sid`.

@@ -205,8 +209,13 @@ *

*/
DynamoDBStore.prototype.destroy = function(sid, fn){
sid = this.prefix + sid;
this.client.deleteItem(this.table, sid, fn || function(){});
};
DynamoDBStore.prototype.destroy = function(sid, fn){
sid = this.prefix + sid;
this.client.deleteItem({
TableName : this.table,
Key : {
HashKeyElement: { 'S' : sid }
}
}, fn || function(){});
};
/**
/**
* Clear intervals

@@ -217,7 +226,7 @@ *

*/
DynamoDBStore.prototype.clearInterval = function () {
if (this._reap) clearInterval(this._reap);
};
DynamoDBStore.prototype.clearInterval = function () {
if (this._reap) clearInterval(this._reap);
};
return DynamoDBStore;
return DynamoDBStore;
};
{
"name": "connect-dynamodb",
"description": "DynamoDB session store for Connect",
"version": "0.0.6",
"author": "Mike Carson <ca98am79@gmail.com>",
"version": "0.1.0",
"author": "Mike Carson <ca98am79@gmail.com> (http://ca98am79.com)",
"main": "./index.js",
"dependencies": { "dynode": "*"},
"devDependencies": { "connect": "*" },
"engines": { "node": "*" }
}
"dependencies": {
"aws-sdk": "*"
},
"devDependencies": {
"connect": "*"
},
"engines": {
"node": "*"
},
"optionalDependencies": {},
"homepage": "https://github.com/ca98am79/connect-dynamodb",
"repository": {
"type": "git",
"url": "git://github.com/ca98am79/connect-dynamodb.git"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc