Split -> Join Streams
A pair of streams and a cache to facilitate serialization and deserialization of javascript objects.
Classes
- Split
- Join
- Cache
Split - abstract interface (stream)
This class must be extended and split(obj)
method must be implemented.
The split(obj)
method must serialize the obj
into array of packets
and the result must be cached with unique ID string. Must return the ID.
Join - abstract interface (stream)
This class must be extended and join(packet)
method must be implemented.
At every execution the join(packet)
method must create an array of the
chunks which make up the original obj
and cache it with unique ID string.
If the array contains enough packets to reassemble the original obj
, the
join(packet)
method must return the ID, otherwise, it must return null
Usage (CoffeeScript)
Implement Split (provided as requireable simple.Split)
extends require 'split-join'Split : if @cacheisFull then throw 'Cache is full' chunksId = MathrandomtoString36 @cacheadd chunksIdchunks = JSONstringify objmatch /.{1,256}/g total = chunkslength for chunkindex in chunks chunksindex= " \n\n" return chunksId
Implement Join (provided as requireable simple.Join)
extends require 'split-join'Join regexp = /^\s+\s+\s*?\n\n$/m : if parsed = packettoStringmatch regexp parsed1..4 unless chunks = @cacheget id if @cacheisFull then throw 'Cache is full' @cacheadd idchunks = chunkspush chunk if parseInt index + 1 is parseInt total return id return
Sender
socket = getSocketSomehowsplit = splitpipe socket # serialize the array into packets and send them out splitwrite 'data''from''sender'
Receiver
socket = getSocketSomehowjoin = socketpipe join # the data coming from join will be the array from the sender