express-mung
Advanced tools
Comparing version 0.4.6 to 0.5.0
51
index.js
@@ -162,2 +162,53 @@ 'use strict'; | ||
mung.write = function write (fn, options = {}) { | ||
return function (req, res, next) { | ||
const original = res.write; | ||
const mungError = options.mungError; | ||
function write_hook (chunk, encoding, callback) { | ||
// If res.end has already been called, do nothing. | ||
if (res.finished) { | ||
return false; | ||
} | ||
// Do not mung on errors | ||
if (!mungError && res.statusCode >= 400) { | ||
return original.apply(res, arguments); | ||
} | ||
try { | ||
let modifiedChunk = fn( | ||
chunk, | ||
// Since `encoding` is an optional argument to `res.write`, | ||
// make sure it is a string and not actually the callback. | ||
typeof encoding === 'string' ? encoding : null, | ||
req, | ||
res | ||
); | ||
// res.finished is set to `true` once res.end has been called. | ||
// If it is called in the mung function, stop execution here. | ||
if (res.finished) { | ||
return false; | ||
} | ||
// If no returned value from fn, then set it back to the original value | ||
if (modifiedChunk === undefined) { | ||
modifiedChunk = chunk; | ||
} | ||
return original.call(res, modifiedChunk, encoding, callback) | ||
} catch (err) { | ||
return mung.onError(err, req, res); | ||
} | ||
} | ||
res.write = write_hook; | ||
next && next(); | ||
} | ||
} | ||
module.exports = mung; |
{ | ||
"name": "express-mung", | ||
"version": "0.4.6", | ||
"version": "0.5.0", | ||
"description": "Transform an express response (or make until no good)", | ||
@@ -22,3 +22,4 @@ "main": "index.js", | ||
"contributors": [ | ||
"Ido Schachter <ido.schachter@ironsrc.com>" | ||
"Ido Schachter <ido.schachter@ironsrc.com>", | ||
"Sachin Hegde <sachin.hegde91@gmail.com>" | ||
], | ||
@@ -33,5 +34,5 @@ "license": "MIT", | ||
"express": "^4.13.3", | ||
"mocha": "^2.3.4", | ||
"should": "^7.1.1", | ||
"supertest": "^1.1.0" | ||
"mocha": "^3.2.0", | ||
"should": "^11.1.1", | ||
"supertest": "^3.0.0" | ||
}, | ||
@@ -38,0 +39,0 @@ "dependencies": { |
@@ -28,2 +28,3 @@ # express-mung [![Build Status](https://travis-ci.org/richardschneider/express-prefer.svg)](https://travis-ci.org/richardschneider/express-mung) | ||
function redact(body, req, res) { | ||
if (body.secret) body.secret = '****'; | ||
// ... | ||
@@ -48,3 +49,3 @@ return body; | ||
Transform the JSON body of the response. | ||
Transform the JSON body of the response. | ||
@@ -55,3 +56,3 @@ `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. | ||
Asynchronously transform the JSON body of the response. | ||
Asynchronously transform the JSON body of the response. | ||
@@ -66,8 +67,12 @@ `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. | ||
### mung.headersAsync(fn) | ||
### mung.headersAsync(fn) | ||
Asynchronously transform the HTTP headers of the response. | ||
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). | ||
### mung.write(fn, [options]) | ||
`fn(chunk, encoding, req, res)` receives the string or buffer as `chunk`, its `encoding` if applicable (`null` otherwise), `req` and `res`. It returns the modified body. If `undefined` is returned (i.e. nothing) then the original unmodified chunk is used. If `null` is returned, then a 204 No Content HTTP status is returned to client. | ||
### Notes | ||
@@ -74,0 +79,0 @@ |
28855
651
94