Skip to content

Commit

Permalink
stronger checking for functions in client.js
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Mohamad mehdi Kharatizadeh committed Mar 5, 2018
1 parent ed2a048 commit 1106277
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
}
],
"dependencies": {
"is-function": "^1.0.1",
"run-sequence": "^2.2.0",
"symlink": "^2.1.0"
}
Expand Down
13 changes: 7 additions & 6 deletions packages/grpc-native-core/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var constants = require('./constants');
var EventEmitter = require('events').EventEmitter;

var stream = require('stream');
var isFunction = require('is-function');

var Readable = stream.Readable;
var Writable = stream.Writable;
Expand Down Expand Up @@ -429,15 +430,15 @@ exports.Client = Client;
Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
argument, metadata, options,
callback) {
if (options instanceof Function) {
if (isFunction(options)) {
callback = options;
if (metadata instanceof Metadata) {
options = {};
} else {
options = metadata;
metadata = new Metadata();
}
} else if (metadata instanceof Function) {
} else if (isFunction(metadata)) {
callback = metadata;
metadata = new Metadata();
options = {};
Expand All @@ -450,7 +451,7 @@ Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
}
if (!((metadata instanceof Metadata) &&
(options instanceof Object) &&
(callback instanceof Function))) {
(isFunction(callback)))) {
throw new Error('Argument mismatch in makeUnaryRequest');
}

Expand Down Expand Up @@ -508,15 +509,15 @@ Client.prototype.makeUnaryRequest = function(path, serialize, deserialize,
Client.prototype.makeClientStreamRequest = function(path, serialize,
deserialize, metadata,
options, callback) {
if (options instanceof Function) {
if (isFunction(options)) {
callback = options;
if (metadata instanceof Metadata) {
options = {};
} else {
options = metadata;
metadata = new Metadata();
}
} else if (metadata instanceof Function) {
} else if (isFunction(metadata)) {
callback = metadata;
metadata = new Metadata();
options = {};
Expand All @@ -529,7 +530,7 @@ Client.prototype.makeClientStreamRequest = function(path, serialize,
}
if (!((metadata instanceof Metadata) &&
(options instanceof Object) &&
(callback instanceof Function))) {
(isFunction(callback)))) {
throw new Error('Argument mismatch in makeClientStreamRequest');
}

Expand Down

0 comments on commit 1106277

Please sign in to comment.