'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var Pool = require('pg-pool'); var pg = require('pg'); var postgresMigrations = require('postgres-migrations'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var Pool__default = /*#__PURE__*/_interopDefaultLegacy(Pool); var pg__default = /*#__PURE__*/_interopDefaultLegacy(pg); async function applyMigrations(config) { const client = new pg__default["default"].Client(config); await client.connect(); try { await postgresMigrations.migrate({ client }, __dirname + "/migrations"); } finally { await client.end(); } } class PostgresStoreIndividualIP { /** * The database configuration as specified in https://node-postgres.com/apis/client. */ config; /** * The database connection pool. */ pool; /** * Optional value that the store prepends to keys * * Used by the double-count check to avoid false-positives when a key is counted twice, but with different prefixes */ prefix; /** * The duration of time before which all hit counts are reset (in milliseconds). */ windowMs; /** * @constructor for `PostgresStoreIndividualIP`. * * @param config {any} - The database configuration as specified in https://node-postgres.com/apis/client. * @param prefix {string} - The unique name of the session. This is useful when applying multiple rate limiters with multiple stores. */ constructor(config, prefix) { this.config = config; this.prefix = prefix; applyMigrations(config); } /** * Method that actually initializes the store. Must be synchronous. * * This method is optional, it will be called only if it exists. * * @param options {Options} - The options used to setup express-rate-limit. * * @public */ init(options) { this.windowMs = options.windowMs; this.pool = new Pool__default["default"](this.config); } /** * Method to increment a client's hit counter. * * @param key {string} - The identifier for a client. * * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client. * * @public */ async increment(key) { let recordInsertQuery = "SELECT * FROM rate_limit.ind_increment($1, $2, $3) AS (count int, expires_at timestamptz);"; try { let result = await this.pool.query(recordInsertQuery, [ key, this.prefix, this.windowMs ]); let totalHits = 0; let resetTime = void 0; if (result.rows.length > 0) { totalHits = parseInt(result.rows[0].count); resetTime = result.rows[0].expires_at; } return { totalHits, resetTime }; } catch (err) { console.error(err); throw err; } } /** * Method to decrement a client's hit counter. * * @param key {string} - The identifier for a client. * * @public */ async decrement(key) { let decrementQuery = ` SELECT * FROM rate_limit.ind_decrement($1, $2); `; try { await this.pool.query(decrementQuery, [key, this.prefix]); } catch (err) { console.error(err); throw err; } } /** * Method to reset a client's hit counter. * * @param key {string} - The identifier for a client. * * @public */ async resetKey(key) { let resetQuery = ` SELECT * FROM rate_limit.ind_reset_key($1, $2); `; try { await this.pool.query(resetQuery, [key, this.prefix]); } catch (err) { console.error(err); throw err; } } /** * Method to reset everyone's hit counter. * * This method is optional, it is never called by express-rate-limit. * * @public */ async resetAll() { let resetAllQuery = ` SELECT * FROM rate_limit.ind_reset_session($1); `; try { await this.pool.query(resetAllQuery, [this.prefix]); } catch (err) { console.error(err); throw err; } } } class PostgresStoreAggregatedIP { /** * The database configuration as specified in https://node-postgres.com/apis/client. */ config; /** * Optional value that the store prepends to keys * * Used by the double-count check to avoid false-positives when a key is counted twice, but with different prefixes */ prefix; /** * The database connection pool. */ pool; /** * The duration of time before which all hit counts are reset (in milliseconds). */ windowMs; /** * @constructor for `PostgresStoreAggregatedIP`. * * @param config {any} - The database configuration as specified in https://node-postgres.com/apis/client. * @param prefix {string} - The unique name of the session. This is useful when applying multiple rate limiters with multiple stores. */ constructor(config, prefix) { this.config = config; this.prefix = prefix; applyMigrations(config); } /** * Method that actually initializes the store. Must be synchronous. * * This method is optional, it will be called only if it exists. * * @param options {Options} - The options used to setup express-rate-limit. * * @public */ init(options) { this.windowMs = options.windowMs; this.pool = new Pool__default["default"](this.config); } /** * Method to increment a client's hit counter. * * @param key {string} - The identifier for a client. * * @returns {ClientRateLimitInfo} - The number of hits and reset time for that client. * * @public */ async increment(key) { let recordInsertGetRecordsQuery = `SELECT * FROM rate_limit.agg_increment($1, $2, $3) AS (count int, expires_at timestamptz);`; try { let result = await this.pool.query(recordInsertGetRecordsQuery, [ key, this.prefix, this.windowMs ]); let totalHits = 0; let resetTime = void 0; if (result.rows.length > 0) { totalHits = parseInt(result.rows[0].count); resetTime = result.rows[0].expires_at; } return { totalHits, resetTime }; } catch (err) { console.error(err); throw err; } } /** * Method to decrement a client's hit counter. * * @param key {string} - The identifier for a client. * * @public */ async decrement(key) { let decrementQuery = ` SELECT * FROM rate_limit.agg_decrement($1, $2); `; try { await this.pool.query(decrementQuery, [key, this.prefix]); } catch (err) { console.error(err); throw err; } } /** * Method to reset a client's hit counter. * * @param key {string} - The identifier for a client. * * @public */ async resetKey(key) { let resetQuery = ` SELECT * FROM rate_limit.agg_reset_key($1, $2) `; try { await this.pool.query(resetQuery, [key, this.prefix]); } catch (err) { console.error(err); throw err; } } /** * Method to reset everyone's hit counter. * * This method is optional, it is never called by express-rate-limit. * * @public */ async resetAll() { let resetAllQuery = ` SELECT * FROM rate_limit.agg_reset_session($1); `; try { await this.pool.query(resetAllQuery, [this.prefix]); } catch (err) { console.error(err); throw err; } } } exports.PostgresStore = PostgresStoreAggregatedIP; exports.PostgresStoreIndividualIP = PostgresStoreIndividualIP;