@daiso-tech/core
Version:
The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.
59 lines (54 loc) • 1.88 kB
JavaScript
/**
* @module RateLimiter
*/
import { RATE_LIMITER_STATE } from "../../../../../rate-limiter/contracts/_module.js";
/**
* @internal
*/
export const rateLimterStorageLua = `
-- @template TMetrics
-- @param rateLimiterPolicy RateLimiterPolicy<TMetrics>
-- @param backoffPolicy BackoffPolicy
-- @param currentDate number
local function RateLimiterStorage(rateLimiterPolicy, backoffPolicy, currentDate)
-- @param AllRateLimiterState<TMetrics>
-- @return IRedisJsonRateLimiterState
local function toAdapterState(state)
local resetTime = state.startedAt
if state.type == "${RATE_LIMITER_STATE.ALLOWED}" then
resetTime = nil
end
return {
success = state.type == "${RATE_LIMITER_STATE.ALLOWED}",
attempt = state.attempt,
resetTime = resetTime
}
end
return {
-- @param args AtomicUpdateArgs<TMetrics>
-- @return IRedisJsonRateLimiterState
atomicUpdate = function(args)
local data = cjson.decode(redis.call(args.key))
local currentState = nil
if data ~= nil and data ~= cjson.null then
currentState = data.state
end
if currentState == nil or currentState == cjson.null then
currentState = rateLimiterPolicy.initialState(currentDate)
end
local newState = args.update(currentState)
redis.call("set", args.key, newState)
redis.call(
"pexpireat",
args.key,
rateLimiterPolicy.getExpiration(newState, {
backoffPolicy = backoffPolicy,
currentDate = currentDate
})
)
return toAdapterState(newState)
end
}
end
`;
//# sourceMappingURL=rate-limiter-storage-lua.js.map