Skip to content

Add promise support for sessionStore functions #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,27 @@ function session(options) {

function reload(callback) {
debug('reloading %s', this.id)
_reload.call(this, function () {
wrapmethods(req.session)
callback.apply(this, arguments)
})
if (callback) {
_reload.call(this, function () {
wrapmethods(req.session)
callback.apply(this, arguments)
})
return
}

return _reload.call(this)
.then(function() {
return wrapmethods(req.session)
})
.catch(function(err) {
return err
})
}

function save() {
debug('saving %s', this.id);
savedHash = hash(this);
_save.apply(this, arguments);
return _save.apply(this, arguments);
}

Object.defineProperty(sess, 'reload', {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"devDependencies": {
"after": "0.8.2",
"bluebird": "3.5.3",
"cookie-parser": "1.4.3",
"eslint": "3.19.0",
"eslint-plugin-markdown": "1.0.0",
Expand Down
106 changes: 82 additions & 24 deletions session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,30 @@ defineMethod(Session.prototype, 'resetMaxAge', function resetMaxAge() {
/**
* Save the session data with optional callback `fn(err)`.
*
* @param {Function} fn
* @return {Session} for chaining
* @param {Function} [fn]
* @return {Promise}
* @api public
*/

defineMethod(Session.prototype, 'save', function save(fn) {
this.req.sessionStore.set(this.id, this, fn || function(){});
return this;
});
defineMethod(Session.prototype, 'save', function save (fn) {
if (fn) {
this.req.sessionStore.set(this.id, this, fn)
return
}

if (!fn && !global.Promise) {
this.req.sessionStore.set(this.id, this, function(){})
return
}

var sess = this
return new Promise(function (resolve, reject) {
sess.req.sessionStore.set(sess.id, sess, function (err) {
if (err) reject(err)
resolve()
})
})
})

/**
* Re-loads the session data _without_ altering
Expand All @@ -80,23 +95,38 @@ defineMethod(Session.prototype, 'save', function save(fn) {
* `req.session` property will be a new `Session` object,
* although representing the same session.
*
* @param {Function} fn
* @return {Session} for chaining
* @param {Function} [fn]
* @return {Promise}
* @api public
*/

defineMethod(Session.prototype, 'reload', function reload(fn) {
defineMethod(Session.prototype, 'reload', function reload (fn) {
var req = this.req
var store = this.req.sessionStore
if (fn) {
store.get(this.id, function (err, sess) {
if (err) return fn(err)
if (!sess) return fn(new Error('failed to load session'))
store.createSession(req, sess)
fn()
})
return
}

store.get(this.id, function(err, sess){
if (err) return fn(err);
if (!sess) return fn(new Error('failed to load session'));
store.createSession(req, sess);
fn();
});
return this;
});
if (!fn && !global.Promise) {
throw new Error('must use callback without promises')
}

var parent = this
return new Promise(function (resolve, reject) {
store.get(parent.id, function (err, sess) {
if (err) reject(err)
if (!sess) reject(new Error('failed to load session'))
store.createSession(req, sess)
resolve()
})
})
})

/**
* Destroy `this` session.
Expand All @@ -107,10 +137,24 @@ defineMethod(Session.prototype, 'reload', function reload(fn) {
*/

defineMethod(Session.prototype, 'destroy', function destroy(fn) {
delete this.req.session;
this.req.sessionStore.destroy(this.id, fn);
return this;
});
delete this.req.session
if (fn) {
this.req.sessionStore.destroy(this.id, fn)
return
}

if (!fn && !global.Promise) {
throw new Error('must use callback without promises')
}

var parent = this
return new Promise(function (resolve, reject) {
parent.req.sessionStore.destroy(parent.id, function(err) {
if (err) reject(err)
resolve()
})
})
})

/**
* Regenerate this request's session.
Expand All @@ -121,9 +165,23 @@ defineMethod(Session.prototype, 'destroy', function destroy(fn) {
*/

defineMethod(Session.prototype, 'regenerate', function regenerate(fn) {
this.req.sessionStore.regenerate(this.req, fn);
return this;
});
if (fn) {
this.req.sessionStore.regenerate(this.req, fn)
return
}

if (!fn && !global.Promise) {
throw new Error('must use callback without promises')
}

var sess = this
return new Promise(function (resolve, reject) {
sess.req.sessionStore.regenerate(sess.req, function(err) {
if (err) reject(err)
resolve()
})
})
})

/**
* Helper function for creating a method on a prototype.
Expand Down
Loading