From 35d6c0a17414d5b785ea57217aa1bcad8ed68906 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sun, 9 Feb 2025 20:32:20 -0500 Subject: [PATCH] refactor: replace custom defer function with setImmediate --- index.js | 18 ++++-------------- session/memory.js | 22 ++++++---------------- test/support/smart-store.js | 11 +++-------- 3 files changed, 13 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index d41b2378..9ed83951 100644 --- a/index.js +++ b/index.js @@ -56,16 +56,6 @@ var warning = 'Warning: connect.session() MemoryStore is not\n' + 'designed for a production environment, as it will leak\n' + 'memory, and will not scale past a single process.'; -/** - * Node.js 0.8+ async implementation. - * @private - */ - -/* istanbul ignore next */ -var defer = typeof setImmediate === 'function' - ? setImmediate - : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) } - /** * Setup session store with the given `options`. * @@ -247,7 +237,7 @@ function session(options) { try { setcookie(res, name, req.sessionID, secrets[0], req.session.cookie.data) } catch (err) { - defer(next, err) + setImmediate(next, err) } }); @@ -317,7 +307,7 @@ function session(options) { debug('destroying'); store.destroy(req.sessionID, function ondestroy(err) { if (err) { - defer(next, err); + setImmediate(next, err); } debug('destroyed'); @@ -342,7 +332,7 @@ function session(options) { if (shouldSave(req)) { req.session.save(function onsave(err) { if (err) { - defer(next, err); + setImmediate(next, err); } writeend(); @@ -354,7 +344,7 @@ function session(options) { debug('touching'); store.touch(req.sessionID, req.session, function ontouch(err) { if (err) { - defer(next, err); + setImmediate(next, err); } debug('touched'); diff --git a/session/memory.js b/session/memory.js index 11ed686c..2008939a 100644 --- a/session/memory.js +++ b/session/memory.js @@ -16,16 +16,6 @@ var Store = require('./store') var util = require('util') -/** - * Shim setImmediate for node.js < 0.10 - * @private - */ - -/* istanbul ignore next */ -var defer = typeof setImmediate === 'function' - ? setImmediate - : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) } - /** * Module exports. */ @@ -68,7 +58,7 @@ MemoryStore.prototype.all = function all(callback) { } } - callback && defer(callback, null, sessions) + callback && setImmediate(callback, null, sessions) } /** @@ -80,7 +70,7 @@ MemoryStore.prototype.all = function all(callback) { MemoryStore.prototype.clear = function clear(callback) { this.sessions = Object.create(null) - callback && defer(callback) + callback && setImmediate(callback) } /** @@ -92,7 +82,7 @@ MemoryStore.prototype.clear = function clear(callback) { MemoryStore.prototype.destroy = function destroy(sessionId, callback) { delete this.sessions[sessionId] - callback && defer(callback) + callback && setImmediate(callback) } /** @@ -104,7 +94,7 @@ MemoryStore.prototype.destroy = function destroy(sessionId, callback) { */ MemoryStore.prototype.get = function get(sessionId, callback) { - defer(callback, null, getSession.call(this, sessionId)) + setImmediate(callback, null, getSession.call(this, sessionId)) } /** @@ -118,7 +108,7 @@ MemoryStore.prototype.get = function get(sessionId, callback) { MemoryStore.prototype.set = function set(sessionId, session, callback) { this.sessions[sessionId] = JSON.stringify(session) - callback && defer(callback) + callback && setImmediate(callback) } /** @@ -153,7 +143,7 @@ MemoryStore.prototype.touch = function touch(sessionId, session, callback) { this.sessions[sessionId] = JSON.stringify(currentSession) } - callback && defer(callback) + callback && setImmediate(callback) } /** diff --git a/test/support/smart-store.js b/test/support/smart-store.js index 8b224fdd..5e1bfb2f 100644 --- a/test/support/smart-store.js +++ b/test/support/smart-store.js @@ -3,11 +3,6 @@ var session = require('../../') var util = require('util') -/* istanbul ignore next */ -var defer = typeof setImmediate === 'function' - ? setImmediate - : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) } - module.exports = SmartStore function SmartStore () { @@ -19,7 +14,7 @@ util.inherits(SmartStore, session.Store) SmartStore.prototype.destroy = function destroy (sid, callback) { delete this.sessions[sid] - defer(callback, null) + setImmediate(callback, null) } SmartStore.prototype.get = function get (sid, callback) { @@ -45,10 +40,10 @@ SmartStore.prototype.get = function get (sid, callback) { } } - defer(callback, null, sess) + setImmediate(callback, null, sess) } SmartStore.prototype.set = function set (sid, sess, callback) { this.sessions[sid] = JSON.stringify(sess) - defer(callback, null) + setImmediate(callback, null) }