Socket
Socket
Sign inDemoInstall

logfmt

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logfmt - npm Package Compare versions

Comparing version 0.6.0 to 0.9.0

examples/express_pipe_to_stdout.js

57

lib/body_parser.js

@@ -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

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