jrpc.js
function receive(msg) {
  var requests = [];
  var responses = [];

  if (!this.active) {
    return this;
  }

  // If we got JSON, parse it
  if (typeof msg === 'string') {
    try {
      msg = JSON.parse(msg);
    } catch (e) {
      // The specification doesn't force us to respond in error, ignoring
      return this;
    }
  }

  // If we get a standard single-type batch, dispatch it
  if (msg.constructor === Array) {
    if (msg.length === 0) {
      return this;
    }
    // Hint of request batch
    if (typeof msg[0].method === 'string') {
      requests = msg;
    } else {
      responses = msg;
    }
  } else if (typeof msg === 'object') {
    // Could we be a 'dual-batch' extended message?
    if (
      typeof msg.requests !== 'undefined'
      && typeof msg.responses !== 'undefined'
    ) {
      requests = msg.requests;
      responses = msg.responses;
    } else if (typeof msg.method === 'string') {
      // We're a single request
      requests.push(msg);
    } else {
      // We must be a single response
      responses.push(msg);
    }
  }

  responses.forEach(deliverResponse.bind(this));
  requests.forEach(serveRequest.bind(this));
  return this;
}
function receive(msg) {
  var requests = [];
  var responses = [];

  if (!this.active) {
    return this;
  }

  // If we got JSON, parse it
  if (typeof msg === 'string') {
    try {
      msg = JSON.parse(msg);
    } catch (e) {
      // The specification doesn't force us to respond in error, ignoring
      return this;
    }
  }

  // If we get a standard single-type batch, dispatch it
  if (msg.constructor === Array) {
    if (msg.length === 0) {
      return this;
    }
    // Hint of request batch
    if (typeof msg[0].method === 'string') {
      requests = msg;
    } else {
      responses = msg;
    }
  } else if (typeof msg === 'object') {
    // Could we be a 'dual-batch' extended message?
    if (
      typeof msg.requests !== 'undefined'
      && typeof msg.responses !== 'undefined'
    ) {
      requests = msg.requests;
      responses = msg.responses;
    } else if (typeof msg.method === 'string') {
      // We're a single request
      requests.push(msg);
    } else {
      // We must be a single response
      responses.push(msg);
    }
  }

  responses.forEach(deliverResponse.bind(this));
  requests.forEach(serveRequest.bind(this));
  return this;
}
function receive ( msg ) { var requests = [ ] ; var responses = [ ] ; if ( ! this . active ) { return this ; } // If we got JSON, parse it if ( typeof msg === 'string' ) { try { msg = JSON . parse ( msg ) ; } catch ( e ) { // The specification doesn't force us to respond in error, ignoring return this ; } } // If we get a standard single-type batch, dispatch it if ( msg . constructor === Array ) { if ( msg . length === 0 ) { return this ; } // Hint of request batch if ( typeof msg [ 0 ] . method === 'string' ) { requests = msg ; } else { responses = msg ; } } else if ( typeof msg === 'object' ) { // Could we be a 'dual-batch' extended message? if ( typeof msg . requests !== 'undefined' && typeof msg . responses !== 'undefined' ) { requests = msg . requests ; responses = msg . responses ; } else if ( typeof msg . method === 'string' ) { // We're a single request requests . push ( msg ) ; } else { // We must be a single response responses . push ( msg ) ; } } responses . forEach ( deliverResponse . bind ( this ) ) ; requests . forEach ( serveRequest . bind ( this ) ) ; return this ; }
Handle incoming message

@param {string} msg JSON message to parse

@return {JRPC} This instance, for chaining
Handle incoming message