1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { parentReference } from "https://esm.town/v/stevekrouse/parentReference?v=3";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import { runVal } from "https://esm.town/v/std/runVal";
export let ratelimit = async (limit: string, key?: string = "default", namespaceId?: string, namespacePassword?: string) : Promise<{
ok: boolean,
status: number,
remaining: number,
reset: number
}> => {
const [max, interval] = limit.split("/")
// use user's rlimit.com namespace if defined
if (namespaceId) {
return await fetchJSON(
`https://rlimit.com/${namespaceId}/${max}/${interval}/${key}?password=${namespacePassword}&region=global`,
);
}
// otherwise, use a special val.town free tier
const parent = parentReference();
const valUser = parent.userHandle;
return runVal("rlimit.rlimitValFreeTier", {
max: parseInt(max),
interval,
key,
valUser
});
};