From e3da1263c65760531c93759b0c75e0d32ca6eacc Mon Sep 17 00:00:00 2001 From: da440dil Date: Mon, 26 Apr 2021 16:28:51 +0300 Subject: [PATCH 1/3] use mongodb promise api --- lib/index.js | 321 ++++++++++++++++++++++++++++----------------------- test.js | 98 ++++++++-------- 2 files changed, 226 insertions(+), 193 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7e8aed6..21576d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,148 +8,179 @@ * One day in milliseconds. */ -var oneDay = 86400 * 1000; - -/** - * Module dependencies - */ - -var inherits = require('util').inherits, - events = require('events'), - MongoClient = require('mongodb').MongoClient; - -var createIndexes = function(collection, callback) { - var times = 2; - function done(err) { - if (err) return callback(err); - if (--times < 1) { - callback(); - } - } - collection.createIndex({ttl: 1}, {expireAfterSeconds: 0}, done); - collection.createIndex({sid: 1}, {unique: true}, done); -}; - -var makeConnectionString = function(options) { - return 'mongodb://' + (options.host || '127.0.0.1') + ':' + - (options.port || '27017') + '/' + - (options.db || 'test') + - '?ssl=' + (options.ssl || false); -}; - -/** - * Return the `MongoStore` extending `connect`'s session Store. - * - * @param {object} connect - * @return {Function} - * @api public - */ -module.exports = function(connect) { - - /** - * Connect's Store. - */ - - var Store = connect.Store || connect.session.Store; - - /** - * Initialize MongoStore with the given `options`. - * - * @param {Object} options - * @api public - */ - - function MongoStore(options) { - var self = this; - - options = options || {}; - Store.call(this, options); - this.ttl = options.ttl; - - var emitConnect = function(err) { - if (err) throw err; - self.emit('connect'); - }; - - if (options.db && (typeof options.db !== 'string')) { - this.col = options.db.collection(options.collection || 'sessions'); - createIndexes(this.col, emitConnect); - } else { - var dbUrl = options.url || makeConnectionString(options); - MongoClient.connect(dbUrl, function(err, db) { - if (err) throw err; - self.col = db.collection(options.collection || 'sessions'); - if (options.user && options.password) { - db.authenticate(options.user, options.password, function(err, res) { - if (err) throw err; - if (!res) throw new Error('mongodb authentication failed'); - createIndexes(self.col, emitConnect); - }); - } else { - createIndexes(self.col, emitConnect); - } - }); - } - }; - - /** - * Inherit from `Store`. - */ - - inherits(MongoStore, Store); - - /** - * Attempt to fetch session by the given `sid`. - * - * @param {String} sid - * @param {Function} fn - * @api public - */ - - MongoStore.prototype.get = function(sid, fn){ - this.col.findOne({sid: sid}, {_id: 0, ttl: 0, sid: 0}, function(err, data) { - if (err) return fn(err); - if (!data) return fn(); - return fn(null, data); - }); - }; - - /** - * Commit the given `sess` object associated with the given `sid`. - * - * @param {String} sid - * @param {Session} sess - * @param {Function} fn - * @api public - */ - - MongoStore.prototype.set = function(sid, sess, fn) { - try { - var maxAge = sess.cookie.maxAge; - - sess.sid = sid; - sess.ttl = new Date((this.ttl || ('number' === typeof maxAge - ? maxAge : oneDay)) + Date.now()); - - this.col.replaceOne({sid: sid}, sess, {upsert: true}, function() { - fn && fn.apply(this, arguments); - }); - } catch (err) { - fn && fn(err); - } - }; - - /** - * Destroy the session associated with the given `sid`. - * - * @param {String} sid - * @param {Function} fn - * @api public - */ - - MongoStore.prototype.destroy = function(sid, fn) { - this.col.deleteOne({sid: sid}, fn); - }; - - return MongoStore; -}; + var oneDay = 86400 * 1000; + + /** + * Module dependencies + */ + + var inherits = require('util').inherits, + events = require('events'), + MongoClient = require('mongodb').MongoClient, + Db = require('mongodb').Db; + + var createIndexes = function(collection, callback) { + var times = 2; + function done(err) { + if (err) return callback(err); + if (--times < 1) { + callback(); + } + } + collection.createIndex({ttl: 1}, {expireAfterSeconds: 0}, done); + collection.createIndex({sid: 1}, {unique: true}, done); + }; + + var makeConnectionString = function(options) { + return 'mongodb://' + (options.host || '127.0.0.1') + ':' + + (options.port || '27017') + '/' + + (options.db || 'test') + + '?ssl=' + (options.ssl || false); + }; + + /** + * Return the `MongoStore` extending `connect`'s session Store. + * + * @param {object} connect + * @return {Function} + * @api public + */ + module.exports = function(connect) { + + /** + * Connect's Store. + */ + + var Store = connect.Store || connect.session.Store; + + /** + * Initialize MongoStore with the given `options`. + * + * @param {Object} options + * @api public + */ + + function MongoStore(options) { + var self = this; + + options = options || {}; + Store.call(this, options); + this.ttl = options.ttl; + + var emitConnect = function(err) { + if (err) throw err; + self.emit('connect'); + }; + + if (options.db && (typeof options.db !== 'string')) { + this.col = options.db.collection(options.collection || 'sessions'); + createIndexes(this.col, emitConnect); + } else { + var dbUrl = options.url || makeConnectionString(options); + var opts = {}; + if (options.user && options.password) { + opts.auth = { + user: options.user, + password: options.password + }; + } + MongoClient.connect(dbUrl, opts, function(err, client) { + if (err) throw err; + var db; + if (client instanceof MongoClient) { + var dbName = 'test'; + if (typeof options.db === 'string') { + dbName === options.db; + } else if ( + client.s && client.s.options && + typeof client.s.options.dbName === 'string' + ) { + dbName = client.s.options.dbName; + } + db = client.db(dbName); + } else if (client instanceof Db) { + db = client; + } else { + throw new Error('mongodb connect failed'); + } + self.col = db.collection(options.collection || 'sessions'); + if ( + options.user && options.password && + typeof db.authenticate === 'function' + ) { + db.authenticate(options.user, options.password, function(err, res) { + if (err) throw err; + if (!res) throw new Error('mongodb authentication failed'); + createIndexes(self.col, emitConnect); + }); + } else { + createIndexes(self.col, emitConnect); + } + }); + } + }; + + /** + * Inherit from `Store`. + */ + + inherits(MongoStore, Store); + + /** + * Attempt to fetch session by the given `sid`. + * + * @param {String} sid + * @param {Function} fn + * @api public + */ + + MongoStore.prototype.get = function(sid, fn){ + this.col.findOne({sid: sid}, {projection: {_id: 0, ttl: 0, sid: 0}}, function(err, data) { + if (err) return fn(err); + if (!data) return fn(); + return fn(null, data); + }); + }; + + /** + * Commit the given `sess` object associated with the given `sid`. + * + * @param {String} sid + * @param {Session} sess + * @param {Function} fn + * @api public + */ + + MongoStore.prototype.set = function(sid, sess, fn) { + var maxAge = sess.cookie.maxAge; + + sess.sid = sid; + sess.ttl = new Date((this.ttl || ('number' === typeof maxAge + ? maxAge : oneDay)) + Date.now()); + + this.col.replaceOne({sid: sid}, sess, {upsert: true}) + .then(function() { + fn(null, true); + }) + .catch(fn); + }; + + /** + * Destroy the session associated with the given `sid`. + * + * @param {String} sid + * @param {Function} fn + * @api public + */ + + MongoStore.prototype.destroy = function(sid, fn) { + this.col.deleteOne({sid: sid}) + .then(function() { + fn(); + }) + .catch(fn); + }; + + return MongoStore; + }; + \ No newline at end of file diff --git a/test.js b/test.js index 7acdc01..58c9dbc 100644 --- a/test.js +++ b/test.js @@ -2,74 +2,76 @@ * Module dependencies. */ -var assert = require('assert'), - connect = require('connect'), - MongoStore = require('./lib')(connect), - MongoClient = require('mongodb').MongoClient; + var assert = require('assert'), + connect = require('connect'), + MongoStore = require('./lib')(connect), + MongoClient = require('mongodb').MongoClient; var testsCount = 3; var done = (function () { - var count = 0; - return function () { - ++count; - if (count == testsCount) { - console.log('done'); - process.exit(0); - } - }; + var count = 0; + return function () { + ++count; + if (count == testsCount) { + console.log('done'); + process.exit(0); + } + }; })(); var store = new MongoStore; // simple test store.on('connect', function() { - baseTest.apply(this); + baseTest.apply(this); }); // test with initialized db object -MongoClient.connect('mongodb://127.0.0.1:27017/testdb', function(err, db) { - var store = new MongoStore({db: db}); - store.on('connect', function() { - baseTest.apply(this); - }); +MongoClient.connect('mongodb://127.0.0.1:27017/testdb', function(err, client) { + var db = client instanceof MongoClient ? client.db('testdb') : client; + var store = new MongoStore({db: db}); + store.on('connect', function() { + baseTest.apply(this); + }); }); // test mongodb auth -MongoClient.connect('mongodb://127.0.0.1:27017/testauth', function(err, db) { - db.removeUser('user', function() { - db.addUser('user', 'pass', {roles: ['readWrite']}, function(err, res) { - assert.ok(!err, '#addUser error'); - var store = new MongoStore({user: 'user', password: 'pass', db: 'testauth'}); - store.on('connect', function() { - baseTest.apply(this); - }); - }); - }); +MongoClient.connect('mongodb://127.0.0.1:27017/testauth', function(err, client) { + var db = client instanceof MongoClient ? client.db('testauth'): client; + db.removeUser('user', function() { + db.addUser('user', 'pass', {roles: ['readWrite']}, function(err, res) { + assert.ok(!err, '#addUser error'); + var store = new MongoStore({user: 'user', password: 'pass', db: 'testauth'}); + store.on('connect', function() { + baseTest.apply(this); + }); + }); + }); }); // Basic functionality test function baseTest() { - var self = this; - // #set() - self.set('123', {cookie: {maxAge: 2000}, name: 'name'}, function(err, ok) { - assert.ok(!err, '#set() got an error'); - assert.ok(ok, '#set() is not ok'); + var self = this; + // #set() + self.set('123', {cookie: {maxAge: 2000}, name: 'name'}, function(err, ok) { + assert.ok(!err, '#set() got an error'); + assert.ok(ok, '#set() is not ok'); - // #get() - self.get('123', function(err, data) { - assert.ok(!err, '#get() got an error'); - assert.deepEqual({ - cookie: {maxAge: 2000}, - name: 'name' - }, data); + // #get() + self.get('123', function(err, data) { + assert.ok(!err, '#get() got an error'); + assert.deepEqual({ + cookie: {maxAge: 2000}, + name: 'name' + }, data); - // #set null - self.set('123', {cookie: {maxAge: 2000}, name: 'name'}, function() { - self.destroy('123', function() { - done(); - }); - }); - }); - }); + // #set null + self.set('123', {cookie: {maxAge: 2000}, name: 'name'}, function() { + self.destroy('123', function() { + done(); + }); + }); + }); + }); } From cb20f017a8a646c0b11cd4286dc7563315219a47 Mon Sep 17 00:00:00 2001 From: da440dil Date: Mon, 26 Apr 2021 16:38:08 +0300 Subject: [PATCH 2/3] fix format --- lib/index.js | 295 +++++++++++++++++++++++++-------------------------- test.js | 98 ++++++++--------- 2 files changed, 196 insertions(+), 197 deletions(-) diff --git a/lib/index.js b/lib/index.js index 21576d6..f2a19cd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,141 +8,141 @@ * One day in milliseconds. */ - var oneDay = 86400 * 1000; - - /** - * Module dependencies - */ - - var inherits = require('util').inherits, - events = require('events'), - MongoClient = require('mongodb').MongoClient, - Db = require('mongodb').Db; - - var createIndexes = function(collection, callback) { - var times = 2; - function done(err) { - if (err) return callback(err); - if (--times < 1) { - callback(); - } - } - collection.createIndex({ttl: 1}, {expireAfterSeconds: 0}, done); - collection.createIndex({sid: 1}, {unique: true}, done); - }; - - var makeConnectionString = function(options) { - return 'mongodb://' + (options.host || '127.0.0.1') + ':' + - (options.port || '27017') + '/' + - (options.db || 'test') + - '?ssl=' + (options.ssl || false); - }; - - /** - * Return the `MongoStore` extending `connect`'s session Store. - * - * @param {object} connect - * @return {Function} - * @api public - */ - module.exports = function(connect) { - - /** +var oneDay = 86400 * 1000; + +/** + * Module dependencies + */ + +var inherits = require('util').inherits, + events = require('events'), + MongoClient = require('mongodb').MongoClient, + Db = require('mongodb').Db; + +var createIndexes = function(collection, callback) { + var times = 2; + function done(err) { + if (err) return callback(err); + if (--times < 1) { + callback(); + } + } + collection.createIndex({ttl: 1}, {expireAfterSeconds: 0}, done); + collection.createIndex({sid: 1}, {unique: true}, done); +}; + +var makeConnectionString = function(options) { + return 'mongodb://' + (options.host || '127.0.0.1') + ':' + + (options.port || '27017') + '/' + + (options.db || 'test') + + '?ssl=' + (options.ssl || false); +}; + +/** + * Return the `MongoStore` extending `connect`'s session Store. + * + * @param {object} connect + * @return {Function} + * @api public + */ +module.exports = function(connect) { + + /** * Connect's Store. */ - - var Store = connect.Store || connect.session.Store; - - /** + + var Store = connect.Store || connect.session.Store; + + /** * Initialize MongoStore with the given `options`. * * @param {Object} options * @api public */ - - function MongoStore(options) { - var self = this; - - options = options || {}; - Store.call(this, options); - this.ttl = options.ttl; - - var emitConnect = function(err) { - if (err) throw err; - self.emit('connect'); - }; - - if (options.db && (typeof options.db !== 'string')) { - this.col = options.db.collection(options.collection || 'sessions'); - createIndexes(this.col, emitConnect); - } else { - var dbUrl = options.url || makeConnectionString(options); - var opts = {}; - if (options.user && options.password) { - opts.auth = { - user: options.user, - password: options.password - }; - } - MongoClient.connect(dbUrl, opts, function(err, client) { - if (err) throw err; - var db; - if (client instanceof MongoClient) { - var dbName = 'test'; - if (typeof options.db === 'string') { - dbName === options.db; - } else if ( - client.s && client.s.options && - typeof client.s.options.dbName === 'string' - ) { - dbName = client.s.options.dbName; - } - db = client.db(dbName); - } else if (client instanceof Db) { - db = client; - } else { - throw new Error('mongodb connect failed'); - } - self.col = db.collection(options.collection || 'sessions'); - if ( - options.user && options.password && - typeof db.authenticate === 'function' - ) { - db.authenticate(options.user, options.password, function(err, res) { - if (err) throw err; - if (!res) throw new Error('mongodb authentication failed'); - createIndexes(self.col, emitConnect); - }); - } else { - createIndexes(self.col, emitConnect); - } - }); - } - }; - - /** + + function MongoStore(options) { + var self = this; + + options = options || {}; + Store.call(this, options); + this.ttl = options.ttl; + + var emitConnect = function(err) { + if (err) throw err; + self.emit('connect'); + }; + + if (options.db && (typeof options.db !== 'string')) { + this.col = options.db.collection(options.collection || 'sessions'); + createIndexes(this.col, emitConnect); + } else { + var dbUrl = options.url || makeConnectionString(options); + var opts = {}; + if (options.user && options.password) { + opts.auth = { + user: options.user, + password: options.password + }; + } + MongoClient.connect(dbUrl, opts, function(err, client) { + if (err) throw err; + var db; + if (client instanceof MongoClient) { + var dbName = 'test'; + if (typeof options.db === 'string') { + dbName === options.db; + } else if ( + client.s && client.s.options && + typeof client.s.options.dbName === 'string' + ) { + dbName = client.s.options.dbName; + } + db = client.db(dbName); + } else if (client instanceof Db) { + db = client; + } else { + throw new Error('mongodb connect failed'); + } + self.col = db.collection(options.collection || 'sessions'); + if ( + options.user && options.password && + typeof db.authenticate === 'function' + ) { + db.authenticate(options.user, options.password, function(err, res) { + if (err) throw err; + if (!res) throw new Error('mongodb authentication failed'); + createIndexes(self.col, emitConnect); + }); + } else { + createIndexes(self.col, emitConnect); + } + }); + } + }; + + /** * Inherit from `Store`. */ - - inherits(MongoStore, Store); - - /** + + inherits(MongoStore, Store); + + /** * Attempt to fetch session by the given `sid`. * * @param {String} sid * @param {Function} fn * @api public */ - - MongoStore.prototype.get = function(sid, fn){ - this.col.findOne({sid: sid}, {projection: {_id: 0, ttl: 0, sid: 0}}, function(err, data) { - if (err) return fn(err); - if (!data) return fn(); - return fn(null, data); - }); - }; - - /** + + MongoStore.prototype.get = function(sid, fn) { + this.col.findOne({sid: sid}, {projection: {_id: 0, ttl: 0, sid: 0}}, function(err, data) { + if (err) return fn(err); + if (!data) return fn(); + return fn(null, data); + }); + }; + + /** * Commit the given `sess` object associated with the given `sid`. * * @param {String} sid @@ -150,37 +150,36 @@ * @param {Function} fn * @api public */ - - MongoStore.prototype.set = function(sid, sess, fn) { - var maxAge = sess.cookie.maxAge; - - sess.sid = sid; - sess.ttl = new Date((this.ttl || ('number' === typeof maxAge - ? maxAge : oneDay)) + Date.now()); - - this.col.replaceOne({sid: sid}, sess, {upsert: true}) - .then(function() { - fn(null, true); - }) - .catch(fn); - }; - - /** + + MongoStore.prototype.set = function(sid, sess, fn) { + var maxAge = sess.cookie.maxAge; + + sess.sid = sid; + sess.ttl = new Date((this.ttl || ('number' === typeof maxAge + ? maxAge : oneDay)) + Date.now()); + + this.col.replaceOne({sid: sid}, sess, {upsert: true}) + .then(function() { + fn(null, true); + }) + .catch(fn); + }; + + /** * Destroy the session associated with the given `sid`. * * @param {String} sid * @param {Function} fn * @api public */ - - MongoStore.prototype.destroy = function(sid, fn) { - this.col.deleteOne({sid: sid}) - .then(function() { - fn(); - }) - .catch(fn); - }; - - return MongoStore; - }; - \ No newline at end of file + + MongoStore.prototype.destroy = function(sid, fn) { + this.col.deleteOne({sid: sid}) + .then(function() { + fn(); + }) + .catch(fn); + }; + + return MongoStore; +}; diff --git a/test.js b/test.js index 58c9dbc..8c84b17 100644 --- a/test.js +++ b/test.js @@ -2,76 +2,76 @@ * Module dependencies. */ - var assert = require('assert'), - connect = require('connect'), - MongoStore = require('./lib')(connect), - MongoClient = require('mongodb').MongoClient; +var assert = require('assert'), + connect = require('connect'), + MongoStore = require('./lib')(connect), + MongoClient = require('mongodb').MongoClient; var testsCount = 3; -var done = (function () { - var count = 0; - return function () { - ++count; - if (count == testsCount) { - console.log('done'); - process.exit(0); - } - }; +var done = (function() { + var count = 0; + return function() { + ++count; + if (count == testsCount) { + console.log('done'); + process.exit(0); + } + }; })(); var store = new MongoStore; // simple test store.on('connect', function() { - baseTest.apply(this); + baseTest.apply(this); }); // test with initialized db object MongoClient.connect('mongodb://127.0.0.1:27017/testdb', function(err, client) { - var db = client instanceof MongoClient ? client.db('testdb') : client; - var store = new MongoStore({db: db}); - store.on('connect', function() { - baseTest.apply(this); - }); + var db = client instanceof MongoClient ? client.db('testdb') : client; + var store = new MongoStore({ db: db }); + store.on('connect', function() { + baseTest.apply(this); + }); }); // test mongodb auth MongoClient.connect('mongodb://127.0.0.1:27017/testauth', function(err, client) { - var db = client instanceof MongoClient ? client.db('testauth'): client; - db.removeUser('user', function() { - db.addUser('user', 'pass', {roles: ['readWrite']}, function(err, res) { - assert.ok(!err, '#addUser error'); - var store = new MongoStore({user: 'user', password: 'pass', db: 'testauth'}); - store.on('connect', function() { - baseTest.apply(this); - }); - }); - }); + var db = client instanceof MongoClient ? client.db('testauth') : client; + db.removeUser('user', function() { + db.addUser('user', 'pass', { roles: ['readWrite'] }, function(err, res) { + assert.ok(!err, '#addUser error'); + var store = new MongoStore({ user: 'user', password: 'pass', db: 'testauth' }); + store.on('connect', function() { + baseTest.apply(this); + }); + }); + }); }); // Basic functionality test function baseTest() { - var self = this; - // #set() - self.set('123', {cookie: {maxAge: 2000}, name: 'name'}, function(err, ok) { - assert.ok(!err, '#set() got an error'); - assert.ok(ok, '#set() is not ok'); + var self = this; + // #set() + self.set('123', { cookie: { maxAge: 2000 }, name: 'name' }, function(err, ok) { + assert.ok(!err, '#set() got an error'); + assert.ok(ok, '#set() is not ok'); - // #get() - self.get('123', function(err, data) { - assert.ok(!err, '#get() got an error'); - assert.deepEqual({ - cookie: {maxAge: 2000}, - name: 'name' - }, data); + // #get() + self.get('123', function(err, data) { + assert.ok(!err, '#get() got an error'); + assert.deepEqual({ + cookie: { maxAge: 2000 }, + name: 'name' + }, data); - // #set null - self.set('123', {cookie: {maxAge: 2000}, name: 'name'}, function() { - self.destroy('123', function() { - done(); - }); - }); - }); - }); + // #set null + self.set('123', { cookie: { maxAge: 2000 }, name: 'name' }, function() { + self.destroy('123', function() { + done(); + }); + }); + }); + }); } From 33b21e59a86b8ebb85caf0055618710f38da616c Mon Sep 17 00:00:00 2001 From: da440dil Date: Mon, 26 Apr 2021 16:49:05 +0300 Subject: [PATCH 3/3] update get method --- lib/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index f2a19cd..820d90d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -135,11 +135,12 @@ module.exports = function(connect) { */ MongoStore.prototype.get = function(sid, fn) { - this.col.findOne({sid: sid}, {projection: {_id: 0, ttl: 0, sid: 0}}, function(err, data) { - if (err) return fn(err); - if (!data) return fn(); - return fn(null, data); - }); + this.col.findOne({sid: sid}, {projection: {_id: 0, ttl: 0, sid: 0}}) + .then(function(data) { + if (!data) return fn(); + return fn(null, data); + }) + .catch(fn); }; /**