push-stream-std

@push-stream-std/push-retry (0.0.3)

Published 2026-07-19 21:14:25 +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-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])

  • sourceFactoryfunction(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, default 3) — Maximum number of retry attempts before failing with new Error('Retry exhausted after N attempts').
  • options.delay (number or function, default 10) — Base delay in milliseconds between retries. If a function, called as delay(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) — Return false to skip retry and immediately end with the error.
  • options.timer (function, default setTimeout) — Injectible timer for scheduling retries.
  • options.clearTimer (function, default clearTimeout) — 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

Keywords

push-stream stream retry resilience
Details
npm
2026-07-19 21:14:25 +00:00
0
MIT
4.2 KiB
Assets (1)
Versions (5) View all
0.0.7 2026-07-21
0.0.6 2026-07-20
0.0.5 2026-07-20
0.0.4 2026-07-19
0.0.3 2026-07-19