packages/grpc-native-core/src/client_interceptors.js
function _getServerStreamingInterceptor(method_definition, channel, emitter) {
  var deserialize = common.wrapIgnoreNull(
    method_definition.responseDeserialize);
  var serialize = method_definition.requestSerialize;
  return function (options) {
    var batch_state = {
      completed_ops: [],
      deferred_batches: []
    };
    var call = getCall(channel, method_definition.path, options);
    var final_requester = {};
    var first_listener;
    var get_listener = function() {
      return first_listener;
    };
    final_requester.start = function(metadata, listener) {
      first_listener = listener;
      metadata = metadata.clone();
      var metadata_batch = {
        [grpc.opType.SEND_INITIAL_METADATA]: metadata._getCoreRepresentation(),
        [grpc.opType.RECV_INITIAL_METADATA]: true
      };
      var callback = function(err, response) {
        if (err) {
          // The call has stopped for some reason. A non-OK status will arrive
          // in the other batch.
          return;
        }
        first_listener.onReceiveMetadata(
          Metadata._fromCoreRepresentation(response.metadata));
      };
      batch_state = _startBatchIfReady(call, metadata_batch, batch_state,
                                       callback);
      var status_batch = {
        [grpc.opType.RECV_STATUS_ON_CLIENT]: true
      };
      call.startBatch(status_batch, function(err, response) {
        if (err) {
          emitter.emit('error', err);
          return;
        }
        response.status.metadata = Metadata._fromCoreRepresentation(
          response.status.metadata);
        first_listener.onReceiveStatus(response.status);
      });
    };
    final_requester.sendMessage = function(argument) {
      var message = serialize(argument);
      if (options) {
        message.grpcWriteFlags = options.flags;
      }
      var send_batch = {
        [grpc.opType.SEND_MESSAGE]: message
      };
      var callback = function(err, response) {
        if (err) {
          // The call has stopped for some reason. A non-OK status will arrive
          // in the other batch.
          return;
        }
      };
      batch_state = _startBatchIfReady(call, send_batch, batch_state, callback);
    };
    final_requester.halfClose = function() {
      var batch = {
        [grpc.opType.SEND_CLOSE_FROM_CLIENT]: true
      };
      batch_state = _startBatchIfReady(call, batch, batch_state, function() {});
    };
    final_requester.recvMessageWithContext = function(context) {
      var recv_batch = {
        [grpc.opType.RECV_MESSAGE]: true
      };
      var callback = _getStreamReadCallback(emitter, call,
        get_listener, deserialize);
      batch_state = _startBatchIfReady(call, recv_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 _getServerStreamingInterceptor(method_definition, channel, emitter) {
  var deserialize = common.wrapIgnoreNull(
    method_definition.responseDeserialize);
  var serialize = method_definition.requestSerialize;
  return function (options) {
    var batch_state = {
      completed_ops: [],
      deferred_batches: []
    };
    var call = getCall(channel, method_definition.path, options);
    var final_requester = {};
    var first_listener;
    var get_listener = function() {
      return first_listener;
    };
    final_requester.start = function(metadata, listener) {
      first_listener = listener;
      metadata = metadata.clone();
      var metadata_batch = {
        [grpc.opType.SEND_INITIAL_METADATA]: metadata._getCoreRepresentation(),
        [grpc.opType.RECV_INITIAL_METADATA]: true
      };
      var callback = function(err, response) {
        if (err) {
          // The call has stopped for some reason. A non-OK status will arrive
          // in the other batch.
          return;
        }
        first_listener.onReceiveMetadata(
          Metadata._fromCoreRepresentation(response.metadata));
      };
      batch_state = _startBatchIfReady(call, metadata_batch, batch_state,
                                       callback);
      var status_batch = {
        [grpc.opType.RECV_STATUS_ON_CLIENT]: true
      };
      call.startBatch(status_batch, function(err, response) {
        if (err) {
          emitter.emit('error', err);
          return;
        }
        response.status.metadata = Metadata._fromCoreRepresentation(
          response.status.metadata);
        first_listener.onReceiveStatus(response.status);
      });
    };
    final_requester.sendMessage = function(argument) {
      var message = serialize(argument);
      if (options) {
        message.grpcWriteFlags = options.flags;
      }
      var send_batch = {
        [grpc.opType.SEND_MESSAGE]: message
      };
      var callback = function(err, response) {
        if (err) {
          // The call has stopped for some reason. A non-OK status will arrive
          // in the other batch.
          return;
        }
      };
      batch_state = _startBatchIfReady(call, send_batch, batch_state, callback);
    };
    final_requester.halfClose = function() {
      var batch = {
        [grpc.opType.SEND_CLOSE_FROM_CLIENT]: true
      };
      batch_state = _startBatchIfReady(call, batch, batch_state, function() {});
    };
    final_requester.recvMessageWithContext = function(context) {
      var recv_batch = {
        [grpc.opType.RECV_MESSAGE]: true
      };
      var callback = _getStreamReadCallback(emitter, call,
        get_listener, deserialize);
      batch_state = _startBatchIfReady(call, recv_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 _getServerStreamingInterceptor ( method_definition , channel , emitter ) { var deserialize = common . wrapIgnoreNull ( method_definition . responseDeserialize ) ; var serialize = method_definition . requestSerialize ; return function ( options ) { var batch_state = { completed_ops : [ ] , deferred_batches : [ ] } ; var call = getCall ( channel , method_definition . path , options ) ; var final_requester = { } ; var first_listener ; var get_listener = function ( ) { return first_listener ; } ; final_requester . start = function ( metadata , listener ) { first_listener = listener ; metadata = metadata . clone ( ) ; var metadata_batch = { [ grpc . opType . SEND_INITIAL_METADATA ] : metadata . _getCoreRepresentation ( ) , [ grpc . opType . RECV_INITIAL_METADATA ] : true } ; var callback = function ( err , response ) { if ( err ) { // The call has stopped for some reason. A non-OK status will arrive // in the other batch. return ; } first_listener . onReceiveMetadata ( Metadata . _fromCoreRepresentation ( response . metadata ) ) ; } ; batch_state = _startBatchIfReady ( call , metadata_batch , batch_state , callback ) ; var status_batch = { [ grpc . opType . RECV_STATUS_ON_CLIENT ] : true } ; call . startBatch ( status_batch , function ( err , response ) { if ( err ) { emitter . emit ( 'error' , err ) ; return ; } response . status . metadata = Metadata . _fromCoreRepresentation ( response . status . metadata ) ; first_listener . onReceiveStatus ( response . status ) ; } ) ; } ; final_requester . sendMessage = function ( argument ) { var message = serialize ( argument ) ; if ( options ) { message . grpcWriteFlags = options . flags ; } var send_batch = { [ grpc . opType . SEND_MESSAGE ] : message } ; var callback = function ( err , response ) { if ( err ) { // The call has stopped for some reason. A non-OK status will arrive // in the other batch. return ; } } ; batch_state = _startBatchIfReady ( call , send_batch , batch_state , callback ) ; } ; final_requester . halfClose = function ( ) { var batch = { [ grpc . opType . SEND_CLOSE_FROM_CLIENT ] : true } ; batch_state = _startBatchIfReady ( call , batch , batch_state , function ( ) { } ) ; } ; final_requester . recvMessageWithContext = function ( context ) { var recv_batch = { [ grpc . opType . RECV_MESSAGE ] : true } ; var callback = _getStreamReadCallback ( emitter , call , get_listener , deserialize ) ; batch_state = _startBatchIfReady ( call , recv_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 server streaming
calls.
@private
@param {grpc~MethodDefinition} method_definition
@param {grpc.Channel} channel
@param {EventEmitter} emitter
@return {Interceptor}
Produces an interceptor which will start gRPC batches for server streaming calls .