v160 · webtransport · getStats() · edge cases
Lifecycle edges: connecting, failed, closed
The method steps branch on the session's state. While "connecting", the call waits. If the session "failed" — it never came up — the promise rejects with InvalidStateError, because there is no connection to describe. If it is "closed", it resolves with "the most recent stats available", which is what makes post-mortem logging possible. The first three experiments below need no server at all: they aim at a port where nothing listens.
Availability. Chrome 160 ships WebTransport.prototype.getStats() enabled by default — the Blink runtime feature WebTransportStats is status: "stable" in runtime_enabled_features.json5. On an older Chrome the method is absent: enable chrome://flags/#enable-experimental-web-platform-features and relaunch, or launch with --enable-blink-features=WebTransportStats (both verified on Chrome 153). Real numbers also need a WebTransport server you can reach: this showcase has no HTTP/3 server of its own, so the default endpoint is a public echo server. If your network blocks QUIC the attempt fails with the real error, shown verbatim — never a made-up result. Running your own server is a few lines; see the local echo server recipe.
A · Call while connecting to a dead endpoint
Construct a session to https://127.0.0.1:44443/never and call getStats() in the same task, while the state is still "connecting". The spec says the promise waits for the state to change, then rejects once it becomes "failed". Expected: it settles no earlier than ready rejects, with InvalidStateError.
Not run.
B · Call after the failure is known
Wait for ready to reject first, then call. The state is already "failed", so the very first step rejects: InvalidStateError. A probe that can fail: a build that returned empty stats here would be wrong.
Not run.
C · Call on the wrong object
WebTransport.prototype.getStats.call({}). WebIDL says a promise-returning operation with a bad this does not throw synchronously — it returns a promise rejected with TypeError. This checks the binding, not the network.
Not run.
D · Stats survive close() (needs a reachable server)
Open a real session, take a snapshot, close() it, await closed, then call getStats() twice more. The spec resolves both with the most recent stats; Chromium captures a final snapshot when the connection closes and keeps returning its values (as a fresh dictionary object on each call). This is how you log "how did that session end?" after the fact.
Not run.
| when | smoothedRtt | estimatedSendRate | datagrams |
|---|---|---|---|
| Not run. | |||
the defensive wrapper these edges imply
async function safeStats(transport) {
if (typeof transport.getStats !== "function") return { supported: false };
try {
const s = await transport.getStats(); // waits if still connecting
return { supported: true, stats: s };
} catch (e) {
if (e.name === "InvalidStateError") return { supported: true, failed: true }; // never connected
throw e;
}
}