Class Pbf

Protobuffer Reader and Writer

Create a new PBF instance and either read or write 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);
const pbf = new Pbf();
pbf.writeVarintField(1, 1);
// ...
const result = pbf.commit();

Hierarchy (View Summary)

Constructors

  • Parameters

    • buf: ArrayBuffer | Uint8Array = ...

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

    Returns Pbf

Methods

  • Returns Uint8Array

    • the entire written buffer
  • 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 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
  • Allocate more space in the buffer

    Parameters

    • min: number

      the minimum number of bytes to allocate

    Returns void

  • Skip a value we are not interested in parsing

    Parameters

    • val: number

      the type to skip

    Returns void

  • Write a boolean value. Can also be a number, in which case it will be converted to a boolean. 0 is false, anything else is true.

    Parameters

    • val: number | boolean

      the boolean to write.

    Returns void

  • Write a packed repeated boolean array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number | boolean

      the boolean to write.

    Returns void

  • Write a byte array

    Parameters

    • inBuf: ArrayBuffer | Uint8Array | Buffer

      a Buffer to write. Will write the length of the buffer first. After that, the buffer will be written byte by byte.

    Returns void

  • Write a packed repeated byte array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • buffer: ArrayBuffer | Uint8Array | Buffer

      the buffer of bytes to write.

    Returns void

  • Write a 64-bit floating point number

    Parameters

    • val: number

      a 64-bit floating point number to write

    Returns void

  • Write a packed repeated double array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the double to write.

    Returns void

  • Write a 32-bit unsigned integer

    Parameters

    • val: number

      the 32-bit unsigned integer to write

    Returns void

  • write a packed repeated fixed 32-bit integer array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the unsigned 32-bit integer to write.

    Returns void

  • Write a 64-bit unsigned integer

    Parameters

    • val: number

      the 64-bit unsigned integer to write

    Returns void

  • Write a packed repeated unsigned 64-bit integer array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the unsigned 64-bit integer to write.

    Returns void

  • Write a 32-bit floating point number

    Parameters

    • val: number

      a 32-bit floating point number to write

    Returns void

  • Write a packed repeated float array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the float to write.

    Returns void

  • Write a message to the buffer. Allows you to pass in an object with a write function to define how the message should be written. A good tool to abstract away storing classes or sub-classes.

    Type Parameters

    • T

    Parameters

    • tag: number

      the tag to write to associate with the message. This will help track how to read following data.

    • fn: (obj: T, pbf: Pbf) => void

      user defined function to call to manually define how to write the object

    • obj: T

      the object to pass to the user defined function

    Returns void

  • Write a packed repeated boolean array to the buffer. Supports numbers: 0 is false, everything else is true.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: (number | boolean)[]

      the array of booleans to write.

    Returns void

  • Write a packed repeated 64-bit double array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of doubles to write.

    Returns void

  • Write a packed repeated 32-bit unsigned integer array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of unsigned 32-bit numbers to write.

    Returns void

  • Write a packed repeated 64-bit unsigned integer array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of unsigned 64-bit numbers to write.

    Returns void

  • Write a packed repeated 32-bit float array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of floats to write.

    Returns void

  • Write a packed repeated 32-bit signed integer array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of signed 32-bit numbers to write.

    Returns void

  • Write a packed repeated 64-bit signed integer array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of signed 64-bit numbers to write.

    Returns void

  • Write a packed repeated signed whole number array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of signed whole numbers to write.

    Returns void

  • Write a packed repeated unsigned whole number array to the buffer.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • arr: number[]

      the array of unsigned whole numbers to write.

    Returns void

  • Write a message to the buffer. Allows you to pass in an object with a write function to define how the message should be written. A good tool to abstract away storing classes or sub-classes.

    Type Parameters

    • T

    Parameters

    • fn: (obj: T, pbf: Pbf) => void

      the user defined function to call to write the message

    • obj: T

      the object to pass to the user defined function

    Returns void

  • Write a 32-bit signed integer

    Parameters

    • val: number

      the 32-bit signed integer to write

    Returns void

  • Write a packed repeated signed 32-bit integer array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the signed 32-bit integer to write.

    Returns void

  • Write a 64-bit signed integer

    Parameters

    • val: number

      the 64-bit signed integer to write

    Returns void

  • Write a packed repeated signed 64-bit integer array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the signed 64-bit integer to write.

    Returns void

  • Write a string. Strings larger then 128 bytes will be written in chunks of 128 bytes and are slightly less efficient.

    Parameters

    • str: string

      the string to write

    Returns void

  • Write a packed repeated string array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • str: string

      the string to write.

    Returns void

  • Write a signed varint. Can be max 64-bits. Numbers can be negative but must still be a while number.

    Parameters

    • val: number

      any whole signed number. It's usually best practice to not use this function directly unless you know what you're doing.

    Returns void

  • Write a packed repeated signed integer array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the signed number to write.

    Returns void

  • Write a tag and its associated type

    Parameters

    • tag: number

      the tag to write

    • type: number

      the type to write (will never be greater than 3 bits)

    Returns void

  • Write a varint. Can be max 64-bits. Numbers are coerced to an unsigned while number before using this function.

    Parameters

    • val: number

      any whole unsigned number. It's usually best practice to not use this function directly unless you know what you're doing.

    Returns void

  • Write a packed repeated unsigned integer array to the buffer with an associated tag.

    Parameters

    • tag: number

      the tag to write to associate with the value.

    • val: number

      the unsigned number to write.

    Returns void