From ce44213d656da792644c0d7a093327d6ecce544a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 28 May 2016 14:57:31 +0200 Subject: [PATCH] A function name is only configurable from v8 >= v.4.3 --- changelog.md | 2 +- lib/commands.js | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index 683d9d94c6..a416a04aa9 100644 --- a/changelog.md +++ b/changelog.md @@ -10,7 +10,7 @@ Features - Updated [redis-parser](https://github.com/NodeRedis/redis-parser) dependency ([changelog](https://github.com/NodeRedis/redis-parser/releases/tag/v.2.0.0)) - The JS parser is from now on the new default as it is a lot faster than the hiredis parser - This is no BC as there is no changed behavior for the user at all but just a performance improvement. Explicitly requireing the Hiredis parser is still possible. -- Added name property to all Redis functions +- Added name property to all Redis functions (Node.js >= 4.0) Bugfixes diff --git a/lib/commands.js b/lib/commands.js index 805c3b9a50..0082330471 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -4,6 +4,18 @@ var commands = require('redis-commands'); var Multi = require('./multi'); var RedisClient = require('../').RedisClient; var Command = require('./command'); +// Feature detect if a function may change it's name +var changeFunctionName = (function () { + var fn = function abc () {}; + try { + Object.defineProperty(fn, 'name', { + value: 'foobar' + }); + return true; + } catch (e) { + return false; + } +}()); // TODO: Rewrite this including the invidual commands into a Commands class // that provided a functionality to add new commands to the client @@ -45,9 +57,11 @@ commands.list.forEach(function (command) { } return this.internal_send_command(new Command(command, arr, callback)); }; - Object.defineProperty(RedisClient.prototype[command], 'name', { - value: command - }); + if (changeFunctionName) { + Object.defineProperty(RedisClient.prototype[command], 'name', { + value: command + }); + } } // Do not override existing functions @@ -86,8 +100,10 @@ commands.list.forEach(function (command) { this.queue.push(new Command(command, arr, callback)); return this; }; - Object.defineProperty(Multi.prototype[command], 'name', { - value: command - }); + if (changeFunctionName) { + Object.defineProperty(Multi.prototype[command], 'name', { + value: command + }); + } } });