Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d48775cf authored by Simon Chan's avatar Simon Chan
Browse files

feat(webusb): detect errors from `usb` package

parent e71ae981
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

Backend for `@yume-chan/adb` using WebUSB ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/USB), [Spec](https://wicg.github.io/webusb)) API.

-   [Requirements](#requirements)
-   [Use in browser](#use-in-browser)
-   [Use in Node.js](#use-in-nodejs)
-   [`AdbWebUsbBackend`](#adbwebusbbackend)
    -   [constructor](#constructor)
@@ -13,7 +13,11 @@ Backend for `@yume-chan/adb` using WebUSB ([MDN](https://developer.mozilla.org/e
    -   [`requestDevice`](#requestdevice)
    -   [`getDevices`](#getdevices)

## Requirements
## Use in browser

| Chrome | Edge | Firefox | Internet Explorer | Safari |
| ------ | ---- | ------- | ----------------- | ------ |
| 61     | 79   | No      | No                | No     |

WebUSB API requires [secure context](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts) (HTTPS).

@@ -26,9 +30,13 @@ Chrome will treat `localhost` as secure, but if you want to access a dev server

## Use in Node.js

Node.js doesn't have native support for WebUSB API, but the [`usb`](https://www.npmjs.com/package/usb) NPM package has a `webusb` export that provides a WebUSB compatible API.
| Node.js | `usb` NPM Package |
| ------- | ----------------- |
| 10.5    | 2.9.0             |

Node.js doesn't have native support for WebUSB API, but the [`usb`](https://www.npmjs.com/package/usb) NPM package provides a WebUSB compatible API.

The constructors of `AdbWebUsbBackend`, `AdbWebUsbBackendManager` and `AdbWebUsbBackendWatcher` all have a `usb` parameter that can be used to specify the WebUSB API implementation.
To use a custom WebUSB API implementation, pass it to the constructor of `AdbWebUsbBackend`, `AdbWebUsbBackendManager` and `AdbWebUsbBackendWatcher` via the `usb` parameter.

## `AdbWebUsbBackend`

@@ -42,9 +50,9 @@ public constructor(
);
```

Create a new instance of `AdbWebBackend` using a `USBDevice` instance you already have.
Create a new instance of `AdbWebBackend` using a specified `USBDevice` instance.

`USBDevice` type is from WebUSB API.
`USBDevice` and `USB` types are from WebUSB API.

The `filters` parameter specifies the `classCode`, `subclassCode` and `protocolCode` to use when searching for ADB interface. The default value is `[{ classCode: 0xff, subclassCode: 0x42, protocolCode: 0x1 }]`, defined by Google.

@@ -68,7 +76,7 @@ A helper class that wraps the WebUSB API.
public static readonly BROWSER: AdbWebUsbBackendManager | undefined;
```

Gets the instance of AdbWebUsbBackendManager using browser WebUSB implementation.
Gets the instance of `AdbWebUsbBackendManager` using browser WebUSB implementation.

May be `undefined` if the browser does not support WebUSB.

@@ -104,4 +112,5 @@ public async getDevices(
```

Get all connected and authenticated devices.

This is a convince method for `usb.getDevices()`.
+2 −2
Original line number Diff line number Diff line
@@ -249,9 +249,9 @@ export class AdbWebUsbBackend implements AdbBackend {
    }

    /**
     * Create a new instance of `AdbWebBackend` using a `USBDevice` instance you already have.
     * Create a new instance of `AdbWebBackend` using a specified `USBDevice` instance
     *
     * @param device The `USBDevice` instance you already have.
     * @param device The `USBDevice` instance obtained elsewhere.
     * @param filters The filters to use when searching for ADB interface. Defaults to {@link ADB_DEFAULT_DEVICE_FILTER}.
     */
    public constructor(
+8 −4
Original line number Diff line number Diff line
@@ -49,11 +49,14 @@ export class AdbWebUsbBackendManager {
            });
            return new AdbWebUsbBackend(device, filters, this._usb);
        } catch (e) {
            // User cancelled the device picker
            // TODO: investigate what error the `usb` NPM package will throw
            // No device selected
            // This check is compatible with both Browser implementation
            // and `usb` NPM package from version 2.9.0
            // https://github.com/node-usb/node-usb/issues/573
            if (
                typeof DOMException !== "undefined" &&
                e instanceof DOMException &&
                typeof e === "object" &&
                e !== null &&
                "name" in e &&
                e.name === "NotFoundError"
            ) {
                return undefined;
@@ -62,6 +65,7 @@ export class AdbWebUsbBackendManager {
            throw e;
        }
    }

    /**
     * Get all connected and authenticated devices.
     * This is a convince method for `usb.getDevices()`.