Class: Writer

Writer

Wire format writer using Uint8Array if available, otherwise Array.

new Writer()

Constructs a new writer instance.

Members

Operations head.

len { number }

Current length.

states { Object | null }

Linked forked states.

tail { Object }

Operations tail

Methods

bool(value){ Writer }

Writes a boolish value as a varint.

Parameters:
value boolean

Value to write

Returns:
Writer this

bytes(value){ Writer }

Writes a sequence of bytes.

Parameters:
value Uint8Array | string

Buffer or base64 encoded string to write

Returns:
Writer this

double(value){ Writer }

Writes a double (64 bit float).

Parameters:
value number

Value to write

Returns:
Writer this

finish(){ Uint8Array }

Finishes the write operation.

Returns:
Uint8Array Finished buffer

finishInto(buf, offset){ T }

Finishes the write operation, writing into the provided buffer.
The caller must ensure that buf has enough space starting at offset
to hold Writer#len bytes.

Parameters:
buf T

Target buffer

offset number 0 optional

Offset to start writing at

Returns:
T The provided buffer

fixed32(value){ Writer }

Writes an unsigned 32 bit value as fixed 32 bits.

Parameters:
value number

Value to write

Returns:
Writer this

fixed64(value){ Writer }

Writes an unsigned 64 bit value as fixed 64 bits.

Parameters:
value Long | number | string

Value to write

Throws:
TypeError

If value is a string and no long library is present.

Returns:
Writer this

float(value){ Writer }

Writes a float (32 bit).

Parameters:
value number

Value to write

Returns:
Writer this

fork(){ Writer }

Forks this writer's state by pushing it to a stack.
Calling reset or ldelim resets the writer to the previous state.

Returns:
Writer this

int32(value){ Writer }

Writes a signed 32 bit value as a varint.

Parameters:
value number

Value to write

Returns:
Writer this

int64(value){ Writer }

Writes a signed 64 bit value as a varint.

Parameters:
value Long | number | string

Value to write

Throws:
TypeError

If value is a string and no long library is present.

Returns:
Writer this

ldelim(){ Writer }

Resets to the last state and appends the fork state's current write length as a varint followed by its operations.

Returns:
Writer this

raw(value){ Writer }

Writes raw bytes without a tag or length prefix.

Parameters:
value Uint8Array

Raw bytes

Returns:
Writer this

reset(){ Writer }

Resets this instance to the last state.

Returns:
Writer this

sfixed32(value){ Writer }

Writes a signed 32 bit value as fixed 32 bits.

Parameters:
value number

Value to write

Returns:
Writer this

sfixed64(value){ Writer }

Writes a signed 64 bit value as fixed 64 bits.

Parameters:
value Long | number | string

Value to write

Throws:
TypeError

If value is a string and no long library is present.

Returns:
Writer this

sint32(value){ Writer }

Writes a 32 bit value as a varint, zig-zag encoded.

Parameters:
value number

Value to write

Returns:
Writer this

sint64(value){ Writer }

Writes a signed 64 bit value as a varint, zig-zag encoded.

Parameters:
value Long | number | string

Value to write

Throws:
TypeError

If value is a string and no long library is present.

Returns:
Writer this

string(value){ Writer }

Writes a string.

Parameters:
value string

Value to write

Returns:
Writer this

uint32(value){ Writer }

Writes an unsigned 32 bit value as a varint.

Parameters:
value number

Value to write

Returns:
Writer this

uint64(value){ Writer }

Writes an unsigned 64 bit value as a varint.

Parameters:
value Long | number | string

Value to write

Throws:
TypeError

If value is a string and no long library is present.

Returns:
Writer this

Writer.alloc(size){ Uint8Array }

Allocates a buffer of the specified size.

Parameters:
size number

Buffer size

Returns:
Uint8Array Buffer

Writer.create(){ BufferWriter | Writer }

Creates a new writer.

Returns:
BufferWriter | Writer A BufferWriter when Buffers are supported, otherwise a Writer