diff --git a/lib/pixel-ping.js b/lib/pixel-ping.js index 1f84a49..377dbfd 100644 --- a/lib/pixel-ping.js +++ b/lib/pixel-ping.js @@ -1,7 +1,7 @@ -// Generated by CoffeeScript 2.3.2 +// Generated by CoffeeScript 2.1.0 (function() { // Require Node.js core modules. - var VERSION, config, configPath, emptyHeaders, endParams, endReqOpts, flush, fs, http, https, httpsPattern, log, merge, pixel, pixelHeaders, protocol, protocolOptions, querystring, record, reset, serialize, server, store, url; + var VERSION, config, configPath, emptyHeaders, endParams, endReqOpts, flush, fs, handleRequest, http, https, httpsPattern, log, merge, pixel, pixelHeaders, protocolOptions, querystring, record, reset, serialize, server, store, url; fs = require('fs'); @@ -171,40 +171,42 @@ return console.error(`Uncaught Exception: ${err}`); }); + // When a request comes in, ensure that it's looking + // for `pixel.gif`. If it is, serve the pixel and record a hit. + handleRequest = function(req, res) { + var key, params, ref; + params = url.parse(req.url, true); + if (params.pathname === '/pixel.gif') { + res.writeHead(200, pixelHeaders); + res.end(pixel); + if (key = (ref = params.query) != null ? ref.key : void 0) { + record(key, 1); + } + } else { + res.writeHead(404, emptyHeaders); + res.end(''); + } + return null; + }; + // Determines the right protocol (HTTP/HTTPS) to be used on the nodejs server if (config.sslkey && config.sslcert && config.sslca) { - protocol = https; protocolOptions = { key: fs.readFileSync(config.sslkey), cert: fs.readFileSync(config.sslcert), ca: fs.readFileSync(config.sslca) }; + server = https.createServer(protocolOptions, handleRequest); } else if (config.sslkey && config.sslcert) { - protocol = https; protocolOptions = { key: fs.readFileSync(config.sslkey), cert: fs.readFileSync(config.sslcert) }; + server = https.createServer(protocolOptions, handleRequest); } else { - protocol = http; + server = http.createServer(handleRequest); } - server = protocol.createServer(protocolOptions, function(req, res) { - var key, params, ref; - params = url.parse(req.url, true); - if (params.pathname === '/pixel.gif') { - res.writeHead(200, pixelHeaders); - res.end(pixel); - if (key = (ref = params.query) != null ? ref.key : void 0) { - record(key, 1); - } - } else { - res.writeHead(404, emptyHeaders); - res.end(''); - } - return null; - }); - //### Startup // Start the server listening for pixel hits, and begin the periodic data flush. diff --git a/src/pixel-ping.coffee b/src/pixel-ping.coffee index 5e729e2..0dc18a0 100644 --- a/src/pixel-ping.coffee +++ b/src/pixel-ping.coffee @@ -123,36 +123,37 @@ process.on 'SIGUSR2', -> process.on 'uncaughtException', (err) -> console.error "Uncaught Exception: #{err}" +# When a request comes in, ensure that it's looking +# for `pixel.gif`. If it is, serve the pixel and record a hit. +handleRequest = (req, res) -> + params = url.parse req.url, true + if params.pathname is '/pixel.gif' + res.writeHead 200, pixelHeaders + res.end pixel + if key = params.query?.key + record key, 1 + else + res.writeHead 404, emptyHeaders + res.end '' + null + + # Determines the right protocol (HTTP/HTTPS) to be used on the nodejs server if config.sslkey && config.sslcert && config.sslca - protocol = https; protocolOptions = { key : fs.readFileSync(config.sslkey), cert : fs.readFileSync(config.sslcert), ca : fs.readFileSync(config.sslca), }; + server = https.createServer(protocolOptions, handleRequest) else if config.sslkey && config.sslcert - protocol = https; protocolOptions = { key : fs.readFileSync(config.sslkey), cert : fs.readFileSync(config.sslcert), }; + server = https.createServer(protocolOptions, handleRequest) else - protocol = http; - -# Create a `Server` object. When a request comes in, ensure that it's looking -# for `pixel.gif`. If it is, serve the pixel and record a hit. -server = protocol.createServer protocolOptions, (req, res) -> - params = url.parse req.url, true - if params.pathname is '/pixel.gif' - res.writeHead 200, pixelHeaders - res.end pixel - if key = params.query?.key - record key, 1 - else - res.writeHead 404, emptyHeaders - res.end '' - null + server = http.createServer(handleRequest) #### Startup diff --git a/test/test-ping.coffee b/test/test-ping.coffee index 7696e03..56149dc 100644 --- a/test/test-ping.coffee +++ b/test/test-ping.coffee @@ -28,7 +28,7 @@ exit = (code) -> server.listen 6999, 'localhost' -ping = spawn 'node', ['bin/pixel-ping', 'test/config.json'] +ping = spawn 'node', ['bin/pixel-ping', 'test/config.json'], {stdio: 'inherit'} delay = (time, func) -> setTimeout func, time