packages/grpc-native-core/src/client_interceptors.js
function _getUnaryInterceptor(method_definition, channel, emitter, callback) {
  var serialize = method_definition.requestSerialize;
  var deserialize = method_definition.responseDeserialize;
  return function (options) {
    var call = getCall(channel, method_definition.path, options);
    var first_listener;
    var final_requester = {};
    var batch_state = {
      completed_ops: [],
      deferred_batches: []
    };
    final_requester.start = function (metadata, listener) {
      var batch = {
        [grpc.opType.SEND_INITIAL_METADATA]:
          metadata._getCoreRepresentation(),
      };
      first_listener = listener;
      batch_state = _startBatchIfReady(call, batch, batch_state,
                                       function() {});
    };
    final_requester.sendMessage = function (message) {
      var batch = {
        [grpc.opType.SEND_MESSAGE]: serialize(message),
      };
      batch_state = _startBatchIfReady(call, batch, batch_state,
                                         function() {});
    };
    final_requester.halfClose = function () {
      var batch = {
        [grpc.opType.SEND_CLOSE_FROM_CLIENT]: true,
        [grpc.opType.RECV_INITIAL_METADATA]: true,
        [grpc.opType.RECV_MESSAGE]: true,
        [grpc.opType.RECV_STATUS_ON_CLIENT]: true
      };
      var callback = function (err, response) {
        response.status.metadata = Metadata._fromCoreRepresentation(
          response.status.metadata);
        var status = response.status;
        var deserialized;
        if (status.code === constants.status.OK) {
          if (err) {
            // Got a batch error, but OK status. Something went wrong
            callback(err);
            return;
          } else {
            try {
              deserialized = deserialize(response.read);
            } catch (e) {
              /* Change status to indicate bad server response. This
               * will result in passing an error to the callback */
              status = {
                code: constants.status.INTERNAL,
                details: 'Failed to parse server response'
              };
            }
          }
        }
        response.metadata =
          Metadata._fromCoreRepresentation(response.metadata);
        first_listener.onReceiveMetadata(response.metadata);
        first_listener.onReceiveMessage(deserialized);
        first_listener.onReceiveStatus(status);
      };
      batch_state = _startBatchIfReady(call, batch, batch_state, callback);
    };
    final_requester.cancel = function () {
      call.cancel();
    };
    final_requester.cancelWithStatus = function(code, details) {
      call.cancelWithStatus(code, details)
    };
    final_requester.getPeer = function () {
      return call.getPeer();
    };
    return new InterceptingCall(null, final_requester);
  };
}
function _getUnaryInterceptor(method_definition, channel, emitter, callback) {
  var serialize = method_definition.requestSerialize;
  var deserialize = method_definition.responseDeserialize;
  return function (options) {
    var call = getCall(channel, method_definition.path, options);
    var first_listener;
    var final_requester = {};
    var batch_state = {
      completed_ops: [],
      deferred_batches: []
    };
    final_requester.start = function (metadata, listener) {
      var batch = {
        [grpc.opType.SEND_INITIAL_METADATA]:
          metadata._getCoreRepresentation(),
      };
      first_listener = listener;
      batch_state = _startBatchIfReady(call, batch, batch_state,
                                       function() {});
    };
    final_requester.sendMessage = function (message) {
      var batch = {
        [grpc.opType.SEND_MESSAGE]: serialize(message),
      };
      batch_state = _startBatchIfReady(call, batch, batch_state,
                                         function() {});
    };
    final_requester.halfClose = function () {
      var batch = {
        [grpc.opType.SEND_CLOSE_FROM_CLIENT]: true,
        [grpc.opType.RECV_INITIAL_METADATA]: true,
        [grpc.opType.RECV_MESSAGE]: true,
        [grpc.opType.RECV_STATUS_ON_CLIENT]: true
      };
      var callback = function (err, response) {
        response.status.metadata = Metadata._fromCoreRepresentation(
          response.status.metadata);
        var status = response.status;
        var deserialized;
        if (status.code === constants.status.OK) {
          if (err) {
            // Got a batch error, but OK status. Something went wrong
            callback(err);
            return;
          } else {
            try {
              deserialized = deserialize(response.read);
            } catch (e) {
              /* Change status to indicate bad server response. This
               * will result in passing an error to the callback */
              status = {
                code: constants.status.INTERNAL,
                details: 'Failed to parse server response'
              };
            }
          }
        }
        response.metadata =
          Metadata._fromCoreRepresentation(response.metadata);
        first_listener.onReceiveMetadata(response.metadata);
        first_listener.onReceiveMessage(deserialized);
        first_listener.onReceiveStatus(status);
      };
      batch_state = _startBatchIfReady(call, batch, batch_state, callback);
    };
    final_requester.cancel = function () {
      call.cancel();
    };
    final_requester.cancelWithStatus = function(code, details) {
      call.cancelWithStatus(code, details)
    };
    final_requester.getPeer = function () {
      return call.getPeer();
    };
    return new InterceptingCall(null, final_requester);
  };
}
function _getUnaryInterceptor ( method_definition , channel , emitter , callback ) { var serialize = method_definition . requestSerialize ; var deserialize = method_definition . responseDeserialize ; return function ( options ) { var call = getCall ( channel , method_definition . path , options ) ; var first_listener ; var final_requester = { } ; var batch_state = { completed_ops : [ ] , deferred_batches : [ ] } ; final_requester . start = function ( metadata , listener ) { var batch = { [ grpc . opType . SEND_INITIAL_METADATA ] : metadata . _getCoreRepresentation ( ) , } ; first_listener = listener ; batch_state = _startBatchIfReady ( call , batch , batch_state , function ( ) { } ) ; } ; final_requester . sendMessage = function ( message ) { var batch = { [ grpc . opType . SEND_MESSAGE ] : serialize ( message ) , } ; batch_state = _startBatchIfReady ( call , batch , batch_state , function ( ) { } ) ; } ; final_requester . halfClose = function ( ) { var batch = { [ grpc . opType . SEND_CLOSE_FROM_CLIENT ] : true , [ grpc . opType . RECV_INITIAL_METADATA ] : true , [ grpc . opType . RECV_MESSAGE ] : true , [ grpc . opType . RECV_STATUS_ON_CLIENT ] : true } ; var callback = function ( err , response ) { response . status . metadata = Metadata . _fromCoreRepresentation ( response . status . metadata ) ; var status = response . status ; var deserialized ; if ( status . code === constants . status . OK ) { if ( err ) { // Got a batch error, but OK status. Something went wrong callback ( err ) ; return ; } else { try { deserialized = deserialize ( response . read ) ; } catch ( e ) { /* Change status to indicate bad server response. This
               * will result in passing an error to the callback */ status = { code : constants . status . INTERNAL , details : 'Failed to parse server response' } ; } } } response . metadata = Metadata . _fromCoreRepresentation ( response . metadata ) ; first_listener . onReceiveMetadata ( response . metadata ) ; first_listener . onReceiveMessage ( deserialized ) ; first_listener . onReceiveStatus ( status ) ; } ; batch_state = _startBatchIfReady ( call , batch , batch_state , callback ) ; } ; final_requester . cancel = function ( ) { call . cancel ( ) ; } ; final_requester . cancelWithStatus = function ( code , details ) { call . cancelWithStatus ( code , details ) } ; final_requester . getPeer = function ( ) { return call . getPeer ( ) ; } ; return new InterceptingCall ( null , final_requester ) ; } ; }
Produces an interceptor which will start gRPC batches for unary calls.
@private
@param {grpc~MethodDefinition} method_definition
@param {grpc.Channel} channel
@param {EventEmitter} emitter
@param {function} callback
@return {Interceptor}
Produces an interceptor which will start gRPC batches for unary calls .