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.1 to 0.0.2

44

index.js

@@ -12,4 +12,46 @@ /*

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;
module.exports = function jsonStream(bytes){
return function jsonStream(req, res, next) {
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));
}
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"]));
}
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();
});
}
// for pushing out jsonstream data via a GET request
var first = true;

@@ -26,2 +68,2 @@ res.jsonStream = function(object) {

};
};
};

2

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

@@ -6,0 +6,0 @@ "keywords": [

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