Skip to content

Commit

Permalink
- Mongo's native driver doesn't have a working Long.toNumber(). All t…
Browse files Browse the repository at this point in the history
…imestamps are converted to 32767!
  • Loading branch information
Bart Teeuwisse committed Nov 16, 2010
1 parent 791e416 commit 293d217
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/express-session-mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ var mongo = require('mongodb'),
Store = require('connect').session.Store,
Db = mongo.Db,
Connection = mongo.Connection,
Server = mongo.Server,
BSON = mongo.BSONNative;
Server = mongo.Server;

var MongoStore = module.exports = function(options) {
options = options || {};
Expand Down Expand Up @@ -37,7 +36,9 @@ var MongoStore = module.exports = function(options) {
server= new Server(ip, port, {auto_reconnect: true}, {});
}

this._db = new Db( dbName, server, { native_parser:true });
// As much as I favor native drivers, the node-mongodb-native native driver isn'this
// on par with its pure implementation. E.g. Long.toNumber() doesn't work.
this._db = new Db( dbName, server, { native_parser: false });
this._db.open(function(db) {});

}
Expand Down Expand Up @@ -130,17 +131,23 @@ MongoStore.prototype.clear = function() {
var cleanSessionData = function(json) {
var data = {};
for (var i in json) {
// Don't return mongo's internal ID for the session.
if (i === '_id') { continue; }
data[i] = json[i];
if (data[i] instanceof Object) {
if ('low_' in data[i] || 'high_' in data[i]) {
data[i] = data[i].toNumber();
}
// lastAccess is a Unix timestamp which mongo stores as a 2 component Long object. Convert it back to number.
if (data[i] instanceof mongo.BSONPure.Long) {
data[i] = data[i].toNumber();
}

}
return data;
};

/**
* There is a problem in Mongo's Native & Pure drivers in that functions of the session's prototype are also saved to
* mongo. Cloning just the session's own properties is a workaround.
*
* @param original {Object} The session object whose own properties should be cloned.
*/
var cloneOwnProperties = function(original) {
var copy = {};
for (var i in original) {
Expand Down

0 comments on commit 293d217

Please sign in to comment.