diff --git a/lib/cli-common.js b/lib/cli-common.js index f413fe5..9b04c18 100644 --- a/lib/cli-common.js +++ b/lib/cli-common.js @@ -1,5 +1,6 @@ 'use strict'; +module.exports.debug = (-1 !== (process.env.NODE_DEBUG||'').split(/\s+/g).indexOf('telebit')); var common = module.exports; var path = require('path'); @@ -157,17 +158,17 @@ common.api.token = function (state, handlers) { common.api.directory(state, function (err, dir) { // directory, requested, connect, tunnelUrl, offer, granted, end function afterDir() { - console.log('[debug] after dir'); + if (common.debug) { console.log('[debug] after dir'); } state.wss = common.api._parseWss(state, dir); handlers.tunnelUrl(state.wss, function () { - console.log('[debug] after tunnelUrl'); + if (common.debug) { console.log('[debug] after tunnelUrl'); } if (state.config.secret /* && !state.config.token */) { state.config._token = common.signToken(state); } state.token = state.token || state.config.token || state.config._token; if (state.token) { - console.log('[debug] token via token or secret'); + if (common.debug) { console.log('[debug] token via token or secret'); } // { token, pretoken } handlers.connect(state.token, function () { handlers.end(null, function () {}); @@ -177,7 +178,7 @@ common.api.token = function (state, handlers) { // backwards compat (TODO remove) if (err || !dir || !dir.pair_request) { - console.log('[debug] no dir, connect'); + if (common.debug) { console.log('[debug] no dir, connect'); } handlers.error(new Error("No token found or generated, and no pair_request api found.")); return; } @@ -209,11 +210,11 @@ common.api.token = function (state, handlers) { var firstReady = true; function gotoNext(req) { - console.log('[debug] gotoNext called'); - console.log(req); + if (common.debug) { console.log('[debug] gotoNext called'); } + if (common.debug) { console.log(req); } urequest(req, function (err, resp, body) { if (err) { - console.log('[debug] gotoNext error'); + if (common.debug) { console.log('[debug] gotoNext error'); } err._request = req; err._hint = '[telebitd.js] pair request'; handlers.error(err, function () {}); @@ -221,19 +222,19 @@ common.api.token = function (state, handlers) { } function checkLocation() { - console.log('[debug] checkLocation'); - console.log(body); + if (common.debug) { console.log('[debug] checkLocation'); } + if (common.debug) { console.log(body); } // pending, try again if ('pending' === body.status && resp.headers.location) { - console.log('[debug] pending'); + if (common.debug) { console.log('[debug] pending'); } setTimeout(gotoNext, 2 * 1000, { url: resp.headers.location, json: true }); return; } if ('ready' === body.status) { - console.log('[debug] ready'); + if (common.debug) { console.log('[debug] ready'); } if (firstReady) { - console.log('[debug] first ready'); + if (common.debug) { console.log('[debug] first ready'); } firstReady = false; state.token = body.access_token; state.config.token = state.token; @@ -246,21 +247,21 @@ common.api.token = function (state, handlers) { } if ('complete' === body.status) { - console.log('[debug] complete'); + if (common.debug) { console.log('[debug] complete'); } handlers.granted(null, function () { handlers.end(null, function () {}); }); return; } - console.log('[debug] bad status'); + if (common.debug) { console.log('[debug] bad status'); } var err = new Error("Bad State:" + body.status); err._request = req; handlers.error(err, function () {}); } if (firstReq) { - console.log('[debug] first req'); + if (common.debug) { console.log('[debug] first req'); } handlers.requested(authReq, function () { handlers.connect(body.access_token || body.jwt, function () { var err; @@ -276,7 +277,7 @@ common.api.token = function (state, handlers) { firstReq = false; return; } else { - console.log('[debug] other req'); + if (common.debug) { console.log('[debug] other req'); } checkLocation(); } });