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 1.4.0 to 2.0.0

HEADER.md

6

index.js

@@ -19,2 +19,3 @@ var mongodb = require('mongodb');

uri: 'mongodb://localhost:27017/test',
databaseName: 'test',
collection: 'sessions',

@@ -44,3 +45,3 @@ connectionOptions: {},

var connOptions = options.connectionOptions;
mongodb.MongoClient.connect(options.uri, connOptions, function(error, db) {
mongodb.MongoClient.connect(options.uri, connOptions, function(error, client) {
if (error) {

@@ -51,2 +52,5 @@ var e = new Error('Error connecting to db: ' + error.message);

var db = client.db(options.databaseName);
this.db = db;
db.

@@ -53,0 +57,0 @@ collection(options.collection).

9

package.json
{
"name": "connect-mongodb-session",
"version": "1.4.0",
"version": "2.0.0",
"description": "MongoDB session store for connect/express built by MongoDB",

@@ -18,6 +18,8 @@ "keywords": [

"dependencies": {
"mongodb": "~2.2.0"
"mongodb": "~3.0.4"
},
"devDependencies": {
"acquit": "0.4.1",
"acquit": "0.5.1",
"acquit-ignore": "0.0.3",
"acquit-markdown": "0.0.8",
"cookie": "0.3.1",

@@ -37,2 +39,3 @@ "express": "4.14.0",

"scripts": {
"docs": "acquit-markdown -r acquit-ignore -p './test/examples.test.js' -h './HEADER.md' > README.md",
"test": "env NODE_PATH=../ ./node_modules/mocha/bin/mocha ./test/*.test.js",

@@ -39,0 +42,0 @@ "test-travis": "env NODE_PATH=../ ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*.test.js",

@@ -7,6 +7,7 @@ # connect-mongodb-session

# API
## MongoDBStore
# MongoDBStore
This module exports a single function which takes an instance of connect

@@ -16,4 +17,6 @@ (or Express) and returns a `MongoDBStore` class that can be used to

#### It can store sessions for Express 4
## It can store sessions for Express 4
If you pass in an instance of the

@@ -24,6 +27,7 @@ [`express-session` module](http://npmjs.org/package/express-session)

The MongoDBStore class has 2 required options:
The MongoDBStore class has 3 required options:
1. `uri`: a [MongoDB connection string](http://docs.mongodb.org/manual/reference/connection-string/)
2. `collection`: the MongoDB collection to store sessions in
2. `databaseName`: the MongoDB database to store sessions in
3. `collection`: the MongoDB collection to store sessions in

@@ -35,4 +39,5 @@ **Note:** You can pass a callback to the `MongoDBStore` constructor,

```javascript
var express = require('express');

@@ -46,2 +51,3 @@ var session = require('express-session');

uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
databaseName: 'connect_mongodb_session_test',
collection: 'mySessions'

@@ -74,55 +80,10 @@ });

server = app.listen(3000);
console.log('listening on port 3000');
```
**Optional:** To clear all sessions from the store, use `store.clear(callback);`.
The callback should be called as `callback(error)`.
## It throws an error when it can't connect to MongoDB
#### It can store sessions for latest Express 3.x
If you're using Express 3.x, you need to pass the Express module itself
rather than the `express-session` module. Session storage is part of
the Express core in 3.x but not in 4.x.
**Note:** This example doesn't pass a callback to the `MongoDBStore`
constructor. This module can queue up requests to execute once the
database is connected. However, the `MongoDBStore` constructor will
throw an exception if it can't connect and no callback is passed.
```javascript
var express = require('../vendor/express-3.18.1');
var MongoDBStore = require('connect-mongodb-session')(express);
var app = express();
var store = new MongoDBStore(
{
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
collection: 'mySessions'
});
app.use(express.session({
secret: 'This is a secret',
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
},
store: store,
// Boilerplate options, see:
// * https://www.npmjs.com/package/express-session#resave
// * https://www.npmjs.com/package/express-session#saveuninitialized
resave: true,
saveUninitialized: true
}));
app.get('/', function(req, res) {
res.send('Hello ' + JSON.stringify(req.session));
});
server = app.listen(3000);
```
#### It throws an error when it can't connect to MongoDB
You should pass a callback to the `MongoDBStore` constructor to catch

@@ -132,7 +93,8 @@ errors. If you don't pass a callback to the `MongoDBStore` constructor,

```javascript
var express = require('../vendor/express-3.18.1');
var MongoDBStore = require('connect-mongodb-session')(express);
var express = require('express');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);

@@ -144,2 +106,3 @@ var app = express();

uri: 'mongodb://bad.host:27000/connect_mongodb_session_test?connectTimeoutMS=10',
databaseName: 'connect_mongodb_session_test',
collection: 'mySessions'

@@ -155,3 +118,3 @@ },

app.use(express.session({
app.use(session({
secret: 'This is a secret',

@@ -176,2 +139,1 @@ cookie: {

```

@@ -17,8 +17,8 @@ var assert = require('assert');

'mongodb://localhost:27017/connect_mongodb_session_test',
function(error, db) {
function(error, client) {
if (error) {
return done(error);
}
underlyingDb = db;
db.collection('mySessions').remove({}, function(error) {
underlyingDb = client.db('connect_mongodb_session_test');
client.db('connect_mongodb_session_test').collection('mySessions').remove({}, function(error) {
return done(error);

@@ -39,6 +39,7 @@ });

*
* The MongoDBStore class has 2 required options:
* The MongoDBStore class has 3 required options:
*
* 1. `uri`: a [MongoDB connection string](http://docs.mongodb.org/manual/reference/connection-string/)
* 2. `collection`: the MongoDB collection to store sessions in
* 2. `databaseName`: the MongoDB database to store sessions in
* 3. `collection`: the MongoDB collection to store sessions in
*

@@ -59,2 +60,3 @@ * **Note:** You can pass a callback to the `MongoDBStore` constructor,

uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
databaseName: 'connect_mongodb_session_test',
collection: 'mySessions'

@@ -120,65 +122,4 @@ });

// acquit:ignore:end
});
/**
* If you're using Express 3.x, you need to pass the Express module itself
* rather than the `express-session` module. Session storage is part of
* the Express core in 3.x but not in 4.x.
*
* **Note:** This example doesn't pass a callback to the `MongoDBStore`
* constructor. This module can queue up requests to execute once the
* database is connected. However, the `MongoDBStore` constructor will
* throw an exception if it can't connect and no callback is passed.
*/
it('can store sessions for latest Express 3.x', function(done) {
var express = require('../vendor/express-3.18.1');
var MongoDBStore = require('connect-mongodb-session')(express);
var app = express();
var store = new MongoDBStore(
{
uri: 'mongodb://localhost:27017/connect_mongodb_session_test',
collection: 'mySessions'
});
app.use(express.session({
secret: 'This is a secret',
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7 // 1 week
},
store: store,
// Boilerplate options, see:
// * https://www.npmjs.com/package/express-session#resave
// * https://www.npmjs.com/package/express-session#saveuninitialized
resave: true,
saveUninitialized: true
}));
app.get('/', function(req, res) {
res.send('Hello ' + JSON.stringify(req.session));
});
server = app.listen(3000);
// acquit:ignore:start
underlyingDb.collection('mySessions').count({}, function(error, count) {
assert.ifError(error);
assert.equal(0, count);
request('http://localhost:3000', function(error, response, body) {
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').find({}).toArray(function(error, docs) {
assert.ifError(error);
assert.equal(1, docs.length);
assert.equal(typeof docs[0]._id, 'string');
done();
});
});
});
// acquit:ignore:end
console.log('listening on port 3000');
});

@@ -192,6 +133,6 @@

it('throws an error when it can\'t connect to MongoDB', function(done) {
var express = require('../vendor/express-3.18.1');
var express = require('express');
var session = require('express-session');
var MongoDBStore = require('connect-mongodb-session')(session);
var MongoDBStore = require('connect-mongodb-session')(express);
var app = express();

@@ -202,2 +143,3 @@ var numExpectedSources = 2;

uri: 'mongodb://bad.host:27000/connect_mongodb_session_test?connectTimeoutMS=10',
databaseName: 'connect_mongodb_session_test',
collection: 'mySessions'

@@ -221,3 +163,3 @@ },

app.use(express.session({
app.use(session({
secret: 'This is a secret',

@@ -224,0 +166,0 @@ cookie: {

@@ -8,2 +8,3 @@ var assert = require('assert');

describe('connectMongoDBSession', function() {
var client = {"db": {}};
var db;

@@ -21,4 +22,6 @@ var StoreStub;

client.db = function(n) {return db;};
mongodb.MongoClient.connect = function(uri, options, callback) {
process.nextTick(function() { callback(null, db); });
process.nextTick(function() { callback(null, client); });
};

@@ -169,3 +172,3 @@

emitter.on('success', function() {
callback(null, db);
callback(null, client);
});

@@ -264,3 +267,3 @@ };

emitter.on('success', function() {
callback(null, db);
callback(null, client);
});

@@ -320,3 +323,3 @@ };

emitter.on('success', function() {
callback(null, db);
callback(null, client);
});

@@ -323,0 +326,0 @@ };

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