Skip to content

fix: replace rate-limiter #2356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2024
Merged

Conversation

achingbrain
Copy link
Member

rate-limiter-flexible is a CJS module with a single export of all it's various implementations.

This defeats tree shaking resulting in the addition of 42KB to the bundle size.

animir/node-rate-limiter-flexible#249

This PR brings the source & tests for the in-memory rate limiter into @libp2p/utils which reduces the bundle size increase to a few KBs.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

[rate-limiter-flexible](https://npmjs.com/package/rate-limiter-flexible) is a CJS module with a single export of all it's various implementations.

This defeats tree shaking resulting in the addition of 42KB to the bundle size.

animir/node-rate-limiter-flexible#249

This PR brings the source & tests for the in-memory rate limiter into `@libp2p/utils` which reduces the bundle size increase to a few KBs.
@achingbrain achingbrain requested a review from a team as a code owner January 11, 2024 17:57
Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@achingbrain achingbrain merged commit ddaa59a into main Jan 12, 2024
@achingbrain achingbrain deleted the fix/replace-rate-limiter-flexible branch January 12, 2024 08:40
@mroderick
Copy link

This issue is really a shortcoming of your bundler/treeshaker and not of rate-limiter-simple.

However, there's hope.

Since you're using ESM, you can deep import files from packages, instead of using the root file.

import RateLimiterMemory from "rate-limiter-flexible/lib/RateLimiterMemory.js";

console.log(typeof RateLimiterMemory);
// => function

That means that you don't need to copy RateLimiterMemory to your own codebase and keep it up to date as the source version changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants