From 019d030ed1c6cdb0247d9e377874a71a8952dd27 Mon Sep 17 00:00:00 2001 From: Mohamad mehdi Kharatizadeh Date: Tue, 6 Mar 2018 01:32:55 +0330 Subject: [PATCH] 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. --- package.json | 1 + packages/grpc-native-core/src/client.js | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 5310e7f5c..4e6c8149e 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ } ], "dependencies": { + "is-function": "^1.0.1", "run-sequence": "^2.2.0", "symlink": "^2.1.0" } diff --git a/packages/grpc-native-core/src/client.js b/packages/grpc-native-core/src/client.js index 79868df46..2375b1315 100644 --- a/packages/grpc-native-core/src/client.js +++ b/packages/grpc-native-core/src/client.js @@ -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; @@ -429,7 +430,7 @@ 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 = {}; @@ -437,7 +438,7 @@ Client.prototype.makeUnaryRequest = function(path, serialize, deserialize, options = metadata; metadata = new Metadata(); } - } else if (metadata instanceof Function) { + } else if (isFunction(metadata)) { callback = metadata; metadata = new Metadata(); options = {}; @@ -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'); } @@ -508,7 +509,7 @@ 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 = {}; @@ -516,7 +517,7 @@ Client.prototype.makeClientStreamRequest = function(path, serialize, options = metadata; metadata = new Metadata(); } - } else if (metadata instanceof Function) { + } else if (isFunction(metadata)) { callback = metadata; metadata = new Metadata(); options = {}; @@ -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'); }