<commit_before>const sqlite = require('sqlite')
const log = require('./lib/logger')(`server[${process.pid}]`)
const path = require('path')
const config = require('../project.config')
const getIPAddress = require('./lib/getIPAddress')
const http = require('http')
const fs = require('fs')
const { promisify } = require('util')
const parseCookie = require('./lib/parseCookie')
const jwtVerify = require('jsonwebtoken').verify
const Koa = require('koa')
const koaBody = require('koa-body')
const koaFavicon = require('koa-favicon')
const koaLogger = require('koa-logger')
const koaMount = require('koa-mount')
const koaRange = require('koa-range')
const koaStatic = require('koa-static')
const app = new Koa()

const Prefs = require('./Prefs')
const libraryRouter = require('./Library/router')
const mediaRouter = require('./Media/router')
const prefsRouter = require('./Prefs/router')
const roomsRouter = require('./Rooms/router')
const userRouter = require('./User/router')
const SocketIO = require('socket.io')
const socketActions = require('./socket')
const IPCMediaActions = require('./Media/ipc')
const IPCPrefsActions = require('./Prefs/ipc')

const {
  SERVER_WORKER_STATUS,
  SERVER_WORKER_ERROR,
} = require('../shared/actionTypes')

// Koa error handling
app.use(async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    ctx.status = err.status || 500
    ctx.body = err.message
    ctx.app.emit('error', err, ctx)
  }
})

app.on('error', (err, ctx) => {
  if (err.code === 'EPIPE') {
    // these are common since browsers make multiple requests for media files
    log.verbose(err.message)
    return
  }

  // silence 4xx response "errors" (koa-logger should show these anyway)
  if (ctx.response && ctx.response.status.toString().startsWith('4')) {
    return
  }

  if (err.stack) log.error(err.stack)
  else log.error(err)
})

log.info('Opening database file %s', config.database)

Promise.resolve()
  .then(() => sqlite.open(config.database, { Promise }))
  .then(db => db.migrate({
    migrationsPath: path.join(config.basePath, 'server', 'lib', 'schemas'),
    // force: 'last',
  }))
  .then(() => Prefs.getJwtKey())
  .then(jwtKey => {
    // koa request/response logging
    app.use(koaLogger((str, args) => {
      if (args.length === 6 && args[3] >= 500) {
        // 5xx status response
        log.error(str)
      } else {
        log.verbose(str)
      }
    }))

    app.use(koaFavicon(path.join(config.assetPath, 'favicon.ico')))
    app.use(koaRange)
    app.use(koaBody({ multipart: true }))

    // all http (koa) requests
    app.use(async (ctx, next) => {
      ctx.io = io
      ctx.jwtKey = jwtKey

      // verify jwt
      try {
        const { kfToken } = parseCookie(ctx.request.header.cookie)
        ctx.user = jwtVerify(kfToken, jwtKey)
      } catch (err) {<commit_msg>Remove (commented) option that won't work in sqlite@4<commit_after>const sqlite = require('sqlite')
const log = require('./lib/logger')(`server[${process.pid}]`)
const path = require('path')
const config = require('../project.config')
const getIPAddress = require('./lib/getIPAddress')
const http = require('http')
const fs = require('fs')
const { promisify } = require('util')
const parseCookie = require('./lib/parseCookie')
const jwtVerify = require('jsonwebtoken').verify
const Koa = require('koa')
const koaBody = require('koa-body')
const koaFavicon = require('koa-favicon')
const koaLogger = require('koa-logger')
const koaMount = require('koa-mount')
const koaRange = require('koa-range')
const koaStatic = require('koa-static')
const app = new Koa()

const Prefs = require('./Prefs')
const libraryRouter = require('./Library/router')
const mediaRouter = require('./Media/router')
const prefsRouter = require('./Prefs/router')
const roomsRouter = require('./Rooms/router')
const userRouter = require('./User/router')
const SocketIO = require('socket.io')
const socketActions = require('./socket')
const IPCMediaActions = require('./Media/ipc')
const IPCPrefsActions = require('./Prefs/ipc')

const {
  SERVER_WORKER_STATUS,
  SERVER_WORKER_ERROR,
} = require('../shared/actionTypes')

// Koa error handling
app.use(async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    ctx.status = err.status || 500
    ctx.body = err.message
    ctx.app.emit('error', err, ctx)
  }
})

app.on('error', (err, ctx) => {
  if (err.code === 'EPIPE') {
    // these are common since browsers make multiple requests for media files
    log.verbose(err.message)
    return
  }

  // silence 4xx response "errors" (koa-logger should show these anyway)
  if (ctx.response && ctx.response.status.toString().startsWith('4')) {
    return
  }

  if (err.stack) log.error(err.stack)
  else log.error(err)
})

log.info('Opening database file %s', config.database)

Promise.resolve()
  .then(() => sqlite.open(config.database, { Promise }))
  .then(db => db.migrate({
    migrationsPath: path.join(config.basePath, 'server', 'lib', 'schemas'),
  }))
  .then(() => Prefs.getJwtKey())
  .then(jwtKey => {
    // koa request/response logging
    app.use(koaLogger((str, args) => {
      if (args.length === 6 && args[3] >= 500) {
        // 5xx status response
        log.error(str)
      } else {
        log.verbose(str)
      }
    }))

    app.use(koaFavicon(path.join(config.assetPath, 'favicon.ico')))
    app.use(koaRange)
    app.use(koaBody({ multipart: true }))

    // all http (koa) requests
    app.use(async (ctx, next) => {
      ctx.io = io
      ctx.jwtKey = jwtKey

      // verify jwt
      try {
        const { kfToken } = parseCookie(ctx.request.header.cookie)
        ctx.user = jwtVerify(kfToken, jwtKey)
      } catch (err) {
{'fraction_non_alphanumeric': 0.1694316978468055, 'fraction_numerical': 0.004059301094246382, 'mean_word_length': 3.5518072289156626, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '623878', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1867, 'n_words': 425}