I want to call an async function from a sync function in a dialect of Javascript known as njs. It’s part of nginx.
Please don’t tell me I don’t need this just because browsers don’t like it. Browsers aren’t the only thing that use Javascript.
I’m using njs’ js_set to call a Javascript function I’m writing named get_obfuscated_token(), and per the doc js_set wants synchronous code (please see https://nginx.org/en/docs/http/ngx_http_js_module.html if you don’t believe me). get_obfuscated_token() needs to compute a SHA-512 hash, which seems to mean:
result = await crypto.subtle.digest("SHA-512", msgUint8);
But it appears I can’t await on crypto.subtle.digest from a synchronous function. I’d share an error message/stack trace if I had one, but njs is a little tight-lipped about such things. I can say that I’m getting back: "http_authorization":"[object Promise]"
.
So anyway there’s a bit of a conflict. I need to provide a synchronous function, but that function needs to call a function that is available in an asynchronous form.
How can I call crypto.subtle.digest() in a way that will keep js_set happy?
In the worst case, I could resort to a pure-javascript SHA-512 function (like perhaps https://github.com/emn178/js-sha512), and make sure it’s synchronous. But I’d rather not do that if there’s a way that’ll mean less code to maintain and won’t require a license review.
Thanks!