2021-03-03 15:18:51 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const {MaxLimit, FlagLimit} = require('./limit');
|
|
|
|
const config = require('./config');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
class LimitService {
|
|
|
|
constructor() {
|
|
|
|
this.limits = {};
|
|
|
|
}
|
|
|
|
|
2021-04-05 07:02:35 +03:00
|
|
|
/**
|
|
|
|
* Initializes the limits based on configuration
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Object} options.limits - hash containing limit configurations keyed by limit name and containing
|
|
|
|
* @param {String} options.helpLink - URL pointing to help resources for when limit is reached
|
|
|
|
* @param {Object} options.db - knex db connection instance or other data source for the limit checks
|
|
|
|
*/
|
2021-03-03 15:18:51 +03:00
|
|
|
loadLimits({limits, helpLink, db}) {
|
|
|
|
Object.keys(limits).forEach((name) => {
|
2021-03-04 23:11:54 +03:00
|
|
|
name = _.camelCase(name);
|
|
|
|
|
2021-03-03 15:18:51 +03:00
|
|
|
if (config[name]) {
|
2021-04-05 07:02:35 +03:00
|
|
|
/** @type LimitConfig */
|
2021-03-03 15:18:51 +03:00
|
|
|
let limitConfig = _.merge({}, limits[name], config[name]);
|
|
|
|
|
|
|
|
if (_.has(limitConfig, 'max')) {
|
|
|
|
this.limits[name] = new MaxLimit({name: name, config: limitConfig, helpLink, db});
|
|
|
|
} else {
|
|
|
|
this.limits[name] = new FlagLimit({name: name, config: limitConfig, helpLink});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
isLimited(limitName) {
|
2021-03-04 23:11:54 +03:00
|
|
|
return !!this.limits[_.camelCase(limitName)];
|
2021-03-03 15:18:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async checkIsOverLimit(limitName) {
|
|
|
|
if (!this.isLimited(limitName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.limits[limitName].errorIfIsOverLimit();
|
|
|
|
return false;
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof errors.HostLimitError) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async checkWouldGoOverLimit(limitName) {
|
|
|
|
if (!this.isLimited(limitName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.limits[limitName].errorIfWouldGoOverLimit();
|
|
|
|
return false;
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof errors.HostLimitError) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async errorIfIsOverLimit(limitName) {
|
|
|
|
if (!this.isLimited(limitName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.limits[limitName].errorIfIsOverLimit();
|
|
|
|
}
|
|
|
|
|
|
|
|
async errorIfWouldGoOverLimit(limitName) {
|
|
|
|
if (!this.isLimited(limitName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.limits[limitName].errorIfWouldGoOverLimit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LimitService;
|
2021-04-05 07:02:35 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} LimitConfig
|
|
|
|
* @prop {Number} [max] - max limit
|
|
|
|
* @prop {Boolean} [disabled] - flag disabling/enabling limit
|
|
|
|
* @prop {String} error - custom error to be displayed when the limit is reached
|
|
|
|
*/
|