Skip to content

Instantly share code, notes, and snippets.

@Lull3rSkat3r
Last active March 8, 2016 16:50
Show Gist options
  • Save Lull3rSkat3r/4a33589e6121525ecb63 to your computer and use it in GitHub Desktop.
Save Lull3rSkat3r/4a33589e6121525ecb63 to your computer and use it in GitHub Desktop.

Solution *

This is the output we expect and should be the same regardless of any solution. Note that WSRewriter and ProxySocket receive different values and the client receives only one message.

Dashboard Server

> node streams.js
Solution *
WSRewriter:  <Buffer 61 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66 72 6f 6e 74 65 6e 64 0a>
ProxySocket:  <Buffer 41 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 57 53 52 65 77 72 69 74 65 72 0a>

Client side

'a message from the frontend' can be thought of as a jupyter message generated client side.

> nc localhost 3000
Solution *
a message from the frontend
A message from the ProxySocket
var Transform = require('stream').Transform;
/// Server:
var server = require('net').createServer();
function onConnection(socket) {
// Simulates our rewriter transform
var wsRewriter = new Transform();
wsRewriter._transform = function(chunk, encoding, done) {
console.log('WSRewriter: ', chunk);
done(null, 'A message from WSRewriter\n');
};
// Simulates the proxy result from
var proxySocket = new Transform();
proxySocket._transform = function(chunk, encoding, done) {
console.log('ProxySocket: ', chunk);
done(null, 'A message from the ProxySocket\n');
};
socket.setEncoding('utf8');
// // The Situation
// socket.write('The Situation\n');
// console.log('The Situation');
// // api.js
// socket.pipe(wsRewriter).pipe(socket);
// // nodejitsue/node-http-proxy/lib/http-proxy/passes/ws-incoming.js
// proxySocket.pipe(socket).pipe(proxySocket);
// Solution 1: Ideally we should be able to pass the transformer to nodejitsu
socket.write('Solution 1\n');
console.log('Solution 1');
// nodejitsue/node-http-proxy/lib/http-proxy/passes/ws-incoming.js
socket.pipe(wsRewriter).pipe(proxySocket).pipe(socket);
// Solution 2: Not ideal but would still work. Requires we can pass in a non-socket stream to proxy.ws
// Don't know exactly where all these pieces would go, but would most likely be partially in api.js
// socket.write('Solution 2\n');
// console.log('Solution 2');
// // maybe api.js?
// socket.pipe(wsRewriter);
// wsRewriter.pipe(proxySocket);
// proxySocket.pipe(socket);
}
server.on('connection', onConnection);
server.listen(3000);

The Situation

Two things to note here:

  1. The WSRewriter and ProxySocket instances get teh same message. This means there is no pipe established between them
  2. The client side receives a message from WSRewriter which is incorrect. This message should have gone to the ProxySocket instance. Furthermore, this causes the socket to close because this message is masked , and a server cannot send a masked message to a client.

Dashboard Server

> node streams.js
The Situation
WSRewriter:  <Buffer 61 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66 72 6f 6e 74 65 6e 64 0a>
ProxySocket:  <Buffer 61 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66 72 6f 6e 74 65 6e 64 0a>

Client side

'a message from the frontend' can be thought of as a jupyter message generated client side.

> nc localhost 3000
The Situation
a message from the frontend
A message from WSRewriter
A message from the ProxySocket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment