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

Breaks any other libraries that rely on window #17

Open
ChuckJonas opened this issue Apr 30, 2019 · 10 comments
Open

Breaks any other libraries that rely on window #17

ChuckJonas opened this issue Apr 30, 2019 · 10 comments

Comments

@ChuckJonas
Copy link

Pretty much any other library that does a if(window) check will no longer operate as expected if you install this.

@gregw
Copy link
Member

gregw commented May 1, 2019

please explain a bit more?

@ChuckJonas
Copy link
Author

ChuckJonas commented May 1, 2019

this library "patches" cometd by adding window to the global scope...

There might be another library (@salesforce/core or jsforce for example), that checks window to determine if it is running in a browser or not.

After patching, this library will think it's operating in a browser, and likely break.

I know this because that's exactly what happened when I required this package before running this code from @salesforce/core

const connection: Connection = await Connection.create({
   authInfo: await AuthInfo.create({ username: 'john@example.com'})
});
let org = await Org.create({connection});
org.refreshAuth();

@ChuckJonas
Copy link
Author

I guess you could say jsforce is equally to blame here. https://github.com/jsforce/jsforce/blob/master/lib/transport.js#L29

ChuckJonas added a commit to ChuckJonas/jsforce that referenced this issue May 1, 2019
fixes
>TypeError: Cannot read property 'host' of undefined`.

if somehow `window` gets created on the global scope.

For example:  See cometd/cometd-nodejs-client#17
@sbordet
Copy link
Member

sbordet commented May 8, 2019

@ChuckJonas so what suggestions do you have to avoid this?

cometd.js needs a bunch of functions that are provided by the window object on browsers, and by the global object in Node.
In addition when running on Node I need to "fake" other functions (such as XMLHttpRequest) - this is currently done by adapt().

I can pollute the global object in Node with the functions that I need (apparently this is what others are doing, e.g. mocha).

Alternatively I can pollute the window (in browsers) and the global (in Node) object with a single property object, say cometdExternals, and then modify cometd.js to assume that a cometdExternals object exist rather than relying on window.
How does that sound?

@ChuckJonas
Copy link
Author

I think that makes...

Although I'm wondering if you're going to change the cometd core anyways, why not just make the library itself isomorphic?

Seems like setTimeout, console, and clearTimeout could be resolved simply by first checking if they exist and then using window.setTimeout, if needed.

You could do the same with XMLHttpRequest by conditionally requiring it from this pkg (or maybe even just use this npm package.

@sbordet
Copy link
Member

sbordet commented May 8, 2019

I need some adapter code anyways for 2 reasons:

  1. Node's console does not have a debug() function, while browsers do.
  2. I really need XMLHttpRequest as provided, I cannot put a require() in the core cometd.js code.

Especially for the latter, again I can pollute the global object, or I can assume there exist a cometdExternals object.
When used in a browser, cometdExternals = window, otherwise I will make a cometdExternals in adapt() and only pollute the global object with it: global.cometdExternals = ....

When supporting WebSocket on the client side I would just need to do cometdExternals.WebSocket = require('ws');.

Makes sense?

@ChuckJonas
Copy link
Author

ChuckJonas commented May 8, 2019

I believe you could do something like this:

if(isNode){
   require('XMLHttpRequest');
}

Alternatively, tools like browserfy and webpack allow you to require package in browser modules.

But I understand the hesitation to muddy the core implementation.

Seems like the solution you've outline above should work...

Thanks for being responsive!

@sbordet
Copy link
Member

sbordet commented May 8, 2019

@ChuckJonas yeah, there are users that don't use CommonJS but AMD, others that just rely on vanilla JavaScript 4, etc. so I can't really muddy the core implementation.

I'm experimenting with my outlined solution above and seems to be working.

@sbordet
Copy link
Member

sbordet commented Jul 8, 2019

Turns out this fix breaks other libraries.

@ChuckJonas
Copy link
Author

as noted in #18

sbordet added a commit that referenced this issue Jul 11, 2019
Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
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

No branches or pull requests

3 participants