lib/io.js
function applyMiddleware(socket, req, done) {
  // A stub for the `res` argument express usually provides.
  var res = {};

  // Map the socket.io request into a `req` like stub as well.
  var params = clone(req);
  assign(params, {
    session_id: socket.decoded_token.session_id,
    deployment_id: socket.decoded_token.deployment_id
  });

  req = {
    params: params,
    query: {},
    body: {}
  };

  // Yay! Now we can just use all of our existing middleware!
  series({
    'Load token data': partial(Token.load, socket.decoded_token, req),
    'Add typed param getters': partial(typedParams, req, res),
    'Parse the namespace param if presented': function(next) {
      unpackNamespaceParam(req, res, next, req.params.ns);
    },
    'The collection must already exist': partial(collectionRequired, req, res),
    'Parse sample options': partial(sampleOptions, req, res)
  }, function(err) {
    if (err) {
      return done(err);
    }
    debug('middleware applied successfully', req);
    done(null, req);
  });
}
function applyMiddleware(socket, req, done) {
  // A stub for the `res` argument express usually provides.
  var res = {};

  // Map the socket.io request into a `req` like stub as well.
  var params = clone(req);
  assign(params, {
    session_id: socket.decoded_token.session_id,
    deployment_id: socket.decoded_token.deployment_id
  });

  req = {
    params: params,
    query: {},
    body: {}
  };

  // Yay! Now we can just use all of our existing middleware!
  series({
    'Load token data': partial(Token.load, socket.decoded_token, req),
    'Add typed param getters': partial(typedParams, req, res),
    'Parse the namespace param if presented': function(next) {
      unpackNamespaceParam(req, res, next, req.params.ns);
    },
    'The collection must already exist': partial(collectionRequired, req, res),
    'Parse sample options': partial(sampleOptions, req, res)
  }, function(err) {
    if (err) {
      return done(err);
    }
    debug('middleware applied successfully', req);
    done(null, req);
  });
}
A small helper which allows us to just use all of our
existing `./middleware/*.js` and `./params/*.js`
targeted at express with socket.io.  This is in
an effort to reuse as much code as possible and
lower the probability of "oops" bugs, as in
"Oops! I changed it in middleware/ns.js and tested
it, but I forgot got to update it in io.js :(".

@see https://medium.com/@osklopez/sharing-express-session-data-with-socket-io-190b4237f60e

@param {socket.io.Socket} socket - http://socket.io/docs/server-api/#socket
@param {Object} req - Request data sent by the client.
@param {Function} done