diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-11-04 20:33:14 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-11-04 20:33:14 -0500 |
| commit | 83517eb8d489cfceda8e6b7ab1b104a8f0b1f3a8 (patch) | |
| tree | be6fce89034de08d745cfd0940ea7ea747a1b2a7 /proxy | |
| parent | 8d7a5da72199e25db9056b9ee585094ac3274f49 (diff) | |
iscroll workin, api fetching, hud populating
Diffstat (limited to 'proxy')
| -rw-r--r-- | proxy/index.js | 99 | ||||
| -rw-r--r-- | proxy/package.json | 2 |
2 files changed, 61 insertions, 40 deletions
diff --git a/proxy/index.js b/proxy/index.js index d2d4e738..b530c7a3 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -5,9 +5,10 @@ var fs = require('fs') var url = require('url') var PORT = 9090 +var cache = {} var server = http.createServer().listen(PORT, function(){ - console.log("Proxy listening on: http://lvh.me:%s", PORT) + console.log('Proxy listening on: http://lvh.me:%s', PORT) }) server.on('request', function (req, res){ @@ -18,6 +19,12 @@ server.on('request', function (req, res){ } console.log(req.method, req.url) + + if (req.method == 'GET' && req.url in cache) { + res.writeHeader(200, { 'Content-type': 'application/json' }) + res.end(cache[req.url]) + return + } // console.log(req.headers) @@ -25,21 +32,28 @@ server.on('request', function (req, res){ options.headers = get_headers(req.headers) options.method = req.method options.port = 443 - options.hostname = "secure.api.yoox.biz" + options.hostname = 'secure.api.yoox.biz' options.path = req.url req.pause() var connector = https.request(options, function(server_res) { - console.log(">> GOT", server_res.statusCode) - + console.log('>> GOT', server_res.statusCode) + server_res.pause() res.writeHeader(server_res.statusCode, server_res.headers) - // server_res.on("data", function(s){ console.log("<<", s.toString()) }) server_res.pipe(res) + + if (req.method == 'GET') { + cache[req.url] = '' + server_res.on('data', function(s){ + cache[req.url] += s.toString() + }) + } + server_res.resume() }) - // req.on("data", function(s){ console.log(">>", s.toString()) }) - req.on("error", function(s){ console.log("/!\\ ERROR /!\\"); console.log(s) }) + // req.on('data', function(s){ console.log('>>', s.toString()) }) + req.on('error', function(s){ console.log('/!\\ ERROR /!\\'); console.log(s) }) req.pipe(connector) req.resume() @@ -47,82 +61,89 @@ server.on('request', function (req, res){ /* headers: { - "x-yoox-appname": auth.appname, - "x-yoox-account-token": auth.access_token, - "x-yoox-device": auth.device, - "x-yoox-api-key": auth.apikey, - "x-yoox-cart-token": cart.token, + 'x-yoox-appname': auth.appname, + 'x-yoox-account-token': auth.access_token, + 'x-yoox-device': auth.device, + 'x-yoox-api-key': auth.apikey, + 'x-yoox-cart-token': cart.token, }, */ function get_headers (h){ var hh = {} - "appname account-token device api-key cart-token".split(" ").forEach(function(s){ - var key = "x-yoox-" + s + 'appname account-token device api-key cart-token'.split(' ').forEach(function(s){ + var key = 'x-yoox-' + s if (key in h) hh[key] = h[key] }) - hh['Content-Type'] = "application/json" // h['content-type'] + hh['Content-Type'] = 'application/json' // h['content-type'] if ('content-length' in h) hh['Content-Length'] = h['content-length'] if ('connection' in h) hh['Connection'] = h['connection'] return hh } var mimes = { - "gif": "image/gif", - "png": "image/png", - "jpg": "image/jpeg", - "jpeg": "image/jpeg", - "html": "text/html", - "js": "application/javascript", - "css": "text/css", - "woff": "application/font-woff", - "ttf": "application/font-woff", + 'gif': 'image/gif', + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'html': 'text/html', + 'js': 'application/javascript', + 'css': 'text/css', + 'woff': 'application/font-woff', + 'ttf': 'application/font-woff', } function stream (req, res) { - var url = req.url.toLowerCase().split("?")[0] - var ext_partz = url.split("."), ext = ext_partz[ext_partz.length-1] - var mime = mimes[ext] || "application/octet-stream" + var url = req.url.toLowerCase().split('?')[0] + var ext_partz = url.split('.'), ext = ext_partz[ext_partz.length-1] + var mime = mimes[ext] || 'application/octet-stream' - if (! url || url == "/") { - url = "index.html" - mime = "text/html" + if (! url || url == '/') { + url = 'index.html' + mime = 'text/html' + } + if (url == '/cordova.js') { + console.log(url) + res.writeHead(200, 'OK', { 'Content-type': 'application/javascript' }) + res.end('{}') + return } - var file = path.resolve("../StoneIsland/www/" + url) - + var file = path.resolve('./StoneIsland/www/' + url) fs.stat(file, function(err, stats) { if (! stats) { // console.log(404) res.writeHead(404) - res.end("404") + res.end('404') return } var headers = { - "Content-Length": stats.size, - "Content-Type": mime, + 'Content-Length': stats.size, + 'Content-Type': mime, } - res.writeHead(200, "OK", headers) + res.writeHead(200, 'OK', headers) var stream = fs.createReadStream(file, { start: 0, end: stats.size }) - stream.on("open", function() { + stream.on('open', function() { stream.pipe(res) }) - stream.on("error", function(err) { + stream.on('error', function(err) { console.log(err) stream.destroy() res.end() }) - stream.on("close", function(){ + stream.on('close', function(){ stream.destroy() res.end() }) +/* req.connection.setMaxListeners(50) req.connection.on('close', function(){ stream.destroy() res.end() }) +*/ }) } diff --git a/proxy/package.json b/proxy/package.json index 7938559d..a9f4ba2c 100644 --- a/proxy/package.json +++ b/proxy/package.json @@ -4,5 +4,5 @@ "description": "local proxy for stone island api", "main": "index.js", "author": "okfocus", - "license": "LNT", + "license": "LNT" } |
