Welcome! Log In Create A New Profile

Advanced

ngx_stream_js_module: ngx.fetch in js_filter function

Posted by wice 
ngx_stream_js_module: ngx.fetch in js_filter function
July 13, 2022 11:48AM
Hi!

I'm working on an MQTT reverse proxy.

In the js_preread handler function I can do this:

function preread(s) {
s.on("upload", function(data, flags) {
ngx.fetch("http://...").then(reply => reply.text().then(result => {
if (isResultCool(result)) {
s.allow();
} else {
s.deny();
}
}));
}
}

and it works (so the connection gets allowed or denied based on the async result of my fetch call).

But in the js_filter handler function, this doesn't work:

const DISCONNECT = Uint8Array.of(224, 0);
function filter(s) {
s.on("upload", function (data, flags) {
ngx.fetch("http://...").then(reply => reply.text().then(result => {
if (isResultCool(result)) {
s.send(data);
} else {
s.send(DISCONNECT);
}
}));
});
}

So basically s.send(...) doesn't seem to work if I call it in the callback of an asynchronous function.

My questions are:
1. Is this intentional, or a bug?
2. If it's intentional, is there any way to make a blocking http call in the js_filter handler method?

This is very frustrating because there is some information I only have in the MQTT SUBSCRIBE an PUBLISH messages (which are only passed to the filter function), so I cannot use the info in the CONNECT message to make this call to the HTTP endpoint.

Thanks in advance.

(Sorry for the formatting, I don't know how to insert code blocks here.)
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 58
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready