ProtobufWriter
Writes the protobuf wire format: tags, varints, fixed-width numbers, and length-delimited data, into one growing buffer.
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 writer knows nothing about messages or schemas. The caller writes each tag and value in the order it wants them on the wire.
- A nested message, string, or bytes field is length-delimited. Nested messages are written in place between ProtobufWriter.fork and ProtobufWriter.join, which fills in the length prefix afterwards.
Gotchas
- Values are not validated. As with
DataView, numbers are truncated to integers, and integers outside the range of their type wrap around. Tags are checked and invalid field numbers or wire types throwRangeError. - Unpaired UTF-16 surrogates in strings are written as U+FFFD, as with
TextEncoder.
Example Writing a message with a nested message
import { ProtobufWriter } from "effect/protobuf"
// message Point { int32 x = 1; int32 y = 2; }// message Shape { string name = 1; Point origin = 2; }const { Len, Varint } = ProtobufWriter.WireTypeconst w = ProtobufWriter.make()w.tag(1, Len).string("box")w.tag(2, Len).fork()w.tag(1, Varint).int32(3)w.tag(2, Varint).int32(-1)w.join()
w.finish().length // => 20Constants
The wire types of the protobuf format, which a tag carries with the field number.
Details
Varint:int32,int64,uint32,uint64,sint32,sint64,bool, and enums.I64:fixed64,sfixed64, anddouble.Len: strings, bytes, nested messages, and packed repeated fields.StartGroupandEndGroup: the deprecated proto2 groups.I32:fixed32,sfixed32, andfloat.
Signature
declare const WireType: { readonly EndGroup: 4; readonly I32: 5; readonly I64: 1; readonly Len: 2; readonly StartGroup: 3; readonly Varint: 0;}Constructors
Models
Since v4.1.5 Source
A writer of the protobuf wire format.
Details
Every write returns the writer, so writes chain: w.tag(1, WireType.Len).string("a").
64-bit integers are given as a bigint, a number that is an integer, or a
decimal string. ProtobufWriter.finish returns the bytes and resets
the writer, which can then write a new message. Create one with make.
Signature
declare class ProtobufWriter { constructor(); bool(value: boolean): this; bytes(bytes: Uint8Array): this; double(n: number): this; finish(): Uint8Array<ArrayBuffer>; fixed32(n: number): this; fixed64(n: string | number | bigint): this; float(n: number): this; fork(): this; int32(n: number): this; int64(n: string | number | bigint): this; join(): this; raw(bytes: Uint8Array): this; sfixed32(n: number): this; sfixed64(n: string | number | bigint): this; sint32(n: number): this; sint64(n: string | number | bigint): this; string(s: string): this; tag(fieldNumber: number, wireType: WireType): this; uint32(n: number): this; uint64(n: string | number | bigint): this;}Example Writing a field
import { ProtobufWriter } from "effect/protobuf"
// message Test { int32 a = 1; } with a = 150ProtobufWriter.make() .tag(1, ProtobufWriter.WireType.Varint) .int32(150) .finish() // => new Uint8Array([0x08, 0x96, 0x01])A protobuf wire type, from 0 through 5.
Signature
type WireType = typeof WireType[keyof typeof WireType]