Last active
August 29, 2015 13:57
-
-
Save chriddyp/9373939 to your computer and use it in GitHub Desktop.
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
/* | |
* npm install split | |
* npm install through2 | |
* npm install timezone //http://bigeasy.github.io/timezone | |
*/ | |
var http = require('http') | |
, split = require("split") | |
, through2 = require("through2") | |
, tz = require("timezone") | |
, server = http.createServer(sourcehandler); | |
server.listen(8080); | |
function sourcehandler (req, res) { | |
if (req.method !== "POST") { | |
console.log(JSON.stringify({msg: "Bad Request Method: " + req.method, code: 406})); | |
req.destroy(); | |
} | |
else { | |
if (!req.headers["plotly-streamtoken"]) { | |
console.log("BAD HEADERS"); | |
req.destroy(); | |
} | |
} | |
var token = req.headers["plotly-streamtoken"]; | |
console.log(req.headers); | |
if(req.headers['plotly-converttimestamp']){ | |
var startTime = new Date().getTime() | |
, convertTimestamp = true | |
, timezone = req.headers['plotly-converttimestamp'] | |
, timezoneObj; | |
console.log("got timezone: ", timezone); | |
try{ | |
timezoneObj = require("timezone/"+timezone); | |
} catch(e){ | |
timezoneObj = require("timezone/America/Montreal"); | |
} | |
} | |
console.log("GOT STREAM WITH TOKEN:", token); | |
req.once('error', function (e) { | |
console.log(JSON.stringify(({err:e, msg: "Request Stream Error", code: 500}))); | |
}); | |
req.once('close', function (haderror) { | |
console.log("REQUEST CLOSING"); | |
}); | |
req.setEncoding('utf8'); | |
var datastream = through2(dateTransformer); | |
var splitter = split(); | |
req.pipe(splitter).pipe(datastream).pipe(process.stdout); | |
function dateTransformer (jsonStr, enc, callback) { | |
var data = JSON.parse(jsonStr); | |
if(convertTimestamp){ | |
console.log("convertingTimestamp: ", data.x, ' -- ', timezone); | |
data.x = tz(data.x+startTime, timezone, timezoneObj, "%m-%d-%Y %H:%M:%S.%N"); //http://bigeasy.github.io/timezone/#section-70 | |
} | |
this.push(JSON.stringify(data)+"\n"); | |
callback(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment