/** * The batching ingest transport. * * Everything in this file exists to keep a log call cheap and safe for the * caller: `enqueue` does bounded synchronous work and returns, and the network * happens later on a timer or a size trigger. Nothing here is allowed to throw * into application code, and nothing here is allowed to write through the host * logger, because this package is usually installed as a drain on that very * logger and a diagnostic would loop straight back in. * * The status handling is the interesting part, and it is dictated by * `docs/ingest.md`: a `201` is not proof of delivery (reconcile * `stored`/`dropped`/`skipped`), `401`/`403`/`404` are permanent and retrying * them is a self-inflicted flood, `413` means split rather than back off, and * `429` always carries `Retry-After`. */ import type { DisabledReason, LogHQConfig, LogHQEntry, LogHQLevel, LogHQUser, ResolvedConfig } from './types'; /** * Parse `https://@`. * * A DSN without a key is not a usable DSN, so it returns null rather than half * a config that would fail later with a less obvious error. */ export declare function parseDsn(dsn: string): { host: string; key: string; } | null; /** * Apply defaults, DSN parts, and bounds. * * Explicit `key`/`host` beat the DSN because the DSN is the bulk convenience * and an explicit field is a deliberate override. Numeric options are clamped * rather than validated: a bad `batchSize` should not stop a process from * logging, and 501 entries per request would be silently halved by the server's * 500 cap anyway. */ export declare function resolveConfig(config: LogHQConfig): ResolvedConfig; export declare class LogHQClient { readonly config: ResolvedConfig; private queue; private timer; private disabledBy; /** Epoch ms before which nothing is sent, set from a `429` `Retry-After`. */ private pausedUntil; /** Serializes drains so two flushes cannot splice the same queue. */ private chain; /** The drain currently on the wire, if any. See {@link flush}. */ private running; /** Entries this client is known to have thrown away, for the debug channel. */ private lost; /** * How many times a batch has come back to the queue. * * The drain loops use this to tell progress from a stall. Counting requeues * rather than watching the queue length is what makes them immune to entries * arriving while a send is in flight. */ private requeues; constructor(config: LogHQConfig); setUser(user: LogHQUser | null): void; setRelease(release: string): void; setEnvironment(environment: string): void; /** False once disabled, closed, or switched off in config. */ isEnabled(): boolean; /** * Why this client stopped, or null. * * Null while a client is merely configured `enabled: false`: that is not a * failure the SDK diagnosed, so there is no reason to report. */ disabledReason(): DisabledReason | null; /** * The single ingress. Everything else in this class funnels through here. * * Filtering happens before defaulting so a dropped entry costs almost * nothing, and clipping happens after `beforeSend` so a hook that enriches an * entry cannot push it back over a wire limit. */ enqueue(entry: LogHQEntry): void; capture(level: LogHQLevel, message: string, context?: Record): void; /** * Report a thrown value. * * The stack goes in `context` because loghq stores a flat stream with no * stack column, and the type is folded into the message because there is no * type column either: `TypeError: x is not a function` is what a reader needs * on the line itself. */ captureException(err: unknown, extra?: Record): void; /** * Clip to the wire limits. * * Runs last so that whatever is queued is exactly what will be sent, and the * server has nothing left to truncate. */ private clip; /** * Hold the queue under its ceiling by dropping the oldest entries. * * Oldest-first is the deliberate choice: during an incident the newest lines * are the ones describing what is happening now, and a queue that drops the * tail would preserve the calm before it instead. */ private enforceQueueLimit; /** Put a batch back at the head of the queue, oldest-first order preserved. */ private requeue; private discard; private scheduleFlush; private clearTimer; /** * Drain the queue. * * Resolves true only when every queued entry was accepted and the server * reported none skipped. A rate-limit pause, a permanent disable, or a batch * that exhausted its retries all resolve false, so a caller flushing before * exit can tell delivery from an attempt. * * A drain already on the wire counts toward this verdict. Hitting `batchSize` * starts one on its own, so entries the caller just logged are often in that * drain rather than in the queue this call will take, and ignoring it would * report a clean flush for a batch that had just been refused. */ flush(): Promise; /** * Queue a drain behind whatever is already draining, and publish it as the * one in flight. * * `running` is assigned synchronously, before the chained drain gets its * microtask, because the caller most likely to need it is a `flush()` landing * in the same tick as the `batchSize` trigger that scheduled this one. * * The drain itself also starts synchronously when nothing is already * draining. Going through `.then()` unconditionally cost the `beforeExit` * flush its request on Bun, which exits without running a pending microtask: * the handler queued the drain, the process ended, and the fetch was never * issued. Deferring only when there is genuinely something to queue behind * keeps the serialization guarantee and gets the request onto the wire in the * same tick. `running === null` implies the previous drain settled, so this * cannot overlap two drains. */ private chainDrain; private drain; /** * True while sending would be pointless: permanently stopped, or paused. * * `closed` is deliberately not one of these. `close()` sets it before running * the final drain, so counting it here gave that drain a retry budget of one: * the first network blip inside `close()` reached `backoff`, saw a disabled * client, and threw the batch away rather than retrying it. Ingress is closed * by `isEnabled`, which does treat `closed` as disabled. */ private blocked; /** Permanently stopped. No later send can succeed, so nothing should queue. */ private stopped; /** * Stop for good. * * `401`/`403`/`404` never become success by being retried, and a client that * keeps trying turns a misconfigured key into an outbound flood. The queue is * released too, since nothing in it can ever be delivered. */ private disable; private deliver; private post; /** * Read `stored`/`dropped`/`skipped` off a `201` and act on the difference. * * A `201` is an acknowledgement of the request, not of the entries. `dropped` * entries fell past the server's 500 cap without ever being examined, so they * are worth resending; `skipped` entries were examined and refused, so * resending them would produce the same refusal and waste the quota. */ private reconcile; /** * Halve an oversized batch and send both halves. * * A single entry that still comes back `413` is over the request limit all by * itself, and no amount of splitting will save it. */ private split; /** * Find the entries that cannot be serialized and drop only those. * * Bisecting costs log2(n) extra requests in the rare case something in the * batch is unserializable, and nothing at all otherwise, which is a better * trade than losing every entry that happened to be queued alongside it. */ private isolate; /** * Deliver a batch as two halves. * * The attempt count is deliberately not incremented. Neither caller is * reacting to a transport failure: a `413` is a sizing answer and a * serialization throw is a content answer, so each half deserves its own full * retry budget. */ private halve; /** * Pause every send until the server's deadline. * * The pause is client-wide rather than per batch: the limit is charged per * entry per project, so another batch racing ahead would only deepen it. */ private pause; /** * Wait, then try the same batch again. * * Jittered because every instance of an app hits the same outage at the same * moment, and a bare doubling would send them all back in lockstep. */ private backoff; /** * Flush and shut down. * * Ingress closes first so a log call racing the shutdown cannot extend the * drain forever, but `closed` is not treated as a transport failure, so the * final flush still gets to run. */ close(): Promise; private finalDrain; /** * Write to the console, never to the host logger. * * This SDK is normally installed as a drain on the application's logger, so a * diagnostic routed through that logger would come straight back in as an * entry, which produces another diagnostic. Console is the only safe channel, * and it stays silent unless `debug` is on. */ private debug; }