UNPKG

1.38 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * This plugin accumulates data chunks from the target into an array
5 * property on the response, concats them on end and delivers the entire
6 * accumulated response data as one big chunk to the next plugin in the
7 * sequence. Since this plugin operates on responses, which are applied
8 * in reverse order, it should be the last plugin in the sequence so
9 * that subsequent plugins receive the accumulated response data.
10 *
11 * Users should be aware that buffering large requests or responses in
12 * memory can cause Apigee Edge Microgateway to run out of memory under
13 * high load or with a large number of concurrent requests. So this plugin
14 * should only be used when it is known that request/response bodies are small.
15 */
16module.exports.init = function(/*config, logger, stats*/) {
17
18 function accumulate(res, data) {
19 if (!res._chunks) res._chunks = [];
20 res._chunks.push(data);
21 }
22
23 return {
24
25 ondata_response: function(req, res, data, next) {
26 if (data && data.length > 0) accumulate(res, data);
27 next(null, null);
28 },
29
30 onend_response: function(req, res, data, next) {
31 if (data && data.length > 0) accumulate(res, data);
32 var content = null;
33 if(res._chunks && res._chunks.length) {
34 content = Buffer.concat(res._chunks);
35 }
36 delete res._chunks;
37 next(null, content);
38 }
39
40 };
41
42}