SDK host
createSdkHost() (from @toets/protocol/host, Node only) is the host side of the protocol: a loopback WebSocket server that issues launch tokens and turns each app connection into an SdkSession. The toets core owns one per run; drivers only put its URL and a token into the launch payload.
import { createSdkHost } from '@toets/protocol/host';
const host = await createSdkHost();
const token = host.issueToken();
await driver.launch({ payload: { v: 1, sdkUrl: host.url, token } });
const session = await host.waitForSession(token, { timeoutMs: 15_000 });
if (session.has('tree.get')) {
const { root } = await session.request('tree.get', {});
}Behavior
- Binding: loopback only by default,
127.0.0.1and::1on the same port, solocalhostworks whichever family the device resolves.urlis alwaysws://localhost:<port>. Plain HTTP requests get 426. - Tokens: one per launch, 128 bit, expire after 10 minutes unused. One session at a time: after the app closes its session (a reload), the same launch may connect again with the token.
session.close()orrevokeToken(token)ends the launch: the token is refused from then on, andrevokeTokenalso rejects pending waits withsdk.unknown-token. waitForSession: resolves with the launch's open session, or on the next handshake (also after a reload). On timeout it fails withsdk.session-timeoutand explains rejections seen meanwhile (wrong token, protocol mismatch), so "the app never connected" and "the app was refused" read differently.session.has(method): true when the SDK announced the method's domain and its protocol minor is recent enough. Check it before relying on an SDK feature; fall back to black-box behavior otherwise.- Requests: time out after 10 s by default;
sync.waitForIdlegets its owntimeoutMsplus 2 s. - Notifications: buffered (500 per method) until the first listener subscribes, so startup logs are not lost.
- Heartbeat: pings every 5 s; two missed pongs close the session (code 4003).
close(): rejects pending waits and requests withsdk.host-closedand closes sessions with 1001.
Options (SdkHostOptions): port, host, handshakeTimeoutMs, requestTimeoutMs, heartbeatIntervalMs, tokenTtlMs, maxPayloadBytes, onEvent (connection, rejection and session events for logs and failure reports).
Errors
Every failure is a ProtocolError with code, hint and docs (same shape as the core's ToetsError): sdk.listen-failed, sdk.unknown-token, sdk.session-timeout, sdk.host-closed, sdk.closed, sdk.request-timeout, sdk.method-unsupported, sdk.remote-error (rpcCode and data carry the SDK's JSON-RPC error).
TV trap
A reload of the app (Metro r, location.reload(), a JS restart after a language change) ends its session and starts a new JS context that presents the same token. The host accepts it as the same launch, and the core waits for it: reads during the reload wait up to timeouts.sdkConnectMs for the new session. Two JS contexts at once (a second WebView with the same payload) are refused (Token in use by an open session).