How it works
This section describes tscache from its source code. Each diagram lists the files and symbols it is drawn from, as path#symbol, so you can check it against the code. Paths are relative to the repository root.
- Overview (this page): the pieces of the system and where each one runs.
- Package map: the five package entries, and a module graph generated from the package's imports.
- Data model: the grid, slots, segments, coverage, versions and watermarks inside one cache.
- Code tour: the source read in the order a request travels, from the entries to the Vite plugin, with the demo-hub scenario that shows each part at work.
The flows, each a sequence diagram with a walkthrough and the code behind it:
- Startup and fallback: the hosting chain and the handshake.
- A read, end to end:
get()from a tab to the worker and back, with in-flight subtraction and coalescing. - Sharing across tabs: the first tab starts the SharedWorker, later tabs join, a tab closes, the worker dies.
- Fences and invalidation: the clear generation and the invalidate fence.
- Auth refresh: a refused token, the
authInvalidbroadcast,updateAuth, and the next read. - Persistence: restore at
cache(), write-back, and a quota stop. - Live feeds: the reference clients' elected feed tab, which puts bars into the shared cache.
The system
Every tab runs its own client. The client reaches one engine through a MessagePort: in a SharedWorker, which every tab that loads the same worker script shares, or in a dedicated worker or the page itself when a SharedWorker cannot start. Next to the engine, the orchestrator turns a read's misses into calls to your fetcher module, which is the only part of tscache that talks to your backend. In the SharedWorker, the persistence layer keeps the caches that ask for it in IndexedDB.
| Piece | Runs in | What it does |
|---|---|---|
| Client | each page | createClient tries the hostings in turn and wraps the connected port; client.cache(config) returns a CacheHandle, whose data methods are each one request |
PortClient | each page | the page's end of the port: completes the handshake, matches each response to its request by id, rebuilds errors and passes events on |
RpcServer | the hosting | one per engine, any number of ports: greets each port with a hello naming its client id, expects an init back, dispatches requests, and sends events to every ready port |
Orchestrator | the hosting | serves get and invalidate: plans the fetches a read needs, minus those already in flight, calls the fetcher, applies each response as a put, then reads once |
Engine | the hosting | the caches, by id; each CacheState holds its segments, coverage index, dataset version and watermark |
| Persistence | the hosting | in the SharedWorker, loads the persister on demand and keeps caches with persist: true in IndexedDB; in the other hostings, only empties the database at clearAll |
| Fetcher module | the hosting | your module: its default export's fetch answers the orchestrator's requests; loaded once, with import(), during the handshake |
Drawn from:
packages/tscache/src/client/client.ts#createClientpackages/tscache/src/client/cache.ts#CacheHandlepackages/tscache/src/rpc/port-client.ts#PortClientpackages/tscache/src/rpc/server.ts#RpcServerpackages/tscache/src/orchestrator/orchestrator.ts#Orchestratorpackages/tscache/src/engine/engine.ts#Enginepackages/tscache/src/engine/cache.ts#CacheStatepackages/tscache/src/segment/dense.ts#DenseSegmentpackages/tscache/src/coverage.ts#CoverageIndexpackages/tscache/src/persist/hosted.ts#HostedPersistencepackages/tscache/src/persist/clear-only.ts#ClearOnlyPersistencepackages/tscache/src/persist/persister.ts#Persisterpackages/tscache/src/persist/idb-store.ts#openIdbStorepackages/tscache/src/types.ts#Fetcher
The three hostings
createClient tries the hostings in a fixed order, shared, then dedicated, then in-process, starting from options.mode (default shared). A hosting that cannot start throws a HostingError: the client steps down to the next one and reports the step as a modeFallback event. Any other error, such as a protocol mismatch, rejects createClient. client.mode tells which hosting won.
| Hosting | Where the engine runs | Persistence |
|---|---|---|
shared | a SharedWorker, created with type: 'module', extendedLifetime: true and its generation key as name; one engine serves every tab that connects | caches with persist: true are kept in IndexedDB |
dedicated | a dedicated Worker per client; the worker's global scope is the port | none; where IndexedDB exists, clearAll empties the database |
in-process | the page itself, over a MessageChannel; its code is a chunk the page loads with import() only when the chain reaches it | none; where IndexedDB exists, clearAll empties the database |
All three run the same server: each builds an RpcServer around a new Engine, and the server builds its own Orchestrator. The fetcher module is therefore loaded wherever the engine runs, in the page for the in-process hosting.
Where the browser has Web Locks (secure contexts only), the SharedWorker holds a lock for as long as it runs and names it in its hello (hello.lock). A shared-mode client queues on that lock; it is granted the lock only once the worker is gone. A dedicated worker's client learns of its worker's failure from the worker's error event instead. Either way the client then rejects everything pending and emits workerLost; only the detection differs.
The client queues on the lock as soon as hello names it, before the handshake ends. A grant that arrives before init-ok, for example because the worker died while it loaded the fetcher module, fails the shared hosting with a HostingError, and the client steps down the chain instead of emitting workerLost. The handshake timeout (handshakeTimeoutMs) bounds only the wait for hello.
Without Web Locks, the SharedWorker names no lock and the client watches none. A worker that dies with the tab hosting it (a renderer crash) is then noticed only if the browser fires a close event on the port; where it does not, pending requests stay pending and no workerLost is emitted.
Drawn from:
packages/tscache/src/client/client.ts#CHAINpackages/tscache/src/client/hosting.ts#HostingErrorpackages/tscache/src/client/hosting.ts#openSharedpackages/tscache/src/client/hosting.ts#openDedicatedpackages/tscache/src/client/hosting.ts#openInProcesspackages/tscache/src/client/in-process.ts#connectInProcesspackages/tscache/src/entries/worker.ts#createServerpackages/tscache/src/rpc/lifetime.ts#holdLifetimeLockpackages/tscache/src/rpc/lifetime.ts#watchLifetimeLockpackages/tscache/src/rpc/lifetime.ts#lockManagerpackages/tscache/src/rpc/port-client.ts#PortClient
One read, briefly
A read, end to end follows these steps in a sequence diagram.
cache.get(range)sends agetrequest through thePortClient.- The
RpcServerhands it toOrchestrator.get, with a request id of the formclientId:seq. - Without a fetcher, or with
cacheOnly: true, the orchestrator answers with oneEngine.get. Otherwise it finds the read's misses, subtracts the fetches already in flight for that cache (it waits for those instead), merges adjacent misses, extends each by one slot into the coverage next to it, and calls the fetcher once per merged range. While auth is invalid, or while the fetcher'supdateAuthhook is swapping credentials, it fetches and joins nothing: those ranges come back as'auth-pending'at once. - Each response is applied as a put over the range it was asked for. That makes the part of the range below the cache's watermark, and below the response's own
meta.finalizedUntilwhen it reports one, authoritative (see the watermark). A response that lands after the cache was cleared is dropped instead. The orchestrator waits for the fetches until they settle or auth goes invalid, whichever comes first; fetches still running when auth goes invalid finish in the background. - If the cache was cleared while the read waited, the orchestrator runs one more round: it asks again for what is still missing, minus the ranges whose fetch failed or came back auth-pending. There is no third round.
- It then reads the engine once, and labels the misses left by a failed fetch, or by auth going invalid, as
'fetch-failed'or'auth-pending'. - The result travels back to the page with its typed arrays transferred, not copied.
Drawn from:
packages/tscache/src/client/cache.ts#CacheHandlepackages/tscache/src/rpc/server.ts#RpcServerpackages/tscache/src/orchestrator/orchestrator.ts#Orchestratorpackages/tscache/src/orchestrator/orchestrator.ts#planRoundpackages/tscache/src/orchestrator/orchestrator.ts#coalescepackages/tscache/src/orchestrator/orchestrator.ts#blockedBypackages/tscache/src/engine/cache.ts#CacheStatepackages/tscache/src/orchestrator/orchestrator.ts#annotatepackages/tscache/src/rpc/protocol.ts#transferablesOf