<commit_before>nconf = require 'nconf'
nconf.formats.yaml = require 'nconf-yaml'

nconf.file 'user', {
  file: "#{process.cwd()}/breakpad-server.yaml", format: nconf.formats.yaml
}
nconf.file 'system', {
  file: '/etc/breakpad-server.yaml', format: nconf.formats.yaml
}

nconf.defaults
  port: process.env.BREAKPAD_PORT || 1127
  baseUrl: process.env.BASEURL || '/'
  database:
    host: 'localhost'
    dialect: 'sqlite'
    storage: 'database.sqlite'
    logging: no
  customFields:
    files: []
    params: []

moment = require 'moment'
bodyParser = require 'body-parser'
methodOverride = require('method-override')
path = require 'path'
express = require 'express'
exphbs = require 'express-handlebars'
Crashreport = require './model/crashreport'
Symfile = require './model/symfile'

crashreportToApiJson = (crashreport) ->
  json = crashreport.toJSON()

  for k,v of json
    if Buffer.isBuffer(json[k])
      json[k] = "/crashreports/#{json.id}/#{k}"

  json

crashreportToViewJson = (report) ->
  fields = {}
  json = report.toJSON()
  for k,v of json
    if Buffer.isBuffer(json[k])
      fields[k] = { path: "/crashreports/#{report.id}/#{k}" }
    else if v instanceof Date
      fields[k] = moment(v).format('lll')
    else
      fields[k] = if v? then v else 'not present'

  return fields

# initialization: write all symfiles to disk
Symfile.findAll()
  .then (symfiles) ->
    Promise.all(symfiles.map((s) -> Symfile.saveToDisk(s))).then(run)
  .catch (err) ->
    console.log "could not save symfiles to disk: #{err.message}"

run = ->
  app = express()
  breakpad = express()

  breakpad.set 'views', path.resolve(__dirname, '..', 'views')
  breakpad.engine('handlebars', exphbs({defaultLayout: 'main'}))
  breakpad.set 'view engine', 'handlebars'
  breakpad.use bodyParser.json()
  breakpad.use bodyParser.urlencoded({extended: true})
  breakpad.use methodOverride()

  baseUrl = nconf.get('baseUrl')
  port = nconf.get('port')

  app.use baseUrl, breakpad

  # serve minidumps as files
  breakpad.use '/minidumps', express.static('pool/files/minidump')
  breakpad.use '/assets', express.static('node_modules/bootstrap/dist/css')

  # error handler
  app.use (err, req, res, next) ->
    if not err.message?
      console.log 'warning: error thrown without a message'

    res.status(500).send "Bad things happened:<br/> #{err.message || err}"

  breakpad.post '/crashreports', (req, res, next) ->
    Crashreport.createFromRequest req, (err, record) ->
      return next err if err?
      res.json crashreportToApiJson(record)

  breakpad.get '/', (req, res, next) ->
    res.redirect '/crashreports'

  breakpad.get '/crashreports', (req, res, next) ->
    Crashreport.findAll(order: 'createdAt DESC').then (records) ->
      viewReports = records.map(crashreportToViewJson)
      for r in viewReports
        delete r['updatedAt']
      fields = if viewReports.length then Object.keys(viewReports[0]) else []
      res.render 'index',
        title: 'Crash Reports'
        records: viewReports
        fields: fields

  breakpad.get '/crashreports/:id', (req, res, next) ->
    Crashreport.findById(req.params.id).then (report) ->
      if not report?
        return res.send 404, 'Crash report not found'
      Crashreport.getStackTrace report, (err, stackwalk) ->
        return next err if err?
        fields = crashreportToViewJson(report)

        delete fields['id']
        delete fields['updatedAt']

        res.render 'view', {
          title: 'Crash Report'
          stackwalk: stackwalk
          product: fields.product
          version: fields.version
          fields: fields
        }

  breakpad.get '/crashreports/:id/:filefield', (req, res, next) ->
    # download the file for the given id
    Crashreport.findById(req.params.id).then (crashreport) ->
      if not crashreport?
        return res.status(404).send 'Crash report not found'

      contents = crashreport.get(req.params.filefield)

      if not Buffer.isBuffer(contents)
        return res.status(404).send 'Crash report field is not a file'

      res.send(contents)

  breakpad.post '/symfiles', (req, res, next) ->
    Symfile.createFromRequest req, (err, symfile) ->
      return next(err) if err?
      symfileJson = symfile.toJSON()
      delete symfileJson.contents
      res.json symfileJson

  app.listen port
  console.log "Listening on port #{port}"<commit_msg>Add route to show raw stackwalk<commit_after>nconf = require 'nconf'
nconf.formats.yaml = require 'nconf-yaml'

nconf.file 'user', {
  file: "#{process.cwd()}/breakpad-server.yaml", format: nconf.formats.yaml
}
nconf.file 'system', {
  file: '/etc/breakpad-server.yaml', format: nconf.formats.yaml
}

