index.js
function router(rpc, handlers, req, res) {
  var $       = rpc.$;
  var api     = rpc.api;
  var request = api.handle(req.method, req.url);
  var dom     = domain.create();

  // the request handler doesn't exit
  // TODO: hoist response logic
  if (!request) {
    res.statusCode = 404;
    res.end();
    return;
  }

  var context = new Context(); // TODO: hoist to own module
  var handle  = request.handle;
  var params  = {};

  // TODO: hoist logic to 'union' module
  var key;
  for(key in request.params) params[key] = request.params[key];
  for(key in request.query)  params[key] = request.query[key];

  var future = $.future();

  future.setWritable(res);
  future.setReadable(req);

  // TODO: extract request making logic
  res.setHeader('Transfer-Encoding' , 'chunked');
  res.setHeader('Content-Type'      , 'application/json');

  dom.on('error', function (err) {
    var statusCode = err.statusCode;

    // only catch known errors, re-throw unexpected errors
    if (!statusCode) throw err;

    // TODO: hoist response logic
    res.statusCode = statusCode;
    res.write(JSON.stringify(err));
    res.end();
  });

  // domain should handle all route errors
  dom.run(function () {
    handlers[handle](future, params, context);
  });
}
function router(rpc, handlers, req, res) {
  var $       = rpc.$;
  var api     = rpc.api;
  var request = api.handle(req.method, req.url);
  var dom     = domain.create();

  // the request handler doesn't exit
  // TODO: hoist response logic
  if (!request) {
    res.statusCode = 404;
    res.end();
    return;
  }

  var context = new Context(); // TODO: hoist to own module
  var handle  = request.handle;
  var params  = {};

  // TODO: hoist logic to 'union' module
  var key;
  for(key in request.params) params[key] = request.params[key];
  for(key in request.query)  params[key] = request.query[key];

  var future = $.future();

  future.setWritable(res);
  future.setReadable(req);

  // TODO: extract request making logic
  res.setHeader('Transfer-Encoding' , 'chunked');
  res.setHeader('Content-Type'      , 'application/json');

  dom.on('error', function (err) {
    var statusCode = err.statusCode;

    // only catch known errors, re-throw unexpected errors
    if (!statusCode) throw err;

    // TODO: hoist response logic
    res.statusCode = statusCode;
    res.write(JSON.stringify(err));
    res.end();
  });

  // domain should handle all route errors
  dom.run(function () {
    handlers[handle](future, params, context);
  });
}
function router ( rpc , handlers , req , res ) { var $ = rpc . $ ; var api = rpc . api ; var request = api . handle ( req . method , req . url ) ; var dom = domain . create ( ) ; // the request handler doesn't exit // TODO: hoist response logic if ( ! request ) { res . statusCode = 404 ; res . end ( ) ; return ; } var context = new Context ( ) ; // TODO: hoist to own module var handle = request . handle ; var params = { } ; // TODO: hoist logic to 'union' module var key ; for ( key in request . params ) params [ key ] = request . params [ key ] ; for ( key in request . query ) params [ key ] = request . query [ key ] ; var future = $ . future ( ) ; future . setWritable ( res ) ; future . setReadable ( req ) ; // TODO: extract request making logic res . setHeader ( 'Transfer-Encoding' , 'chunked' ) ; res . setHeader ( 'Content-Type' , 'application/json' ) ; dom . on ( 'error' , function ( err ) { var statusCode = err . statusCode ; // only catch known errors, re-throw unexpected errors if ( ! statusCode ) throw err ; // TODO: hoist response logic res . statusCode = statusCode ; res . write ( JSON . stringify ( err ) ) ; res . end ( ) ; } ) ; // domain should handle all route errors dom . run ( function ( ) { handlers [ handle ] ( future , params , context ) ; } ) ; }
make http router use Function.bind to create a router given an rpc, and set of handlers
make http router use Function . bind to create a router given an rpc and set of handlers