Skip to content

Commit 09bddcb

Browse files
Pixel.gif server: unbreak for node.js <= 8.x
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.
1 parent c5a32f5 commit 09bddcb

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

lib/pixel-ping.js

+23-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pixel-ping.coffee

+18-17
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,37 @@ process.on 'SIGUSR2', ->
123123
process.on 'uncaughtException', (err) ->
124124
console.error "Uncaught Exception: #{err}"
125125

126+
# When a request comes in, ensure that it's looking
127+
# for `pixel.gif`. If it is, serve the pixel and record a hit.
128+
handleRequest = (req, res) ->
129+
params = url.parse req.url, true
130+
if params.pathname is '/pixel.gif'
131+
res.writeHead 200, pixelHeaders
132+
res.end pixel
133+
if key = params.query?.key
134+
record key, 1
135+
else
136+
res.writeHead 404, emptyHeaders
137+
res.end ''
138+
null
139+
140+
126141
# Determines the right protocol (HTTP/HTTPS) to be used on the nodejs server
127142
if config.sslkey && config.sslcert && config.sslca
128-
protocol = https;
129143
protocolOptions = {
130144
key : fs.readFileSync(config.sslkey),
131145
cert : fs.readFileSync(config.sslcert),
132146
ca : fs.readFileSync(config.sslca),
133147
};
148+
server = https.createServer(protocolOptions, handleRequest)
134149
else if config.sslkey && config.sslcert
135-
protocol = https;
136150
protocolOptions = {
137151
key : fs.readFileSync(config.sslkey),
138152
cert : fs.readFileSync(config.sslcert),
139153
};
154+
server = https.createServer(protocolOptions, handleRequest)
140155
else
141-
protocol = http;
142-
143-
# Create a `Server` object. When a request comes in, ensure that it's looking
144-
# for `pixel.gif`. If it is, serve the pixel and record a hit.
145-
server = protocol.createServer protocolOptions, (req, res) ->
146-
params = url.parse req.url, true
147-
if params.pathname is '/pixel.gif'
148-
res.writeHead 200, pixelHeaders
149-
res.end pixel
150-
if key = params.query?.key
151-
record key, 1
152-
else
153-
res.writeHead 404, emptyHeaders
154-
res.end ''
155-
null
156+
server = http.createServer(handleRequest)
156157

157158
#### Startup
158159

test/test-ping.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exit = (code) ->
2828

2929
server.listen 6999, 'localhost'
3030

31-
ping = spawn 'node', ['bin/pixel-ping', 'test/config.json']
31+
ping = spawn 'node', ['bin/pixel-ping', 'test/config.json'], {stdio: 'inherit'}
3232

3333
delay = (time, func) -> setTimeout func, time
3434

0 commit comments

Comments
 (0)