From bb24b1dfccdc52ce6d4b0c509d9c2e57eb055014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Tue, 31 Jul 2018 10:25:56 +0200 Subject: [PATCH 01/31] indentation --- lib/cartodb/backends/template_maps.js | 34 +++++++++++++-------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 75a482fe..7a7e38a0 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -73,23 +73,23 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { var that = this; var db = that.db_signatures; - step( - function getRedisClient() { - that.redis_pool.acquire(db, this); - }, - function executeQuery(err, data) { - assert.ifError(err); - redisClient = data; - redisArgs.push(this); - redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); - }, - function releaseRedisClient(err, data) { - if ( ! _.isUndefined(redisClient) ) { - that.redis_pool.release(db, redisClient); - } - callback(err, data); - } - ); + step( + function getRedisClient() { + that.redis_pool.acquire(db, this); + }, + function executeQuery(err, data) { + assert.ifError(err); + redisClient = data; + redisArgs.push(this); + redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); + }, + function releaseRedisClient(err, data) { + if ( ! _.isUndefined(redisClient) ) { + that.redis_pool.release(db, redisClient); + } + callback(err, data); + } + ); }; var _reValidNameIdentifier = /^[a-z0-9][0-9a-z_\-]*$/i; From 23ef884e9b3b033b4559d8620fcbc540c1e8a78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Tue, 31 Jul 2018 10:26:46 +0200 Subject: [PATCH 02/31] remove step 1 --- lib/cartodb/backends/template_maps.js | 30 +++++++++++++-------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 7a7e38a0..8d4008e6 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -73,23 +73,21 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { var that = this; var db = that.db_signatures; - step( - function getRedisClient() { - that.redis_pool.acquire(db, this); - }, - function executeQuery(err, data) { - assert.ifError(err); - redisClient = data; - redisArgs.push(this); - redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); - }, - function releaseRedisClient(err, data) { - if ( ! _.isUndefined(redisClient) ) { - that.redis_pool.release(db, redisClient); - } - callback(err, data); + function getRedisClient() { + that.redis_pool.acquire(db, this); + }, + function executeQuery(err, data) { + assert.ifError(err); + redisClient = data; + redisArgs.push(this); + redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); + }, + function releaseRedisClient(err, data) { + if ( ! _.isUndefined(redisClient) ) { + that.redis_pool.release(db, redisClient); } - ); + callback(err, data); + } }; var _reValidNameIdentifier = /^[a-z0-9][0-9a-z_\-]*$/i; From 5c0b7487f73d58ad9ab356685b373fb2825d132e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 11:25:55 +0200 Subject: [PATCH 03/31] using calbacks instead of step --- lib/cartodb/backends/template_maps.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 8d4008e6..19c11efe 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -73,21 +73,17 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { var that = this; var db = that.db_signatures; - function getRedisClient() { - that.redis_pool.acquire(db, this); - }, - function executeQuery(err, data) { + that.redis_pool.acquire(db, (err, data) => { assert.ifError(err); redisClient = data; - redisArgs.push(this); + redisArgs.push((err, data) => { + if ( ! _.isUndefined(redisClient) ) { + that.redis_pool.release(db, redisClient); + } + callback(err, data); + }); redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); - }, - function releaseRedisClient(err, data) { - if ( ! _.isUndefined(redisClient) ) { - that.redis_pool.release(db, redisClient); - } - callback(err, data); - } + }); }; var _reValidNameIdentifier = /^[a-z0-9][0-9a-z_\-]*$/i; From 79f2a8dde98284bc3f031317d0f5deb6e88a8f87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 11:30:17 +0200 Subject: [PATCH 04/31] vars refactor: that to this, ... --- lib/cartodb/backends/template_maps.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 19c11efe..907e1c83 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -69,16 +69,11 @@ TemplateMaps.prototype._userTemplateLimit = function() { * @param callback - function to pass results too. */ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { - var redisClient; - var that = this; - var db = that.db_signatures; - - that.redis_pool.acquire(db, (err, data) => { + this.redis_pool.acquire(this.db_signatures, (err, redisClient) => { assert.ifError(err); - redisClient = data; redisArgs.push((err, data) => { if ( ! _.isUndefined(redisClient) ) { - that.redis_pool.release(db, redisClient); + this.redis_pool.release(this.db_signatures, redisClient); } callback(err, data); }); From 5471a218eb5ecc25004ee395956887b2b21a7743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 11:39:07 +0200 Subject: [PATCH 05/31] return errors --- lib/cartodb/backends/template_maps.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 907e1c83..e50f078b 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -70,13 +70,22 @@ TemplateMaps.prototype._userTemplateLimit = function() { */ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { this.redis_pool.acquire(this.db_signatures, (err, redisClient) => { - assert.ifError(err); + if (err) { + return callback(err); + } + redisArgs.push((err, data) => { + if (err) { + return callback(err); + } + if ( ! _.isUndefined(redisClient) ) { this.redis_pool.release(this.db_signatures, redisClient); } - callback(err, data); + + callback(null, data); }); + redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); }); }; From 74831c9b7f8578f04d9c1dece181f2d01c8ae437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 11:48:12 +0200 Subject: [PATCH 06/31] using ... operator instead of apply --- lib/cartodb/backends/template_maps.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index e50f078b..39abd163 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -74,7 +74,7 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { return callback(err); } - redisArgs.push((err, data) => { + redisClient[redisFunc.toUpperCase()](...redisArgs, (err, data) => { if (err) { return callback(err); } @@ -85,8 +85,6 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { callback(null, data); }); - - redisClient[redisFunc.toUpperCase()].apply(redisClient, redisArgs); }); }; From 04faaea10dfc7781e272508cd926221e3e3b2d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 11:49:40 +0200 Subject: [PATCH 07/31] remove unneded check --- lib/cartodb/backends/template_maps.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 39abd163..6b39742f 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -79,10 +79,7 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { return callback(err); } - if ( ! _.isUndefined(redisClient) ) { - this.redis_pool.release(this.db_signatures, redisClient); - } - + this.redis_pool.release(this.db_signatures, redisClient); callback(null, data); }); }); From 750798d0a311e968b2bf698de0b91016183ed729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 12:10:09 +0200 Subject: [PATCH 08/31] ensuring redis_pool connection release --- lib/cartodb/backends/template_maps.js | 68 ++++++++++++--------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 6b39742f..87d2f055 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -75,12 +75,8 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { } redisClient[redisFunc.toUpperCase()](...redisArgs, (err, data) => { - if (err) { - return callback(err); - } - this.redis_pool.release(this.db_signatures, redisClient); - callback(null, data); + return callback(err, data); }); }); }; @@ -205,39 +201,37 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { var userTemplatesKey = this.key_usr_tpl({ owner:owner }); var limit = this._userTemplateLimit(); - step( - function checkLimit() { - if ( ! limit ) { - return 0; - } - self._redisCmd('HLEN', [ userTemplatesKey ], this); - }, - function installTemplateIfDoesNotExist(err, numberOfTemplates) { - assert.ifError(err); - if ( limit && numberOfTemplates >= limit ) { - var limitReachedError = new Error("User '" + owner + "' reached limit on number of templates (" + - numberOfTemplates + "/" + limit + ")"); - limitReachedError.http_status = 409; - throw limitReachedError; - } - self._redisCmd('HSETNX', [ userTemplatesKey, templateName, JSON.stringify(template) ], this); - }, - function validateInstallation(err, wasSet) { - assert.ifError(err); - if ( ! wasSet ) { - throw new Error("Template '" + templateName + "' of user '" + owner + "' already exists"); - } - - return true; - }, - function finish(err) { - if (!err) { - self.emit('add', owner, templateName, template); - } - - callback(err, templateName, template); + function checkLimit() { + if ( ! limit ) { + return 0; } - ); + self._redisCmd('HLEN', [ userTemplatesKey ], this); + }, + function installTemplateIfDoesNotExist(err, numberOfTemplates) { + assert.ifError(err); + if ( limit && numberOfTemplates >= limit ) { + var limitReachedError = new Error("User '" + owner + "' reached limit on number of templates (" + + numberOfTemplates + "/" + limit + ")"); + limitReachedError.http_status = 409; + throw limitReachedError; + } + self._redisCmd('HSETNX', [ userTemplatesKey, templateName, JSON.stringify(template) ], this); + }, + function validateInstallation(err, wasSet) { + assert.ifError(err); + if ( ! wasSet ) { + throw new Error("Template '" + templateName + "' of user '" + owner + "' already exists"); + } + + return true; + }, + function finish(err) { + if (!err) { + self.emit('add', owner, templateName, template); + } + + callback(err, templateName, template); + } }; // Delete a template From fd875c41c7abaea17b9ef9b7b3d380f9f4e05c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 12:20:01 +0200 Subject: [PATCH 09/31] remove step 2 --- lib/cartodb/backends/template_maps.js | 38 +++++++++++---------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 87d2f055..4175eba1 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -201,13 +201,11 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { var userTemplatesKey = this.key_usr_tpl({ owner:owner }); var limit = this._userTemplateLimit(); - function checkLimit() { - if ( ! limit ) { - return 0; - } - self._redisCmd('HLEN', [ userTemplatesKey ], this); - }, - function installTemplateIfDoesNotExist(err, numberOfTemplates) { + if ( ! limit ) { + return 0; + } + + self._redisCmd('HLEN', [ userTemplatesKey ], (err, numberOfTemplates) => { assert.ifError(err); if ( limit && numberOfTemplates >= limit ) { var limitReachedError = new Error("User '" + owner + "' reached limit on number of templates (" + @@ -215,23 +213,19 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { limitReachedError.http_status = 409; throw limitReachedError; } - self._redisCmd('HSETNX', [ userTemplatesKey, templateName, JSON.stringify(template) ], this); - }, - function validateInstallation(err, wasSet) { - assert.ifError(err); - if ( ! wasSet ) { - throw new Error("Template '" + templateName + "' of user '" + owner + "' already exists"); - } + self._redisCmd('HSETNX', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, wasSet) => { + assert.ifError(err); + if ( ! wasSet ) { + throw new Error("Template '" + templateName + "' of user '" + owner + "' already exists"); + } - return true; - }, - function finish(err) { - if (!err) { - self.emit('add', owner, templateName, template); - } + if (!err) { + self.emit('add', owner, templateName, template); + } - callback(err, templateName, template); - } + callback(err, templateName, template); + }); + }); }; // Delete a template From 410cbd082c37b77b0a9d19c4a280354d1fde05b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 12:26:40 +0200 Subject: [PATCH 10/31] manage errors --- lib/cartodb/backends/template_maps.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 4175eba1..4f100a4f 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -206,23 +206,28 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { } self._redisCmd('HLEN', [ userTemplatesKey ], (err, numberOfTemplates) => { - assert.ifError(err); + if (err) { + return callback(err); + } + if ( limit && numberOfTemplates >= limit ) { - var limitReachedError = new Error("User '" + owner + "' reached limit on number of templates (" + - numberOfTemplates + "/" + limit + ")"); + var limitReachedError = new Error( + `User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})` + ); limitReachedError.http_status = 409; - throw limitReachedError; + return callback(limitReachedError); } self._redisCmd('HSETNX', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, wasSet) => { - assert.ifError(err); - if ( ! wasSet ) { - throw new Error("Template '" + templateName + "' of user '" + owner + "' already exists"); + if (err) { + return callback(err); } - if (!err) { - self.emit('add', owner, templateName, template); + if (!wasSet) { + const ownerExistsError = new Error(`Template '${templateName}' of user '${owner}' already exists`); + return callback(ownerExistsError); } + self.emit('add', owner, templateName, template); callback(err, templateName, template); }); }); From d45d0018d20462ec1bfa2aaecf8d45159923553d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 12:32:20 +0200 Subject: [PATCH 11/31] self to this --- lib/cartodb/backends/template_maps.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 4f100a4f..9e17cad4 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -188,8 +188,6 @@ function templateDefaults(template) { // Return template identifier (only valid for given user) // TemplateMaps.prototype.addTemplate = function(owner, template, callback) { - var self = this; - template = templateDefaults(template); var invalidError = this._checkInvalidTemplate(template); @@ -197,7 +195,6 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { return callback(invalidError); } - var templateName = template.name; var userTemplatesKey = this.key_usr_tpl({ owner:owner }); var limit = this._userTemplateLimit(); @@ -205,7 +202,7 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { return 0; } - self._redisCmd('HLEN', [ userTemplatesKey ], (err, numberOfTemplates) => { + this._redisCmd('HLEN', [ userTemplatesKey ], (err, numberOfTemplates) => { if (err) { return callback(err); } @@ -217,18 +214,19 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { limitReachedError.http_status = 409; return callback(limitReachedError); } - self._redisCmd('HSETNX', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, wasSet) => { + + this._redisCmd('HSETNX', [ userTemplatesKey, template.name, JSON.stringify(template) ], (err, wasSet) => { if (err) { return callback(err); } if (!wasSet) { - const ownerExistsError = new Error(`Template '${templateName}' of user '${owner}' already exists`); + var ownerExistsError = new Error(`Template '${template.name}' of user '${owner}' already exists`); return callback(ownerExistsError); } - self.emit('add', owner, templateName, template); - callback(err, templateName, template); + this.emit('add', owner, template.name, template); + callback(err, template.name, template); }); }); }; From cec9994addee0be179efe06aafd417f69f4f3b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 12:33:59 +0200 Subject: [PATCH 12/31] style details --- lib/cartodb/backends/template_maps.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 9e17cad4..dc439a4e 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -191,23 +191,23 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { template = templateDefaults(template); var invalidError = this._checkInvalidTemplate(template); - if ( invalidError ) { + if (invalidError) { return callback(invalidError); } - var userTemplatesKey = this.key_usr_tpl({ owner:owner }); + var userTemplatesKey = this.key_usr_tpl({ owner }); var limit = this._userTemplateLimit(); - if ( ! limit ) { + if (!limit) { return 0; } - this._redisCmd('HLEN', [ userTemplatesKey ], (err, numberOfTemplates) => { + this._redisCmd('HLEN', [userTemplatesKey], (err, numberOfTemplates) => { if (err) { return callback(err); } - if ( limit && numberOfTemplates >= limit ) { + if (limit && numberOfTemplates >= limit) { var limitReachedError = new Error( `User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})` ); @@ -215,7 +215,7 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { return callback(limitReachedError); } - this._redisCmd('HSETNX', [ userTemplatesKey, template.name, JSON.stringify(template) ], (err, wasSet) => { + this._redisCmd('HSETNX', [userTemplatesKey, template.name, JSON.stringify(template)], (err, wasSet) => { if (err) { return callback(err); } From 09a67871fb88a2b134558545ddece5d811cdca1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 12:38:50 +0200 Subject: [PATCH 13/31] manage JSON.stringify sync error --- lib/cartodb/backends/template_maps.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index dc439a4e..cdfaf638 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -215,7 +215,14 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { return callback(limitReachedError); } - this._redisCmd('HSETNX', [userTemplatesKey, template.name, JSON.stringify(template)], (err, wasSet) => { + let templateString; + try { + templateString = JSON.stringify(template); + } catch (error) { + return callback(error); + } + + this._redisCmd('HSETNX', [userTemplatesKey, template.name, templateString], (err, wasSet) => { if (err) { return callback(err); } From f27d5ba7d1a619f9d6b895fd60932ee5b67913cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 12:40:22 +0200 Subject: [PATCH 14/31] unneeded check --- lib/cartodb/backends/template_maps.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index cdfaf638..bb7d5485 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -207,7 +207,7 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { return callback(err); } - if (limit && numberOfTemplates >= limit) { + if (numberOfTemplates >= limit) { var limitReachedError = new Error( `User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})` ); From 2134bf898aaaab50fbe4a0da764446342afe8388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 13:13:25 +0200 Subject: [PATCH 15/31] check user template limit in the right way --- lib/cartodb/backends/template_maps.js | 46 +++++++++++++++++++-------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index bb7d5485..7ba0b13f 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -173,6 +173,37 @@ function templateDefaults(template) { }); } +/** + * Checks if the if the user reaches the templetes limit + * + * @param userTemplatesKey user templat key in Redis + * @param owner cartodb username of the template owner + * @param callback returns error if the user reaches the limit + */ +TemplateMaps.prototype._checkUserTemplatesLimit = function(userTemplatesKey, owner, callback) { + const limit = this._userTemplateLimit(); + + if(!limit) { + return callback(null); + } + + this._redisCmd('HLEN', [userTemplatesKey], (err, numberOfTemplates) => { + if (err) { + return callback(err); + } + + if (numberOfTemplates >= limit) { + const limitReachedError = new Error( + `User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})` + ); + limitReachedError.http_status = 409; + return callback(limitReachedError); + } + + return callback(null); + }); +}; + //--------------- PUBLIC API ------------------------------------- // Add a template @@ -196,25 +227,12 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { } var userTemplatesKey = this.key_usr_tpl({ owner }); - var limit = this._userTemplateLimit(); - if (!limit) { - return 0; - } - - this._redisCmd('HLEN', [userTemplatesKey], (err, numberOfTemplates) => { + this._checkUserTemplatesLimit(userTemplatesKey, owner, err => { if (err) { return callback(err); } - if (numberOfTemplates >= limit) { - var limitReachedError = new Error( - `User '${owner}' reached limit on number of templates (${numberOfTemplates}/${limit})` - ); - limitReachedError.http_status = 409; - return callback(limitReachedError); - } - let templateString; try { templateString = JSON.stringify(template); From da8d92b78ebadff880f523634c29af191bfde398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 13:20:10 +0200 Subject: [PATCH 16/31] remove step 3 --- lib/cartodb/backends/template_maps.js | 28 +++++++++++---------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 7ba0b13f..cb0cd4c9 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -267,25 +267,19 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { // TemplateMaps.prototype.delTemplate = function(owner, tpl_id, callback) { var self = this; - step( - function deleteTemplate() { - self._redisCmd('HDEL', [ self.key_usr_tpl({ owner:owner }), tpl_id ], this); - }, - function handleDeletion(err, deleted) { - assert.ifError(err); - if (!deleted) { - throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist"); - } - return true; - }, - function finish(err) { - if (!err) { - self.emit('delete', owner, tpl_id); - } - callback(err); + self._redisCmd('HDEL', [ self.key_usr_tpl({ owner:owner }), tpl_id ], (err, deleted) => { + assert.ifError(err); + if (!deleted) { + throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist"); } - ); + + if (!err) { + self.emit('delete', owner, tpl_id); + } + + return callback(err); + }); }; // Update a template From 085d26f1b2aecf47507c176baed059701b297a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 13:27:07 +0200 Subject: [PATCH 17/31] remove step 3: refactor --- lib/cartodb/backends/template_maps.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index cb0cd4c9..cf62a8cb 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -266,19 +266,17 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { // @param callback function(err) // TemplateMaps.prototype.delTemplate = function(owner, tpl_id, callback) { - var self = this; + this._redisCmd('HDEL', [ this.key_usr_tpl({ owner:owner }), tpl_id ], (err, deleted) => { + if (err) { + return callback(err); + } - self._redisCmd('HDEL', [ self.key_usr_tpl({ owner:owner }), tpl_id ], (err, deleted) => { - assert.ifError(err); if (!deleted) { - throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist"); + return callback(new Error(`Template '${tpl_id}' of user '${owner}' does not exist`)); } - if (!err) { - self.emit('delete', owner, tpl_id); - } - - return callback(err); + this.emit('delete', owner, tpl_id); + return callback(); }); }; From e9f88a78d5c8e3cf42981c53757f72cd065a71f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:28:15 +0200 Subject: [PATCH 18/31] remove step 4 --- lib/cartodb/backends/template_maps.js | 29 ++++++++++----------------- 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index cf62a8cb..b31e5fb5 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -318,26 +318,19 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) var previousTemplate = null; - step( - function getExistingTemplate() { - self._redisCmd('HGET', [ userTemplatesKey, tpl_id ], this); - }, - function updateTemplate(err, _currentTemplate) { - assert.ifError(err); - if (!_currentTemplate) { - throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist"); - } - previousTemplate = _currentTemplate; - self._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], this); - }, - function handleTemplateUpdate(err, didSetNewField) { + + self._redisCmd('HGET', [ userTemplatesKey, tpl_id ], (err, _currentTemplate) => { + assert.ifError(err); + if (!_currentTemplate) { + throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist"); + } + previousTemplate = _currentTemplate; + self._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, didSetNewField) => { assert.ifError(err); if (didSetNewField) { debug('New template created on update operation'); } - return true; - }, - function finish(err) { + if (!err) { if (self.fingerPrint(JSON.parse(previousTemplate)) !== self.fingerPrint(template)) { self.emit('update', owner, templateName, template); @@ -345,8 +338,8 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) } callback(err, template); - } - ); + }); + }); }; // List user templates From bcad6dbe2240a693c34629b0d158e30452dbd1ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:32:44 +0200 Subject: [PATCH 19/31] anage errors --- lib/cartodb/backends/template_maps.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index b31e5fb5..9ba54837 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -320,24 +320,29 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) self._redisCmd('HGET', [ userTemplatesKey, tpl_id ], (err, _currentTemplate) => { - assert.ifError(err); - if (!_currentTemplate) { - throw new Error("Template '" + tpl_id + "' of user '" + owner + "' does not exist"); + if (err) { + return callback(err); } + + if (!_currentTemplate) { + return callback(new Error(`Template '${tpl_id}' of user '${owner}' does not exist`)); + } + previousTemplate = _currentTemplate; self._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, didSetNewField) => { - assert.ifError(err); + if (err) { + return callback(err); + } + if (didSetNewField) { debug('New template created on update operation'); } - if (!err) { - if (self.fingerPrint(JSON.parse(previousTemplate)) !== self.fingerPrint(template)) { - self.emit('update', owner, templateName, template); - } + if (self.fingerPrint(JSON.parse(previousTemplate)) !== self.fingerPrint(template)) { + self.emit('update', owner, templateName, template); } - callback(err, template); + return callback(err, template); }); }); }; From e85f4e4129f42c972f7979d146b424519db81975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:39:25 +0200 Subject: [PATCH 20/31] selft to this --- lib/cartodb/backends/template_maps.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 9ba54837..1151bc46 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -298,8 +298,6 @@ TemplateMaps.prototype.delTemplate = function(owner, tpl_id, callback) { // TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) { - var self = this; - template = templateDefaults(template); var invalidError = this._checkInvalidTemplate(template); @@ -319,7 +317,7 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) var previousTemplate = null; - self._redisCmd('HGET', [ userTemplatesKey, tpl_id ], (err, _currentTemplate) => { + this._redisCmd('HGET', [ userTemplatesKey, tpl_id ], (err, _currentTemplate) => { if (err) { return callback(err); } @@ -329,7 +327,7 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) } previousTemplate = _currentTemplate; - self._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, didSetNewField) => { + this._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, didSetNewField) => { if (err) { return callback(err); } @@ -338,8 +336,8 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) debug('New template created on update operation'); } - if (self.fingerPrint(JSON.parse(previousTemplate)) !== self.fingerPrint(template)) { - self.emit('update', owner, templateName, template); + if (this.fingerPrint(JSON.parse(previousTemplate)) !== this.fingerPrint(template)) { + this.emit('update', owner, templateName, template); } return callback(err, template); From 2a2f703abce4476d039af398205b5b120a4f4f4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:40:40 +0200 Subject: [PATCH 21/31] style details --- lib/cartodb/backends/template_maps.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 1151bc46..8bec7c36 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -301,23 +301,20 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) template = templateDefaults(template); var invalidError = this._checkInvalidTemplate(template); - - if ( invalidError ) { + if (invalidError) { return callback(invalidError); } var templateName = template.name; - if ( tpl_id !== templateName ) { + if (tpl_id !== templateName) { return callback(new Error("Cannot update name of a map template ('" + tpl_id + "' != '" + templateName + "')")); } - var userTemplatesKey = this.key_usr_tpl({ owner:owner }); - + var userTemplatesKey = this.key_usr_tpl({ owner }); var previousTemplate = null; - - this._redisCmd('HGET', [ userTemplatesKey, tpl_id ], (err, _currentTemplate) => { + this._redisCmd('HGET', [userTemplatesKey, tpl_id], (err, _currentTemplate) => { if (err) { return callback(err); } @@ -327,7 +324,7 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) } previousTemplate = _currentTemplate; - this._redisCmd('HSET', [ userTemplatesKey, templateName, JSON.stringify(template) ], (err, didSetNewField) => { + this._redisCmd('HSET', [userTemplatesKey, templateName, JSON.stringify(template)], (err, didSetNewField) => { if (err) { return callback(err); } From 7b7bee2901a7dfbf42a9a9d482d7c65fc6160df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:43:45 +0200 Subject: [PATCH 22/31] improve naming --- lib/cartodb/backends/template_maps.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 8bec7c36..d6ab6c46 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -305,26 +305,22 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) return callback(invalidError); } - var templateName = template.name; - - if (tpl_id !== templateName) { - return callback(new Error("Cannot update name of a map template ('" + tpl_id + "' != '" + templateName + "')")); + if (tpl_id !== template.name) { + return callback(new Error("Cannot update name of a map template ('" + tpl_id + "' != '" + template.name + "')")); } var userTemplatesKey = this.key_usr_tpl({ owner }); - var previousTemplate = null; - this._redisCmd('HGET', [userTemplatesKey, tpl_id], (err, _currentTemplate) => { + this._redisCmd('HGET', [userTemplatesKey, tpl_id], (err, beforeUpdateTemplate) => { if (err) { return callback(err); } - if (!_currentTemplate) { + if (!beforeUpdateTemplate) { return callback(new Error(`Template '${tpl_id}' of user '${owner}' does not exist`)); } - previousTemplate = _currentTemplate; - this._redisCmd('HSET', [userTemplatesKey, templateName, JSON.stringify(template)], (err, didSetNewField) => { + this._redisCmd('HSET', [userTemplatesKey, template.name, JSON.stringify(template)], (err, didSetNewField) => { if (err) { return callback(err); } @@ -333,8 +329,8 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) debug('New template created on update operation'); } - if (this.fingerPrint(JSON.parse(previousTemplate)) !== this.fingerPrint(template)) { - this.emit('update', owner, templateName, template); + if (this.fingerPrint(JSON.parse(beforeUpdateTemplate)) !== this.fingerPrint(template)) { + this.emit('update', owner, template.name, template); } return callback(err, template); From 0e28348e160c5fa1122b1348691caeff5d79fc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:48:24 +0200 Subject: [PATCH 23/31] manage JSON parse and stringify sinc errors --- lib/cartodb/backends/template_maps.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index d6ab6c46..ac1308cd 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -297,7 +297,6 @@ TemplateMaps.prototype.delTemplate = function(owner, tpl_id, callback) { // @param callback function(err) // TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) { - template = templateDefaults(template); var invalidError = this._checkInvalidTemplate(template); @@ -320,7 +319,14 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) return callback(new Error(`Template '${tpl_id}' of user '${owner}' does not exist`)); } - this._redisCmd('HSET', [userTemplatesKey, template.name, JSON.stringify(template)], (err, didSetNewField) => { + let templateString; + try { + templateString = JSON.stringify(template) + } catch (error) { + return callback(error); + } + + this._redisCmd('HSET', [userTemplatesKey, template.name, templateString], (err, didSetNewField) => { if (err) { return callback(err); } @@ -329,7 +335,14 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) debug('New template created on update operation'); } - if (this.fingerPrint(JSON.parse(beforeUpdateTemplate)) !== this.fingerPrint(template)) { + let beforeUpdateTemplateObject; + try { + beforeUpdateTemplateObject = JSON.parse(beforeUpdateTemplate) + } catch (error) { + return callback(error); + } + + if (this.fingerPrint(beforeUpdateTemplateObject) !== this.fingerPrint(template)) { this.emit('update', owner, template.name, template); } From e5b75abc762280e24c2d523be45a2167e18565f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:53:41 +0200 Subject: [PATCH 24/31] some details --- lib/cartodb/backends/template_maps.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index ac1308cd..1e8dc51e 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -305,7 +305,7 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) } if (tpl_id !== template.name) { - return callback(new Error("Cannot update name of a map template ('" + tpl_id + "' != '" + template.name + "')")); + return callback(new Error(`Cannot update name of a map template ('${tpl_id}' != '${template.name}')`)); } var userTemplatesKey = this.key_usr_tpl({ owner }); @@ -321,7 +321,7 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) let templateString; try { - templateString = JSON.stringify(template) + templateString = JSON.stringify(template); } catch (error) { return callback(error); } @@ -337,7 +337,7 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) let beforeUpdateTemplateObject; try { - beforeUpdateTemplateObject = JSON.parse(beforeUpdateTemplate) + beforeUpdateTemplateObject = JSON.parse(beforeUpdateTemplate); } catch (error) { return callback(error); } From b169c96f1c87d05ec9225114ea152c25abc0b055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 15:57:22 +0200 Subject: [PATCH 25/31] remove step 5 --- lib/cartodb/backends/template_maps.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 1e8dc51e..3e555fb0 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -374,18 +374,10 @@ TemplateMaps.prototype.listTemplates = function(owner, callback) { // TemplateMaps.prototype.getTemplate = function(owner, tpl_id, callback) { var self = this; - step( - function getTemplate() { - self._redisCmd('HGET', [ self.key_usr_tpl({owner:owner}), tpl_id ], this); - }, - function parseTemplate(err, tpl_val) { - assert.ifError(err); - return JSON.parse(tpl_val); - }, - function finish(err, tpl) { - callback(err, tpl); - } - ); + self._redisCmd('HGET', [ self.key_usr_tpl({owner:owner}), tpl_id ], (err, tpl_val) => { + assert.ifError(err); + return callback(err, JSON.parse(tpl_val)); + }); }; TemplateMaps.prototype.isAuthorized = function(template, authTokens) { From 3bb4ad86ff2655fce6e931976723b1e872bf6e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 16:00:31 +0200 Subject: [PATCH 26/31] remove template 5 refactor --- lib/cartodb/backends/template_maps.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 3e555fb0..ea639084 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -373,10 +373,19 @@ TemplateMaps.prototype.listTemplates = function(owner, callback) { // Return full template definition // TemplateMaps.prototype.getTemplate = function(owner, tpl_id, callback) { - var self = this; - self._redisCmd('HGET', [ self.key_usr_tpl({owner:owner}), tpl_id ], (err, tpl_val) => { - assert.ifError(err); - return callback(err, JSON.parse(tpl_val)); + this._redisCmd('HGET', [this.key_usr_tpl({owner:owner}), tpl_id], (err, template) => { + if (err) { + return callback(err); + } + + let templateObject; + try { + templateObject = JSON.parse(template); + } catch (error) { + return callback(error); + } + + return callback(null, templateObject); }); }; From efafd4cb3e69eaef797106327095d7f5b7ffbf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 16:02:53 +0200 Subject: [PATCH 27/31] success callback without err --- lib/cartodb/backends/template_maps.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index ea639084..17ed8f6e 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -184,7 +184,7 @@ TemplateMaps.prototype._checkUserTemplatesLimit = function(userTemplatesKey, own const limit = this._userTemplateLimit(); if(!limit) { - return callback(null); + return callback(); } this._redisCmd('HLEN', [userTemplatesKey], (err, numberOfTemplates) => { @@ -200,7 +200,7 @@ TemplateMaps.prototype._checkUserTemplatesLimit = function(userTemplatesKey, own return callback(limitReachedError); } - return callback(null); + return callback(); }); }; @@ -251,7 +251,7 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { } this.emit('add', owner, template.name, template); - callback(err, template.name, template); + callback(null, template.name, template); }); }); }; @@ -346,7 +346,7 @@ TemplateMaps.prototype.updTemplate = function(owner, tpl_id, template, callback) this.emit('update', owner, template.name, template); } - return callback(err, template); + return callback(null, template); }); }); }; From 01658c33fd531fc99fd8adc42ce50f9b863e0fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 16:03:36 +0200 Subject: [PATCH 28/31] remove step and assert dependencies --- lib/cartodb/backends/template_maps.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 17ed8f6e..e362be3b 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -1,7 +1,5 @@ -var assert = require('assert'); var crypto = require('crypto'); var debug = require('debug')('windshaft:templates'); -var step = require('step'); var _ = require('underscore'); var dot = require('dot'); From 44ba5aa5688969f214cf4e0cdad5e7e77599bd80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 1 Aug 2018 16:22:43 +0200 Subject: [PATCH 29/31] forgotten return --- lib/cartodb/backends/template_maps.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index e362be3b..e2c53e8c 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -249,7 +249,7 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { } this.emit('add', owner, template.name, template); - callback(null, template.name, template); + return callback(null, template.name, template); }); }); }; From a910f1442ef85baae53bfd50ef2c8ed459526bc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Thu, 2 Aug 2018 11:35:54 +0200 Subject: [PATCH 30/31] explicit error return --- lib/cartodb/backends/template_maps.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index e2c53e8c..00f0cd2e 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -74,7 +74,10 @@ TemplateMaps.prototype._redisCmd = function(redisFunc, redisArgs, callback) { redisClient[redisFunc.toUpperCase()](...redisArgs, (err, data) => { this.redis_pool.release(this.db_signatures, redisClient); - return callback(err, data); + if (err) { + return callback(err); + } + return callback(null, data); }); }); }; From 6c8f38a2412f04e389983bb4e5c2afc119029213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Thu, 2 Aug 2018 11:38:54 +0200 Subject: [PATCH 31/31] improve var naming --- lib/cartodb/backends/template_maps.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cartodb/backends/template_maps.js b/lib/cartodb/backends/template_maps.js index 00f0cd2e..41e655bd 100644 --- a/lib/cartodb/backends/template_maps.js +++ b/lib/cartodb/backends/template_maps.js @@ -247,8 +247,8 @@ TemplateMaps.prototype.addTemplate = function(owner, template, callback) { } if (!wasSet) { - var ownerExistsError = new Error(`Template '${template.name}' of user '${owner}' already exists`); - return callback(ownerExistsError); + var templateExistsError = new Error(`Template '${template.name}' of user '${owner}' already exists`); + return callback(templateExistsError); } this.emit('add', owner, template.name, template);