foxxyz / loupedeck

Node.js API for Loupedeck Controllers
MIT License
87 stars 10 forks source link

How to terminate a connection? #31

Closed elementalTIMING closed 2 months ago

elementalTIMING commented 6 months ago

Hi,

today I tried to programatically close the connection to the Loupedeck CT. So I used:

loupedeck.close();

Unfortunately this results in:

TypeError: Failed to execute 'close' on 'SerialPort': Cannot cancel a locked stream
    at web-serial.js:87:25
    at Generator.next (<anonymous>)

Do you have any idea or fix for that?

Thx and cheers, Lars

foxxyz commented 6 months ago

I believe WebSerial connections are managed by your browser, so you can't manually close those.

What are you trying to accomplish?

elementalTIMING commented 6 months ago

I believe WebSerial connections are managed by your browser, so you can't manually close those.

What are you trying to accomplish?

Currently, I'm trying to bring your software into an Angular project. Here I would like to allow (via a button) to turn on/off the "loupedeck interface". So it would be nice to open/close the connection.

BTW: I also discovered an issue with the serial connection. Everything works fine in the original Google Chrome. But not for any other browsers, like Brave (Chromium) or Firefox:

if (typeof navigator !== 'undefined' && navigator.serial) {
    -> CHROME
    SerialConnection = require('./connections/web-serial');
} else {
    -> OTHER BROWSERS
    // this is used for all browsrs other than Chrome but fails
    // SerialConnection = require('./connections/serial');
    // WSConnection = require('./connections/ws');
}

here 'OTHER BROWSERS' fail with errors. You can test it easily by yourself if you try to start the 'simple' demo, e.g. in the Brave browser.

Julusian commented 6 months ago

Generally only Chromium and its derivatives support webserial https://caniuse.com/web-serial

And while brave is chromium based, it explicitly disables webserial https://github.com/brave/brave-browser/wiki/Deviations-from-Chromium-(features-we-disable-or-remove)#what-chromium-features-are-removed-for-privacysecurity-reasons

elementalTIMING commented 6 months ago

Generally only Chromium and its derivatives support webserial https://caniuse.com/web-serial

And while brave is chromium based, it explicitly disables webserial https://github.com/brave/brave-browser/wiki/Deviations-from-Chromium-(features-we-disable-or-remove)#what-chromium-features-are-removed-for-privacysecurity-reasons

yes, I know this. But from what I understood you have build in a fallback, didn't you? And this fallback doesn't work. I found that you used stream and serialport, but there are some polyfills available to be able to use this in a browser. The main problem seems to come from

class MagicByteLengthParser extends Transform

Here it is not possible to extend Transform. So I'm wondering if you could provide a version that works on all major browsers, too.

Julusian commented 6 months ago

hmm.. that fallback to me looks like that it is the nodejs mode, not for other browsers

elementalTIMING commented 6 months ago

hmm.. that fallback to me looks like that it is the nodejs mode, not for other browsers

True, but wouldn't it be a good idea/possible/better :-) to make the code more general to make it working in nodejs but also for browsers?

foxxyz commented 6 months ago

thanks @Julusian

@elementalPRESS I think there's a misunderstanding.

Unlike a general purpose programming environment (I.E. NodeJS), browsers are generally sandboxed execution environments that only allow access to low-level protocols that they create/enable API's for. This means that it is not possible to access serial ports in browsers that do not support or implement WebSerial, and there is no way to get around that.

foxxyz commented 6 months ago

However, getting back on topic - I do think that .close() should execute correctly, even in browser, and if the port can't be closed by the application it's likely a bug on our side.

Reading this issue about closing a WebSerial connection makes me think we might need to release a lock somewhere before closing the connection. I will look into it.

Thanks for reporting!

elementalTIMING commented 6 months ago

thanks @Julusian

@elementalPRESS I think there's a misunderstanding.

Unlike a general purpose programming environment (I.E. NodeJS), browsers are generally sandboxed execution environments that only allow access to low-level protocols that they create/enable API's for. This means that it is not possible to access serial ports in browsers that do not support or implement WebSerial, and there is no way to get around that.

Well, I guess it is possible using the polyfill versions of 'stream' and 'serial'.

web-serial-polyfill
@stardazed/streams-polyfill

I was already able to include it, but stopped because I had to change your package to a module. This migration failed doe to my missing knowledge of nodejs.