1 | ;
|
2 |
|
3 | /**
|
4 | * This plugin transforms content. Since this transformation (uppercase) is
|
5 | * trivial, it can do the transformation on a chunk-by-chunk basis or all-at-once.
|
6 | * For most non-trivial cases, this plugin should be preceded by the accumulate
|
7 | * plugin, which will accumulate chunks and deliver them concatenated as one data
|
8 | * Buffer to the onend handler.
|
9 | */
|
10 | module.exports.init = function(config, logger, stats) {
|
11 |
|
12 | // perform content transformation here
|
13 | // the result of the transformation must be another Buffer
|
14 | function transform(data) {
|
15 | return new Buffer(data.toString().toUpperCase());
|
16 | }
|
17 |
|
18 | return {
|
19 |
|
20 | ondata_response: function(req, res, data, next) {
|
21 | // transform each chunk as it is received
|
22 | next(null, data ? transform(data) : null);
|
23 | },
|
24 |
|
25 | onend_response: function(req, res, data, next) {
|
26 | // transform accumulated data, if any
|
27 | next(null, data ? transform(data) : null);
|
28 | },
|
29 |
|
30 | ondata_request: function(req, res, data, next) {
|
31 | // transform each chunk as it is received
|
32 | next(null, data ? transform(data) : null);
|
33 | },
|
34 |
|
35 | onend_request: function(req, res, data, next) {
|
36 | // transform accumulated data, if any
|
37 | next(null, data ? transform(data) : null);
|
38 | }
|
39 |
|
40 | };
|
41 |
|
42 | }
|