connect-dynamodb
Advanced tools
Comparing version 0.0.6 to 0.1.0
@@ -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 @@ ================== |
/*! | ||
* 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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
224
1
2
9484
82
+ Addedaws-sdk@*
+ Addedaws-sdk@2.1692.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@4.9.2(transitive)
+ Addedevents@1.1.1(transitive)
+ Addedieee754@1.1.13(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-generator-function@1.1.0(transitive)
+ Addedis-typed-array@1.1.15(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedjmespath@0.16.0(transitive)
+ Addedpunycode@1.3.2(transitive)
+ Addedquerystring@0.2.0(transitive)
+ Addedsax@1.2.1(transitive)
+ Addedurl@0.10.3(transitive)
+ Addedutil@0.12.5(transitive)
+ Addeduuid@8.0.0(transitive)
+ Addedxml2js@0.6.2(transitive)
+ Addedxmlbuilder@11.0.1(transitive)
- Removeddynode@*
- Removedarray-buffer-byte-length@1.0.2(transitive)
- Removedasync@0.1.22(transitive)
- Removeddeep-equal@2.2.3(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddynode@0.6.1(transitive)
- Removedes-get-iterator@1.1.3(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedhas-bigints@1.1.0(transitive)
- Removedi@0.3.7(transitive)
- Removedinternal-slot@1.1.0(transitive)
- Removedis-array-buffer@3.0.5(transitive)
- Removedis-bigint@1.1.0(transitive)
- Removedis-boolean-object@1.2.2(transitive)
- Removedis-date-object@1.1.0(transitive)
- Removedis-map@2.0.3(transitive)
- Removedis-number-object@1.1.1(transitive)
- Removedis-set@2.0.3(transitive)
- Removedis-shared-array-buffer@1.0.4(transitive)
- Removedis-string@1.1.1(transitive)
- Removedis-symbol@1.1.1(transitive)
- Removedis-weakmap@2.0.2(transitive)
- Removedis-weakset@2.0.4(transitive)
- Removedisarray@2.0.5(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedncp@0.2.7(transitive)
- Removedobject-inspect@1.13.4(transitive)
- Removedobject-is@1.1.6(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.7(transitive)
- Removedregexp.prototype.flags@1.5.4(transitive)
- Removedretry@0.6.1(transitive)
- Removedrimraf@1.0.9(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.1.0(transitive)
- Removedside-channel-list@1.0.0(transitive)
- Removedside-channel-map@1.0.1(transitive)
- Removedside-channel-weakmap@1.0.2(transitive)
- Removedstop-iteration-iterator@1.1.0(transitive)
- Removedunderscore@1.3.3(transitive)
- Removedutile@0.1.7(transitive)
- Removedwhich-boxed-primitive@1.1.1(transitive)
- Removedwhich-collection@1.0.2(transitive)