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

[Bug]: bundler hard code navigator in mqtt.esm #1905

Closed
EmixamPP opened this issue Jul 19, 2024 · 5 comments
Closed

[Bug]: bundler hard code navigator in mqtt.esm #1905

EmixamPP opened this issue Jul 19, 2024 · 5 comments
Labels

Comments

@EmixamPP
Copy link
Contributor

EmixamPP commented Jul 19, 2024

MQTTjs Version

5.8.0

Broker

mosquitto

Environment

Browser

Description

The variable navigator, used to determine some env, like tkiki.js #1895, is replaced by a content determined at bundle time, namely: P={deviceMemory:8,hardwareConcurrency:8,language:"en-US"} if you look at mqtt/dist/mqtt.esm. And because of that, it is impossible to determine the runtime using this technique (thus the WebSocket lib choice is wrong).

Minimal Reproduction

  1. Open mqtt/dist/mqtt.esm
  2. Search for txiki.js and see that the code from feat: add compatibility with txiki.js #1895, navigator.userAgent==="txiki.js" has been transformed in P.userAgent==="txiki.js"
  3. Search for P= and see that the value assigned is P={deviceMemory:8,hardwareConcurrency:8,language:"en-US"}
  4. Can be confirmed at runtime by adding console.log(P)&& before P.userAgent==="txiki.js"

Debug logs

@EmixamPP EmixamPP added the bug label Jul 19, 2024
@EmixamPP EmixamPP changed the title [Bug]: bundler hard code navigator [Bug]: bundler hard code navigator in mqtt.esm Jul 19, 2024
@robertsLando
Copy link
Member

Would you like to submit a PR to fix this issue?

@EmixamPP
Copy link
Contributor Author

After investigation, the issue is because of

navigator: true, // Needed for WeChat, ref #1789

from

MQTT.js/esbuild.js

Lines 20 to 33 in 11bb9bd

plugins: [
polyfillNode({
polyfills: [
'readable-stream'
],
globals: {
global: false,
__dirname: false,
__filename: false,
buffer: true,
process: true,
navigator: true, // Needed for WeChat, ref #1789
}
}),

If I remove that line, navigator is untouched, and so it works as expected, but I guess I cannot PR that because of #1789.

@robertsLando
Copy link
Member

Wondering if we can have a different solution for wechat, maybe if they add a navigator polifilly before importing mqtt it could work?

@EmixamPP
Copy link
Contributor Author

EmixamPP commented Jul 23, 2024

So if I understand correctly, you're not against a PR that removes out-of-the-box support for WeChat?

Here is what I propose:
I could PR something similar to what is proposed in #1900 or the current timeVariant option, with a new forceNativeWebSocket option, that will by-pass the automatic IS_BROWSER call. (Consequently, I will need to move the import of the modules inside the connect function).
You do not need to extend the isBrowser function anymore, you could just support standard browser page, webworker and NodeJS detection, and if it is not good for the others envs, they can use that option.

Advantage/Disadvantage:
- Yet another option specific to underlying implementation in the constructor
+ Support all the env that support native WS. Even for users that are under Node 21 and do not want ws (?)
-+ Remove backward automatic compatibility for React Native may not be what you want, so maybe I could just remove txiki.js from isBroswer, then you could "refuse" niche runtime/envs (I understand it can be difficult...)

Let me know if you think it is valuable for the project, or any other idea; I would be happy to make a PR that would allow to easily support all runtime/envs that have native WebSocket support.

@robertsLando
Copy link
Member

So if I understand correctly, you're not against a PR that removes out-of-the-box support for WeChat?

Yeah I'm not, they already have to import some polifilly: https://github.com/mqttjs/MQTT.js#wechat-and-ali-mini-program-support

I'm ok with all implementatins that do not break existing things (except the one about wechat I mentioned above)

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