reliable-requests 0.3.0 → 0.3.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1 — prepared 2026-12-02
4
+
5
+ - Harden retry-delay cancellation and listener cleanup.
6
+ - Preserve terminal stream errors from custom transports without consulting the retry predicate.
7
+ - Add cancellation, late response cleanup, ordinary network retry and normalized loopback regression tests.
8
+ - Add npm discovery keywords.
9
+
3
10
  ## 0.3.0 — prepared 2026-12-02
4
11
 
5
12
  - Add optional first-text and text-idle deadlines for model streams with a distinct public `StreamTimeoutError`, leaving existing timeout and retry behavior unchanged when omitted.
package/docs/USAGE.md CHANGED
@@ -140,7 +140,7 @@ controller.abort();
140
140
  await pending; // rejects with AbortError
141
141
  ```
142
142
 
143
- An already aborted signal sends no request. Cancellation also interrupts retry delays. A timeout rejects with `RequestTimeoutError` and `code === 'REQUEST_TIMEOUT'`. The wrapper aborts its own fetch signal and rejects on time even if a custom fetch implementation ignores abort.
143
+ An already aborted signal sends no request. Cancellation also interrupts retry delays. A timeout rejects with `RequestTimeoutError` and `code === 'REQUEST_TIMEOUT'`. The wrapper aborts its own fetch signal and rejects on time even if a custom fetch implementation ignores abort. A response that arrives after cancellation or timeout is discarded without calling consume or starting another attempt. Arbitrary custom promises cannot be forcibly stopped: custom fetch implementations and consumers must cooperate with the signal, stop their own side effects and release any readers they hold.
144
144
 
145
145
  ## Streaming
146
146
 
@@ -174,7 +174,7 @@ The deadline includes the entire stream. If it expires after some chunks were de
174
174
 
175
175
  `retries` means additional attempts (default 2); `timeoutMs` defaults to 30,000 ms for `reliableRequest` and 120,000 ms for `streamLocalChat`. In `reliableRequest`, only `GET` and `HEAD` retry. `streamLocalChat` can retry text-generation POST requests under its separate safety policy. A fetch TypeError or HTTP 429, 500, 502, 503, or 504 may retry. HTTP 4xx errors such as 401 and 404 fail immediately. Both APIs use the same exponential backoff with bounded jitter (250 ms initial base, 5 s maximum base). A valid Retry-After value (seconds or HTTP date) is treated as a minimum delay, with small nonnegative jitter; it is never shortened to an earlier retry. Every delay consumes the same total time budget. If the next delay cannot fit, the operation rejects immediately with RequestTimeoutError rather than sending another request.
176
176
 
177
- Both APIs accept an optional synchronous shouldRetryError(error, { attempt, method, phase }) predicate. Return true to retry an eligible transport error. The default accepts TypeError, which Fetch uses for network failures but also for some invalid request options; browsers do not reliably distinguish them. For example, shouldRetryError: () => false disables transport-error retries. In reliableRequest the predicate is called only for a fetch rejection on GET/HEAD, never for errors in consume. In streamLocalChat it may also receive phase: 'read' for a rejected stream read. It does not override HTTP-status rules, retry budgets, method safety, remote opt-in, or the interrupted-stream policy. It is not called for malformed SSE/NDJSON, callback errors, cancellation, or timeouts. Exceptions from the predicate propagate without another attempt. The streaming retry event also exposes delayMs.
177
+ Both APIs accept an optional synchronous shouldRetryError(error, { attempt, method, phase }) predicate. Return true to retry an eligible transport error. The default accepts TypeError, which Fetch uses for network failures but also for some invalid request options; browsers do not reliably distinguish them. For example, shouldRetryError: () => false disables transport-error retries. In reliableRequest the predicate is called only for a fetch rejection on GET/HEAD, never for errors in consume. In streamLocalChat it may also receive phase: 'read' for a rejected stream read. It does not override HTTP-status rules, retry budgets, method safety, remote opt-in, or the interrupted-stream policy. It is not called for malformed SSE/NDJSON, callback errors, cancellation, or timeouts. Recognized terminal stream error instances (including redirects, unsupported output and parser limits) also bypass the predicate when raised by a custom transport. Exceptions from the predicate propagate without another attempt. The streaming retry event also exposes delayMs.
178
178
 
179
179
  The general `reliableRequest` wrapper never retries `POST`, `PUT`, `PATCH`, or `DELETE`. Repeating one could create a duplicate operation or charge twice. If your API has an idempotency key contract, put a separate, explicit retry policy around that operation.
180
180
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reliable-requests",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=18"
@@ -20,6 +20,22 @@
20
20
  },
21
21
  "license": "MIT",
22
22
  "description": "Resilient local-model text streams with explicit reset on retry",
23
+ "keywords": [
24
+ "fetch",
25
+ "http",
26
+ "retry",
27
+ "timeout",
28
+ "streaming",
29
+ "sse",
30
+ "llm",
31
+ "local-ai",
32
+ "ollama",
33
+ "llama-cpp",
34
+ "lm-studio",
35
+ "fault-tolerance",
36
+ "typescript",
37
+ "zero-dependencies"
38
+ ],
23
39
  "main": "./index.js",
24
40
  "types": "./index.d.ts",
25
41
  "exports": {
package/src/local-chat.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { reliableRequest, HttpError, RequestTimeoutError } from './index.js';
2
- import { createLineParser, createSseParser } from './sse.js';
2
+ import { createLineParser, createSseParser, StreamLineTooLongError, StreamEventTooLongError } from './sse.js';
3
3
  import { parseEndpoint, isLoopback } from './endpoint.js';
4
4
  import { parseEvent, openaiChoice, ollamaMessage, InvalidStreamEventError } from './stream-events.js';
5
5
  import {
@@ -177,6 +177,12 @@ async function readStream(response, {
177
177
  }
178
178
 
179
179
  function shouldRetryStream(error, attempt, networkPhase, shouldRetryError) {
180
+ if (error instanceof StreamTimeoutError ||
181
+ error instanceof ModelRedirectError ||
182
+ error instanceof UnsupportedModelOutputError ||
183
+ error instanceof InvalidStreamEventError ||
184
+ error instanceof StreamLineTooLongError ||
185
+ error instanceof StreamEventTooLongError) return false;
180
186
  if (error instanceof HttpError) return isRetryableStatus(error.status);
181
187
  if (error instanceof StreamInterruptedError) return true;
182
188
  if (networkPhase === null) return false;
package/src/retry.js CHANGED
@@ -29,17 +29,23 @@ export function retryDelayMs({ attempt, response, remainingMs, random = Math.ran
29
29
  export function pause(ms, signal) {
30
30
  if (signal.aborted) return Promise.reject(signal.reason);
31
31
  return new Promise((resolve, reject) => {
32
- const cleanup = () => signal.removeEventListener('abort', onAbort);
33
- const onAbort = () => {
32
+ let timer;
33
+ let settled = false;
34
+ const cleanup = () => {
35
+ if (settled) return false;
36
+ settled = true;
34
37
  clearTimeout(timer);
35
- cleanup();
36
- reject(signal.reason);
38
+ signal.removeEventListener('abort', onAbort);
39
+ return true;
40
+ };
41
+ const onAbort = () => {
42
+ if (cleanup()) reject(signal.reason);
37
43
  };
38
- const timer = setTimeout(() => {
39
- cleanup();
40
- resolve();
44
+ timer = setTimeout(() => {
45
+ if (cleanup()) resolve();
41
46
  }, ms);
42
47
  signal.addEventListener('abort', onAbort, { once: true });
48
+ if (signal.aborted) onAbort();
43
49
  });
44
50
  }
45
51