Auth refresh
The fetcher module carries the credentials; the orchestrator keeps one auth state for everything its engine fetches. This page follows a refused token from the fetcher to a refresh and the next read. The credentials guide shows the code a page writes around it. The tabs' requests and the events pass through the RpcServer, which the diagram leaves out.
- The refusal. The fetcher throws an
AuthInvalidErrorwhen the backend refuses its credentials. The orchestrator recognises it by its marker,code === 'tscache:auth-invalid', not by class. If the fetch started under the current auth generation, auth becomes invalid; a refusal of a fetch started before the lastupdateAuthis ignored, since it says nothing about the newer credentials. - The broadcast. On the flip to invalid, and only then, the server sends
authInvalidto every ready port, with the error and the context the refused fetch carried. In the shared hosting that is every connected tab; a dedicated worker or an in-process engine has one client, so only that tab hears it. - Reads do not wait. Reads still waiting on fetches are released at the flip: their outstanding fetches are reported as
'auth-pending'and finish in the background. While auth is invalid, a read fetches and joins nothing, and its missing slots come back as'auth-pending'at once. AcacheOnlyread never reaches the auth state. - The update. A page calls
client.updateAuth(context). Updates apply one at a time, in call order. The orchestrator waits for the fetcher module's load if it is still running, then calls the module's optionalupdateAuthhook with a copy of the context. While the hook runs, reads fetch and join nothing and report'auth-pending'. Then the new context and the next auth generation take effect together, and auth is valid again. If the hook throws,updateAuthrejects and the previous context stays in place. Without a fetcher module,updateAuthdoes nothing. - The next read. Nothing is retried automatically: a read that already returned
'auth-pending'stays answered. Each page callsgetagain; the new fetches carry the new context.
A tab that joins with createClient({ fetcher: { module, context } }) while another tab's context is in place sends its context in init. When it is not the identical value, the worker applies it through the same updateAuth path (Sharing across tabs), with one exception: a context that is the same material (sameMaterial) as the one the last refusal named changes nothing, whatever the auth state. A tab that read the expired token at startup therefore neither reopens fetching nor undoes a refresh that took effect before its turn. The worker remembers the refused context until the next refusal replaces it, and checks the rule when the update's turn comes and again after the fetcher's hook, in case a refusal lands while the hook runs. A tab's own updateAuth call is always applied.
Drawn from:
packages/tscache/src/orchestrator/orchestrator.ts#Orchestratorpackages/tscache/src/orchestrator/auth.ts#AuthStatepackages/tscache/src/orchestrator/auth.ts#sameMaterialpackages/tscache/src/errors.ts#AuthInvalidErrorpackages/tscache/src/errors.ts#isAuthInvalidErrorpackages/tscache/src/errors.ts#AUTH_INVALID_CODEpackages/tscache/src/rpc/server.ts#RpcServerpackages/tscache/src/client/client.ts#TscacheClientpackages/tscache/src/client/events.ts#ClientEmitterpackages/tscache/src/types.ts#Fetcher