<commit_before>import express from 'express'
import graphQLHTTP from 'express-graphql'
import {Schema} from './data/schema'
import writeSchema from './data/writeSchema'
import path from 'path'
import fallback from 'express-history-api-fallback'
import bodyParser from 'body-parser'
import session from 'express-session'
import BSD from './bsd'
import MG from './mail'
import demoData from './data/demo.json'
import log from './log'
import {fromGlobalId} from 'graphql-relay'
import passport from 'passport'
import LocalStrategy  from 'passport-local'
import url from 'url'
import Minilog from 'minilog'
import rateLimit from 'express-rate-limit'
import {compare, hash} from './bcrypt-promise'
import knex from './data/knex'
import KnexSessionStoreFactory from 'connect-session-knex'
import DataLoader from 'dataloader'

log.info('Writing schema...')
writeSchema()

const wrap = (fn) => {
  return (...args) =>
  {
    return fn(...args)
      .catch((ex) => {
        log.error(ex)
        process.nextTick(() => { throw ex })
      })
  }
}

const clientLogger = Minilog('client')
const KnexSessionStore = KnexSessionStoreFactory(session)
const Mailgun = new MG(process.env.MAILGUN_KEY, process.env.MAILGUN_DOMAIN)
const BSDClient = new BSD(process.env.BSD_HOST, process.env.BSD_API_ID, process.env.BSD_API_SECRET)
const port = process.env.PORT
const publicPath = path.resolve(__dirname, '../frontend/public')
const limiter = rateLimit({windowMs: 10000, max: 50})
const oneWeekInMillis = 604800000

const sessionStore = new KnexSessionStore({
  knex: knex,
  tablename: 'sessions',
})

function isAuthenticated(req, res, next) {
  if (req.user)
    return next()

  res.redirect('/signup');
}

passport.use('signup', new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  wrap(async (req, email, password, done) => {
    let user = await knex('users')
      .where('email', email.toLowerCase())
      .first()

    if (!user) {
      let hashedPassword = await hash(password)
      let newUser = await knex.insertAndFetch('users', {
          email: email.toLowerCase(),
          password: <PASSWORD>
        })
      return done(null, newUser)
    } else if (!await compare(password, user.password)) {
      return done(null, false, { message: 'Incorrect password.' })
    }

    return done(null, user)
  })
))

passport.serializeUser(wrap(async (user, done) => {
  done(null, user.id)
}))

passport.deserializeUser(wrap(async (id, done) => {
  let user = await knex('users').where('id', id).first()
  done(null, user)
}))

const app = express()

app.enable('trust proxy') // don't rate limit heroku

// List the routes that need to be rate limited
let rateLimitRoutes = [
  "/graphql",
  "/log",
  "/signup",
  "/events"
]

// Rate limit the routes
//rateLimitRoutes.forEach((route) => {
//  app.use(route,limiter)
//})

function dataLoaderCreator(tablename, idField) {
  return new DataLoader(async (keys) => {
    // This way it works with strings passed in as well
    let rows = await knex(tablename).whereIn(idField, keys)
    return keys.map((key) => {
      return rows.find((row) => row[idField].toString() === key.toString())
    })
  })
}

class QueryLoader {
  batchQueries(queries) {
    let promises = queries.map((query) => {
      knex.raw(query)
    })
    return Promise.all(promises);
  }

  constructor() {
    this.dataLoader = new DataLoader(this.batchQueries)
  }

  load(query) {
    return this.dataLoader.load(query.toString())
  }<commit_msg>Remove unused rate limiter code.<commit_after>import express from 'express'
import graphQLHTTP from 'express-graphql'
import {Schema} from './data/schema'
import writeSchema from './data/writeSchema'
import path from 'path'
import fallback from 'express-history-api-fallback'
import bodyParser from 'body-parser'
import session from 'express-session'
import BSD from './bsd'
import MG from './mail'
import demoData from './data/demo.json'
import log from './log'
import {fromGlobalId} from 'graphql-relay'
import passport from 'passport'
import LocalStrategy  from 'passport-local'
import url from 'url'
import Minilog from 'minilog'
import {compare, hash} from './bcrypt-promise'
import knex from './data/knex'
import KnexSessionStoreFactory from 'connect-session-knex'
import DataLoader from 'dataloader'

log.info('Writing schema...')
writeSchema()

const wrap = (fn) => {
  return (...args) =>
  {
    return fn(...args)
      .catch((ex) => {
        log.error(ex)
        process.nextTick(() => { throw ex })
      })
  }
}

const clientLogger = Minilog('client')
const KnexSessionStore = KnexSessionStoreFactory(session)
const Mailgun = new MG(process.env.MAILGUN_KEY, process.env.MAILGUN_DOMAIN)
const BSDClient = new BSD(process.env.BSD_HOST, process.env.BSD_API_ID, process.env.BSD_API_SECRET)
const port = process.env.PORT
const publicPath = path.resolve(__dirname, '../frontend/public')
const oneWeekInMillis = 604800000

const sessionStore = new KnexSessionStore({
  knex: knex,
  tablename: 'sessions',
})

function isAuthenticated(req, res, next) {
  if (req.user)
    return next()

  res.redirect('/signup');
}

passport.use('signup', new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  },
  wrap(async (req, email, password, done) => {
    let user = await knex('users')
      .where('email', email.toLowerCase())
      .first()

    if (!user) {
      let hashedPassword = await hash(password)
      let newUser = await knex.insertAndFetch('users', {
          email: email.toLowerCase(),
          password: <PASSWORD>
        })
      return done(null, newUser)
    } else if (!await compare(password, user.password)) {
      return done(null, false, { message: 'Incorrect password.' })
    }

    return done(null, user)
  })
))

passport.serializeUser(wrap(async (user, done) => {
  done(null, user.id)
}))

passport.deserializeUser(wrap(async (id, done) => {
  let user = await knex('users').where('id', id).first()
  done(null, user)
}))

const app = express()

app.enable('trust proxy') // don't rate limit heroku

function dataLoaderCreator(tablename, idField) {
  return new DataLoader(async (keys) => {
    // This way it works with strings passed in as well
    let rows = await knex(tablename).whereIn(idField, keys)
    return keys.map((key) => {
      return rows.find((row) => row[idField].toString() === key.toString())
    })
  })
}

class QueryLoader {
  batchQueries(queries) {
    let promises = queries.map((query) => {
      knex.raw(query)
    })
    return Promise.all(promises);
  }

  constructor() {
    this.dataLoader = new DataLoader(this.batchQueries)
  }

  load(query) {
    return this.dataLoader.load(query.toString())
  }
{'fraction_non_alphanumeric': 0.14148574804312508, 'fraction_numerical': 0.0036922168069709054, 'mean_word_length': 3.7489481065918655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '1511250', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2168, 'n_words': 563}