express-jsonstream
Advanced tools
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) { | ||
}; | ||
}; | ||
}; |
{ | ||
"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": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4244
72