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 detection of root object #673

Merged
merged 1 commit into from
Sep 4, 2015

Conversation

targos
Copy link
Contributor

@targos targos commented Jun 5, 2015

self is compatible with Window and Web Workers (see this article).
Checking for this is breaking browserify builds that run in Workers.

Another solution is to simply do var root = self;. What is the reason to have a fallback to this ?

@defunctzombie
Copy link
Contributor

has this been tested in non-worker environment?

@targos
Copy link
Contributor Author

targos commented Jul 10, 2015

Yes, I'm using it in usual window environment as well.
CI seems to be run in browsers and passed without any error : https://travis-ci.org/visionmedia/superagent/jobs/65554746

@oleics
Copy link

oleics commented Jul 12, 2015

The PR will not work for code in browserify-bundles, as somehow self is not window inside a module.

Following should cover pretty much all environments:

var root;
if(typeof window !== 'undefined') root = window;      // Browser
else if(typeof self !== 'undefined') root = self;     // Web Worker
else if(typeof global !== 'undefined') root = global; // Web Worker, but in a module (browserify)
else root = this;                                     // everything else

@oleics
Copy link

oleics commented Jul 12, 2015

Just checked the issue again:
The reason that using superagent in browserify-bundles running in web-worker-env fails, is not that self is undefined. Instead, this is defined. So, this does the trick:

var root;
if(typeof window !== 'undefined') root = window;      // Browser
else if(typeof self !== 'undefined') root = self;     // Web Worker
else root = this;                                     // everything else

Could be rewritten as a one-liner, but I would not do so for readability.

@targos
Copy link
Contributor Author

targos commented Jul 12, 2015

@oleics I cannot reproduce a situation where self is not equal to window with browserify. When does that happen ?

@oleics
Copy link

oleics commented Jul 12, 2015

@targos

In web-worker-env, self is not window, as code running in a web-worker does not have access to window. self in web-workers is - simply put - a new context. See https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

But that is not the problem here. Let me refer to this:

var root = 'undefined' == typeof self
  ? this
  : self;

this might not always be window, especially when it comes to module-systems like nodejs/browserify or whatever-module-system. It is unsave to rely on this beeing always window. I think it is better to be explicit here and test for window first, then self, and then (as the last resort) this.

@targos
Copy link
Contributor Author

targos commented Jul 12, 2015

@oleics
I agree with your explanation, and my PR is here exactly to fix the fact that this is defined in browserify builds but not with the expected value.
I can add a check for window first if that feels safer, but it should not be needed because self is a reference to window in the browser and to the worker global scope in web workers (see link in the OP).

@oleics
Copy link

oleics commented Jul 12, 2015

@targos
The check for self alone makes me a little nervous, as we all do var self = this. But this should really only be the last resort. I would throw an error if neither window nor self is defined.

@targos
Copy link
Contributor Author

targos commented Jul 12, 2015

I pushed a change to safely check for window first, PTAL

@oleics
Copy link

oleics commented Jul 12, 2015

@targos Thanks.
@defunctzombie What do you think, merge?

@defunctzombie
Copy link
Contributor

please follow existing coding style (use braces)

What is the consensus around the changeset now? Is this is correct version?

We need to check for 'self' before 'this' because 'this' is defined
with the wrong value in browserify builds that run in a Web Worker
@targos
Copy link
Contributor Author

targos commented Jul 16, 2015

@defunctzombie braces added.
This is a correct, more conservative version than the first one. I think both behave exactly the same, but this one has the advantage to be explicit about about browser and web worker compatibility.

@taish
Copy link

taish commented Aug 9, 2015

👍 👍
I am facing same problem using webpack & webworker.

@mohsen1
Copy link

mohsen1 commented Aug 29, 2015

+1
Please merge this. This module needs Web Worker support. It's deep in my dependency chain, I can't just use a fork.
Thanks!

defunctzombie added a commit that referenced this pull request Sep 4, 2015
client: Fix detection of root object for webworkers
@defunctzombie defunctzombie merged commit a40919c into ladjs:master Sep 4, 2015
@oleics
Copy link

oleics commented Sep 4, 2015

thanks a lot, just in time.

@targos targos deleted the fix-browserify-worker branch September 4, 2015 21:51
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.

5 participants