HomeBlogThrift Bytes: Binary, Compact and TFramed

Thrift Bytes: Binary, Compact and TFramed

Ten hand-built vectors show what Binary, Compact and TFramed reveal without IDL—and why names, enum labels and union identity stay missing.

DevKit is maintained by hcatree. I wrote the tools on this site and ran the compression benchmarks myself — the script and the raw data download from the benchmarks page.

2026-11-2710 min read

TFramed is not a third way to encode a Thrift value. It is a four-byte ruler placed around bytes that are already Binary or Compact. Confusing those layers makes a simple capture look mysterious: the frame can be perfectly valid while the protocol inside it is being read incorrectly.

The second surprise is that a message can reveal its method name while hiding every field name. In the vectors below, ping appears literally in the envelope. A field called count does not. Thrift carries field IDs, not the names attached to them in .thrift IDL.

I ran the project's parser against ten hand-built vectors: seven valid and three malformed. All ten produced the expected result. This tests the implementation, not cross-library compatibility: tmp/thrift-vectors.mjs encodes bytes from Apache's specifications. It is not an independent reference library and does not compare output from an Apache runtime.

Two grammars for the same bare struct

The harness constructs one struct with six top-level fields: i32 = 7, string "devkit", true, a three-element i32 list, a one-entry map, and a nested struct containing i16 = 5. These are the exact dumps:

BIN_BARE =
080001000000070b0002000000066465766b6974020003010f000408000000030000000100000002000000030d00050b08000000010000000161000000010c000606000100050000

CMP_BARE =
150e18066465766b69741119350204061b01850161021c140a0000

Both decodes contain 12 nodes: six fields, three list elements, two map children, and one nested field. Containers and separate map key/value nodes count too; this is not an IDL declaration count.

ID Decoded value Binary field/value bytes Compact field/value bytes
1 i32 = 7 08 0001 00000007 15 0e
2 string "devkit" 0b 0002 00000006 6465766b6974 18 06 6465766b6974
3 bool true 02 0003 01 11
4 list [1, 2, 3] 0f 0004 08 00000003 00000001 00000002 00000003 19 35 02 04 06
5 map {"a": 1} 0d 0005 0b 08 00000001 00000001 61 00000001 1b 01 85 01 61 02
6 struct {1: i16 = 5} 0c 0006 06 0001 0005 00 1c 14 0a 00

Each struct ends with Binary STOP 00. Compact uses the same one-byte zero terminator, but reaches it through a different field-header grammar.

Binary buys predictability with fixed widths

Apache's Binary specification defines a field header as a type byte followed by a two-byte field ID. Integers use most-significant-byte-first order: i16, i32, and i64 occupy two, four, and eight bytes. Thus field 1's header 08 0001 means I32, field ID 1; its value 00000007 is exactly 7. Binary doubles are eight-byte IEEE 754 values in big-endian order as specified and implemented here.

A Binary bool is a separate byte. Strings and binary blobs share type 11: signed 32-bit length, then payload. Lists and sets carry element type and count; maps carry key type, value type, and count. These headers expose container boundaries, not meaningful names.

Compact trades width for context

The Compact specification packs a four-bit type and usually a four-bit field-ID delta into one byte. 15 means “previous field ID 0 + 1, Compact type 5,” which maps to Binary I32. 0e is a varint containing ZigZag-encoded 7: 7 becomes 14, and decoding reverses that transform.

ZigZag maps signed small integers to unsigned varints efficiently; it does not make them unsigned on the wire. Compact i16, i32, and i64 use it, while bytes remain bytes and doubles are little-endian IEEE 754. Lengths are unsigned varints. Collection sizes up to 14 fit in the header's high nibble; larger sizes use nibble 15 followed by a varint count.

Compact booleans are stranger still. A field's type nibble is 1 for true or 2 for false, so the entire 11 field consumes no value byte. Boolean collection elements use bytes 1 or 2. Deltas outside 1–15 use a zero high nibble followed by an explicit ZigZag-varint field ID. Delta therefore depends on the previous field in that struct.

The envelope explains the visible name

These are three CALL envelopes named ping, sequence ID 42. Binary and TFramed carry field 1 as i32 = 1; the Compact vector deliberately carries i32 = 7:

BIN_MSG    = 800100010000000470696e670000002a0800010000000100
CMP_MSG    = 82212a0470696e67150e00
FRAMED_MSG = 00000018800100010000000470696e670000002a0800010000000100

Binary strict begins 80 01 00 01: version 1 plus message type CALL. Then come a four-byte name length, 70696e67 (ping), a four-byte sequence ID 0000002a, the struct, and STOP. Compact begins 82 21: protocol ID 0x82, then CALL and version packed into 0x21. Its sequence ID 2a is a plain varint, not ZigZag; 04 introduces the four name bytes. Finally, 15 0e means field 1 is I32 7 in this vector—not the I32 1 used in the Binary message.

The harness also accepts Binary non-strict input:

00000004 70696e67 01 0000002a 0800010000000100

That is name length, name, message type, sequence ID, and payload, with no strict version word. Both forms produced a message node named ping and a child field node. This is why “Thrift contains no names” is too broad: envelope method names travel; field and struct names do not.

TFramed measures the payload, not its meaning

FRAMED_MSG starts 00 00 00 18. Read as a four-byte big-endian integer, 0x18 is 24: exactly the length of BIN_MSG. The remaining bytes are unchanged Binary. The parser therefore reports transport=framed, one frame, and a message starting at absolute byte offset 4 rather than 0.

