From fe609f2ee791ca2f687c9dad517d272d5b02dbd6 Mon Sep 17 00:00:00 2001 From: Garrone Joseph Date: Fri, 16 Jun 2017 00:09:47 +0200 Subject: [PATCH] Multiple fix (#48) * Fix 'generic case of multiple copies of a key in an event' * Spaces can mean something, dont remove them ( cf action Command in asterisk 14+ ) * using once we do not need to remove listener so we can remove the timer * Use multiple variable instead of coma separated list * fix issue: when use keepConnected prevent to explicitely disconnect * updating version number and adding garronej to contributor * adding missing contributor in licence --- LICENSE | 14 +++++++++++--- README.md | 19 +++++++++++++------ lib/ami.js | 41 +++++++++++++++++++++++------------------ package.json | 2 +- 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/LICENSE b/LICENSE index c916a02..b97e3db 100644 --- a/LICENSE +++ b/LICENSE @@ -1,11 +1,19 @@ MIT License ----------- -Copyright (C) 2012 by - Philipp Dunkel - Tekay +Copyright (C) 2012 - 2017 by + Philipp Dunkel abroweb + Igor Escobar + Tekay Kofi Hagan + Hugo Chinchilla Carbonell + Nick Mooney + Asp3ctus + Christian Gutierrez + bchavet + Joserwan + Joseph Garrone Based on a work Copyright (C) 2010 Brian White , but radically altered thereafter so as to constitute a new work. diff --git a/README.md b/README.md index 05acaf1..bf99b64 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,8 @@ ami.action({ ``` ## Contributors - * [Philipp Dunkel](https://github.com/phidelta) - * [Igor Escobar]( - Tekay abroweb - Kofi Hagan Igor Escobar - Joserwan https://github.com/joserwan + Tekay + Kofi Hagan + Hugo Chinchilla Carbonell + Nick Mooney + Asp3ctus + Christian Gutierrez + bchavet + Joserwan + Joseph Garrone Based on a work Copyright (C) 2010 Brian White , but radically altered thereafter so as to constitute a new work. diff --git a/lib/ami.js b/lib/ami.js index 35c04c6..5966687 100644 --- a/lib/ami.js +++ b/lib/ami.js @@ -137,10 +137,12 @@ function ManagerReader(context, data) { item = {}; while (lines.length) { line = lines.shift(); - line = line.split(':'); + line = line.split(': '); var key = Utils.removeSpaces(line.shift()).toLowerCase(); - line = Utils.removeSpaces(line.join(':')); + line = line.join(': '); + if (key === 'variable' || key === 'chanvariable') { + // Handle special case of one or more variables attached to an event and // create a variables subobject in the event object if (typeof(item[key]) !== 'object') @@ -155,7 +157,7 @@ function ManagerReader(context, data) { if (Array.isArray(item[key])) item[key].push(line); else - item[key] = [item[key]]; + item[key] = [item[key], line]; } else item[key] = line; } @@ -244,14 +246,17 @@ function ManagerKeepConnected(context) { function MakeManagerAction(req, id) { var msg = []; msg.push('ActionID: ' + id); - msg = msg.concat(Object.keys(req).map(function(key) { + + Object.keys(req).forEach(function (key) { var nkey = Utils.removeSpaces(key).toLowerCase(); if (!nkey.length || ('actionid' == nkey)) return; var nval = req[key]; + nkey = nkey.substr(0, 1).toUpperCase() + nkey.substr(1); + switch (typeof nval) { case 'undefined': return; @@ -262,21 +267,25 @@ function MakeManagerAction(req, id) { return String(e); }).join(','); } else if (nval instanceof RegExp === false) { - nval = Object.keys(nval).map(function(name) { - return [name, nval[name]].join('='); - }).join(','); + Object.keys(nval).forEach( function(name) { + msg.push( nkey + ": " + name + "=" + nval[name].toString() ); + }); + return; } break; default: nval = String(nval); break; } - return [nkey, nval].join(': '); - }).filter(function(line) { - return line ? true : false; - })); + + msg.push([nkey, nval].join(': ')); + + }); + msg.sort(); + return msg.join('\r\n') + '\r\n\r\n'; + } function ManagerAction(context, action, callback) { @@ -322,11 +331,7 @@ function ManagerAction(context, action, callback) { return id; } - this.on(id, callback); - - setTimeout(function(_, id, callback){ - _.removeListener(id, callback); - }, 60000, this, id, callback); + this.once(id, callback); return context.lastid = id; } @@ -334,8 +339,8 @@ function ManagerAction(context, action, callback) { function ManagerDisconnect(context, callback) { - if (context.reconnect) - this.removeListener('on', context.reconnect); + if (this.reconnect) + this.removeListener('close', this.reconnect); if (context.connection && context.connection.readyState === 'open') context.connection.end(); diff --git a/package.json b/package.json index 50a7b5a..629d454 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "web": "https://github.com/igorescobar" } ], - "version":"0.1.15", + "version":"0.1.16", "description":"A node.js module for interacting with the Asterisk Manager API.", "keywords": ["asterisk", "voip", "ami", "asterisk-manager"], "main": "index.js",