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

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.