1
0
Fork 0
mirror of synced 2024-07-14 02:36:22 +12:00

Updating public API rate limiting functionality to be better typed as well.

This commit is contained in:
mike12345567 2023-11-03 18:00:13 +00:00
parent f8f1ec4ce9
commit 001cf01303
2 changed files with 25 additions and 25 deletions

View file

@ -43,7 +43,7 @@ export enum Databases {
export enum SelectableDatabase { export enum SelectableDatabase {
DEFAULT = 0, DEFAULT = 0,
SOCKET_IO = 1, SOCKET_IO = 1,
UNUSED_1 = 2, RATE_LIMITING = 2,
UNUSED_2 = 3, UNUSED_2 = 3,
UNUSED_3 = 4, UNUSED_3 = 4,
UNUSED_4 = 5, UNUSED_4 = 5,
@ -96,6 +96,7 @@ export function getRedisOptions() {
return { return {
opts, opts,
host, host,
password,
port: parseInt(port), port: parseInt(port),
} }
} }

View file

@ -15,6 +15,16 @@ import env from "../../../environment"
const Router = require("@koa/router") const Router = require("@koa/router")
const { RateLimit, Stores } = require("koa2-ratelimit") const { RateLimit, Stores } = require("koa2-ratelimit")
import { middleware, redis } from "@budibase/backend-core" import { middleware, redis } from "@budibase/backend-core"
import { SelectableDatabase } from "@budibase/backend-core/src/redis/utils"
interface KoaRateLimitOptions {
socket: {
host: string
port: number
}
password?: string
database?: number
}
const PREFIX = "/api/public/v1" const PREFIX = "/api/public/v1"
// allow a lot more requests when in test // allow a lot more requests when in test
@ -29,32 +39,21 @@ function getApiLimitPerSecond(): number {
let rateLimitStore: any = null let rateLimitStore: any = null
if (!env.isTest()) { if (!env.isTest()) {
const REDIS_OPTS = redis.utils.getRedisOptions() const { password, host, port } = redis.utils.getRedisOptions()
let options let options: KoaRateLimitOptions = {
if (REDIS_OPTS.redisProtocolUrl) { socket: {
// fully qualified redis URL host: host,
options = { port: port,
url: REDIS_OPTS.redisProtocolUrl, },
} }
} else {
options = {
socket: {
host: REDIS_OPTS.host,
port: REDIS_OPTS.port,
},
}
if (REDIS_OPTS.opts?.password || REDIS_OPTS.opts.redisOptions?.password) { if (password) {
// @ts-ignore options.password = password
options.password = }
REDIS_OPTS.opts.password || REDIS_OPTS.opts.redisOptions.password
}
if (!env.REDIS_CLUSTERED) { if (!env.REDIS_CLUSTERED) {
// @ts-ignore // Can't set direct redis db in clustered env
// Can't set direct redis db in clustered env options.database = SelectableDatabase.RATE_LIMITING
options.database = 1
}
} }
rateLimitStore = new Stores.Redis(options) rateLimitStore = new Stores.Redis(options)
RateLimit.defaultOptions({ RateLimit.defaultOptions({