lib/client/signalflow/websocket_message_parser.js
function parseBinaryMessage(data, knownComputations) {
  var msg = {};

  var header = new DataView(data, 0, binaryHeaderLength);
  var version = header.getUint8(0);
  extractBinaryFields(msg, binaryHeaderFormats[version], header);

  var type = binaryMessageTypes[msg.type];
  if (type === undefined) {
    console.warn('Unknown binary message type ' + msg.type);
    return null;
  }
  msg.type = type;

  var bigNumberRequested = false;
  if (typeof knownComputations[msg.channel] !== 'undefined' && knownComputations[msg.channel].params) {
    bigNumberRequested = knownComputations[msg.channel].params.bigNumber;
  }

  var compressed = msg.flags & (1 << 0);
  var json = msg.flags & (1 << 1);

  if (compressed) {
    // Decompress the message body if necessary.
    data = new DataView(pako.ungzip(new Uint8Array(data, binaryHeaderLength)).buffer);
  } else {
    data = new DataView(data, binaryHeaderLength);
  }

  if (json) {
    var decoded = textDecoder.decode(data);
    var body = JSON.parse(decoded);
    Object.keys(body).forEach(function (k) {
      msg[k] = body[k];
    });
    return msg;
  }

  switch (msg['type']) {
    case 'data':
      return parseBinaryDataMessage(msg, data, bigNumberRequested);
    default:
      console.warn('Unsupported binary "' + msg['type'] + '" message');
      return null;
  }
}
function parseBinaryMessage(data, knownComputations) {
  var msg = {};

  var header = new DataView(data, 0, binaryHeaderLength);
  var version = header.getUint8(0);
  extractBinaryFields(msg, binaryHeaderFormats[version], header);

  var type = binaryMessageTypes[msg.type];
  if (type === undefined) {
    console.warn('Unknown binary message type ' + msg.type);
    return null;
  }
  msg.type = type;

  var bigNumberRequested = false;
  if (typeof knownComputations[msg.channel] !== 'undefined' && knownComputations[msg.channel].params) {
    bigNumberRequested = knownComputations[msg.channel].params.bigNumber;
  }

  var compressed = msg.flags & (1 << 0);
  var json = msg.flags & (1 << 1);

  if (compressed) {
    // Decompress the message body if necessary.
    data = new DataView(pako.ungzip(new Uint8Array(data, binaryHeaderLength)).buffer);
  } else {
    data = new DataView(data, binaryHeaderLength);
  }

  if (json) {
    var decoded = textDecoder.decode(data);
    var body = JSON.parse(decoded);
    Object.keys(body).forEach(function (k) {
      msg[k] = body[k];
    });
    return msg;
  }

  switch (msg['type']) {
    case 'data':
      return parseBinaryDataMessage(msg, data, bigNumberRequested);
    default:
      console.warn('Unsupported binary "' + msg['type'] + '" message');
      return null;
  }
}
function parseBinaryMessage ( data , knownComputations ) { var msg = { } ; var header = new DataView ( data , 0 , binaryHeaderLength ) ; var version = header . getUint8 ( 0 ) ; extractBinaryFields ( msg , binaryHeaderFormats [ version ] , header ) ; var type = binaryMessageTypes [ msg . type ] ; if ( type === undefined ) { console . warn ( 'Unknown binary message type ' + msg . type ) ; return null ; } msg . type = type ; var bigNumberRequested = false ; if ( typeof knownComputations [ msg . channel ] !== 'undefined' && knownComputations [ msg . channel ] . params ) { bigNumberRequested = knownComputations [ msg . channel ] . params . bigNumber ; } var compressed = msg . flags & ( 1 << 0 ) ; var json = msg . flags & ( 1 << 1 ) ; if ( compressed ) { // Decompress the message body if necessary. data = new DataView ( pako . ungzip ( new Uint8Array ( data , binaryHeaderLength ) ) . buffer ) ; } else { data = new DataView ( data , binaryHeaderLength ) ; } if ( json ) { var decoded = textDecoder . decode ( data ) ; var body = JSON . parse ( decoded ) ; Object . keys ( body ) . forEach ( function ( k ) { msg [ k ] = body [ k ] ; } ) ; return msg ; } switch ( msg [ 'type' ] ) { case 'data' : return parseBinaryDataMessage ( msg , data , bigNumberRequested ) ; default : console . warn ( 'Unsupported binary "' + msg [ 'type' ] + '" message' ) ; return null ; } }
Parse a binary WebSocket message.

Binary messages have a 20-byte header (binaryHeaderLength), followed by a
body. Depending on the flags set in the header, the body of the message may
be compressed, so it needs to be decompressed before being parsed.

Finally, depending on the message type and the 'json' flag, the body of the
message may be a JSON string, or a binary format. As of now, only data batch
messages encode their body in binary, as per the binaryDataMessageFormats
defined above.
Parse a binary WebSocket message .