Class: MessageBuffer

MessageBuffer

Creates an instance of MessageBuffer.

The buffer accumulates received data and returns true or false for when a delimiter has been recieved.

If a delimiter is recieved into the buffer, the message up to that point can be removed from the buffer and returned.


new MessageBuffer(delimiter)

Parameters:
Name Type Description
delimiter string

The delimiter to use to determine if a message is complete

Source:
Example
// We can receive whole messages or parital, so we need to buffer

{"jsonrpc": 2.0, "params": ["hello"], id: 1}\n // whole message
// or
{"jsonrpc": 2.0, "params" // partial message

Methods


emptyBuffer()

Returns the contents of this.buffer.

Particularily useful for http buffering, where delimiters might not be used.

Source:
Returns:

message

Type
string

getMessage()

Return the message from the buffer if delimiter is found, and null otherwise.

The message is everything before the delimiter.

Replace message string in buffer with empty string.

Source:
Returns:

message

Type
string | null
Example
const message = messageBuffer.getMessage()
console.log(message) // "hello"

handleData()

Source:
Returns:

MessageBuffer.getMessage()

Type
function

isFinished()

Used to determine if the buffer is empty. Buffer is considered empty if its an empty string or contains no delimiter

Source:
Returns:
Type
boolean
Example
while(!messageBuffer.isFinished()){
   // get current data and verify
}

push(data)

Accumulate the buffer with messages.

If the server isnt sending delimiters for some reason then nothing will ever come back for these requests.

Parameters:
Name Type Description
data string

Data to push into buffer

Source:
Example
messageBuffer.push("hello\n")