@push-stream-std/push-retry (0.0.3)
Installation
@push-stream-std:registry=https://hub.kl1.tenere.ai/api/packages/push-stream-std/npm/npm install @push-stream-std/push-retry@0.0.3"@push-stream-std/push-retry": "0.0.3"About this package
push-retry
Retry a push-stream source on error with configurable backoff.
Published as @push-stream-std/push-retry.
Synopsis
import { retry } from '@push-stream-std/push-retry';
import { values } from '@push-stream-std/push-values';
import { collect } from '@push-stream-std/push-collect';
let attempt = 0;
const source = retry((n) => {
if (n === 1) throw new Error('temporary failure');
return values([42]);
}, { attempts: 3, delay: 10, backoff: 'exponential' });
source.pipe(collect((err, results) => {
if (err) console.error('all retries exhausted:', err.message);
else console.log(results); // [42]
}));
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-retry
Description
Wraps a push-stream source factory and retries it when an error occurs, applying a configurable delay and backoff strategy between attempts. The source factory receives the current attempt number (1-indexed) and should return a new push-stream source. Supports fixed, linear, and exponential backoff. The shouldRetry option allows filtering which errors trigger a retry. Timer functions are injectable via timer and clearTimer options for testability and CI compatibility. After a successful write, the attempt counter resets to 1.
API
retry(sourceFactory, [options])
sourceFactory—function(attempt)— Called with the attempt number (starting at 1). Should return a push-stream source. If an error is thrown synchronously, the retry logic handles it as an error from the source.options.attempts(number, default3) — Maximum number of retry attempts before failing withnew Error('Retry exhausted after N attempts').options.delay(number or function, default10) — Base delay in milliseconds between retries. If a function, called asdelay(attempt)and should return milliseconds.options.backoff(string, default'fixed') — Backoff strategy:'fixed'(constant delay),'linear'(delay * attempt), or'exponential'(delay * 2^(attempt-1)).options.shouldRetry(function, default() => true) —function(err)— Returnfalseto skip retry and immediately end with the error.options.timer(function, defaultsetTimeout) — Injectible timer for scheduling retries.options.clearTimer(function, defaultclearTimeout) — Injectable timer clearing function.
Returns a push-stream through. Pipe a sink into it to consume values from the retried source.
Tests
npm test
Tests cover successful source production, retry on error with fixed/exponential/linear backoff, shouldRetry filtering, attempt exhaustion, abort during retry delays, and timer injection.
Requirements
Node.js 18 or later. ESM only.
License
MIT