<commit_before>var _ = require('lodash');
var Router = require('react-router');
var ServerCookies = require('./serverCookies');

var HEADERS_TO_IGNORE = [
  'accept',
  'accept-encoding',
  'host',
  'connection'
];

module.exports = function (options) {
  options = options || {};

  if (!options.routes) {
    throw new Error('routes is required');
  }

  var Marty = options.marty || require('marty');

  Marty.CookieStateSource.setCookieFactory(function (context) {
    return new ServerCookies(context.req, context.res);
  });

  Marty.LocationStateSource.setLocationFactory(function (context) {
    return _.pick(context.req, 'url', 'protocol', 'query', 'path', 'hostname');
  });

  Marty.HttpStateSource.addHook({
    priority: 0.00000000001,
    before: function (req) {
      var context = this.context;

      if (!context || !context.req) {
        return;
      }

      // Don't change fully qualified urls
      if (!/^https?:\/\//.test(req.url)) {
        req.url = getBaseUrl(context.req) + req.url;
      }

      // Add all headers from original request
      _.extend(req.headers, headers());

      function getBaseUrl(req) {
        return req.protocol + '://' + req.get('host');
      }

      function headers() {
        return _.omit(context.req.headers, HEADERS_TO_IGNORE);
      }
    }
  });

  return function (req, res, next) {
    var router = Router.create({
      location: req.url,
      routes: options.routes,
      onAbort: onAbort
    });

    if (!router.match(req.url)) {
      return next();
    }

    router.run(function (Handler, state) {
      var context = Marty.createContext();

      context.req = req;
      context.res = res;

      var renderOptions = {
        type: Handler,
        context: context,
        props: state.params,
        timeout: options.timeout
      };

      Marty
        .renderToString(renderOptions)<commit_msg>Update Marty.createContext call for martyjs/marty#278<commit_after>var _ = require('lodash');
var Router = require('react-router');
var ServerCookies = require('./serverCookies');

var HEADERS_TO_IGNORE = [
  'accept',
  'accept-encoding',
  'host',
  'connection'
];

module.exports = function (options) {
  options = options || {};

  if (!options.routes) {
    throw new Error('routes is required');
  }

  var Marty = options.marty || require('marty');

  Marty.CookieStateSource.setCookieFactory(function (context) {
    return new ServerCookies(context.req, context.res);
  });

  Marty.LocationStateSource.setLocationFactory(function (context) {
    return _.pick(context.req, 'url', 'protocol', 'query', 'path', 'hostname');
  });

  Marty.HttpStateSource.addHook({
    priority: 0.00000000001,
    before: function (req) {
      var context = this.context;

      if (!context || !context.req) {
        return;
      }

      // Don't change fully qualified urls
      if (!/^https?:\/\//.test(req.url)) {
        req.url = getBaseUrl(context.req) + req.url;
      }

      // Add all headers from original request
      _.extend(req.headers, headers());

      function getBaseUrl(req) {
        return req.protocol + '://' + req.get('host');
      }

      function headers() {
        return _.omit(context.req.headers, HEADERS_TO_IGNORE);
      }
    }
  });

  return function (req, res, next) {
    var router = Router.create({
      location: req.url,
      routes: options.routes,
      onAbort: onAbort
    });

    if (!router.match(req.url)) {
      return next();
    }

    router.run(function (Handler, state) {
      var context = Marty.createContext({req: req, res: res});

      var renderOptions = {
        type: Handler,
        context: context,
        props: state.params,
        timeout: options.timeout
      };

      Marty
        .renderToString(renderOptions)
{'fraction_non_alphanumeric': 0.1456848772763262, 'fraction_numerical': 0.007125890736342043, 'mean_word_length': 2.6198662846227316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5148413', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1149, 'n_words': 254}