push-stream-std

@push-stream-std/push-rate-limit (0.0.6)

Published 2026-07-19 23:31:59 +00:00 by Dvorak

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.6
"@push-stream-std/push-rate-limit": "0.0.6"

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 (default 100) – Number of items allowed per interval.
    • interval number (default 1000) – Refill interval in milliseconds.
    • burst number (default 10) – Maximum token bucket capacity (initial burst allowance).
    • fair boolean (default true) – When true, dequeues one item per token. When false, dequeues as many as tokens allow at once.
    • now () => number (default Date.now) – Function returning current time in milliseconds.
    • setTimer (fn, ms) => timer (default setTimeout) – Timer scheduling function.
    • clearTimer (timer) => void (default clearTimeout) – Timer cancellation function.
  • Returns A through stream with pipe(), resume(), abort(), write(), end(), paused, and ended.

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

Keywords

push-stream rate-limit throttle token-bucket
Details
npm
2026-07-19 23:31:59 +00:00
0
MIT
3.9 KiB
Assets (1)
Versions (6) View all
0.0.9 2026-07-21
0.0.8 2026-07-20
0.0.7 2026-07-20
0.0.6 2026-07-19
0.0.5 2026-07-19