# net-buffer

> send and receive buffer through network socket

Latest version **0.1.0** (published 2016-02-13) · ISC license · 0 weekly downloads

## Install

```sh
npm install net-buffer
pnpm add net-buffer
yarn add net-buffer
bun add net-buffer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2016-02-13 |
| First published | 2014-12-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | David Sky |
| Maintainers | david_sky |
| Keywords | buffer, net, netowrk, message |

## Links

- npm: https://www.npmjs.com/package/net-buffer
- Repository: https://github.com/davidSky/node-net-buffer
- Issues: https://github.com/davidSky/node-net-buffer/issues
- npm.io page: https://npm.io/package/net-buffer

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2016-02-13
- 0.0.9 — 2016-02-12
- 0.0.7 — 2015-01-07
- 0.0.6 — 2015-01-07
- 0.0.5 — 2015-01-02
- 0.0.4 — 2015-01-02
- 0.0.3 — 2015-01-01
- 0.0.2 — 2014-12-31
- 0.0.1 — 2014-12-31

## README

# net-buffer
Very fast message length-prefixing encoder and decoder.

## .encode([Buffer])
encode can accept `Buffer` or `undefined`

```js
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.
```js
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.
```js
var netBuf= require('net-buffer')
netBuf.setMaxMessageLength(4)
```

## Benchmark
Below is Mac Air 2013 results
```shell
 $ node test.js 100000
 $ > ok sent and received 100000 messages in 0.786218543 seconds
```


## Installation
```
npm install net-buffer
```
---
MIT License

---
_Source: https://npm.io/package/net-buffer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
