forge icon indicating copy to clipboard operation
forge copied to clipboard

fix compatibility in `usePureJavaScript` mode

Open boyxuper opened this issue 4 years ago • 4 comments

fix compatibility in usePureJavaScript mode, which globalScope can be undefined

boyxuper avatar Jul 28 '21 07:07 boyxuper

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

mattcollier avatar Jul 28 '21 17:07 mattcollier

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.

boyxuper avatar Jul 29 '21 06:07 boyxuper

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.

mattcollier avatar Jul 29 '21 13:07 mattcollier

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.

boyxuper avatar Jul 29 '21 14:07 boyxuper