Skip to content

push-stream/push-chunks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

push-chunks

push-chunks allows you implement binary parsers in a few lines

example

suppose we want a stream of length delimited blocks.

var chunks = new Chunks(function header (bl) {

  //check if there isn't enough input to read the header
  if(bl.length < header_length) return

  //check if there is enough input to read the body
  if(bl.readUInt32BE(0) < bl.length) return

  //we are good! go to the body
  return true
}, function body (bl) {
  var length = bl.readUInt32BE(0)

  //if the length is zero, return undefined to signal end.
  if(!length) return

  //else slice the content and return!
  var buffer = bl.slice(4, 4+length)
  bl.consume(4)
  return buffer
}

source.pipe(chunks).pipe(sink)

api: new Chunks (header(bl) => boolean, body (bl) => *)

header and body take the buffer-list which is holding the buffers written to this stream. If header returns true, then body is called. If body returns undefined the stream is ended. If body returns anything else it's written to the streams sink.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published