@steve31415/baselib 3.22.0 → 3.23.0

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.
@@ -31,6 +31,12 @@ export function createBrowserLogger(opts = {}) {
31
31
  // remains best-effort, so a browser give-up is only console-noted.
32
32
  batchSize: 20,
33
33
  fetchInit: { keepalive: true },
34
+ // Browser logs have no stdout backstop, and the ERRORs most worth
35
+ // keeping are often the ones an offline stretch causes: while the
36
+ // browser reports itself offline, failed attempts don't count, so a
37
+ // batch waits out the outage (until the page dies or the buffer cap
38
+ // evicts it) instead of being dropped after ~60 s.
39
+ isOffline: () => navigator.onLine === false,
34
40
  onGiveUp: (batch, error, reason) => console.warn(`log ship dropped a batch (${reason}): ${String(error)}`, {
35
41
  batch_id: batch.id,
36
42
  events: batch.events.length,
@@ -66,6 +72,7 @@ export function createBrowserLogger(opts = {}) {
66
72
  // Ship what's pending whenever the page may be going away.
67
73
  const flushNow = () => void logger.flush();
68
74
  addEventListener('pagehide', flushNow);
75
+ addEventListener('online', () => shipper?.resume());
69
76
  document.addEventListener('visibilitychange', () => {
70
77
  if (document.visibilityState === 'hidden')
71
78
  flushNow();
@@ -67,6 +67,13 @@ export interface ShipperOptions {
67
67
  fetchFn?: typeof fetch;
68
68
  /** Extra fetch options (browser sets keepalive). */
69
69
  fetchInit?: RequestInit;
70
+ /** True while the platform knows it has no network (the browser passes
71
+ * `navigator.onLine === false`). A failed attempt made while offline says
72
+ * nothing about the batch or the ingest service, so it is not counted
73
+ * toward maxAttempts and not reported: the batch waits and retries at the
74
+ * longest backoff delay, or at once on resume(). The signal only decides
75
+ * what a failure means — attempts still run while "offline". */
76
+ isOffline?: () => boolean;
70
77
  /** A delivery attempt failed; the batch will be retried. */
71
78
  onAttemptFailure?: (batch: ShippedBatch, error: unknown) => void;
72
79
  /** The batch is abandoned — its events will never reach Axiom. */
@@ -101,6 +108,9 @@ export declare class LogShipper {
101
108
  * — raced with one attempt timeout, so it is bounded even when the pump
102
109
  * is busy with older batches. */
103
110
  flush(): Promise<void>;
111
+ /** Retry every waiting batch now, ahead of its backoff — the browser
112
+ * calls this when it comes back online. */
113
+ resume(): void;
104
114
  /** Shutdown path: one parallel last-chance attempt for everything
105
115
  * undelivered, then a sweep that marks the rest dropped. Single-flight
106
116
  * while in flight (racing SIGTERM callers join one promise), but re-arms
package/dist/log-core.js CHANGED
@@ -111,6 +111,13 @@ export class LogShipper {
111
111
  return raceTimeout(Promise.allSettled([...this.inflight]), cap);
112
112
  return Promise.resolve();
113
113
  }
114
+ /** Retry every waiting batch now, ahead of its backoff — the browser
115
+ * calls this when it comes back online. */
116
+ resume() {
117
+ for (const b of this.pending)
118
+ b.nextAttemptAt = 0;
119
+ void this.pump();
120
+ }
114
121
  /** Shutdown path: one parallel last-chance attempt for everything
115
122
  * undelivered, then a sweep that marks the rest dropped. Single-flight
116
123
  * while in flight (racing SIGTERM callers join one promise), but re-arms
@@ -229,8 +236,15 @@ export class LogShipper {
229
236
  batch.attempting = false;
230
237
  if (batch.terminal)
231
238
  return;
232
- batch.attempt++;
233
239
  batch.settleFirst();
240
+ if (!outcome.ok && !outcome.terminal && this.opts.isOffline?.()) {
241
+ // Uncounted: the budget is for failures that could be the batch's
242
+ // or the service's, and an offline failure is neither.
243
+ batch.nextAttemptAt = Date.now() + this.retryDelaysMs[this.retryDelaysMs.length - 1];
244
+ this.schedulePump();
245
+ return;
246
+ }
247
+ batch.attempt++;
234
248
  if (outcome.ok) {
235
249
  this.remove(batch);
236
250
  if (outcome.failed > 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.22.0",
3
+ "version": "3.23.0",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",