<commit_before>import bodyParser from 'body-parser';
import createAuthenticate from './create-authenticate';
import createGenerateToken from './create-generate-token';
import createValidateToken from './create-validate-token';
import express from 'express';
import extractJwtFromHeader from './extract-jwt-from-header';
import generateSession from './generate-session';
import getCouchOptions from './get-couch-options';
import info from './info';
import invariant from 'invariant';
import login from './login';
import logout from './logout';
import renew from './renew';
import {route as errorRoute,HTTPError} from "./http-error";

export default function createApp({algorithms=['HS256'], session={}, couchdb, endpoint="/", expiresIn='5m', secret}) {
  invariant(algorithms, 'missing algorithms');
  invariant(session, 'missing session options');
  invariant(endpoint, 'missing endpoint');
  invariant(expiresIn, 'missing expiresIn');
  invariant(secret, 'missing JWT secret');

  // resolve session store
  const storeType = session.store;
  delete session.store;
  let createSessionStore;
  if (storeType === 'couch') {
    createSessionStore = require('./couch-store');
  } else if (storeType === 'memory' || typeof storeType === 'undefined') {
    createSessionStore = require('./memory-store');
  } else {
    createSessionStore = require(storeType);
  }

  // create express app and parse options
  const app = express();
  const couchOptions = getCouchOptions(couchdb);

  // set a few helpers on the app
  app.authenticate = createAuthenticate(couchOptions.baseUrl).bind(app);
  app.generateSession = generateSession.bind(app);
  app.generateToken = createGenerateToken({algorithms, expiresIn, secret}).bind(app);
  app.validateToken = createValidateToken({algorithms, secret}).bind(app);

  // set up the session store
  let _setup;
  app.setup = () => {
    if (_setup) return _setup;
    return (_setup = Promise.resolve(createSessionStore(session, couchOptions))
      .then((store) => {
        app.sessionStore = store;
      }));
  };

  app.disable('x-powered-by');

  // app must be setup before routes can be used
  app.use((req, res, next) => {
    app.setup().then(() => next(), next);
  });<commit_msg>fix: setup immediately<commit_after>import bodyParser from 'body-parser';
import createAuthenticate from './create-authenticate';
import createGenerateToken from './create-generate-token';
import createValidateToken from './create-validate-token';
import express from 'express';
import extractJwtFromHeader from './extract-jwt-from-header';
import generateSession from './generate-session';
import getCouchOptions from './get-couch-options';
import info from './info';
import invariant from 'invariant';
import login from './login';
import logout from './logout';
import renew from './renew';
import {route as errorRoute,HTTPError} from "./http-error";

export default function createApp({algorithms=['HS256'], session={}, couchdb, endpoint="/", expiresIn='5m', secret}) {
  invariant(algorithms, 'missing algorithms');
  invariant(session, 'missing session options');
  invariant(endpoint, 'missing endpoint');
  invariant(expiresIn, 'missing expiresIn');
  invariant(secret, 'missing JWT secret');

  // resolve session store
  const storeType = session.store;
  delete session.store;
  let createSessionStore;
  if (storeType === 'couch') {
    createSessionStore = require('./couch-store');
  } else if (storeType === 'memory' || typeof storeType === 'undefined') {
    createSessionStore = require('./memory-store');
  } else {
    createSessionStore = require(storeType);
  }

  // create express app and parse options
  const app = express();
  const couchOptions = getCouchOptions(couchdb);

  // set a few helpers on the app
  app.authenticate = createAuthenticate(couchOptions.baseUrl).bind(app);
  app.generateSession = generateSession.bind(app);
  app.generateToken = createGenerateToken({algorithms, expiresIn, secret}).bind(app);
  app.validateToken = createValidateToken({algorithms, secret}).bind(app);

  // set up the session store
  let _setup = (async () => {
    let store = await createSessionStore(session, couchOptions);
    app.sessionStore = store;
  })();
  app.setup = () => _setup;

  app.disable('x-powered-by');

  // app must be setup before routes can be used
  app.use((req, res, next) => {
    _setup.then(() => next(), next);
  });
{'fraction_non_alphanumeric': 0.14256433614210887, 'fraction_numerical': 0.0018219084491004327, 'mean_word_length': 4.689119170984456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '858936', 'n_tokens_mistral': 1233, 'n_tokens_neox': 1211, 'n_words': 352}