req.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Generated by IcedCoffeeScript 1.7.1-c
  2. (function() {
  3. var Client, E, PackageJson, certs, dcopy, env, iced, k, log, m, proxyca, request, tor, urlmod, __iced_k, __iced_k_noop, _cli, _fn,
  4. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
  5. __slice = [].slice;
  6. iced = require('iced-runtime').iced;
  7. __iced_k = __iced_k_noop = function() {};
  8. request = require('request');
  9. env = require('./env').env;
  10. urlmod = require('url');
  11. E = require('./err').E;
  12. log = require('./log');
  13. certs = require('./ca').certs;
  14. PackageJson = require('./package').PackageJson;
  15. proxyca = require('./proxyca');
  16. tor = require('./tor');
  17. m = function(dict, method) {
  18. dict.method = method;
  19. return dict;
  20. };
  21. dcopy = function(d) {
  22. var k, out, v;
  23. out = {};
  24. for (k in d) {
  25. v = d[k];
  26. out[k] = v;
  27. }
  28. return out;
  29. };
  30. exports.Client = Client = (function() {
  31. function Client(headers) {
  32. this.headers = headers;
  33. this._cookies = {};
  34. this._session = null;
  35. this._csrf = null;
  36. this._warned = false;
  37. }
  38. Client.prototype.set_headers = function(h) {
  39. return this.headers = h;
  40. };
  41. Client.prototype.get_headers = function() {
  42. return this.headers;
  43. };
  44. Client.prototype.add_headers = function(d) {
  45. var k, v;
  46. this.headers || (this.headers = {});
  47. for (k in d) {
  48. v = d[k];
  49. this.headers[k] = v;
  50. }
  51. return true;
  52. };
  53. Client.prototype.set_session = function(s) {
  54. if (!tor.strict()) {
  55. this.add_headers({
  56. "X-Keybase-Session": s
  57. });
  58. return this._session = s;
  59. }
  60. };
  61. Client.prototype.clear_session = function() {
  62. this._session = null;
  63. if (this.headers != null) {
  64. return delete this.headers['X-Keybase-Session'];
  65. }
  66. };
  67. Client.prototype.clear_csrf = function() {
  68. this._csrf = null;
  69. if (this.headers != null) {
  70. return delete this.headers['X-CSRF-Token'];
  71. }
  72. };
  73. Client.prototype.set_csrf = function(c) {
  74. if (!tor.strict()) {
  75. this.add_headers({
  76. "X-CSRF-Token": c
  77. });
  78. return this._csrf = c;
  79. }
  80. };
  81. Client.prototype.get_session = function() {
  82. return this._session;
  83. };
  84. Client.prototype.get_csrf = function() {
  85. return this._csrf;
  86. };
  87. Client.prototype._find_cookies = function(res) {
  88. var cookie_line, name, parts, v, val, _i, _len, _ref, _ref1, _results;
  89. if ((v = (_ref = res.headers) != null ? _ref['set-cookie'] : void 0) != null) {
  90. _results = [];
  91. for (_i = 0, _len = v.length; _i < _len; _i++) {
  92. cookie_line = v[_i];
  93. parts = cookie_line.split("; ");
  94. if (parts.length) {
  95. _ref1 = parts[0].split("="), name = _ref1[0], val = _ref1[1];
  96. _results.push(this._cookies[name] = decodeURIComponent(val));
  97. } else {
  98. _results.push(void 0);
  99. }
  100. }
  101. return _results;
  102. }
  103. };
  104. Client.prototype.error_for_humans = function(_arg) {
  105. var err, host, port, uri, uri_fields;
  106. err = _arg.err, uri = _arg.uri, uri_fields = _arg.uri_fields;
  107. host = uri_fields.hostname;
  108. port = uri_fields.port;
  109. switch (err.code) {
  110. case 'ENOTFOUND':
  111. return new E.ReqNotFoundError("Host '" + host + "' wasn't found in DNS; check your network");
  112. case 'ECONNREFUSED':
  113. return new E.ReqConnRefusedError("Host '" + host + ":" + port + "' refused connection; maybe the server is down");
  114. default:
  115. return new E.ReqGenericError("Could not access URL: " + uri);
  116. }
  117. };
  118. Client.prototype.req = function(_arg, cb) {
  119. var args, body, ca, endpoint, err, http_status, jar, json, kb_status, method, need_cookie, opts, pathname, pcc, pjs, prx, res, search, tha, tls, tor_on, uri_fields, v, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  120. __iced_k = __iced_k_noop;
  121. ___iced_passed_deferral = iced.findDeferral(arguments);
  122. method = _arg.method, endpoint = _arg.endpoint, args = _arg.args, http_status = _arg.http_status, kb_status = _arg.kb_status, pathname = _arg.pathname, search = _arg.search, json = _arg.json, jar = _arg.jar, need_cookie = _arg.need_cookie;
  123. method || (method = 'GET');
  124. tha = null;
  125. if ((tor_on = tor.enabled())) {
  126. tha = tor.hidden_address();
  127. log.debug("| Using tor hidden address: " + (JSON.stringify(tha)));
  128. }
  129. if (!(jar != null) && !(tor.strict()) && (need_cookie || !tor_on)) {
  130. jar = true;
  131. }
  132. if (json == null) {
  133. json = true;
  134. }
  135. opts = {
  136. method: method,
  137. json: json
  138. };
  139. if (jar != null) {
  140. opts.jar = jar;
  141. }
  142. opts.headers = this.headers != null ? dcopy(this.headers) : {};
  143. pjs = new PackageJson;
  144. opts.headers["X-Keybase-Client"] = pjs.identify_as();
  145. opts.headers["User-Agent"] = pjs.user_agent();
  146. kb_status || (kb_status = ["OK"]);
  147. http_status || (http_status = [200]);
  148. tls = !tor_on && !(env().get_no_tls());
  149. uri_fields = {
  150. protocol: "http" + (tls ? 's' : ''),
  151. hostname: tor_on ? tha.hostname : env().get_host(),
  152. port: tor_on ? tha.port : env().get_port(),
  153. pathname: pathname || [env().get_api_uri_prefix(), endpoint + ".json"].join("/"),
  154. search: search
  155. };
  156. if (method === 'GET' || method === 'DELETE') {
  157. uri_fields.query = args;
  158. }
  159. opts.uri = urlmod.format(uri_fields);
  160. if (method === 'POST') {
  161. opts.body = args;
  162. }
  163. log.debug("+ request to " + endpoint + " (" + opts.uri + ") (cookie=" + (!!jar) + ")");
  164. if ((prx = env().get_proxy()) != null) {
  165. log.debug("| using proxy " + prx);
  166. opts.proxy = prx;
  167. }
  168. if (!tls) {
  169. } else if ((opts.proxy != null) && ((pcc = proxyca.get()) != null)) {
  170. log.debug("| Using proxy CA certs " + (pcc.files().join(':')));
  171. opts.ca = pcc.data().concat([ca]);
  172. } else if ((ca = certs[uri_fields.hostname]) != null) {
  173. log.debug("| Adding a custom CA for host " + uri_fields.hostname + " when tls=" + tls);
  174. opts.ca = [ca];
  175. }
  176. tor.agent(opts);
  177. (function(_this) {
  178. return (function(__iced_k) {
  179. __iced_deferrals = new iced.Deferrals(__iced_k, {
  180. parent: ___iced_passed_deferral,
  181. filename: "/Users/max/src/keybase/node-client/src/req.iced",
  182. funcname: "Client.req"
  183. });
  184. request(opts, __iced_deferrals.defer({
  185. assign_fn: (function() {
  186. return function() {
  187. err = arguments[0];
  188. res = arguments[1];
  189. return body = arguments[2];
  190. };
  191. })(),
  192. lineno: 152
  193. }));
  194. __iced_deferrals._fulfill();
  195. });
  196. })(this)((function(_this) {
  197. return function() {
  198. var _ref, _ref1, _ref2, _ref3, _ref4;
  199. if (typeof err !== "undefined" && err !== null) {
  200. err = _this.error_for_humans({
  201. err: err,
  202. uri: opts.uri,
  203. uri_fields: uri_fields
  204. });
  205. } else if (!(_ref = res.statusCode, __indexOf.call(http_status, _ref) >= 0)) {
  206. if (res.statusCode === 400 && ((_ref1 = res.headers) != null ? _ref1["x-keybase-client-unsupported"] : void 0)) {
  207. v = res.headers["x-keybase-client-upgrade-to"];
  208. err = new E.RequiredUpgradeError("Upgrade is required! Run `keybase-installer` to upgrade to v" + v);
  209. err.upgrade_to = v;
  210. } else {
  211. err = new E.HttpError("Got reply " + res.statusCode);
  212. }
  213. } else if (json && !(_ref2 = typeof body !== "undefined" && body !== null ? (_ref3 = body.status) != null ? _ref3.name : void 0 : void 0, __indexOf.call(kb_status, _ref2) >= 0)) {
  214. err = new E.KeybaseError("" + body.status.desc + " (error #" + body.status.code + ")");
  215. err.fields = ((_ref4 = body.status) != null ? _ref4.fields : void 0) || {};
  216. opts.agent = null;
  217. log.debug("Full request: " + (JSON.stringify(opts)));
  218. log.debug("Full reply: " + (JSON.stringify(body)));
  219. } else {
  220. if (((v = res.headers["x-keybase-client-upgrade-to"]) != null) && !_this._warned) {
  221. log.warn("Upgrade suggested! Run `keybase-installer` to upgrade to v" + v);
  222. _this._warned = true;
  223. }
  224. _this._find_cookies(res);
  225. }
  226. log.debug("- request to " + endpoint + " -> " + err);
  227. return cb(err, body, res);
  228. };
  229. })(this));
  230. };
  231. Client.prototype.post = function(args, cb) {
  232. return this.req(m(args, "POST"), cb);
  233. };
  234. Client.prototype.get = function(args, cb) {
  235. return this.req(m(args, "GET"), cb);
  236. };
  237. Client.prototype.cookies = function() {
  238. return this._cookies;
  239. };
  240. return Client;
  241. })();
  242. exports.client = _cli = new Client();
  243. _fn = function(fname) {
  244. return exports[fname] = function() {
  245. var args;
  246. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  247. return _cli[fname].apply(_cli, args);
  248. };
  249. };
  250. for (k in Client.prototype) {
  251. _fn(k);
  252. }
  253. }).call(this);