Comparing version 0.6.0 to 0.9.0
@@ -7,16 +7,2 @@ | ||
/* | ||
noop middleware. | ||
*/ | ||
var noop, utils, _limit; | ||
noop = function(req, res, next) { | ||
return next(); | ||
}; | ||
utils = require("express/node_modules/connect/lib/utils"); | ||
_limit = require("express/node_modules/connect/lib/middleware/limit"); | ||
/* | ||
JSON: | ||
@@ -38,3 +24,2 @@ | ||
if (options == null) options = {}; | ||
limit = (options.limit ? _limit(options.limit) : noop); | ||
@@ -44,28 +29,24 @@ return function(req, res, next) { | ||
req.body = req.body || {}; | ||
if (!utils.hasBody(req)) return next(); | ||
if (options.content_type !== utils.mime(req)) return next(); | ||
var is_logplex = req.get('content-type') === "application/logplex-1"; | ||
if (!is_logplex) return next(); | ||
req._body = true; | ||
var logplex = limit(req, res, function(err) { | ||
var buf; | ||
if (err) return next(err); | ||
buf = ""; | ||
req.setEncoding("utf8"); | ||
req.on("data", function(chunk) { | ||
return buf += chunk; | ||
}); | ||
req.on("end", function() { | ||
var first; | ||
first = buf.trim(); | ||
try { | ||
req.body = options.parser(buf); | ||
} catch (err) { | ||
err.body = buf; | ||
err.status = 400; | ||
return next(err); | ||
} | ||
return next(); | ||
}); | ||
var buf; | ||
buf = ""; | ||
req.setEncoding("utf8"); | ||
req.on("data", function(chunk) { | ||
return buf += chunk; | ||
}); | ||
return logplex; | ||
req.on("end", function() { | ||
var first; | ||
first = buf.trim(); | ||
try { | ||
req.body = options.parser(buf); | ||
} catch (err) { | ||
err.body = buf; | ||
err.status = 400; | ||
return next(err); | ||
} | ||
return next(); | ||
}); | ||
}; | ||
}; |
@@ -0,1 +1,4 @@ | ||
var split = require('split'); | ||
var through = require('through'); | ||
var parse = require('./lib/logfmt_parser').parse; | ||
@@ -21,20 +24,47 @@ | ||
try { | ||
//this will fail if express is not on the require path | ||
var body_parser = require('./lib/body_parser') | ||
var logplex = function (body) { | ||
var lines = [] | ||
body.split("\n").forEach(function(line){ | ||
lines.push(parse(line)) | ||
}) | ||
return lines; | ||
} | ||
//Syncronous Body Parser | ||
var bodyParser = require('./lib/body_parser') | ||
exports.bodyParser = function() { | ||
return body_parser({content_type: "application/logplex-1", parser: logplex}) | ||
var logfmtBodyParser = function (body) { | ||
var lines = [] | ||
body.split("\n").forEach(function(line){ | ||
lines.push(parse(line)) | ||
}) | ||
return lines; | ||
} | ||
exports.bodyParser = function() { | ||
return bodyParser({content_type: "application/logplex-1", parser: logfmtBodyParser}) | ||
} | ||
//Stream Body Parser | ||
var Readable = require('readable-stream').Readable; | ||
var bodyParserStream = function(options){ | ||
return function(req, res, next) { | ||
//setup | ||
if (req._body) return next(); | ||
var is_logplex = req.get('content-type') === "application/logplex-1"; | ||
if (!is_logplex) return next(); | ||
req._body = true; | ||
//define Readable body Stream | ||
req.body = new Readable({ objectMode: true }); | ||
req.body._read = function(n) { | ||
req.body._paused = false; | ||
}; | ||
function parseLine(line) { | ||
if(line) { | ||
var parsedLine = parse(line); | ||
if(!req.body._paused) req.body._paused = !req.body.push(parsedLine); | ||
} | ||
} | ||
function end() { req.body.push(null); } | ||
req.pipe(split()).pipe(through(parseLine, end)); | ||
return next(); | ||
} | ||
} | ||
catch(e){ | ||
//no express defined | ||
} | ||
exports.bodyParserStream = bodyParserStream; |
{ | ||
"name": "logfmt", | ||
"version": "0.6.0", | ||
"version": "0.9.0", | ||
"description": "Key-Value log line parser", | ||
@@ -12,3 +12,4 @@ "main": "logfmt.js", | ||
"split" : "*", | ||
"through" : "*" | ||
"through" : "*", | ||
"readable-stream" : "*" | ||
}, | ||
@@ -15,0 +16,0 @@ "devDependencies" : { |
@@ -59,3 +59,53 @@ # node-logfmt | ||
#### Streaming | ||
If you use the `logfmt.bodyParserStream()` for a body parser, | ||
you will have a `req.body` that is a readable stream. | ||
```javascript | ||
var app = require('express')(); | ||
var http = require('http'); | ||
var logfmt = require('logfmt'); | ||
app.use(logfmt.bodyParserStream()); | ||
// req.body is now a Readable Stream | ||
app.post('/logs', function(req, res){ | ||
req.body.on('readable', function(){ | ||
var parsedLine = req.body.read(); | ||
if(parsedLine) console.log(parsedLine); | ||
else res.send('OK'); | ||
}) | ||
}) | ||
http.createServer(app).listen(3000); | ||
``` | ||
```javascript | ||
var app = require('express')(); | ||
var http = require('http'); | ||
var through = require('through'); | ||
var logfmt = require('logfmt'); | ||
app.use(logfmt.bodyParserStream()); | ||
app.post('/logs', function(req, res){ | ||
if(!req.body) return res.send('OK'); | ||
req.body.pipe(through(function(line){ | ||
console.dir(line); | ||
})) | ||
res.send('OK'); | ||
}) | ||
http.createServer(app).listen(3000); | ||
``` | ||
#### Non-Streaming | ||
If you use the `logfmt.bodyParser()` for a body parser, | ||
you will have a `req.body` that is an array of objects. | ||
```javascript | ||
var logfmt = require('logfmt'); | ||
@@ -77,3 +127,3 @@ | ||
app.listen(3000) | ||
http.createServer(app).listen(3000); | ||
``` | ||
@@ -80,0 +130,0 @@ |
Sorry, the diff of this file is not supported yet
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
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
19825
23
347
152
3
3
4
+ Addedreadable-stream@*
+ Addedabort-controller@3.0.0(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedreadable-stream@4.5.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)