<commit_before>import Vue from 'vue'

const noopData = () => ({})

// window.onNuxtReady(() => console.log('Ready')) hook
// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
if (process.browser) {
  window._nuxtReadyCbs = []
  window.onNuxtReady = function (cb) {
    window._nuxtReadyCbs.push(cb)
  }
}

export function applyAsyncData (Component, asyncData) {
  const ComponentData = Component.options.data || noopData
  // Prevent calling this method for each request on SSR context
  if (!asyncData && Component.options.hasAsyncData) {
    return
  }
  Component.options.hasAsyncData = true
  Component.options.data = function () {
    const data =  ComponentData.call(this)
    if (this.$ssrContext) {
      asyncData = this.$ssrContext.asyncData[Component.cid]
    }
    return { ...data, ...asyncData }
  }
  if (Component._Ctor && Component._Ctor.options) {
    Component._Ctor.options.data = Component.options.data
  }
}

export function sanitizeComponent (Component) {
  if (!Component.options) {
    Component = Vue.extend(Component) // fix issue #6
    Component._Ctor = Component
  } else {
    Component._Ctor = Component
    Component.extendOptions = Component.options
  }
  // For debugging purpose
  if (!Component.options.name && Component.options.__file) {
    Component.options.name = Component.options.__file
  }
  return Component
}

export function getMatchedComponents (route) {
  return [].concat.apply([], route.matched.map(function (m) {
    return Object.keys(m.components).map(function (key) {
      return m.components[key]
    })
  }))
}

export function getMatchedComponentsInstances (route) {
  return [].concat.apply([], route.matched.map(function (m) {
    return Object.keys(m.instances).map(function (key) {
      return m.instances[key]
    })
  }))
}

export function flatMapComponents (route, fn) {
  return Array.prototype.concat.apply([], route.matched.map(function (m, index) {
    return Object.keys(m.components).map(function (key) {
      return fn(m.components[key], m.instances[key], m, key, index)
    })
  }))
}

export function getContext (context, app) {
  let ctx = {
    isServer: !!context.isServer,
    isClient: !!context.isClient,
    isStatic: process.static,
    isDev: <%= isDev %>,
    isHMR: context.isHMR || false,
    app: app,
    <%= (store ? 'store: context.store,' : '') %>
    route: (context.to ? context.to : context.route),
    payload: context.payload,
    error: context.error,
    base: '<%= router.base %>',
    env: <%= JSON.stringify(env) %>
  }
  const next = context.next
  ctx.params = ctx.route.params || {}
  ctx.query = ctx.route.query || {}
  ctx.redirect = function (status, path, query) {
    if (!status) return
    ctx._redirected = true // Used in middleware
    // if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' })
    if (typeof status === 'string' && (typeof path === 'undefined' || typeof path === 'object')) {
      query = path || {}
      path = status
      status = 302
    }
    next({
      path: path,
      query: query,
      status: status
    })
  }
  if (context.req) ctx.req = context.req
  if (context.res) ctx.res = context.res
  if (context.from) ctx.from = context.from
  if (ctx.isServer && context.beforeRenderFns) {
    ctx.beforeNuxtRender = (fn) => context.beforeRenderFns.push(fn)
  }
  <% if (store) { %>
  if (ctx.isClient && window.__NUXT__ && window.__NUXT__.state) {
    ctx.serverStoreState = window.__NUXT__.state
  }
  <% } %>
  return ctx
}

export function middlewareSeries (promises, context) {
  if (!promises.length || context._redirected) {
    return Promise.resolve()
  }<commit_msg>Add serverState<commit_after>import Vue from 'vue'

const noopData = () => ({})

// window.onNuxtReady(() => console.log('Ready')) hook
// Useful for jsdom testing or plugins (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
if (process.browser) {
  window._nuxtReadyCbs = []
  window.onNuxtReady = function (cb) {
    window._nuxtReadyCbs.push(cb)
  }
}

export function applyAsyncData (Component, asyncData) {
  const ComponentData = Component.options.data || noopData
  // Prevent calling this method for each request on SSR context
  if (!asyncData && Component.options.hasAsyncData) {
    return
  }
  Component.options.hasAsyncData = true
  Component.options.data = function () {
    const data =  ComponentData.call(this)
    if (this.$ssrContext) {
      asyncData = this.$ssrContext.asyncData[Component.cid]
    }
    return { ...data, ...asyncData }
  }
  if (Component._Ctor && Component._Ctor.options) {
    Component._Ctor.options.data = Component.options.data
  }
}

export function sanitizeComponent (Component) {
  if (!Component.options) {
    Component = Vue.extend(Component) // fix issue #6
    Component._Ctor = Component
  } else {
    Component._Ctor = Component
    Component.extendOptions = Component.options
  }
  // For debugging purpose
  if (!Component.options.name && Component.options.__file) {
    Component.options.name = Component.options.__file
  }
  return Component
}

export function getMatchedComponents (route) {
  return [].concat.apply([], route.matched.map(function (m) {
    return Object.keys(m.components).map(function (key) {
      return m.components[key]
    })
  }))
}

export function getMatchedComponentsInstances (route) {
  return [].concat.apply([], route.matched.map(function (m) {
    return Object.keys(m.instances).map(function (key) {
      return m.instances[key]
    })
  }))
}

export function flatMapComponents (route, fn) {
  return Array.prototype.concat.apply([], route.matched.map(function (m, index) {
    return Object.keys(m.components).map(function (key) {
      return fn(m.components[key], m.instances[key], m, key, index)
    })
  }))
}

export function getContext (context, app) {
  let ctx = {
    isServer: !!context.isServer,
    isClient: !!context.isClient,
    isStatic: process.static,
    isDev: <%= isDev %>,
    isHMR: context.isHMR || false,
    app: app,
    <%= (store ? 'store: context.store,' : '') %>
    route: (context.to ? context.to : context.route),
    payload: context.payload,
    error: context.error,
    base: '<%= router.base %>',
    env: <%= JSON.stringify(env) %>
  }
  const next = context.next
  ctx.params = ctx.route.params || {}
  ctx.query = ctx.route.query || {}
  ctx.redirect = function (status, path, query) {
    if (!status) return
    ctx._redirected = true // Used in middleware
    // if only 1 or 2 arguments: redirect('/') or redirect('/', { foo: 'bar' })
    if (typeof status === 'string' && (typeof path === 'undefined' || typeof path === 'object')) {
      query = path || {}
      path = status
      status = 302
    }
    next({
      path: path,
      query: query,
      status: status
    })
  }
  if (context.req) ctx.req = context.req
  if (context.res) ctx.res = context.res
  if (context.from) ctx.from = context.from
  if (ctx.isServer && context.beforeRenderFns) {
    ctx.beforeNuxtRender = (fn) => context.beforeRenderFns.push(fn)
  }
  if (ctx.isClient && window.__NUXT__) {
    ctx.serverState = window.__NUXT__
    <% if (store) { %>
    ctx.serverStoreState = ctx.serverState.state
    <% } %>
  }
  return ctx
}

export function middlewareSeries (promises, context) {
  if (!promises.length || context._redirected) {
    return Promise.resolve()
  }
{'fraction_non_alphanumeric': 0.14832278054694867, 'fraction_numerical': 0.001616597063182002, 'mean_word_length': 3.2544412607449855, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 2, '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': '867382', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2341, 'n_words': 532}