+ "description_md": "Sets an njs function as a response body filter.\nThe filter function is called for each data chunk of a response body\nwith the following arguments:\n\n- `r`\n\n the [HTTP request](https://nginx.org/en/docs/njs/reference.html#http) object\n- `data`\n\n the incoming data chunk,\n may be a string or Buffer\n depending on the `buffer_type` value,\n by default is a string.\n Since [0.8.5](https://nginx.org/en/docs/njs/changes.html#njs0.8.5), the\n `data` value is implicitly converted to a valid UTF-8 string\n by default.\n For binary data, the `buffer_type` value\n should be set to `buffer`.\n- `flags`\n\n an object with the following properties:\n - `last`\n \n a boolean value, true if data is a last buffer.\n\nThe filter function can pass its own modified version\nof the input data chunk to the next body filter by calling\n[`r.sendBuffer()`](https://nginx.org/en/docs/njs/reference.html#r_sendbuffer).\nFor example, to transform all the lowercase letters in the response body:\n```\nfunction filter(r, data, flags) {\n r.sendBuffer(data.toLowerCase(), flags);\n}\n```\n\nIf the filter function changes the length of the response body, the\n\"Content-Length\" response header (if present) should be cleared\nin [`js_header_filter`](https://nginx.org/en/docs/http/ngx_http_js_module.html#js_header_filter)\nto enforce chunked transfer encoding:\n```\nexample.conf:\n location /foo {\n # proxy_pass http://localhost:8080;\n\n js_header_filter main.clear_content_length;\n js_body_filter main.filter;\n }\n\nexample.js:\n function clear_content_length(r) {\n delete r.headersOut['Content-Length'];\n }\n```\n\nTo stop filtering and pass the data chunks to the client\nwithout calling `js_body_filter`,\n[`r.done()`](https://nginx.org/en/docs/njs/reference.html#r_done)\ncan be used.\nFor example, to prepend some data to the response body:\n```\nfunction prepend(r, data, flags) {\n r.sendBuffer(\"XXX\");\n r.sendBuffer(data, flags);\n r.done();\n}\n```\n\n> As the `js_body_filter` handler\n> returns its result immediately, it supports\n> only synchronous operations.\n> Thus, asynchronous operations such as\n> [r.subrequest()](https://nginx.org/en/docs/njs/reference.html#r_subrequest)\n> or\n> [setTimeout()](https://nginx.org/en/docs/njs/reference.html#settimeout)\n> are not supported.\n\n> The directive can be specified inside the\n> [if](https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if) block\n> since [0.7.7](https://nginx.org/en/docs/njs/changes.html#njs0.7.7).",
0 commit comments