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 compatibility in usePureJavaScript mode #896

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

boyxuper
Copy link

fix compatibility in usePureJavaScript mode, which globalScope can be undefined

@mattcollier
Copy link
Contributor

Hello @boyxuper, thank you for the PR. Can you determine how it is that util.globalScope is returning undefined in your use case?

https://github.com/digitalbazaar/forge/blob/master/lib/util.js#L118

@boyxuper
Copy link
Author

Yes, usePureJavaScript indicates a pure ES6 environment, which lack of window or self variables, and also not NodeJS runtime context.
It's exactly my production environment, it's wechat miniapp runtime context.

@mattcollier
Copy link
Contributor

Thanks for the additional detail. It sounds like to me that util.globalscope is returning the value of window which is undefined in your case.

Based on this one line: https://github.com/digitalbazaar/forge/blob/master/lib/util.js#L123

return typeof self === 'undefined' ? window : self;

I think adding the following conditional before L123 might work.

// applicable for wechat miniapp runtime context
if(!(window || self)) {
  return {};
}

Which I believe would eliminate the need for the additional code changes you've made.

@boyxuper
Copy link
Author

boyxuper commented Jul 29, 2021

This workaround is from this function _detectSubtleMsCrypto:
https://github.com/digitalbazaar/forge/blob/master/lib/rsa.js#L1815

And, maybe the detection for undefined variables, should be kept this way?
typeof self === 'undefined'

I worried that change type of a variable maybe too much for this.

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.

None yet

2 participants