Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HazelReader

Hierarchy

Index

Methods

Static from

  • Create a hazel reader from a buffer.

    example
    const buffer = Buffer.alloc(2);
    buffer.writeUInt16(53, 0);
    
    const reader = HazelReader.from(buffer);
    

    Parameters

    • buffer: Buffer

      The buffer to read from.

    Returns HazelReader

    The created reader.

  • Create a hazel reader from a string.

    example
    const reader = HazelReader.from("weakeyes", "utf8");
    

    Parameters

    • str: string

      The string to read from.

    • encoding: BufferEncoding

    Returns HazelReader

    The created reader.

  • Create a hazel reader from a number array.

    example
    const reader = HazelReader.from([5, 6, 7, 8]);
    

    Parameters

    • bytes: number[]

      The byte array to read from.

    • encoding: BufferEncoding

    Returns HazelReader

    The created reader.

[iterator]

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

bool

  • bool(): boolean
  • Read a single boolean value.

    example
    const reader = HazelReader.from("0001", "hex");
    
    console.log(reader.bool()) // => false
    console.log(reader.bool()) // => true
    

    Returns boolean

    The boolean that was read.

byte

  • byte(): number
  • Read a single unsigned byte.

    example
    const reader = HazelReader.from("41", "hex");
    
    console.log(reader.byte()) // => 65
    

    Returns number

    The byte that was read.

bytes

  • Read a specified number of bytes.

    example
    const reader = HazelReader.from("030201);
    
    const message = reader.bytes(2);
    
    console.log(message.buffer); // => <Buffer 03 02>
    

    Parameters

    • bytes: number

      The number of bytes to read.

    Returns HazelReader

char

  • char(): string
  • Read a single ascii character.

    example
    const reader = HazelReader.from("41", "hex");
    
    console.log(reader.char()) // => A
    

    Returns string

    The character that was read.

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.

float

  • float(be?: boolean): number
  • Read an IEEE 754 floating point number.

    example
    const reader = HazelReader.from([130, 6, 7, 8]);
    
    console.log(reader.float()); // => 1666
    

    Parameters

    • be: boolean = false

    Returns number

    The value that was read.

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 HazelReader

    The writer.

int16

  • int16(be?: boolean): number
  • Read a signed 16-bit integer value.

    example
    const reader = HazelReader.from([130, 6, 7, 8]);
    
    console.log(reader.int16()); // => 1666
    

    Parameters

    • be: boolean = false

    Returns number

    The value that was read.

int32

  • int32(be?: boolean): number
  • Read a signed 32-bit integer value.

    example
    const reader = HazelReader.from([130, 6, 7, 8]);
    
    console.log(reader.int32()); // => 1666
    

    Parameters

    • be: boolean = false

    Returns number

    The value that was read.

int64

  • int64(be?: boolean): bigint
  • Read a signed 64-bit integer value.

    example
    console.log(reader.int64()); // => 1666
    

    Parameters

    • be: boolean = false

    Returns bigint

    The value that was read.

int8

  • int8(): number
  • Read a signed 8-bit integer value.

    example
    const reader = HazelReader.from([130, 6, 7, 8]);
    
    console.log(reader.int8()); // => -125
    

    Returns number

    The value that was read.

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 HazelReader

    The writer.

list

  • list<T>(length: number, fn: ListReader<T>): T[]
  • list<T>(fn: ListReader<T>): T[]
  • Read an object list from the buffer.

    example
    const reader = HazelReader.from([5, 6, 7, 8]);
    
    const items = reader.list(reader => reader.uint8());
    
    console.log(items); // => [5, 6, 7, 8];
    

    Type parameters

    • T

    Parameters

    • length: number

      The length of the list.

    • fn: ListReader<T>

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

    Returns T[]

  • Type parameters

    • T

    Parameters

    • fn: ListReader<T>

    Returns T[]

lread

  • lread<K>(length: number, deserializable: K, ...args: GetDeserializeArgs<K>): GetSerializable<K>[]
  • Read a list of deserializable objects from the reader.

    Type parameters

    • K: Deserializable<any[]>

    Parameters

    • length: number
    • deserializable: K

      The object class to read.

    • Rest ...args: GetDeserializeArgs<K>

    Returns GetSerializable<K>[]

    An array of deserialized objects.

message

  • Read a hazel message.

    example
    const reader = HazelReader.from("0005010a0a0a0a0a");
    
    const [ tag, mreader ] = reader.message();
    
    console.log(tag, mreader.size); // => 1 5
    

    Returns [number, HazelReader]

    The message that was read.

packed

  • packed(): number
  • Read a signed variable-sized integer.

    example
    const reader = HazelReader.from("ac9e04", "hex");
    
    console.log(reader.packed()); // => 69420
    

    Returns number

    The value that was read.

read

  • read<K>(deserializable: K, ...args: GetDeserializeArgs<K>): GetSerializable<K>
  • Read a deserializable object from the reader.

    Type parameters

    • K: Deserializable<any[]>

    Parameters

    • deserializable: K

      The object class to read.

    • Rest ...args: GetDeserializeArgs<K>

    Returns GetSerializable<K>

    The deserialized data.

sbyte

  • sbyte(): number
  • Read a single signed byte.

    example
    const reader = HazelReader.from("41", "hex");
    
    console.log(reader.sbyte()) // => 65
    

    Returns number

    The byte that was read.

string

  • string(): string
  • Read a list of chars.

    example
    const reader = HazelReader.from("48656c6c6f2c20776f726c6421", "hex");
    
    console.log(reader.string()); // => Hello, world!
    

    Returns string

    The string that was read.

toString

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

    • encoding: BufferEncoding = "hex"

    Returns string

uint16

  • uint16(be?: boolean): number
  • Read an unsigned 16-bit integer value.

    example
    const reader = HazelReader.from([130, 6, 7, 8]);
    
    console.log(reader.uint16()); // => 1666
    

    Parameters

    • be: boolean = false

    Returns number

    The value that was read.

uint32

  • uint32(be?: boolean): number
  • Read an unsigned 32-bit integer value.

    example
    const reader = HazelReader.from([130, 6, 7, 8]);
    
    console.log(reader.uint32()); // => 1666
    

    Parameters

    • be: boolean = false

    Returns number

    The value that was read.

uint64

  • uint64(be?: boolean): bigint
  • Read an unsigned 64-bit integer value.

    example
    console.log(reader.uint64()); // => 1666
    

    Parameters

    • be: boolean = false

    Returns bigint

    The value that was read.

uint8

  • uint8(): number
  • Read an unsigned 8-bit integer value.

    example
    const reader = HazelReader.from([5, 6, 7, 8]);
    
    console.log(reader.uint8()); // => 5
    

    Returns number

    The value that was read.

upacked

  • upacked(): number
  • Read an unsigned variable-sized integer.

    example
    const reader = HazelReader.from("ac9e04", "hex");
    
    console.log(reader.upacked()); // => 69420
    

    Returns number

    The value that was read.

vector

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