nginx / njs

A subset of JavaScript language to use in nginx
http://nginx.org/en/docs/njs/
BSD 2-Clause "Simplified" License
1.14k stars 148 forks source link

Sending a Buffer with r.send() does not work #701

Closed mtyaka closed 5 months ago

mtyaka commented 5 months ago

According to the documentation it should be possible to use either a String or a Buffer instance when using r.send() to send part of the response to the client.

If I try to send a Buffer, the request fails and I see an error in the log:

js exception: TypeError: Cannot convert object to primitive value

Here's is my example handler that I'm trying to use with the js_content directive:

function handler(req) {
  req.status = 200;
  req.sendHeader();
  req.send(Buffer.from('mystring'));
  req.finish();
}

Sending a String instead of a Buffer with r.send("mystring") works fine. Sending the entire response as a buffer using r.return(200, Buffer.from('mystring')) seems to work fine as well.

I'm using njs 0.8.3 with nginx 1.24.0.

xeioex commented 5 months ago

Hi @mtyaka,

Thank you for reporting the issue. It will be fixed. Meanwhile you can use the code below:

function handler(req) {
  req.return(200, Buffer.from('mystring'));
}