BSER Binary Serialization

2.1.1 · active · verified Sun Apr 19

BSER (Binary Serialization) is a compact, framed binary serialization scheme designed as an alternative to JSON, primarily for local Inter-Process Communication (IPC). It is part of the larger Watchman project by Facebook (Meta Platforms, Inc.), which implies its development is active and closely tied to Watchman's needs. While the npm package version is 2.1.1, the upstream Watchman project sees rapid, often weekly, releases in a `vYYYY.MM.DD.00` format, indicating continuous development and updates. BSER differentiates itself with framed encoding for streaming sequences of values, treating strings as binary without specific character encoding (matching OS filename conventions), making it efficient for specific low-level IPC scenarios. It offers both synchronous (`loadFromBuffer`, `dumpToBuffer`) and asynchronous (`BunserBuf` for event-driven decoding) APIs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic synchronous BSER encoding (`dumpToBuffer`) and asynchronous decoding (`BunserBuf`) for IPC over a Unix domain socket, including event handling for streamed data.

const net = require('net');
const bser = require('bser');

// Simulate a socket server
const server = net.createServer(socket => {
  console.log('Client connected.');

  const bunser = new bser.BunserBuf();

  bunser.on('value', obj => {
    console.log('Server received (decoded):', obj);
    // Echo back a response
    const response = bser.dumpToBuffer(['pong', obj[1]]);
    socket.write(response);
  });

  socket.on('data', buf => {
    bunser.append(buf);
  });

  socket.on('end', () => {
    console.log('Client disconnected.');
  });

  socket.on('error', err => {
    console.error('Server socket error:', err);
  });
});

const SOCKET_PATH = '/tmp/bser-socket';
server.listen(SOCKET_PATH, () => {
  console.log(`Server listening on ${SOCKET_PATH}`);

  // Simulate a client
  const client = net.connect(SOCKET_PATH, () => {
    console.log('Client connected to server.');
    const payload = ['hello', 'world', 123];
    const encoded = bser.dumpToBuffer(payload);
    console.log('Client sending (encoded):', encoded);
    client.write(encoded);

    const clientBunser = new bser.BunserBuf();
    clientBunser.on('value', obj => {
      console.log('Client received (decoded):', obj);
      client.end(); // End client after receiving response
    });
    client.on('data', buf => {
      clientBunser.append(buf);
    });

    client.on('error', err => {
      console.error('Client socket error:', err);
    });
  });

  client.on('end', () => {
    console.log('Client disconnected. Server closing.');
    server.close();
  });
});

view raw JSON →