@markwharton/eh-payroll 3.0.1 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +0,0 @@
1
- /**
2
- * Sliding window rate limiter
3
- *
4
- * Enforces a maximum number of requests per time window.
5
- * Callers that exceed the limit are queued and released when capacity opens.
6
- *
7
- * Uses a sliding window approach: tracks timestamps of recent requests and
8
- * checks that no more than `maxRequests` fall within any `windowMs` period.
9
- *
10
- * Thread-safe in single-threaded JS — no races between concurrent acquire() calls.
11
- */
12
- export declare class RateLimiter {
13
- private maxRequests;
14
- private windowMs;
15
- private timestamps;
16
- private queue;
17
- private timer;
18
- /**
19
- * @param maxRequests - Maximum requests allowed per window
20
- * @param windowMs - Window size in milliseconds (default: 1000)
21
- */
22
- constructor(maxRequests: number, windowMs?: number);
23
- /**
24
- * Acquire a rate limit token.
25
- *
26
- * Resolves immediately if under the limit, otherwise queues until capacity opens.
27
- */
28
- acquire(): Promise<void>;
29
- /** Remove timestamps outside the current window */
30
- private pruneTimestamps;
31
- /** Schedule a flush to release queued callers when the oldest timestamp expires */
32
- private scheduleFlush;
33
- }
@@ -1,63 +0,0 @@
1
- /**
2
- * Sliding window rate limiter
3
- *
4
- * Enforces a maximum number of requests per time window.
5
- * Callers that exceed the limit are queued and released when capacity opens.
6
- *
7
- * Uses a sliding window approach: tracks timestamps of recent requests and
8
- * checks that no more than `maxRequests` fall within any `windowMs` period.
9
- *
10
- * Thread-safe in single-threaded JS — no races between concurrent acquire() calls.
11
- */
12
- export class RateLimiter {
13
- /**
14
- * @param maxRequests - Maximum requests allowed per window
15
- * @param windowMs - Window size in milliseconds (default: 1000)
16
- */
17
- constructor(maxRequests, windowMs = 1000) {
18
- this.maxRequests = maxRequests;
19
- this.windowMs = windowMs;
20
- this.timestamps = [];
21
- this.queue = [];
22
- this.timer = null;
23
- }
24
- /**
25
- * Acquire a rate limit token.
26
- *
27
- * Resolves immediately if under the limit, otherwise queues until capacity opens.
28
- */
29
- acquire() {
30
- this.pruneTimestamps();
31
- if (this.timestamps.length < this.maxRequests) {
32
- this.timestamps.push(Date.now());
33
- return Promise.resolve();
34
- }
35
- return new Promise(resolve => {
36
- this.queue.push({ resolve });
37
- this.scheduleFlush();
38
- });
39
- }
40
- /** Remove timestamps outside the current window */
41
- pruneTimestamps() {
42
- const cutoff = Date.now() - this.windowMs;
43
- this.timestamps = this.timestamps.filter(t => t > cutoff);
44
- }
45
- /** Schedule a flush to release queued callers when the oldest timestamp expires */
46
- scheduleFlush() {
47
- if (this.timer || this.queue.length === 0 || this.timestamps.length === 0)
48
- return;
49
- const oldest = this.timestamps[0];
50
- const delay = Math.max(0, this.windowMs - (Date.now() - oldest) + 1);
51
- this.timer = setTimeout(() => {
52
- this.timer = null;
53
- this.pruneTimestamps();
54
- while (this.queue.length > 0 && this.timestamps.length < this.maxRequests) {
55
- this.timestamps.push(Date.now());
56
- this.queue.shift().resolve();
57
- }
58
- if (this.queue.length > 0) {
59
- this.scheduleFlush();
60
- }
61
- }, delay);
62
- }
63
- }