Skip to content
// EFFECT DAYS 2026

ProtobufReader

Reads the protobuf wire format: tags, varints, fixed-width numbers, and length-delimited data, from a byte array.

Mental model

  • A message is a sequence of fields. Each field is a tag, made of a field number and a WireType, followed by its value.
  • The reader knows nothing about messages or schemas. The caller reads a tag, picks the value to read by its field number, and skips the fields it does not know with ProtobufReader.skip.
  • ProtobufReader.delimited reads a length prefix and returns a child reader restricted to that range. The parent advances past the range. Both readers report offsets in the original input.
  • The reader borrows its input. ProtobufReader.bytes returns an independent copy; ProtobufReader.bytesView returns a view sharing the input's memory.

Gotchas

  • Malformed input throws a ProtobufReaderError: a truncated value, a varint longer than ten bytes, a length past the end of the input, a tag with field number 0 or wire type 6 or 7, an unmatched group tag, or a string that is not valid UTF-8, unless validation is explicitly disabled.
  • As in protobuf, int32, uint32, sint32, and enum varints of up to ten bytes are accepted and truncated to 32 bits.
  • Skipping unknown groups uses a bounded stack. Callers can supply the remaining nesting budget to ProtobufReader.skip.

Example Reading a message with a nested message

import { ProtobufReader, ProtobufWriter } from "effect/protobuf"
// message Point { int32 x = 1; int32 y = 2; }
const readPoint = (r: ProtobufReader.ProtobufReader) => {
let x = 0
let y = 0
while (!r.isDone()) {
const tag = r.tag()
switch (ProtobufReader.fieldNumber(tag)) {
case 1:
x = r.int32()
break
case 2:
y = r.int32()
break
default:
r.skip(tag)
}
}
return { x, y }
}
// message Shape { string name = 1; Point origin = 2; }
const { Len, Varint } = ProtobufWriter.WireType
const w = ProtobufWriter.make()
w.tag(1, Len).string("box")
w.tag(2, Len).fork().tag(2, Varint).int32(-1).join()
const r = ProtobufReader.make(w.finish())
r.tag()
r.string() // => "box"
r.tag()
readPoint(r.delimited()) // => { x: 0, y: -1 }
r.isDone() // => true
7 exports Since v4.1.5 Source

Constants

Since v4.1.5

The wire types of the protobuf format, which a tag carries with the field number. The same as ProtobufWriter.WireType.

Signature

declare const WireType: {
readonly EndGroup: 4;
readonly I32: 5;
readonly I64: 1;
readonly Len: 2;
readonly StartGroup: 3;
readonly Varint: 0;
}

Constructors

Since v4.1.5

Creates a reader of bytes, which are not copied.

Signature

declare function make(bytes: Uint8Array): ProtobufReader

Example Creating a reader

import { ProtobufReader } from "effect/protobuf"
ProtobufReader.make(new Uint8Array([0x96, 0x01])).uint32() // => 150

Errors

The error thrown by a ProtobufReader for malformed input.

Details

offset is the position in the input where reading failed.

Signature

declare class ProtobufReaderError extends YieldableError<this> & {
readonly _tag: "ProtobufReaderError";
} & Readonly<{
readonly message: string;
readonly offset: number;
}> {
constructor(args: {
readonly message: string;
readonly offset: number;
});
}

Example Catching malformed input

import { ProtobufReader } from "effect/protobuf"
// A varint whose last byte is missing
const r = ProtobufReader.make(new Uint8Array([0x08, 0x96]))
r.tag()
try {
r.int32()
} catch (e) {
e instanceof ProtobufReader.ProtobufReaderError // => true
String((e as ProtobufReader.ProtobufReaderError).message) // => "Expected a complete value at offset 2"
}

Models

A reader of the protobuf wire format, positioned at its current offset.

Details

Every method throws a ProtobufReaderError for malformed input. 64-bit integers are read as a bigint, or as a number with ProtobufReader.int64Number and its siblings. Create one with make.

Signature

declare class ProtobufReader {
constructor(bytes: Uint8Array);
offset: number;
bool(): boolean;
bytes(): Uint8Array<ArrayBuffer>;
bytesView(): Uint8Array;
delimited(): ProtobufReader;
double(): number;
fixed32(): number;
fixed64(): bigint;
fixed64Number(): number;
float(): number;
int32(): number;
int64(): bigint;
int64Number(): number;
isDone(): boolean;
sfixed32(): number;
sfixed64(): bigint;
sfixed64Number(): number;
sint32(): number;
sint64(): bigint;
sint64Number(): number;
skip(tag: number, maxDepth: number): void;
string(options?: {
readonly validateUtf8?: boolean;
}): string;
tag(): number;
uint32(): number;
uint64(): bigint;
uint64Number(): number;
}

Example Reading a field

import { ProtobufReader } from "effect/protobuf"
// message Test { int32 a = 1; } with a = 150
const r = ProtobufReader.make(new Uint8Array([0x08, 0x96, 0x01]))
const tag = r.tag()
ProtobufReader.fieldNumber(tag) // => 1
r.int32() // => 150
r.isDone() // => true
Since v4.1.5

A protobuf wire type, from 0 through 5.

Signature

type WireType = typeof WireType[keyof typeof WireType]

Utils

The field number of a tag from ProtobufReader.tag.

Signature

declare function fieldNumber(tag: number): number
Since v4.1.5

The wire type of a tag from ProtobufReader.tag.

Signature

declare function wireType(tag: number): WireType