Public
Script
Readme

rlimit.com - rate-limiting as a service

This val abstracts rlimit.com service and allows val.town users to enjoy simple rate-limiting for their vals.

Using @rlimit.ratelimit users can benefit out of a special free tier without signing up for rlimit.com account. If anyone require more than what free tier has to offer, they just sign up and pass their namespace ID created in the rlimit.com dashboard.

Usage

Following val example will limit calling val to 10 requests per 60s.

let limitedVal = (async () => { const limit = await @rlimit.ratelimit("10/1m"); if (!limit.ok) { throw new Error("limit reached"); } console.log("limit ok", limit); // continue with expensive operation })();

A couple of examples

  • link to val1
  • link to val2
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
});
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023