Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-jsonstream

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-jsonstream - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

14

example.js

@@ -11,3 +11,15 @@ var jsonStream = require('./index');

app.post('/stream-yo', function(req, res) {
req.jsonStream()
.on('object', console.log)
.on('response', function(response) {
if(response.statusCode !== 200) console.error('got non 200 response', response);
})
.on('error', console.error).on('end', function() {
res.send('ok');
});
});
app.listen(8553);
console.log('listening on', 8553);
console.log('listening on', 8553);

44

index.js

@@ -12,43 +12,19 @@ /*

function RemnantError(remnant) {
this.name = "RemnantError"
this.message = "There was data remaining on a JSON stream."
this.remnant = remnant;
}
RemnantError.prototype = new Error();
RemnantError.prototype.constructor = RemnantError;
var jsonStreamSplitter = require('json-stream-splitter');
module.exports = function jsonStream(bytes){
return function jsonStream(req, res, next) {
var incomingData = ""
var incomingData = '';
var errs = [];
// for parsing incoming jsonstream data via a POST or PUT request
req.jsonStream = function(cbEach, cbDone) {
if (req.method != "POST" && req.method != "PUT") {
return cbDone(new Error("Can not stream in unless the request method is POST or PUT got " + req.method));
req.jsonStream = function() {
var splitStream = jsonStreamSplitter.splitStream(req);
if (req.method !== 'POST' && req.method !== 'PUT') {
splitStream.emit('error', new Error('Can not stream in unless the request method is POST or PUT, got ' + req.method));
}
if (req.headers["content-type"] != "application/jsonstream") {
return cbDone(new Error("Only a content-type of application/jsonstream may be streamed in got " + req.headers["content-type"]));
if (req.headers['content-type'] !== 'application/jsonstream') {
splitStream.emit('error', new Error('Only a content-type of application/jsonstream may be streamed in got ' + req.headers['content-type']));
}
req.on("data", function(data) {
incomingData += data.toString("utf8");
var chunks = incomingData.split("\n");
// The last one will always have the last bit or empty
incomingData = chunks[chunks.length - 1];
// Iterate over all but the last one, handled above
for (var i = 0; i < chunks.length - 1; ++i) {
var obj;
try {
obj = JSON.parse(chunks[i]);
} catch (E) {
errs.push(E);
}
cbEach(obj);
}
});
req.on("end", function() {
if (incomingData) errs.push(new RemnantError(incomingData));
if (errs.length > 0) return cbDone(errs);
cbDone();
});
return splitStream;
}

@@ -55,0 +31,0 @@

{
"name": "express-jsonstream",
"description": "Simple middleware for JSON streaming in Express",
"version": "0.0.2",
"version": "0.0.3",
"author": "Simon Murtha-Smith <simon@murtha-smith.com>",

@@ -16,2 +16,5 @@ "keywords": [

},
"dependencies": {
"json-stream-splitter": "0.0.3"
},
"engines": {

@@ -18,0 +21,0 @@ "node": ">=0.4.6 <0.7.0"

@@ -13,2 +13,13 @@ Simple middleware for JSON streaming in Express. Stringifys and sends object delimited by ```\n```'s with the ```Content-Type : application/jsonstream``` header

app.post('/stream-yo', function(req, res) {
req.jsonStream()
.on('object', console.log)
.on('response', function(response) {
if(response.statusCode !== 200) console.error('got non 200 response', response);
})
.on('error', console.error).on('end', function() {
res.send('ok');
});
});
app.listen(8553);

@@ -41,2 +52,25 @@ console.log('listening on', 8553);

* Connection #0 to host localhost left intact
* Closing connection #0
* Closing connection #0
a POST to /stream-yo
curl "http://localhost:8553/stream-yo" -d "{\"balh\":\"asdasd\"}"$'\n' -H "content-type: application/jsonstream" -v
* About to connect() to localhost port 8553 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8553 (#0)
> POST /stream-yo HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:8553
> Accept: */*
> content-type: application/jsonstream
> Content-Length: 18
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 2
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
ok
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