nconf.defaults
  port: process.env.BREAKPAD_PORT || 1127
  baseUrl: process.env.BASEURL || '/'
  database:
    host: 'localhost'
    dialect: 'sqlite'
    storage: 'database.sqlite'
    logging: no
  customFields:
    files: []
    params: []

moment = require 'moment'
bodyParser = require 'body-parser'
methodOverride = require('method-override')
path = require 'path'
express = require 'express'
exphbs = require 'express-handlebars'
Crashreport = require './model/crashreport'
Symfile = require './model/symfile'

crashreportToApiJson = (crashreport) ->
  json = crashreport.toJSON()

  for k,v of json
    if Buffer.isBuffer(json[k])
      json[k] = "/crashreports/#{json.id}/#{k}"

  json

crashreportToViewJson = (report) ->
  fields = {}
  json = report.toJSON()
  for k,v of json
    if Buffer.isBuffer(json[k])
      fields[k] = { path: "/crashreports/#{report.id}/#{k}" }
    else if v instanceof Date
      fields[k] = moment(v).format('lll')
    else
      fields[k] = if v? then v else 'not present'

  return fields

# initialization: write all symfiles to disk
Symfile.findAll()
  .then (symfiles) ->
    Promise.all(symfiles.map((s) -> Symfile.saveToDisk(s))).then(run)
  .catch (err) ->
    console.log "could not save symfiles to disk: #{err.message}"

run = ->
  app = express()
  breakpad = express()

  breakpad.set 'views', path.resolve(__dirname, '..', 'views')
  breakpad.engine('handlebars', exphbs({defaultLayout: 'main'}))
  breakpad.set 'view engine', 'handlebars'
  breakpad.use bodyParser.json()
  breakpad.use bodyParser.urlencoded({extended: true})
  breakpad.use methodOverride()

  baseUrl = nconf.get('baseUrl')
  port = nconf.get('port')

  app.use baseUrl, breakpad

  # serve minidumps as files
  breakpad.use '/minidumps', express.static('pool/files/minidump')
  breakpad.use '/assets', express.static('node_modules/bootstrap/dist/css')

  # error handler
  app.use (err, req, res, next) ->
    if not err.message?
      console.log 'warning: error thrown without a message'

    res.status(500).send "Bad things happened:<br/> #{err.message || err}"

  breakpad.post '/crashreports', (req, res, next) ->
    Crashreport.createFromRequest req, (err, record) ->
      return next err if err?
      res.json crashreportToApiJson(record)

  breakpad.get '/', (req, res, next) ->
    res.redirect '/crashreports'

  breakpad.get '/crashreports', (req, res, next) ->
    Crashreport.findAll(order: 'createdAt DESC').then (records) ->
      viewReports = records.map(crashreportToViewJson)
      for r in viewReports
        delete r['updatedAt']
      fields = if viewReports.length then Object.keys(viewReports[0]) else []
      res.render 'index',
        title: 'Crash Reports'
        records: viewReports
        fields: fields

  breakpad.get '/crashreports/:id', (req, res, next) ->
    Crashreport.findById(req.params.id).then (report) ->
      if not report?
        return res.send 404, 'Crash report not found'
      Crashreport.getStackTrace report, (err, stackwalk) ->
        return next err if err?
        fields = crashreportToViewJson(report)

        delete fields['id']
        delete fields['updatedAt']

        res.render 'view', {
          title: 'Crash Report'
          stackwalk: stackwalk
          product: fields.product
          version: fields.version
          fields: fields
        }

  breakpad.get '/crashreports/:id/stackwalk', (req, res, next) ->
    # give the raw stackwalk
    Crashreport.findById(req.params.id).then (report) ->
      if not report?
        return res.send 404, 'Crash report not found'
      Crashreport.getStackTrace report, (err, stackwalk) ->
        return next err if err?
        res.set('Content-Type', 'text/plain')
        res.send(stackwalk.toString('utf8'))

  breakpad.get '/crashreports/:id/:filefield', (req, res, next) ->
    # download the file for the given id
    Crashreport.findById(req.params.id).then (crashreport) ->
      if not crashreport?
        return res.status(404).send 'Crash report not found'

      contents = crashreport.get(req.params.filefield)

      if not Buffer.isBuffer(contents)
        return res.status(404).send 'Crash report field is not a file'

      res.send(contents)

  breakpad.post '/symfiles', (req, res, next) ->
    Symfile.createFromRequest req, (err, symfile) ->
      return next(err) if err?
      symfileJson = symfile.toJSON()
      delete symfileJson.contents
      res.json symfileJson

  app.listen port
  console.log "Listening on port #{port}"
{'fraction_non_alphanumeric': 0.13662569346241707, 'fraction_numerical': 0.004133579897748286, 'mean_word_length': 3.1526648599819334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '295376', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2942, 'n_words': 739}