packages/grpc-native-core/src/server.js
function setUpWritable(stream, serialize) {
  stream.finished = false;
  stream.status = {
    code : constants.status.OK,
    details : 'OK',
    metadata : new Metadata()
  };
  stream.serialize = common.wrapIgnoreNull(serialize);
  function sendStatus() {
    var batch = {};
    if (!stream.call.metadataSent) {
      stream.call.metadataSent = true;
      batch[grpc.opType.SEND_INITIAL_METADATA] =
          (new Metadata())._getCoreRepresentation();
    }

    if (stream.status.metadata) {
      stream.status.metadata = stream.status.metadata._getCoreRepresentation();
    }
    batch[grpc.opType.SEND_STATUS_FROM_SERVER] = stream.status;
    stream.call.startBatch(batch, function(){});
  }
  stream.on('finish', sendStatus);
  /**
   * Set the pending status to a given error status. If the error does not have
   * code or details properties, the code will be set to grpc.status.UNKNOWN
   * and the details will be set to 'Unknown Error'.
   * @param {Error} err The error object
   */
  function setStatus(err) {
    var code = constants.status.UNKNOWN;
    var details = 'Unknown Error';
    var metadata = new Metadata();
    if (err.hasOwnProperty('message')) {
      details = err.message;
    }
    if (err.hasOwnProperty('code')) {
      code = err.code;
      if (err.hasOwnProperty('details')) {
        details = err.details;
      }
    }
    if (err.hasOwnProperty('metadata')) {
      metadata = err.metadata;
    }
    stream.status = {code: code, details: details, metadata: metadata};
  }
  /**
   * Terminate the call. This includes indicating that reads are done, draining
   * all pending writes, and sending the given error as a status
   * @param {Error} err The error object
   * @this GrpcServerStream
   */
  function terminateCall(err) {
    // Drain readable data
    setStatus(err);
    stream.end();
  }
  stream.on('error', terminateCall);
  /**
   * Override of Writable#end method that allows for sending metadata with a
   * success status.
   * @param {Metadata=} metadata Metadata to send with the status
   */
  stream.end = function(metadata) {
    if (metadata) {
      stream.status.metadata = metadata;
    }
    Writable.prototype.end.call(this);
  };
}
function setUpWritable(stream, serialize) {
  stream.finished = false;
  stream.status = {
    code : constants.status.OK,
    details : 'OK',
    metadata : new Metadata()
  };
  stream.serialize = common.wrapIgnoreNull(serialize);
  function sendStatus() {
    var batch = {};
    if (!stream.call.metadataSent) {
      stream.call.metadataSent = true;
      batch[grpc.opType.SEND_INITIAL_METADATA] =
          (new Metadata())._getCoreRepresentation();
    }

    if (stream.status.metadata) {
      stream.status.metadata = stream.status.metadata._getCoreRepresentation();
    }
    batch[grpc.opType.SEND_STATUS_FROM_SERVER] = stream.status;
    stream.call.startBatch(batch, function(){});
  }
  stream.on('finish', sendStatus);
  /**
   * Set the pending status to a given error status. If the error does not have
   * code or details properties, the code will be set to grpc.status.UNKNOWN
   * and the details will be set to 'Unknown Error'.
   * @param {Error} err The error object
   */
  function setStatus(err) {
    var code = constants.status.UNKNOWN;
    var details = 'Unknown Error';
    var metadata = new Metadata();
    if (err.hasOwnProperty('message')) {
      details = err.message;
    }
    if (err.hasOwnProperty('code')) {
      code = err.code;
      if (err.hasOwnProperty('details')) {
        details = err.details;
      }
    }
    if (err.hasOwnProperty('metadata')) {
      metadata = err.metadata;
    }
    stream.status = {code: code, details: details, metadata: metadata};
  }
  /**
   * Terminate the call. This includes indicating that reads are done, draining
   * all pending writes, and sending the given error as a status
   * @param {Error} err The error object
   * @this GrpcServerStream
   */
  function terminateCall(err) {
    // Drain readable data
    setStatus(err);
    stream.end();
  }
  stream.on('error', terminateCall);
  /**
   * Override of Writable#end method that allows for sending metadata with a
   * success status.
   * @param {Metadata=} metadata Metadata to send with the status
   */
  stream.end = function(metadata) {
    if (metadata) {
      stream.status.metadata = metadata;
    }
    Writable.prototype.end.call(this);
  };
}
function setUpWritable ( stream , serialize ) { stream . finished = false ; stream . status = { code : constants . status . OK , details : 'OK' , metadata : new Metadata ( ) } ; stream . serialize = common . wrapIgnoreNull ( serialize ) ; function sendStatus ( ) { var batch = { } ; if ( ! stream . call . metadataSent ) { stream . call . metadataSent = true ; batch [ grpc . opType . SEND_INITIAL_METADATA ] = ( new Metadata ( ) ) . _getCoreRepresentation ( ) ; } if ( stream . status . metadata ) { stream . status . metadata = stream . status . metadata . _getCoreRepresentation ( ) ; } batch [ grpc . opType . SEND_STATUS_FROM_SERVER ] = stream . status ; stream . call . startBatch ( batch , function ( ) { } ) ; } stream . on ( 'finish' , sendStatus ) ; /**
   * Set the pending status to a given error status. If the error does not have
   * code or details properties, the code will be set to grpc.status.UNKNOWN
   * and the details will be set to 'Unknown Error'.
   * @param {Error} err The error object
   */ function setStatus ( err ) { var code = constants . status . UNKNOWN ; var details = 'Unknown Error' ; var metadata = new Metadata ( ) ; if ( err . hasOwnProperty ( 'message' ) ) { details = err . message ; } if ( err . hasOwnProperty ( 'code' ) ) { code = err . code ; if ( err . hasOwnProperty ( 'details' ) ) { details = err . details ; } } if ( err . hasOwnProperty ( 'metadata' ) ) { metadata = err . metadata ; } stream . status = { code : code , details : details , metadata : metadata } ; } /**
   * Terminate the call. This includes indicating that reads are done, draining
   * all pending writes, and sending the given error as a status
   * @param {Error} err The error object
   * @this GrpcServerStream
   */ function terminateCall ( err ) { // Drain readable data setStatus ( err ) ; stream . end ( ) ; } stream . on ( 'error' , terminateCall ) ; /**
   * Override of Writable#end method that allows for sending metadata with a
   * success status.
   * @param {Metadata=} metadata Metadata to send with the status
   */ stream . end = function ( metadata ) { if ( metadata ) { stream . status . metadata = metadata ; } Writable . prototype . end . call ( this ) ; } ; }
Initialize a writable stream. This is used for both the writable and duplex
stream constructors.
@private
@param {Writable} stream The stream to set up
@param {function(*):Buffer=} Serialization function for responses
Initialize a writable stream . This is used for both the writable and duplex stream constructors .