Startup and fallback
createClient connects a page to an engine. It tries three hostings in a fixed order and keeps the first one whose handshake completes. This page follows that chain, then the handshake every hosting runs.
The chain
createClient first checks its options: the chain is shared, dedicated, in-process, cut to start at options.mode, and workerUrl is required unless the chain starts at in-process. It then opens each hosting in turn.
- A
HostingErrorsteps down. A hosting throws one when it cannot start: the constructor is missing (SharedWorker is not available) or throws, the worker fires anerrorevent beforeinit-ok, nohelloarrives withinhandshakeTimeoutMs, or, in shared mode, the worker's lifetime lock is granted beforeinit-ok(the worker stopped running during the handshake). Each step is recorded and later emitted as amodeFallbackevent (from,to,reason). - Any other error rejects
createClient, whichever hosting raised it: aProtocolMismatchError, or theTscacheErroraninit-errcarries back when the fetcher module fails to load. - The last hosting has nowhere to step down to. When the in-process hosting fails, for example because its chunk cannot be loaded,
createClientrejects with itsHostingError.
The modeFallback events are emitted on the next macrotask after createClient resolves, so a listener added right after await createClient(...) still receives them. client.mode names the hosting that won. Any event the worker sends while createClient is still waiting for its last answer (a superseded event, an authInvalid that another tab's refused fetch caused, a cacheCleared) is kept and delivered on that same macrotask, once, after the modeFallback events and in the order the worker sent it.
Drawn from:
packages/tscache/src/client/client.ts#createClientpackages/tscache/src/client/client.ts#CHAINpackages/tscache/src/client/client.ts#resolvepackages/tscache/src/client/client.ts#connectpackages/tscache/src/client/client.ts#Clientpackages/tscache/src/client/hosting.ts#HostingErrorpackages/tscache/src/client/hosting.ts#openSharedpackages/tscache/src/client/hosting.ts#openDedicatedpackages/tscache/src/client/hosting.ts#openInProcess
The handshake
Every hosting runs the same handshake over its port: the worker greets, the page answers, and the worker confirms once it has loaded the fetcher module. The diagram shows the SharedWorker, which adds the lifetime lock.
- The worker starts. The page creates the SharedWorker with
type: 'module',extendedLifetime: trueand aname, its generation key (the tscache version and the fetcher module URL), so a tab reaches a running worker only when both would run the same code. The worker script (./worker) listens forconnectat once, but attaches each port only after it holds its lifetime lock, so everyhellocan name the lock. Without Web Locks the lock isundefinedandhellonames none. helloand the timer. The server greets each port with its protocol version, the package version and a client id of its own (c1,c2, …). The page checks the protocol; a mismatch rejects withProtocolMismatchError. When the protocol matches, the handshake timer stops:handshakeTimeoutMsbounds only the wait forhello.- The lock watch. In shared mode the page queues on the named lock as soon as
helloarrives. A grant means the worker is gone. Beforeinit-okthat fails the hosting with aHostingError; after it, the same request goes on watching the connected client (see Sharing across tabs). initand the fetcher. The page sends its protocol and, whenoptions.fetcheris set, the fetcher's module URL and context. The server checks the protocol, then has the orchestratorimport()the module. A worker runs one fetcher module: a later tab naming the same module shares its load, and one naming another module is refused with aConfigError. A load that fails leaves the slot free, so the next tab'sinittries again.- The answer. A failed load, a module whose default export has no
fetch, or a protocol mismatch is answered withinit-err, and the server drops the port. The page rebuilds the error andcreateClientrejects with it, without falling back. Otherwise the server marks the port ready and answersinit-ok.
A worker error event at any point before init-ok, or the timeout before hello, aborts the handshake with a HostingError, as does the lock grant. The aborted side closes its end of the port, so a late hello completes nothing.
The other hostings
- Dedicated worker.
new Worker(url, { type: 'module' }). The worker script attaches its own global scope as the port when it loads, with no lifetime lock, sohellonames none. The timer and theerrorlistener work as above. On a failed handshake the page terminates the worker. - In-process. The page loads the in-process chunk with
import(), builds anEngineand anRpcServerin its own realm, and connects them over aMessageChannel. There is no handshake timer and no lock; the handshake messages are the same.
Drawn from:
packages/tscache/src/client/hosting.ts#handshakepackages/tscache/src/client/hosting.ts#openSharedpackages/tscache/src/client/hosting.ts#openDedicatedpackages/tscache/src/client/in-process.ts#connectInProcesspackages/tscache/src/rpc/port-client.ts#PortClientpackages/tscache/src/rpc/server.ts#RpcServerpackages/tscache/src/rpc/protocol.ts#Hellopackages/tscache/src/rpc/protocol.ts#InitResultpackages/tscache/src/rpc/lifetime.ts#holdLifetimeLockpackages/tscache/src/rpc/lifetime.ts#watchLifetimeLockpackages/tscache/src/entries/worker.ts#createServerpackages/tscache/src/entries/worker.ts#sharedWorkerScopepackages/tscache/src/entries/worker.ts#dedicatedWorkerScopepackages/tscache/src/orchestrator/orchestrator.ts#Orchestrator