Created
April 30, 2015 12:41
-
-
Save gre/7e0f2098520d7bdfd9f9 to your computer and use it in GitHub Desktop.
proxy lichess stream Server Sent Event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "enhance-lichess", | |
"version": "1.0.0", | |
"dependencies": { | |
"event-stream": "^3.3.0", | |
"request": "^2.55.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var request = require('request'); | |
var http = require('http'); | |
var fs = require('fs'); | |
var es = require('event-stream'); | |
var port = 9876; | |
http.createServer(function(req, res) { | |
if (req.url === "/") { | |
var readStream = | |
request('http://lichess.org/network/stream') | |
.pipe(es.split()) | |
.pipe(es.map(function (data, cb) { | |
if (data.length > 0 && data.indexOf("data: ")===0) { | |
data += "|kiki"; | |
cb(null, data + "\n\n"); | |
} | |
else | |
cb(); // drop | |
})); | |
res.writeHead(200, { | |
"Content-Type": "text/event-stream; charset=utf-8" | |
}); | |
readStream.pipe(res); | |
readStream.on('error', function(err) { | |
res.end(err); | |
}); | |
} | |
else { | |
res.writeHead(404); | |
res.end(); | |
} | |
}).listen(port); | |
console.log("Listening on: http://127.0.0.1:"+port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment