Files
headroom/frontend/node_modules/effect/dist/esm/internal/rateLimiter.js
Santhosh Janardhanan de2d83092e feat: Reinitialize frontend with SvelteKit and TypeScript
- Delete old Vite+Svelte frontend
- Initialize new SvelteKit project with TypeScript
- Configure Tailwind CSS v4 + DaisyUI
- Implement JWT authentication with auto-refresh
- Create login page with form validation (Zod)
- Add protected route guards
- Update Docker configuration for single-stage build
- Add E2E tests with Playwright (6/11 passing)
- Fix Svelte 5 reactivity with $state() runes

Known issues:
- 5 E2E tests failing (timing/async issues)
- Token refresh implementation needs debugging
- Validation error display timing
2026-02-17 16:19:59 -05:00

43 lines
2.1 KiB
JavaScript

import * as Duration from "../Duration.js";
import * as Effect from "../Effect.js";
import * as FiberRef from "../FiberRef.js";
import { pipe } from "../Function.js";
import { globalValue } from "../GlobalValue.js";
/** @internal */
export const make = ({
algorithm = "token-bucket",
interval,
limit
}) => {
switch (algorithm) {
case "fixed-window":
{
return fixedWindow(limit, interval);
}
case "token-bucket":
{
return tokenBucket(limit, interval);
}
}
};
const tokenBucket = (limit, window) => Effect.gen(function* () {
const millisPerToken = Math.ceil(Duration.toMillis(window) / limit);
const semaphore = yield* Effect.makeSemaphore(limit);
const latch = yield* Effect.makeSemaphore(0);
const refill = Effect.sleep(millisPerToken).pipe(Effect.zipRight(latch.releaseAll), Effect.zipRight(semaphore.release(1)), Effect.flatMap(free => free === limit ? Effect.void : refill));
yield* pipe(latch.take(1), Effect.zipRight(refill), Effect.forever, Effect.forkScoped, Effect.interruptible);
const take = Effect.uninterruptibleMask(restore => Effect.flatMap(FiberRef.get(currentCost), cost => Effect.zipRight(restore(semaphore.take(cost)), latch.release(1))));
return effect => Effect.zipRight(take, effect);
});
const fixedWindow = (limit, window) => Effect.gen(function* () {
const semaphore = yield* Effect.makeSemaphore(limit);
const latch = yield* Effect.makeSemaphore(0);
yield* pipe(latch.take(1), Effect.zipRight(Effect.sleep(window)), Effect.zipRight(latch.releaseAll), Effect.zipRight(semaphore.releaseAll), Effect.forever, Effect.forkScoped, Effect.interruptible);
const take = Effect.uninterruptibleMask(restore => Effect.flatMap(FiberRef.get(currentCost), cost => Effect.zipRight(restore(semaphore.take(cost)), latch.release(1))));
return effect => Effect.zipRight(take, effect);
});
/** @internal */
const currentCost = /*#__PURE__*/globalValue(/*#__PURE__*/Symbol.for("effect/RateLimiter/currentCost"), () => FiberRef.unsafeMake(1));
/** @internal */
export const withCost = cost => Effect.locally(currentCost, cost);
//# sourceMappingURL=rateLimiter.js.map