request.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Generated by IcedCoffeeScript 1.7.1-f
  2. (function() {
  3. var ProgressBar, Request, format_url, http, https, iced, parse, request, request_mikeal, request_progress, single, urlmod, __iced_k, __iced_k_noop;
  4. iced = require('iced-runtime');
  5. __iced_k = __iced_k_noop = function() {};
  6. https = require('https');
  7. http = require('http');
  8. parse = require('url').parse;
  9. ProgressBar = require('progress');
  10. urlmod = require('url');
  11. request = require('request');
  12. Request = (function() {
  13. function Request(_arg) {
  14. var progress, uri, url;
  15. url = _arg.url, uri = _arg.uri, this.headers = _arg.headers, progress = _arg.progress;
  16. url = url || uri;
  17. this._res = null;
  18. this._data = [];
  19. this._err = null;
  20. this.uri = this.url = typeof url === 'string' ? parse(url) : url;
  21. this._bar = null;
  22. this._opts = {
  23. progress: progress
  24. };
  25. }
  26. Request.prototype.run = function(cb) {
  27. this._done_cb = cb;
  28. return this._launch();
  29. };
  30. Request.prototype._make_opts = function() {
  31. var opts;
  32. opts = {
  33. host: this.url.hostname,
  34. port: this.url.port || (this.url.protocol === 'https:' ? 443 : 80),
  35. path: this.url.path,
  36. method: 'GET',
  37. headers: this.headers
  38. };
  39. if (this.url.protocol === 'https:') {
  40. opts.mod = https;
  41. opts.agent = new https.Agent(opts);
  42. } else {
  43. opts.mod = http;
  44. }
  45. return opts;
  46. };
  47. Request.prototype._launch = function() {
  48. var opts, req;
  49. opts = this._make_opts();
  50. req = opts.mod.request(opts, (function(_this) {
  51. return function(res) {
  52. var l, _ref;
  53. if ((_this._opts.progress != null) && ((l = (_ref = res.headers) != null ? _ref["content-length"] : void 0) != null) && !(isNaN(l = parseInt(l, 10))) && l > _this._opts.progress) {
  54. _this._bar = new ProgressBar("Download " + _this.url.path + " [:bar] :percent :etas (" + l + " bytes total)", {
  55. complete: "=",
  56. incomplete: ' ',
  57. width: 50,
  58. total: l
  59. });
  60. }
  61. _this._res = res;
  62. res.request = _this;
  63. res.on('data', function(d) {
  64. var _ref1;
  65. _this._data.push(d);
  66. return (_ref1 = _this._bar) != null ? _ref1.tick(d.length) : void 0;
  67. });
  68. return res.on('end', function() {
  69. return _this._finish();
  70. });
  71. };
  72. })(this));
  73. req.end();
  74. return req.on('error', (function(_this) {
  75. return function(e) {
  76. _this._err = e;
  77. return _this._finish();
  78. };
  79. })(this));
  80. };
  81. Request.prototype._finish = function() {
  82. var cb;
  83. cb = this._done_cb;
  84. this._done_cb = null;
  85. return cb(this._err, this._res, Buffer.concat(this._data));
  86. };
  87. return Request;
  88. })();
  89. single = function(opts, cb) {
  90. return (new Request(opts)).run(cb);
  91. };
  92. format_url = function(u) {
  93. if (typeof u === 'string') {
  94. return u;
  95. } else {
  96. return urlmod.format(u);
  97. }
  98. };
  99. request_progress = function(opts, cb) {
  100. var body, err, found, i, lim, prev_url, res, url, ___iced_passed_deferral, __iced_deferrals, __iced_k, _begin, _end, _positive;
  101. __iced_k = __iced_k_noop;
  102. ___iced_passed_deferral = iced.findDeferral(arguments);
  103. lim = opts.maxRedirects || 10;
  104. res = body = null;
  105. found = false;
  106. if (typeof opts.url === 'string') {
  107. opts.url = parse(opts.url);
  108. }
  109. (function(_this) {
  110. return (function(__iced_k) {
  111. var _i, _results, _while;
  112. i = 0;
  113. _begin = 0;
  114. _end = lim;
  115. _positive = _end > _begin;
  116. _results = [];
  117. _while = function(__iced_k) {
  118. var _break, _continue, _next;
  119. _break = function() {
  120. return __iced_k(_results);
  121. };
  122. _continue = function() {
  123. return iced.trampoline(function() {
  124. if (_positive) {
  125. i += 1;
  126. } else {
  127. i -= 1;
  128. }
  129. return _while(__iced_k);
  130. });
  131. };
  132. _next = function(__iced_next_arg) {
  133. _results.push(__iced_next_arg);
  134. return _continue();
  135. };
  136. if (!!((_positive === true && i >= lim) || (_positive === false && i <= lim))) {
  137. return _break();
  138. } else {
  139. prev_url = opts.url;
  140. (function(__iced_k) {
  141. __iced_deferrals = new iced.Deferrals(__iced_k, {
  142. parent: ___iced_passed_deferral,
  143. filename: "/home/max/src/keybase/node-installer/src/request.iced"
  144. });
  145. single(opts, __iced_deferrals.defer({
  146. assign_fn: (function() {
  147. return function() {
  148. err = arguments[0];
  149. res = arguments[1];
  150. return body = arguments[2];
  151. };
  152. })(),
  153. lineno: 93
  154. }));
  155. __iced_deferrals._fulfill();
  156. })(function() {
  157. (function(__iced_k) {
  158. if (err != null) {
  159. (function(__iced_k) {
  160. _break()
  161. })(__iced_k);
  162. } else {
  163. (function(__iced_k) {
  164. var _ref;
  165. if (!((_ref = res.statusCode) === 301 || _ref === 302)) {
  166. found = true;
  167. (function(__iced_k) {
  168. _break()
  169. })(__iced_k);
  170. } else {
  171. (function(__iced_k) {
  172. var _ref1;
  173. if ((url = (_ref1 = res.headers) != null ? _ref1.location : void 0) == null) {
  174. err = new Error("Can't find a location in header for redirect");
  175. (function(__iced_k) {
  176. _break()
  177. })(__iced_k);
  178. } else {
  179. url = parse(url);
  180. if (!url.host) {
  181. url.host = prev_url.host;
  182. url.hostname = prev_url.hostname;
  183. url.port = prev_url.port;
  184. url.protocol = prev_url.protocol;
  185. }
  186. return __iced_k(opts.url = url);
  187. }
  188. })(__iced_k);
  189. }
  190. })(__iced_k);
  191. }
  192. })(_next);
  193. });
  194. }
  195. };
  196. _while(__iced_k);
  197. });
  198. })(this)((function(_this) {
  199. return function() {
  200. err = err != null ? err : !found ? new Error("Too many redirects") : res.statusCode >= 200 && res.statusCode < 300 ? null : new Error("In " + (format_url(opts.url)) + ": HTTP failure, code " + res.statusCode);
  201. return cb(err, res, body);
  202. };
  203. })(this));
  204. };
  205. request_mikeal = function(opts, cb) {
  206. var body, err, res, rv, url, url_s, which, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  207. __iced_k = __iced_k_noop;
  208. ___iced_passed_deferral = iced.findDeferral(arguments);
  209. opts.encoding = null;
  210. rv = new iced.Rendezvous();
  211. url = opts.url || opts.uri;
  212. url_s = typeof url === 'object' ? url.format() : url;
  213. request(opts, rv.id(true).defer({
  214. assign_fn: (function(_this) {
  215. return function() {
  216. return function() {
  217. err = arguments[0];
  218. res = arguments[1];
  219. return body = arguments[2];
  220. };
  221. };
  222. })(this)(),
  223. lineno: 124,
  224. context: __iced_deferrals
  225. }));
  226. process.stderr.write("Downloading...");
  227. (function(_this) {
  228. return (function(__iced_k) {
  229. var _results, _while;
  230. _results = [];
  231. _while = function(__iced_k) {
  232. var _break, _continue, _next;
  233. _break = function() {
  234. return __iced_k(_results);
  235. };
  236. _continue = function() {
  237. return iced.trampoline(function() {
  238. return _while(__iced_k);
  239. });
  240. };
  241. _next = function(__iced_next_arg) {
  242. _results.push(__iced_next_arg);
  243. return _continue();
  244. };
  245. if (!true) {
  246. return _break();
  247. } else {
  248. setTimeout(rv.id(false).defer({
  249. lineno: 128,
  250. context: __iced_deferrals
  251. }), 100);
  252. (function(__iced_k) {
  253. __iced_deferrals = new iced.Deferrals(__iced_k, {
  254. parent: ___iced_passed_deferral,
  255. filename: "/home/max/src/keybase/node-installer/src/request.iced"
  256. });
  257. rv.wait(__iced_deferrals.defer({
  258. assign_fn: (function() {
  259. return function() {
  260. return which = arguments[0];
  261. };
  262. })(),
  263. lineno: 129
  264. }));
  265. __iced_deferrals._fulfill();
  266. })(function() {
  267. (function(__iced_k) {
  268. if (which) {
  269. (function(__iced_k) {
  270. _break()
  271. })(__iced_k);
  272. } else {
  273. return __iced_k();
  274. }
  275. })(function() {
  276. return _next(process.stderr.write("."));
  277. });
  278. });
  279. }
  280. };
  281. _while(__iced_k);
  282. });
  283. })(this)((function(_this) {
  284. return function() {
  285. process.stderr.write("..done\n");
  286. return cb(err, res, body);
  287. };
  288. })(this));
  289. };
  290. exports.request = function(opts, cb) {
  291. if (opts.proxy != null) {
  292. return request_mikeal(opts, cb);
  293. } else {
  294. return request_progress(opts, cb);
  295. }
  296. };
  297. }).call(this);