text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const tslib_1 = require("tslib");
  const express_session_1 = tslib_1.__importDefault(require("express-session"));
  const redis = tslib_1.__importStar(require("redis"));
  const connect_redis_1 = tslib_1.__importDefault(require("connect-redis"));
  const connect_pg_simple_1 = tslib_1.__importDefault(require("connect-pg-simple"));
  const installDatabasePools_1 = require("./installDatabasePools");
  const app_1 = require("../app");
  const RedisStore = connect_redis_1.default(express_session_1.default);
  const PgStore = connect_pg_simple_1.default(express_session_1.default);
  const MILLISECOND = 1;
  const SECOND = 1000 * MILLISECOND;
  const MINUTE = 60 * SECOND;
  const HOUR = 60 * MINUTE;
  const DAY = 24 * HOUR;
  const { SECRET } = process.env;
  if (!SECRET) {
      throw new Error("Server misconfigured");
  }
  const MAXIMUM_SESSION_DURATION_IN_MILLISECONDS = parseInt(process.env.MAXIMUM_SESSION_DURATION_IN_MILLISECONDS || "", 10) ||
      3 * DAY;
  exports.default = (app) => {
      const rootPgPool = installDatabasePools_1.getRootPgPool(app);
      const store = process.env.REDIS_URL
          ? /*
             * Using redis for session storage means the session can be shared across
             * multiple Node.js instances (and survives a server restart), see:
             *
             * https://medium.com/mtholla/managing-node-js-express-sessions-with-redis-94cd099d6f2f
             */
              new RedisStore({
                  client: redis.createClient({
                      url: process.env.REDIS_URL,
                  }),
              })
          : /*
             * Using PostgreSQL for session storage is easy to set up, but increases
             * the load on your database. We recommend that you graduate to using
             * redis for session storage when you're ready.
             */
              new PgStore({
                  pool: rootPgPool,
                  schemaName: "app_private",
                  tableName: "connect_pg_simple_sessions",
              });
      const sessionMiddleware = express_session_1.default({
          rolling: true,
          saveUninitialized: false,
          resave: false,
          cookie: {
              maxAge: MAXIMUM_SESSION_DURATION_IN_MILLISECONDS,
          },
          store,
          secret: SECRET,
      });
      app.use(sessionMiddleware);
      app_1.getWebsocketMiddlewares(app).push(sessionMiddleware);
  };
  //# sourceMappingURL=installSession.js.map