New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

json-lines

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-lines - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

14

lib/route/index.js

@@ -12,5 +12,15 @@ 'use strict';

return function (req, res) {
var client = new Client(req, res);
var client,
sendHeartbeat;
var sendHeartbeat = function () {
if (req.method !== 'POST' && req.method !== 'OPTIONS') {
res.writeHead(405, {
allow: 'POST, OPTIONS'
});
return res.end();
}
client = new Client(req, res);
sendHeartbeat = function () {
client.send({ name: 'heartbeat' });

@@ -17,0 +27,0 @@ };

9

package.json
{
"name": "json-lines",
"version": "0.3.0",
"version": "0.4.0",
"description": "json-lines streams JSON Lines.",

@@ -17,9 +17,10 @@ "contributors": [

"dependencies": {
"timer2": "0.2.2"
"timer2": "0.2.3"
},
"devDependencies": {
"assertthat": "0.6.0",
"express": "4.12.3",
"body-parser": "1.13.2",
"express": "4.13.1",
"grunt": "0.4.5",
"tourism": "0.16.1"
"tourism": "0.20.2"
},

@@ -26,0 +27,0 @@ "repository": {

@@ -17,6 +17,8 @@ # json-lines

To send JSON you need an Express application and a route that you want to use. Then subscribe to the `connect` and `disconnect` events to initialize or end data transfer.
To send JSON you need an Express application and a route that you want to use. Please note that for technical reasons json-lines only works with `POST` routes.
Then subscribe to the `connect` and `disconnect` events to initialize or end data transfer.
```javascript
app.get('/events', jsonLines(function (client) {
app.post('/events', jsonLines(function (client) {
client.once('connect', function () {

@@ -46,2 +48,27 @@ // ...

### Parsing the request body
From time to time you may want to send a request body to the json-lines route, e.g. to provide a configuration object. On the server, you can access the request's body using the `req.body` property of the `client` object.
In order to do so you *must* add the [body-parser middleware](https://www.npmjs.com/package/body-parser) to your application, before adding the route itself.
```javascript
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/events', jsonLines(function (client) {
console.log(client.req.body);
// => {
// foo: 'bar'
// }
}));
```
### Using the client module
To connect to a json-lines enabled server, use the [json-lines-client](https://www.npmjs.com/package/json-lines-client) module.
**Please note that json-lines 0.4.0 is not backwards-compatible. You must use [json-lines-client](https://www.npmjs.com/package/json-lines-client) 0.6.0 or higher.**
## Running the build

@@ -48,0 +75,0 @@

@@ -6,2 +6,3 @@ 'use strict';

var assert = require('assertthat'),
bodyParser = require('body-parser'),
express = require('express'),

@@ -18,2 +19,3 @@ Timer = require('timer2');

app = express();
app.use(bodyParser.json());
http.createServer(app).listen(++port);

@@ -31,4 +33,21 @@ });

test('returns a 405 if GET is used.', function (done) {
app.get('/', route(function () {
// Intentionally left blank...
}));
http.request({
method: 'GET',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
assert.that(res.statusCode).is.equalTo(405);
assert.that(res.headers.allow).is.equalTo('POST, OPTIONS');
done();
}).end();
});
test('emits a connect event when the client connects.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.once('connect', function () {

@@ -39,3 +58,8 @@ done();

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
setTimeout(function () {

@@ -45,7 +69,40 @@ res.socket.end();

}, 0.5 * 1000);
}).end();
});
test('passes the request body to the client object.', function (done) {
var req;
app.post('/', route(function (client) {
client.once('connect', function () {
assert.that(client.req.body).is.equalTo({
foo: 'bar'
});
done();
});
}));
req = http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/',
headers: {
'content-type': 'application/json'
}
}, function (res) {
setTimeout(function () {
res.socket.end();
res.removeAllListeners();
}, 0.5 * 1000);
});
req.write(JSON.stringify({
foo: 'bar'
}));
req.end();
});
test('emits a disconnect event when the client disconnects.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.once('disconnect', function () {

@@ -56,3 +113,8 @@ done();

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
setTimeout(function () {

@@ -62,7 +124,7 @@ res.socket.end();

}, 0.5 * 1000);
});
}).end();
});
test('is able to disconnect a client.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.once('connect', function () {

@@ -74,3 +136,8 @@ client.send({ foo: 'bar' });

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
res.once('data', function (data) {

@@ -83,7 +150,7 @@ assert.that(JSON.parse(data.toString())).is.equalTo({ foo: 'bar' });

});
});
}).end();
});
test('emits a disconnect event when the client is disconnected from the server.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.once('connect', function () {

@@ -98,3 +165,8 @@ client.disconnect();

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
setTimeout(function () {

@@ -104,7 +176,7 @@ res.socket.end();

}, 0.5 * 1000);
});
}).end();
});
test('cleans up when the client disconnects.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.on('connect', function () {

@@ -123,3 +195,8 @@ // Intentionally left blank...

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
setTimeout(function () {

@@ -129,3 +206,3 @@ res.socket.end();

}, 500);
});
}).end();
});

@@ -138,9 +215,15 @@

app.get('/', route(function () {
app.post('/', route(function () {
// Intentionally left blank ;-)
}));
http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
res.on('data', function (data) {
var result = JSON.parse(data.toString());
assert.that(result.name).is.equalTo('heartbeat');

@@ -155,7 +238,7 @@ counter++;

});
});
}).end();
});
test('streams data.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
var counter = 0,

@@ -175,3 +258,8 @@ timer = new Timer(100);

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
res.on('data', function (data) {

@@ -191,7 +279,7 @@ var result = JSON.parse(data.toString());

});
});
}).end();
});
test('handles newlines in data gracefully.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.once('connect', function () {

@@ -202,3 +290,8 @@ client.send({ text: 'foo\nbar' });

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
res.on('data', function (data) {

@@ -218,7 +311,7 @@ var result = JSON.parse(data.toString());

});
});
}).end();
});
test('throws an error if data is not an object.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.once('connect', function () {

@@ -232,3 +325,8 @@ assert.that(function () {

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
setTimeout(function () {

@@ -238,7 +336,7 @@ res.socket.end();

}, 500);
});
}).end();
});
test('throws an error if data is null.', function (done) {
app.get('/', route(function (client) {
app.post('/', route(function (client) {
client.once('connect', function () {

@@ -252,3 +350,8 @@ assert.that(function () {

http.get('http://localhost:' + port, function (res) {
http.request({
method: 'POST',
hostname: 'localhost',
port: port,
path: '/'
}, function (res) {
setTimeout(function () {

@@ -258,4 +361,4 @@ res.socket.end();

}, 500);
});
}).end();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc