Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HazelWriter

Hierarchy

Index

Methods

Static alloc

  • Allocate a message writer with a buffer of the specified number of bytes.

    example
    const writer = HazelWriter.alloc(6);
    
    console.log(writer.size); // => 6
    

    Parameters

    • bytes: number

      The number of bytes to allocate.

    Returns HazelWriter

    The message writer writing to the allocated bytes.

[iterator]

  • [iterator](): Generator<number, void, unknown>

begin

  • Begin writing a new length & tag message.

    Parameters

    • tag: number

      The tag of the message.

    Returns HazelWriter

    The writer.

    const writer = HazelWriter.alloc(4);
    writer.begin(5);
    writer.uint8(0x45);
    writer.end();
    

bool

  • Write a true or false value.

    example
    const writer = HazelWriter.alloc(4);
    
    writer.bool(true);
    writer.bool(false);
    

    Parameters

    • val: boolean

      The value to write.

    Returns HazelWriter

    The writer.

byte

  • Write a single unsigned byte.

    example
    const writer = HazelWriter.alloc(1);
    
    writer.byte(69);
    

    Parameters

    • val: number

      The value to write. (Between 0 and 255 inclusive.)

    Returns HazelWriter

    The writer.

bytes

  • Write non-length-prefixed bytes.

    Parameters

    • bytes: string | HazelBuffer | Buffer | number[]

      The bytes to write to the buffer.

    Returns HazelWriter

    The writer.

    const writer = HazelWriter.alloc(5);
    writer.bytes("Hello");
    

char

  • char(val: string): void
  • Write a single utf8 char.

    example
    const writer = HazelWriter.alloc(4);
    
    writer.char("A");
    writer.char("6");
    

    Parameters

    • val: string

      The value to write.

    Returns void

    The writer.

clone

  • Clone the message writer to a new writer with a separate buffer.

    example
    const writer = HazelWriter.alloc(2);
    writer.uint8(32);
    writer.uint8(12);
    
    const cloned = writer.clone();
    cloned.uint8(90);
    
    console.log(writer); // => <HazelBuffer 20 0c [  ]>
    console.log(cloned); // => <HazelBuffer 20 0c 5a [  ]>
    

    Returns HazelWriter

    The new message writer.

compare

  • Check whether two hazel buffers contain the same information.

    example
    const writer = HazelWriter.alloc(2);
    writer.uint8(21);
    writer.uint8(69);
    
    const writer2 = HazelWriter.alloc(2);
    writer.uint8(21);
    writer.uint8(69);
    
    console.log(writer.compare(writer2)); // => true
    
    writer2.uint8(90);
    
    console.log(writer.compare(writer2)); // => false
    

    Parameters

    Returns boolean

    Whether or not the hazel writers are the same.

end

  • End writing an opened message.

    Returns HazelWriter

    The writer.

    const writer = HazelWriter.alloc(4);
    writer.begin(5);
    writer.uint8(0x45);
    writer.end();
    

expand

  • Expand the writer by the number of bytes required to write a value. Won't reallocate if there are enough bytes remaining.

    example
    const writer = HazelWriter.alloc(6);
    writer.expand(2); // The cursor is at 0, since there is 2 bytes remaining, the size remains at 6.
    
    console.log(writer.size); // => 6
    
    writer.expand(8); // There is not 8 bytes remaining so the writer buffer is reallocated to 8.
    console.log(writer.size); // => 8
    

    Parameters

    • required: number

      The number of bytes required to write a value.

    Returns HazelWriter

    The writer.

float

  • Write an IEEE 754 floating point number.

    example
    const writer = HazelWriter.alloc(8);
    
    writer.float(54.32);
    writer.float(21.69420);
    

    Parameters

    • val: number

      The value to write.

    • be: boolean = false

    Returns HazelWriter

    The writer.

goto

  • Move the cursor to a position.

    example
    const writer = HazelWriter.alloc(12);
    writer.goto(5);
    
    console.log(writer.cursor); // => 5
    

    Parameters

    • pos: number

      The position to move to.

    Returns HazelWriter

    The writer.

int16

  • Write a signed 16-bit integer value.

    example
    const writer = HazelWriter.alloc(4);
    
    writer.int16(-3452);
    writer.int16(1933);
    

    Parameters

    • val: number

      The value to write. (Between -32767 and 32767 inclusive.)

    • be: boolean = false

    Returns HazelWriter

    The writer.

int32

  • Write a signed 32-bit integer value.

    example
    const writer = HazelWriter.alloc(8);
    
    writer.int32(-432423);
    writer.int32(1212112);
    

    Parameters

    • val: number

      The value to write. (Between -2147483647 and 2147483647 inclusive.)

    • be: boolean = false

    Returns HazelWriter

    The writer.

int64

  • Write a signed 64-bit integer value.

    example
    const writer = HazelWriter.alloc(8);
    
    writer.int64(-432423);
    

    Parameters

    • val: bigint

      The value to write. (Between -2147483647 and 2147483647 inclusive.)

    • be: boolean = false

    Returns HazelWriter

    The writer.

int8

  • Write a signed 8-bit integer value.

    example
    const writer = HazelWriter.alloc(4);
    
    writer.int8(-120);
    writer.int8(68);
    

    Parameters

    • val: number

      The value to write. (Between -127 and 127 inclusive.)

    Returns HazelWriter

    The writer.

jump

  • Skip a speciied number of bytes.

    example
    const writer = HazelWriter.alloc(12);
    writer.skip(3);
    writer.skip(2);
    writer.skip(5);
    
    console.log(writer.cursor); // => 10
    

    Parameters

    • bytes: number

    Returns HazelWriter

    The writer.

list

  • list<T>(length: boolean, arr: T[], fn?: ListWriter<T>): HazelWriter
  • Write an object list from the buffer.

    example
    const nums = [5, 6, 7, 8];
    const reader = HazelReader.alloc(0);
    
    const items = reader.list(nums, (item, writer) => writer.uint8(item));
    
    console.log(items); // => [5, 6, 7, 8];
    

    Type parameters

    • T

    Parameters

    • length: boolean

      The length of the list.

    • arr: T[]
    • Optional fn: ListWriter<T>

      The function accepting a single reader to use for reading data.

    Returns HazelWriter

    The writer.

lwrite

  • lwrite<K>(length: boolean, serializable: K[], ...args: GetSerializeArgs<K>): HazelWriter
  • Write a list of serializable objects to the writer.

    example
    const writer = HazelWriter.alloc(0);
    
    writer.write(players.map(player => player.control));
    

    Type parameters

    • K: Serializable<any[]>

    Parameters

    • length: boolean
    • serializable: K[]

      The objects to write.

    • Rest ...args: GetSerializeArgs<K>

    Returns HazelWriter

    The writer.

packed

  • packed(val: number): void
  • Write a signed variable-sized integer.

    example
    const writer = HazelWriter.alloc(0);
    
    writer.packed(-420);
    writer.packed(153);
    

    Parameters

    • val: number

      The value to write.

    Returns void

    The writer.

realloc

  • Reallocate the the number of bytes in the writer.

    example
    const writer = HazelWriter.alloc(4);
    writer.realloc(8);
    
    console.log(writer.size); // => 8
    

    Parameters

    • size: number

      The size to reallocate to.

    Returns HazelWriter

    The writer.

sbyte

  • Write a single signed byte.

    example
    const writer = HazelWriter.alloc(1);
    
    writer.byte(-32);
    

    Parameters

    • val: number

      The value to write. (Between -127 and 127 inclusive.)

    Returns HazelWriter

    The writer.

string

  • string(val: string): void
  • Write a packed int length-prefixed string.

    example
    const writer = HazelWriter.alloc(9);
    
    writer.string("poopy");
    writer.string("poop");
    

    Parameters

    • val: string

      The string to write.

    Returns void

    The writer.

toString

  • toString(encoding?: BufferEncoding): string
  • Parameters

    • encoding: BufferEncoding = "hex"

    Returns string

uint16

  • Write an unsigned 16-bit integer value.

    example
    const writer = HazelWriter.alloc(4);
    
    writer.uint16(5342);
    writer.uint16(256);
    

    Parameters

    • val: number

      The value to write. (Between 0 and 65535 inclusive.)

    • be: boolean = false

    Returns HazelWriter

    The writer.

uint32

  • Write an unsigned 32-bit integer value.

    example
    const writer = HazelWriter.alloc(8);
    
    writer.uint32(6764774);
    writer.uint32(12314352);
    

    Parameters

    • val: number

      The value to write. (Between 0 and 4294967295 inclusive.)

    • be: boolean = false

    Returns HazelWriter

    The writer.

uint64

  • Write an unsigned 64-bit integer value.

    example
    const writer = HazelWriter.alloc(8);
    
    writer.uint64(6764774);
    

    Parameters

    • val: bigint

      The value to write. (Between 0 and 18446744073709552000 inclusive.)

    • be: boolean = false

    Returns HazelWriter

    The writer.

uint8

  • Write an unsigned 8-bit integer value.

    example
    const writer = HazelWriter.alloc(1);
    
    writer.uint8(54);
    

    Parameters

    • val: number

      The value to write. (Between 0 and 255 inclusive.)

    Returns HazelWriter

    The writer.

upacked

  • upacked(val: number): void
  • Write an unsigned variable-size integer.

    example
    const writer = HazelWriter.alloc(0);
    
    writer.packed(54325);
    writer.packed(11293);
    

    Parameters

    • val: number

      The value to write.

    Returns void

    The writer.

vector

write

  • write<K>(serializable: K, ...args: GetSerializeArgs<K>): HazelWriter
  • Write a serializable object to the writer.

    example
    const writer = HazelWriter.alloc(0);
    
    writer.write(player.control);
    

    Type parameters

    • K: Serializable<any[]>

    Parameters

    • serializable: K

      The object to write.

    • Rest ...args: GetSerializeArgs<K>

    Returns HazelWriter

    The writer.

Accessors

buffer

  • get buffer(): Buffer
  • The buffer that the writer or reader is targeting.

    example
    const writer = HazelWriter.alloc(6);
    
    console.log(writer; // => <HazelBuffer [00] 00 00 00 00 00>
    

    Returns Buffer

cursor

  • get cursor(): number
  • The current position of the writer or reader.

    example
    const writer = HazelWriter.alloc(2);
    writer.uint16(4);
    
    console.log(writer.cursor); // => 2
    

    Returns number

left

  • get left(): number
  • The number of bytes left in the writer or reader.

    example
    const writer = HazelWriter.alloc(6);
    writer.uint16(4);
    
    console.log(writer.left); // => 4
    

    Returns number

size

  • get size(): number
  • The size of the buffer that the writer or reader is targeting.

    example
    const writer = HazelWriter.alloc(6);
    
    console.log(writer.size); // => 6
    

    Returns number

Generated using TypeDoc