[tor-commits] [flashproxy/master] node-flashproxy: read unix-style command line options using optimist

infinity0 at torproject.org infinity0 at torproject.org
Fri Nov 22 13:19:27 UTC 2013


commit 293987a90a7bd2aaa19aeac90ad076788b6e73c4
Author: Ximin Luo <infinity0 at gmx.com>
Date:   Thu Nov 21 23:19:59 2013 +0000

    node-flashproxy: read unix-style command line options using optimist
    - this is backwards-compatible; non-hyphenated args are concatenated to the querystring
---
 proxy/modules/nodejs/INSTALL      |    2 +-
 proxy/modules/nodejs/main.js      |   41 +++++++++++++++++++++++++++++++------
 proxy/modules/nodejs/package.json |    1 +
 3 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/proxy/modules/nodejs/INSTALL b/proxy/modules/nodejs/INSTALL
index 9f6f677..adef21a 100644
--- a/proxy/modules/nodejs/INSTALL
+++ b/proxy/modules/nodejs/INSTALL
@@ -10,7 +10,7 @@ a plain copy.
 If your distribution contains both the following packages (newer suites of
 Debian and Ubuntu), you can run main.js directly:
 
-    # apt-get install node-xmlhttprequest node-ws
+    # apt-get install node-optimist node-xmlhttprequest node-ws
     $ nodejs main.js
 
 Otherwise, you can use npm to install it as a package, along with all its
diff --git a/proxy/modules/nodejs/main.js b/proxy/modules/nodejs/main.js
index 64e50b9..cfadff5 100755
--- a/proxy/modules/nodejs/main.js
+++ b/proxy/modules/nodejs/main.js
@@ -1,14 +1,42 @@
 #!/usr/bin/env node
 
-var path = require("path");
 var fs = require("fs");
+var path = require("path");
+var querystring = require("querystring")
+
+var meta = require("./package.json")
 
 // Get a querystring from the command line
-var location_search = "debug=1&initial_facilitator_poll_interval=10";
-if (process.argv[2] != null)
-    location_search = process.argv[2];
+var argv = require("optimist")
+    .default("debug", 1)
+    .default("initial_facilitator_poll_interval", 10)
+    .argv
+
+if ("v" in argv || "version" in argv) {
+    console.log(meta.version)
+    process.exit()
+}
+if ("h" in argv || "help" in argv) {
+    console.log("Usage: %s [-h|-v] [--param[=val]] ... [extra querystring]\n\
+\n\
+Run flashproxy on the node.js server. You can give querystring parameters as \n\
+command line options; see the main flashproxy.js program for documentation on \n\
+which parameters are accepted. For example: \n\
+\n\
+%s --debug --initial_facilitator_poll_interval=10\n\
+", argv.$0, argv.$0)
+    process.exit()
+}
+
+var extra = argv._.join("&")
+delete argv._
+delete argv.$0
+var location_search = querystring.stringify(argv)
+if (extra) {
+    location_search += "&" + extra
+}
 
-// Setup environment variables for node.js
+// Setup global variables that flashproxy.js expects
 var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
 var window = {
     location: { search: "?" + location_search },
@@ -16,7 +44,8 @@ var window = {
     WebSocket: require("ws")
 };
 
-// Include flashproxy.js using eval to avoid modifying it.
+// Include flashproxy.js using eval to run it in the scope of this script
+// so we don't need to make non-browser adjustments to flashproxy.js
 var file = path.join(__dirname, "flashproxy.js");
 try {
     var data = fs.readFileSync(file, "utf8");
diff --git a/proxy/modules/nodejs/package.json b/proxy/modules/nodejs/package.json
index 1de1157..079865c 100644
--- a/proxy/modules/nodejs/package.json
+++ b/proxy/modules/nodejs/package.json
@@ -11,6 +11,7 @@
         "flashproxy": "main.js"
     },
     "dependencies": {
+        "optimist": "0.3.5",
         "xmlhttprequest": "1.5.0",
         "ws": "0.4.27"
     },





More information about the tor-commits mailing list