<commit_before>// This plugin anticipates the response from the client
// to save wasted prerender CPU/RAM and cache disk space
// The response can be different from what the client would actually return:
// Ex: https://inventaire.io/entity/wd:Q34660/edit will be redirected to https://inventaire.io/entity/wd:Q34660
// instead of returning a 401

const { URL } = require('url')

module.exports = {
  init: () => console.log('using anticipate redirect plugin'),

  requestReceived: (req, res, next) => {
    const { href, pathname, origin } = new URL(req.prerender.url)

    const [ part1, part2, part3 ] = pathname.slice(1).split('/')
    let redirection

    if (loggedInSections.has(part1)) {
      redirection = `${origin}/login`
    } else if (loggedInSubSections[part1] != null && loggedInSubSections[part1].has(part2)) {
      redirection = `${origin}/login`
    } else if (dropPart3[part1] != null && dropPart3[part1].has(part3)) {
      redirection = href.replace(`/${part3}`, '')
    } else if (part1 === 'entity') {
      // Legacy URL: entity label was added as part3
      if (part3) redirection = `${origin}/${part1}/${part2}`
    }

    if (redirection) res.redirect(redirection)
    else next()
  }
}

const loggedInSections = new Set([
  'add',
  'network',
  'notifications',
  'settings',
  'tasks',
  'transactions',<commit_msg>anticipateRedirect: redirect search pages to the home page<commit_after>// This plugin anticipates the response from the client
// to save wasted prerender CPU/RAM and cache disk space
// The response can be different from what the client would actually return:
// Ex: https://inventaire.io/entity/wd:Q34660/edit will be redirected to https://inventaire.io/entity/wd:Q34660
// instead of returning a 401

const { URL } = require('url')

module.exports = {
  init: () => console.log('using anticipate redirect plugin'),

  requestReceived: (req, res, next) => {
    const { href, pathname, origin } = new URL(req.prerender.url)

    const [ part1, part2, part3 ] = pathname.slice(1).split('/')
    let redirection

    if (loggedInSections.has(part1)) {
      redirection = `${origin}/login`
    } else if (loggedInSubSections[part1] != null && loggedInSubSections[part1].has(part2)) {
      redirection = `${origin}/login`
    } else if (dropPart3[part1] != null && dropPart3[part1].has(part3)) {
      redirection = href.replace(`/${part3}`, '')
    } else if (part1 === 'entity') {
      // Legacy URL: entity label was added as part3
      if (part3) redirection = `${origin}/${part1}/${part2}`
    } else if (part1 === 'search') {
      // Search pages just create dupplicated content, redirect to the home page
      redirection = origin
    }

    if (redirection) res.redirect(redirection)
    else next()
  }
}

const loggedInSections = new Set([
  'add',
  'network',
  'notifications',
  'settings',
  'tasks',
  'transactions',
{'fraction_non_alphanumeric': 0.1469161469161469, 'fraction_numerical': 0.02252252252252252, 'mean_word_length': 3.553627760252366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, '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': '7370529', 'n_tokens_mistral': 931, 'n_tokens_neox': 906, 'n_words': 257}