Packages

effect

4.1.4 · 11 Nov 2026MIT66.6M / weekRepositoryHomepagenpmAtom feed

Your version

2.3.3

12 Feb 2024Where it was published

2.4.19 is the newest release of 2.x.

In short

3 fixed

Security

2 advisories affect 2.3.3; 3.22.1 fixes them

Release note

Patch Changes

  • Update RateLimiter to support passing in a custom cost per effect. This is really useful for API(s) that have a "credit cost" per endpoint.

    Usage Example :

    import { Effect, RateLimiter } from "effect";
    import { compose } from "effect/Function";
    
    const program = Effect.scoped(
      Effect.gen(function* ($) {
        // Create a rate limiter that has an hourly limit of 1000 credits
        const rateLimiter = yield* $(RateLimiter.make(1000, "1 hours"));
        // Query API costs 1 credit per call ( 1 is the default cost )
        const queryAPIRL = compose(rateLimiter, RateLimiter.withCost(1));
        // Mutation API costs 5 credits per call
        const mutationAPIRL = compose(rateLimiter, RateLimiter.withCost(5));
        // ...
        // Use the pre-defined rate limiters
        yield* $(queryAPIRL(Effect.log("Sample Query")));
        yield* $(mutationAPIRL(Effect.log("Sample Mutation")));
    
        // Or set a cost on-the-fly
        yield* $(
          rateLimiter(Effect.log("Another query with a different cost")).pipe(
            RateLimiter.withCost(3),
          ),
        );
      }),
    );
  • Updates the RateLimiter.make constructor to take an object of RateLimiter.Options, which allows for specifying the rate-limiting algorithm to utilize:

    You can choose from either the token-bucket or the fixed-window algorithms for rate-limiting.

    export declare namespace RateLimiter {
      export interface Options {
        /**
         * The maximum number of requests that should be allowed.
         */
        readonly limit: number;
        /**
         * The interval to utilize for rate-limiting requests. The semantics of the
         * specified `interval` vary depending on the chosen `algorithm`:
         *
         * `token-bucket`: The maximum number of requests will be spread out over
         * the provided interval if no tokens are available.
         *
         * For example, for a `RateLimiter` using the `token-bucket` algorithm with
         * a `limit` of `10` and an `interval` of `1 seconds`, `1` request can be
         * made every `100 millis`.
         *
         * `fixed-window`: The maximum number of requests will be reset during each
         * interval. For example, for a `RateLimiter` using the `fixed-window`
         * algorithm with a `limit` of `10` and an `interval` of `1 seconds`, a
         * maximum of `10` requests can be made each second.
         */
        readonly interval: DurationInput;
        /**
         * The algorithm to utilize for rate-limiting requests.
         *
         * Defaults to `token-bucket`.
         */
        readonly algorithm?: "fixed-window" | "token-bucket";
      }
    }
  • Return the resulting available permits from Semaphore.release

A whole project to check? Paste its package.json in the dependency check: every deprecated, outdated or vulnerable dependency, with what to upgrade to. Free, no account.