Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grpc-js: stricter function check than instanceof #1761

Merged
merged 3 commits into from Apr 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/grpc-js/src/client.ts
Expand Up @@ -55,6 +55,10 @@ const INTERCEPTOR_SYMBOL = Symbol();
const INTERCEPTOR_PROVIDER_SYMBOL = Symbol();
const CALL_INVOCATION_TRANSFORMER_SYMBOL = Symbol();

function isFunction<ResponseType>(arg: Metadata | CallOptions | UnaryCallback<ResponseType> | undefined): arg is UnaryCallback<ResponseType>{
return Object.prototype.toString.call(arg) === '[object Function]'
}

export interface UnaryCallback<ResponseType> {
(err: ServiceError | null, value?: ResponseType): void;
}
Expand Down Expand Up @@ -198,9 +202,9 @@ export class Client {
options: CallOptions;
callback: UnaryCallback<ResponseType>;
} {
if (arg1 instanceof Function) {
if (isFunction(arg1)) {
return { metadata: new Metadata(), options: {}, callback: arg1 };
} else if (arg2 instanceof Function) {
} else if (isFunction(arg2)) {
if (arg1 instanceof Metadata) {
return { metadata: arg1, options: {}, callback: arg2 };
} else {
Expand All @@ -211,7 +215,7 @@ export class Client {
!(
arg1 instanceof Metadata &&
arg2 instanceof Object &&
arg3 instanceof Function
isFunction(arg3)
)
) {
throw new Error('Incorrect arguments passed');
Expand Down Expand Up @@ -671,3 +675,4 @@ export class Client {
return stream;
}
}