<commit_before>var explain = require('explain-error')
var series = require('run-series')
var logHttp = require('log-http')
var assert = require('assert')
var open = require('open')
var http = require('http')
var path = require('path')
var pump = require('pump')
var fs = require('fs')

var findManifest = require('../lib/find-manifest')
var zlibMaybe = require('../lib/gzip-maybe')
var Sse = require('../lib/sse')
var bankai = require('../')

module.exports = start

function start (entry, argv, done) {
  var log = argv.log

  log.debug('running command: start')
  argv.watch = true

  var staticAsset = new RegExp('/' + argv.assets)
  var address = argv.address
  var port = argv.port
  var assets, sse

  series([
    getManifest,   // detect manifest.json files
    startBankai,   // kick off bankai
    startServer    // serve bankai over http
  ])

  function getManifest (next) {
    findManifest(entry, function (_, filename) {
      if (filename) argv.html.manifest = filename
      next()
    })
  }

  function startBankai (next) {
    assets = bankai(entry, argv)
    sse = Sse(assets)

    assets.on('js-bundle', function () {
      log.info('bundle:js')
    })

    assets.on('css-bundle', function () {
      log.info('bundle:css')
    })

    next()
  }

  function startServer () {
    var server = http.createServer(handler)
    server.listen(port, address, onlisten)

    var stats = logHttp(server)
    stats.on('data', function (level, data) {
      log[level](data)
    })
  }

  function handler (req, res) {
    var url = req.url
    log.debug('received request on url: ' + url)
    if (url === '/') {
      if (fs.existsSync(path.join(process.cwd(), 'index.html'))) {
        fs.createReadStream(path.join(process.cwd(), 'index.html')).pipe(res)
      } else {
        pump(assets.html(req, res), res, function (err) {
          if (err) log.error(err)
        })
      }
    } else if (url === '/sse') {
      sse(req, res)
    } else if (url === '/bundle.js') {
      assets.js(req, res).pipe(zlibMaybe(req, res)).pipe(res)
    } else if (url === '/sw.js') {
      res.writeHead(200, { 'content-type': 'application/javascript' })
      fs.createReadStream(path.join(path.dirname(entry), 'sw.js')).pipe(res)
    } else if (url === '/bundle.css') {
      assets.css(req, res).pipe(zlibMaybe(req, res)).pipe(res)
    } else if (url === '/manifest.json') {
      assert.ok(argv.html.manifest, 'bankai.start: no manifest file found')
      fs.createReadStream(argv.html.manifest).pipe(zlibMaybe(req, res)).pipe(res)
    } else if ((url === '/' || req.headers['accept'].indexOf('html') > 0) && req.url.indexOf('favicon') === -1) {
      if (fs.existsSync(path.join(process.cwd(), 'index.html'))) {<commit_msg>Add CORS headers & OPTIONS response (#228)<commit_after>var explain = require('explain-error')
var series = require('run-series')
var logHttp = require('log-http')
var assert = require('assert')
var open = require('open')
var http = require('http')
var path = require('path')
var pump = require('pump')
var fs = require('fs')

var findManifest = require('../lib/find-manifest')
var zlibMaybe = require('../lib/gzip-maybe')
var Sse = require('../lib/sse')
var bankai = require('../')

module.exports = start

function start (entry, argv, done) {
  var log = argv.log

  log.debug('running command: start')
  argv.watch = true

  var staticAsset = new RegExp('/' + argv.assets)
  var address = argv.address
  var port = argv.port
  var assets, sse

  series([
    getManifest,   // detect manifest.json files
    startBankai,   // kick off bankai
    startServer    // serve bankai over http
  ])

  function getManifest (next) {
    findManifest(entry, function (_, filename) {
      if (filename) argv.html.manifest = filename
      next()
    })
  }

  function startBankai (next) {
    assets = bankai(entry, argv)
    sse = Sse(assets)

    assets.on('js-bundle', function () {
      log.info('bundle:js')
    })

    assets.on('css-bundle', function () {
      log.info('bundle:css')
    })

    next()
  }

  function startServer () {
    var server = http.createServer(handler)
    server.listen(port, address, onlisten)

    var stats = logHttp(server)
    stats.on('data', function (level, data) {
      log[level](data)
    })
  }

  function handler (req, res) {
    var url = req.url
    log.debug('received request on url: ' + url)

    res.setHeader('access-control-allow-origin', '*')
    res.setHeader('access-control-allow-method', '*')
    res.setHeader('access-control-allow-header', '*')
    res.setHeader('access-control-allow-credentials', 'true')

    if (req.method === 'OPTIONS') {
      res.statusCode = 204
      return res.end()
    }

    if (url === '/') {
      if (fs.existsSync(path.join(process.cwd(), 'index.html'))) {
        fs.createReadStream(path.join(process.cwd(), 'index.html')).pipe(res)
      } else {
        pump(assets.html(req, res), res, function (err) {
          if (err) log.error(err)
        })
      }
    } else if (url === '/sse') {
      sse(req, res)
    } else if (url === '/bundle.js') {
      assets.js(req, res).pipe(zlibMaybe(req, res)).pipe(res)
    } else if (url === '/sw.js') {
      res.writeHead(200, { 'content-type': 'application/javascript' })
      fs.createReadStream(path.join(path.dirname(entry), 'sw.js')).pipe(res)
    } else if (url === '/bundle.css') {
      assets.css(req, res).pipe(zlibMaybe(req, res)).pipe(res)
    } else if (url === '/manifest.json') {
      assert.ok(argv.html.manifest, 'bankai.start: no manifest file found')
      fs.createReadStream(argv.html.manifest).pipe(zlibMaybe(req, res)).pipe(res)
    } else if ((url === '/' || req.headers['accept'].indexOf('html') > 0) && req.url.indexOf('favicon') === -1) {
      if (fs.existsSync(path.join(process.cwd(), 'index.html'))) {
{'fraction_non_alphanumeric': 0.18044077134986225, 'fraction_numerical': 0.0027548209366391185, 'mean_word_length': 3.0340277777777778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5232791', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1918, 'n_words': 423}