Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It'd be great if we could js_set an async function, not just sync #758

Closed
dstromberg opened this issue Jul 9, 2024 · 2 comments
Closed
Labels

Comments

@dstromberg
Copy link

Is your feature request related to a problem? Please describe

A clear and concise description of what the problem is.
Yes. If I try to crypto.subtle.digest("SHA-512", msgUint8), that means async is required. But if I try to call it from js_set, then sync is required. TTBOMK, that's an inherent conflict that keeps js_set from being used with with crypto.subtle.digest.

Describe the solution you'd like

js_set'ing an async function should "do the right thing", instead of returning a string that vaguely describes the Promise.

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.
I tried a few ways of turning the async crypto.subtle.digest into something synchronous, but none of them worked. The general Javascript community seems hostile to the idea.
I also tried to find a synchronous SHA-512 module, but found that none of the ones I tried were "compatible enough" with njs.

Additional context

Add any other context or screenshots about the feature request here.

@xeioex
Copy link
Contributor

xeioex commented Jul 9, 2024

Hi @dstromberg,

See r.setReturnValue() for this.

For example:

http {
    js_import http.js;

    js_set $hash    http.hash;

    server {
        listen 8000;

        location = /crypto {
            add_header Hash $hash;
            return     200;
        }
    }
}
async function hash(r) {
    let hash = await crypto.subtle.digest('SHA-512', r.headersIn.host);
    r.setReturnValue(Buffer.from(hash).toString('hex'));
}

export default {hash};
@dstromberg
Copy link
Author

r.setReturnValue() helped in one case, and didn't in another. I'm going to open a new ticket about the one that doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 participants