# xfer

> A simple, general purpose, TLV-like binary protocol

Latest version **0.2.1** (published 2013-11-10) · 0 weekly downloads

## Install

```sh
npm install xfer
pnpm add xfer
yarn add xfer
bun add xfer
```

## 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.2.1 |
| Published | 2013-11-10 |
| First published | 2011-08-05 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Brian White |
| Maintainers | mscdex |
| Keywords | binary, protocol, tlv, generic, multipurpose, simple, messaging |

## Links

- npm: https://www.npmjs.com/package/xfer
- Repository: http://github.com/mscdex/xfer
- Issues: https://github.com/mscdex/xfer/issues
- npm.io page: https://npm.io/package/xfer

## Recent versions

- 0.2.1 (latest) — 2013-11-10
- 0.2.0 — 2013-10-26
- 0.1.2 — 2012-08-02
- 0.1.1 — 2012-08-01
- 0.1.0 — 2012-03-20
- 0.0.3 — 2012-03-20
- 0.0.2 — 2012-01-08
- 0.0.1 — 2011-08-05

## README

Description
===========

xfer is a module for [node.js](http://nodejs.org/) that reads and writes binary-compatible messages using a simple TLV-like protocol.


Requirements
============

* [node.js](http://nodejs.org/) -- v0.10.0 or newer


Install
=======

    npm install xfer


Example
=======
```javascript
  var net = require('net'),
      inspect = require('util').inspect,
      Xfer = require('./xfer');

  function makeDisplay(role, kind) {
    return function(arg1, arg2) {
      var type, stream;
      if (kind === true) {
        type = arg1;
        stream = arg2;
      } else {
        type = kind;
        stream = arg1;
      }
      if (!stream)
        console.log('[' + role + '] Type: ' + type + ', Data: (none)');
      else {
        var s = '';
        stream.on('data', function(d) { s += d; })
              .on('end', function() {
                console.log('[' + role + '] Type: ' + type + ', Data: ' + inspect(s));
              });
      }
    };
  }

  net.createServer(function(client) {
    this.close();
    client.xfer = new Xfer();
    client.pipe(client.xfer).pipe(client);

    client.xfer.on('*', makeDisplay('SERVER', true));

    client.xfer.send(0x01, 'Node.js rules! :-)');
    client.xfer.send(0x05);
  }).listen(8118, function() {
    var client = net.createConnection(8118);
    client.xfer = new Xfer();
    client.pipe(client.xfer).pipe(client);

    client.xfer.on(0x01, makeDisplay('CLIENT', 0x01))
               .on(0x05, makeDisplay('CLIENT', 0x05));
    client.on('connect', function() {
      client.xfer.send(0xFF, 'I am caught by the catch-all event!');
      client.end();
    });
  });

  // output:
  //
  // [CLIENT] Type: 5, Data: (none)
  // [CLIENT] Type: 1, Data: 'Node.js rules! :-)'
  // [SERVER] Type: 255, Data: 'I am caught by the catch-all event!'
```


API
===

_Xfer_ is a _Duplex_ stream

Xfer Events
-----------

Two types of events are emitted from an Xfer instance: integer (type) events and a special catch-all event ('*').

Integer (type) events are passed a Readable stream object if there was data with the message. The catch-all ('*') event is passed an additional argument (< _integer_ > type) before the stream object.


Xfer Methods
------------

 * *constructor* ([< _object_ >config]) - Creates and returns a new Xfer instance with the following valid `config` settings:

    * **highWaterMark** - _integer_ - The high water mark (in bytes) used for backpressure handling for this Xfer instance (default: 128KB)

    * **streamHWM** - _integer_ - The high water mark (in bytes) used for the data streams for inbound messages (default: `highWaterMark` value from above)

    * **skipWriteTick** - _boolean_ - This disables the use of process.nextTick() inside node core's Writable stream class after data is written to this xfer instance. This can help prevent exceeding the maximum call stack if you run into that issue. (default: false)

 * **send** (< _integer_ >type[, < _mixed_ >data]) - _boolean_ - Writes the message to the Readable stream portion of the Xfer instance. If provided, `data` can be either a Buffer or string. The return value indicates whether or not more sends should be performed.

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