Comparing version 0.4.2 to 0.4.3
'use strict'; | ||
const events = require('events'), | ||
util = require('util'); | ||
util = require('util'); | ||
@@ -23,3 +23,13 @@ const EventEmitter = events.EventEmitter; | ||
this.res.write(JSON.stringify(data) + '\n'); | ||
try { | ||
this.res.write(`${JSON.stringify(data)}\n`); | ||
} catch (ex) { | ||
if (ex.message === 'write after end') { | ||
// Ignore write after end errors. This simply means that the connection | ||
// was closed concurrently, and we can't do anything about it anyway. | ||
// Hence, simply return. | ||
return; | ||
} | ||
throw ex; | ||
} | ||
}; | ||
@@ -26,0 +36,0 @@ |
@@ -16,2 +16,3 @@ 'use strict'; | ||
}); | ||
return res.end(); | ||
@@ -52,3 +53,5 @@ } | ||
/* eslint-disable callback-return */ | ||
callback(client); | ||
/* eslint-enable callback-return */ | ||
client.emit('connect'); | ||
@@ -55,0 +58,0 @@ }; |
{ | ||
"name": "json-lines", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "json-lines streams JSON Lines.", | ||
@@ -20,7 +20,6 @@ "contributors": [ | ||
"devDependencies": { | ||
"assertthat": "0.6.0", | ||
"body-parser": "1.15.0", | ||
"assertthat": "0.7.0", | ||
"body-parser": "1.15.1", | ||
"express": "4.13.4", | ||
"gulp": "^3.9.1", | ||
"roboter": "^0.5.1" | ||
"roboter": "0.8.1" | ||
}, | ||
@@ -27,0 +26,0 @@ "repository": { |
@@ -6,19 +6,21 @@ 'use strict'; | ||
const assert = require('assertthat'), | ||
bodyParser = require('body-parser'), | ||
express = require('express'), | ||
Timer = require('timer2'); | ||
bodyParser = require('body-parser'), | ||
express = require('express'), | ||
Timer = require('timer2'); | ||
const route = require('../lib/route')(1.5); | ||
suite('route', function () { | ||
suite('route', () => { | ||
let app, | ||
port = 3000; | ||
setup(function () { | ||
setup(() => { | ||
app = express(); | ||
app.use(bodyParser.json()); | ||
http.createServer(app).listen(++port); | ||
port += 1; | ||
http.createServer(app).listen(port); | ||
}); | ||
test('is a function.', function (done) { | ||
test('is a function.', done => { | ||
assert.that(route).is.ofType('function'); | ||
@@ -28,3 +30,3 @@ done(); | ||
test('returns a 405 if GET is used.', function (done) { | ||
test('returns a 405 if GET is used.', done => { | ||
app.get('/', route(() => { | ||
@@ -46,3 +48,3 @@ // Intentionally left blank... | ||
test('emits a connect event when the client connects.', function (done) { | ||
test('emits a connect event when the client connects.', done => { | ||
app.post('/', route(client => { | ||
@@ -67,3 +69,3 @@ client.once('connect', () => { | ||
test('passes the request body to the client object.', function (done) { | ||
test('passes the request body to the client object.', done => { | ||
app.post('/', route(client => { | ||
@@ -95,3 +97,3 @@ client.once('connect', () => { | ||
test('emits a disconnect event when the client disconnects.', function (done) { | ||
test('emits a disconnect event when the client disconnects.', done => { | ||
app.post('/', route(client => { | ||
@@ -116,3 +118,3 @@ client.once('disconnect', () => { | ||
test('is able to disconnect a client.', function (done) { | ||
test('is able to disconnect a client.', done => { | ||
app.post('/', route(client => { | ||
@@ -143,3 +145,3 @@ client.once('connect', () => { | ||
test('emits a disconnect event when the client is disconnected from the server.', function (done) { | ||
test('emits a disconnect event when the client is disconnected from the server.', done => { | ||
app.post('/', route(client => { | ||
@@ -168,3 +170,3 @@ client.once('connect', () => { | ||
test('cleans up when the client disconnects.', function (done) { | ||
test('cleans up when the client disconnects.', done => { | ||
app.post('/', route(client => { | ||
@@ -216,3 +218,3 @@ client.on('connect', () => { | ||
assert.that(result.name).is.equalTo('heartbeat'); | ||
counter++; | ||
counter += 1; | ||
@@ -222,3 +224,4 @@ if (counter === 3) { | ||
res.removeAllListeners(); | ||
done(); | ||
return done(); | ||
} | ||
@@ -229,3 +232,26 @@ }); | ||
test('streams data.', function (done) { | ||
test('immediately sends the first heartbeat.', done => { | ||
app.post('/', route(() => { | ||
// Intentionally left blank ;-) | ||
})); | ||
http.request({ | ||
method: 'POST', | ||
hostname: 'localhost', | ||
port, | ||
path: '/' | ||
}, res => { | ||
res.on('data', data => { | ||
const result = JSON.parse(data.toString()); | ||
assert.that(result.name).is.equalTo('heartbeat'); | ||
res.socket.end(); | ||
res.removeAllListeners(); | ||
return done(); | ||
}); | ||
}).end(); | ||
}); | ||
test('streams data.', done => { | ||
app.post('/', route(client => { | ||
@@ -238,3 +264,4 @@ const timer = new Timer(100); | ||
timer.on('tick', () => { | ||
client.send({ counter: counter++ }); | ||
client.send({ counter }); | ||
counter += 1; | ||
}); | ||
@@ -265,3 +292,4 @@ }); | ||
res.removeAllListeners(); | ||
done(); | ||
return done(); | ||
} | ||
@@ -272,3 +300,3 @@ }); | ||
test('handles newlines in data gracefully.', function (done) { | ||
test('handles newlines in data gracefully.', done => { | ||
app.post('/', route(client => { | ||
@@ -285,3 +313,3 @@ client.once('connect', () => { | ||
path: '/' | ||
}, function (res) { | ||
}, res => { | ||
res.on('data', data => { | ||
@@ -302,3 +330,3 @@ const result = JSON.parse(data.toString()); | ||
test('throws an error if data is not an object.', function (done) { | ||
test('throws an error if data is not an object.', done => { | ||
app.post('/', route(client => { | ||
@@ -326,3 +354,3 @@ client.once('connect', () => { | ||
test('throws an error if data is null.', function (done) { | ||
test('throws an error if data is null.', done => { | ||
app.post('/', route(client => { | ||
@@ -329,0 +357,0 @@ client.once('connect', () => { |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14924
4
407
1