<commit_before>'use strict'

module.exports = function (req, res, next) {
  // Attach specific params.
  req.permit = function (...keys) {
    const result = {}
    const body = req.body
    for (const key of keys) {
      if (body[key] !== undefined) result[key] = body[key]
    }
    return result
  }

  // Sign in a user
  req.signIn = function (user) {
    if (user) req.session.userId = user.id
  }

  // Sign out
  req.signOut = function () {
    req.session = null
  }

  // Put the request in the locals for convenience.
  res.locals.req = req

  // Log an error and render a 500 page.
  res.error = function (error) {
    if (error.message === 'invalid' && error.model) {
      res.status(422).json(error.model.errors)
      return
    }
    console.log(error.stack)
    res.status(500).react()
  }

  res.unauthorized = function () {
    res.status(401).react()
  }

  res.notfound = function () {
    res.status(404).react()
  }

  // JSON script tags
  res.locals.json = function (id, data) {
    const json = JSON.stringify(data || null)
      .replace(/<\/script|<!--/g, (match) => (
        match === '<!--' ? '<\\u0021--' : '<\\/script'
      ))
    return `<script type='application/json' id='${id}'>${json}</script>`
  }

  next()
}<commit_msg>Clean up.<commit_after>'use strict'

// Prevent XSS attacks in embedded JSON.
const escapeJson = (json) => (
  json.replace(/<\/script|<!--/g, (match) => {
    switch (match) {
      case '<!--': return '<\\u0021--'
      case '</script': return '<\\/script'
    }
  })
)

module.exports = function (req, res, next) {
  // Attach specific params.
  req.permit = function (...keys) {
    const result = {}
    const body = req.body
    for (const key of keys) {
      if (body[key] !== undefined) result[key] = body[key]
    }
    return result
  }

  // Sign in a user
  req.signIn = function (user) {
    if (user) req.session.userId = user.id
  }

  // Sign out
  req.signOut = function () {
    req.session = null
  }

  // Put the request in the locals for convenience.
  res.locals.req = req

  // Log an error and render a 500 page.
  res.error = function (error) {
    if (error.message === 'invalid' && error.model) {
      res.status(422).json(error.model.errors)
      return
    }
    console.log(error.stack)
    res.status(500).react()
  }

  res.unauthorized = function () {
    res.status(401).react()
  }

  res.notfound = function () {
    res.status(404).react()
  }

  // JSON script tags
  res.locals.json = function (id, data) {
    const json = escapeJson(JSON.stringify(data || null))
    return `<script type='application/json' id='${id}'>${json}</script>`
  }

  next()
}
{'fraction_non_alphanumeric': 0.17374952989845807, 'fraction_numerical': 0.01429108687476495, 'mean_word_length': 2.397190293742018, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 10, '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': '7334681', 'n_tokens_mistral': 939, 'n_tokens_neox': 892, 'n_words': 222}