<commit_before>var React = require('react')
var serialize = require('serialize-javascript')
var log = require('debug')('bshed:client:renderer')
var {FluxibleComponent} = require('fluxible/addons')

var NavigateAction = require('../app/application/actions/NavigateAction')
var Html = require('./Html')
var app = require('../app')

module.exports = renderer
function* renderer (options) {
  var {url, host, protocol, cookie, assets} = options

  log(`creating context with url ${url}`)
  var context = app.createContext({
    protocol,
    cookie,
    host,
    url
  })

  log('running router')
  var {Handler, state} = yield runRouter(context)

  try {
    log('executing navigate action')
    yield context.executeAction(NavigateAction, state)
  } catch (err) {
    console.error('Error executing navigate action', err)
    throw err
  }

  return render({context, Handler, assets})
}

function runRouter (context) {
  return new Promise(resolve => {
    context.getActionContext().router.run((Handler, state) => {
      resolve({Handler, state})
    })
  })
}

function render ({context, Handler, assets}={}) {
  log('generating component context')
  var componentContext = context.getComponentContext()

  log('generating BSHED')
  var BSHED = `window.BSHED=${serialize(app.dehydrate(context))}`

  log('generating markup')
  var markup = React.renderToString(
    <FluxibleComponent context={componentContext}>
      <Handler/>
    </FluxibleComponent>
  )

  log('generating html')
  var html = React.renderToStaticMarkup(
    <FluxibleComponent context={componentContext}>
      <Html markup={markup} assets={assets} BSHED={BSHED} context={componentContext}/>
    </FluxibleComponent>
  )

  log('render finished')
  return {body: `<!DOCTYPE html>${html}`, type: 'html', status: 200}
}
<commit_msg>Simplify renderer<commit_after>const React = require('react')
const serialize = require('serialize-javascript')
const log = require('debug')('bshed:client:renderer')
const {FluxibleComponent} = require('fluxible/addons')

const NavigateActions = require('../actions/NavigateActions')
const Html = require('./Html')
const app = require('../app')

module.exports = async function renderer ({rootUrl, url, cookie, assets}={}) {
  log(`creating context with url ${url}`)
  const context = app.createContext({
    rootUrl,
    cookie,
    url
  })

  log('running router')
  const {Root, state} = await runRouter(context)

  log('executing navigate action')
  await context.executeAction(NavigateActions.router, state)

  log('running render')
  const result = render({context, Root, assets})

  return {
    body: result,
    type: 'html',
    status: 200
  }
}

/**
 * Run router
 * @param {Object} context Application context
 * @returns {Promise} Promise of resulting Handler and state
 */
function runRouter (context) {
  return new Promise(resolve => {
    context.router.run((Root, state) => {
      resolve({
        Root,
        state
      })
    })
  })
}

/**
 * Render app
 * @returns {Object} Resulting body, type, and status
 */
function render ({context, Root, assets}={}) {
  log('generating component context')
  const componentContext = context.getComponentContext()

  log('generating BSHED')
  const BSHED = `window.BSHED=${serialize(app.dehydrate(context))}`

  log('generating markup')
  const markup = React.renderToString(
    <FluxibleComponent context={componentContext}>
      <Root/>
    </FluxibleComponent>
  )

  log('generating html')
  const html = React.renderToStaticMarkup(
    <FluxibleComponent context={componentContext}>
      <Html markup={markup} assets={assets} BSHED={BSHED}/>
    </FluxibleComponent>
  )

  return `<!DOCTYPE html>${html}`
}

{'fraction_non_alphanumeric': 0.14343817787418656, 'fraction_numerical': 0.0016268980477223427, 'mean_word_length': 4.285100286532951, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '6482182', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1092, 'n_words': 273}