Skip to content

fix compatibility in usePureJavaScript mode - #896

Open
boyxuper wants to merge 1 commit into
digitalbazaar:mainfrom
boyxuper:fix-purejs
Open

boyxuper wants to merge 1 commit into
digitalbazaar:mainfrom
boyxuper:fix-purejs

Conversation

@boyxuper

Copy link
Copy Markdown

fix compatibility in usePureJavaScript mode, which globalScope can be undefined

@mattcollier

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

boyxuper commented Jul 29, 2021 •

Copy link
Copy Markdown
Author

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.

2 participants