Skip to content

Commit

Permalink
Pixel.gif server: unbreak for node.js <= 8.x
Browse files Browse the repository at this point in the history
Pull #9 added support for serving via https. Great!

It also broke support for node 8.x and below (both 8 and 6 are LTS).

In v10, http.createServer() got an optional first argument of
an options object. Previous versions don't support that optional first
arg and thus ignore the second arg. This leaves them with no function
to handle requests and thus they stall all requests forever.
  • Loading branch information
danielbeardsley committed Mar 26, 2019
1 parent c5a32f5 commit 09bddcb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
44 changes: 23 additions & 21 deletions lib/pixel-ping.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions src/pixel-ping.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test-ping.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 09bddcb

Please sign in to comment.