sockjs.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var App, Listener, Server, chunking_test, events, fs, generate_dispatcher, iframe, sockjsVersion, trans_eventsource, trans_htmlfile, trans_jsonp, trans_websocket, trans_xhr, utils, webjs,
  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. bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  7. events = require('events');
  8. fs = require('fs');
  9. webjs = require('./webjs');
  10. utils = require('./utils');
  11. trans_websocket = require('./trans-websocket');
  12. trans_jsonp = require('./trans-jsonp');
  13. trans_xhr = require('./trans-xhr');
  14. iframe = require('./iframe');
  15. trans_eventsource = require('./trans-eventsource');
  16. trans_htmlfile = require('./trans-htmlfile');
  17. chunking_test = require('./chunking-test');
  18. sockjsVersion = function() {
  19. var pkg, x;
  20. try {
  21. pkg = fs.readFileSync(__dirname + '/../package.json', 'utf-8');
  22. } catch (error) {
  23. x = error;
  24. }
  25. if (pkg) {
  26. return JSON.parse(pkg).version;
  27. } else {
  28. return null;
  29. }
  30. };
  31. App = (function(superClass) {
  32. extend(App, superClass);
  33. function App() {
  34. return App.__super__.constructor.apply(this, arguments);
  35. }
  36. App.prototype.welcome_screen = function(req, res) {
  37. res.setHeader('content-type', 'text/plain; charset=UTF-8');
  38. res.writeHead(200);
  39. res.end("Welcome to SockJS!\n");
  40. return true;
  41. };
  42. App.prototype.handle_404 = function(req, res) {
  43. res.setHeader('content-type', 'text/plain; charset=UTF-8');
  44. res.writeHead(404);
  45. res.end('404 Error: Page not found\n');
  46. return true;
  47. };
  48. App.prototype.disabled_transport = function(req, res, data) {
  49. return this.handle_404(req, res, data);
  50. };
  51. App.prototype.h_sid = function(req, res, data) {
  52. var jsid;
  53. req.cookies = utils.parseCookie(req.headers.cookie);
  54. if (typeof this.options.jsessionid === 'function') {
  55. this.options.jsessionid(req, res);
  56. } else if (this.options.jsessionid && res.setHeader) {
  57. jsid = req.cookies['JSESSIONID'] || 'dummy';
  58. res.setHeader('Set-Cookie', 'JSESSIONID=' + jsid + '; path=/');
  59. }
  60. return data;
  61. };
  62. App.prototype.log = function(severity, line) {
  63. return this.options.log(severity, line);
  64. };
  65. return App;
  66. })(webjs.GenericApp);
  67. utils.objectExtend(App.prototype, iframe.app);
  68. utils.objectExtend(App.prototype, chunking_test.app);
  69. utils.objectExtend(App.prototype, trans_websocket.app);
  70. utils.objectExtend(App.prototype, trans_jsonp.app);
  71. utils.objectExtend(App.prototype, trans_xhr.app);
  72. utils.objectExtend(App.prototype, trans_eventsource.app);
  73. utils.objectExtend(App.prototype, trans_htmlfile.app);
  74. generate_dispatcher = function(options) {
  75. var opts_filters, p, prefix_dispatcher, t, transport_dispatcher;
  76. p = (function(_this) {
  77. return function(s) {
  78. return new RegExp('^' + options.prefix + s + '[/]?$');
  79. };
  80. })(this);
  81. t = (function(_this) {
  82. return function(s) {
  83. return [p('/([^/.]+)/([^/.]+)' + s), 'server', 'session'];
  84. };
  85. })(this);
  86. opts_filters = function(options_filter) {
  87. if (options_filter == null) {
  88. options_filter = 'xhr_options';
  89. }
  90. return ['h_sid', 'xhr_cors', 'cache_for', options_filter, 'expose'];
  91. };
  92. prefix_dispatcher = [['GET', p(''), ['welcome_screen']], ['GET', p('/iframe[0-9-.a-z_]*.html'), ['iframe', 'cache_for', 'expose']], ['OPTIONS', p('/info'), opts_filters('info_options')], ['GET', p('/info'), ['xhr_cors', 'h_no_cache', 'info', 'expose']], ['OPTIONS', p('/chunking_test'), opts_filters()], ['POST', p('/chunking_test'), ['xhr_cors', 'expect_xhr', 'chunking_test']]];
  93. transport_dispatcher = [['GET', t('/jsonp'), ['h_sid', 'h_no_cache', 'jsonp']], ['POST', t('/jsonp_send'), ['h_sid', 'h_no_cache', 'expect_form', 'jsonp_send']], ['POST', t('/xhr'), ['h_sid', 'h_no_cache', 'xhr_cors', 'xhr_poll']], ['OPTIONS', t('/xhr'), opts_filters()], ['POST', t('/xhr_send'), ['h_sid', 'h_no_cache', 'xhr_cors', 'expect_xhr', 'xhr_send']], ['OPTIONS', t('/xhr_send'), opts_filters()], ['POST', t('/xhr_streaming'), ['h_sid', 'h_no_cache', 'xhr_cors', 'xhr_streaming']], ['OPTIONS', t('/xhr_streaming'), opts_filters()], ['GET', t('/eventsource'), ['h_sid', 'h_no_cache', 'eventsource']], ['GET', t('/htmlfile'), ['h_sid', 'h_no_cache', 'htmlfile']]];
  94. if (options.websocket) {
  95. prefix_dispatcher.push(['GET', p('/websocket'), ['raw_websocket']]);
  96. transport_dispatcher.push(['GET', t('/websocket'), ['sockjs_websocket']]);
  97. } else {
  98. prefix_dispatcher.push(['GET', p('/websocket'), ['cache_for', 'disabled_transport']]);
  99. transport_dispatcher.push(['GET', t('/websocket'), ['cache_for', 'disabled_transport']]);
  100. }
  101. return prefix_dispatcher.concat(transport_dispatcher);
  102. };
  103. Listener = (function() {
  104. function Listener(options1, emit) {
  105. this.options = options1;
  106. this.handler = bind(this.handler, this);
  107. this.app = new App();
  108. this.app.options = this.options;
  109. this.app.emit = emit;
  110. this.app.log('debug', 'SockJS v' + sockjsVersion() + ' ' + 'bound to ' + JSON.stringify(this.options.prefix));
  111. this.dispatcher = generate_dispatcher(this.options);
  112. this.webjs_handler = webjs.generateHandler(this.app, this.dispatcher);
  113. this.path_regexp = new RegExp('^' + this.options.prefix + '([/].+|[/]?)$');
  114. }
  115. Listener.prototype.handler = function(req, res, extra) {
  116. if (!req.url.match(this.path_regexp)) {
  117. return false;
  118. }
  119. this.webjs_handler(req, res, extra);
  120. return true;
  121. };
  122. Listener.prototype.getHandler = function() {
  123. return (function(_this) {
  124. return function(a, b, c) {
  125. return _this.handler(a, b, c);
  126. };
  127. })(this);
  128. };
  129. return Listener;
  130. })();
  131. Server = (function(superClass) {
  132. extend(Server, superClass);
  133. function Server(user_options) {
  134. this.options = {
  135. prefix: '',
  136. response_limit: 128 * 1024,
  137. websocket: true,
  138. faye_server_options: null,
  139. jsessionid: false,
  140. heartbeat_delay: 25000,
  141. disconnect_delay: 5000,
  142. log: function(severity, line) {
  143. return console.log(line);
  144. },
  145. sockjs_url: 'https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js'
  146. };
  147. if (user_options) {
  148. utils.objectExtend(this.options, user_options);
  149. }
  150. }
  151. Server.prototype.listener = function(handler_options) {
  152. var options;
  153. options = utils.objectExtend({}, this.options);
  154. if (handler_options) {
  155. utils.objectExtend(options, handler_options);
  156. }
  157. return new Listener(options, (function(_this) {
  158. return function() {
  159. return _this.emit.apply(_this, arguments);
  160. };
  161. })(this));
  162. };
  163. Server.prototype.installHandlers = function(http_server, handler_options) {
  164. var handler;
  165. handler = this.listener(handler_options).getHandler();
  166. utils.overshadowListeners(http_server, 'request', handler);
  167. utils.overshadowListeners(http_server, 'upgrade', handler);
  168. return true;
  169. };
  170. Server.prototype.middleware = function(handler_options) {
  171. var handler;
  172. handler = this.listener(handler_options).getHandler();
  173. handler.upgrade = handler;
  174. return handler;
  175. };
  176. return Server;
  177. })(events.EventEmitter);
  178. exports.createServer = function(options) {
  179. return new Server(options);
  180. };
  181. exports.listen = function(http_server, options) {
  182. var srv;
  183. srv = exports.createServer(options);
  184. if (http_server) {
  185. srv.installHandlers(http_server);
  186. }
  187. return srv;
  188. };
  189. }).call(this);