Skip to content

Commit 09fe69f

Browse files
authored
Merge pull request #385 from murgatroid99/generic_client_interceptor_fix
Fix generic client interceptor resolution
2 parents 8d9ee1b + 01e0d32 commit 09fe69f

File tree

2 files changed

+26
-99
lines changed

2 files changed

+26
-99
lines changed

packages/grpc-native-core/src/client.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,18 @@ function Client(address, credentials, options) {
380380
options['grpc.primary_user_agent'] += 'grpc-node/' + version;
381381

382382
// Resolve interceptor options and assign interceptors to each method
383-
var interceptor_providers = options.interceptor_providers || [];
384-
var interceptors = options.interceptors || [];
385-
if (interceptor_providers.length && interceptors.length) {
383+
if (_.isArray(options.interceptor_providers) && _.isArray(options.interceptors)) {
386384
throw new client_interceptors.InterceptorConfigurationError(
387385
'Both interceptors and interceptor_providers were passed as options ' +
388386
'to the client constructor. Only one of these is allowed.');
389387
}
388+
self.$interceptors = options.interceptors || [];
389+
self.$interceptor_providers = options.interceptor_providers || [];
390390
_.each(self.$method_definitions, function(method_definition, method_name) {
391391
self[method_name].interceptors = client_interceptors
392-
.resolveInterceptorProviders(interceptor_providers, method_definition)
393-
.concat(interceptors);
392+
.resolveInterceptorProviders(self.$interceptor_providers, method_definition)
393+
.concat(self.$interceptors);
394394
});
395-
396395
// Exclude interceptor options which have already been consumed
397396
var channel_options = _.omit(options,
398397
['interceptors', 'interceptor_providers']);
@@ -403,6 +402,21 @@ function Client(address, credentials, options) {
403402

404403
exports.Client = Client;
405404

405+
Client.prototype.resolveCallInterceptors = function(method_definition, interceptors, interceptor_providers) {
406+
if (_.isArray(interceptors) && _.isArray(interceptor_providers)) {
407+
throw new client_interceptors.InterceptorConfigurationError(
408+
'Both interceptors and interceptor_providers were passed as call ' +
409+
'options. Only one of these is allowed.');
410+
}
411+
if (_.isArray(interceptors) || _.isArray(interceptor_providers)) {
412+
interceptors = interceptors || [];
413+
interceptor_providers = interceptor_providers || [];
414+
return client_interceptors.resolveInterceptorProviders(interceptor_providers, method_definition).concat(interceptors);
415+
} else {
416+
return client_interceptors.resolveInterceptorProviders(this.$interceptor_providers, method_definition).concat(this.$interceptors);
417+
}
418+
}
419+
406420
/**
407421
* @callback grpc.Client~requestCallback
408422
* @param {?grpc~ServiceError} error The error, if the call
@@ -454,10 +468,6 @@ Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
454468
throw new Error('Argument mismatch in makeUnaryRequest');
455469
}
456470

457-
var method_name = this.$method_names[path];
458-
var constructor_interceptors = this[method_name] ?
459-
this[method_name].interceptors :
460-
null;
461471
var method_definition = options.method_definition = {
462472
path: path,
463473
requestStream: false,
@@ -471,7 +481,7 @@ Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
471481
var intercepting_call = client_interceptors.getInterceptingCall(
472482
method_definition,
473483
options,
474-
constructor_interceptors,
484+
Client.prototype.resolveCallInterceptors.call(this, method_definition, options.interceptors, options.interceptor_providers),
475485
this.$channel,
476486
callback
477487
);
@@ -533,10 +543,6 @@ Client.prototype.makeClientStreamRequest = function(path, serialize,
533543
throw new Error('Argument mismatch in makeClientStreamRequest');
534544
}
535545

536-
var method_name = this.$method_names[path];
537-
var constructor_interceptors = this[method_name] ?
538-
this[method_name].interceptors :
539-
null;
540546
var method_definition = options.method_definition = {
541547
path: path,
542548
requestStream: true,
@@ -550,7 +556,7 @@ Client.prototype.makeClientStreamRequest = function(path, serialize,
550556
var intercepting_call = client_interceptors.getInterceptingCall(
551557
method_definition,
552558
options,
553-
constructor_interceptors,
559+
Client.prototype.resolveCallInterceptors.call(this, method_definition, options.interceptors, options.interceptor_providers),
554560
this.$channel,
555561
callback
556562
);
@@ -595,10 +601,6 @@ Client.prototype.makeServerStreamRequest = function(path, serialize,
595601
throw new Error('Argument mismatch in makeServerStreamRequest');
596602
}
597603

598-
var method_name = this.$method_names[path];
599-
var constructor_interceptors = this[method_name] ?
600-
this[method_name].interceptors :
601-
null;
602604
var method_definition = options.method_definition = {
603605
path: path,
604606
requestStream: false,
@@ -613,7 +615,7 @@ Client.prototype.makeServerStreamRequest = function(path, serialize,
613615
var intercepting_call = client_interceptors.getInterceptingCall(
614616
method_definition,
615617
options,
616-
constructor_interceptors,
618+
Client.prototype.resolveCallInterceptors.call(this, method_definition, options.interceptors, options.interceptor_providers),
617619
this.$channel,
618620
emitter
619621
);
@@ -655,10 +657,6 @@ Client.prototype.makeBidiStreamRequest = function(path, serialize,
655657
throw new Error('Argument mismatch in makeBidiStreamRequest');
656658
}
657659

658-
var method_name = this.$method_names[path];
659-
var constructor_interceptors = this[method_name] ?
660-
this[method_name].interceptors :
661-
null;
662660
var method_definition = options.method_definition = {
663661
path: path,
664662
requestStream: true,
@@ -673,7 +671,7 @@ Client.prototype.makeBidiStreamRequest = function(path, serialize,
673671
var intercepting_call = client_interceptors.getInterceptingCall(
674672
method_definition,
675673
options,
676-
constructor_interceptors,
674+
Client.prototype.resolveCallInterceptors.call(this, method_definition, options.interceptors, options.interceptor_providers),
677675
this.$channel,
678676
emitter
679677
);

packages/grpc-native-core/src/client_interceptors.js

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -367,32 +367,6 @@ var resolveInterceptorProviders = function(providers, method_definition) {
367367
return interceptors;
368368
};
369369

370-
/**
371-
* Resolves interceptor options at call invocation time
372-
* @param {grpc.Client~CallOptions} options The call options passed to a gRPC
373-
* call.
374-
* @param {Interceptor[]} [options.interceptors]
375-
* @param {InterceptorProvider[]} [options.interceptor_providers]
376-
* @param {grpc~MethodDefinition} method_definition
377-
* @return {null|function[]}
378-
*/
379-
var resolveInterceptorOptions = function(options, method_definition) {
380-
var provided = resolveInterceptorProviders(options.interceptor_providers,
381-
method_definition);
382-
if (_.isArray(options.interceptors) && _.isArray(provided)) {
383-
throw new InterceptorConfigurationError(
384-
'Both interceptors and interceptor_providers were passed as options ' +
385-
'to the call invocation. Only one of these is allowed.');
386-
}
387-
if (_.isArray(options.interceptors)) {
388-
return options.interceptors;
389-
}
390-
if (_.isArray(provided)) {
391-
return provided;
392-
}
393-
return null;
394-
};
395-
396370
/**
397371
* A chainable gRPC call proxy which will delegate to an optional requester
398372
* object. By default, interceptor methods will chain to next_call. If a
@@ -1360,17 +1334,12 @@ function getLastListener(method_definition, emitter, callback) {
13601334
*
13611335
* @param {grpc~MethodDefinition} method_definition
13621336
* @param {grpc.Client~CallOptions} options
1363-
* @param {Interceptor[]} constructor_interceptors
1337+
* @param {Interceptor[]} interceptors
13641338
* @param {grpc.Channel} channel
13651339
* @param {function|EventEmitter} responder
13661340
*/
13671341
function getInterceptingCall(method_definition, options,
1368-
constructor_interceptors, channel, responder) {
1369-
var interceptors = _processInterceptorLayers(
1370-
options,
1371-
constructor_interceptors,
1372-
method_definition
1373-
);
1342+
interceptors, channel, responder) {
13741343
var last_interceptor = _getLastInterceptor(method_definition, channel,
13751344
responder);
13761345
var all_interceptors = interceptors.concat(last_interceptor);
@@ -1416,29 +1385,6 @@ function _buildChain(interceptors, options) {
14161385
return new InterceptingCall(chain);
14171386
}
14181387

1419-
/**
1420-
* Process call options and the interceptor override layers to get the final set
1421-
* of interceptors.
1422-
* @private
1423-
* @param {grpc.Client~CallOptions} call_options The options passed to the gRPC
1424-
* call.
1425-
* @param {Interceptor[]} constructor_interceptors Interceptors passed to the
1426-
* client constructor.
1427-
* @param {grpc~MethodDefinition} method_definition Details of the RPC method.
1428-
* @return {Interceptor[]|null} The final set of interceptors.
1429-
*/
1430-
function _processInterceptorLayers(call_options,
1431-
constructor_interceptors,
1432-
method_definition) {
1433-
var calltime_interceptors = resolveInterceptorOptions(call_options,
1434-
method_definition);
1435-
var interceptor_overrides = [
1436-
calltime_interceptors,
1437-
constructor_interceptors
1438-
];
1439-
return _resolveInterceptorOverrides(interceptor_overrides);
1440-
}
1441-
14421388
/**
14431389
* Wraps a plain listener object in an InterceptingListener if it isn't an
14441390
* InterceptingListener already.
@@ -1464,23 +1410,6 @@ function _isInterceptingListener(listener) {
14641410
return listener && listener.constructor.name === 'InterceptingListener';
14651411
}
14661412

1467-
/**
1468-
* Chooses the first valid array of interceptors or returns null.
1469-
* @param {Interceptor[][]} interceptor_lists A list of interceptor lists in
1470-
* descending override priority order.
1471-
* @return {Interceptor[]|null} The resulting interceptors
1472-
* @private
1473-
*/
1474-
function _resolveInterceptorOverrides(interceptor_lists) {
1475-
for (var i = 0; i < interceptor_lists.length; i++) {
1476-
var interceptor_list = interceptor_lists[i];
1477-
if (_.isArray(interceptor_list)) {
1478-
return interceptor_list;
1479-
}
1480-
}
1481-
return null;
1482-
}
1483-
14841413
exports.resolveInterceptorProviders = resolveInterceptorProviders;
14851414

14861415
exports.InterceptingCall = InterceptingCall;

0 commit comments

Comments
 (0)