Skip to content

Commit 1106277

Browse files
author
Mohamad mehdi Kharatizadeh
committed
stronger checking for functions in client.js
checking for functions simply by instanceof would render library usesless in vm or REPL contexts. because if client is created in another V8 context, typeof would still return "function" but instanceof Function would fail and return false for functions and arrow functions. thus it would be impossible to create client before starting a REPL context.
1 parent ed2a048 commit 1106277

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
}
4848
],
4949
"dependencies": {
50+
"is-function": "^1.0.1",
5051
"run-sequence": "^2.2.0",
5152
"symlink": "^2.1.0"
5253
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var constants = require('./constants');
4646
var EventEmitter = require('events').EventEmitter;
4747

4848
var stream = require('stream');
49+
var isFunction = require('is-function');
4950

5051
var Readable = stream.Readable;
5152
var Writable = stream.Writable;
@@ -429,15 +430,15 @@ exports.Client = Client;
429430
Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
430431
argument, metadata, options,
431432
callback) {
432-
if (options instanceof Function) {
433+
if (isFunction(options)) {
433434
callback = options;
434435
if (metadata instanceof Metadata) {
435436
options = {};
436437
} else {
437438
options = metadata;
438439
metadata = new Metadata();
439440
}
440-
} else if (metadata instanceof Function) {
441+
} else if (isFunction(metadata)) {
441442
callback = metadata;
442443
metadata = new Metadata();
443444
options = {};
@@ -450,7 +451,7 @@ Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
450451
}
451452
if (!((metadata instanceof Metadata) &&
452453
(options instanceof Object) &&
453-
(callback instanceof Function))) {
454+
(isFunction(callback)))) {
454455
throw new Error('Argument mismatch in makeUnaryRequest');
455456
}
456457

@@ -508,15 +509,15 @@ Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
508509
Client.prototype.makeClientStreamRequest = function(path, serialize,
509510
deserialize, metadata,
510511
options, callback) {
511-
if (options instanceof Function) {
512+
if (isFunction(options)) {
512513
callback = options;
513514
if (metadata instanceof Metadata) {
514515
options = {};
515516
} else {
516517
options = metadata;
517518
metadata = new Metadata();
518519
}
519-
} else if (metadata instanceof Function) {
520+
} else if (isFunction(metadata)) {
520521
callback = metadata;
521522
metadata = new Metadata();
522523
options = {};
@@ -529,7 +530,7 @@ Client.prototype.makeClientStreamRequest = function(path, serialize,
529530
}
530531
if (!((metadata instanceof Metadata) &&
531532
(options instanceof Object) &&
532-
(callback instanceof Function))) {
533+
(isFunction(callback)))) {
533534
throw new Error('Argument mismatch in makeClientStreamRequest');
534535
}
535536

0 commit comments

Comments
 (0)