hkp_loopback.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Generated by IcedCoffeeScript 1.7.1-c
  2. (function() {
  3. var E, HKPLoopback, env, http, iced, log, reqmod, urlmod, __iced_k, __iced_k_noop;
  4. iced = require('iced-runtime').iced;
  5. __iced_k = __iced_k_noop = function() {};
  6. http = require('http');
  7. env = require('./env').env;
  8. reqmod = require('./req');
  9. log = require('./log');
  10. E = require('./env').E;
  11. urlmod = require('url');
  12. exports.HKPLoopback = HKPLoopback = (function() {
  13. function HKPLoopback() {
  14. this._srv = http.createServer(this.serve.bind(this));
  15. this._port = null;
  16. this._hostname = "127.0.0.1";
  17. }
  18. HKPLoopback.prototype.url = function() {
  19. return "hkp://" + this._hostname + ":" + this._port + "/";
  20. };
  21. HKPLoopback.prototype.listen = function(cb) {
  22. var err, p, r, ___iced_passed_deferral, __iced_deferrals, __iced_k, _begin, _end, _positive;
  23. __iced_k = __iced_k_noop;
  24. ___iced_passed_deferral = iced.findDeferral(arguments);
  25. log.debug("+ HKPLoopback::init; hunt for a port");
  26. r = env().get_loopback_port_range();
  27. (function(_this) {
  28. return (function(__iced_k) {
  29. var _i, _results, _while;
  30. p = r[0];
  31. _begin = r[0];
  32. _end = r[1];
  33. _positive = _end > _begin;
  34. _results = [];
  35. _while = function(__iced_k) {
  36. var _break, _continue, _next;
  37. _break = function() {
  38. return __iced_k(_results);
  39. };
  40. _continue = function() {
  41. return iced.trampoline(function() {
  42. if (_positive) {
  43. p += 1;
  44. } else {
  45. p -= 1;
  46. }
  47. return _while(__iced_k);
  48. });
  49. };
  50. _next = function(__iced_next_arg) {
  51. _results.push(__iced_next_arg);
  52. return _continue();
  53. };
  54. if (!!((_positive === true && p > r[1]) || (_positive === false && p < r[1]))) {
  55. return _break();
  56. } else {
  57. (function(__iced_k) {
  58. __iced_deferrals = new iced.Deferrals(__iced_k, {
  59. parent: ___iced_passed_deferral,
  60. filename: "/Users/max/src/keybase/node-client/src/hkp_loopback.iced",
  61. funcname: "HKPLoopback.listen"
  62. });
  63. _this._srv.listen(p, _this._hostname, __iced_deferrals.defer({
  64. assign_fn: (function() {
  65. return function() {
  66. return err = arguments[0];
  67. };
  68. })(),
  69. lineno: 23
  70. }));
  71. __iced_deferrals._fulfill();
  72. })(function() {
  73. (function(__iced_k) {
  74. if (err == null) {
  75. log.debug("| found " + p);
  76. _this._port = p;
  77. (function(__iced_k) {
  78. _break()
  79. })(__iced_k);
  80. } else {
  81. return __iced_k();
  82. }
  83. })(_next);
  84. });
  85. }
  86. };
  87. _while(__iced_k);
  88. });
  89. })(this)((function(_this) {
  90. return function() {
  91. err = _this._port != null ? null : new E.LoopbackError("no available ports found");
  92. log.debug("- HKPLoopback::init -> " + err);
  93. return cb(err);
  94. };
  95. })(this));
  96. };
  97. HKPLoopback.prototype.serve = function(req, res) {
  98. var body, err, gres, inurl, opts, u, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  99. __iced_k = __iced_k_noop;
  100. ___iced_passed_deferral = iced.findDeferral(arguments);
  101. u = req.url;
  102. log.debug("+ Incoming HKP request on loopback :" + this._port + ": " + u);
  103. inurl = urlmod.parse(u);
  104. opts = {
  105. json: false,
  106. jar: false,
  107. pathname: inurl.pathname,
  108. search: inurl.search
  109. };
  110. (function(_this) {
  111. return (function(__iced_k) {
  112. __iced_deferrals = new iced.Deferrals(__iced_k, {
  113. parent: ___iced_passed_deferral,
  114. filename: "/Users/max/src/keybase/node-client/src/hkp_loopback.iced",
  115. funcname: "HKPLoopback.serve"
  116. });
  117. reqmod.get(opts, __iced_deferrals.defer({
  118. assign_fn: (function() {
  119. return function() {
  120. err = arguments[0];
  121. body = arguments[1];
  122. return gres = arguments[2];
  123. };
  124. })(),
  125. lineno: 41
  126. }));
  127. __iced_deferrals._fulfill();
  128. });
  129. })(this)((function(_this) {
  130. return function() {
  131. gres.headers['connection'] = 'close';
  132. res.writeHead(gres.statusCode, gres.headers);
  133. res.write(gres.body);
  134. log.debug("- Replied to loopback request w/ status=" + gres.statusCode);
  135. return res.end();
  136. };
  137. })(this));
  138. };
  139. HKPLoopback.prototype.close = function(cb) {
  140. var ___iced_passed_deferral, __iced_deferrals, __iced_k;
  141. __iced_k = __iced_k_noop;
  142. ___iced_passed_deferral = iced.findDeferral(arguments);
  143. (function(_this) {
  144. return (function(__iced_k) {
  145. if (_this._srv != null) {
  146. (function(__iced_k) {
  147. __iced_deferrals = new iced.Deferrals(__iced_k, {
  148. parent: ___iced_passed_deferral,
  149. filename: "/Users/max/src/keybase/node-client/src/hkp_loopback.iced",
  150. funcname: "HKPLoopback.close"
  151. });
  152. _this._srv.close(__iced_deferrals.defer({
  153. lineno: 50
  154. }));
  155. __iced_deferrals._fulfill();
  156. })(__iced_k);
  157. } else {
  158. return __iced_k();
  159. }
  160. });
  161. })(this)((function(_this) {
  162. return function() {
  163. return cb(null);
  164. };
  165. })(this));
  166. };
  167. return HKPLoopback;
  168. })();
  169. }).call(this);