arcjet / arcjet-js

Arcjet JS SDKs. Bot detection, rate limiting, email validation, attack protection, data redaction for Node.js, Next.js, Deno, Bun, SvelteKit, NestJS.
https://arcjet.com
Apache License 2.0
265 stars 7 forks source link

Can you have multiple `aj` per app with Arcjet? #2159

Open lancejpollard opened 6 hours ago

lancejpollard commented 6 hours ago

How does it know how to store permanently the rate limiting stuff? Can I have multiple const aj = arcjet({...}) per project, like in theory one per route.ts?

That way, I can say "for this route, rate limit like this, for that route, rate limit this other way", etc..

Will that just work?

All of the sample apps only have 1 aj per app, so I'm not sure.

davidmytton commented 6 hours ago

Hi @lancejpollard ! Yes, you can have. multiple aj instances per app. We set it up like this so that we can have base rules that apply everywhere we use that instance, but then use withRule to apply per route rules. You don't have to use the same aj instance, but we do it because then we only need to instantiate the client once.

This has some performance benefits because we instantiate the bundled WebAssembly module when the client is created. There is also no shared local cache across instances so if a user gets blocked from one rule from one instance, there wouldn't be anything in the cache in another instance.

Does that help?