Nothing in that prefix chooses Binary versus Compact, assigns field names, or validates an RPC. It only bounds the frame. The same framing concept can surround either value protocol. DevKit exposes protocol and transport as separate controls for precisely this reason.

What the ten vectors actually printed

The command was node --experimental-strip-types tmp/thrift-vectors.mjs. Every case printed PASS; warning count was zero in every row. For malformed vectors, PASS means the expected error was observed.

Case Selection Protocol / transport / frames Nodes Error
binary bare Binary binary / raw / 0 12 —
compact bare Compact compact / raw / 0 12 —
binary strict msg Auto protocol binary / raw / 0 2 —
binary loose msg Auto protocol binary / raw / 0 2 —
compact msg Auto protocol compact / raw / 0 2 —
framed binary Auto transport binary / framed / 1 2 —
compact long list Compact compact / raw / 0 21 —
truncated compact varint Compact compact / raw / 0 1 unexpected end of input
binary invalid type Binary binary / raw / 0 1 invalid type 5
map size overrun Binary binary / raw / 0 1 length 999 overruns input

The valid message rows contain an envelope node plus one field node. The framed case additionally passed the harness check that its first node starts at offset 4.

Malformed bytes are useful evidence

The three negative vectors are small enough to inspect completely:

TRUNC_VARINT = 1581
INVALID_TYPE = 05000100
MAP_OVERRUN  = 0d00010b08000003e700
LONG_LIST    = 19f514020406080a0c0e10121416181a1c1e202224262800

15 81 starts Compact field 1 as I32, but its varint never terminates before end of input. 05 0001 declares undefined Binary type 5 for field 1. In the map case, 0d 0001 0b 08 000003e7 announces 999 pairs after only one remaining byte, so the length check fails before an entry is read. The decoder retains partial nodes and reports an error offset; it does not manufacture a complete-looking result.

The 20-element Compact list takes the long form: 19 selects field 1 as LIST, f5 combines the escape size with I32 element type 5, and 14 is varint 20. Its 20 ZigZag values run from 02 through 28; the final 00 stops the struct. That is why this one field produces 21 nodes.

What stays missing without IDL

A successful parse recovers field IDs, wire types, scalar values, container shapes, byte spans, and—when present—message envelope metadata. It cannot recover field names, struct names, enum labels, or which named union member was intended.

Enums are integer values on the wire, not label strings. Unions use struct encoding and constrain which members may be set in the IDL; the bytes carry no union discriminator or union type name. Likewise, type 11 cannot distinguish an intended string from an intended binary. DevKit displays text only when the payload passes its UTF-8 and control-character checks; otherwise it exposes the bytes. That presentation decision is not recovered schema.

Choose the layer before choosing the decoder

Your question Select or inspect Do not conclude
Are integers at fixed offsets with explicit type and ID? Binary That a numeric field has a particular name
Are field deltas, varints, or header booleans present? Compact That a varint is unsigned without applying its type rules
Does a four-byte length exactly bound one or more frames? TFramed, then Binary or Compact inside That TFramed is a third value protocol
Is the input one RPC-style envelope or only a struct? Inspect envelope bytes; use Auto or pin the protocol That every struct has a method name or sequence ID
Do you need enum labels or union member identity? Obtain the .thrift IDL That plausible numeric values identify schema symbols
Must the tool execute an RPC or compile IDL? Stop: it does neither That decoding performs transport or application work

The implementation's actual limits

The code supports Binary and Compact values for bool, byte, double, i16, i32, i64, string/binary, struct, map, set, list, and UUID. It does not read .thrift files, generate bindings, open a connection, send requests, or execute RPCs.

Auto protocol starts with envelope detection, otherwise Binary. For a bare struct, it tries the other protocol only when the first attempt errors or leaves bytes unconsumed; ambiguity can therefore remain. Non-strict Binary envelope detection limits candidate method names to 1–128 bytes. Pin the protocol when you know it.

Auto framing recognizes a fitting four-byte prefix, including consecutive fitting frames. A bad prefix falls back to raw when no frames were decoded. An unusable later frame header stops the loop with a warning; a payload parse error stops it with an error. Only the first frame supplies the reported message metadata.

Nesting is capped at 32 levels and a node budget at 20,000. Varints are limited to ten bytes; lengths larger than JavaScript's safe-integer range are rejected. Negative lengths and lengths exceeding remaining input fail explicitly. These are parser guards, not claims about every Apache language implementation. Uploads are limited to 2 MB in the UI; pasted input does not have that upload check.

Inspect your own bytes

The Thrift Decoder accepts hex or Base64, separates Binary/Compact from raw/TFramed selection, and links each decoded node to its original byte range. Paste CMP_MSG and compare its tree with BIN_MSG; then wrap the Binary vector in the exact frame prefix above. The useful outcome is not a prettier field name. It is knowing which facts came from the bytes and which still require the IDL.

Thrift Binary, Compact and TFramed byte layouts

References

Environment: Node.js v24.22.0 on Windows, measured 2026-11-27. The command above exercised decodeThrift() in lib/thrift-wire.ts, the parser used by components/ThriftInspector.tsx; lib/site.ts registers the tool as thrift-decoder. The development harness tmp/thrift-vectors.mjs hand-builds ten vectors from Apache's specifications and checks parser results; it is not an independent reference implementation, and no RPC was executed.

Discuss this articleEchoForum · no sign-up, replies in any language

Related Tools

Thrift DecoderDecode Thrift Binary & Compact