|
|
|
@ -50,6 +50,7 @@ |
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
var _ = require('lodash'); |
|
|
|
|
var arguejs = require('arguejs'); |
|
|
|
|
|
|
|
|
|
var grpc = require('./grpc_extension'); |
|
|
|
|
|
|
|
|
@ -353,21 +354,23 @@ function makeUnaryRequestFunction(method, serialize, deserialize) { |
|
|
|
|
* @this {Client} Client object. Must have a channel member. |
|
|
|
|
* @param {*} argument The argument to the call. Should be serializable with |
|
|
|
|
* serialize |
|
|
|
|
* @param {function(?Error, value=)} callback The callback to for when the |
|
|
|
|
* response is received |
|
|
|
|
* @param {Metadata=} metadata Metadata to add to the call |
|
|
|
|
* @param {Object=} options Options map |
|
|
|
|
* @param {function(?Error, value=)} callback The callback to for when the |
|
|
|
|
* response is received |
|
|
|
|
* @return {EventEmitter} An event emitter for stream related events |
|
|
|
|
*/ |
|
|
|
|
function makeUnaryRequest(argument, callback, metadata, options) { |
|
|
|
|
function makeUnaryRequest(argument, metadata, options, callback) { |
|
|
|
|
/* jshint validthis: true */ |
|
|
|
|
/* While the arguments are listed in the function signature, those variables |
|
|
|
|
* are not used directly. Instead, ArgueJS processes the arguments |
|
|
|
|
* object. This allows for simple handling of optional arguments in the |
|
|
|
|
* middle of the argument list, and also provides type checking. */ |
|
|
|
|
var args = arguejs({argument: null, metadata: [Metadata, new Metadata()], |
|
|
|
|
options: [Object], callback: Function}, arguments); |
|
|
|
|
var emitter = new EventEmitter(); |
|
|
|
|
var call = getCall(this.$channel, method, options); |
|
|
|
|
if (metadata === null || metadata === undefined) { |
|
|
|
|
metadata = new Metadata(); |
|
|
|
|
} else { |
|
|
|
|
metadata = metadata.clone(); |
|
|
|
|
} |
|
|
|
|
var call = getCall(this.$channel, method, args.options); |
|
|
|
|
metadata = args.metadata.clone(); |
|
|
|
|
emitter.cancel = function cancel() { |
|
|
|
|
call.cancel(); |
|
|
|
|
}; |
|
|
|
@ -375,9 +378,9 @@ function makeUnaryRequestFunction(method, serialize, deserialize) { |
|
|
|
|
return call.getPeer(); |
|
|
|
|
}; |
|
|
|
|
var client_batch = {}; |
|
|
|
|
var message = serialize(argument); |
|
|
|
|
if (options) { |
|
|
|
|
message.grpcWriteFlags = options.flags; |
|
|
|
|
var message = serialize(args.argument); |
|
|
|
|
if (args.options) { |
|
|
|
|
message.grpcWriteFlags = args.options.flags; |
|
|
|
|
} |
|
|
|
|
client_batch[grpc.opType.SEND_INITIAL_METADATA] = |
|
|
|
|
metadata._getCoreRepresentation(); |
|
|
|
@ -395,7 +398,7 @@ function makeUnaryRequestFunction(method, serialize, deserialize) { |
|
|
|
|
if (status.code === grpc.status.OK) { |
|
|
|
|
if (err) { |
|
|
|
|
// Got a batch error, but OK status. Something went wrong
|
|
|
|
|
callback(err); |
|
|
|
|
args.callback(err); |
|
|
|
|
return; |
|
|
|
|
} else { |
|
|
|
|
try { |
|
|
|
@ -414,9 +417,9 @@ function makeUnaryRequestFunction(method, serialize, deserialize) { |
|
|
|
|
error = new Error(status.details); |
|
|
|
|
error.code = status.code; |
|
|
|
|
error.metadata = status.metadata; |
|
|
|
|
callback(error); |
|
|
|
|
args.callback(error); |
|
|
|
|
} else { |
|
|
|
|
callback(null, deserialized); |
|
|
|
|
args.callback(null, deserialized); |
|
|
|
|
} |
|
|
|
|
emitter.emit('status', status); |
|
|
|
|
emitter.emit('metadata', Metadata._fromCoreRepresentation( |
|
|
|
@ -440,21 +443,23 @@ function makeClientStreamRequestFunction(method, serialize, deserialize) { |
|
|
|
|
* Make a client stream request with this method on the given channel with the |
|
|
|
|
* given callback, etc. |
|
|
|
|
* @this {Client} Client object. Must have a channel member. |
|
|
|
|
* @param {function(?Error, value=)} callback The callback to for when the |
|
|
|
|
* response is received |
|
|
|
|
* @param {Metadata=} metadata Array of metadata key/value pairs to add to the |
|
|
|
|
* call |
|
|
|
|
* @param {Object=} options Options map |
|
|
|
|
* @param {function(?Error, value=)} callback The callback to for when the |
|
|
|
|
* response is received |
|
|
|
|
* @return {EventEmitter} An event emitter for stream related events |
|
|
|
|
*/ |
|
|
|
|
function makeClientStreamRequest(callback, metadata, options) { |
|
|
|
|
function makeClientStreamRequest(metadata, options, callback) { |
|
|
|
|
/* jshint validthis: true */ |
|
|
|
|
var call = getCall(this.$channel, method, options); |
|
|
|
|
if (metadata === null || metadata === undefined) { |
|
|
|
|
metadata = new Metadata(); |
|
|
|
|
} else { |
|
|
|
|
metadata = metadata.clone(); |
|
|
|
|
} |
|
|
|
|
/* While the arguments are listed in the function signature, those variables |
|
|
|
|
* are not used directly. Instead, ArgueJS processes the arguments |
|
|
|
|
* object. This allows for simple handling of optional arguments in the |
|
|
|
|
* middle of the argument list, and also provides type checking. */ |
|
|
|
|
var args = arguejs({metadata: [Metadata, new Metadata()], |
|
|
|
|
options: [Object], callback: Function}, arguments); |
|
|
|
|
var call = getCall(this.$channel, method, args.options); |
|
|
|
|
metadata = args.metadata.clone(); |
|
|
|
|
var stream = new ClientWritableStream(call, serialize); |
|
|
|
|
var metadata_batch = {}; |
|
|
|
|
metadata_batch[grpc.opType.SEND_INITIAL_METADATA] = |
|
|
|
@ -481,7 +486,7 @@ function makeClientStreamRequestFunction(method, serialize, deserialize) { |
|
|
|
|
if (status.code === grpc.status.OK) { |
|
|
|
|
if (err) { |
|
|
|
|
// Got a batch error, but OK status. Something went wrong
|
|
|
|
|
callback(err); |
|
|
|
|
args.callback(err); |
|
|
|
|
return; |
|
|
|
|
} else { |
|
|
|
|
try { |
|
|
|
@ -500,9 +505,9 @@ function makeClientStreamRequestFunction(method, serialize, deserialize) { |
|
|
|
|
error = new Error(response.status.details); |
|
|
|
|
error.code = status.code; |
|
|
|
|
error.metadata = status.metadata; |
|
|
|
|
callback(error); |
|
|
|
|
args.callback(error); |
|
|
|
|
} else { |
|
|
|
|
callback(null, deserialized); |
|
|
|
|
args.callback(null, deserialized); |
|
|
|
|
} |
|
|
|
|
stream.emit('status', status); |
|
|
|
|
}); |
|
|
|
@ -533,17 +538,18 @@ function makeServerStreamRequestFunction(method, serialize, deserialize) { |
|
|
|
|
*/ |
|
|
|
|
function makeServerStreamRequest(argument, metadata, options) { |
|
|
|
|
/* jshint validthis: true */ |
|
|
|
|
var call = getCall(this.$channel, method, options); |
|
|
|
|
if (metadata === null || metadata === undefined) { |
|
|
|
|
metadata = new Metadata(); |
|
|
|
|
} else { |
|
|
|
|
metadata = metadata.clone(); |
|
|
|
|
} |
|
|
|
|
/* While the arguments are listed in the function signature, those variables |
|
|
|
|
* are not used directly. Instead, ArgueJS processes the arguments |
|
|
|
|
* object. */ |
|
|
|
|
var args = arguejs({argument: null, metadata: [Metadata, new Metadata()], |
|
|
|
|
options: [Object]}, arguments); |
|
|
|
|
var call = getCall(this.$channel, method, args.options); |
|
|
|
|
metadata = args.metadata.clone(); |
|
|
|
|
var stream = new ClientReadableStream(call, deserialize); |
|
|
|
|
var start_batch = {}; |
|
|
|
|
var message = serialize(argument); |
|
|
|
|
if (options) { |
|
|
|
|
message.grpcWriteFlags = options.flags; |
|
|
|
|
var message = serialize(args.argument); |
|
|
|
|
if (args.options) { |
|
|
|
|
message.grpcWriteFlags = args.options.flags; |
|
|
|
|
} |
|
|
|
|
start_batch[grpc.opType.SEND_INITIAL_METADATA] = |
|
|
|
|
metadata._getCoreRepresentation(); |
|
|
|
@ -595,12 +601,13 @@ function makeBidiStreamRequestFunction(method, serialize, deserialize) { |
|
|
|
|
*/ |
|
|
|
|
function makeBidiStreamRequest(metadata, options) { |
|
|
|
|
/* jshint validthis: true */ |
|
|
|
|
var call = getCall(this.$channel, method, options); |
|
|
|
|
if (metadata === null || metadata === undefined) { |
|
|
|
|
metadata = new Metadata(); |
|
|
|
|
} else { |
|
|
|
|
metadata = metadata.clone(); |
|
|
|
|
} |
|
|
|
|
/* While the arguments are listed in the function signature, those variables |
|
|
|
|
* are not used directly. Instead, ArgueJS processes the arguments |
|
|
|
|
* object. */ |
|
|
|
|
var args = arguejs({metadata: [Metadata, new Metadata()], |
|
|
|
|
options: [Object]}, arguments); |
|
|
|
|
var call = getCall(this.$channel, method, args.options); |
|
|
|
|
metadata = args.metadata.clone(); |
|
|
|
|
var stream = new ClientDuplexStream(call, serialize, deserialize); |
|
|
|
|
var start_batch = {}; |
|
|
|
|
start_batch[grpc.opType.SEND_INITIAL_METADATA] = |
|
|
|
|