diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md index 3b38214..dc479ae 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ node-cgminer ============ +Forked from: [node-cgminer](https://github.com/tlrobinson/node-cgminer) + Usage ----- CGMiner command return a Promises/A compatible promise (specifically a [Q](https://github.com/kriskowal/q) promise): - var client = new CGMinerClient(HOST, PORT); + var client = new CGMinerClient({host: HOST, port: PORT, timeout: TIMEOUT}); client.COMMAND(ARG1, ARG2).then(function(results) { console.log(results); }, function(err) { @@ -14,9 +16,3 @@ CGMiner command return a Promises/A compatible promise (specifically a [Q](https }); COMMAND corresponds to one of the commands detailed in [CGMiner's API documentation](https://github.com/ckolivas/cgminer/blob/master/API-README). - -TODO ----- - -* Some commands return the raw JSON response object while other return a customized object (`devs`, etc). -* Documentation. diff --git a/bin/cgminer-cli b/bin/cgminer-cli deleted file mode 100755 index 170bdaf..0000000 --- a/bin/cgminer-cli +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env coffee - -CGMinerClient = require "../index" - -client = new CGMinerClient - host: process.env.CGMINER_HOST or "localhost" - port: process.env.CGMINER_PORT or 4028 - -command = process.argv[2].toLowerCase() - -if command is "help" - if process.argv.length > 3 - command = process.argv[3].toLowerCase() - info = CGMinerClient.commands[command] - console.log "Command:", info.name, if info.privledged then "(privledged)" else "" - console.log "Arguments:", info.args if info.args? - console.log "Reply:", info.reply if info.reply? - console.log "Details:\n" - console.log info.details.split("\n").map((l) -> " " + l).join("\n") - console.log "" - else - for name, command of CGMinerClient.commands - console.log name + " " + - (if command.args? then "#{command.args} " else "") + - (if command.reply? then "-> " + command.reply else "") -else - client[command].apply(client, process.argv[3..]).then (result) -> - console.log result - .done() diff --git a/commands.coffee b/commands.coffee deleted file mode 100644 index 56b9764..0000000 --- a/commands.coffee +++ /dev/null @@ -1,16 +0,0 @@ - -# -REGEX = /^([\w-]+)(?:\|([^ ]+))?( \(\*\))?[\n\s]+(\w+)((?:.|\n)*)$/ - -exports.getCommands = -> - docs = require("fs").readFileSync("#{__dirname}/commands.txt", "utf-8") - parsed = docs.split(/\n\n /g).map (command) -> command.match REGEX - commands = {} - for command in parsed when command? - commands[command[1]] = - name: command[1] - args: command[2] - privileged: !!command[3] - reply: if command[4] isnt "none" then command[4] else null - details: command[5].split("\n").map((l) -> l.replace(/^\s+/, "")).join("\n") - commands diff --git a/commands.js b/commands.js new file mode 100644 index 0000000..4b2f41c --- /dev/null +++ b/commands.js @@ -0,0 +1,30 @@ +var REGEX; + +REGEX = /^([\w-]+)(?:\|([^ ]+))?( \(\*\))?[\n\s]+(\w+)((?:.|\n)*)$/; + +exports.getCommands = function() { + var command, commands, docs, i, len, parsed; + docs = require("fs").readFileSync(__dirname + "/commands.txt", "utf-8"); + parsed = docs.split(/\n\n /g).map(function(command) { + return command.match(REGEX); + }); + commands = {}; + for (i = 0, len = parsed.length; i < len; i++) { + command = parsed[i]; + if (command != null) { + commands[command[1]] = { + name: command[1], + args: command[2], + privileged: !!command[3], + reply: command[4] !== "none" ? command[4] : null, + details: command[5].split("\n").map(function(l) { + return l.replace(/^\s+/, ""); + }).join("\n") + }; + } + } + return commands; +}; + +// --- +// generated by coffee-script 1.9.2 \ No newline at end of file diff --git a/example.coffee b/example.coffee deleted file mode 100644 index b6de49d..0000000 --- a/example.coffee +++ /dev/null @@ -1,9 +0,0 @@ -CGMinerClient = require "./index" - -client = new CGMinerClient() - -client.version().then (result) -> - console.log result - client.summary().then (result) -> - console.log result -.done() diff --git a/index.coffee b/index.coffee deleted file mode 100644 index fdb7408..0000000 --- a/index.coffee +++ /dev/null @@ -1,47 +0,0 @@ -net = require "net" -Q = require "q" - -class CGMinerClient - constructor: (options = {}) -> - @host = options.host or "127.0.0.1" - @port = options.port or 4028 - request: (command, args...) -> - deferred = Q.defer() - socket = net.connect host: @host, port: @port - socket.on "error", (err) -> - deferred.reject err - socket.on "connect", -> - buffer = "" - socket.on "data", (data) -> - buffer += data.toString() - socket.on "end", -> - try - deferred.resolve JSON.parse buffer.replace(/[^\}]+$/, "") - catch err - deferred.reject err - socket.write JSON.stringify - command: command - parameter: args.join(",") - deferred.promise - - _version: (r) -> r.VERSION[0] - _devs: (r) -> r.DEVS - -for device in ["pga", "gpu", "asc"] - do (device) -> - deviceUC = device.toUpperCase() - CGMinerClient::["_#{device}"] = (r) -> r["#{deviceUC}"][0] - CGMinerClient::["_#{device}count"] = (r) -> r["#{deviceUC}S"][0].Count - -CGMinerClient.commands = require("./commands").getCommands() - -for name, command of CGMinerClient.commands - do (name, command) -> - CGMinerClient::[name] = (args...) -> - @request.apply(@, [name].concat(args)).then (result) => - if @["_#{name}"]? - Q.try => @["_#{name}"] result - else - result - -module.exports = CGMinerClient diff --git a/index.js b/index.js new file mode 100644 index 0000000..b2bbfec --- /dev/null +++ b/index.js @@ -0,0 +1,117 @@ +var CGMinerClient, Q, command, device, fn, fn1, i, len, name, net, ref, ref1, + slice = [].slice; + +net = require("net"); +Q = require("q"); + +CGMinerClient = (function() { + function CGMinerClient(options) { + if (options == null) { + options = {}; + } + this.host = options.host || "127.0.0.1"; + this.port = options.port || 4028; + this.timeout = options.timeout || 3000; + } + + CGMinerClient.prototype.request = function() { + var args, command, deferred, socket; + command = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : []; + + deferred = Q.defer(); + + // Set a timeout to receive data otherwise thrown an error + Q.delay(this.timeout).then(function () { + deferred.reject(new Error("Timed out")); + }); + + socket = net.connect({ + host: this.host, + port: this.port + }); + + socket.on("error", function(err) { + return deferred.reject(err); + }); + + socket.on("connect", function() { + var buffer; + buffer = ""; + + socket.on("data", function(data) { + return buffer += data.toString(); + }); + + socket.on("end", function() { + var err; + try { + Q.when(buffer, deferred.resolve(JSON.parse(buffer.replace(/[^\}]+$/, "")))); + // return deferred.resolve(JSON.parse(buffer.replace(/[^\}]+$/, ""))); + } catch (_error) { + err = _error; + return deferred.reject(err); + } + }); + + return socket.write(JSON.stringify({ + command: command, + parameter: args.join(",") + })); + }); + return deferred.promise; + }; + + CGMinerClient.prototype._version = function(r) { + return r.VERSION[0]; + }; + + CGMinerClient.prototype._devs = function(r) { + return r.DEVS; + }; + + return CGMinerClient; + +})(); + +ref = ["pga", "gpu", "asc"]; +fn = function(device) { + var deviceUC; + deviceUC = device.toUpperCase(); + CGMinerClient.prototype["_" + device] = function(r) { + return r["" + deviceUC][0]; + }; + return CGMinerClient.prototype["_" + device + "count"] = function(r) { + return r[deviceUC + "S"][0].Count; + }; +}; +for (i = 0, len = ref.length; i < len; i++) { + device = ref[i]; + fn(device); +} + +CGMinerClient.commands = require("./commands").getCommands(); + +ref1 = CGMinerClient.commands; +fn1 = function(name, command) { + return CGMinerClient.prototype[name] = function() { + var args; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; + return this.request.apply(this, [name].concat(args)).then((function(_this) { + return function(result) { + if (_this["_" + name] != null) { + return Q["try"](function() { + return _this["_" + name](result); + }); + } else { + return result; + } + }; + })(this)); + }; +}; +for (name in ref1) { + command = ref1[name]; + fn1(name, command); +} + +module.exports = CGMinerClient; diff --git a/package.json b/package.json index f83a5e2..ff51ad3 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,50 @@ { "name": "cgminer", - "version": "0.1.0", + "version": "0.1.1", "description": "API client for CGMiner Bitcoin miner", - "main": "index.coffee", + "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "https://github.com/tlrobinson/node-cgminer.git" + "url": "git+https://github.com/minera/node-cgminer.git" }, "keywords": [ "bitcoin", "cgminer", + "bfgminer", "client", "promises", "promises/a", "promises/a+", "q" ], - "author": "Tom Robinson", + "author": { + "name": "Michele Marcucci" + }, "license": "BSD", "dependencies": { - "q": "~0.9.6", - "coffee-script": "~1.6.3" + "q": "~0.9.6" + }, + "readme": "node-cgminer\n============\n\nUsage\n-----\n\nCGMiner command return a Promises/A compatible promise (specifically a [Q](https://github.com/kriskowal/q) promise):\n\n var client = new CGMinerClient(HOST, PORT);\n client.COMMAND(ARG1, ARG2).then(function(results) {\n console.log(results);\n }, function(err) {\n // error handler\n });\n\nCOMMAND corresponds to one of the commands detailed in [CGMiner's API documentation](https://github.com/ckolivas/cgminer/blob/master/API-README).\n\nTODO\n----\n\n* Some commands return the raw JSON response object while other return a customized object (`devs`, etc).\n* Documentation.", + "readmeFilename": "README.md", + "bugs": { + "url": "https://github.com/minera/node-cgminer/issues" }, - "bin": { "cgminer-cli": "./bin/cgminer-cli" } + "_id": "cgminer@0.1.1", + "_from": "cgminer@latest", + "_npmVersion": "1.2.25", + "_npmUser": { + "name": "minera", + "email": "support@minera.com" + }, + "maintainers": [ + { + "name": "minera", + "email": "support@minera.com" + } + ], + "directories": {}, + "homepage": "https://github.com/minera/node-cgminer#readme" }