actionhero-client
Advanced tools
+3
-2
| sudo: false | ||
| language: node_js | ||
| node_js: | ||
| - "4.0" | ||
| - "5.0" | ||
| - "4" | ||
| - "6" | ||
| - "7" |
+43
-39
@@ -1,52 +0,56 @@ | ||
| var actionheroClient = require(__dirname + "/lib/actionhero-client.js"); | ||
| var client = new actionheroClient(); | ||
| var path = require('path') | ||
| client.on("say", function(msgBlock){ | ||
| console.log(" > SAY: " + msgBlock.message + " | from: " + msgBlock.from); | ||
| }); | ||
| var ActionheroClient = require(path.join(__dirname, '/lib/actionhero-client.js')) | ||
| var client = new ActionheroClient() | ||
| client.on("welcome", function(msg){ | ||
| console.log("WELCOME: " + msg); | ||
| }); | ||
| client.on('say', function (msgBlock) { | ||
| console.log(' > SAY: ' + msgBlock.message + ' | from: ' + msgBlock.from) | ||
| }) | ||
| client.on("error", function(err, data){ | ||
| console.log("ERROR: " + err); | ||
| if(data){ console.log(data); } | ||
| }); | ||
| client.on('welcome', function (msg) { | ||
| console.log('WELCOME: ' + msg) | ||
| }) | ||
| client.on("end", function(){ | ||
| console.log("Connection Ended"); | ||
| }); | ||
| client.on('error', function (error, data) { | ||
| console.log('ERROR: ' + error) | ||
| if (data) { console.log(data) } | ||
| }) | ||
| client.on("timeout", function(err, request, caller){ | ||
| console.log(request + " timed out"); | ||
| }); | ||
| client.on('end', function () { | ||
| console.log('Connection Ended') | ||
| }) | ||
| client.on('timeout', function (error, request, caller) { | ||
| if (error) { throw error } | ||
| console.log(request + ' timed out') | ||
| }) | ||
| client.connect({ | ||
| host: "127.0.0.1", | ||
| port: "5000", | ||
| }, function(){ | ||
| host: '127.0.0.1', | ||
| port: '5000' | ||
| }, function () { | ||
| // get details about myself | ||
| console.log(client.details); | ||
| console.log(client.details) | ||
| // try an action | ||
| var params = { key: "mykey", value: "myValue" }; | ||
| client.actionWithParams("cacheTest", params, function(err, apiResponse, delta){ | ||
| console.log("cacheTest action response: " + apiResponse.cacheTestResults.saveResp); | ||
| console.log(" ~ request duration: " + delta + "ms"); | ||
| }); | ||
| var params = { key: 'mykey', value: 'myValue' } | ||
| client.actionWithParams('cacheTest', params, function (error, apiResponse, delta) { | ||
| if (error) { throw error } | ||
| console.log('cacheTest action response: ' + apiResponse.cacheTestResults.saveResp) | ||
| console.log(' ~ request duration: ' + delta + 'ms') | ||
| }) | ||
| // join a chat room and talk | ||
| client.roomAdd("defaultRoom", function(err){ | ||
| client.say("defaultRoom", "Hello from the actionheroClient"); | ||
| client.roomLeave("defaultRoom"); | ||
| }); | ||
| client.roomAdd('defaultRoom', function (error) { | ||
| if (error) { throw error } | ||
| client.say('defaultRoom', 'Hello from the actionheroClient') | ||
| client.roomLeave('defaultRoom') | ||
| }) | ||
| // leave | ||
| setTimeout(function(){ | ||
| client.disconnect(function(){ | ||
| console.log("all done!"); | ||
| }); | ||
| }, 1000); | ||
| }); | ||
| setTimeout(function () { | ||
| client.disconnect(function () { | ||
| console.log('all done!') | ||
| }) | ||
| }, 1000) | ||
| }) |
+223
-223
@@ -1,10 +0,10 @@ | ||
| var net = require('net'); | ||
| var tls = require('tls'); | ||
| var util = require('util'); | ||
| var EventEmitter = require('events').EventEmitter; | ||
| var net = require('net') | ||
| var tls = require('tls') | ||
| var util = require('util') | ||
| var EventEmitter = require('events').EventEmitter | ||
| var Defaults = { | ||
| host: "127.0.0.1", | ||
| port: "5000", | ||
| delimiter: "\r\n", | ||
| host: '127.0.0.1', | ||
| port: '5000', | ||
| delimiter: '\r\n', | ||
| logLength: 100, | ||
@@ -14,145 +14,147 @@ secure: false, | ||
| reconnectTimeout: 1000, | ||
| reconnectAttempts: 10, | ||
| }; | ||
| reconnectAttempts: 10 | ||
| } | ||
| actionheroClient = function(){ | ||
| EventEmitter.call(this); | ||
| this.connected = false; | ||
| this.connection = null; | ||
| this.params = {}; | ||
| this.details = null; | ||
| this.connectCallback = null; | ||
| this.lastLine = null; | ||
| this.stream = ""; | ||
| this.log = []; | ||
| this.reconnectAttempts = 0; | ||
| this.messageCount = 0; | ||
| this.userMessages = {}; | ||
| this.expectedResponses = {}; | ||
| this.startingTimeStamps = {}; | ||
| this.responseTimesouts = {}; | ||
| this.defaults = Defaults; | ||
| }; | ||
| var ActionheroClient = function () { | ||
| EventEmitter.call(this) | ||
| this.connected = false | ||
| this.connection = null | ||
| this.params = {} | ||
| this.details = null | ||
| this.connectCallback = null | ||
| this.lastLine = null | ||
| this.stream = '' | ||
| this.log = [] | ||
| this.reconnectAttempts = 0 | ||
| this.messageCount = 0 | ||
| this.userMessages = {} | ||
| this.expectedResponses = {} | ||
| this.startingTimeStamps = {} | ||
| this.responseTimesouts = {} | ||
| this.defaults = Defaults | ||
| } | ||
| util.inherits(actionheroClient, EventEmitter); | ||
| util.inherits(ActionheroClient, EventEmitter) | ||
| actionheroClient.prototype.connect = function(params, next){ | ||
| var self = this; | ||
| ActionheroClient.prototype.connect = function (params, next) { | ||
| var self = this | ||
| if(self.connection){ delete self.connection; } | ||
| if (self.connection) { delete self.connection } | ||
| if(params){ self.params = params; } | ||
| if(typeof next == 'function'){ | ||
| self.connectCallback = next; | ||
| if (params) { self.params = params } | ||
| if (typeof next === 'function') { | ||
| self.connectCallback = next | ||
| } | ||
| for(var i in self.defaults){ | ||
| if(self.params[i] === null || self.params[i] === undefined){ | ||
| self.params[i] = self.defaults[i]; | ||
| for (var i in self.defaults) { | ||
| if (self.params[i] === null || self.params[i] === undefined) { | ||
| self.params[i] = self.defaults[i] | ||
| } | ||
| } | ||
| if(self.params.secure === true){ | ||
| self.connection = tls.connect(self.params.port, self.params.host); | ||
| }else{ | ||
| self.connection = net.connect(self.params.port, self.params.host); | ||
| if (self.params.secure === true) { | ||
| self.connection = tls.connect(self.params.port, self.params.host) | ||
| } else { | ||
| self.connection = net.connect(self.params.port, self.params.host) | ||
| } | ||
| self.connection.setEncoding("utf8"); | ||
| self.connection.setEncoding('utf8') | ||
| self.connection.on("data", function (chunk){ | ||
| self.emit('rawData', String(chunk)); | ||
| self.stream += String(chunk); | ||
| var delimited = false; | ||
| if(self.stream.indexOf(self.params.delimiter) >= 0){ | ||
| delimited = true; | ||
| self.connection.on('data', function (chunk) { | ||
| self.emit('rawData', String(chunk)) | ||
| self.stream += String(chunk) | ||
| var delimited = false | ||
| if (self.stream.indexOf(self.params.delimiter) >= 0) { | ||
| delimited = true | ||
| } | ||
| if(delimited === true){ | ||
| try{ | ||
| var lines = self.stream.split(self.params.delimiter); | ||
| for(var j in lines){ | ||
| var line = lines[j]; | ||
| if(line.length > 0){ | ||
| self.handleData(line); | ||
| if (delimited === true) { | ||
| try { | ||
| var lines = self.stream.split(self.params.delimiter) | ||
| for (var j in lines) { | ||
| var line = lines[j] | ||
| if (line.length > 0) { | ||
| self.handleData(line) | ||
| } | ||
| } | ||
| self.stream = ""; | ||
| }catch(e){ | ||
| self.lastLine = null; | ||
| self.stream = '' | ||
| } catch (e) { | ||
| self.lastLine = null | ||
| } | ||
| } | ||
| }); | ||
| }) | ||
| self.connection.on("connect", function(err){ | ||
| self.connected = true; | ||
| self.messageCount = 0; | ||
| self.reconnectAttempts = 0; | ||
| self.detailsView(function(){ | ||
| self.emit('connected'); | ||
| if(typeof self.connectCallback === 'function'){ | ||
| self.connectCallback(null, self.lastLine.welcome); | ||
| delete self.connectCallback; | ||
| self.connection.on('connect', function (error) { | ||
| if (error) { self.emit('error', error) } | ||
| self.connected = true | ||
| self.messageCount = 0 | ||
| self.reconnectAttempts = 0 | ||
| self.detailsView(function () { | ||
| self.emit('connected') | ||
| if (typeof self.connectCallback === 'function') { | ||
| self.connectCallback(null, self.lastLine.welcome) | ||
| delete self.connectCallback | ||
| } | ||
| }); | ||
| }); | ||
| }) | ||
| }) | ||
| self.connection.on("error", function(err){ | ||
| self.emit('error', err); | ||
| self.reconnect(); | ||
| }); | ||
| self.connection.on('error', function (error) { | ||
| self.emit('error', error) | ||
| self.reconnect() | ||
| }) | ||
| self.connection.on("end", function (){ | ||
| if(self.connected !== null){ self.connected = false; } | ||
| self.emit('end', null); | ||
| self.connection.removeAllListeners('data'); | ||
| self.connection.removeAllListeners('error'); | ||
| self.connection.removeAllListeners('end'); | ||
| self.reconnect(); | ||
| }); | ||
| }; | ||
| self.connection.on('end', function () { | ||
| if (self.connected !== null) { self.connected = false } | ||
| self.emit('end', null) | ||
| self.connection.removeAllListeners('data') | ||
| self.connection.removeAllListeners('error') | ||
| self.connection.removeAllListeners('end') | ||
| self.reconnect() | ||
| }) | ||
| } | ||
| actionheroClient.prototype.reconnect = function(){ | ||
| var self = this; | ||
| ActionheroClient.prototype.reconnect = function () { | ||
| var self = this | ||
| self.reconnectAttempts++; | ||
| if(self.reconnectAttempts > self.params.reconnectAttempts){ | ||
| self.emit('error', new Error('maximim reconnectAttempts reached')); | ||
| }else if(self.connected === false && self.params.reconnectTimeout && self.params.reconnectTimeout > 0){ | ||
| setTimeout(function(){ | ||
| self.connect(); | ||
| }.bind(self), self.params.reconnectTimeout); | ||
| self.reconnectAttempts++ | ||
| if (self.reconnectAttempts > self.params.reconnectAttempts) { | ||
| self.emit('error', new Error('maximim reconnectAttempts reached')) | ||
| } else if (self.connected === false && self.params.reconnectTimeout && self.params.reconnectTimeout > 0) { | ||
| setTimeout(function () { | ||
| self.connect() | ||
| }, self.params.reconnectTimeout) | ||
| } | ||
| }; | ||
| } | ||
| actionheroClient.prototype.disconnect = function(next){ | ||
| var self = this; | ||
| self.connected = null; | ||
| ActionheroClient.prototype.disconnect = function (next) { | ||
| var self = this | ||
| self.connected = null | ||
| process.nextTick(function(){ | ||
| self.send('exit'); | ||
| if(typeof next === 'function'){ | ||
| next(); | ||
| process.nextTick(function () { | ||
| self.send('exit') | ||
| if (typeof next === 'function') { | ||
| next() | ||
| } | ||
| }); | ||
| }; | ||
| }) | ||
| } | ||
| actionheroClient.prototype.handleData = function(data){ | ||
| try{ | ||
| this.lastLine = JSON.parse(data); | ||
| }catch(e){ | ||
| this.emit('error', e, data); | ||
| ActionheroClient.prototype.handleData = function (data) { | ||
| try { | ||
| this.lastLine = JSON.parse(data) | ||
| } catch (e) { | ||
| this.emit('error', e, data) | ||
| } | ||
| this.emit('data', this.lastLine); | ||
| this.addLog(this.lastLine); | ||
| if(this.lastLine.messageCount && this.lastLine.messageCount > this.messageCount){ | ||
| this.messageCount = this.lastLine.messageCount; | ||
| this.emit('data', this.lastLine) | ||
| this.addLog(this.lastLine) | ||
| if (this.lastLine.messageCount && this.lastLine.messageCount > this.messageCount) { | ||
| this.messageCount = this.lastLine.messageCount | ||
| } | ||
| if(this.lastLine.context == "api" && this.lastLine.welcome){ | ||
| if (this.lastLine.context === 'api' && this.lastLine.welcome) { | ||
| // welcome message; indicates successfull connection | ||
| this.emit('welcome', this.lastLine.welcome); | ||
| } | ||
| this.emit('welcome', this.lastLine.welcome) | ||
| // "say" messages from other users | ||
| else if(this.lastLine.context == "user"){ | ||
| } else if (this.lastLine.context === 'user') { | ||
| this.userMessages[this.lastLine.from] = { | ||
@@ -162,145 +164,143 @@ timeStamp: new Date(), | ||
| from: this.lastLine.from | ||
| }; | ||
| this.emit('say', this.userMessages[this.lastLine.from]); | ||
| } | ||
| } | ||
| this.emit('say', this.userMessages[this.lastLine.from]) | ||
| // responses to your actions | ||
| else if(this.lastLine.context == "response"){ | ||
| if(this.expectedResponses[this.lastLine.messageCount]){ | ||
| clearTimeout(this.responseTimesouts[this.lastLine.messageCount]); | ||
| var next = this.expectedResponses[this.lastLine.messageCount]; | ||
| var delta = new Date().getTime() - this.startingTimeStamps[this.lastLine.messageCount]; | ||
| delete this.expectedResponses[this.lastLine.messageCount]; | ||
| delete this.startingTimeStamps[this.lastLine.messageCount]; | ||
| delete this.responseTimesouts[this.lastLine.messageCount]; | ||
| } else if (this.lastLine.context === 'response') { | ||
| if (this.expectedResponses[this.lastLine.messageCount]) { | ||
| clearTimeout(this.responseTimesouts[this.lastLine.messageCount]) | ||
| var next = this.expectedResponses[this.lastLine.messageCount] | ||
| var delta = new Date().getTime() - this.startingTimeStamps[this.lastLine.messageCount] | ||
| delete this.expectedResponses[this.lastLine.messageCount] | ||
| delete this.startingTimeStamps[this.lastLine.messageCount] | ||
| delete this.responseTimesouts[this.lastLine.messageCount] | ||
| var error = null; | ||
| if(this.lastLine.error){ | ||
| error = new Error(this.lastLine.error); | ||
| }else if(this.lastLine.status && this.lastLine.status != 'OK'){ | ||
| error = new Error(this.lastLine.status); | ||
| var error = null | ||
| if (this.lastLine.error) { | ||
| error = new Error(this.lastLine.error) | ||
| } else if (this.lastLine.status && this.lastLine.status !== 'OK') { | ||
| error = new Error(this.lastLine.status) | ||
| } | ||
| next(error, this.lastLine, delta); | ||
| next(error, this.lastLine, delta) | ||
| } | ||
| } | ||
| // ? | ||
| else{ } | ||
| }; | ||
| } | ||
| actionheroClient.prototype.send = function(str){ | ||
| this.connection.write(str + "\r\n"); | ||
| }; | ||
| ActionheroClient.prototype.send = function (str) { | ||
| this.connection.write(str + '\r\n') | ||
| } | ||
| actionheroClient.prototype.registerResponseAndCall = function(msg, next){ | ||
| var self = this; | ||
| if(self.connection){ | ||
| self.messageCount++; | ||
| var responseID = self.messageCount; | ||
| if(typeof next == "function"){ | ||
| self.expectedResponses[responseID] = next; | ||
| self.startingTimeStamps[responseID] = new Date().getTime(); | ||
| ActionheroClient.prototype.registerResponseAndCall = function (msg, next) { | ||
| var self = this | ||
| if (self.connection) { | ||
| self.messageCount++ | ||
| var responseID = self.messageCount | ||
| if (typeof next === 'function') { | ||
| self.expectedResponses[responseID] = next | ||
| self.startingTimeStamps[responseID] = new Date().getTime() | ||
| self.responseTimesouts[responseID] = setTimeout( | ||
| function(msg, next){ | ||
| var error = new Error("Timeout reached"); | ||
| self.emit('timeout', error, msg, next); | ||
| next(error); | ||
| delete self.startingTimeStamps[responseID]; | ||
| delete self.expectedResponses[responseID]; | ||
| }, | ||
| self.params.timeout, msg, next); | ||
| function (msg, next) { | ||
| var error = new Error('Timeout reached') | ||
| self.emit('timeout', error, msg, next) | ||
| next(error) | ||
| delete self.startingTimeStamps[responseID] | ||
| delete self.expectedResponses[responseID] | ||
| }, | ||
| self.params.timeout, msg, next) | ||
| } | ||
| process.nextTick(function(){ | ||
| self.send(msg); | ||
| }); | ||
| }else{ | ||
| self.emit('error',new Error("Not Connected")); | ||
| process.nextTick(function () { | ||
| self.send(msg) | ||
| }) | ||
| } else { | ||
| self.emit('error', new Error('Not Connected')) | ||
| } | ||
| }; | ||
| } | ||
| actionheroClient.prototype.documentation = function(next){ | ||
| this.registerResponseAndCall("documentation", next); | ||
| }; | ||
| ActionheroClient.prototype.documentation = function (next) { | ||
| this.registerResponseAndCall('documentation', next) | ||
| } | ||
| actionheroClient.prototype.paramAdd = function(k, v, next){ | ||
| if(k !== null && v !== null){ | ||
| this.registerResponseAndCall("paramAdd "+k+"="+v, next); | ||
| }else{ | ||
| if(typeof next == "function"){ next(new Error("key and value are required"), null); } | ||
| ActionheroClient.prototype.paramAdd = function (k, v, next) { | ||
| if (k !== null && v !== null) { | ||
| this.registerResponseAndCall('paramAdd ' + k + '=' + v, next) | ||
| } else { | ||
| if (typeof next === 'function') { next(new Error('key and value are required'), null) } | ||
| } | ||
| }; | ||
| } | ||
| actionheroClient.prototype.paramDelete = function(k, next){ | ||
| if(k !== null){ | ||
| this.registerResponseAndCall("paramDelete "+k, next); | ||
| }else{ | ||
| if(typeof next == "function"){ next(new Error("key is required"), null); } | ||
| ActionheroClient.prototype.paramDelete = function (k, next) { | ||
| if (k !== null) { | ||
| this.registerResponseAndCall('paramDelete ' + k, next) | ||
| } else { | ||
| if (typeof next === 'function') { next(new Error('key is required'), null) } | ||
| } | ||
| }; | ||
| } | ||
| actionheroClient.prototype.paramsDelete = function(next){ | ||
| this.registerResponseAndCall("paramsDelete", next); | ||
| }; | ||
| ActionheroClient.prototype.paramsDelete = function (next) { | ||
| this.registerResponseAndCall('paramsDelete', next) | ||
| } | ||
| actionheroClient.prototype.paramView = function(k, next){ | ||
| this.registerResponseAndCall("paramView "+k, next); | ||
| }; | ||
| ActionheroClient.prototype.paramView = function (k, next) { | ||
| this.registerResponseAndCall('paramView ' + k, next) | ||
| } | ||
| actionheroClient.prototype.paramsView = function(next){ | ||
| this.registerResponseAndCall("paramsView", next); | ||
| }; | ||
| ActionheroClient.prototype.paramsView = function (next) { | ||
| this.registerResponseAndCall('paramsView', next) | ||
| } | ||
| actionheroClient.prototype.roomView = function(room, next){ | ||
| this.registerResponseAndCall("roomView " + room, next); | ||
| }; | ||
| ActionheroClient.prototype.roomView = function (room, next) { | ||
| this.registerResponseAndCall('roomView ' + room, next) | ||
| } | ||
| actionheroClient.prototype.detailsView = function(next){ | ||
| var self = this; | ||
| self.registerResponseAndCall('detailsView', function(err, data, delta){ | ||
| if(!err){ | ||
| self.details = data.data; | ||
| self.id = data.data.id; | ||
| ActionheroClient.prototype.detailsView = function (next) { | ||
| var self = this | ||
| self.registerResponseAndCall('detailsView', function (error, data, delta) { | ||
| if (!error) { | ||
| self.details = data.data | ||
| self.id = data.data.id | ||
| } | ||
| next(err, data, delta); | ||
| }); | ||
| }; | ||
| next(error, data, delta) | ||
| }) | ||
| } | ||
| actionheroClient.prototype.roomAdd = function(room, next){ | ||
| this.registerResponseAndCall('roomAdd ' + room, next); | ||
| }; | ||
| ActionheroClient.prototype.roomAdd = function (room, next) { | ||
| this.registerResponseAndCall('roomAdd ' + room, next) | ||
| } | ||
| actionheroClient.prototype.roomLeave = function(room, next){ | ||
| this.registerResponseAndCall('roomLeave ' + room, next); | ||
| }; | ||
| ActionheroClient.prototype.roomLeave = function (room, next) { | ||
| this.registerResponseAndCall('roomLeave ' + room, next) | ||
| } | ||
| actionheroClient.prototype.say = function(room, msg, next){ | ||
| this.registerResponseAndCall('say '+ room + ' ' + msg, next); | ||
| }; | ||
| ActionheroClient.prototype.say = function (room, msg, next) { | ||
| this.registerResponseAndCall('say ' + room + ' ' + msg, next) | ||
| } | ||
| actionheroClient.prototype.action = function(action, next){ | ||
| this.registerResponseAndCall(action, next); | ||
| }; | ||
| ActionheroClient.prototype.action = function (action, next) { | ||
| this.registerResponseAndCall(action, next) | ||
| } | ||
| actionheroClient.prototype.file = function(file, next){ | ||
| this.registerResponseAndCall('file ' + file, next); | ||
| }; | ||
| ActionheroClient.prototype.file = function (file, next) { | ||
| this.registerResponseAndCall('file ' + file, next) | ||
| } | ||
| actionheroClient.prototype.actionWithParams = function(action, params, next){ | ||
| ActionheroClient.prototype.actionWithParams = function (action, params, next) { | ||
| var msg = { | ||
| action: action, | ||
| params: params, | ||
| }; | ||
| params: params | ||
| } | ||
| this.registerResponseAndCall(JSON.stringify(msg), next); | ||
| }; | ||
| this.registerResponseAndCall(JSON.stringify(msg), next) | ||
| } | ||
| actionheroClient.prototype.addLog = function(entry){ | ||
| ActionheroClient.prototype.addLog = function (entry) { | ||
| this.log.push({ | ||
| timestamp: new Date(), | ||
| data: entry | ||
| }); | ||
| if(this.log.length > this.params.logLength){ | ||
| this.log.splice(0,1); | ||
| }) | ||
| if (this.log.length > this.params.logLength) { | ||
| this.log.splice(0, 1) | ||
| } | ||
| }; | ||
| } | ||
| module.exports = actionheroClient; | ||
| module.exports = ActionheroClient |
+10
-4
@@ -5,3 +5,3 @@ { | ||
| "description": "actionhero client in JS for other node servers to use", | ||
| "version": "4.0.1", | ||
| "version": "5.0.0", | ||
| "homepage": "https://github.com/evantahler/actionhero-client", | ||
@@ -26,8 +26,14 @@ "repository": { | ||
| "actionhero": "git://github.com/evantahler/actionhero.git#master", | ||
| "mocha": "^2.1.0", | ||
| "should": "^8.3.0" | ||
| "mocha": "^3.2.0", | ||
| "should": "^11.1.1", | ||
| "standard": "^8.6.0" | ||
| }, | ||
| "standard": { | ||
| "globals":[ | ||
| "it", "describe", "beforeEach" | ||
| ] | ||
| }, | ||
| "scripts": { | ||
| "test": "mocha" | ||
| "test": "standard && mocha" | ||
| } | ||
| } |
+29
-29
@@ -1,2 +0,2 @@ | ||
| #actionheroClient (for nodeJS) | ||
| #ActionheroClient (for nodeJS) | ||
@@ -22,7 +22,7 @@   [](https://travis-ci.org/evantahler/actionhero-client) | ||
| ```javascript | ||
| var actionheroClient = require("actionhero-client"); | ||
| var client = new actionheroClient(); | ||
| var ActionheroClient = require("actionhero-client"); | ||
| var client = new ActionheroClient(); | ||
| ``` | ||
| Once you have included the actionheroClient library within your project, you can connect like this: | ||
| Once you have included the ActionheroClient library within your project, you can connect like this: | ||
@@ -53,3 +53,3 @@ ```javascript | ||
| actionheroClient will emit a few types of events (many of which are caught in the example below). Here are the events, and how you might catch them: | ||
| ActionheroClient will emit a few types of events (many of which are caught in the example below). Here are the events, and how you might catch them: | ||
@@ -71,36 +71,36 @@ * `client.on("connected", function(null){})` | ||
| * `actionheroClient.disconnect(next)` | ||
| * `actionheroClient.paramAdd(key,value,next)` | ||
| * `ActionheroClient.disconnect(next)` | ||
| * `ActionheroClient.paramAdd(key,value,next)` | ||
| * remember that both key and value must pass JSON.stringify | ||
| * `actionheroClient.paramDelete(key,next)` | ||
| * `actionheroClient.paramsDelete(next)` | ||
| * `actionheroClient.paramView(key,next)` | ||
| * `actionheroClient.paramsView(next)` | ||
| * `actionheroClient.details(next)` | ||
| * `actionheroClient.roomView(room, next)` | ||
| * `actionheroClient.roomAdd(room,next)` | ||
| * `actionheroClient.roomLeave(room,next)` | ||
| * `actionheroClient.say(room, msg, next)` | ||
| * `actionheroClient.action(action, next)` | ||
| * `ActionheroClient.paramDelete(key,next)` | ||
| * `ActionheroClient.paramsDelete(next)` | ||
| * `ActionheroClient.paramView(key,next)` | ||
| * `ActionheroClient.paramsView(next)` | ||
| * `ActionheroClient.details(next)` | ||
| * `ActionheroClient.roomView(room, next)` | ||
| * `ActionheroClient.roomAdd(room,next)` | ||
| * `ActionheroClient.roomLeave(room,next)` | ||
| * `ActionheroClient.say(room, msg, next)` | ||
| * `ActionheroClient.action(action, next)` | ||
| * this basic action method will not set or unset any params | ||
| * next will be passed (err, data, duration) | ||
| * `actionheroClient.actionWithParams(action, params, next)` | ||
| * `ActionheroClient.actionWithParams(action, params, next)` | ||
| * this action will ignore any previously set params to the connection | ||
| * params is a hash of this form `{key: "myKey", value: "myValue"}` | ||
| * params is a hash of this form `{key: "myKey", value: "myValue"}` | ||
| * next will be passed (err, data, duration) | ||
| Each callback will receive the full data hash returned from the server and a timestamp: `(err, data, duration)` | ||
| Each callback will receive the full data hash returned from the server and a timestamp: `(err, data, duration)` | ||
| ## Data | ||
| ## Data | ||
| There are a few data elements you can inspect on `actionheroClient`: | ||
| * `actionheroClient.lastLine` | ||
| * `ActionheroClient.lastLine` | ||
| * This is the last parsed JSON message received from the server (chronologically, not by messageID) | ||
| * `actionheroClient.userMessages` | ||
| * `ActionheroClient.userMessages` | ||
| * a hash which contains the latest `say` message from all users | ||
| * `actionheroClient.log` | ||
| * `ActionheroClient.log` | ||
| * An array of the last n parsable JSON replies from the server | ||
| * each entry is of the form {data, timeStamp} where data was the server's full response | ||
| * `actionheroClient.messageCount` | ||
| * `ActionheroClient.messageCount` | ||
| * An integer counting the number of messages received from the server | ||
@@ -111,4 +111,4 @@ | ||
| ```javascript | ||
| var actionheroClient = require("actionhero-client"); | ||
| var client = new actionheroClient(); | ||
| var ActionheroClient = require("actionhero-client"); | ||
| var client = new ActionheroClient(); | ||
@@ -142,3 +142,3 @@ client.on("say", function(msgBlock){ | ||
| console.log(client.details); | ||
| // try an action | ||
@@ -153,3 +153,3 @@ var params = { key: "mykey", value: "myValue" }; | ||
| client.roomAdd("defaultRoom", function(err){ | ||
| client.say("defaultRoom", "Hello from the actionheroClient"); | ||
| client.say("defaultRoom", "Hello from the ActionheroClient"); | ||
| client.roomLeave("defaultRoom"); | ||
@@ -156,0 +156,0 @@ }); |
+164
-162
@@ -1,185 +0,187 @@ | ||
| var should = require("should"); | ||
| var setup = require(__dirname + "/setup.js").setup; | ||
| var actionheroClient = require(__dirname + "/../lib/actionhero-client.js"); | ||
| var client; | ||
| var should = require('should') | ||
| var path = require('path') | ||
| var setup = require(path.join(__dirname, '/setup.js')).setup | ||
| var ActionheroClient = require(path.join(__dirname, '/../lib/actionhero-client.js')) | ||
| var client | ||
| var connectionParams = { | ||
| host: "0.0.0.0", | ||
| host: '0.0.0.0', | ||
| port: setup.serverConfigChanges.servers.socket.port, | ||
| timeout: 1000, | ||
| }; | ||
| timeout: 1000 | ||
| } | ||
| describe('integration', function () { | ||
| beforeEach(function (done) { | ||
| client = new ActionheroClient() | ||
| setup.startServer(function () { | ||
| client.connect(connectionParams, function () { | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| describe('integration', function(){ | ||
| beforeEach(function(done){ | ||
| client = new actionheroClient(); | ||
| setup.startServer(function(){ | ||
| client.connect(connectionParams, function(){ | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| it('actionhero server should have booted', function (done) { | ||
| setup.api.should.be.an.instanceOf(Object) | ||
| done() | ||
| }) | ||
| it("actionhero server should have booted", function(done){ | ||
| setup.api.should.be.an.instanceOf(Object); | ||
| done(); | ||
| }); | ||
| it('can get my connection details', function (done) { | ||
| client.detailsView(function (erroror, details, delta) { | ||
| should.not.exist(erroror) | ||
| details.status.should.equal('OK') | ||
| details.context.should.equal('response') | ||
| details.data.totalActions.should.equal(0) | ||
| details.data.pendingActions.should.equal(0) | ||
| done() | ||
| }) | ||
| }) | ||
| it("can get my connection details", function(done){ | ||
| client.detailsView(function(err, details, delta){ | ||
| should.not.exist(err); | ||
| details.status.should.equal("OK"); | ||
| details.context.should.equal("response"); | ||
| details.data.totalActions.should.equal(0); | ||
| details.data.pendingActions.should.equal(0); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('should log server messages internally', function (done) { | ||
| client.log.length.should.equal(2) | ||
| client.log[0].data.welcome.should.equal('Hello! Welcome to the actionhero api') | ||
| done() | ||
| }) | ||
| it("should log server messages internally", function(done){ | ||
| client.log.length.should.equal(2); | ||
| client.log[0].data.welcome.should.equal("Hello! Welcome to the actionhero api"); | ||
| done(); | ||
| }); | ||
| it('should be able to set params', function (done) { | ||
| client.paramAdd('key', 'value', function (erroror, response) { | ||
| should.not.exist(erroror) | ||
| response.status.should.equal('OK') | ||
| client.paramsView(function (erroror, params) { | ||
| params.data.key.should.equal('value') | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| it("should be able to set params", function(done){ | ||
| client.paramAdd("key", "value", function(err, response){ | ||
| should.not.exist(err); | ||
| response.status.should.equal("OK"); | ||
| client.paramsView(function(err, params){ | ||
| params.data.key.should.equal("value"); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| it('can delete params and confirm they are gone', function (done) { | ||
| client.paramAdd('key', 'value', function (erroror, response) { | ||
| should.not.exist(erroror) | ||
| client.paramsDelete(function (erroror, response) { | ||
| should.not.exist(erroror) | ||
| response.status.should.equal('OK') | ||
| client.paramsView(function (erroror, params) { | ||
| should.not.exist(erroror) | ||
| should.not.exist(params.data.key) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| it("can delete params and confirm they are gone", function(done){ | ||
| client.paramAdd("key", "value", function(err, response){ | ||
| should.not.exist(err); | ||
| client.paramsDelete(function(err, response){ | ||
| should.not.exist(err); | ||
| response.status.should.equal("OK"); | ||
| client.paramsView(function(err, params){ | ||
| should.not.exist(err); | ||
| should.not.exist(params.data.key); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| it('can delete a param and confirm they are gone', function (done) { | ||
| client.paramAdd('key', 'v1', function (erroror, response) { | ||
| should.not.exist(erroror) | ||
| client.paramAdd('value', 'v2', function (erroror, response) { | ||
| should.not.exist(erroror) | ||
| client.paramDelete('key', function (erroror, response) { | ||
| should.not.exist(erroror) | ||
| client.paramsView(function (erroror, params) { | ||
| Object.keys(params.data).length.should.equal(1) | ||
| params.data.value.should.equal('v2') | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| it("can delete a param and confirm they are gone", function(done){ | ||
| client.paramAdd("key", "v1", function(err, response){ | ||
| should.not.exist(err); | ||
| client.paramAdd("value", "v2", function(err, response){ | ||
| should.not.exist(err); | ||
| client.paramDelete("key", function(err, response){ | ||
| should.not.exist(err); | ||
| client.paramsView(function(err, params){ | ||
| Object.keys( params.data ).length.should.equal(1); | ||
| params.data.value.should.equal("v2"); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| it('can run an action (simple params)', function (done) { | ||
| client.action('status', function (erroror, apiResponse) { | ||
| should.not.exist(erroror) | ||
| apiResponse.uptime.should.be.above(0) | ||
| apiResponse.context.should.equal('response') | ||
| done() | ||
| }) | ||
| }) | ||
| it("can run an action (simple params)", function(done){ | ||
| client.action("status", function(err, apiResponse){ | ||
| should.not.exist(err); | ||
| apiResponse.uptime.should.be.above(0); | ||
| apiResponse.context.should.equal("response"); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('can run an action (complex params)', function (done) { | ||
| var params = { key: 'mykey', value: 'myValue' } | ||
| client.actionWithParams('cacheTest', params, function (erroror, apiResponse) { | ||
| should.not.exist(erroror) | ||
| apiResponse.context.should.equal('response') | ||
| apiResponse.cacheTestResults.saveResp.should.equal(true) | ||
| done() | ||
| }) | ||
| }) | ||
| it("can run an action (complex params)", function(done){ | ||
| var params = { key: "mykey", value: "myValue" }; | ||
| client.actionWithParams("cacheTest", params, function(err, apiResponse){ | ||
| should.not.exist(err); | ||
| apiResponse.context.should.equal("response"); | ||
| apiResponse.cacheTestResults.saveResp.should.equal(true); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('can join a room', function (done) { | ||
| client.roomAdd('defaultRoom', function (erroror, data) { | ||
| client.roomView('defaultRoom', function (erroror, data) { | ||
| Object.keys(data.data.members).length.should.equal(1) | ||
| Object.keys(data.data.members)[0].should.equal(client.id) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| it("can join a room", function(done){ | ||
| client.roomAdd('defaultRoom', function(err, data){ | ||
| client.roomView('defaultRoom', function(err, data){ | ||
| Object.keys( data.data.members ).length.should.equal(1); | ||
| Object.keys( data.data.members )[0].should.equal( client.id ); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| it('can leave a room', function (done) { | ||
| client.detailsView(function (error, data) { | ||
| should.not.exist(error) | ||
| data.data.rooms.length.should.equal(0) | ||
| client.roomAdd('defaultRoom', function (error, data) { | ||
| should.not.exist(error) | ||
| client.roomView('defaultRoom', function (error, data) { | ||
| should.not.exist(error) | ||
| Object.keys(data.data.members).should.containEql(client.id) | ||
| it("can leave a room", function(done){ | ||
| client.detailsView(function(err, data){ | ||
| data.data.rooms.length.should.equal(0); | ||
| client.roomAdd('defaultRoom', function(err, data){ | ||
| client.roomView('defaultRoom', function(err, data){ | ||
| Object.keys( data.data.members ).should.containEql( client.id ); | ||
| client.roomLeave('defaultRoom', function(err, data){ | ||
| client.detailsView(function(err, data){ | ||
| data.data.rooms.length.should.equal(0); | ||
| done(); | ||
| }); | ||
| }); | ||
| client.roomLeave('defaultRoom', function (error, data) { | ||
| should.not.exist(error) | ||
| client.detailsView(function (error, data) { | ||
| should.not.exist(error) | ||
| data.data.rooms.length.should.equal(0) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| it('will translate bad status to an error callback', function (done) { | ||
| client.roomView('someCrazyRoom', function (error, data) { | ||
| String(error).should.equal('Error: not member of room someCrazyRoom') | ||
| data.status.should.equal('not member of room someCrazyRoom') | ||
| done() | ||
| }) | ||
| }) | ||
| it('will translate bad status to an error callback', function(done){ | ||
| client.roomView('someCrazyRoom', function(err, data){ | ||
| String(err).should.equal('Error: not member of room someCrazyRoom'); | ||
| data.status.should.equal('not member of room someCrazyRoom'); | ||
| done(); | ||
| }); | ||
| }); | ||
| it("will get SAY events", function(done){ | ||
| var used = false; | ||
| client.roomAdd('defaultRoom', function(){ | ||
| client.on("say",function(msgBlock){ | ||
| if(used === false){ | ||
| used = true; | ||
| msgBlock.message.should.equal("TEST MESSAGE"); | ||
| done(); | ||
| it('will get SAY events', function (done) { | ||
| var used = false | ||
| client.roomAdd('defaultRoom', function () { | ||
| client.on('say', function (msgBlock) { | ||
| if (used === false) { | ||
| used = true | ||
| msgBlock.message.should.equal('TEST MESSAGE') | ||
| done() | ||
| } | ||
| }); | ||
| }) | ||
| setup.api.chatRoom.broadcast({}, 'defaultRoom', "TEST MESSAGE"); | ||
| }); | ||
| }); | ||
| setup.api.chatRoom.broadcast({}, 'defaultRoom', 'TEST MESSAGE') | ||
| }) | ||
| }) | ||
| it('will obey the servers simultaneousActions policy', function(done){ | ||
| client.actionWithParams("sleepTest", {sleepDuration: 500}); | ||
| client.actionWithParams("sleepTest", {sleepDuration: 500}); | ||
| client.actionWithParams("sleepTest", {sleepDuration: 500}); | ||
| client.actionWithParams("sleepTest", {sleepDuration: 500}); | ||
| client.actionWithParams("sleepTest", {sleepDuration: 500}); | ||
| client.actionWithParams("sleepTest", {sleepDuration: 500}, function(err, apiResponse){ | ||
| String(err).should.equal('Error: you have too many pending requests'); | ||
| apiResponse.error.should.equal('you have too many pending requests'); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('will obey the servers simultaneousActions policy', function (done) { | ||
| client.actionWithParams('sleepTest', {sleepDuration: 500}) | ||
| client.actionWithParams('sleepTest', {sleepDuration: 500}) | ||
| client.actionWithParams('sleepTest', {sleepDuration: 500}) | ||
| client.actionWithParams('sleepTest', {sleepDuration: 500}) | ||
| client.actionWithParams('sleepTest', {sleepDuration: 500}) | ||
| client.actionWithParams('sleepTest', {sleepDuration: 500}, function (error, apiResponse) { | ||
| String(error).should.equal('Error: you have too many pending requests') | ||
| apiResponse.error.should.equal('you have too many pending requests') | ||
| done() | ||
| }) | ||
| }) | ||
| it("will obey timeouts", function(done){ | ||
| client.actionWithParams("sleepTest", {sleepDuration: 2 * 1000}, function(err, apiResponse){ | ||
| String( err ).should.equal('Error: Timeout reached'); | ||
| should.not.exist(apiResponse); | ||
| done(); | ||
| }); | ||
| }); | ||
| it('will obey timeouts', function (done) { | ||
| client.actionWithParams('sleepTest', {sleepDuration: 2 * 1000}, function (error, apiResponse) { | ||
| String(error).should.equal('Error: Timeout reached') | ||
| should.not.exist(apiResponse) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| }); | ||
| describe('connection and reconnection', function(){ | ||
| describe('connection and reconnection', function () { | ||
| // TODO | ||
| }); | ||
| }) |
+28
-26
| exports.setup = { | ||
| serverPrototype: require("../node_modules/actionhero/actionhero.js").actionheroPrototype, | ||
| ServerPrototype: require('../node_modules/actionhero/actionhero.js'), | ||
| serverConfigChanges: { | ||
| general: { | ||
| id: "test-server-1", | ||
| id: 'test-server-1', | ||
| workers: 1, | ||
@@ -10,6 +10,6 @@ developmentMode: false, | ||
| 'defaultRoom': {}, | ||
| 'otherRoom': {}, | ||
| }, | ||
| 'otherRoom': {} | ||
| } | ||
| }, | ||
| logger: { transports: null, }, | ||
| logger: { transports: null }, | ||
| // logger: { | ||
@@ -32,29 +32,31 @@ // transports: [ | ||
| secure: false, | ||
| port: 9000, | ||
| }, | ||
| port: 9000 | ||
| } | ||
| } | ||
| }, | ||
| startServer: function(callback){ | ||
| var self = this; | ||
| startServer: function (callback) { | ||
| var self = this | ||
| if(!self.server){ | ||
| process.env.ACTIONHERO_CONFIG = process.cwd() + "/node_modules/actionhero/config/"; | ||
| self.server = new self.serverPrototype(); | ||
| self.server.start({configChanges: self.serverConfigChanges}, function(err, api){ | ||
| self.api = api; | ||
| callback(err, self.api); | ||
| }); | ||
| }else{ | ||
| process.nextTick(function(){ | ||
| callback(); | ||
| }); | ||
| if (!self.server) { | ||
| process.env.ACTIONHERO_CONFIG = process.cwd() + '/node_modules/actionhero/config/' | ||
| self.server = new self.ServerPrototype() | ||
| self.server.start({configChanges: self.serverConfigChanges}, function (err, api) { | ||
| self.api = api | ||
| callback(err, self.api) | ||
| }) | ||
| } else { | ||
| process.nextTick(function () { | ||
| callback() | ||
| }) | ||
| } | ||
| }, | ||
| stopServer: function(callback){ | ||
| self.server.stop(function(){ | ||
| callback(); | ||
| }); | ||
| }, | ||
| }; | ||
| stopServer: function (callback) { | ||
| var self = this | ||
| self.server.stop(function () { | ||
| callback() | ||
| }) | ||
| } | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
35311
1.61%530
1.92%4
33.33%3
50%