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

V10.34.0 _utils.isBrowserEnv not working in all cases. #1134

Open
spydmobile opened this issue Mar 28, 2024 · 1 comment
Open

V10.34.0 _utils.isBrowserEnv not working in all cases. #1134

spydmobile opened this issue Mar 28, 2024 · 1 comment
Labels

Comments

@spydmobile
Copy link

Sorry but I cant reproduce this outside of my code yet,but i am reporting it anyway.

Using "dropbox": "^10.34.0", in Node 21, in javascript, on mac m2 studio on the backend only not in the client.
I use the api perfectly fine until I have to reject an error back to the webUI, after which _utils.isBrowserEnv stops working correctly and causes:

/Users/xxx/localcode/dropbox_service/node_modules/dropbox/cjs/src/auth.js:58
      fetch = window.fetch.bind(window);
                           ^

So: this is the code from the current version of dropbox:

   if ((0, _utils.isBrowserEnv)()) {
      fetch = window.fetch.bind(window);
      crypto = window.crypto || window.msCrypto; // for IE11
    } else if ((0, _utils.isWorkerEnv)()) {
      /* eslint-disable no-restricted-globals */
      fetch = self.fetch.bind(self);
      crypto = self.crypto;
      /* eslint-enable no-restricted-globals */
    } else {
      fetch = require('node-fetch'); // eslint-disable-line global-require

      crypto = require('crypto'); // eslint-disable-line global-require
    }

after i reject and send my error to the client, this logic no longer works correctly, and mistakenly thinks its in the browser environment instead so tries to use browser fetch, from the browsers global window object, which does not exist in NodeJS.

so to fix this, I altered the dropbox code to this:

   if ((typeof window == 'undefined')&&(0, _utils.isBrowserEnv)()) {
      fetch = window.fetch.bind(window);
      crypto = window.crypto || window.msCrypto; // for IE11
    } else if ((0, _utils.isWorkerEnv)()) {
      /* eslint-disable no-restricted-globals */
      fetch = self.fetch.bind(self);
      crypto = self.crypto;
      /* eslint-enable no-restricted-globals */
    } else {
      fetch = require('node-fetch'); // eslint-disable-line global-require

      crypto = require('crypto'); // eslint-disable-line global-require
    }

I have added a safety check, so that the dropbox utility function "isBrowserEnv" which is failing to work properly, must pass, but also, the existence of the window object must pass before it can use the browser window version of fetch, so now it falls back to loading node-fetch and works correctly!!!!!!!

@spydmobile spydmobile added the bug label Mar 28, 2024
@greg-db
Copy link
Contributor

greg-db commented Apr 1, 2024

Thanks for the report. I'll be happy to send this along to the team to see if they can look into if/how we can address this in the SDK, but I expect they'll want to be able to reproduce the issue to make sure they understand it and can address it properly. That being the case, can you share some steps to reproduce the issue when possible? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants