Skip to content

Commit d82b1a3

Browse files
authored
Merge pull request #1761 from zereraz/master
grpc-js: stricter function check than instanceof
2 parents d35fa3a + 923b44b commit d82b1a3

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

packages/grpc-js/src/client.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ const INTERCEPTOR_SYMBOL = Symbol();
5555
const INTERCEPTOR_PROVIDER_SYMBOL = Symbol();
5656
const CALL_INVOCATION_TRANSFORMER_SYMBOL = Symbol();
5757

58+
function isFunction<ResponseType>(arg: Metadata | CallOptions | UnaryCallback<ResponseType> | undefined): arg is UnaryCallback<ResponseType>{
59+
return Object.prototype.toString.call(arg) === '[object Function]'
60+
}
61+
5862
export interface UnaryCallback<ResponseType> {
5963
(err: ServiceError | null, value?: ResponseType): void;
6064
}
@@ -198,9 +202,9 @@ export class Client {
198202
options: CallOptions;
199203
callback: UnaryCallback<ResponseType>;
200204
} {
201-
if (arg1 instanceof Function) {
205+
if (isFunction(arg1)) {
202206
return { metadata: new Metadata(), options: {}, callback: arg1 };
203-
} else if (arg2 instanceof Function) {
207+
} else if (isFunction(arg2)) {
204208
if (arg1 instanceof Metadata) {
205209
return { metadata: arg1, options: {}, callback: arg2 };
206210
} else {
@@ -211,7 +215,7 @@ export class Client {
211215
!(
212216
arg1 instanceof Metadata &&
213217
arg2 instanceof Object &&
214-
arg3 instanceof Function
218+
isFunction(arg3)
215219
)
216220
) {
217221
throw new Error('Incorrect arguments passed');
@@ -671,3 +675,4 @@ export class Client {
671675
return stream;
672676
}
673677
}
678+

0 commit comments

Comments
 (0)