JsonChunkAssembler
See source codeTable of contents
Assembles chunked JSON messages back into complete objects. Handles both regular JSON messages and chunked messages created by the chunk() function. Maintains internal state to track partially received chunked messages.
class JsonChunkAssembler {}Example
const assembler = new JsonChunkAssembler()
// Handle regular JSON message
const result1 = assembler.handleMessage('{"hello": "world"}')
// Returns: { data: { hello: "world" }, stringified: '{"hello": "world"}' }
// Handle chunked message
assembler.handleMessage('1_hello') // Returns: null (partial)
const result2 = assembler.handleMessage('0_ world')
// Returns: { data: "hello world", stringified: "hello world" }Properties
state
Current assembly state - either 'idle' or tracking chunks being received
state:
| 'idle'
| {
chunksReceived: string[]
totalChunks: number
}Methods
handleMessage( )
Processes a single message, which can be either a complete JSON object or a chunk. For complete JSON objects (starting with '{'), parses immediately. For chunks (prefixed with "{number}_"), accumulates until all chunks received.
handleMessage(msg: string):
| {
data: object
stringified: string
}
| {
error: Error
}
| nullExample
const assembler = new JsonChunkAssembler()
// Complete JSON message
const result = assembler.handleMessage('{"key": "value"}')
if (result && 'data' in result) {
console.log(result.data) // { key: "value" }
}
// Chunked message sequence
assembler.handleMessage('2_hel') // null - more chunks expected
assembler.handleMessage('1_lo ') // null - more chunks expected
assembler.handleMessage('0_wor') // { data: "hello wor", stringified: "hello wor" }Parameters
| Name | Description |
|---|---|
| The message to process, either JSON or chunk format |
Returns
| {
data: object
stringified: string
}
| {
error: Error
}
| nullResult object with data/stringified on success, error object on failure, or null for incomplete chunks
\{ data: object, stringified: string \}- Successfully parsed complete message\{ error: Error \}- Parse error or invalid chunk sequencenull- Chunk received but more chunks expected
Prev
StoreSideEffectsNext
TLRemoteSyncError