microgateway-plugins
Version:
Plugins for Apige Edge Microgateway
55 lines (45 loc) • 1.85 kB
JavaScript
;
const debug = require('debug')('plugin:accumulate-response');
const helperFunctions = require('../lib/helperFunctions');
/**
* This plugin accumulates data chunks from the target into an array
* property on the response, concats them on end and delivers the entire
* accumulated response data as one big chunk to the next plugin in the
* sequence. Since this plugin operates on responses, which are applied
* in reverse order, it should be the last plugin in the sequence so
* that subsequent plugins receive the accumulated response data.
*
* Users should be aware that buffering large requests or responses in
* memory can cause Apigee Edge Microgateway to run out of memory under
* high load or with a large number of concurrent requests. So this plugin
* should only be used when it is known that request/response bodies are small.
*/
const LOG_TAG_COMP = 'accumulate-response';
module.exports.init = function(/*config, logger, stats*/) {
function accumulate(res, data) {
if (!res._chunks) res._chunks = [];
res._chunks.push(data);
}
return {
ondata_response: function(req, res, data, next) {
data = helperFunctions.toBuffer(data);
if (data && data.length > 0) accumulate(res, data);
next(null, null);
},
onend_response: function(req, res, data, next) {
if (data && data.length > 0) accumulate(res, data);
var content = null;
if(res._chunks && res._chunks.length) {
try {
content = Buffer.concat(res._chunks);
} catch(err){
debug('Error in creating buffered content', err);
content='';
logger.eventLog({level:'warn', req: req, res: res, err:err, component:LOG_TAG_COMP }, "Error in creating buffered content");
}
}
delete res._chunks;
next(null, content);
}
};
}