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

[FIX] nacl should not be undefined when module.exports is defined #21

Merged
merged 1 commit into from
Dec 21, 2022
Merged

[FIX] nacl should not be undefined when module.exports is defined #21

merged 1 commit into from
Dec 21, 2022

Conversation

sdenovan
Copy link
Contributor

Context:

(function(nacl) {
... // Jump to the end of the function
})(typeof module !== 'undefined' && module.exports ? module.exports : globalThis.nacl = globalThis.nacl || {});
const nacl = globalThis.nacl;

Reviewing the ternary clause (above) that determines what to pass as nacl to the function: if module.exports is defined, nacl will be written into module.exports and globalThis.nacl won't be reached in the ternary else clause where it would have been defined. So nacl becomes undefined when module.exports is defined via the the next statement (show above but copied here for clarity):

const nacl = globalThis.nacl;

And the first attempt to work with nacl in the derived websocket projects (nats.ws) results in .sign going against the undefined nacl object:

const nacl = globalThis.nacl; // globalThis.nacl isn't defined when module.exports is defined
const denoHelper = {
    fromSeed: nacl.sign.keyPair.fromSeed, // nacl is undefined because it was written into module.exports, not globalThis.nacl
    sign: nacl.sign.detached,
    verify: nacl.sign.detached.verify,
    randomBytes: nacl.randomBytes
};

This PR makes the nacl assignment mirror the ternary condition, yielding:

const nacl = typeof module !== 'undefined' && module.exports ? module.exports : globalThis.nacl;

See my comment on the related nats.ws issue for additional context.

@aricart aricart self-requested a review December 21, 2022 12:40
Copy link
Member

@aricart aricart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - @sdenovan thank you for the contribution

@aricart
Copy link
Member

aricart commented Dec 21, 2022

I am going to put this in and release, but for esm I have a new option that will eliminate the generation, after I verify this, and do the release I will explore this, as the nkey library could just be loaded directly by Deno when building thus removing a step

@aricart aricart merged commit 125cd8c into nats-io:main Dec 21, 2022
aricart added a commit that referenced this pull request Dec 21, 2022
[FIX] propagated #21 to the tweetnacl-esm.ts, as the esm repackaging is done by a script
[BUMP] build/ci dependencies
aricart added a commit that referenced this pull request Dec 21, 2022
@sdenovan sdenovan deleted the fix-undefined-nacl branch December 21, 2022 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants