@push-stream-std/push-rate-limit (0.0.7)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-rate-limit@0.0.7"@push-stream-std/push-rate-limit": "0.0.7"About this package
push-rate-limit
Limit throughput rate using a token bucket algorithm with queueing.
Published as @push-stream-std/push-rate-limit.
Synopsis
import rateLimit from '@push-stream-std/push-rate-limit';
import pushable from '@push-stream-std/push-pushable';
import concat from '@push-stream-std/push-concat';
const source = pushable();
source.pipe(rateLimit({ rate: 10, interval: 1000, burst: 5 })).pipe(concat((err, result) => {
if (err) return console.error(err);
console.log(result);
}));
for (let i = 0; i < 20; i++) source.push(i);
source.end();
Install
npm config set @push-stream-std:registry https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/
npm config set -- //hub.kl1.tenere.ai/api/packages/push-stream-std/npm/:_authToken "$PACKAGE_TOKEN"
npm install @push-stream-std/push-rate-limit
Description
A through stream that enforces a maximum data rate using a token bucket. Tokens are refilled at the configured rate and consumed per write. When tokens are exhausted, items are queued and released as tokens refill. The queue is capped at burst * 2 — exceeding this triggers an error. Timer injection options (now, setTimer, clearTimer) enable deterministic testing and CI compatibility.
API
rateLimit(options?)
Creates a rate-limited through stream.
- options
object(optional):- rate
number(default100) – Number of items allowed per interval. - interval
number(default1000) – Refill interval in milliseconds. - burst
number(default10) – Maximum token bucket capacity (initial burst allowance). - fair
boolean(defaulttrue) – Whentrue, dequeues one item per token. Whenfalse, dequeues as many as tokens allow at once. - now
() => number(defaultDate.now) – Function returning current time in milliseconds. - setTimer
(fn, ms) => timer(defaultsetTimeout) – Timer scheduling function. - clearTimer
(timer) => void(defaultclearTimeout) – Timer cancellation function.
- rate
- Returns A through stream with
pipe(),resume(),abort(),write(),end(),paused, andended.
Tests
npm test
Tests cover token consumption and refill, queueing when tokens exhausted, burst allowance, queue overflow error, fair vs unfair dequeue, timer injection, end with pending queue, and abort clearing pending items.
Requirements
Node.js 18 or later. ESM only.
License
MIT