UNPKG

1.33 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * This plugin accumulates data chunks from the client into an array
5 * property on the request, concats them on end and delivers the entire
6 * accumulated request data as one big chunk to the next plugin in the
7 * sequence. Since this plugin operates on requests, it should be the
8 * first plugin in the sequence so that subsequent plugins receive the
9 * accumulated request 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 function accumulate(req, data) {
18 if (!req._chunks) req._chunks = [];
19 req._chunks.push(data);
20 }
21
22 return {
23
24 ondata_request: function(req, res, data, next) {
25 if (data && data.length > 0) accumulate(req, data);
26 next(null, null);
27 },
28
29 onend_request: function(req, res, data, next) {
30 if (data && data.length > 0) accumulate(req, data);
31 var content = null;
32 if (req._chunks && req._chunks.length) {
33 content = Buffer.concat(req._chunks);
34 }
35 delete req._chunks;
36 next(null, content);
37 }
38 };
39
40}