sveltekit-rate-limiter
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -80,9 +80,2 @@ import { nanoid } from 'nanoid'; | ||
} | ||
else { | ||
import('crypto').then((crypto) => { | ||
defaultHashFunction = (input) => { | ||
return Promise.resolve(crypto.createHash('sha256').update(input).digest('hex')); | ||
}; | ||
}); | ||
} | ||
async function _subtleSha256(str) { | ||
@@ -192,4 +185,5 @@ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str)); | ||
this.hashFunction = options.hashFunction ?? defaultHashFunction; | ||
if (!this.hashFunction) | ||
if (!this.hashFunction) { | ||
throw new Error('No RateLimiter hash function found. Please set one with the hashFunction option.'); | ||
} | ||
if (options.rates?.IP) | ||
@@ -196,0 +190,0 @@ this.plugins.push(new IPRateLimiter(options.rates.IP)); |
{ | ||
"name": "sveltekit-rate-limiter", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"author": "Andreas Söderlund <ciscoheat@gmail.com> (https://blog.encodeart.dev)", | ||
@@ -46,2 +46,3 @@ "description": "A modular rate limiter for SvelteKit. Use in password resets, account registration, etc.", | ||
"@sveltejs/adapter-auto": "^2.1.0", | ||
"@sveltejs/adapter-cloudflare": "^2.3.2", | ||
"@sveltejs/kit": "^1.22.5", | ||
@@ -48,0 +49,0 @@ "@sveltejs/package": "^2.2.1", |
@@ -104,3 +104,3 @@ # sveltekit-rate-limiter | ||
rates: { | ||
IP: [10, 'h'] | ||
IP: [10, 'h'], | ||
IPUA: [5, 'm'] | ||
@@ -114,3 +114,3 @@ } | ||
if(status.limited) { | ||
if (status.limited) { | ||
event.setHeaders({ | ||
@@ -131,2 +131,14 @@ 'Retry-After': status.retryAfter.toString() | ||
## Custom hash function | ||
The default hash function is using `crypto.subtle` to generate a SHA-256 digest, but if isn't available in your environment, you can supply your own with the `hashFunction` option. Here's an example with the NodeJS `crypto` package: | ||
```ts | ||
import crypto from 'crypto'; | ||
// (input: string) => Promise<string> | ||
const hashFunction = (input: string) => | ||
Promise.resolve(crypto.createHash('sha256').update(input).digest('hex')); | ||
``` | ||
## Creating a custom limiter | ||
@@ -133,0 +145,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22671
196
21
388