|
|
|
@ -31,6 +31,11 @@ |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Server module |
|
|
|
|
* @module |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
var _ = require('lodash'); |
|
|
|
@ -50,6 +55,7 @@ var EventEmitter = require('events').EventEmitter; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Handle an error on a call by sending it as a status |
|
|
|
|
* @access private |
|
|
|
|
* @param {grpc.Call} call The call to send the error on |
|
|
|
|
* @param {Object} error The error object |
|
|
|
|
*/ |
|
|
|
@ -82,6 +88,7 @@ function handleError(call, error) { |
|
|
|
|
/** |
|
|
|
|
* Wait for the client to close, then emit a cancelled event if the client |
|
|
|
|
* cancelled. |
|
|
|
|
* @access private |
|
|
|
|
* @param {grpc.Call} call The call object to wait on |
|
|
|
|
* @param {EventEmitter} emitter The event emitter to emit the cancelled event |
|
|
|
|
* on |
|
|
|
@ -102,6 +109,7 @@ function waitForCancel(call, emitter) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Send a response to a unary or client streaming call. |
|
|
|
|
* @access private |
|
|
|
|
* @param {grpc.Call} call The call to respond on |
|
|
|
|
* @param {*} value The value to respond with |
|
|
|
|
* @param {function(*):Buffer=} serialize Serialization function for the |
|
|
|
@ -130,6 +138,7 @@ function sendUnaryResponse(call, value, serialize, metadata) { |
|
|
|
|
/** |
|
|
|
|
* Initialize a writable stream. This is used for both the writable and duplex |
|
|
|
|
* stream constructors. |
|
|
|
|
* @access private |
|
|
|
|
* @param {Writable} stream The stream to set up |
|
|
|
|
* @param {function(*):Buffer=} Serialization function for responses |
|
|
|
|
*/ |
|
|
|
@ -203,6 +212,7 @@ function setUpWritable(stream, serialize) { |
|
|
|
|
/** |
|
|
|
|
* Initialize a readable stream. This is used for both the readable and duplex |
|
|
|
|
* stream constructors. |
|
|
|
|
* @access private |
|
|
|
|
* @param {Readable} stream The stream to initialize |
|
|
|
|
* @param {function(Buffer):*=} deserialize Deserialization function for |
|
|
|
|
* incoming data. |
|
|
|
@ -242,6 +252,7 @@ function ServerWritableStream(call, serialize) { |
|
|
|
|
/** |
|
|
|
|
* Start writing a chunk of data. This is an implementation of a method required |
|
|
|
|
* for implementing stream.Writable. |
|
|
|
|
* @access private |
|
|
|
|
* @param {Buffer} chunk The chunk of data to write |
|
|
|
|
* @param {string} encoding Ignored |
|
|
|
|
* @param {function(Error=)} callback Callback to indicate that the write is |
|
|
|
@ -266,6 +277,11 @@ function _write(chunk, encoding, callback) { |
|
|
|
|
|
|
|
|
|
ServerWritableStream.prototype._write = _write; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Send the initial metadata for a writable stream. |
|
|
|
|
* @param {Object<String, Array<(String|Buffer)>>} responseMetadata Metadata |
|
|
|
|
* to send |
|
|
|
|
*/ |
|
|
|
|
function sendMetadata(responseMetadata) { |
|
|
|
|
/* jshint validthis: true */ |
|
|
|
|
if (!this.call.metadataSent) { |
|
|
|
@ -281,6 +297,10 @@ function sendMetadata(responseMetadata) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @inheritdoc |
|
|
|
|
* @alias module:src/server~ServerWritableStream#sendMetadata |
|
|
|
|
*/ |
|
|
|
|
ServerWritableStream.prototype.sendMetadata = sendMetadata; |
|
|
|
|
|
|
|
|
|
util.inherits(ServerReadableStream, Readable); |
|
|
|
@ -301,6 +321,7 @@ function ServerReadableStream(call, deserialize) { |
|
|
|
|
/** |
|
|
|
|
* Start reading from the gRPC data source. This is an implementation of a |
|
|
|
|
* method required for implementing stream.Readable |
|
|
|
|
* @access private |
|
|
|
|
* @param {number} size Ignored |
|
|
|
|
*/ |
|
|
|
|
function _read(size) { |
|
|
|
@ -388,6 +409,7 @@ ServerDuplexStream.prototype.getPeer = getPeer; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Fully handle a unary call |
|
|
|
|
* @access private |
|
|
|
|
* @param {grpc.Call} call The call to handle |
|
|
|
|
* @param {Object} handler Request handler object for the method that was called |
|
|
|
|
* @param {Object} metadata Metadata from the client |
|
|
|
@ -442,6 +464,7 @@ function handleUnary(call, handler, metadata) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Fully handle a server streaming call |
|
|
|
|
* @access private |
|
|
|
|
* @param {grpc.Call} call The call to handle |
|
|
|
|
* @param {Object} handler Request handler object for the method that was called |
|
|
|
|
* @param {Object} metadata Metadata from the client |
|
|
|
@ -470,6 +493,7 @@ function handleServerStreaming(call, handler, metadata) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Fully handle a client streaming call |
|
|
|
|
* @access private |
|
|
|
|
* @param {grpc.Call} call The call to handle |
|
|
|
|
* @param {Object} handler Request handler object for the method that was called |
|
|
|
|
* @param {Object} metadata Metadata from the client |
|
|
|
@ -504,6 +528,7 @@ function handleClientStreaming(call, handler, metadata) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Fully handle a bidirectional streaming call |
|
|
|
|
* @access private |
|
|
|
|
* @param {grpc.Call} call The call to handle |
|
|
|
|
* @param {Object} handler Request handler object for the method that was called |
|
|
|
|
* @param {Object} metadata Metadata from the client |
|
|
|
@ -587,7 +612,8 @@ function Server(options) { |
|
|
|
|
} |
|
|
|
|
server.requestCall(handleNewCall); |
|
|
|
|
}; |
|
|
|
|
/** Shuts down the server. |
|
|
|
|
/** |
|
|
|
|
* Shuts down the server. |
|
|
|
|
*/ |
|
|
|
|
this.shutdown = function() { |
|
|
|
|
server.shutdown(); |
|
|
|
@ -621,6 +647,15 @@ Server.prototype.register = function(name, handler, serialize, deserialize, |
|
|
|
|
return true; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Add a service to the server, with a corresponding implementation. If you are |
|
|
|
|
* generating this from a proto file, you should instead use |
|
|
|
|
* addProtoService. |
|
|
|
|
* @param {Object<String, *>} service The service descriptor, as |
|
|
|
|
* {@link module:src/common.getProtobufServiceAttrs} returns |
|
|
|
|
* @param {Object<String, function>} implementation Map of method names to |
|
|
|
|
* method implementation for the provided service. |
|
|
|
|
*/ |
|
|
|
|
Server.prototype.addService = function(service, implementation) { |
|
|
|
|
if (this.started) { |
|
|
|
|
throw new Error('Can\'t add a service to a started server.'); |
|
|
|
@ -658,6 +693,12 @@ Server.prototype.addService = function(service, implementation) { |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Add a proto service to the server, with a corresponding implementation |
|
|
|
|
* @param {Protobuf.Reflect.Service} service The proto service descriptor |
|
|
|
|
* @param {Object<String, function>} implementation Map of method names to |
|
|
|
|
* method implementation for the provided service. |
|
|
|
|
*/ |
|
|
|
|
Server.prototype.addProtoService = function(service, implementation) { |
|
|
|
|
this.addService(common.getProtobufServiceAttrs(service), implementation); |
|
|
|
|
}; |
|
|
|
@ -681,6 +722,6 @@ Server.prototype.bind = function(port, creds) { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* See documentation for Server |
|
|
|
|
* @see module:src/server~Server |
|
|
|
|
*/ |
|
|
|
|
exports.Server = Server; |
|
|
|
|