forge
forge copied to clipboard
fix compatibility in `usePureJavaScript` mode
fix compatibility in usePureJavaScript mode, which globalScope can be undefined
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
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.
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.
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.