0.1.0 • Published 10 years ago

net-buffer v0.1.0

Weekly downloads
198
License
ISC
Repository
github
Last release
10 years ago

net-buffer

Very fast message length-prefixing encoder and decoder.

.encode(Buffer)

encode can accept Buffer or undefined

var net= require('net')
var netBuf= require('net-buffer')

var client= net.connect({port: 8181}, function()
{
    client.write( netBuf.encode(undefined) ) // or just .encode()
    client.write( netBuf.encode( new Buffer('Hello World!') ) )
})

.decode(socket, callback, bodyOnly)

decode directly reads from the socekt without using the socket.on('data', ) event.

var net= require('net')
var decode= require('net-buffer').decode

net.createServer(function(socket)
{
	socket.on('readable', decode(socket, function(buffer){
	    console.log(buffer)
	}, true))
}).listen(8181)
  • socket Socket
  • callback Function, called with a single Buffer argument
  • bodyOnly Boolean, Optional, Default true; Set to false to get body prefixed with length (can reuse Buffer when need to proxy the Buffer)

.setMaxMessageLength(number)

Defines the maximum supported message length (header), valid values are 2 (for 65,135 bytes) and 4 (for 4,294,967,196 bytes), default is 2.

var netBuf= require('net-buffer')
netBuf.setMaxMessageLength(4)

Benchmark

Below is Mac Air 2013 results

 $ node test.js 100000
 $ > ok sent and received 100000 messages in 0.786218543 seconds

Installation

npm install net-buffer

MIT License

0.1.0

10 years ago

0.0.9

10 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago