<commit_before>// 
// shiny-proxy.js: A shiny proxy server using sockjs
//
// [browser] <-----sockjs-----> [shiny-proxy] <----websockets----> [shiny app]
// 
// Currently supports only one client and one shiny app
//
// Call like:
//
// node shiny-proxy.js
//
// And edit the below SHINY object
//
//
SHINY = {
   forward_addr: '127.0.0.1',
   forward_port: 9000,
   listen_addr: '127.0.0.1',
   listen_port: 8000
};

var util = require('util'),
    http = require('http'),
    httpProxy = require('http-proxy'),
    sockjs = require('sockjs'),
    websocket = require('faye-websocket'),
    wsapi = require('./node_modules/faye-websocket/lib/faye/websocket/api');


var sockjs_server = sockjs.createServer();

sockjs_server.on('connection', function(conn) {
   console.log("creating forward ws connection");
   var ws = new websocket.Client('ws://'+SHINY.forward_addr+':'+SHINY.forward_port+'/'); 
   
   var message_pending = null;

   ws.onopen = function(event){
      console.log("conn: "+conn.url+" ws open");
      if (message_pending){
         ws.send(message_pending);
         message_pending = null;
      }
   }

   ws.onmessage = function(event){
      console.log("conn: "+conn.url+" ws message");
      conn.write(event.data);
   };
   ws.onclose = function(event){
      console.log("conn: "+conn.url+" ws close");
      conn.close();
      ws.close();
   };
   conn.on('data', function(message) {
      console.log('conn: '+conn.url+' data');
      if (ws.readyState == wsapi.OPEN){
         ws.send(message);
      } else if (message_pending == null){
         message_pending = message;
      } else {
         console.log('conn: '+conn.url+' ws sync error!');
         conn.close();
         ws.close();
      }
   });

   conn.on('close', function(message){
      console.log('conn: '+conn.url+' close');
      ws.close();
      conn.close();
   });
});

var proxy = httpProxy.createServer(SHINY.forward_port, SHINY.forward_addr);

sockjs_server.installHandlers(proxy, {prefix:'/sockjs'});

proxy.listen(SHINY.listen_port,SHINY.listen_addr);
<commit_msg>Eliminate dependency on internal faye-websockets module; add queuing to forwarded websocket<commit_after>// 
// shiny-proxy.js: A shiny proxy server using sockjs
//
// [browser] <-----sockjs-----> [shiny-proxy] <----websockets----> [shiny app]
// 
// Currently supports only one client and one shiny app
//
// Call like:
//
// node shiny-proxy.js
//
// And edit the below SHINY object
//
//
SHINY = {
   forward_addr: '127.0.0.1',
   forward_port: 9000,
   listen_addr: '0.0.0.0',
   listen_port: 8000
};

var util = require('util'),
    http = require('http'),
    httpProxy = require('http-proxy'),
    sockjs = require('sockjs'),
    websocket = require('faye-websocket');


var sockjs_server = sockjs.createServer();

sockjs_server.on('connection', function(conn) {
   
   // Forwarding Message Queue
   var fmq = [];

   var ws = new websocket.Client('ws://'+SHINY.forward_addr+':'+SHINY.forward_port+'/'); 

	var ws_is_open = false;

   ws.onopen = function(event){
		ws_is_open = true;
		var i;

      console.log("conn: "+conn.url+" ws open");

      if (fmq.length){
			for (i = 0; i < fmq.length; i++){
         	ws.send(fmq[i]);
			}
			fmq = [];
      }
   }

   ws.onmessage = function(event){
      console.log("conn: "+conn.url+" ws message");
      conn.write(event.data);
   };

   ws.onclose = function(event){
      console.log("conn: "+conn.url+" ws close");
      conn.close();
      ws.close();
   };

   conn.on('data', function(message) {
      console.log('conn: '+conn.url+' data');
      if (ws_is_open){
         ws.send(message);
      } else {
         fmq.push(message);
      }
   });

   conn.on('close', function(message){
      console.log('conn: '+conn.url+' close');
      ws.close();
      conn.close();
   });
});

var proxy = httpProxy.createServer(SHINY.forward_port, SHINY.forward_addr);

sockjs_server.installHandlers(proxy, {prefix:'/sockjs'});

proxy.listen(SHINY.listen_port,SHINY.listen_addr);

{'fraction_non_alphanumeric': 0.1842170774209609, 'fraction_numerical': 0.009708737864077669, 'mean_word_length': 2.808530805687204, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1594884', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1369, 'n_words': 273}