You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
12 KiB

4 years ago
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var GenericReceiver, MAP, ResponseReceiver, Session, SockJSConnection, Transport, closeFrame, register, stream, utils, uuidv4,
  4. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  5. hasProp = {}.hasOwnProperty;
  6. stream = require('stream');
  7. uuidv4 = require('uuid/v4');
  8. utils = require('./utils');
  9. Transport = (function() {
  10. function Transport() {}
  11. return Transport;
  12. })();
  13. Transport.CONNECTING = 0;
  14. Transport.OPEN = 1;
  15. Transport.CLOSING = 2;
  16. Transport.CLOSED = 3;
  17. closeFrame = function(status, reason) {
  18. return 'c' + JSON.stringify([status, reason]);
  19. };
  20. SockJSConnection = (function(superClass) {
  21. extend(SockJSConnection, superClass);
  22. function SockJSConnection(_session) {
  23. this._session = _session;
  24. this.id = uuidv4();
  25. this.headers = {};
  26. this.prefix = this._session.prefix;
  27. }
  28. SockJSConnection.prototype.toString = function() {
  29. return '<SockJSConnection ' + this.id + '>';
  30. };
  31. SockJSConnection.prototype.write = function(string) {
  32. return this._session.send('' + string);
  33. };
  34. SockJSConnection.prototype.end = function(string) {
  35. if (string) {
  36. this.write(string);
  37. }
  38. this.close();
  39. return null;
  40. };
  41. SockJSConnection.prototype.close = function(code, reason) {
  42. return this._session.close(code, reason);
  43. };
  44. SockJSConnection.prototype.destroy = function() {
  45. this.end();
  46. return this.removeAllListeners();
  47. };
  48. SockJSConnection.prototype.destroySoon = function() {
  49. return this.destroy();
  50. };
  51. return SockJSConnection;
  52. })(stream.Stream);
  53. SockJSConnection.prototype.__defineGetter__('readable', function() {
  54. return this._session.readyState === Transport.OPEN;
  55. });
  56. SockJSConnection.prototype.__defineGetter__('writable', function() {
  57. return this._session.readyState === Transport.OPEN;
  58. });
  59. SockJSConnection.prototype.__defineGetter__('readyState', function() {
  60. return this._session.readyState;
  61. });
  62. MAP = {};
  63. Session = (function() {
  64. function Session(session_id1, server) {
  65. this.session_id = session_id1;
  66. this.heartbeat_delay = server.options.heartbeat_delay;
  67. this.disconnect_delay = server.options.disconnect_delay;
  68. this.prefix = server.options.prefix;
  69. this.send_buffer = [];
  70. this.is_closing = false;
  71. this.readyState = Transport.CONNECTING;
  72. if (this.session_id) {
  73. MAP[this.session_id] = this;
  74. }
  75. this.timeout_cb = (function(_this) {
  76. return function() {
  77. return _this.didTimeout();
  78. };
  79. })(this);
  80. this.to_tref = setTimeout(this.timeout_cb, this.disconnect_delay);
  81. this.connection = new SockJSConnection(this);
  82. this.emit_open = (function(_this) {
  83. return function() {
  84. _this.emit_open = null;
  85. return server.emit('connection', _this.connection);
  86. };
  87. })(this);
  88. }
  89. Session.prototype.register = function(req, recv) {
  90. if (this.recv) {
  91. recv.doSendFrame(closeFrame(2010, "Another connection still open"));
  92. recv.didClose();
  93. return;
  94. }
  95. if (this.to_tref) {
  96. clearTimeout(this.to_tref);
  97. this.to_tref = null;
  98. }
  99. if (this.readyState === Transport.CLOSING) {
  100. this.flushToRecv(recv);
  101. recv.doSendFrame(this.close_frame);
  102. recv.didClose();
  103. this.to_tref = setTimeout(this.timeout_cb, this.disconnect_delay);
  104. return;
  105. }
  106. this.recv = recv;
  107. this.recv.session = this;
  108. this.decorateConnection(req);
  109. if (this.readyState === Transport.CONNECTING) {
  110. this.recv.doSendFrame('o');
  111. this.readyState = Transport.OPEN;
  112. process.nextTick(this.emit_open);
  113. }
  114. if (!this.recv) {
  115. return;
  116. }
  117. this.tryFlush();
  118. };
  119. Session.prototype.decorateConnection = function(req) {
  120. var address, headers, i, key, len, ref, remoteAddress, remotePort, socket, x;
  121. if (!(socket = this.recv.connection)) {
  122. socket = this.recv.response.connection;
  123. }
  124. try {
  125. remoteAddress = socket.remoteAddress;
  126. remotePort = socket.remotePort;
  127. address = socket.address();
  128. } catch (error) {
  129. x = error;
  130. }
  131. if (remoteAddress) {
  132. this.connection.remoteAddress = remoteAddress;
  133. this.connection.remotePort = remotePort;
  134. this.connection.address = address;
  135. }
  136. this.connection.url = req.url;
  137. this.connection.pathname = req.pathname;
  138. this.connection.protocol = this.recv.protocol;
  139. headers = {};
  140. ref = ['referer', 'x-client-ip', 'x-forwarded-for', 'x-forwarded-host', 'x-forwarded-port', 'x-cluster-client-ip', 'via', 'x-real-ip', 'x-forwarded-proto', 'x-ssl', 'dnt', 'host', 'user-agent', 'accept-language'];
  141. for (i = 0, len = ref.length; i < len; i++) {
  142. key = ref[i];
  143. if (req.headers[key]) {
  144. headers[key] = req.headers[key];
  145. }
  146. }
  147. if (headers) {
  148. return this.connection.headers = headers;
  149. }
  150. };
  151. Session.prototype.unregister = function() {
  152. var delay;
  153. delay = this.recv.delay_disconnect;
  154. this.recv.session = null;
  155. this.recv = null;
  156. if (this.to_tref) {
  157. clearTimeout(this.to_tref);
  158. }
  159. if (delay) {
  160. return this.to_tref = setTimeout(this.timeout_cb, this.disconnect_delay);
  161. } else {
  162. return this.timeout_cb();
  163. }
  164. };
  165. Session.prototype.flushToRecv = function(recv) {
  166. var ref, sb;
  167. if (this.send_buffer.length > 0) {
  168. ref = [this.send_buffer, []], sb = ref[0], this.send_buffer = ref[1];
  169. recv.doSendBulk(sb);
  170. return true;
  171. }
  172. return false;
  173. };
  174. Session.prototype.tryFlush = function() {
  175. var x;
  176. if (!this.flushToRecv(this.recv) || !this.to_tref) {
  177. if (this.to_tref) {
  178. clearTimeout(this.to_tref);
  179. }
  180. x = (function(_this) {
  181. return function() {
  182. if (_this.recv) {
  183. _this.to_tref = setTimeout(x, _this.heartbeat_delay);
  184. return _this.recv.heartbeat();
  185. }
  186. };
  187. })(this);
  188. this.to_tref = setTimeout(x, this.heartbeat_delay);
  189. }
  190. };
  191. Session.prototype.didTimeout = function() {
  192. if (this.to_tref) {
  193. clearTimeout(this.to_tref);
  194. this.to_tref = null;
  195. }
  196. if (this.readyState !== Transport.CONNECTING && this.readyState !== Transport.OPEN && this.readyState !== Transport.CLOSING) {
  197. throw Error('INVALID_STATE_ERR');
  198. }
  199. if (this.recv) {
  200. throw Error('RECV_STILL_THERE');
  201. }
  202. this.readyState = Transport.CLOSED;
  203. this.connection.emit('end');
  204. this.connection.emit('close');
  205. this.connection = null;
  206. if (this.session_id) {
  207. delete MAP[this.session_id];
  208. return this.session_id = null;
  209. }
  210. };
  211. Session.prototype.didMessage = function(payload) {
  212. if (this.readyState === Transport.OPEN) {
  213. this.connection.emit('data', payload);
  214. }
  215. };
  216. Session.prototype.send = function(payload) {
  217. if (this.readyState !== Transport.OPEN) {
  218. return false;
  219. }
  220. this.send_buffer.push('' + payload);
  221. if (this.recv) {
  222. this.tryFlush();
  223. }
  224. return true;
  225. };
  226. Session.prototype.close = function(status, reason) {
  227. if (status == null) {
  228. status = 1000;
  229. }
  230. if (reason == null) {
  231. reason = "Normal closure";
  232. }
  233. if (this.readyState !== Transport.OPEN) {
  234. return false;
  235. }
  236. this.readyState = Transport.CLOSING;
  237. this.close_frame = closeFrame(status, reason);
  238. if (this.recv) {
  239. this.recv.doSendFrame(this.close_frame);
  240. if (this.recv) {
  241. this.recv.didClose();
  242. }
  243. if (this.recv) {
  244. this.unregister();
  245. }
  246. }
  247. return true;
  248. };
  249. return Session;
  250. })();
  251. Session.bySessionId = function(session_id) {
  252. if (!session_id) {
  253. return null;
  254. }
  255. return MAP[session_id] || null;
  256. };
  257. register = function(req, server, session_id, receiver) {
  258. var session;
  259. session = Session.bySessionId(session_id);
  260. if (!session) {
  261. session = new Session(session_id, server);
  262. }
  263. session.register(req, receiver);
  264. return session;
  265. };
  266. exports.register = function(req, server, receiver) {
  267. return register(req, server, req.session, receiver);
  268. };
  269. exports.registerNoSession = function(req, server, receiver) {
  270. return register(req, server, void 0, receiver);
  271. };
  272. GenericReceiver = (function() {
  273. function GenericReceiver(thingy) {
  274. this.thingy = thingy;
  275. this.setUp(this.thingy);
  276. }
  277. GenericReceiver.prototype.setUp = function() {
  278. this.thingy_end_cb = (function(_this) {
  279. return function() {
  280. return _this.didAbort();
  281. };
  282. })(this);
  283. this.thingy.addListener('close', this.thingy_end_cb);
  284. return this.thingy.addListener('end', this.thingy_end_cb);
  285. };
  286. GenericReceiver.prototype.tearDown = function() {
  287. this.thingy.removeListener('close', this.thingy_end_cb);
  288. this.thingy.removeListener('end', this.thingy_end_cb);
  289. return this.thingy_end_cb = null;
  290. };
  291. GenericReceiver.prototype.didAbort = function() {
  292. this.delay_disconnect = false;
  293. return this.didClose();
  294. };
  295. GenericReceiver.prototype.didClose = function() {
  296. if (this.thingy) {
  297. this.tearDown(this.thingy);
  298. this.thingy = null;
  299. }
  300. if (this.session) {
  301. return this.session.unregister();
  302. }
  303. };
  304. GenericReceiver.prototype.doSendBulk = function(messages) {
  305. var m, q_msgs;
  306. q_msgs = (function() {
  307. var i, len, results;
  308. results = [];
  309. for (i = 0, len = messages.length; i < len; i++) {
  310. m = messages[i];
  311. results.push(utils.quote(m));
  312. }
  313. return results;
  314. })();
  315. return this.doSendFrame('a' + '[' + q_msgs.join(',') + ']');
  316. };
  317. GenericReceiver.prototype.heartbeat = function() {
  318. return this.doSendFrame('h');
  319. };
  320. return GenericReceiver;
  321. })();
  322. ResponseReceiver = (function(superClass) {
  323. extend(ResponseReceiver, superClass);
  324. ResponseReceiver.prototype.max_response_size = void 0;
  325. ResponseReceiver.prototype.delay_disconnect = true;
  326. function ResponseReceiver(request, response, options) {
  327. var x;
  328. this.request = request;
  329. this.response = response;
  330. this.options = options;
  331. this.curr_response_size = 0;
  332. try {
  333. this.request.connection.setKeepAlive(true, 5000);
  334. } catch (error) {
  335. x = error;
  336. }
  337. ResponseReceiver.__super__.constructor.call(this, this.request.connection);
  338. if (this.max_response_size === void 0) {
  339. this.max_response_size = this.options.response_limit;
  340. }
  341. }
  342. ResponseReceiver.prototype.doSendFrame = function(payload) {
  343. var r, x;
  344. this.curr_response_size += payload.length;
  345. r = false;
  346. try {
  347. this.response.write(payload);
  348. r = true;
  349. } catch (error) {
  350. x = error;
  351. }
  352. if (this.max_response_size && this.curr_response_size >= this.max_response_size) {
  353. this.didClose();
  354. }
  355. return r;
  356. };
  357. ResponseReceiver.prototype.didClose = function() {
  358. var x;
  359. ResponseReceiver.__super__.didClose.apply(this, arguments);
  360. try {
  361. this.response.end();
  362. } catch (error) {
  363. x = error;
  364. }
  365. return this.response = null;
  366. };
  367. return ResponseReceiver;
  368. })(GenericReceiver);
  369. exports.GenericReceiver = GenericReceiver;
  370. exports.Transport = Transport;
  371. exports.Session = Session;
  372. exports.ResponseReceiver = ResponseReceiver;
  373. exports.SockJSConnection = SockJSConnection;
  374. }).call(this);