I want to call an async function from a sync function in a dialect of JavaScript known as njs (it’s part of Nginx).
I’m using njs’ js_set
to call a JavaScript function I’m writing named get_obfuscated_token
, and per the docs js_set
wants synchronous code. 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’m getting back: "http_authorization":"[object Promise]"
.
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.
2