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

connect-mongodb-session

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-mongodb-session - npm Package Compare versions

Comparing version 3.1.1 to 4.0.0

.github/workflows/test.yml

4

History.md

@@ -0,1 +1,5 @@

4.0.0 / 2024-01-17
==================
* BREAKING CHANGE: upgrade to MongoDB driver 5.x
3.1.1 / 2021-10-04

@@ -2,0 +6,0 @@ ==================

132

index.js

@@ -11,3 +11,3 @@ 'use strict';

$required: true,
$default: 'mongodb://localhost:27017/test'
$default: 'mongodb://127.0.0.1:27017/test'
},

@@ -87,30 +87,32 @@ collection: {

const connOptions = options.connectionOptions;
mongodb.MongoClient.connect(options.uri, connOptions, function(error, client) {
if (error) {
var e = new Error('Error connecting to db: ' + error.message);
return _this._errorHandler(e, callback);
}
const client = new mongodb.MongoClient(options.uri, connOptions);
this.client = client;
const db = options.databaseName == null ?
client.db() :
client.db(options.databaseName);
this.db = db;
this.collection = db.collection(this.options.collection);
this.initialConnectionPromise = client.connect().
then(() => {
const expiresIndex = {};
expiresIndex[options.expiresKey] = 1
const db = options.databaseName == null ?
client.db() :
client.db(options.databaseName);
_this.client = client;
_this.db = db;
const expiresIndex = {};
expiresIndex[options.expiresKey] = 1
db.
collection(options.collection).
createIndex(expiresIndex, { expireAfterSeconds: options.expiresAfterSeconds }, function(error) {
if (error) {
const e = new Error('Error creating index: ' + error.message);
return this.collection.
createIndex(expiresIndex, { expireAfterSeconds: options.expiresAfterSeconds }).
catch(err => {
const e = new Error('Error creating index: ' + err.message);
return _this._errorHandler(e, callback);
}
_this._emitter.emit('connected');
return callback && callback();
});
});
});
}).then(() => {
process.nextTick(() => callback && callback());
this._emitter.emit('connected');
return client;
}).
catch(error => {
var e = new Error('Error connecting to db: ' + error.message);
_this._errorHandler(e, callback);
if (callback == null) {
throw e;
}
});
};

@@ -129,16 +131,8 @@

if (!this.db) {
return this._emitter.once('connected', function() {
_this.get.call(_this, id, callback);
});
}
this.db.collection(this.options.collection).
findOne(this._generateQuery(id), function(error, session) {
if (error) {
const e = new Error('Error finding ' + id + ': ' + error.message);
return _this._errorHandler(e, callback);
} else if (session) {
this.collection.
findOne(this._generateQuery(id)).
then(session => {
if (session) {
if (!session.expires || new Date < session.expires) {
return callback(null, session.session);
return process.nextTick(() => callback(null, session.session));
} else {

@@ -148,4 +142,8 @@ return _this.destroy(id, callback);

} else {
return callback();
return process.nextTick(() => callback());
}
}).
catch(error => {
const e = new Error('Error finding ' + id + ': ' + error.message);
return _this._errorHandler(e, callback);
});

@@ -182,15 +180,9 @@ };

const _this = this;
if (!this.db) {
return this._emitter.once('connected', function() {
_this.destroy.call(_this, id, callback);
});
}
this.db.collection(this.options.collection).
deleteOne(this._generateQuery(id), function(error) {
if (error) {
const e = new Error('Error destroying ' + id + ': ' + error.message);
return _this._errorHandler(e, callback);
}
callback && callback();
this.collection.deleteOne(this._generateQuery(id)).
then(() => {
process.nextTick(() => callback && callback());
}).catch(error => {
const e = new Error('Error destroying ' + id + ': ' + error.message);
return _this._errorHandler(e, callback);
});

@@ -201,15 +193,10 @@ };

const _this = this;
if (!this.db) {
return this._emitter.once('connected', function() {
_this.clear.call(_this, callback);
});
}
this.db.collection(this.options.collection).
deleteMany({}, function(error) {
if (error) {
const e = new Error('Error clearing all sessions: ' + error.message);
this.collection.deleteMany({}).
then(() => {
process.nextTick(() => callback && callback());
}).
catch(error => {
const e = new Error('Error clearing all sessions: ' + error.message);
return _this._errorHandler(e, callback);
}
callback && callback();
});

@@ -221,8 +208,2 @@ };

if (!this.db) {
return this._emitter.once('connected', function() {
_this.set.call(_this, id, session, callback);
});
}
const sess = {};

@@ -246,10 +227,9 @@ for (const key in session) {

this.db.collection(this.options.collection).
updateOne(this._generateQuery(id), { $set: s }, { upsert: true }, function(error) {
if (error) {
const e = new Error('Error setting ' + id + ' to ' +
this.collection.updateOne(this._generateQuery(id), { $set: s }, { upsert: true }).
then(() => {
process.nextTick(() => callback && callback());
}).catch(error => {
const e = new Error('Error setting ' + id + ' to ' +
require('util').inspect(session) + ': ' + error.message);
return _this._errorHandler(e, callback);
}
callback && callback();
});

@@ -256,0 +236,0 @@ };

{
"name": "connect-mongodb-session",
"version": "3.1.1",
"version": "4.0.0",
"description": "MongoDB session store for connect/express built by MongoDB",

@@ -19,3 +19,3 @@ "keywords": [

"archetype": "0.13.x",
"mongodb": "4.x"
"mongodb": "5.x"
},

@@ -31,2 +31,3 @@ "devDependencies": {

"mocha": "3.1.2",
"sinon": "17.0.1",
"strawman": "0.0.1",

@@ -33,0 +34,0 @@ "superagent": "3.x"

@@ -44,3 +44,3 @@ # connect-mongodb-session

var store = new MongoDBStore({
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
uri: 'mongodb://127.0.0.1:27017/connect_mongodb_session_test',
collection: 'mySessions'

@@ -134,3 +134,3 @@ });

var store = new MongoDBStore({
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
uri: 'mongodb://127.0.0.1:27017/connect_mongodb_session_test',
collection: 'mySessions',

@@ -137,0 +137,0 @@

@@ -0,1 +1,3 @@

'use strict';
var assert = require('assert');

@@ -14,14 +16,9 @@ var superagent = require('superagent');

beforeEach(function(done) {
mongodb.MongoClient.connect(
'mongodb://localhost:27017/connect_mongodb_session_test',
function(error, client) {
if (error) {
return done(error);
}
underlyingDb = client.db('connect_mongodb_session_test');
client.db('connect_mongodb_session_test').collection('mySessions').deleteMany({}, function(error) {
return done(error);
});
});
beforeEach(async function() {
const client = await mongodb.MongoClient.connect(
'mongodb://127.0.0.1:27017/connect_mongodb_session_test',
{ serverSelectionTimeoutMS: 5000 }
);
underlyingDb = client.db('connect_mongodb_session_test');
await client.db('connect_mongodb_session_test').collection('mySessions').deleteMany({});
});

@@ -50,3 +47,3 @@

*/
it('can store sessions for Express 4', function(done) {
it('can store sessions for Express 4', async function() {
var express = require('express');

@@ -58,3 +55,3 @@ var session = require('express-session');

var store = new MongoDBStore({
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
uri: 'mongodb://127.0.0.1:27017/connect_mongodb_session_test',
collection: 'mySessions'

@@ -99,30 +96,16 @@ });

// acquit:ignore:start
underlyingDb.collection('mySessions').countDocuments({}, function(error, count) {
assert.ifError(error);
assert.equal(0, count);
let count = await underlyingDb.collection('mySessions').countDocuments({});
assert.equal(0, count);
superagent.get('http://localhost:3000', function(error, response) {
assert.ifError(error);
assert.equal(1, response.headers['set-cookie'].length);
var cookie = require('cookie').parse(response.headers['set-cookie'][0]);
assert.ok(cookie['connect.sid']);
underlyingDb.collection('mySessions').countDocuments({}, function(error, count) {
assert.ifError(error);
assert.equal(1, count);
superagent.get('http://localhost:3000').set('Cookie', 'connect.sid=' + cookie['connect.sid']).end(function(error, response) {
assert.ok(!response.headers['set-cookie']);
store.clear(function(error) {
assert.ifError(error);
underlyingDb.collection('mySessions').countDocuments({}, function(error, count) {
assert.ifError(error);
assert.equal(0, count);
done();
});
});
});
});
});
});
// acquit:ignore:end
let response = await superagent.get('http://127.0.0.1:3000');
assert.equal(1, response.headers['set-cookie'].length);
var cookie = require('cookie').parse(response.headers['set-cookie'][0]);
assert.ok(cookie['connect.sid']);
count = await underlyingDb.collection('mySessions').countDocuments({});
assert.equal(count, 1);
response = await superagent.get('http://127.0.0.1:3000').set('Cookie', 'connect.sid=' + cookie['connect.sid']);
assert.ok(!response.headers['set-cookie']);
await store.clear();
count = await underlyingDb.collection('mySessions').countDocuments({});
assert.equal(count, 0);
});

@@ -193,3 +176,3 @@

var store = new MongoDBStore({
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
uri: 'mongodb://127.0.0.1:27017/connect_mongodb_session_test',
collection: 'mySessions',

@@ -196,0 +179,0 @@

@@ -1,28 +0,15 @@

var assert = require('assert');
var connectMongoDBSession = require('../');
var ee = require('events').EventEmitter;
var mongodb = require('mongodb');
var strawman = require('strawman');
'use strict';
const assert = require('assert');
const connectMongoDBSession = require('../');
const ee = require('events').EventEmitter;
const mongodb = require('mongodb');
const sinon = require('sinon');
describe('connectMongoDBSession', function() {
var client = {"db": {}};
var db;
var StoreStub;
afterEach(() => sinon.restore());
beforeEach(function() {
db = strawman({
collection: { argumentNames: ['collection'], chain: true },
createIndex: { argumentNames: ['index', 'options', 'callback'] },
findOne: { argumentNames: ['query', 'callback'] },
deleteOne: { argumentNames: ['query', 'callback'] },
deleteMany: { argumentNames: ['query', 'callback'] },
updateOne: { argumentNames: ['query', 'update', 'options', 'callback' ] }
});
client.db = function(n) {return db;};
mongodb.MongoClient.connect = function(uri, options, callback) {
process.nextTick(function() { callback(null, client); });
};
StoreStub = function() {};

@@ -35,4 +22,4 @@ StoreStub.prototype = { connectMongoDB: 1 };

var SessionStore = connectMongoDBSession({ Store: StoreStub });
var session = new SessionStore({ uri: 'mongodb://host:port/db' });
assert.equal(session.options.uri, 'mongodb://host:port/db');
var session = new SessionStore({ uri: 'mongodb://host:1111/db' });
assert.equal(session.options.uri, 'mongodb://host:1111/db');
assert.equal(session.options.idField, '_id');

@@ -45,3 +32,3 @@ done();

var session = SessionStore({ collection: 'notSessions' });
assert.equal(session.options.uri, 'mongodb://localhost:27017/test');
assert.equal(session.options.uri, 'mongodb://127.0.0.1:27017/test');
assert.equal(session.options.collection, 'notSessions');

@@ -54,3 +41,3 @@ done();

var session = new SessionStore({ expires: 25 });
assert.equal(session.options.uri, 'mongodb://localhost:27017/test');
assert.equal(session.options.uri, 'mongodb://127.0.0.1:27017/test');
assert.equal(session.options.expires, 25);

@@ -63,3 +50,3 @@ done();

var session = new SessionStore({ idField: 'sessionId' });
assert.equal(session.options.uri, 'mongodb://localhost:27017/test');
assert.equal(session.options.uri, 'mongodb://127.0.0.1:27017/test');
assert.deepEqual(session._generateQuery('1234'), { sessionId: '1234' });

@@ -85,8 +72,2 @@ done();

var SessionStore = connectMongoDBSession({ Store: StoreStub });
var numIndexCalls = 0;
db.createIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback();
});

@@ -97,3 +78,3 @@ var session = new SessionStore(function(error) {

});
assert.equal(session.options.uri, 'mongodb://localhost:27017/test');
assert.equal(session.options.uri, 'mongodb://127.0.0.1:27017/test');
});

@@ -103,11 +84,5 @@

var SessionStore = connectMongoDBSession({ Store: StoreStub });
var numIndexCalls = 0;
db.createIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback();
});
var session = new SessionStore();
assert.equal(session.options.uri, 'mongodb://localhost:27017/test');
assert.equal(session.options.uri, 'mongodb://127.0.0.1:27017/test');

@@ -120,6 +95,5 @@ session.on('connected', function() {

it('throws an error when connection fails and no callback', function(done) {
mongodb.MongoClient.connect = function(uri, options, callback) {
// purposely make callback sync
callback(new Error('Cant connect'));
};
sinon.stub(mongodb.MongoClient.prototype, 'connect').callsFake(() => {
return Promise.reject(new Error('Cant connect'));
});

@@ -140,5 +114,5 @@ var SessionStore = connectMongoDBSession({ Store: StoreStub });

it('passes error to callback if specified', function(done) {
mongodb.MongoClient.connect = function(uri, options, callback) {
process.nextTick(function() { callback(new Error('Cant connect')); });
};
sinon.stub(mongodb.MongoClient.prototype, 'connect').callsFake(() => {
return Promise.reject(new Error('connect issues'));
});

@@ -159,7 +133,5 @@ var SessionStore = connectMongoDBSession({ Store: StoreStub });

var SessionStore = connectMongoDBSession({ Store: StoreStub });
var numIndexCalls = 0;
db.createIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback(new Error('Index fail'));
sinon.stub(mongodb.Collection.prototype, 'createIndex').callsFake(() => {
return Promise.reject(new Error('Index fail'));
});

@@ -174,47 +146,23 @@

describe('get()', function() {
var numIndexCalls;
it('gets the session', function(done) {
const SessionStore = connectMongoDBSession({ Store: StoreStub });
beforeEach(function() {
numIndexCalls = 0;
db.createIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback();
});
});
it('buffers get() calls', function(done) {
var SessionStore = connectMongoDBSession({ Store: StoreStub });
var emitter = new ee();
mongodb.MongoClient.connect = function(uri, options, callback) {
emitter.on('success', function() {
callback(null, client);
});
};
var session = new SessionStore();
db.findOne.on('called', function(args) {
args.callback(null,
{ expires: new Date('2040-06-01T00:00:00.000Z'), session: { data: 1 } });
sinon.stub(session.collection, 'findOne').callsFake(() => {
return Promise.resolve({ expires: new Date('2040-06-01T00:00:00.000Z'), session: { data: 1 } });
});
session.get('1234', function(error) {
session.get('1234', function(error, session) {
assert.ifError(error);
assert.equal(numIndexCalls, 1);
assert.deepStrictEqual(session, { data: 1 });
done();
});
setImmediate(function() {
emitter.emit('success');
});
});
it('handles get() errors', function(done) {
var SessionStore = connectMongoDBSession({ Store: StoreStub });
const SessionStore = connectMongoDBSession({ Store: StoreStub });
var session = new SessionStore();
db.findOne.on('called', function(args) {
args.callback(new Error('fail!'));
const session = new SessionStore();
sinon.stub(session.collection, 'findOne').callsFake(() => {
return Promise.reject(new Error('fail!'));
});

@@ -231,13 +179,9 @@

var SessionStore = connectMongoDBSession({ Store: StoreStub });
var numRemoveCalls = 0;
var session = new SessionStore();
db.findOne.on('called', function(args) {
args.callback(null, { expires: new Date('2011-06-01T00:00:00.000Z') });
sinon.stub(session.collection, 'findOne').callsFake(() => {
return Promise.resolve({ expires: new Date('2011-06-01T00:00:00.000Z') });
});
db.deleteOne.on('called', function(args) {
++numRemoveCalls;
assert.equal(args.query._id, '1234');
args.callback();
sinon.stub(session.collection, 'deleteOne').callsFake(() => {
return Promise.resolve();
});

@@ -248,3 +192,3 @@

assert.ok(!doc);
assert.equal(numRemoveCalls, 1);
assert.equal(session.collection.deleteOne.getCalls().length, 1);
done();

@@ -258,4 +202,4 @@ });

var session = new SessionStore();
db.findOne.on('called', function(args) {
args.callback(null, null);
sinon.stub(session.collection, 'findOne').callsFake(() => {
return Promise.resolve(null);
});

@@ -272,40 +216,2 @@

describe('destroy()', function() {
var numIndexCalls;
beforeEach(function() {
numIndexCalls = 0;
db.createIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback();
});
});
it('buffers until connected', function(done) {
var SessionStore = connectMongoDBSession({ Store: StoreStub });
var emitter = new ee();
mongodb.MongoClient.connect = function(uri, options, callback) {
emitter.on('success', function() {
callback(null, client);
});
};
var session = new SessionStore();
db.deleteOne.on('called', function(args) {
args.callback(null);
});
session.destroy('1234', function(error) {
assert.ifError(error);
assert.equal(numIndexCalls, 1);
done();
});
setImmediate(function() {
emitter.emit('success');
});
});
it('reports driver errors', function(done) {

@@ -315,9 +221,8 @@ var SessionStore = connectMongoDBSession({ Store: StoreStub });

var session = new SessionStore();
db.deleteOne.on('called', function(args) {
args.callback(new Error('fail!'));
});
sinon.stub(session.collection, 'deleteOne')
.callsFake(() => Promise.reject(new Error('roadrunners pachyderma')));
session.destroy('1234', function(error) {
assert.ok(error);
assert.equal(error.message, 'Error destroying 1234: fail!');
assert.equal(error.message, 'Error destroying 1234: roadrunners pachyderma');
done();

@@ -328,41 +233,3 @@ });

describe('set()', function(done) {
var numIndexCalls;
beforeEach(function() {
numIndexCalls = 0;
db.createIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback();
});
});
it('buffers until connected', function(done) {
var SessionStore = connectMongoDBSession({ Store: StoreStub });
var emitter = new ee();
mongodb.MongoClient.connect = function(uri, options, callback) {
emitter.on('success', function() {
callback(null, client);
});
};
var session = new SessionStore();
db.updateOne.on('called', function(args) {
args.callback(null);
});
session.set('1234', { test: 1 }, function(error) {
assert.ifError(error);
assert.equal(numIndexCalls, 1);
done();
});
setImmediate(function() {
emitter.emit('success');
});
});
describe('set()', function() {
it('converts expires to a date', function(done) {

@@ -373,7 +240,4 @@ var SessionStore = connectMongoDBSession({ Store: StoreStub });

db.updateOne.on('called', function(args) {
assert.ok(args.update.$set.expires instanceof Date);
assert.equal(args.update.$set.expires.getTime(),
new Date('2011-06-01T00:00:00.000Z').getTime());
args.callback(null);
sinon.stub(session.collection, 'updateOne').callsFake(() => {
return Promise.resolve(null);
});

@@ -386,3 +250,6 @@ var update = {

assert.ifError(error);
assert.equal(db.updateOne.calls.length, 1);
assert.equal(session.collection.updateOne.getCalls().length, 1);
assert.ok(session.collection.updateOne.getCalls()[0].args[1].$set.expires instanceof Date);
assert.equal(session.collection.updateOne.getCalls()[0].args[1].$set.expires.getTime(),
new Date('2011-06-01T00:00:00.000Z').getTime());
done();

@@ -396,4 +263,4 @@ });

var session = new SessionStore();
db.updateOne.on('called', function(args) {
args.callback(new Error('fail!'));
sinon.stub(session.collection, 'updateOne').callsFake(() => {
return Promise.reject(new Error('taco tuesday'));
});

@@ -403,3 +270,3 @@

assert.ok(error);
assert.equal(error.message, 'Error setting 1234 to {}: fail!');
assert.equal(error.message, 'Error setting 1234 to {}: taco tuesday');
done();

@@ -415,5 +282,4 @@ });

db.updateOne.on('called', function(args) {
assert.equal(args.update.$set.session.cookie, 'put that cookie down!');
args.callback(null);
sinon.stub(session.collection, 'updateOne').callsFake(() => {
return Promise.resolve(null);
});

@@ -426,3 +292,7 @@ var update = {

assert.ifError(error);
assert.equal(db.updateOne.calls.length, 1);
assert.equal(session.collection.updateOne.getCalls().length, 1);
assert.equal(
session.collection.updateOne.getCalls()[0].args[1].$set.session.cookie,
'put that cookie down!'
);
done();

@@ -438,5 +308,4 @@ });

db.updateOne.on('called', function(args) {
assert.deepEqual(args.update.$set.session.cookie, { test: 2 });
args.callback(null);
sinon.stub(session.collection, 'updateOne').callsFake(() => {
return Promise.resolve(null);
});

@@ -449,3 +318,7 @@ var update = {

assert.ifError(error);
assert.equal(db.updateOne.calls.length, 1);
assert.equal(session.collection.updateOne.getCalls().length, 1);
assert.deepEqual(
session.collection.updateOne.getCalls()[0].args[1].$set.session.cookie,
{ test: 2 }
);
done();

@@ -456,15 +329,3 @@ });

describe('clear()', function(done){
var numIndexCalls;
beforeEach(function() {
numIndexCalls = 0;
db.createIndex.on('called', function(args) {
assert.equal(++numIndexCalls, 1);
assert.equal(args.index.expires, 1);
args.callback();
});
});
describe('clear()', function() {
it('clears the session store', function(done) {

@@ -474,8 +335,8 @@ var SessionStore = connectMongoDBSession({ Store: StoreStub });

var session = new SessionStore();
db.deleteMany.on('called', function(args) {
args.callback(null);
});
sinon.stub(session.collection, 'deleteMany').callsFake(() => Promise.resolve());
session.clear(function(error) {
assert.ifError(error);
assert.ok(session.collection.deleteMany.calledOnce);
assert.deepStrictEqual(session.collection.deleteMany.getCalls()[0].args[0], {});
done();

@@ -489,9 +350,8 @@ });

var session = new SessionStore();
db.deleteMany.on('called', function(args) {
args.callback(new Error('fail!'));
});
sinon.stub(session.collection, 'deleteMany').
callsFake(() => Promise.reject(new Error('clear issue')));
session.clear(function(error) {
assert.ok(error);
assert.equal(error.message, 'Error clearing all sessions: fail!');
assert.equal(error.message, 'Error clearing all sessions: clear issue');
done();

@@ -498,0 +358,0 @@ });

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