Class PbfReader

Protobuffer Reader

Create a new PBF instance read to it. Follows the early Protobuf spec supporting various types of encoding including messages (which are usually representative of class objects).

const data = fs.readFileSync(path);
const pbf = new Pbf(data);
// start reading here

Hierarchy (View Summary)

Constructors

  • Parameters

    • buf: ArrayBuffer | Uint8Array = ...

      an optional Uint8Array to use for reading. otherwise defaults to an empty Uint8Array for writing

    Returns PbfReader

Methods

  • Destroys the PBF instance. You can still use the Pbf instance after calling this method. However, the buffer will be emptied.

    Returns void

  • Returns boolean

    • parses the varint byte as a boolean expression
  • NOTE: bytes is preceeded by a varint dscribing the length of the bytes. The bytes themselves are presumed to be u8s and therefore don't need to be decoded

    Returns Uint8Array

    • the decoded byte array
  • Read in a 64-bit float from the buffer. There are no compression advantages with this type of encoding.

    Returns number

    • 64-bit float
  • If you know you are reading a message, but have already read the length of the message OR you're reading fields of the top level data, then this method is the alternative. It's often used by sub-classes So that it can be instationated prior to reading the message.

    Type Parameters

    • U

    Parameters

    • readField: ReadFieldFunction<U>

      user defined input function to parse the message fields

    • input: U

      The class to mutate given field data

    • end: number

      the end position of the message in the buffer

    Returns U

    • The class we mutated will be returned

    readMessage.

    Ex.

    export class MapboxVectorLayer {
    constructor(pbf: Protobuf, end: number) {
    this.#pbf = pbf;
    pbf.readFields(this.#readLayer, this, end);
    }

    #readLayer(tag: number, layer: MapboxVectorLayer, pbf: Protobuf): void {
    if (tag === 15) layer.version = pbf.readVarint();
    else if (tag === 1) layer.name = pbf.readString();
    // ...
    }
    }
  • Read in a 32-bit unsigned integer from the buffer. There are no compression advantages with this type of encoding.

    Returns number

    • 32-bit unsigned integer
  • Read in a 64-bit unsigned integer from the buffer. There are no compression advantages with this type of encoding.

    Returns number

    • 64-bit unsigned integer
  • Read in a 32-bit float from the buffer. There are no compression advantages with this type of encoding.

    Returns number

    • 32-bit float
  • Reads a message from the buffer. Sometimes it's easier to manage sub structures so that the current method can build multiples of an entire structure/class. If you you are at the top level, or parsing the message inside a class, then

    Type Parameters

    • U

    Parameters

    • readField: ReadFieldFunction<U>

      user defined input function

    • input: U

      an instance of the class you are reading into

    Returns U

    • The class itself will be returned

    readFields.

    Ex.

    class Test {
    a: number = 0;

    static read(tag: number, test: Test, pbf: Protobuf): void {
    if (tag === 1) test.a = pbf.readVarint();
    // ...
    }
    }

    const pbf = new Pbf(data);
    const t = new Test();
    pbf3.readTag();
    pbf3.readMessage(Test.read, t);
  • Parameters

    • arr: boolean[] = []

      the array to write to

    Returns boolean[]

    • the arr input with the decoded boolean values is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    Returns number[]

    • the arr input with the decoded doubles is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    Returns number[]

    • the arr input with the decoded unsigned integers is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    Returns number[]

    • the arr input with the decoded unsigned 64-bit integers is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    Returns number[]

    • the arr input with the decoded floats is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    Returns number[]

    • the arr input with the decoded signed integers is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    Returns number[]

    • the arr input with the decoded signed 64-bit integers is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    Returns number[]

    • the arr input with the decoded numbers is also returned
  • Parameters

    • arr: number[] = []

      the array to write to

    • isSigned: boolean = false

      true if the numbers are signed

    Returns number[]

    • the arr input with the decoded numbers is also returned
  • Read in a 32-bit signed integer from the buffer. There are no compression advantages with this type of encoding.

    Returns number

    • 32-bit signed integer
  • Read in a 64-bit signed integer from the buffer. There are no compression advantages with this type of encoding.

    Returns number

    • 64-bit signed integer
  • Returns string

    • the decoded string
  • Returns number

    • the decoded number as a signed number
  • Reads a tag from the buffer, pulls out the tag and type and returns it.

    Returns Tag

    • {tag: number, type: number}
  • Parameters

    • isSigned: boolean = false

      true if the number is signed

    Returns number

    • the decoded number
  • Returns number

    • the decoded number

    readVarint for better performance

  • Skip a value we are not interested in parsing

    Parameters

    • val: number

      the type to skip

    Returns void