Socket
Socket
Sign inDemoInstall

express-mung

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.4.4 to 0.4.6

14

index.js

@@ -19,5 +19,8 @@ 'use strict';

mung.json = function json (fn) {
mung.json = function json (fn, options) {
return function (req, res, next) {
let original = res.json;
options = options || {};
let mungError = options.mungError;
function json_hook (json) {

@@ -28,3 +31,3 @@ let originalJson = json;

return res;
if (res.statusCode >= 400)
if (!mungError && res.statusCode >= 400)
return original.call(this, json);

@@ -63,5 +66,8 @@

mung.jsonAsync = function json (fn) {
mung.jsonAsync = function json (fn, options) {
return function (req, res, next) {
let original = res.json;
options = options || {};
let mungError = options.mungError;
function json_async_hook (json) {

@@ -71,3 +77,3 @@ res.json = original;

return;
if (res.statusCode >= 400)
if (!mungError && res.statusCode >= 400)
return original.call(this, json);

@@ -74,0 +80,0 @@ try {

{
"name": "express-mung",
"version": "0.4.4",
"version": "0.4.6",
"description": "Transform an express response (or make until no good)",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -45,16 +45,38 @@ # express-mung [![Build Status](https://travis-ci.org/richardschneider/express-prefer.svg)](https://travis-ci.org/richardschneider/express-mung)

- `mung.json(fn)` transform the JSON body of the response. `fn(json, req, res)` receives the JSON as an object, the `req` and `res`. It returns the modified body. If `undefined` is returned (i.e. nothing) then the original JSON is assumed to be modified. If `null` is returned, then a 204 No Content HTTP status is returned to client.
### mung.json(fn, [options])
- `mung.jsonAsync(fn)` transform the JSON body of the response. `fn(json, req, res)` receives the JSON as an object, the `req` and `res`. It returns a promise to a modified body. The promise returns an `object.` If it is `null` then a 204 No Content is sent to the client.
Transform the JSON body of the response.
- `mung.headers(fn)` transform the HTTP headers of the response. `fn(req, res)` receives the `req` and `res`. It should modify the header(s) and then return.
`fn(json, req, res)` receives the JSON as an object, the `req` and `res`. It returns the modified body. If `undefined` is returned (i.e. nothing) then the original JSON is assumed to be modified. If `null` is returned, then a 204 No Content HTTP status is returned to client.
- `mung.headersAsync(fn)` transform the HTTP headers of the response. `fn(req, res)` receives the `req` and `res`. It returns a `promise` to modify the header(s).
### mung.jsonAsync(fn, [options])
**NOTE** when `mung.json*` receives a scalar value then the `content-type` is switched `text-plain`.
Asynchronously transform the JSON body of the response.
**NOTE** when `mung.json*` detects that a response has been sent, it will abort.
`fn(json, req, res)` receives the JSON as an object, the `req` and `res`. It returns a promise to a modified body. The promise returns an `object.` If it is `null` then a 204 No Content is sent to the client.
**NOTE** sending a response while in `mung.headers*` is **undefined behaviour** and will most likely result in an error.
### mung.headers(fn)
Transform the HTTP headers of the response.
`fn(req, res)` receives the `req` and `res`. It should modify the header(s) and then return.
### mung.headersAsync(fn)
Asynchronously transform the HTTP headers of the response.
`fn(req, res)` receives the `req` and `res`. It returns a `promise` to modify the header(s).
### Notes
* when `mung.json*` receives a scalar value then the `content-type` is switched `text-plain`.
* when `mung.json*` detects that a response has been sent, it will abort.
* sending a response while in `mung.headers*` is **undefined behaviour** and will most likely result in an error.
### options
- `mungError`, when `true` the munger function is always invoked. When `false` (the default) the munger function is only invoked when the response is not in error.
## Exception handling

@@ -61,0 +83,0 @@

@@ -41,2 +41,30 @@ 'use strict';

it('should not mung an error response (by default)', done => {
let server = express()
.use(mung.json(inspect))
.get('/', (req, res) => res.status(404).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(404)
.expect(res => {
res.body.should.not.have.property('inspected_by');
})
.end(done);
});
it('should mung an error response when told to', done => {
let server = express()
.use(mung.json(inspect, { mungError: true }))
.get('/', (req, res) => res.status(404).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(404)
.expect(res => {
let expected = {a : 'a', 'inspected_by': 'me'};
res.body.should.eql(expected);
res.headers['content-length'].should.equal(JSON.stringify(expected).length.toString())
})
.end(done);
});
it('should return 204 on null JSON result', done => {

@@ -43,0 +71,0 @@ let server = express()

@@ -49,2 +49,30 @@ 'use strict';

it('should not mung an error response (by default)', done => {
let server = express()
.use(mung.jsonAsync(inspect))
.get('/', (req, res) => res.status(404).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(404)
.expect(res => {
res.body.should.not.have.property('inspected_by');
})
.end(done);
});
it('should mung an error response when told to', done => {
let server = express()
.use(mung.jsonAsync(inspect, { mungError: true} ))
.get('/', (req, res) => res.status(404).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(404)
.expect(res => {
let expected = {a : 'a', 'inspected_by': 'me'};
res.body.should.eql(expected);
res.headers['content-length'].should.equal(JSON.stringify(expected).length.toString())
})
.end(done);
});
it('should return 204 on null JSON result', done => {

@@ -51,0 +79,0 @@ let server = express()

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc