Skip to content

decodeClientEnvelope

decodeClientEnvelope(raw): Result<ClientEnvelope, EnvelopeError>

Defined in: packages/protocol/src/lib/envelope.ts:518

Decodes a client → engine-host frame into a typed ClientEnvelope.

Accepts a JSON string, a Uint8Array/Buffer, or an already-parsed object. Owns the two failure modes: a malformed JSON string yields an invalid_json EnvelopeError; an unknown type or any schema violation yields an invalid_envelope one whose message names the offending discriminator and the first issue. Returns a Result — it never throws — so a host message loop can branch without a try/catch.

unknown

Result<ClientEnvelope, EnvelopeError>

// engine-host message handler
const decoded = decodeClientEnvelope(rawFrame);
if (isErr(decoded)) { ws.send(encodeEnvelope(EngineErrorMessage({ error: decoded.error.message }))); return; }
switch (decoded.value.type) {
case 'chart_request': ...; break;
case 'client.history.get': ...; break;
}