@yveskaufmann/koa2-ratelimit
Version:
IP rate-limiting middleware for Koajs 2. Use to limit repeated requests to APIs and/or endpoints such as password reset.
51 lines • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Time = void 0;
const TimeKey = [
"ms",
"sec",
"min",
"hour",
"day",
"week",
"month",
"year",
];
const Times = {
ms: 1,
sec: 1000,
min: 60000,
hour: 3600000,
day: 86400000,
week: 604800000,
month: 2628000000,
year: 12 * 2628000000,
};
/**
* Converts a given time object to timestamp in ms.
*/
var Time;
(function (Time) {
function toMs(time) {
if (typeof time === "number") {
return time;
}
if (typeof time !== "object") {
return 0;
}
let timeMs = 0;
for (const key of Object.keys(time)) {
if (key) {
if (!TimeKey.includes(key)) {
throw new Error(`Invalide key ${key}, allow keys: ${TimeKey.toString()}`);
}
if (time[key] > 0) {
timeMs += time[key] * Times[key];
}
}
}
return timeMs;
}
Time.toMs = toMs;
})(Time = exports.Time || (exports.Time = {}));
//# sourceMappingURL=Time.js.map