@semiont/http-transport 0.5.33 → 0.5.34
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/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import ky, { HTTPError } from 'ky';
|
|
2
2
|
import { Subject, BehaviorSubject } from 'rxjs';
|
|
3
|
-
import { RESOURCE_BROADCAST_TYPES, PERSISTED_EVENT_TYPES, BRIDGED_CHANNELS, SemiontError, uuidV4, retryWithBackoff, isRetryableRequestError, busLog, busLogEnabled } from '@semiont/core';
|
|
3
|
+
import { RESOURCE_BROADCAST_TYPES, PERSISTED_EVENT_TYPES, BRIDGED_CHANNELS, SemiontError, uuidV4, retryWithBackoff, isRetryableRequestError, busLog, busLogEnabled, equalJitter } from '@semiont/core';
|
|
4
4
|
import { getActiveTraceparent, recordBusEmit, withSpan, SpanKind, extractTraceparent, withTraceparent } from '@semiont/observability';
|
|
5
5
|
import { share, filter, map } from 'rxjs/operators';
|
|
6
6
|
|
|
@@ -143,7 +143,7 @@ function createActorStateUnit(options) {
|
|
|
143
143
|
const backoffDelay = () => {
|
|
144
144
|
const cap = Math.min(reconnectMs * 2 ** retryAttempt, MAX_RECONNECT_MS);
|
|
145
145
|
retryAttempt++;
|
|
146
|
-
return
|
|
146
|
+
return equalJitter(cap);
|
|
147
147
|
};
|
|
148
148
|
const connect = async (keepPrevious = false) => {
|
|
149
149
|
const token = getToken();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/transport/sse-connect-error.ts","../src/transport/api-error.ts","../src/transport/actor-state-unit.ts","../src/transport/http-transport.ts","../src/transport/http-content-transport.ts"],"names":["SemiontError","Subject","BehaviorSubject","busLog","withSpan","SpanKind","getActiveTraceparent","body","err"],"mappings":";;;;;;;AAqBO,IAAM,eAAA,GAAN,cAA8B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AC1E,MAAA,UAAA,CAAW,GAAG,CAAA;AACd,MAAA,MAAA,CAAO,GAAG,CAAA;AAAA,IACZ,CAAA;AAEA,IAAA,GAAA,CAAI,YAAY,MAAM;AACpB,MAAA,MAAM,GAAA,GAAM,IAAI,QAAA,CAAS,kBAAA,EAAoB,GAAG,SAAS,CAAA;AACzD,MAAA,UAAA,CAAW,GAAG,CAAA;AACd,MAAA,MAAA,CAAO,GAAG,CAAA;AAAA,IACZ,CAAA;AAEA,IAAA,GAAA,CAAI,UAAU,MAAM;AAIlB,MAAA,MAAM,GAAA,GAAM,IAAI,QAAA,CAAS,gBAAA,EAAkB,GAAG,SAAS,CAAA;AACvD,MAAA,UAAA,CAAW,GAAG,CAAA;AACd,MAAA,MAAA,CAAO,GAAG,CAAA;AAAA,IACZ,CAAA;AAEA,IAAA,IAAI,MAAA,EAAQ;AACV,MAAA,MAAM,OAAA,GAAU,MAAM,GAAA,CAAI,KAAA,EAAM;AAChC,MAAA,MAAA,CAAO,iBAAiB,OAAA,EAAS,OAAA,EAAS,EAAE,IAAA,EAAM,MAAM,CAAA;AAAA,IAG1D;AAEA,IAAA,GAAA,CAAI,KAAK,QAAQ,CAAA;AAAA,EACnB,CAAC,CAAA;AACH","file":"index.js","sourcesContent":["import { SemiontError } from '@semiont/core';\n\n/**\n * A refused SSE connect — `POST /bus/subscribe` answered non-2xx (or 2xx\n * with no body). The status is CARRIED, not interpolated into the message:\n * P3's backoff/terminal split reads it (auth-refused is terminal in kind;\n * 5xx is transient), and it surfaces on `ActorStateUnit.errors$` so a\n * refused client is observable instead of a silent retry loop\n * (SSE-AUTH-RESILIENCE P2, shape B).\n *\n * Extends `SemiontError` (core) rather than reusing `APIError`: APIError\n * lives in http-transport.ts, which imports the actor — reaching for it\n * there would buy one inherited field with an import cycle.\n *\n * Its OWN module, not `actor-state-unit.ts`, because `*-state-unit.ts` files\n * carry no class declarations: state units are plain-object factories, and\n * `audit-state-unit-no-class.sh` is the static half of the A1 axiom whose\n * allowlist is deliberately empty. An error type is not state-unit\n * machinery, so it moves rather than earning that gate its first exception.\n * A leaf module importing only core also keeps the cycle shut.\n */\nexport class SseConnectError extends SemiontError {\n readonly status: number;\n constructor(status: number) {\n super(`SSE connect failed: ${status}`, 'SSE_CONNECT_FAILED', { status });\n this.name = 'SseConnectError';\n this.status = status;\n }\n}\n","/**\n * `APIError` — the transport's HTTP failure, carrying the status it came from.\n *\n * Lives in its own module rather than in `http-transport.ts` because\n * `actor-state-unit.ts` throws it too (SIDECAR-BOOT-RESILIENCE P2) and\n * `http-transport.ts` imports `actor-state-unit.ts` — so the obvious home is a\n * cycle. The alternative, a second status-bearing error class beside this one,\n * is the duplicated shape the house rules forbid: there would then be two\n * answers to \"how does an HTTP failure carry its status\", and the retry\n * predicate could only agree with one of them.\n */\n\nimport { SemiontError, type TransportErrorCode, type HttpStatusError } from '@semiont/core';\n\nfunction classifyApiCode(status: number): TransportErrorCode {\n if (status === 400) return 'bad-request';\n if (status === 401) return 'unauthorized';\n if (status === 403) return 'forbidden';\n if (status === 404) return 'not-found';\n if (status === 409) return 'conflict';\n if (status >= 500) return 'unavailable';\n return 'error';\n}\n\nexport class APIError extends SemiontError {\n declare code: TransportErrorCode;\n readonly status: number;\n readonly statusText: string;\n\n constructor(message: string, status: number, statusText: string, body?: unknown) {\n super(message, classifyApiCode(status), { status, statusText, body });\n this.name = 'APIError';\n this.status = status;\n this.statusText = statusText;\n }\n}\n\n/**\n * The contract between this class and core's `isRetryableRequestError`, asserted\n * at compile time (SIDECAR-BOOT-RESILIENCE P1/P2).\n *\n * Core cannot name `APIError` — http-transport depends on core, not the reverse —\n * so the predicate reads the `status` field structurally. Without this line that\n * agreement is a coincidence: rename `status` here and every 429 silently stops\n * being retryable, with no test failing, because a predicate that finds no status\n * simply answers `false`. **A silent loss of retry is exactly the failure this\n * plan exists to prevent**, so it is pinned by the compiler rather than by a test.\n */\nconst _conformsToRetryContract: HttpStatusError = new APIError('', 0, '');\nvoid _conformsToRetryContract;\n","import { BehaviorSubject, Observable, Subject } from 'rxjs';\nimport { filter, map, share } from 'rxjs/operators';\nimport { busLog, busLogEnabled, uuidV4, retryWithBackoff, isRetryableRequestError, type components, type ConnectionState, type StateUnit, type RetryPolicy } from '@semiont/core';\nimport {\n SpanKind,\n extractTraceparent,\n getActiveTraceparent,\n withSpan,\n withTraceparent,\n} from '@semiont/observability';\nimport { SseConnectError } from './sse-connect-error';\nimport { APIError } from './api-error';\n\nexport type { ConnectionState };\n\nexport interface BusEvent {\n channel: string;\n payload: Record<string, unknown>;\n scope?: string;\n}\n\n\nexport interface ActorStateUnitOptions {\n baseUrl: string;\n token: string | (() => string);\n channels: string[];\n /**\n * Base of the failure-retry backoff ladder AND the flat cadence of the\n * `unauthenticated` waiting tick (which polls the token getter, no\n * network). A failure retry waits jitter(min(reconnectMs·2ⁿ, 60 s));\n * n resets on a successful open. Default 5 s.\n */\n reconnectMs?: number;\n /**\n * The SAME hook `HttpTransportConfig.tokenRefresher` wires into the HTTP\n * beforeRetry path (SSE-AUTH-RESILIENCE P4, D2 — no second refresh\n * mechanism). Consulted ONCE per outage when a connect is refused 401,\n * before parking `unauthenticated`; a successful open re-arms it. The\n * refresher's owner rotates the token SOURCE (sessions push the new token\n * into `token$`); the actor then reconnects through the getter, so the\n * token stays single-sourced. A throwing refresher is treated as `null`.\n */\n tokenRefresher?: () => Promise<string | null>;\n /**\n * Remove-side reconnect hysteresis (MULTI-RESOURCE-SCOPE). Scope\n * additions need liveness quickly (100 ms debounce), but a removal only\n * narrows delivery — extra events for a just-released scope are\n * idempotent locally — so remove-only changes wait this long before\n * reconnecting. Keeps hover-churn (transient per-citation previews)\n * from turning every mouse pass into a reconnect storm; any addition\n * flushes pending removals with it on the fast path.\n */\n lazyRemoveMs?: number;\n /**\n * B17 (LOCAL-STORAGE) — IO-abstracted persistence of the last seen\n * PERSISTED event id PER SCOPE, so a reloaded client resumes each\n * scope's replay instead of gapping. `load` runs once at construction;\n * `save` fires per persisted (`p-*`) id with that frame's scope —\n * ephemeral (`e-*`) ids are never saved: they carry no replay meaning,\n * and letting them displace a scope's watermark was exactly the silent\n * replay-loss hole the single-id design had. The transport stays\n * storage-free; callers wrap their own adapter in these thunks.\n */\n loadLastEventIds?: () => Record<string, string> | null;\n saveLastEventId?: (scope: string, id: string) => void;\n}\n\n/** Time in the `reconnecting` state before transitioning to `degraded`. */\nexport const DEGRADED_THRESHOLD_MS = 3_000;\n\n/**\n * Ceiling for the failure-retry backoff (SSE-AUTH-RESILIENCE P3, D1a): the\n * exponential ladder from `reconnectMs` caps here, so a long outage settles\n * into roughly one attempt per minute rather than growing without bound.\n */\nconst MAX_RECONNECT_MS = 60_000;\n\n/**\n * Deadline on the `/bus/emit` POST (JOB-RESTART-SAFETY P7). The gateway\n * accepts an emit and returns 202 promptly; a POST that has not resolved by\n * here means the gateway is unresponsive (mid-restart, overwhelmed), and the\n * emit is rejected rather than awaited forever. This is the transport-level\n * bound behind the 2026-09-03 finalization hang: a worker's mark:create /\n * job:complete emit to a wedged gateway used to hang the worker loop with no\n * timeout of its own. The rejection surfaces as a job failure the queue\n * classifies transient (an unreachable gateway is not the request's fault),\n * so the work retries instead of wedging. Covers EVERY emit and every job\n * type — not just the reference-annotation persist P6 bounded.\n */\nexport const EMIT_TIMEOUT_MS = 30_000;\n\n/**\n * Retry budget for ONE `/bus/emit` POST (SIDECAR-BOOT-RESILIENCE D3).\n *\n * Per request, deliberately — not per boot pass. \"Retry when one settles\" is\n * advice about a single request; re-running a whole catch-up pass to recover\n * from one refusal re-sends hundreds of already-successful emits, which is the\n * amplification that wedged the weaver in the first place.\n *\n * Small on purpose. `EMIT_TIMEOUT_MS` bounds each attempt, so the worst case\n * here is 4 attempts plus up to ~7s of jittered waiting, and an emit that a\n * caller is awaiting must fail while the caller still cares. The patience for a\n * gateway that is genuinely down belongs to the boot pass above it, not here.\n */\nexport const EMIT_RETRY: RetryPolicy = {\n attempts: 4,\n initialDelayMs: 1_000,\n maxDelayMs: 4_000,\n};\n\n/**\n * How long a superseded connection keeps DRAINING after a make-before-break\n * handoff before being aborted. Aborting the old connection the instant the\n * new one opened discarded replies already written to the old socket but not\n * yet read by the client (a freshly-hydrating page's main thread is busy —\n * exactly the N-concurrent-loaders-at-connect repro in\n * .plans/bugs/concurrent-browse-resource-starvation.md). The overlap is safe:\n * persisted ids are stable and correlated-reply ids are deterministic\n * (`e-<channel>:<cid>`, routes/bus.ts), so `seenEventIds` dedups double\n * delivery.\n */\nexport const LINGER_MS = 1_000;\n\nexport interface ActorStateUnit extends StateUnit {\n on$<T = Record<string, unknown>>(channel: string): Observable<T>;\n emit(channel: string, payload: Record<string, unknown>, emitScope?: string): Promise<number>;\n state$: Observable<ConnectionState>;\n /**\n * Refused connects (SSE-AUTH-RESILIENCE P2). One `SseConnectError` per\n * non-2xx `/bus/subscribe` answer, carrying the HTTP status as\n * structured data. Network-level failures (fetch rejections) have no\n * status and do not emit here — they stay on the reconnect path.\n */\n errors$: Observable<SseConnectError>;\n /** With `scope`: upsert channels into that scope's matrix entry. Without: global channels. */\n addChannels(channels: string[], scope?: string): void;\n /**\n * Whether `channel` is in the current GLOBAL subscription set — i.e. the\n * gateway delivers it on this connection. Correlated replies always ride\n * global channels, so this is `busRequest`'s fail-fast probe on a\n * narrowed-subscription transport (see `BusRequestPrimitive.isSubscribed`).\n */\n isSubscribed(channel: string): boolean;\n /** With `scope`: remove channels from that scope's entry (empty entry drops the scope). Without: global channels. */\n removeChannels(channels: string[], scope?: string): void;\n /**\n * Correlated-reply retention, client side (BUS-RESUMPTION Phase 2 /\n * SDK-DEBT S1): register a busRequest correlationId as awaiting its\n * reply. Every connect body includes the currently-tracked set as\n * `pendingReplies`, so a reply published while the connection was down\n * is replayed from the server's retention buffer. The returned disposer\n * (idempotent) removes the id on settle.\n */\n trackReply(correlationId: string): () => void;\n start(): void;\n stop(): void;\n}\n\n/** Allowed transitions in the connection state machine. */\nconst ALLOWED_TRANSITIONS: Record<ConnectionState, ReadonlyArray<ConnectionState>> = {\n initial: ['connecting', 'unauthenticated', 'closed'],\n connecting: ['open', 'reconnecting', 'unauthenticated', 'closed'],\n open: ['reconnecting', 'closed'],\n reconnecting: ['connecting', 'degraded', 'unauthenticated', 'closed'],\n // `degraded → reconnecting` is a legitimate recovery edge: a channel-set\n // change (`addChannels`/`removeChannels`) schedules a reconnect that can\n // fire while the connection is degraded. Omitting it made `reconnect()`\n // throw a fatal, uncaught exception from the reconnect timer (#844).\n degraded: ['connecting', 'reconnecting', 'unauthenticated', 'closed'],\n // Leaving `unauthenticated` takes a usable credential (the gate saw a\n // different, non-empty token) → straight to `connecting`; or teardown.\n unauthenticated: ['connecting', 'closed'],\n closed: [],\n};\n\nexport function createActorStateUnit(options: ActorStateUnitOptions): ActorStateUnit {\n const { baseUrl, token: tokenOrGetter, channels: initialChannels, reconnectMs = 5_000, lazyRemoveMs = 5_000, tokenRefresher } = options;\n const getToken = typeof tokenOrGetter === 'function' ? tokenOrGetter : () => tokenOrGetter;\n\n const globalChannels = new Set(initialChannels);\n /** The subscription matrix's scoped half: scope → channels (MULTI-RESOURCE-SCOPE). */\n const scopedSubscriptions = new Map<string, Set<string>>();\n /**\n * Per-scope resumption watermarks: the last PERSISTED (`p-*`) id seen for\n * each scope. Sent as `lastEventId` on that scope's matrix entry so the\n * server replays each scope's own gap. A scope keeps its watermark after\n * its channels are removed — re-subscribing later replays what was missed\n * in between. Ephemeral ids never touch this map.\n */\n const scopeWatermarks = new Map<string, string>(\n Object.entries(options.loadLastEventIds?.() ?? {}),\n );\n /** Outstanding busRequest correlationIds — ride every connect body (S1). */\n const pendingReplies = new Set<string>();\n /**\n * This bus client's routing address for correlated replies\n * (CORRELATED-REPLY-ROUTING D1). Minted once per ACTOR — deliberately not\n * per connection: a make-before-break handover runs two connections at\n * once and a reconnect replaces one, so a per-connection id would strand\n * the reply on the dying socket. Both overlap connections present this\n * same address, and the deterministic `e-<channel>:<cid>` id dedups the\n * double delivery exactly as it does today.\n *\n * Not persisted: \"stable\" means across transport reconnects, not across\n * page reloads. A reload builds a new actor, and its `pendingReplies`\n * replay is what recovers in-flight replies (S1).\n */\n const clientId = uuidV4();\n\n const events$ = new Subject<BusEvent>();\n const state$ = new BehaviorSubject<ConnectionState>('initial');\n const errors$ = new Subject<SseConnectError>();\n let currentState: ConnectionState = 'initial';\n let degradedTimer: ReturnType<typeof setTimeout> | null = null;\n\n /**\n * Move the state machine to `next`. An unexpected edge is logged and\n * ignored — NOT thrown. `transition()` runs inside timer callbacks (the\n * reconnect and degraded timers), so a throw here is an uncaught exception\n * that takes down the host process (#844). A bad edge means a bug in the\n * reconnect loop, but degrading gracefully (keep the current state, warn)\n * is strictly better than killing a long-running job. The permitted edges\n * — including the `degraded → reconnecting` recovery edge — are in\n * `ALLOWED_TRANSITIONS`.\n *\n * Side effect: manages the `degraded` timer. Enters on\n * `reconnecting`, cleared on exit.\n */\n const transition = (next: ConnectionState): void => {\n if (currentState === next) return;\n const allowed = ALLOWED_TRANSITIONS[currentState];\n if (!allowed.includes(next)) {\n console.warn(`[actor] ignoring invalid connection state transition: ${currentState} → ${next}`);\n return;\n }\n const prev = currentState;\n currentState = next;\n\n if (next === 'reconnecting' && prev !== 'reconnecting') {\n // Starting a reconnect cycle — arm the degraded-threshold timer.\n if (degradedTimer) clearTimeout(degradedTimer);\n degradedTimer = setTimeout(() => {\n if (currentState === 'reconnecting') transition('degraded');\n }, DEGRADED_THRESHOLD_MS);\n }\n if (prev === 'reconnecting' && next !== 'reconnecting') {\n // Leaving reconnecting (to connecting, degraded, or closed) —\n // the timer is either no longer relevant or has just fired.\n if (degradedTimer) { clearTimeout(degradedTimer); degradedTimer = null; }\n }\n\n state$.next(next);\n };\n\n let running = false;\n /**\n * All in-flight SSE fetch controllers. Tracked as a Set because\n * connect() may race with itself under mount-churn or rapid channel-\n * set changes — whenever a new connect() starts we abort ALL previous\n * in-flight fetches rather than only the last-tracked one. A previous\n * single-slot implementation leaked orphaned streams (diagnosed by\n * observing 3 concurrent SSE subscribes in the /bus/subscribe network\n * log, each delivering duplicate RECV frames). Using a Set guarantees\n * at most one live stream post-reconnect regardless of race order.\n */\n const inflightControllers = new Set<AbortController>();\n let reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n\n /**\n * Connections retired by a make-before-break handoff. A superseded\n * connection lingers (still draining) for LINGER_MS before its abort; its\n * read loop ending — naturally or via that abort — must NOT drive the\n * actor-wide reconnect logic, which belongs to the live connection only.\n */\n const superseded = new WeakSet<AbortController>();\n /** Pending linger-abort timers, cleared on stop/dispose. */\n const lingerTimers = new Set<ReturnType<typeof setTimeout>>();\n\n /**\n * Recently-delivered event ids, to dedup the make-before-break overlap: the\n * brief window where the old and new connection both deliver the same live\n * event during a scope-change handoff. Persisted ids (`p-<scope>-<seq>`) are\n * stable across connections, so this collapses such an overlap to a single\n * emission. Ephemeral ids (`e-<connectionId>-<counter>`) are per-connection,\n * so a cross-connection ephemeral duplicate is NOT caught here — its\n * consumers tolerate the rare double (a correlation reply is taken with\n * `take(1)`; cache invalidations and job-completion are idempotent/terminal).\n * Bounded FIFO (insertion-ordered Set) to cap memory.\n *\n * Cost note: this is *always-on* — every delivered event does a has/add here\n * — yet a duplicate is only possible during a handoff overlap; in steady\n * state there's a single connection and nothing can collide. So every\n * consumer of this transport carries a small standing structure for a path\n * that fires only on (now-rare) scope changes. It's left unconditional\n * because the per-event cost is negligible next to the JSON.parse + trace\n * span already on this path. If that ever stops being true, scope it to the\n * overlap (build on handoff start, drop once the old read loop exits) or\n * track a high-water `Map<scope, maxSeq>` instead of every id.\n */\n const seenEventIds = new Set<string>();\n const SEEN_EVENT_IDS_MAX = 512;\n const rememberEventId = (id: string): void => {\n seenEventIds.add(id);\n if (seenEventIds.size > SEEN_EVENT_IDS_MAX) {\n const oldest = seenEventIds.values().next().value;\n if (oldest !== undefined) seenEventIds.delete(oldest);\n }\n };\n /** Release a claim whose apply threw, so a redelivery is re-processed\n * rather than swallowed by its own dedup entry. */\n const forgetEventId = (id: string): void => {\n seenEventIds.delete(id);\n };\n\n const shared$ = events$.pipe(share());\n\n const disconnect = () => {\n for (const c of inflightControllers) {\n try { c.abort(); } catch { /* noop */ }\n }\n inflightControllers.clear();\n if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }\n for (const t of lingerTimers) clearTimeout(t);\n lingerTimers.clear();\n };\n\n /**\n * Drop-recovery scheduling (SSE-AUTH-RESILIENCE P3). One place arms\n * `reconnectTimer`, so the gate's waiting tick and the failure retry can\n * never disagree on cadence (the split-brain the P1 handoff warned about).\n * Failure retries BACK OFF (D1a): equal-jitter exponential — delay ∈\n * [cap/2, cap], cap = min(reconnectMs·2ⁿ, MAX_RECONNECT_MS) — so a downed\n * gateway sees a thinning trickle instead of 12 requests/min from every\n * client, jitter keeps N clients out of lockstep, and a transient failure\n * still recovers (backoff delays recovery; it never prevents it). The\n * WAITING tick stays flat at `reconnectMs`: it polls the token getter and\n * makes no requests, so there is no load to bound — and backing it off\n * would only slow recovery after a re-login. Scope-churn reconnects\n * (debounce/hysteresis timers) are a separate mechanism, untouched.\n */\n let retryAttempt = 0;\n let refusedToken: string | null = null;\n /**\n * One refresher consult per outage (SSE-AUTH-RESILIENCE P4). Burned on\n * the first 401 that consults; re-armed only by a successful open. Without\n * this, a gateway refusing every token turns refresh-then-401 into a loop\n * at connect cadence — the storm with extra steps.\n */\n let refreshBurned = false;\n const scheduleRetry = (delayMs: number, keepPrevious = false) => {\n if (!running) return;\n if (reconnectTimer) clearTimeout(reconnectTimer);\n reconnectTimer = setTimeout(() => {\n if (running) connect(keepPrevious);\n }, delayMs);\n };\n const backoffDelay = () => {\n const cap = Math.min(reconnectMs * 2 ** retryAttempt, MAX_RECONNECT_MS);\n retryAttempt++;\n return cap / 2 + Math.random() * (cap / 2);\n };\n\n const connect = async (keepPrevious = false) => {\n // ── The credential gate (SSE-AUTH-RESILIENCE P1 + P3, shapes A + B) ─\n //\n // A connect with no bearer CANNOT succeed (shape A: `HttpTransport`\n // renders an exhausted session's null `token$` as `''`). A connect\n // re-sending a bearer the gateway just REFUSED cannot succeed either\n // (shape B: the same credential gets the same 401). Both are\n // guaranteed-failing requests generated on a timer — the storm in\n // .plans/bugs/stale-sse-actor-401-loops-after-token-expiry.md — so\n // neither is attempted.\n //\n // This is D3 and D6a answered together: ONE `unauthenticated` state for\n // \"no credential yet\" and \"credential refused\", because they are the\n // same operational fact — not attempting, and only a usable DIFFERENT\n // credential changes anything. The finer cause (the 401) is on\n // `errors$`.\n //\n // We WAIT rather than give up: the tick polls the GETTER — no network,\n // so there is no load to bound, and the tick stays at the flat base.\n // Recovery is automatic: a re-login or session refresh rotates the\n // getter's value and the next tick connects. That makes a 401 terminal\n // PER-CREDENTIAL, not per-actor — a hard stop here would kill the SPA's\n // bus for good on an out-of-band revocation (logout elsewhere bumping\n // `tokenVersion`) that the session's next proactive refresh heals.\n const token = getToken();\n if (!token || token === refusedToken) {\n if (running) {\n if (currentState !== 'unauthenticated') transition('unauthenticated');\n scheduleRetry(reconnectMs, keepPrevious);\n }\n return;\n }\n refusedToken = null; // a different credential — worth attempting\n\n // Transition to `connecting` from whichever reconnect-ish state\n // we're currently in (`initial`, `reconnecting`, `degraded`).\n transition('connecting');\n\n // Snapshot the connections this connect() supersedes.\n // - keepPrevious=false (initial connect / drop-recovery): there is no\n // live connection worth preserving, so abort up front — this closes\n // the orphan-stream leak described above.\n // - keepPrevious=true (scope-change reconnect): MAKE-BEFORE-BREAK. Keep\n // the previous connection(s) ALIVE until the new one is `open`, then\n // abort them (below, after the fetch resolves), so an in-flight\n // ephemeral result isn't dropped in a reconnect gap (#847). The brief\n // window where old and new both deliver is deduped by event id.\n const previous = [...inflightControllers];\n if (!keepPrevious) {\n for (const c of previous) {\n try { c.abort(); } catch { /* noop */ }\n }\n inflightControllers.clear();\n }\n\n // POST subscription matrix (MULTI-RESOURCE-SCOPE): global channels plus\n // one entry per scope, each carrying its own resumption watermark.\n // `satisfies` is the drift-lock (the MEDIA_TYPES idiom): the body is\n // hand-written while the schema owns the shape, so the compiler — not a\n // reviewer — is what notices a required field going missing. That is\n // what makes `clientId` required in BusSubscribeRequest worth anything\n // on this side of the wire (CORRELATED-REPLY-ROUTING P2).\n const body = JSON.stringify({\n global: [...globalChannels],\n scoped: [...scopedSubscriptions.entries()].map(([scope, chans]) => {\n const watermark = scopeWatermarks.get(scope);\n return {\n scope,\n channels: [...chans],\n ...(watermark !== undefined ? { lastEventId: watermark } : {}),\n };\n }),\n ...(pendingReplies.size > 0 ? { pendingReplies: [...pendingReplies] } : {}),\n clientId,\n } satisfies components['schemas']['BusSubscribeRequest']);\n const url = `${baseUrl}/bus/subscribe`;\n\n const controller = new AbortController();\n inflightControllers.add(controller);\n\n try {\n const headers: Record<string, string> = {\n // The gate's single read: `token` is what this ATTEMPT sends, and\n // exactly what `refusedToken` records if the gateway says 401.\n Authorization: `Bearer ${token}`,\n 'Content-Type': 'application/json',\n };\n const response = await fetch(url, { method: 'POST', headers, body, signal: controller.signal });\n\n if (!response.ok || !response.body) {\n throw new SseConnectError(response.status);\n }\n\n // Stopped/disposed while the fetch was in flight — don't proceed to open\n // (and retire the old connection on) a stream we've been told to tear\n // down. `stop()`/`dispose()` already aborted this controller.\n if (!running) return;\n\n // Make-before-break handoff: the new connection is established (the\n // gateway has subscribed it and any `Last-Event-ID` replay is flowing),\n // so mark the previous connection(s) superseded and LINGER them — keep\n // them draining for LINGER_MS before the abort. Aborting immediately\n // here discarded replies already written to the old socket but not yet\n // read (the buffered-bytes loss in\n // .plans/bugs/concurrent-browse-resource-starvation.md); an event\n // delivered by both connections during the overlap is deduped by id in\n // the read loop below (persisted ids are stable; correlated-reply ids\n // are deterministic per routes/bus.ts). Had the fetch failed, we'd have\n // thrown above and never reached here, leaving the old connection live\n // (no gap).\n if (keepPrevious) {\n for (const c of previous) superseded.add(c);\n const lingerTimer = setTimeout(() => {\n lingerTimers.delete(lingerTimer);\n for (const c of previous) {\n try { c.abort(); } catch { /* noop */ }\n inflightControllers.delete(c);\n }\n }, LINGER_MS);\n lingerTimers.add(lingerTimer);\n }\n\n transition('open');\n retryAttempt = 0; // a success resets the backoff ladder\n refreshBurned = false; // …and re-arms the refresh-once (P4)\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n\n /**\n * Segments of the current, still-incomplete line. A single `data:`\n * line carries a whole JSON payload — a browse reply can run to\n * megabytes — delivered across many `reader.read()` chunks. The\n * previous `buffer += chunk` + `buffer.split('\\n')` re-flattened and\n * re-scanned the ENTIRE accumulated buffer on every read: O(frame² /\n * chunkSize) bytes of large-string allocation per frame, all landing\n * in V8's large-object space, which only major GC reclaims. Under the\n * reply fan-out burst (~85 multi-MB `browse:*-result` frames/min)\n * that allocation rate outran mark-compact and OOM'd the worker\n * (2026-09-03, DoD #7). Segments are joined exactly once, when the\n * line's newline arrives; each read scans only its own chunk.\n */\n let lineSegments: string[] = [];\n\n // SSE parse state is declared OUTSIDE the read loop: a single\n // event can span many `reader.read()` chunks when the payload is\n // large (a full resource-result with annotations can easily exceed\n // one TCP segment). Resetting these on every read would silently\n // drop any event whose `event:`/`id:` headers land in one chunk\n // and whose terminating blank line lands in the next.\n let currentEvent = '';\n let currentData = '';\n let currentId: string | undefined;\n\n while (running && inflightControllers.has(controller)) {\n const { done, value } = await reader.read();\n if (done) break;\n\n const text = decoder.decode(value, { stream: true });\n\n let searchFrom = 0;\n while (searchFrom <= text.length) {\n const nl = text.indexOf('\\n', searchFrom);\n if (nl === -1) {\n if (searchFrom < text.length) {\n lineSegments.push(searchFrom === 0 ? text : text.slice(searchFrom));\n }\n break;\n }\n let line = text.slice(searchFrom, nl);\n searchFrom = nl + 1;\n if (lineSegments.length > 0) {\n lineSegments.push(line);\n line = lineSegments.join('');\n lineSegments = [];\n }\n if (line.startsWith('event: ')) {\n currentEvent = line.slice(7);\n } else if (line.startsWith('data: ')) {\n currentData = line.slice(6);\n } else if (line.startsWith('id: ')) {\n currentId = line.slice(4);\n } else if (line === '') {\n // Skip an overlap duplicate — the same stable-id event delivered\n // by both the old and new connection during a make-before-break\n // handoff (#847). Ephemeral ids are unique per connection, so this\n // never spuriously drops a distinct event.\n const isDuplicate = currentId !== undefined && seenEventIds.has(currentId);\n if (currentEvent === 'bus-event' && currentData && !isDuplicate) {\n const parsed = JSON.parse(currentData) as BusEvent;\n busLog('RECV', parsed.channel, parsed.payload, parsed.scope);\n // Drain-window forensics: an event delivered by a SUPERSEDED\n // (lingering) connection is one that an immediate handover abort\n // would have discarded — the loss mode of\n // .plans/bugs/concurrent-browse-resource-starvation.md. Gated\n // (per-event, bursty during overlap); flip bus logging on to\n // see how real the window is.\n if (busLogEnabled() && superseded.has(controller)) {\n // eslint-disable-next-line no-console\n console.debug(`[bus LINGER] ${parsed.channel} delivered on superseded connection`);\n }\n // Tier 2: lift trace context off the SSE payload (the\n // gateway's writeBusEvent puts it there). The synchronous\n // fan-out to subscribers happens inside the bus.recv span,\n // so handlers see the parent trace.\n const carrier = extractTraceparent(\n parsed.payload as Record<string, unknown>,\n );\n // The two kinds of id bookkeeping sit on OPPOSITE sides of the\n // awaited fan-out, because they answer different questions.\n //\n // `seenEventIds` answers \"has this frame been claimed?\" and must\n // be recorded BEFORE the await: the await yields the event loop,\n // so during a make-before-break overlap the sibling connection\n // can read the same stable-id frame, find the set still missing\n // it, and deliver it a second time — defeating the overlap dedup\n // (#847) that .plans/bugs/BRIDGE-GAPS.md exists to protect.\n // Rolled back if the apply throws, so a redelivery after a\n // dropped read loop is re-processed rather than silently\n // swallowed by its own claim.\n if (currentId !== undefined) rememberEventId(currentId);\n try {\n await withTraceparent(carrier, () =>\n withSpan(\n `bus.recv:${parsed.channel}`,\n () => { events$.next(parsed); },\n {\n kind: SpanKind.CONSUMER,\n attrs: {\n 'bus.channel': parsed.channel,\n ...(parsed.scope ? { 'bus.scope': parsed.scope } : {}),\n },\n },\n ),\n );\n } catch (err) {\n if (currentId !== undefined) forgetEventId(currentId);\n throw err;\n }\n // The resume watermark and the persisted bookmark answer \"have\n // this event's effects been absorbed?\" and stay AFTER the apply.\n // The pre-fix order (stash first, then an AWAITED apply) opened a\n // gap where a bystander cache's debounced save could fire\n // mid-await, find every cache quiet, and flush a bookmark whose\n // event nothing had absorbed — the fast-path reload loss\n // (.plans/bugs/annotation-lost-on-immediate-reload-after-create.md).\n // Both stay on the LAGGING side, which is safe: a reconnect or\n // crash mid-apply resumes from the previous id and redelivers,\n // and re-invalidation is idempotent.\n //\n // Watermarks are PER SCOPE and persisted-ids-only: a `p-*` id is\n // stamped only on scoped deliveries (the frame always carries\n // `scope`), and ephemeral ids never displace a scope's watermark\n // — the silent replay-loss hole the old single-id design had.\n if (currentId !== undefined && currentId.startsWith('p-') && parsed.scope) {\n scopeWatermarks.set(parsed.scope, currentId);\n // B17: persist per scope — see ActorStateUnitOptions.\n options.saveLastEventId?.(parsed.scope, currentId);\n }\n }\n currentEvent = '';\n currentData = '';\n currentId = undefined;\n }\n }\n }\n } catch (err) {\n if ((err as Error).name === 'AbortError') return;\n // A refused connect carries its status out (P2). Anything else — a\n // network failure, a dropped stream — has no status to carry and\n // stays off errors$.\n if (err instanceof SseConnectError) {\n errors$.next(err);\n // 401: re-sending THIS bearer is deterministic, so park instead of\n // retrying (P3). The gate re-admits the actor the moment the getter\n // yields a different token; until then, no requests at all. Non-401\n // refusals (5xx, proxies) are transient in kind and take the\n // backoff path below.\n if (err.status === 401 && running && !superseded.has(controller)) {\n refusedToken = token;\n retryAttempt = 0;\n if (currentState !== 'unauthenticated') transition('unauthenticated');\n // P4: refresh ONCE before staying parked — the same hook the HTTP\n // beforeRetry path uses (D2). Parked state is truthful while the\n // refresh call is in flight. On success the refresher's owner has\n // rotated the token source, so an immediate tick reconnects\n // through the gate's getter read; on null/throw (or an unchanged\n // token) the flat waiting tick stands.\n if (tokenRefresher && !refreshBurned) {\n refreshBurned = true;\n let refreshed: string | null = null;\n try {\n refreshed = await tokenRefresher();\n } catch {\n // A throwing refresher is a failed refresh, not a crash.\n }\n if (running && refreshed && refreshed !== refusedToken) {\n scheduleRetry(0, keepPrevious);\n return;\n }\n }\n scheduleRetry(reconnectMs, keepPrevious);\n return;\n }\n }\n } finally {\n inflightControllers.delete(controller);\n }\n\n // If we reached here without an AbortError, the connection dropped\n // or the fetch failed (a 401 already diverted to `unauthenticated`\n // above). Transition to reconnecting and schedule a retry on the\n // backoff ladder — unless this was a SUPERSEDED (lingering)\n // connection ending: its termination is expected teardown, not a drop\n // of the live stream, and must not restart the reconnect machinery.\n if (running && !superseded.has(controller)) {\n transition('reconnecting');\n scheduleRetry(backoffDelay());\n }\n };\n\n const reconnect = () => {\n if (!running) return;\n // Transition to `reconnecting` BEFORE aborting the current\n // connection. This matches the pre-state-machine contract where\n // gap-detection relied on seeing a \"dropped\" signal before a\n // subsequent \"connected\" signal; with the state machine, the\n // transition sequence `open → reconnecting → connecting → open`\n // is what BrowseNamespace's gap-detection (pre-BUS-RESUMPTION\n // code path) watches for.\n if (currentState === 'open' || currentState === 'connecting' || currentState === 'degraded') {\n transition('reconnecting');\n }\n // Make-before-break: do NOT abort the live connection here. Cancel only a\n // pending drop-recovery retry, then connect — `connect(keepPrevious=true)`\n // retires the old connection after the new one is open (no gap).\n if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }\n connect(true);\n };\n\n // Debounce channel-set-change reconnects. React StrictMode in dev\n // produces mount → cleanup → mount synchronously, which previously\n // translated into three back-to-back reconnects — enough to tear down\n // in-flight responses, fire gap detection, refetch, tear that down\n // again, and leave the page stuck in \"Loading...\" while caches\n // thrashed. With a short debounce the whole sequence collapses into\n // one reconnect after the final channel-set is stable.\n //\n // Two cadences (MULTI-RESOURCE-SCOPE remove-side hysteresis): additions\n // take the fast 100 ms path (a new scope needs liveness now); remove-only\n // changes wait `lazyRemoveMs` — removal merely narrows delivery, and the\n // consumer's hover churn would otherwise reconnect on every mouse pass.\n // The connect body reads current state, so whichever timer fires first\n // carries ALL pending changes; a fast schedule therefore supersedes any\n // pending lazy one, and a lazy schedule never preempts a pending fast one.\n let reconnectTimer2: ReturnType<typeof setTimeout> | null = null;\n let lazyReconnectTimer: ReturnType<typeof setTimeout> | null = null;\n const RECONNECT_DEBOUNCE_MS = 100;\n const scheduleReconnect = () => {\n if (lazyReconnectTimer) { clearTimeout(lazyReconnectTimer); lazyReconnectTimer = null; }\n if (reconnectTimer2) clearTimeout(reconnectTimer2);\n reconnectTimer2 = setTimeout(() => {\n reconnectTimer2 = null;\n reconnect();\n }, RECONNECT_DEBOUNCE_MS);\n };\n const scheduleLazyReconnect = () => {\n if (reconnectTimer2 || lazyReconnectTimer) return; // a pending flush already covers this change\n lazyReconnectTimer = setTimeout(() => {\n lazyReconnectTimer = null;\n reconnect();\n }, lazyRemoveMs);\n };\n\n return {\n on$<T = Record<string, unknown>>(channel: string): Observable<T> {\n return shared$.pipe(\n filter((e) => e.channel === channel),\n map((e) => e.payload as T),\n );\n },\n\n emit: async (channel: string, payload: Record<string, unknown>, emitScope?: string): Promise<number> => {\n // EMIT logging + bus.emit span live at the transport contract layer\n // (`HttpTransport.emit`). ActorStateUnit is plumbing. We do propagate the\n // active span's W3C traceparent on the outbound POST so the gateway\n // can stitch the bus.dispatch server span as a child.\n const body: Record<string, unknown> = { channel, payload, clientId };\n if (emitScope) body.scope = emitScope;\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${getToken()}`,\n };\n const trace = getActiveTraceparent();\n if (trace) {\n headers['traceparent'] = trace.traceparent;\n if (trace.tracestate) headers['tracestate'] = trace.tracestate;\n }\n // Retried per request (SIDECAR-BOOT-RESILIENCE D3): a 429/503/504 or an\n // expired deadline gets another attempt; a 400/401/403 rejects on the\n // first, unretried. The predicate is core's, shared with the boot passes\n // above, so \"retryable\" means one thing across the fleet.\n //\n // The whole attempt — POST, status check, and the error it throws — is\n // inside the retried unit, because the refusal IS the failure being\n // classified. Retrying only the fetch would re-run the request and then\n // hand back the same unexamined response.\n const res = await retryWithBackoff(async () => {\n // Bounded (JOB-RESTART-SAFETY P7): an unresponsive gateway must not hang\n // the caller's loop forever. AbortSignal.timeout rejects with a\n // DOMException named TimeoutError — which the predicate treats as\n // retryable, since a deadline is the definition of \"try again\".\n const attempt = await fetch(`${baseUrl}/bus/emit`, {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n signal: AbortSignal.timeout(EMIT_TIMEOUT_MS),\n });\n // A refused emit (validation 400, auth 401…) must REJECT — busRequest's\n // contract detaches its doomed reply and propagates this to the caller.\n // Resolving a sentinel here instead leaves that caller waiting for a\n // reply the gateway will never send.\n if (!attempt.ok) {\n let detail = '';\n try {\n detail = (await attempt.text()).slice(0, 500);\n } catch {\n // status alone\n }\n // APIError, not a bare Error: the status rides as a FIELD (D1), which\n // is what makes it classifiable without parsing it back out of prose.\n // The message keeps its shape, so callers matching on it are unaffected.\n throw new APIError(\n `/bus/emit ${attempt.status}${detail ? `: ${detail}` : ''}`,\n attempt.status,\n attempt.statusText,\n detail || undefined,\n );\n }\n return attempt;\n }, isRetryableRequestError, EMIT_RETRY);\n // `-1` = count unknown (older gateway / unreadable body) — never let a\n // parse failure read as an empty room. Same sentinel as the Go client.\n try {\n const reply = (await res.json()) as { subscribers?: unknown };\n return typeof reply.subscribers === 'number' ? reply.subscribers : -1;\n } catch {\n return -1;\n }\n },\n\n state$: state$.asObservable(),\n\n errors$: errors$.asObservable(),\n\n isSubscribed: (channel: string) => globalChannels.has(channel),\n\n addChannels: (channels: string[], scope?: string) => {\n let changed = false;\n if (scope !== undefined) {\n let entry = scopedSubscriptions.get(scope);\n if (!entry) {\n entry = new Set<string>();\n scopedSubscriptions.set(scope, entry);\n }\n for (const ch of channels) {\n if (!entry.has(ch)) { entry.add(ch); changed = true; }\n }\n } else {\n for (const ch of channels) {\n if (!globalChannels.has(ch)) { globalChannels.add(ch); changed = true; }\n }\n }\n if (changed) scheduleReconnect();\n },\n\n removeChannels: (channels: string[], scope?: string) => {\n let changed = false;\n if (scope !== undefined) {\n const entry = scopedSubscriptions.get(scope);\n if (entry) {\n for (const ch of channels) {\n if (entry.delete(ch)) changed = true;\n }\n // The watermark survives the scope's removal deliberately: a later\n // re-subscribe replays what was missed in between.\n if (entry.size === 0) scopedSubscriptions.delete(scope);\n }\n } else {\n for (const ch of channels) {\n if (globalChannels.delete(ch)) changed = true;\n }\n }\n if (changed) scheduleLazyReconnect();\n },\n\n trackReply: (correlationId: string) => {\n pendingReplies.add(correlationId);\n let released = false;\n return () => {\n if (released) return;\n released = true;\n pendingReplies.delete(correlationId);\n };\n },\n\n start: () => {\n if (running) return;\n running = true;\n connect();\n },\n\n stop: () => {\n running = false;\n if (currentState !== 'closed') transition('closed');\n if (reconnectTimer2) { clearTimeout(reconnectTimer2); reconnectTimer2 = null; }\n if (lazyReconnectTimer) { clearTimeout(lazyReconnectTimer); lazyReconnectTimer = null; }\n if (degradedTimer) { clearTimeout(degradedTimer); degradedTimer = null; }\n disconnect();\n },\n\n dispose: () => {\n running = false;\n if (currentState !== 'closed') transition('closed');\n if (reconnectTimer2) { clearTimeout(reconnectTimer2); reconnectTimer2 = null; }\n if (lazyReconnectTimer) { clearTimeout(lazyReconnectTimer); lazyReconnectTimer = null; }\n if (degradedTimer) { clearTimeout(degradedTimer); degradedTimer = null; }\n disconnect();\n events$.complete();\n state$.complete();\n errors$.complete();\n },\n };\n}\n","/**\n * HttpTransport — the HTTP/SSE implementation of ITransport.\n *\n * Phase 1 of TRANSPORT-ABSTRACTION. Owns everything that crosses the wire\n * in remote mode: the bus actor (SSE + POST /bus/emit), auth/admin/exchange/\n * system HTTP endpoints, and connection-state plumbing.\n *\n * Does NOT own the local coordination bus — that lives on `SemiontClient`.\n * `bridgeInto(bus)` wires SSE-received events into the caller-supplied bus\n * once at construction.\n */\n\nimport ky, { HTTPError, type KyInstance } from 'ky';\nimport { BehaviorSubject, Observable, Subject } from 'rxjs';\nimport type {\n AccessToken,\n BaseUrl,\n Email,\n EventBus,\n EventMap,\n GoogleCredential,\n Logger,\n RefreshToken,\n ResourceId,\n UserDID,\n components,\n} from '@semiont/core';\nimport {\n PERSISTED_EVENT_TYPES,\n RESOURCE_BROADCAST_TYPES,\n SemiontError,\n busLog,\n} from '@semiont/core';\nimport { SpanKind, recordBusEmit, withSpan } from '@semiont/observability';\nimport { createActorStateUnit, type ActorStateUnit } from './actor-state-unit';\nimport { APIError } from './api-error';\nimport type {\n ConnectionState,\n IGatewayOperations,\n ITransport,\n HealthCheckResponse,\n StatusResponse,\n UserResponse,\n UpdateUserRequest,\n UpdateUserResponse,\n ListUsersResponse,\n} from '@semiont/core';\nimport { BRIDGED_CHANNELS } from '@semiont/core';\n\ntype AuthResponse = components['schemas']['AuthResponse'];\ntype TokenRefreshResponse = components['schemas']['TokenRefreshResponse'];\ntype AdminUserStatsResponse = components['schemas']['AdminUserStatsResponse'];\ntype OAuthConfigResponse = components['schemas']['OAuthConfigResponse'];\n\n// ── Channel constants (mirror client.ts) ────────────────────────────────\n\nexport const RESOURCE_SCOPED_CHANNELS = [\n // Exclude channels already globally bridged: a channel in both lists is\n // forwarded twice on a scoped connection (global copy → ephemeral id, scoped\n // copy → persisted id) with different SSE ids, escaping the client dedup\n // (.plans/bugs/BRIDGE-GAPS.md). Generalizes the former one-off\n // `frame:entity-type-added` exclusion.\n ...PERSISTED_EVENT_TYPES.filter((t) => !(BRIDGED_CHANNELS as readonly string[]).includes(t)),\n ...RESOURCE_BROADCAST_TYPES,\n];\n\nexport type TokenRefresher = () => Promise<string | null>;\n\nexport interface HttpTransportConfig {\n baseUrl: BaseUrl;\n /** Observable token source; headers read the current value. */\n token$?: BehaviorSubject<AccessToken | null>;\n timeout?: number;\n retry?: number;\n logger?: Logger;\n /** Optional 401-recovery hook. See {@link TokenRefresher}. */\n tokenRefresher?: TokenRefresher;\n /**\n * B17 — persistence thunks for the last seen persisted SSE id PER\n * SCOPE, passed through to the actor state unit. See\n * {@link ActorStateUnitOptions}.\n */\n loadLastEventIds?: () => Record<string, string> | null;\n saveLastEventId?: (scope: string, id: string) => void;\n /**\n * The global SSE channel set this transport subscribes. Absent means the\n * full `BRIDGED_CHANNELS` — a full client must receive every operation's\n * reply channel, or its `busRequest`s time out. A narrow-profile process\n * (the worker) passes exactly the reply channels for the operations it\n * awaits: reply channels are global fan-out on the gateway, so a full\n * subscription receives every OTHER client's replies too — measured at\n * ~85 multi-MB `browse:annotations-result` frames/min during the\n * 2026-09-03 worker OOM, all parsed and dropped by cid filtering.\n * A `busRequest` on an operation whose replies are outside this set\n * fails fast with `bus.unsubscribed` (see `BusRequestPrimitive`).\n */\n channels?: readonly string[];\n}\n\nexport class HttpTransport implements ITransport, IGatewayOperations {\n readonly baseUrl: BaseUrl;\n private readonly http: KyInstance;\n private readonly token$: BehaviorSubject<AccessToken | null>;\n private readonly logger?: Logger;\n private readonly errorsSubject: Subject<SemiontError> = new Subject<SemiontError>();\n /**\n * Stream of `APIError` instances surfaced from any HTTP request just\n * before the transport throws to the caller. Satisfies the `ITransport`\n * `errors$` contract — see `@semiont/core/transport.ts`.\n */\n readonly errors$: Observable<SemiontError> = this.errorsSubject.asObservable();\n\n private _actor: ActorStateUnit | null = null;\n private _actorStarted = false;\n private disposed = false;\n\n /**\n * Per-resource subscription ref-counts (MULTI-RESOURCE-SCOPE). Distinct\n * resources COMPOSE — each key's first subscribe adds its scoped channels\n * to the actor's matrix, its last release removes them; keys are fully\n * independent. Local fan-out for scoped channels is a SINGLETON wired in\n * the actor getter (one delivery per event regardless of how many scopes\n * are held), so entries here are counts only.\n */\n private readonly scopeRefCounts = new Map<string, number>();\n\n /** Buses we've been asked to bridge wire events into. */\n private readonly bridges: EventBus[] = [];\n\n private readonly config: HttpTransportConfig;\n\n constructor(config: HttpTransportConfig) {\n const { baseUrl, timeout = 30000, retry = 2, logger, tokenRefresher } = config;\n this.config = config;\n\n this.baseUrl = (baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl) as BaseUrl;\n this.token$ = config.token$ ?? new BehaviorSubject<AccessToken | null>(null);\n this.logger = logger;\n\n // Retry policy: when a refresher is configured, expand retry to also\n // cover 401 (one attempt). Otherwise use the plain `retry` number.\n const retryConfig = tokenRefresher\n ? {\n limit: 1,\n methods: ['get', 'post', 'put', 'patch', 'delete', 'head', 'options'],\n statusCodes: [401, 408, 413, 429, 500, 502, 503, 504],\n }\n : retry;\n\n this.http = ky.create({\n timeout,\n retry: retryConfig,\n hooks: {\n beforeRequest: [\n ({ request }) => {\n if (this.logger) {\n this.logger.debug('HTTP Request', {\n type: 'http_request',\n url: request.url,\n method: request.method,\n timestamp: Date.now(),\n hasAuth: request.headers.has('Authorization'),\n });\n }\n },\n ],\n beforeRetry: tokenRefresher\n ? [\n async ({ request, error }) => {\n if (!(error instanceof HTTPError) || error.response.status !== 401) {\n return undefined;\n }\n try {\n const newToken = await tokenRefresher();\n if (!newToken) return ky.stop;\n request.headers.set('Authorization', `Bearer ${newToken}`);\n return undefined;\n } catch {\n return ky.stop;\n }\n },\n ]\n : [],\n afterResponse: [\n ({ request, response }) => {\n if (this.logger) {\n this.logger.debug('HTTP Response', {\n type: 'http_response',\n url: request.url,\n method: request.method,\n status: response.status,\n statusText: response.statusText,\n });\n }\n return response;\n },\n ],\n beforeError: [\n async ({ request, error }) => {\n const response = error instanceof HTTPError ? error.response : undefined;\n if (response) {\n const body = await response.json().catch(() => ({})) as { message?: string };\n if (this.logger) {\n this.logger.error('HTTP Request Failed', {\n type: 'http_error',\n url: request.url,\n method: request.method,\n status: response.status,\n statusText: response.statusText,\n error: body.message || `HTTP ${response.status}: ${response.statusText}`,\n });\n }\n const apiError = new APIError(\n body.message || `HTTP ${response.status}: ${response.statusText}`,\n response.status,\n response.statusText,\n body,\n );\n this.errorsSubject.next(apiError);\n throw apiError;\n }\n return error;\n },\n ],\n },\n });\n\n // Auto-start the bus actor once a token arrives.\n this.token$.subscribe((token) => {\n if (token && !this._actorStarted && !this.disposed) {\n this._actorStarted = true;\n this.actor.start();\n }\n });\n }\n\n // ── Lazy actor construction + per-channel fan-in to bridges ───────────\n //\n // `actor` is exposed so the legacy `SemiontClient` can keep `.actor`\n // pointing at the same ActorStateUnit during the transport-abstraction\n // migration. Once SemiontClient is removed, this should be made\n // private again — external callers should use emit/on/stream/state$.\n\n get actor(): ActorStateUnit {\n if (!this._actor) {\n const globalChannels = this.config.channels ?? BRIDGED_CHANNELS;\n this._actor = createActorStateUnit({\n baseUrl: this.baseUrl,\n token: () => this.token$.getValue() ?? '',\n channels: [...globalChannels],\n ...(this.config.loadLastEventIds ? { loadLastEventIds: this.config.loadLastEventIds } : {}),\n ...(this.config.saveLastEventId ? { saveLastEventId: this.config.saveLastEventId } : {}),\n // The SAME hook the ky beforeRetry path uses (SSE-AUTH-RESILIENCE\n // P4, D2) — the SSE connect path refreshes once before parking\n // `unauthenticated`, and no second refresh mechanism exists.\n ...(this.config.tokenRefresher ? { tokenRefresher: this.config.tokenRefresher } : {}),\n });\n // Refused connects surface on the transport's contract stream too —\n // an SSE subscribe IS an HTTP request, and `SseConnectError` is a\n // `SemiontError` (SSE-AUTH-RESILIENCE P4, closing P2's deferred\n // bridge question).\n this._actor.errors$.subscribe((e) => this.errorsSubject.next(e));\n // One fan-in per channel, wired once for the actor's lifetime — the\n // globally-subscribed set AND the resource-scoped set (disjoint by the\n // bus-invariants guard). Scoped events only arrive for scopes in the\n // actor's matrix (gateway-authoritative filtering), so an always-on\n // scoped fan-in delivers nothing while no scope is held — and exactly\n // ONCE per event however many scopes are held (the per-scope\n // bridge-subs design would have duplicated delivery N×).\n for (const channel of [...globalChannels, ...RESOURCE_SCOPED_CHANNELS]) {\n this._actor.on$<Record<string, unknown>>(channel).subscribe((payload) => {\n for (const bus of this.bridges) {\n (bus.get(channel as keyof EventMap) as { next(v: unknown): void }).next(payload);\n }\n });\n }\n }\n return this._actor;\n }\n\n // ── ITransport — bus primitives ───────────────────────────────────────\n\n async emit<K extends keyof EventMap>(\n channel: K,\n payload: EventMap[K],\n resourceScope?: ResourceId,\n ): Promise<number> {\n busLog('EMIT', channel as string, payload, resourceScope as string | undefined);\n recordBusEmit(channel as string, resourceScope as string | undefined);\n return withSpan(\n `bus.emit:${channel as string}`,\n async () => {\n if (resourceScope !== undefined) {\n return this.actor.emit(\n channel as string,\n payload as unknown as Record<string, unknown>,\n resourceScope as string,\n );\n }\n return this.actor.emit(\n channel as string,\n payload as unknown as Record<string, unknown>,\n );\n },\n {\n kind: SpanKind.PRODUCER,\n attrs: {\n 'bus.channel': channel as string,\n ...(resourceScope ? { 'bus.scope': resourceScope as string } : {}),\n },\n },\n );\n }\n\n on<K extends keyof EventMap>(\n channel: K,\n handler: (payload: EventMap[K]) => void,\n ): () => void {\n const sub = this.actor.on$<EventMap[K]>(channel as string).subscribe(handler);\n return () => sub.unsubscribe();\n }\n\n stream<K extends keyof EventMap>(channel: K): Observable<EventMap[K]> {\n return this.actor.on$<EventMap[K]>(channel as string);\n }\n\n /**\n * Wire this transport's SSE fan-in into the given bus. Every channel\n * in `BRIDGED_CHANNELS` (and subsequently per-resource scoped channels\n * opened by `subscribeToResource`) is published on the bus. Safe to\n * call multiple times — each bus is added to the fan-out list.\n */\n bridgeInto(bus: EventBus): void {\n this.bridges.push(bus);\n }\n\n subscribeToResource(resourceId: ResourceId): () => void {\n const key = resourceId as string;\n const count = this.scopeRefCounts.get(key) ?? 0;\n this.scopeRefCounts.set(key, count + 1);\n if (count === 0) {\n this.actor.addChannels([...RESOURCE_SCOPED_CHANNELS], key);\n }\n\n let called = false;\n return () => {\n if (called) return;\n called = true;\n const remaining = (this.scopeRefCounts.get(key) ?? 0) - 1;\n if (remaining > 0) {\n this.scopeRefCounts.set(key, remaining);\n return;\n }\n this.scopeRefCounts.delete(key);\n this.actor.removeChannels([...RESOURCE_SCOPED_CHANNELS], key);\n };\n }\n\n get state$(): Observable<ConnectionState> {\n return this.actor.state$;\n }\n\n /**\n * Correlated-reply retention, client side (BUS-RESUMPTION Phase 2 /\n * SDK-DEBT S1): `busRequest` registers its cid here before emitting;\n * the actor carries the tracked set as `pendingReplies` on every\n * subscribe body, so a reply published while the connection was down\n * replays from the server's retention buffer on reconnect.\n */\n trackReply(correlationId: string): () => void {\n return this.actor.trackReply(correlationId);\n }\n\n /**\n * `busRequest`'s fail-fast probe (`BusRequestPrimitive.isSubscribed`):\n * whether the actor's global subscription set delivers `channel`. On a\n * full client (no `channels` config) every bridged reply channel is\n * subscribed and this never gates; on a narrowed transport it turns a\n * doomed request into an immediate `bus.unsubscribed` error.\n */\n isSubscribed(channel: string): boolean {\n return this.actor.isSubscribed(channel);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n this.scopeRefCounts.clear();\n if (this._actor) {\n this._actor.dispose();\n this._actor = null;\n }\n this.errorsSubject.complete();\n }\n\n /**\n * Route a transport-level error onto `errors$`. Used by sibling adapters\n * (e.g. `HttpContentTransport`'s XHR upload path) that don't go through\n * the `ky` `beforeError` hook and need to surface failures on the same\n * stream the rest of the transport publishes to.\n */\n pushError(error: SemiontError): void {\n if (this.disposed) return;\n this.errorsSubject.next(error);\n }\n\n // ── Auth ──────────────────────────────────────────────────────────────\n\n private authHeaders(): Record<string, string> {\n const token = this.token$.getValue() ?? undefined;\n return token ? { Authorization: `Bearer ${token}` } : {};\n }\n\n async authenticatePassword(email: Email, password: string): Promise<AuthResponse> {\n return this.http.post(`${this.baseUrl}/api/tokens/password`, {\n json: { email, password },\n headers: this.authHeaders(),\n }).json();\n }\n\n async authenticateGoogle(credential: GoogleCredential): Promise<AuthResponse> {\n return this.http.post(`${this.baseUrl}/api/tokens/google`, {\n json: { credential },\n headers: this.authHeaders(),\n }).json();\n }\n\n async refreshAccessToken(token: RefreshToken): Promise<TokenRefreshResponse> {\n return this.http.post(`${this.baseUrl}/api/tokens/refresh`, {\n json: { refreshToken: token },\n headers: this.authHeaders(),\n }).json();\n }\n\n async logout(): Promise<void> {\n await this.http.post(`${this.baseUrl}/api/users/logout`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async acceptTerms(): Promise<void> {\n await this.http.post(`${this.baseUrl}/api/users/accept-terms`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getCurrentUser(): Promise<UserResponse> {\n return this.http.get(`${this.baseUrl}/api/users/me`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getMediaToken(resourceId: ResourceId): Promise<{ token: string }> {\n return this.http.post(`${this.baseUrl}/api/tokens/media`, {\n json: { resourceId },\n headers: this.authHeaders(),\n }).json();\n }\n\n // ── Admin ─────────────────────────────────────────────────────────────\n\n async listUsers(): Promise<ListUsersResponse> {\n return this.http.get(`${this.baseUrl}/api/admin/users`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getUserStats(): Promise<AdminUserStatsResponse> {\n return this.http.get(`${this.baseUrl}/api/admin/users/stats`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async updateUser(id: UserDID, data: UpdateUserRequest): Promise<UpdateUserResponse> {\n return this.http.patch(`${this.baseUrl}/api/admin/users/${id}`, {\n json: data,\n headers: this.authHeaders(),\n }).json();\n }\n\n async getOAuthConfig(): Promise<OAuthConfigResponse> {\n return this.http.get(`${this.baseUrl}/api/admin/oauth/config`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n // ── System status ─────────────────────────────────────────────────────\n\n async healthCheck(): Promise<HealthCheckResponse> {\n return this.http.get(`${this.baseUrl}/api/health`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getStatus(): Promise<StatusResponse> {\n return this.http.get(`${this.baseUrl}/api/status`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n // ── Internal: ky accessor for legacy passthroughs (temporary) ─────────\n\n /**\n * Temporary escape hatch for the ongoing transport migration: namespaces\n * that still need to issue ad-hoc HTTP calls (e.g. legacy browse/mark\n * HTTP fallbacks) can borrow the configured `ky` instance here. Will be\n * deleted once all namespaces route through bus channels or through\n * typed methods on this transport.\n */\n get rawHttp(): KyInstance {\n return this.http;\n }\n\n /**\n * Current access token (synchronously read from the BehaviorSubject).\n * Used by content-transport and legacy namespace HTTP fallbacks that\n * need to pass `auth: token` through some code paths.\n */\n getToken(): AccessToken | undefined {\n return this.token$.getValue() ?? undefined;\n }\n}\n\n// Re-export for convenience\nexport type { ConnectionState } from '@semiont/core';\n","/**\n * HttpContentTransport — binary I/O over HTTP.\n *\n * Phase 1 of TRANSPORT-ABSTRACTION. Narrow by design because binary has\n * different backpressure and streaming characteristics than typed command\n * payloads. Uses the HttpTransport's underlying ky instance + token, so\n * retries, logging, and auth behave identically to the rest of the wire.\n *\n * Two `putBinary` paths live side by side, selected by runtime\n * environment + caller intent:\n * - **ky path (default + Node)** — the original `ky.post(...)` path.\n * Keeps retry-with-refresh, beforeError → APIError, observability\n * spans intact. Hits when no `onProgress`/`signal` is passed, OR\n * when `XMLHttpRequest` isn't available in the runtime (Node\n * workers, the CLI). On Node-side `signal`-aborts: the in-flight\n * `fetch` continues in the background and the `cancelled` flag in\n * `yield.resource` suppresses the resolve/reject callbacks.\n * - **XHR path (browsers with `onProgress` or `signal`)** — hand-rolled\n * because `ky` wraps `fetch` which can't observe upload byte-\n * progress today (`Request({ duplex: 'half' })` is the long-term\n * direction; not yet widely available across the webviews this\n * codepath needs to run in). Threads auth + traceparent headers,\n * emits `onProgress` from `xhr.upload.onprogress`, supports\n * cancellation via the `signal` option (calling `xhr.abort()`),\n * and routes failures onto the same `transport.errors$` stream\n * the ky path uses.\n *\n * The runtime check on `XMLHttpRequest` is the load-bearing seam: a\n * Node worker calling `client.yield.resource(...)` (which always passes\n * a `signal` for unsubscribe-aborts) must NOT take the XHR path —\n * `XMLHttpRequest` is undefined and the upload throws synchronously.\n * Browsers always have it; Node does not.\n *\n * v1 limitation: the XHR path does NOT auto-refresh on 401. Mitigation:\n * the session's proactive refresh fires before token expiry, so an\n * upload that *starts* with a fresh token usually completes. An upload\n * spanning the narrow window between expiry and proactive-refresh would\n * fail; the existing `errors$` → modal path surfaces it as session-\n * expired. If retry-with-refresh on the upload path becomes a real\n * complaint, wire a manual retry loop here that reads `token$` afresh.\n */\n\nimport type { AccessToken, ResourceId, PutBinaryOptions, components } from '@semiont/core';\nimport { busLog } from '@semiont/core';\nimport { SpanKind, getActiveTraceparent, withSpan } from '@semiont/observability';\nimport type { HttpTransport } from './http-transport';\nimport { APIError } from './api-error';\nimport type { IContentTransport, PutBinaryRequest } from '@semiont/core';\n\ntype GetResourceResponse = components['schemas']['GetResourceResponse'];\n\nexport class HttpContentTransport implements IContentTransport {\n constructor(private readonly transport: HttpTransport) {}\n\n async putBinary(\n request: PutBinaryRequest,\n options?: PutBinaryOptions,\n ): Promise<{ resourceId: ResourceId }> {\n const sizeBytes = request.file instanceof File ? request.file.size : request.file.length;\n busLog('PUT', 'content', {\n name: request.name,\n format: request.format,\n storageUri: request.storageUri,\n sizeBytes,\n });\n return withSpan(\n 'content.put',\n async () => {\n const formData = buildFormData(request);\n const headers = this.requestHeaders(options?.auth);\n\n // Branch on caller intent AND runtime support. The ky path is\n // the well-trodden default; the XHR path lights up only when a\n // caller wants byte progress or cancellation AND the runtime\n // has `XMLHttpRequest` (browsers do; Node does not). Without\n // the runtime guard, every Node-side `yield.resource(...)`\n // call (which always passes `signal`) would throw\n // `XMLHttpRequest is not defined`.\n const xhrAvailable = typeof XMLHttpRequest !== 'undefined';\n if (xhrAvailable && (options?.onProgress || options?.signal)) {\n return uploadViaXhr({\n url: `${this.transport.baseUrl}/resources`,\n formData,\n headers,\n onProgress: options.onProgress,\n signal: options.signal,\n onApiError: (err) => this.transport.pushError(err),\n });\n }\n\n const result = await this.transport.rawHttp\n .post(`${this.transport.baseUrl}/resources`, {\n body: formData,\n headers,\n })\n .json<{ resourceId: string }>();\n\n return { resourceId: result.resourceId as ResourceId };\n },\n {\n kind: SpanKind.CLIENT,\n attrs: {\n 'content.format': request.format,\n 'content.size_bytes': sizeBytes,\n },\n },\n );\n }\n\n async getBinary(\n resourceId: ResourceId,\n options?: { auth?: AccessToken },\n ): Promise<{ data: ArrayBuffer; contentType: string }> {\n busLog('GET', 'content', { resourceId });\n return withSpan(\n 'content.get',\n async () => {\n // Pure pipe: no Accept header — the route serves the stored bytes\n // verbatim with their real Content-Type (SIMPLER-JSON-LD.md).\n const response = await this.transport.rawHttp.get(`${this.transport.baseUrl}/resources/${resourceId}`, {\n headers: this.requestHeaders(options?.auth),\n });\n const contentType = response.headers.get('content-type') || 'application/octet-stream';\n const data = await response.arrayBuffer();\n return { data, contentType };\n },\n { kind: SpanKind.CLIENT, attrs: { 'resource.id': resourceId as unknown as string } },\n );\n }\n\n async getBinaryStream(\n resourceId: ResourceId,\n options?: { auth?: AccessToken },\n ): Promise<{ stream: ReadableStream<Uint8Array>; contentType: string }> {\n busLog('GET', 'content', { resourceId, stream: true });\n return withSpan(\n 'content.get',\n async () => {\n // Pure pipe: no Accept header (see getBinary).\n const response = await this.transport.rawHttp.get(`${this.transport.baseUrl}/resources/${resourceId}`, {\n headers: this.requestHeaders(options?.auth),\n });\n const contentType = response.headers.get('content-type') || 'application/octet-stream';\n if (!response.body) {\n throw new Error('Response body is null - cannot create stream');\n }\n return { stream: response.body, contentType };\n },\n {\n kind: SpanKind.CLIENT,\n attrs: { 'resource.id': resourceId as unknown as string, 'content.stream': true },\n },\n );\n }\n\n /**\n * Dereference the resource's JSON-LD graph over HTTP — the LD face an\n * external linked-data client sees. Deliberately HTTP, not the bus\n * (SIMPLER-JSON-LD.md §5).\n */\n async getResourceGraph(\n resourceId: ResourceId,\n options?: { auth?: AccessToken },\n ): Promise<GetResourceResponse> {\n busLog('GET', 'content', { resourceId, graph: true });\n return withSpan(\n 'content.get_graph',\n () =>\n this.transport.rawHttp\n .get(`${this.transport.baseUrl}/resources/${resourceId}/jsonld`, {\n headers: this.requestHeaders(options?.auth),\n })\n .json<GetResourceResponse>(),\n { kind: SpanKind.CLIENT, attrs: { 'resource.id': resourceId as unknown as string, 'content.graph': true } },\n );\n }\n\n\n\n\n\n dispose(): void {\n // HttpContentTransport has no resources of its own; HttpTransport owns\n // the ky instance and token subject. No-op is correct here.\n }\n\n /** Auth header + W3C trace propagation for the active span. */\n private requestHeaders(override?: AccessToken): Record<string, string> {\n const token = override ?? this.transport.getToken();\n const headers: Record<string, string> = token ? { Authorization: `Bearer ${token}` } : {};\n const trace = getActiveTraceparent();\n if (trace) {\n headers['traceparent'] = trace.traceparent;\n if (trace.tracestate) headers['tracestate'] = trace.tracestate;\n }\n return headers;\n }\n}\n\nfunction buildFormData(request: PutBinaryRequest): FormData {\n const formData = new FormData();\n formData.append('name', request.name);\n formData.append('format', request.format);\n formData.append('storageUri', request.storageUri);\n\n if (request.file instanceof File) {\n formData.append('file', request.file);\n } else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(request.file)) {\n // `Buffer` is a Node global; referencing it bare in the browser throws\n // ReferenceError before the isBuffer call. Browser uploads always hit\n // the File branch above; this branch is for Node-side workers.\n const blob = new Blob([new Uint8Array(request.file)], { type: request.format });\n formData.append('file', blob, request.name);\n } else {\n throw new Error('file must be a File or Buffer');\n }\n\n if (request.entityTypes && request.entityTypes.length > 0) {\n formData.append('entityTypes', JSON.stringify(request.entityTypes));\n }\n if (request.language) formData.append('language', request.language);\n if (request.sourceAnnotationId) formData.append('sourceAnnotationId', String(request.sourceAnnotationId));\n if (request.sourceResourceId) formData.append('sourceResourceId', String(request.sourceResourceId));\n if (request.generationPrompt) formData.append('generationPrompt', request.generationPrompt);\n if (request.generator) formData.append('generator', JSON.stringify(request.generator));\n if (request.cloneToken) formData.append('cloneToken', request.cloneToken);\n if (request.archiveOriginal !== undefined) formData.append('archiveOriginal', String(request.archiveOriginal));\n if (request.isDraft !== undefined) formData.append('isDraft', String(request.isDraft));\n\n return formData;\n}\n\ninterface XhrUploadOptions {\n url: string;\n formData: FormData;\n headers: Record<string, string>;\n onProgress?: (event: { bytesUploaded: number; totalBytes: number }) => void;\n signal?: AbortSignal;\n onApiError: (error: APIError) => void;\n}\n\n/**\n * XHR-based POST that exposes `xhr.upload.onprogress` byte counts and\n * supports cancellation via `AbortSignal`. Mirrors the ky path's error\n * shape: 4xx/5xx and network-level failures both surface as `APIError`,\n * and every error is routed onto `transport.errors$` before the promise\n * rejects.\n */\nfunction uploadViaXhr(opts: XhrUploadOptions): Promise<{ resourceId: ResourceId }> {\n const { url, formData, headers, onProgress, signal, onApiError } = opts;\n\n return new Promise((resolve, reject) => {\n const xhr = new XMLHttpRequest();\n\n if (signal?.aborted) {\n const err = new APIError('Upload aborted', 0, 'aborted');\n onApiError(err);\n reject(err);\n return;\n }\n\n xhr.open('POST', url);\n for (const [name, value] of Object.entries(headers)) {\n xhr.setRequestHeader(name, value);\n }\n\n if (onProgress) {\n xhr.upload.onprogress = (e: ProgressEvent) => {\n // `lengthComputable` is true when Content-Length is known. For\n // FormData posts the browser computes it, so this is true in\n // practice; the false branch handles the rare chunked-encoding\n // / gzip-while-uploading case.\n const totalBytes = e.lengthComputable ? e.total : 0;\n onProgress({ bytesUploaded: e.loaded, totalBytes });\n };\n }\n\n xhr.onload = () => {\n if (xhr.status >= 200 && xhr.status < 300) {\n try {\n const body = JSON.parse(xhr.responseText) as { resourceId: string };\n resolve({ resourceId: body.resourceId as ResourceId });\n } catch (parseErr) {\n const err = new APIError(\n `Upload succeeded but response was not valid JSON: ${(parseErr as Error).message}`,\n xhr.status,\n xhr.statusText,\n xhr.responseText,\n );\n onApiError(err);\n reject(err);\n }\n return;\n }\n let body: unknown = xhr.responseText;\n try { body = JSON.parse(xhr.responseText); } catch { /* keep as text */ }\n const message = (body && typeof body === 'object' && 'message' in body && typeof (body as { message: unknown }).message === 'string')\n ? (body as { message: string }).message\n : `HTTP ${xhr.status}: ${xhr.statusText}`;\n const err = new APIError(message, xhr.status, xhr.statusText, body);\n onApiError(err);\n reject(err);\n };\n\n xhr.onerror = () => {\n // Network-level failure (DNS, TCP reset, CORS). XHR doesn't give\n // us a useful status here; classify as `unavailable` via 0 status\n // mapping in classifyApiCode.\n const err = new APIError('Network error during upload', 0, 'network-error');\n onApiError(err);\n reject(err);\n };\n\n xhr.ontimeout = () => {\n const err = new APIError('Upload timed out', 0, 'timeout');\n onApiError(err);\n reject(err);\n };\n\n xhr.onabort = () => {\n // Caller-initiated abort via `signal`. Emit a single APIError so the\n // shape matches the other failure paths; consumers can disambiguate\n // via `signal.aborted` if they need to.\n const err = new APIError('Upload aborted', 0, 'aborted');\n onApiError(err);\n reject(err);\n };\n\n if (signal) {\n const onAbort = () => xhr.abort();\n signal.addEventListener('abort', onAbort, { once: true });\n // No teardown for the listener — once xhr fires onabort/onerror/onload\n // the signal is no longer relevant; the listener is GC'd with the xhr.\n }\n\n xhr.send(formData);\n });\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/transport/sse-connect-error.ts","../src/transport/api-error.ts","../src/transport/actor-state-unit.ts","../src/transport/http-transport.ts","../src/transport/http-content-transport.ts"],"names":["SemiontError","Subject","BehaviorSubject","busLog","withSpan","SpanKind","getActiveTraceparent","body","err"],"mappings":";;;;;;;AAqBO,IAAM,eAAA,GAAN,cAA8B,YAAA,CAAa;AAAA,EACvC,MAAA;AAAA,EACT,YAAY,MAAA,EAAgB;AAC1B,IAAA,KAAA,CAAM,uBAAuB,MAAM,CAAA,CAAA,EAAI,oBAAA,EAAsB,EAAE,QAAQ,CAAA;AACvE,IAAA,IAAA,CAAK,IAAA,GAAO,iBAAA;AACZ,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,EAChB;AACF;ACdA,SAAS,gBAAgB,MAAA,EAAoC;AAC3D,EAAA,IAAI,MAAA,KAAW,KAAK,OAAO,aAAA;AAC3B,EAAA,IAAI,MAAA,KAAW,KAAK,OAAO,cAAA;AAC3B,EAAA,IAAI,MAAA,KAAW,KAAK,OAAO,WAAA;AAC3B,EAAA,IAAI,MAAA,KAAW,KAAK,OAAO,WAAA;AAC3B,EAAA,IAAI,MAAA,KAAW,KAAK,OAAO,UAAA;AAC3B,EAAA,IAAI,MAAA,IAAU,KAAK,OAAO,aAAA;AAC1B,EAAA,OAAO,OAAA;AACT;AAEO,IAAM,QAAA,GAAN,cAAuBA,YAAAA,CAAa;AAAA,EAEhC,MAAA;AAAA,EACA,UAAA;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,CAAA;AACH","file":"index.js","sourcesContent":["import { SemiontError } from '@semiont/core';\n\n/**\n * A refused SSE connect — `POST /bus/subscribe` answered non-2xx (or 2xx\n * with no body). The status is CARRIED, not interpolated into the message:\n * P3's backoff/terminal split reads it (auth-refused is terminal in kind;\n * 5xx is transient), and it surfaces on `ActorStateUnit.errors$` so a\n * refused client is observable instead of a silent retry loop\n * (SSE-AUTH-RESILIENCE P2, shape B).\n *\n * Extends `SemiontError` (core) rather than reusing `APIError`: APIError\n * lives in http-transport.ts, which imports the actor — reaching for it\n * there would buy one inherited field with an import cycle.\n *\n * Its OWN module, not `actor-state-unit.ts`, because `*-state-unit.ts` files\n * carry no class declarations: state units are plain-object factories, and\n * `audit-state-unit-no-class.sh` is the static half of the A1 axiom whose\n * allowlist is deliberately empty. An error type is not state-unit\n * machinery, so it moves rather than earning that gate its first exception.\n * A leaf module importing only core also keeps the cycle shut.\n */\nexport class SseConnectError extends SemiontError {\n readonly status: number;\n constructor(status: number) {\n super(`SSE connect failed: ${status}`, 'SSE_CONNECT_FAILED', { status });\n this.name = 'SseConnectError';\n this.status = status;\n }\n}\n","/**\n * `APIError` — the transport's HTTP failure, carrying the status it came from.\n *\n * Lives in its own module rather than in `http-transport.ts` because\n * `actor-state-unit.ts` throws it too (SIDECAR-BOOT-RESILIENCE P2) and\n * `http-transport.ts` imports `actor-state-unit.ts` — so the obvious home is a\n * cycle. The alternative, a second status-bearing error class beside this one,\n * is the duplicated shape the house rules forbid: there would then be two\n * answers to \"how does an HTTP failure carry its status\", and the retry\n * predicate could only agree with one of them.\n */\n\nimport { SemiontError, type TransportErrorCode, type HttpStatusError } from '@semiont/core';\n\nfunction classifyApiCode(status: number): TransportErrorCode {\n if (status === 400) return 'bad-request';\n if (status === 401) return 'unauthorized';\n if (status === 403) return 'forbidden';\n if (status === 404) return 'not-found';\n if (status === 409) return 'conflict';\n if (status >= 500) return 'unavailable';\n return 'error';\n}\n\nexport class APIError extends SemiontError {\n declare code: TransportErrorCode;\n readonly status: number;\n readonly statusText: string;\n\n constructor(message: string, status: number, statusText: string, body?: unknown) {\n super(message, classifyApiCode(status), { status, statusText, body });\n this.name = 'APIError';\n this.status = status;\n this.statusText = statusText;\n }\n}\n\n/**\n * The contract between this class and core's `isRetryableRequestError`, asserted\n * at compile time (SIDECAR-BOOT-RESILIENCE P1/P2).\n *\n * Core cannot name `APIError` — http-transport depends on core, not the reverse —\n * so the predicate reads the `status` field structurally. Without this line that\n * agreement is a coincidence: rename `status` here and every 429 silently stops\n * being retryable, with no test failing, because a predicate that finds no status\n * simply answers `false`. **A silent loss of retry is exactly the failure this\n * plan exists to prevent**, so it is pinned by the compiler rather than by a test.\n */\nconst _conformsToRetryContract: HttpStatusError = new APIError('', 0, '');\nvoid _conformsToRetryContract;\n","import { BehaviorSubject, Observable, Subject } from 'rxjs';\nimport { filter, map, share } from 'rxjs/operators';\nimport { busLog, busLogEnabled, uuidV4, retryWithBackoff, equalJitter, isRetryableRequestError, type components, type ConnectionState, type StateUnit, type RetryPolicy } from '@semiont/core';\nimport {\n SpanKind,\n extractTraceparent,\n getActiveTraceparent,\n withSpan,\n withTraceparent,\n} from '@semiont/observability';\nimport { SseConnectError } from './sse-connect-error';\nimport { APIError } from './api-error';\n\nexport type { ConnectionState };\n\nexport interface BusEvent {\n channel: string;\n payload: Record<string, unknown>;\n scope?: string;\n}\n\n\nexport interface ActorStateUnitOptions {\n baseUrl: string;\n token: string | (() => string);\n channels: string[];\n /**\n * Base of the failure-retry backoff ladder AND the flat cadence of the\n * `unauthenticated` waiting tick (which polls the token getter, no\n * network). A failure retry waits jitter(min(reconnectMs·2ⁿ, 60 s));\n * n resets on a successful open. Default 5 s.\n */\n reconnectMs?: number;\n /**\n * The SAME hook `HttpTransportConfig.tokenRefresher` wires into the HTTP\n * beforeRetry path (SSE-AUTH-RESILIENCE P4, D2 — no second refresh\n * mechanism). Consulted ONCE per outage when a connect is refused 401,\n * before parking `unauthenticated`; a successful open re-arms it. The\n * refresher's owner rotates the token SOURCE (sessions push the new token\n * into `token$`); the actor then reconnects through the getter, so the\n * token stays single-sourced. A throwing refresher is treated as `null`.\n */\n tokenRefresher?: () => Promise<string | null>;\n /**\n * Remove-side reconnect hysteresis (MULTI-RESOURCE-SCOPE). Scope\n * additions need liveness quickly (100 ms debounce), but a removal only\n * narrows delivery — extra events for a just-released scope are\n * idempotent locally — so remove-only changes wait this long before\n * reconnecting. Keeps hover-churn (transient per-citation previews)\n * from turning every mouse pass into a reconnect storm; any addition\n * flushes pending removals with it on the fast path.\n */\n lazyRemoveMs?: number;\n /**\n * B17 (LOCAL-STORAGE) — IO-abstracted persistence of the last seen\n * PERSISTED event id PER SCOPE, so a reloaded client resumes each\n * scope's replay instead of gapping. `load` runs once at construction;\n * `save` fires per persisted (`p-*`) id with that frame's scope —\n * ephemeral (`e-*`) ids are never saved: they carry no replay meaning,\n * and letting them displace a scope's watermark was exactly the silent\n * replay-loss hole the single-id design had. The transport stays\n * storage-free; callers wrap their own adapter in these thunks.\n */\n loadLastEventIds?: () => Record<string, string> | null;\n saveLastEventId?: (scope: string, id: string) => void;\n}\n\n/** Time in the `reconnecting` state before transitioning to `degraded`. */\nexport const DEGRADED_THRESHOLD_MS = 3_000;\n\n/**\n * Ceiling for the failure-retry backoff (SSE-AUTH-RESILIENCE P3, D1a): the\n * exponential ladder from `reconnectMs` caps here, so a long outage settles\n * into roughly one attempt per minute rather than growing without bound.\n */\nconst MAX_RECONNECT_MS = 60_000;\n\n/**\n * Deadline on the `/bus/emit` POST (JOB-RESTART-SAFETY P7). The gateway\n * accepts an emit and returns 202 promptly; a POST that has not resolved by\n * here means the gateway is unresponsive (mid-restart, overwhelmed), and the\n * emit is rejected rather than awaited forever. This is the transport-level\n * bound behind the 2026-09-03 finalization hang: a worker's mark:create /\n * job:complete emit to a wedged gateway used to hang the worker loop with no\n * timeout of its own. The rejection surfaces as a job failure the queue\n * classifies transient (an unreachable gateway is not the request's fault),\n * so the work retries instead of wedging. Covers EVERY emit and every job\n * type — not just the reference-annotation persist P6 bounded.\n */\nexport const EMIT_TIMEOUT_MS = 30_000;\n\n/**\n * Retry budget for ONE `/bus/emit` POST (SIDECAR-BOOT-RESILIENCE D3).\n *\n * Per request, deliberately — not per boot pass. \"Retry when one settles\" is\n * advice about a single request; re-running a whole catch-up pass to recover\n * from one refusal re-sends hundreds of already-successful emits, which is the\n * amplification that wedged the weaver in the first place.\n *\n * Small on purpose. `EMIT_TIMEOUT_MS` bounds each attempt, so the worst case\n * here is 4 attempts plus up to ~7s of jittered waiting, and an emit that a\n * caller is awaiting must fail while the caller still cares. The patience for a\n * gateway that is genuinely down belongs to the boot pass above it, not here.\n */\nexport const EMIT_RETRY: RetryPolicy = {\n attempts: 4,\n initialDelayMs: 1_000,\n maxDelayMs: 4_000,\n};\n\n/**\n * How long a superseded connection keeps DRAINING after a make-before-break\n * handoff before being aborted. Aborting the old connection the instant the\n * new one opened discarded replies already written to the old socket but not\n * yet read by the client (a freshly-hydrating page's main thread is busy —\n * exactly the N-concurrent-loaders-at-connect repro in\n * .plans/bugs/concurrent-browse-resource-starvation.md). The overlap is safe:\n * persisted ids are stable and correlated-reply ids are deterministic\n * (`e-<channel>:<cid>`, routes/bus.ts), so `seenEventIds` dedups double\n * delivery.\n */\nexport const LINGER_MS = 1_000;\n\nexport interface ActorStateUnit extends StateUnit {\n on$<T = Record<string, unknown>>(channel: string): Observable<T>;\n emit(channel: string, payload: Record<string, unknown>, emitScope?: string): Promise<number>;\n state$: Observable<ConnectionState>;\n /**\n * Refused connects (SSE-AUTH-RESILIENCE P2). One `SseConnectError` per\n * non-2xx `/bus/subscribe` answer, carrying the HTTP status as\n * structured data. Network-level failures (fetch rejections) have no\n * status and do not emit here — they stay on the reconnect path.\n */\n errors$: Observable<SseConnectError>;\n /** With `scope`: upsert channels into that scope's matrix entry. Without: global channels. */\n addChannels(channels: string[], scope?: string): void;\n /**\n * Whether `channel` is in the current GLOBAL subscription set — i.e. the\n * gateway delivers it on this connection. Correlated replies always ride\n * global channels, so this is `busRequest`'s fail-fast probe on a\n * narrowed-subscription transport (see `BusRequestPrimitive.isSubscribed`).\n */\n isSubscribed(channel: string): boolean;\n /** With `scope`: remove channels from that scope's entry (empty entry drops the scope). Without: global channels. */\n removeChannels(channels: string[], scope?: string): void;\n /**\n * Correlated-reply retention, client side (BUS-RESUMPTION Phase 2 /\n * SDK-DEBT S1): register a busRequest correlationId as awaiting its\n * reply. Every connect body includes the currently-tracked set as\n * `pendingReplies`, so a reply published while the connection was down\n * is replayed from the server's retention buffer. The returned disposer\n * (idempotent) removes the id on settle.\n */\n trackReply(correlationId: string): () => void;\n start(): void;\n stop(): void;\n}\n\n/** Allowed transitions in the connection state machine. */\nconst ALLOWED_TRANSITIONS: Record<ConnectionState, ReadonlyArray<ConnectionState>> = {\n initial: ['connecting', 'unauthenticated', 'closed'],\n connecting: ['open', 'reconnecting', 'unauthenticated', 'closed'],\n open: ['reconnecting', 'closed'],\n reconnecting: ['connecting', 'degraded', 'unauthenticated', 'closed'],\n // `degraded → reconnecting` is a legitimate recovery edge: a channel-set\n // change (`addChannels`/`removeChannels`) schedules a reconnect that can\n // fire while the connection is degraded. Omitting it made `reconnect()`\n // throw a fatal, uncaught exception from the reconnect timer (#844).\n degraded: ['connecting', 'reconnecting', 'unauthenticated', 'closed'],\n // Leaving `unauthenticated` takes a usable credential (the gate saw a\n // different, non-empty token) → straight to `connecting`; or teardown.\n unauthenticated: ['connecting', 'closed'],\n closed: [],\n};\n\nexport function createActorStateUnit(options: ActorStateUnitOptions): ActorStateUnit {\n const { baseUrl, token: tokenOrGetter, channels: initialChannels, reconnectMs = 5_000, lazyRemoveMs = 5_000, tokenRefresher } = options;\n const getToken = typeof tokenOrGetter === 'function' ? tokenOrGetter : () => tokenOrGetter;\n\n const globalChannels = new Set(initialChannels);\n /** The subscription matrix's scoped half: scope → channels (MULTI-RESOURCE-SCOPE). */\n const scopedSubscriptions = new Map<string, Set<string>>();\n /**\n * Per-scope resumption watermarks: the last PERSISTED (`p-*`) id seen for\n * each scope. Sent as `lastEventId` on that scope's matrix entry so the\n * server replays each scope's own gap. A scope keeps its watermark after\n * its channels are removed — re-subscribing later replays what was missed\n * in between. Ephemeral ids never touch this map.\n */\n const scopeWatermarks = new Map<string, string>(\n Object.entries(options.loadLastEventIds?.() ?? {}),\n );\n /** Outstanding busRequest correlationIds — ride every connect body (S1). */\n const pendingReplies = new Set<string>();\n /**\n * This bus client's routing address for correlated replies\n * (CORRELATED-REPLY-ROUTING D1). Minted once per ACTOR — deliberately not\n * per connection: a make-before-break handover runs two connections at\n * once and a reconnect replaces one, so a per-connection id would strand\n * the reply on the dying socket. Both overlap connections present this\n * same address, and the deterministic `e-<channel>:<cid>` id dedups the\n * double delivery exactly as it does today.\n *\n * Not persisted: \"stable\" means across transport reconnects, not across\n * page reloads. A reload builds a new actor, and its `pendingReplies`\n * replay is what recovers in-flight replies (S1).\n */\n const clientId = uuidV4();\n\n const events$ = new Subject<BusEvent>();\n const state$ = new BehaviorSubject<ConnectionState>('initial');\n const errors$ = new Subject<SseConnectError>();\n let currentState: ConnectionState = 'initial';\n let degradedTimer: ReturnType<typeof setTimeout> | null = null;\n\n /**\n * Move the state machine to `next`. An unexpected edge is logged and\n * ignored — NOT thrown. `transition()` runs inside timer callbacks (the\n * reconnect and degraded timers), so a throw here is an uncaught exception\n * that takes down the host process (#844). A bad edge means a bug in the\n * reconnect loop, but degrading gracefully (keep the current state, warn)\n * is strictly better than killing a long-running job. The permitted edges\n * — including the `degraded → reconnecting` recovery edge — are in\n * `ALLOWED_TRANSITIONS`.\n *\n * Side effect: manages the `degraded` timer. Enters on\n * `reconnecting`, cleared on exit.\n */\n const transition = (next: ConnectionState): void => {\n if (currentState === next) return;\n const allowed = ALLOWED_TRANSITIONS[currentState];\n if (!allowed.includes(next)) {\n console.warn(`[actor] ignoring invalid connection state transition: ${currentState} → ${next}`);\n return;\n }\n const prev = currentState;\n currentState = next;\n\n if (next === 'reconnecting' && prev !== 'reconnecting') {\n // Starting a reconnect cycle — arm the degraded-threshold timer.\n if (degradedTimer) clearTimeout(degradedTimer);\n degradedTimer = setTimeout(() => {\n if (currentState === 'reconnecting') transition('degraded');\n }, DEGRADED_THRESHOLD_MS);\n }\n if (prev === 'reconnecting' && next !== 'reconnecting') {\n // Leaving reconnecting (to connecting, degraded, or closed) —\n // the timer is either no longer relevant or has just fired.\n if (degradedTimer) { clearTimeout(degradedTimer); degradedTimer = null; }\n }\n\n state$.next(next);\n };\n\n let running = false;\n /**\n * All in-flight SSE fetch controllers. Tracked as a Set because\n * connect() may race with itself under mount-churn or rapid channel-\n * set changes — whenever a new connect() starts we abort ALL previous\n * in-flight fetches rather than only the last-tracked one. A previous\n * single-slot implementation leaked orphaned streams (diagnosed by\n * observing 3 concurrent SSE subscribes in the /bus/subscribe network\n * log, each delivering duplicate RECV frames). Using a Set guarantees\n * at most one live stream post-reconnect regardless of race order.\n */\n const inflightControllers = new Set<AbortController>();\n let reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n\n /**\n * Connections retired by a make-before-break handoff. A superseded\n * connection lingers (still draining) for LINGER_MS before its abort; its\n * read loop ending — naturally or via that abort — must NOT drive the\n * actor-wide reconnect logic, which belongs to the live connection only.\n */\n const superseded = new WeakSet<AbortController>();\n /** Pending linger-abort timers, cleared on stop/dispose. */\n const lingerTimers = new Set<ReturnType<typeof setTimeout>>();\n\n /**\n * Recently-delivered event ids, to dedup the make-before-break overlap: the\n * brief window where the old and new connection both deliver the same live\n * event during a scope-change handoff. Persisted ids (`p-<scope>-<seq>`) are\n * stable across connections, so this collapses such an overlap to a single\n * emission. Ephemeral ids (`e-<connectionId>-<counter>`) are per-connection,\n * so a cross-connection ephemeral duplicate is NOT caught here — its\n * consumers tolerate the rare double (a correlation reply is taken with\n * `take(1)`; cache invalidations and job-completion are idempotent/terminal).\n * Bounded FIFO (insertion-ordered Set) to cap memory.\n *\n * Cost note: this is *always-on* — every delivered event does a has/add here\n * — yet a duplicate is only possible during a handoff overlap; in steady\n * state there's a single connection and nothing can collide. So every\n * consumer of this transport carries a small standing structure for a path\n * that fires only on (now-rare) scope changes. It's left unconditional\n * because the per-event cost is negligible next to the JSON.parse + trace\n * span already on this path. If that ever stops being true, scope it to the\n * overlap (build on handoff start, drop once the old read loop exits) or\n * track a high-water `Map<scope, maxSeq>` instead of every id.\n */\n const seenEventIds = new Set<string>();\n const SEEN_EVENT_IDS_MAX = 512;\n const rememberEventId = (id: string): void => {\n seenEventIds.add(id);\n if (seenEventIds.size > SEEN_EVENT_IDS_MAX) {\n const oldest = seenEventIds.values().next().value;\n if (oldest !== undefined) seenEventIds.delete(oldest);\n }\n };\n /** Release a claim whose apply threw, so a redelivery is re-processed\n * rather than swallowed by its own dedup entry. */\n const forgetEventId = (id: string): void => {\n seenEventIds.delete(id);\n };\n\n const shared$ = events$.pipe(share());\n\n const disconnect = () => {\n for (const c of inflightControllers) {\n try { c.abort(); } catch { /* noop */ }\n }\n inflightControllers.clear();\n if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }\n for (const t of lingerTimers) clearTimeout(t);\n lingerTimers.clear();\n };\n\n /**\n * Drop-recovery scheduling (SSE-AUTH-RESILIENCE P3). One place arms\n * `reconnectTimer`, so the gate's waiting tick and the failure retry can\n * never disagree on cadence (the split-brain the P1 handoff warned about).\n * Failure retries BACK OFF (D1a): equal-jitter exponential — delay ∈\n * [cap/2, cap], cap = min(reconnectMs·2ⁿ, MAX_RECONNECT_MS) — so a downed\n * gateway sees a thinning trickle instead of 12 requests/min from every\n * client, jitter keeps N clients out of lockstep, and a transient failure\n * still recovers (backoff delays recovery; it never prevents it). The\n * WAITING tick stays flat at `reconnectMs`: it polls the token getter and\n * makes no requests, so there is no load to bound — and backing it off\n * would only slow recovery after a re-login. Scope-churn reconnects\n * (debounce/hysteresis timers) are a separate mechanism, untouched.\n */\n let retryAttempt = 0;\n let refusedToken: string | null = null;\n /**\n * One refresher consult per outage (SSE-AUTH-RESILIENCE P4). Burned on\n * the first 401 that consults; re-armed only by a successful open. Without\n * this, a gateway refusing every token turns refresh-then-401 into a loop\n * at connect cadence — the storm with extra steps.\n */\n let refreshBurned = false;\n const scheduleRetry = (delayMs: number, keepPrevious = false) => {\n if (!running) return;\n if (reconnectTimer) clearTimeout(reconnectTimer);\n reconnectTimer = setTimeout(() => {\n if (running) connect(keepPrevious);\n }, delayMs);\n };\n const backoffDelay = () => {\n const cap = Math.min(reconnectMs * 2 ** retryAttempt, MAX_RECONNECT_MS);\n retryAttempt++;\n // `equalJitter` is core's — this loop and `retryWithBackoff` carried\n // identical copies of the formula, agreeing by coincidence. The CEILING\n // stays this loop's own (it backs off per reconnect attempt, not per retry\n // policy); only the jitter is shared, which is the part that must not drift.\n return equalJitter(cap);\n };\n\n const connect = async (keepPrevious = false) => {\n // ── The credential gate (SSE-AUTH-RESILIENCE P1 + P3, shapes A + B) ─\n //\n // A connect with no bearer CANNOT succeed (shape A: `HttpTransport`\n // renders an exhausted session's null `token$` as `''`). A connect\n // re-sending a bearer the gateway just REFUSED cannot succeed either\n // (shape B: the same credential gets the same 401). Both are\n // guaranteed-failing requests generated on a timer — the storm in\n // .plans/bugs/stale-sse-actor-401-loops-after-token-expiry.md — so\n // neither is attempted.\n //\n // This is D3 and D6a answered together: ONE `unauthenticated` state for\n // \"no credential yet\" and \"credential refused\", because they are the\n // same operational fact — not attempting, and only a usable DIFFERENT\n // credential changes anything. The finer cause (the 401) is on\n // `errors$`.\n //\n // We WAIT rather than give up: the tick polls the GETTER — no network,\n // so there is no load to bound, and the tick stays at the flat base.\n // Recovery is automatic: a re-login or session refresh rotates the\n // getter's value and the next tick connects. That makes a 401 terminal\n // PER-CREDENTIAL, not per-actor — a hard stop here would kill the SPA's\n // bus for good on an out-of-band revocation (logout elsewhere bumping\n // `tokenVersion`) that the session's next proactive refresh heals.\n const token = getToken();\n if (!token || token === refusedToken) {\n if (running) {\n if (currentState !== 'unauthenticated') transition('unauthenticated');\n scheduleRetry(reconnectMs, keepPrevious);\n }\n return;\n }\n refusedToken = null; // a different credential — worth attempting\n\n // Transition to `connecting` from whichever reconnect-ish state\n // we're currently in (`initial`, `reconnecting`, `degraded`).\n transition('connecting');\n\n // Snapshot the connections this connect() supersedes.\n // - keepPrevious=false (initial connect / drop-recovery): there is no\n // live connection worth preserving, so abort up front — this closes\n // the orphan-stream leak described above.\n // - keepPrevious=true (scope-change reconnect): MAKE-BEFORE-BREAK. Keep\n // the previous connection(s) ALIVE until the new one is `open`, then\n // abort them (below, after the fetch resolves), so an in-flight\n // ephemeral result isn't dropped in a reconnect gap (#847). The brief\n // window where old and new both deliver is deduped by event id.\n const previous = [...inflightControllers];\n if (!keepPrevious) {\n for (const c of previous) {\n try { c.abort(); } catch { /* noop */ }\n }\n inflightControllers.clear();\n }\n\n // POST subscription matrix (MULTI-RESOURCE-SCOPE): global channels plus\n // one entry per scope, each carrying its own resumption watermark.\n // `satisfies` is the drift-lock (the MEDIA_TYPES idiom): the body is\n // hand-written while the schema owns the shape, so the compiler — not a\n // reviewer — is what notices a required field going missing. That is\n // what makes `clientId` required in BusSubscribeRequest worth anything\n // on this side of the wire (CORRELATED-REPLY-ROUTING P2).\n const body = JSON.stringify({\n global: [...globalChannels],\n scoped: [...scopedSubscriptions.entries()].map(([scope, chans]) => {\n const watermark = scopeWatermarks.get(scope);\n return {\n scope,\n channels: [...chans],\n ...(watermark !== undefined ? { lastEventId: watermark } : {}),\n };\n }),\n ...(pendingReplies.size > 0 ? { pendingReplies: [...pendingReplies] } : {}),\n clientId,\n } satisfies components['schemas']['BusSubscribeRequest']);\n const url = `${baseUrl}/bus/subscribe`;\n\n const controller = new AbortController();\n inflightControllers.add(controller);\n\n try {\n const headers: Record<string, string> = {\n // The gate's single read: `token` is what this ATTEMPT sends, and\n // exactly what `refusedToken` records if the gateway says 401.\n Authorization: `Bearer ${token}`,\n 'Content-Type': 'application/json',\n };\n const response = await fetch(url, { method: 'POST', headers, body, signal: controller.signal });\n\n if (!response.ok || !response.body) {\n throw new SseConnectError(response.status);\n }\n\n // Stopped/disposed while the fetch was in flight — don't proceed to open\n // (and retire the old connection on) a stream we've been told to tear\n // down. `stop()`/`dispose()` already aborted this controller.\n if (!running) return;\n\n // Make-before-break handoff: the new connection is established (the\n // gateway has subscribed it and any `Last-Event-ID` replay is flowing),\n // so mark the previous connection(s) superseded and LINGER them — keep\n // them draining for LINGER_MS before the abort. Aborting immediately\n // here discarded replies already written to the old socket but not yet\n // read (the buffered-bytes loss in\n // .plans/bugs/concurrent-browse-resource-starvation.md); an event\n // delivered by both connections during the overlap is deduped by id in\n // the read loop below (persisted ids are stable; correlated-reply ids\n // are deterministic per routes/bus.ts). Had the fetch failed, we'd have\n // thrown above and never reached here, leaving the old connection live\n // (no gap).\n if (keepPrevious) {\n for (const c of previous) superseded.add(c);\n const lingerTimer = setTimeout(() => {\n lingerTimers.delete(lingerTimer);\n for (const c of previous) {\n try { c.abort(); } catch { /* noop */ }\n inflightControllers.delete(c);\n }\n }, LINGER_MS);\n lingerTimers.add(lingerTimer);\n }\n\n transition('open');\n retryAttempt = 0; // a success resets the backoff ladder\n refreshBurned = false; // …and re-arms the refresh-once (P4)\n\n const reader = response.body.getReader();\n const decoder = new TextDecoder();\n\n /**\n * Segments of the current, still-incomplete line. A single `data:`\n * line carries a whole JSON payload — a browse reply can run to\n * megabytes — delivered across many `reader.read()` chunks. The\n * previous `buffer += chunk` + `buffer.split('\\n')` re-flattened and\n * re-scanned the ENTIRE accumulated buffer on every read: O(frame² /\n * chunkSize) bytes of large-string allocation per frame, all landing\n * in V8's large-object space, which only major GC reclaims. Under the\n * reply fan-out burst (~85 multi-MB `browse:*-result` frames/min)\n * that allocation rate outran mark-compact and OOM'd the worker\n * (2026-09-03, DoD #7). Segments are joined exactly once, when the\n * line's newline arrives; each read scans only its own chunk.\n */\n let lineSegments: string[] = [];\n\n // SSE parse state is declared OUTSIDE the read loop: a single\n // event can span many `reader.read()` chunks when the payload is\n // large (a full resource-result with annotations can easily exceed\n // one TCP segment). Resetting these on every read would silently\n // drop any event whose `event:`/`id:` headers land in one chunk\n // and whose terminating blank line lands in the next.\n let currentEvent = '';\n let currentData = '';\n let currentId: string | undefined;\n\n while (running && inflightControllers.has(controller)) {\n const { done, value } = await reader.read();\n if (done) break;\n\n const text = decoder.decode(value, { stream: true });\n\n let searchFrom = 0;\n while (searchFrom <= text.length) {\n const nl = text.indexOf('\\n', searchFrom);\n if (nl === -1) {\n if (searchFrom < text.length) {\n lineSegments.push(searchFrom === 0 ? text : text.slice(searchFrom));\n }\n break;\n }\n let line = text.slice(searchFrom, nl);\n searchFrom = nl + 1;\n if (lineSegments.length > 0) {\n lineSegments.push(line);\n line = lineSegments.join('');\n lineSegments = [];\n }\n if (line.startsWith('event: ')) {\n currentEvent = line.slice(7);\n } else if (line.startsWith('data: ')) {\n currentData = line.slice(6);\n } else if (line.startsWith('id: ')) {\n currentId = line.slice(4);\n } else if (line === '') {\n // Skip an overlap duplicate — the same stable-id event delivered\n // by both the old and new connection during a make-before-break\n // handoff (#847). Ephemeral ids are unique per connection, so this\n // never spuriously drops a distinct event.\n const isDuplicate = currentId !== undefined && seenEventIds.has(currentId);\n if (currentEvent === 'bus-event' && currentData && !isDuplicate) {\n const parsed = JSON.parse(currentData) as BusEvent;\n busLog('RECV', parsed.channel, parsed.payload, parsed.scope);\n // Drain-window forensics: an event delivered by a SUPERSEDED\n // (lingering) connection is one that an immediate handover abort\n // would have discarded — the loss mode of\n // .plans/bugs/concurrent-browse-resource-starvation.md. Gated\n // (per-event, bursty during overlap); flip bus logging on to\n // see how real the window is.\n if (busLogEnabled() && superseded.has(controller)) {\n // eslint-disable-next-line no-console\n console.debug(`[bus LINGER] ${parsed.channel} delivered on superseded connection`);\n }\n // Tier 2: lift trace context off the SSE payload (the\n // gateway's writeBusEvent puts it there). The synchronous\n // fan-out to subscribers happens inside the bus.recv span,\n // so handlers see the parent trace.\n const carrier = extractTraceparent(\n parsed.payload as Record<string, unknown>,\n );\n // The two kinds of id bookkeeping sit on OPPOSITE sides of the\n // awaited fan-out, because they answer different questions.\n //\n // `seenEventIds` answers \"has this frame been claimed?\" and must\n // be recorded BEFORE the await: the await yields the event loop,\n // so during a make-before-break overlap the sibling connection\n // can read the same stable-id frame, find the set still missing\n // it, and deliver it a second time — defeating the overlap dedup\n // (#847) that .plans/bugs/BRIDGE-GAPS.md exists to protect.\n // Rolled back if the apply throws, so a redelivery after a\n // dropped read loop is re-processed rather than silently\n // swallowed by its own claim.\n if (currentId !== undefined) rememberEventId(currentId);\n try {\n await withTraceparent(carrier, () =>\n withSpan(\n `bus.recv:${parsed.channel}`,\n () => { events$.next(parsed); },\n {\n kind: SpanKind.CONSUMER,\n attrs: {\n 'bus.channel': parsed.channel,\n ...(parsed.scope ? { 'bus.scope': parsed.scope } : {}),\n },\n },\n ),\n );\n } catch (err) {\n if (currentId !== undefined) forgetEventId(currentId);\n throw err;\n }\n // The resume watermark and the persisted bookmark answer \"have\n // this event's effects been absorbed?\" and stay AFTER the apply.\n // The pre-fix order (stash first, then an AWAITED apply) opened a\n // gap where a bystander cache's debounced save could fire\n // mid-await, find every cache quiet, and flush a bookmark whose\n // event nothing had absorbed — the fast-path reload loss\n // (.plans/bugs/annotation-lost-on-immediate-reload-after-create.md).\n // Both stay on the LAGGING side, which is safe: a reconnect or\n // crash mid-apply resumes from the previous id and redelivers,\n // and re-invalidation is idempotent.\n //\n // Watermarks are PER SCOPE and persisted-ids-only: a `p-*` id is\n // stamped only on scoped deliveries (the frame always carries\n // `scope`), and ephemeral ids never displace a scope's watermark\n // — the silent replay-loss hole the old single-id design had.\n if (currentId !== undefined && currentId.startsWith('p-') && parsed.scope) {\n scopeWatermarks.set(parsed.scope, currentId);\n // B17: persist per scope — see ActorStateUnitOptions.\n options.saveLastEventId?.(parsed.scope, currentId);\n }\n }\n currentEvent = '';\n currentData = '';\n currentId = undefined;\n }\n }\n }\n } catch (err) {\n if ((err as Error).name === 'AbortError') return;\n // A refused connect carries its status out (P2). Anything else — a\n // network failure, a dropped stream — has no status to carry and\n // stays off errors$.\n if (err instanceof SseConnectError) {\n errors$.next(err);\n // 401: re-sending THIS bearer is deterministic, so park instead of\n // retrying (P3). The gate re-admits the actor the moment the getter\n // yields a different token; until then, no requests at all. Non-401\n // refusals (5xx, proxies) are transient in kind and take the\n // backoff path below.\n if (err.status === 401 && running && !superseded.has(controller)) {\n refusedToken = token;\n retryAttempt = 0;\n if (currentState !== 'unauthenticated') transition('unauthenticated');\n // P4: refresh ONCE before staying parked — the same hook the HTTP\n // beforeRetry path uses (D2). Parked state is truthful while the\n // refresh call is in flight. On success the refresher's owner has\n // rotated the token source, so an immediate tick reconnects\n // through the gate's getter read; on null/throw (or an unchanged\n // token) the flat waiting tick stands.\n if (tokenRefresher && !refreshBurned) {\n refreshBurned = true;\n let refreshed: string | null = null;\n try {\n refreshed = await tokenRefresher();\n } catch {\n // A throwing refresher is a failed refresh, not a crash.\n }\n if (running && refreshed && refreshed !== refusedToken) {\n scheduleRetry(0, keepPrevious);\n return;\n }\n }\n scheduleRetry(reconnectMs, keepPrevious);\n return;\n }\n }\n } finally {\n inflightControllers.delete(controller);\n }\n\n // If we reached here without an AbortError, the connection dropped\n // or the fetch failed (a 401 already diverted to `unauthenticated`\n // above). Transition to reconnecting and schedule a retry on the\n // backoff ladder — unless this was a SUPERSEDED (lingering)\n // connection ending: its termination is expected teardown, not a drop\n // of the live stream, and must not restart the reconnect machinery.\n if (running && !superseded.has(controller)) {\n transition('reconnecting');\n scheduleRetry(backoffDelay());\n }\n };\n\n const reconnect = () => {\n if (!running) return;\n // Transition to `reconnecting` BEFORE aborting the current\n // connection. This matches the pre-state-machine contract where\n // gap-detection relied on seeing a \"dropped\" signal before a\n // subsequent \"connected\" signal; with the state machine, the\n // transition sequence `open → reconnecting → connecting → open`\n // is what BrowseNamespace's gap-detection (pre-BUS-RESUMPTION\n // code path) watches for.\n if (currentState === 'open' || currentState === 'connecting' || currentState === 'degraded') {\n transition('reconnecting');\n }\n // Make-before-break: do NOT abort the live connection here. Cancel only a\n // pending drop-recovery retry, then connect — `connect(keepPrevious=true)`\n // retires the old connection after the new one is open (no gap).\n if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }\n connect(true);\n };\n\n // Debounce channel-set-change reconnects. React StrictMode in dev\n // produces mount → cleanup → mount synchronously, which previously\n // translated into three back-to-back reconnects — enough to tear down\n // in-flight responses, fire gap detection, refetch, tear that down\n // again, and leave the page stuck in \"Loading...\" while caches\n // thrashed. With a short debounce the whole sequence collapses into\n // one reconnect after the final channel-set is stable.\n //\n // Two cadences (MULTI-RESOURCE-SCOPE remove-side hysteresis): additions\n // take the fast 100 ms path (a new scope needs liveness now); remove-only\n // changes wait `lazyRemoveMs` — removal merely narrows delivery, and the\n // consumer's hover churn would otherwise reconnect on every mouse pass.\n // The connect body reads current state, so whichever timer fires first\n // carries ALL pending changes; a fast schedule therefore supersedes any\n // pending lazy one, and a lazy schedule never preempts a pending fast one.\n let reconnectTimer2: ReturnType<typeof setTimeout> | null = null;\n let lazyReconnectTimer: ReturnType<typeof setTimeout> | null = null;\n const RECONNECT_DEBOUNCE_MS = 100;\n const scheduleReconnect = () => {\n if (lazyReconnectTimer) { clearTimeout(lazyReconnectTimer); lazyReconnectTimer = null; }\n if (reconnectTimer2) clearTimeout(reconnectTimer2);\n reconnectTimer2 = setTimeout(() => {\n reconnectTimer2 = null;\n reconnect();\n }, RECONNECT_DEBOUNCE_MS);\n };\n const scheduleLazyReconnect = () => {\n if (reconnectTimer2 || lazyReconnectTimer) return; // a pending flush already covers this change\n lazyReconnectTimer = setTimeout(() => {\n lazyReconnectTimer = null;\n reconnect();\n }, lazyRemoveMs);\n };\n\n return {\n on$<T = Record<string, unknown>>(channel: string): Observable<T> {\n return shared$.pipe(\n filter((e) => e.channel === channel),\n map((e) => e.payload as T),\n );\n },\n\n emit: async (channel: string, payload: Record<string, unknown>, emitScope?: string): Promise<number> => {\n // EMIT logging + bus.emit span live at the transport contract layer\n // (`HttpTransport.emit`). ActorStateUnit is plumbing. We do propagate the\n // active span's W3C traceparent on the outbound POST so the gateway\n // can stitch the bus.dispatch server span as a child.\n const body: Record<string, unknown> = { channel, payload, clientId };\n if (emitScope) body.scope = emitScope;\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${getToken()}`,\n };\n const trace = getActiveTraceparent();\n if (trace) {\n headers['traceparent'] = trace.traceparent;\n if (trace.tracestate) headers['tracestate'] = trace.tracestate;\n }\n // Retried per request (SIDECAR-BOOT-RESILIENCE D3): a 429/503/504 or an\n // expired deadline gets another attempt; a 400/401/403 rejects on the\n // first, unretried. The predicate is core's, shared with the boot passes\n // above, so \"retryable\" means one thing across the fleet.\n //\n // The whole attempt — POST, status check, and the error it throws — is\n // inside the retried unit, because the refusal IS the failure being\n // classified. Retrying only the fetch would re-run the request and then\n // hand back the same unexamined response.\n const res = await retryWithBackoff(async () => {\n // Bounded (JOB-RESTART-SAFETY P7): an unresponsive gateway must not hang\n // the caller's loop forever. AbortSignal.timeout rejects with a\n // DOMException named TimeoutError — which the predicate treats as\n // retryable, since a deadline is the definition of \"try again\".\n const attempt = await fetch(`${baseUrl}/bus/emit`, {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n signal: AbortSignal.timeout(EMIT_TIMEOUT_MS),\n });\n // A refused emit (validation 400, auth 401…) must REJECT — busRequest's\n // contract detaches its doomed reply and propagates this to the caller.\n // Resolving a sentinel here instead leaves that caller waiting for a\n // reply the gateway will never send.\n if (!attempt.ok) {\n let detail = '';\n try {\n detail = (await attempt.text()).slice(0, 500);\n } catch {\n // status alone\n }\n // APIError, not a bare Error: the status rides as a FIELD (D1), which\n // is what makes it classifiable without parsing it back out of prose.\n // The message keeps its shape, so callers matching on it are unaffected.\n throw new APIError(\n `/bus/emit ${attempt.status}${detail ? `: ${detail}` : ''}`,\n attempt.status,\n attempt.statusText,\n detail || undefined,\n );\n }\n return attempt;\n }, isRetryableRequestError, EMIT_RETRY);\n // `-1` = count unknown (older gateway / unreadable body) — never let a\n // parse failure read as an empty room. Same sentinel as the Go client.\n try {\n const reply = (await res.json()) as { subscribers?: unknown };\n return typeof reply.subscribers === 'number' ? reply.subscribers : -1;\n } catch {\n return -1;\n }\n },\n\n state$: state$.asObservable(),\n\n errors$: errors$.asObservable(),\n\n isSubscribed: (channel: string) => globalChannels.has(channel),\n\n addChannels: (channels: string[], scope?: string) => {\n let changed = false;\n if (scope !== undefined) {\n let entry = scopedSubscriptions.get(scope);\n if (!entry) {\n entry = new Set<string>();\n scopedSubscriptions.set(scope, entry);\n }\n for (const ch of channels) {\n if (!entry.has(ch)) { entry.add(ch); changed = true; }\n }\n } else {\n for (const ch of channels) {\n if (!globalChannels.has(ch)) { globalChannels.add(ch); changed = true; }\n }\n }\n if (changed) scheduleReconnect();\n },\n\n removeChannels: (channels: string[], scope?: string) => {\n let changed = false;\n if (scope !== undefined) {\n const entry = scopedSubscriptions.get(scope);\n if (entry) {\n for (const ch of channels) {\n if (entry.delete(ch)) changed = true;\n }\n // The watermark survives the scope's removal deliberately: a later\n // re-subscribe replays what was missed in between.\n if (entry.size === 0) scopedSubscriptions.delete(scope);\n }\n } else {\n for (const ch of channels) {\n if (globalChannels.delete(ch)) changed = true;\n }\n }\n if (changed) scheduleLazyReconnect();\n },\n\n trackReply: (correlationId: string) => {\n pendingReplies.add(correlationId);\n let released = false;\n return () => {\n if (released) return;\n released = true;\n pendingReplies.delete(correlationId);\n };\n },\n\n start: () => {\n if (running) return;\n running = true;\n connect();\n },\n\n stop: () => {\n running = false;\n if (currentState !== 'closed') transition('closed');\n if (reconnectTimer2) { clearTimeout(reconnectTimer2); reconnectTimer2 = null; }\n if (lazyReconnectTimer) { clearTimeout(lazyReconnectTimer); lazyReconnectTimer = null; }\n if (degradedTimer) { clearTimeout(degradedTimer); degradedTimer = null; }\n disconnect();\n },\n\n dispose: () => {\n running = false;\n if (currentState !== 'closed') transition('closed');\n if (reconnectTimer2) { clearTimeout(reconnectTimer2); reconnectTimer2 = null; }\n if (lazyReconnectTimer) { clearTimeout(lazyReconnectTimer); lazyReconnectTimer = null; }\n if (degradedTimer) { clearTimeout(degradedTimer); degradedTimer = null; }\n disconnect();\n events$.complete();\n state$.complete();\n errors$.complete();\n },\n };\n}\n","/**\n * HttpTransport — the HTTP/SSE implementation of ITransport.\n *\n * Phase 1 of TRANSPORT-ABSTRACTION. Owns everything that crosses the wire\n * in remote mode: the bus actor (SSE + POST /bus/emit), auth/admin/exchange/\n * system HTTP endpoints, and connection-state plumbing.\n *\n * Does NOT own the local coordination bus — that lives on `SemiontClient`.\n * `bridgeInto(bus)` wires SSE-received events into the caller-supplied bus\n * once at construction.\n */\n\nimport ky, { HTTPError, type KyInstance } from 'ky';\nimport { BehaviorSubject, Observable, Subject } from 'rxjs';\nimport type {\n AccessToken,\n BaseUrl,\n Email,\n EventBus,\n EventMap,\n GoogleCredential,\n Logger,\n RefreshToken,\n ResourceId,\n UserDID,\n components,\n} from '@semiont/core';\nimport {\n PERSISTED_EVENT_TYPES,\n RESOURCE_BROADCAST_TYPES,\n SemiontError,\n busLog,\n} from '@semiont/core';\nimport { SpanKind, recordBusEmit, withSpan } from '@semiont/observability';\nimport { createActorStateUnit, type ActorStateUnit } from './actor-state-unit';\nimport { APIError } from './api-error';\nimport type {\n ConnectionState,\n IGatewayOperations,\n ITransport,\n HealthCheckResponse,\n StatusResponse,\n UserResponse,\n UpdateUserRequest,\n UpdateUserResponse,\n ListUsersResponse,\n} from '@semiont/core';\nimport { BRIDGED_CHANNELS } from '@semiont/core';\n\ntype AuthResponse = components['schemas']['AuthResponse'];\ntype TokenRefreshResponse = components['schemas']['TokenRefreshResponse'];\ntype AdminUserStatsResponse = components['schemas']['AdminUserStatsResponse'];\ntype OAuthConfigResponse = components['schemas']['OAuthConfigResponse'];\n\n// ── Channel constants (mirror client.ts) ────────────────────────────────\n\nexport const RESOURCE_SCOPED_CHANNELS = [\n // Exclude channels already globally bridged: a channel in both lists is\n // forwarded twice on a scoped connection (global copy → ephemeral id, scoped\n // copy → persisted id) with different SSE ids, escaping the client dedup\n // (.plans/bugs/BRIDGE-GAPS.md). Generalizes the former one-off\n // `frame:entity-type-added` exclusion.\n ...PERSISTED_EVENT_TYPES.filter((t) => !(BRIDGED_CHANNELS as readonly string[]).includes(t)),\n ...RESOURCE_BROADCAST_TYPES,\n];\n\nexport type TokenRefresher = () => Promise<string | null>;\n\nexport interface HttpTransportConfig {\n baseUrl: BaseUrl;\n /** Observable token source; headers read the current value. */\n token$?: BehaviorSubject<AccessToken | null>;\n timeout?: number;\n retry?: number;\n logger?: Logger;\n /** Optional 401-recovery hook. See {@link TokenRefresher}. */\n tokenRefresher?: TokenRefresher;\n /**\n * B17 — persistence thunks for the last seen persisted SSE id PER\n * SCOPE, passed through to the actor state unit. See\n * {@link ActorStateUnitOptions}.\n */\n loadLastEventIds?: () => Record<string, string> | null;\n saveLastEventId?: (scope: string, id: string) => void;\n /**\n * The global SSE channel set this transport subscribes. Absent means the\n * full `BRIDGED_CHANNELS` — a full client must receive every operation's\n * reply channel, or its `busRequest`s time out. A narrow-profile process\n * (the worker) passes exactly the reply channels for the operations it\n * awaits: reply channels are global fan-out on the gateway, so a full\n * subscription receives every OTHER client's replies too — measured at\n * ~85 multi-MB `browse:annotations-result` frames/min during the\n * 2026-09-03 worker OOM, all parsed and dropped by cid filtering.\n * A `busRequest` on an operation whose replies are outside this set\n * fails fast with `bus.unsubscribed` (see `BusRequestPrimitive`).\n */\n channels?: readonly string[];\n}\n\nexport class HttpTransport implements ITransport, IGatewayOperations {\n readonly baseUrl: BaseUrl;\n private readonly http: KyInstance;\n private readonly token$: BehaviorSubject<AccessToken | null>;\n private readonly logger?: Logger;\n private readonly errorsSubject: Subject<SemiontError> = new Subject<SemiontError>();\n /**\n * Stream of `APIError` instances surfaced from any HTTP request just\n * before the transport throws to the caller. Satisfies the `ITransport`\n * `errors$` contract — see `@semiont/core/transport.ts`.\n */\n readonly errors$: Observable<SemiontError> = this.errorsSubject.asObservable();\n\n private _actor: ActorStateUnit | null = null;\n private _actorStarted = false;\n private disposed = false;\n\n /**\n * Per-resource subscription ref-counts (MULTI-RESOURCE-SCOPE). Distinct\n * resources COMPOSE — each key's first subscribe adds its scoped channels\n * to the actor's matrix, its last release removes them; keys are fully\n * independent. Local fan-out for scoped channels is a SINGLETON wired in\n * the actor getter (one delivery per event regardless of how many scopes\n * are held), so entries here are counts only.\n */\n private readonly scopeRefCounts = new Map<string, number>();\n\n /** Buses we've been asked to bridge wire events into. */\n private readonly bridges: EventBus[] = [];\n\n private readonly config: HttpTransportConfig;\n\n constructor(config: HttpTransportConfig) {\n const { baseUrl, timeout = 30000, retry = 2, logger, tokenRefresher } = config;\n this.config = config;\n\n this.baseUrl = (baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl) as BaseUrl;\n this.token$ = config.token$ ?? new BehaviorSubject<AccessToken | null>(null);\n this.logger = logger;\n\n // Retry policy: when a refresher is configured, expand retry to also\n // cover 401 (one attempt). Otherwise use the plain `retry` number.\n const retryConfig = tokenRefresher\n ? {\n limit: 1,\n methods: ['get', 'post', 'put', 'patch', 'delete', 'head', 'options'],\n statusCodes: [401, 408, 413, 429, 500, 502, 503, 504],\n }\n : retry;\n\n this.http = ky.create({\n timeout,\n retry: retryConfig,\n hooks: {\n beforeRequest: [\n ({ request }) => {\n if (this.logger) {\n this.logger.debug('HTTP Request', {\n type: 'http_request',\n url: request.url,\n method: request.method,\n timestamp: Date.now(),\n hasAuth: request.headers.has('Authorization'),\n });\n }\n },\n ],\n beforeRetry: tokenRefresher\n ? [\n async ({ request, error }) => {\n if (!(error instanceof HTTPError) || error.response.status !== 401) {\n return undefined;\n }\n try {\n const newToken = await tokenRefresher();\n if (!newToken) return ky.stop;\n request.headers.set('Authorization', `Bearer ${newToken}`);\n return undefined;\n } catch {\n return ky.stop;\n }\n },\n ]\n : [],\n afterResponse: [\n ({ request, response }) => {\n if (this.logger) {\n this.logger.debug('HTTP Response', {\n type: 'http_response',\n url: request.url,\n method: request.method,\n status: response.status,\n statusText: response.statusText,\n });\n }\n return response;\n },\n ],\n beforeError: [\n async ({ request, error }) => {\n const response = error instanceof HTTPError ? error.response : undefined;\n if (response) {\n const body = await response.json().catch(() => ({})) as { message?: string };\n if (this.logger) {\n this.logger.error('HTTP Request Failed', {\n type: 'http_error',\n url: request.url,\n method: request.method,\n status: response.status,\n statusText: response.statusText,\n error: body.message || `HTTP ${response.status}: ${response.statusText}`,\n });\n }\n const apiError = new APIError(\n body.message || `HTTP ${response.status}: ${response.statusText}`,\n response.status,\n response.statusText,\n body,\n );\n this.errorsSubject.next(apiError);\n throw apiError;\n }\n return error;\n },\n ],\n },\n });\n\n // Auto-start the bus actor once a token arrives.\n this.token$.subscribe((token) => {\n if (token && !this._actorStarted && !this.disposed) {\n this._actorStarted = true;\n this.actor.start();\n }\n });\n }\n\n // ── Lazy actor construction + per-channel fan-in to bridges ───────────\n //\n // `actor` is exposed so the legacy `SemiontClient` can keep `.actor`\n // pointing at the same ActorStateUnit during the transport-abstraction\n // migration. Once SemiontClient is removed, this should be made\n // private again — external callers should use emit/on/stream/state$.\n\n get actor(): ActorStateUnit {\n if (!this._actor) {\n const globalChannels = this.config.channels ?? BRIDGED_CHANNELS;\n this._actor = createActorStateUnit({\n baseUrl: this.baseUrl,\n token: () => this.token$.getValue() ?? '',\n channels: [...globalChannels],\n ...(this.config.loadLastEventIds ? { loadLastEventIds: this.config.loadLastEventIds } : {}),\n ...(this.config.saveLastEventId ? { saveLastEventId: this.config.saveLastEventId } : {}),\n // The SAME hook the ky beforeRetry path uses (SSE-AUTH-RESILIENCE\n // P4, D2) — the SSE connect path refreshes once before parking\n // `unauthenticated`, and no second refresh mechanism exists.\n ...(this.config.tokenRefresher ? { tokenRefresher: this.config.tokenRefresher } : {}),\n });\n // Refused connects surface on the transport's contract stream too —\n // an SSE subscribe IS an HTTP request, and `SseConnectError` is a\n // `SemiontError` (SSE-AUTH-RESILIENCE P4, closing P2's deferred\n // bridge question).\n this._actor.errors$.subscribe((e) => this.errorsSubject.next(e));\n // One fan-in per channel, wired once for the actor's lifetime — the\n // globally-subscribed set AND the resource-scoped set (disjoint by the\n // bus-invariants guard). Scoped events only arrive for scopes in the\n // actor's matrix (gateway-authoritative filtering), so an always-on\n // scoped fan-in delivers nothing while no scope is held — and exactly\n // ONCE per event however many scopes are held (the per-scope\n // bridge-subs design would have duplicated delivery N×).\n for (const channel of [...globalChannels, ...RESOURCE_SCOPED_CHANNELS]) {\n this._actor.on$<Record<string, unknown>>(channel).subscribe((payload) => {\n for (const bus of this.bridges) {\n (bus.get(channel as keyof EventMap) as { next(v: unknown): void }).next(payload);\n }\n });\n }\n }\n return this._actor;\n }\n\n // ── ITransport — bus primitives ───────────────────────────────────────\n\n async emit<K extends keyof EventMap>(\n channel: K,\n payload: EventMap[K],\n resourceScope?: ResourceId,\n ): Promise<number> {\n busLog('EMIT', channel as string, payload, resourceScope as string | undefined);\n recordBusEmit(channel as string, resourceScope as string | undefined);\n return withSpan(\n `bus.emit:${channel as string}`,\n async () => {\n if (resourceScope !== undefined) {\n return this.actor.emit(\n channel as string,\n payload as unknown as Record<string, unknown>,\n resourceScope as string,\n );\n }\n return this.actor.emit(\n channel as string,\n payload as unknown as Record<string, unknown>,\n );\n },\n {\n kind: SpanKind.PRODUCER,\n attrs: {\n 'bus.channel': channel as string,\n ...(resourceScope ? { 'bus.scope': resourceScope as string } : {}),\n },\n },\n );\n }\n\n on<K extends keyof EventMap>(\n channel: K,\n handler: (payload: EventMap[K]) => void,\n ): () => void {\n const sub = this.actor.on$<EventMap[K]>(channel as string).subscribe(handler);\n return () => sub.unsubscribe();\n }\n\n stream<K extends keyof EventMap>(channel: K): Observable<EventMap[K]> {\n return this.actor.on$<EventMap[K]>(channel as string);\n }\n\n /**\n * Wire this transport's SSE fan-in into the given bus. Every channel\n * in `BRIDGED_CHANNELS` (and subsequently per-resource scoped channels\n * opened by `subscribeToResource`) is published on the bus. Safe to\n * call multiple times — each bus is added to the fan-out list.\n */\n bridgeInto(bus: EventBus): void {\n this.bridges.push(bus);\n }\n\n subscribeToResource(resourceId: ResourceId): () => void {\n const key = resourceId as string;\n const count = this.scopeRefCounts.get(key) ?? 0;\n this.scopeRefCounts.set(key, count + 1);\n if (count === 0) {\n this.actor.addChannels([...RESOURCE_SCOPED_CHANNELS], key);\n }\n\n let called = false;\n return () => {\n if (called) return;\n called = true;\n const remaining = (this.scopeRefCounts.get(key) ?? 0) - 1;\n if (remaining > 0) {\n this.scopeRefCounts.set(key, remaining);\n return;\n }\n this.scopeRefCounts.delete(key);\n this.actor.removeChannels([...RESOURCE_SCOPED_CHANNELS], key);\n };\n }\n\n get state$(): Observable<ConnectionState> {\n return this.actor.state$;\n }\n\n /**\n * Correlated-reply retention, client side (BUS-RESUMPTION Phase 2 /\n * SDK-DEBT S1): `busRequest` registers its cid here before emitting;\n * the actor carries the tracked set as `pendingReplies` on every\n * subscribe body, so a reply published while the connection was down\n * replays from the server's retention buffer on reconnect.\n */\n trackReply(correlationId: string): () => void {\n return this.actor.trackReply(correlationId);\n }\n\n /**\n * `busRequest`'s fail-fast probe (`BusRequestPrimitive.isSubscribed`):\n * whether the actor's global subscription set delivers `channel`. On a\n * full client (no `channels` config) every bridged reply channel is\n * subscribed and this never gates; on a narrowed transport it turns a\n * doomed request into an immediate `bus.unsubscribed` error.\n */\n isSubscribed(channel: string): boolean {\n return this.actor.isSubscribed(channel);\n }\n\n dispose(): void {\n if (this.disposed) return;\n this.disposed = true;\n this.scopeRefCounts.clear();\n if (this._actor) {\n this._actor.dispose();\n this._actor = null;\n }\n this.errorsSubject.complete();\n }\n\n /**\n * Route a transport-level error onto `errors$`. Used by sibling adapters\n * (e.g. `HttpContentTransport`'s XHR upload path) that don't go through\n * the `ky` `beforeError` hook and need to surface failures on the same\n * stream the rest of the transport publishes to.\n */\n pushError(error: SemiontError): void {\n if (this.disposed) return;\n this.errorsSubject.next(error);\n }\n\n // ── Auth ──────────────────────────────────────────────────────────────\n\n private authHeaders(): Record<string, string> {\n const token = this.token$.getValue() ?? undefined;\n return token ? { Authorization: `Bearer ${token}` } : {};\n }\n\n async authenticatePassword(email: Email, password: string): Promise<AuthResponse> {\n return this.http.post(`${this.baseUrl}/api/tokens/password`, {\n json: { email, password },\n headers: this.authHeaders(),\n }).json();\n }\n\n async authenticateGoogle(credential: GoogleCredential): Promise<AuthResponse> {\n return this.http.post(`${this.baseUrl}/api/tokens/google`, {\n json: { credential },\n headers: this.authHeaders(),\n }).json();\n }\n\n async refreshAccessToken(token: RefreshToken): Promise<TokenRefreshResponse> {\n return this.http.post(`${this.baseUrl}/api/tokens/refresh`, {\n json: { refreshToken: token },\n headers: this.authHeaders(),\n }).json();\n }\n\n async logout(): Promise<void> {\n await this.http.post(`${this.baseUrl}/api/users/logout`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async acceptTerms(): Promise<void> {\n await this.http.post(`${this.baseUrl}/api/users/accept-terms`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getCurrentUser(): Promise<UserResponse> {\n return this.http.get(`${this.baseUrl}/api/users/me`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getMediaToken(resourceId: ResourceId): Promise<{ token: string }> {\n return this.http.post(`${this.baseUrl}/api/tokens/media`, {\n json: { resourceId },\n headers: this.authHeaders(),\n }).json();\n }\n\n // ── Admin ─────────────────────────────────────────────────────────────\n\n async listUsers(): Promise<ListUsersResponse> {\n return this.http.get(`${this.baseUrl}/api/admin/users`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getUserStats(): Promise<AdminUserStatsResponse> {\n return this.http.get(`${this.baseUrl}/api/admin/users/stats`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async updateUser(id: UserDID, data: UpdateUserRequest): Promise<UpdateUserResponse> {\n return this.http.patch(`${this.baseUrl}/api/admin/users/${id}`, {\n json: data,\n headers: this.authHeaders(),\n }).json();\n }\n\n async getOAuthConfig(): Promise<OAuthConfigResponse> {\n return this.http.get(`${this.baseUrl}/api/admin/oauth/config`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n // ── System status ─────────────────────────────────────────────────────\n\n async healthCheck(): Promise<HealthCheckResponse> {\n return this.http.get(`${this.baseUrl}/api/health`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n async getStatus(): Promise<StatusResponse> {\n return this.http.get(`${this.baseUrl}/api/status`, {\n headers: this.authHeaders(),\n }).json();\n }\n\n // ── Internal: ky accessor for legacy passthroughs (temporary) ─────────\n\n /**\n * Temporary escape hatch for the ongoing transport migration: namespaces\n * that still need to issue ad-hoc HTTP calls (e.g. legacy browse/mark\n * HTTP fallbacks) can borrow the configured `ky` instance here. Will be\n * deleted once all namespaces route through bus channels or through\n * typed methods on this transport.\n */\n get rawHttp(): KyInstance {\n return this.http;\n }\n\n /**\n * Current access token (synchronously read from the BehaviorSubject).\n * Used by content-transport and legacy namespace HTTP fallbacks that\n * need to pass `auth: token` through some code paths.\n */\n getToken(): AccessToken | undefined {\n return this.token$.getValue() ?? undefined;\n }\n}\n\n// Re-export for convenience\nexport type { ConnectionState } from '@semiont/core';\n","/**\n * HttpContentTransport — binary I/O over HTTP.\n *\n * Phase 1 of TRANSPORT-ABSTRACTION. Narrow by design because binary has\n * different backpressure and streaming characteristics than typed command\n * payloads. Uses the HttpTransport's underlying ky instance + token, so\n * retries, logging, and auth behave identically to the rest of the wire.\n *\n * Two `putBinary` paths live side by side, selected by runtime\n * environment + caller intent:\n * - **ky path (default + Node)** — the original `ky.post(...)` path.\n * Keeps retry-with-refresh, beforeError → APIError, observability\n * spans intact. Hits when no `onProgress`/`signal` is passed, OR\n * when `XMLHttpRequest` isn't available in the runtime (Node\n * workers, the CLI). On Node-side `signal`-aborts: the in-flight\n * `fetch` continues in the background and the `cancelled` flag in\n * `yield.resource` suppresses the resolve/reject callbacks.\n * - **XHR path (browsers with `onProgress` or `signal`)** — hand-rolled\n * because `ky` wraps `fetch` which can't observe upload byte-\n * progress today (`Request({ duplex: 'half' })` is the long-term\n * direction; not yet widely available across the webviews this\n * codepath needs to run in). Threads auth + traceparent headers,\n * emits `onProgress` from `xhr.upload.onprogress`, supports\n * cancellation via the `signal` option (calling `xhr.abort()`),\n * and routes failures onto the same `transport.errors$` stream\n * the ky path uses.\n *\n * The runtime check on `XMLHttpRequest` is the load-bearing seam: a\n * Node worker calling `client.yield.resource(...)` (which always passes\n * a `signal` for unsubscribe-aborts) must NOT take the XHR path —\n * `XMLHttpRequest` is undefined and the upload throws synchronously.\n * Browsers always have it; Node does not.\n *\n * v1 limitation: the XHR path does NOT auto-refresh on 401. Mitigation:\n * the session's proactive refresh fires before token expiry, so an\n * upload that *starts* with a fresh token usually completes. An upload\n * spanning the narrow window between expiry and proactive-refresh would\n * fail; the existing `errors$` → modal path surfaces it as session-\n * expired. If retry-with-refresh on the upload path becomes a real\n * complaint, wire a manual retry loop here that reads `token$` afresh.\n */\n\nimport type { AccessToken, ResourceId, PutBinaryOptions, components } from '@semiont/core';\nimport { busLog } from '@semiont/core';\nimport { SpanKind, getActiveTraceparent, withSpan } from '@semiont/observability';\nimport type { HttpTransport } from './http-transport';\nimport { APIError } from './api-error';\nimport type { IContentTransport, PutBinaryRequest } from '@semiont/core';\n\ntype GetResourceResponse = components['schemas']['GetResourceResponse'];\n\nexport class HttpContentTransport implements IContentTransport {\n constructor(private readonly transport: HttpTransport) {}\n\n async putBinary(\n request: PutBinaryRequest,\n options?: PutBinaryOptions,\n ): Promise<{ resourceId: ResourceId }> {\n const sizeBytes = request.file instanceof File ? request.file.size : request.file.length;\n busLog('PUT', 'content', {\n name: request.name,\n format: request.format,\n storageUri: request.storageUri,\n sizeBytes,\n });\n return withSpan(\n 'content.put',\n async () => {\n const formData = buildFormData(request);\n const headers = this.requestHeaders(options?.auth);\n\n // Branch on caller intent AND runtime support. The ky path is\n // the well-trodden default; the XHR path lights up only when a\n // caller wants byte progress or cancellation AND the runtime\n // has `XMLHttpRequest` (browsers do; Node does not). Without\n // the runtime guard, every Node-side `yield.resource(...)`\n // call (which always passes `signal`) would throw\n // `XMLHttpRequest is not defined`.\n const xhrAvailable = typeof XMLHttpRequest !== 'undefined';\n if (xhrAvailable && (options?.onProgress || options?.signal)) {\n return uploadViaXhr({\n url: `${this.transport.baseUrl}/resources`,\n formData,\n headers,\n onProgress: options.onProgress,\n signal: options.signal,\n onApiError: (err) => this.transport.pushError(err),\n });\n }\n\n const result = await this.transport.rawHttp\n .post(`${this.transport.baseUrl}/resources`, {\n body: formData,\n headers,\n })\n .json<{ resourceId: string }>();\n\n return { resourceId: result.resourceId as ResourceId };\n },\n {\n kind: SpanKind.CLIENT,\n attrs: {\n 'content.format': request.format,\n 'content.size_bytes': sizeBytes,\n },\n },\n );\n }\n\n async getBinary(\n resourceId: ResourceId,\n options?: { auth?: AccessToken },\n ): Promise<{ data: ArrayBuffer; contentType: string }> {\n busLog('GET', 'content', { resourceId });\n return withSpan(\n 'content.get',\n async () => {\n // Pure pipe: no Accept header — the route serves the stored bytes\n // verbatim with their real Content-Type (SIMPLER-JSON-LD.md).\n const response = await this.transport.rawHttp.get(`${this.transport.baseUrl}/resources/${resourceId}`, {\n headers: this.requestHeaders(options?.auth),\n });\n const contentType = response.headers.get('content-type') || 'application/octet-stream';\n const data = await response.arrayBuffer();\n return { data, contentType };\n },\n { kind: SpanKind.CLIENT, attrs: { 'resource.id': resourceId as unknown as string } },\n );\n }\n\n async getBinaryStream(\n resourceId: ResourceId,\n options?: { auth?: AccessToken },\n ): Promise<{ stream: ReadableStream<Uint8Array>; contentType: string }> {\n busLog('GET', 'content', { resourceId, stream: true });\n return withSpan(\n 'content.get',\n async () => {\n // Pure pipe: no Accept header (see getBinary).\n const response = await this.transport.rawHttp.get(`${this.transport.baseUrl}/resources/${resourceId}`, {\n headers: this.requestHeaders(options?.auth),\n });\n const contentType = response.headers.get('content-type') || 'application/octet-stream';\n if (!response.body) {\n throw new Error('Response body is null - cannot create stream');\n }\n return { stream: response.body, contentType };\n },\n {\n kind: SpanKind.CLIENT,\n attrs: { 'resource.id': resourceId as unknown as string, 'content.stream': true },\n },\n );\n }\n\n /**\n * Dereference the resource's JSON-LD graph over HTTP — the LD face an\n * external linked-data client sees. Deliberately HTTP, not the bus\n * (SIMPLER-JSON-LD.md §5).\n */\n async getResourceGraph(\n resourceId: ResourceId,\n options?: { auth?: AccessToken },\n ): Promise<GetResourceResponse> {\n busLog('GET', 'content', { resourceId, graph: true });\n return withSpan(\n 'content.get_graph',\n () =>\n this.transport.rawHttp\n .get(`${this.transport.baseUrl}/resources/${resourceId}/jsonld`, {\n headers: this.requestHeaders(options?.auth),\n })\n .json<GetResourceResponse>(),\n { kind: SpanKind.CLIENT, attrs: { 'resource.id': resourceId as unknown as string, 'content.graph': true } },\n );\n }\n\n\n\n\n\n dispose(): void {\n // HttpContentTransport has no resources of its own; HttpTransport owns\n // the ky instance and token subject. No-op is correct here.\n }\n\n /** Auth header + W3C trace propagation for the active span. */\n private requestHeaders(override?: AccessToken): Record<string, string> {\n const token = override ?? this.transport.getToken();\n const headers: Record<string, string> = token ? { Authorization: `Bearer ${token}` } : {};\n const trace = getActiveTraceparent();\n if (trace) {\n headers['traceparent'] = trace.traceparent;\n if (trace.tracestate) headers['tracestate'] = trace.tracestate;\n }\n return headers;\n }\n}\n\nfunction buildFormData(request: PutBinaryRequest): FormData {\n const formData = new FormData();\n formData.append('name', request.name);\n formData.append('format', request.format);\n formData.append('storageUri', request.storageUri);\n\n if (request.file instanceof File) {\n formData.append('file', request.file);\n } else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(request.file)) {\n // `Buffer` is a Node global; referencing it bare in the browser throws\n // ReferenceError before the isBuffer call. Browser uploads always hit\n // the File branch above; this branch is for Node-side workers.\n const blob = new Blob([new Uint8Array(request.file)], { type: request.format });\n formData.append('file', blob, request.name);\n } else {\n throw new Error('file must be a File or Buffer');\n }\n\n if (request.entityTypes && request.entityTypes.length > 0) {\n formData.append('entityTypes', JSON.stringify(request.entityTypes));\n }\n if (request.language) formData.append('language', request.language);\n if (request.sourceAnnotationId) formData.append('sourceAnnotationId', String(request.sourceAnnotationId));\n if (request.sourceResourceId) formData.append('sourceResourceId', String(request.sourceResourceId));\n if (request.generationPrompt) formData.append('generationPrompt', request.generationPrompt);\n if (request.generator) formData.append('generator', JSON.stringify(request.generator));\n if (request.cloneToken) formData.append('cloneToken', request.cloneToken);\n if (request.archiveOriginal !== undefined) formData.append('archiveOriginal', String(request.archiveOriginal));\n if (request.isDraft !== undefined) formData.append('isDraft', String(request.isDraft));\n\n return formData;\n}\n\ninterface XhrUploadOptions {\n url: string;\n formData: FormData;\n headers: Record<string, string>;\n onProgress?: (event: { bytesUploaded: number; totalBytes: number }) => void;\n signal?: AbortSignal;\n onApiError: (error: APIError) => void;\n}\n\n/**\n * XHR-based POST that exposes `xhr.upload.onprogress` byte counts and\n * supports cancellation via `AbortSignal`. Mirrors the ky path's error\n * shape: 4xx/5xx and network-level failures both surface as `APIError`,\n * and every error is routed onto `transport.errors$` before the promise\n * rejects.\n */\nfunction uploadViaXhr(opts: XhrUploadOptions): Promise<{ resourceId: ResourceId }> {\n const { url, formData, headers, onProgress, signal, onApiError } = opts;\n\n return new Promise((resolve, reject) => {\n const xhr = new XMLHttpRequest();\n\n if (signal?.aborted) {\n const err = new APIError('Upload aborted', 0, 'aborted');\n onApiError(err);\n reject(err);\n return;\n }\n\n xhr.open('POST', url);\n for (const [name, value] of Object.entries(headers)) {\n xhr.setRequestHeader(name, value);\n }\n\n if (onProgress) {\n xhr.upload.onprogress = (e: ProgressEvent) => {\n // `lengthComputable` is true when Content-Length is known. For\n // FormData posts the browser computes it, so this is true in\n // practice; the false branch handles the rare chunked-encoding\n // / gzip-while-uploading case.\n const totalBytes = e.lengthComputable ? e.total : 0;\n onProgress({ bytesUploaded: e.loaded, totalBytes });\n };\n }\n\n xhr.onload = () => {\n if (xhr.status >= 200 && xhr.status < 300) {\n try {\n const body = JSON.parse(xhr.responseText) as { resourceId: string };\n resolve({ resourceId: body.resourceId as ResourceId });\n } catch (parseErr) {\n const err = new APIError(\n `Upload succeeded but response was not valid JSON: ${(parseErr as Error).message}`,\n xhr.status,\n xhr.statusText,\n xhr.responseText,\n );\n onApiError(err);\n reject(err);\n }\n return;\n }\n let body: unknown = xhr.responseText;\n try { body = JSON.parse(xhr.responseText); } catch { /* keep as text */ }\n const message = (body && typeof body === 'object' && 'message' in body && typeof (body as { message: unknown }).message === 'string')\n ? (body as { message: string }).message\n : `HTTP ${xhr.status}: ${xhr.statusText}`;\n const err = new APIError(message, xhr.status, xhr.statusText, body);\n onApiError(err);\n reject(err);\n };\n\n xhr.onerror = () => {\n // Network-level failure (DNS, TCP reset, CORS). XHR doesn't give\n // us a useful status here; classify as `unavailable` via 0 status\n // mapping in classifyApiCode.\n const err = new APIError('Network error during upload', 0, 'network-error');\n onApiError(err);\n reject(err);\n };\n\n xhr.ontimeout = () => {\n const err = new APIError('Upload timed out', 0, 'timeout');\n onApiError(err);\n reject(err);\n };\n\n xhr.onabort = () => {\n // Caller-initiated abort via `signal`. Emit a single APIError so the\n // shape matches the other failure paths; consumers can disambiguate\n // via `signal.aborted` if they need to.\n const err = new APIError('Upload aborted', 0, 'aborted');\n onApiError(err);\n reject(err);\n };\n\n if (signal) {\n const onAbort = () => xhr.abort();\n signal.addEventListener('abort', onAbort, { once: true });\n // No teardown for the listener — once xhr fires onabort/onerror/onload\n // the signal is no longer relevant; the listener is GC'd with the xhr.\n }\n\n xhr.send(formData);\n });\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@semiont/http-transport",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.34",
|
|
4
4
|
"description": "HTTP transport adapters for the Semiont SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"test:coverage": "vitest run --coverage"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@semiont/core": "0.5.
|
|
43
|
-
"@semiont/observability": "0.5.
|
|
42
|
+
"@semiont/core": "0.5.34",
|
|
43
|
+
"@semiont/observability": "0.5.34",
|
|
44
44
|
"ky": "^2.1.0",
|
|
45
45
|
"rxjs": "^7.8.1"
|
|
46
46
|
},
|