mqttjs / MQTT.js

The MQTT client for Node.js and the browser
Other
8.52k stars 1.41k forks source link

[Bug]: WeChat mini `globalThis` is not defined in browser bundle #1797

Closed Diven-xinhai closed 2 months ago

Diven-xinhai commented 6 months ago

MQTTjs Version

5.3.6

Broker

mqtt

Environment

NodeJS

Description

The latest version 5.3.6 reported an error in the wechat mini program, the error message is as follows: TypeError: Cannot read property 'AbortController' of undefined

Minimal Reproduction

image image

Debug logs

.

Diven-xinhai commented 6 months ago

image

robertsLando commented 6 months ago

Could you try to add this to the mqttjs bundle?

  globalThis = globalThis || {};
Diven-xinhai commented 6 months ago

Could you try to add this to the mqttjs bundle?

  globalThis = globalThis || {};

Sorry, I don't quite understand what you mean. Could you please specify where or in which file I should add?

robertsLando commented 6 months ago

@Diven-xinhai node_modules/mqtt/dist/mqtt.esm.js

Alternatively you could also try to add that in a script tag in your index.html file

Diven-xinhai commented 6 months ago

@Diven-xinhai node_modules/mqtt/dist/mqtt.esm.js

Alternatively you could also try to add that in a script tag in your index.html file

I tried to add globalThis = globalThis || {} in index.html or mqtt.esm.js; But it didn't work. It's the same error

robertsLando commented 6 months ago

The problem is that for some reason when you are running this in wechat mini globalThis is not defined (it should be in all browsers BTW) so you should try fixing it within your bundle someway because I don't have a way to test this out

timzaak commented 3 months ago

MQTTjs Version 5.3.6

TypeError: Cannot set property 'AbortSignal' of undefined

Another issue about this is https://github.com/NervJS/taro/issues/15580

robertsLando commented 3 months ago

@timzaak I don't use wechat and I also have no way to test this so please feel free to open a PR to fix this issue.

timzaak commented 3 months ago

@timzaak I don't use wechat and I also have no way to test this so please feel free to open a PR to fix this issue.

@robertsLando I solved this by import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only' before import mqtt

but worker-timers uses new Blob(), Wechat miniprogram do not support Blob, it should be replaced by ArrayBuffer.

https://github.com/chrisguttandin/worker-timers/blob/8f73b936a20073a809b180d4557c657f26e4e7fa/src/factories/load-or-return-broker.ts#L9

image

robertsLando commented 3 months ago

@timzaak You can disable using worker timer with timerVariant option

timzaak commented 2 months ago

@timzaak You can disable using worker timer with timerVariant option

@robertsLando That works. Thanks

shushenghong commented 1 month ago

引入了import "abortcontroller-polyfill/dist/abortcontroller-polyfill-only"; 还是报错

TypeError: Cannot destructure property 'AbortController' of '(intermediate value)(intermediate value)(intermediate value)' as it is undefined.

timzaak commented 1 month ago

在项目最开始初始化的地方引入。 app.ts or main.ts

shushenghong commented 1 month ago

你用哪个版本的mqtt.js,我这试了还不行,引入了 abortcontroller-polyfill/dist/abortcontroller-polyfill-only打包会报错, SyntaxError: Unexpected token ; onabort;

timzaak commented 1 month ago

"mqtt": "5.7.3"

robertsLando commented 1 month ago

Starting from next MQTTjs version you will need another polifilly in order to make it work with WeChat. See docs