Skip to content

Commit

Permalink
chore: update README and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Jan 25, 2023
1 parent cceaa65 commit 6425e54
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 67 deletions.
10 changes: 10 additions & 0 deletions common/changes/@yume-chan/scrcpy/main_2022-10-18-10-10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yume-chan/scrcpy",
"comment": "Add support for Scrcpy server version 1.25",
"type": "none"
}
],
"packageName": "@yume-chan/scrcpy"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"changes": [
{
"packageName": "@yume-chan/stream-extra",
"comment": "Add the ability to load native Web Streams API implementation from `globalThis`",
"comment": "Change to load native Web Streams API implementation from `globalThis` if available",
"type": "none"
}
],
Expand Down
2 changes: 1 addition & 1 deletion libraries/adb-backend-direct-sockets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

Use [Direct Sockets API](https://wicg.github.io/direct-sockets/) for plugin-free ADB over WiFi connection.

Note: Direct Sockets API is still under development in Chrome and requires extra command line arguments to enable. This package is not intended to be used in production, thus not published to NPM registry.
Note: Direct Sockets API is still under development. Currently it's only available in Chrome and requires extra command line arguments to enable. This package is not ready to be used in production, thus not published to NPM registry.
66 changes: 50 additions & 16 deletions libraries/adb-backend-webusb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,77 @@
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.

- [Note](#note)
- [`pickDevice`](#pickdevice)
- [`fromDevice`](#fromdevice)
- [`connect`](#connect)
- [Use in Node.js](#use-in-nodejs)
- [API](#api)
- [Constructor](#constructor)
- [`isSupported()`](#issupported)
- [`requestDevice`](#requestdevice)
- [`connect`](#connect)

## Note

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

Chrome will treat `localhost` as secure, but if you want to access a dev server running on another machine, you need to add the domain to the allowlist:
Chrome will treat `localhost` as secure, but if you want to access a dev server running on another machine, follow the steps to add the domain name to allowlist:

1. Open `chrome://flags/#unsafely-treat-insecure-origin-as-secure`
2. Add the protocol and domain part of your url (e.g. `http://192.168.0.100:9000`) to the input box
3. Choose `Enable` from the dropdown menu
4. Restart your browser
4. Restart browser

## `pickDevice`
## Use in Node.js

Node.js doesn't support WebUSB API, but you might be able to use this package with the [`usb`](https://www.npmjs.com/package/usb) package (I didn't test this. If you have any results, please open a discussion to share with us).

All static methods will not work, but the constructor only requires an object that's structurally compatible with `USBDevice` interface. The `WebUSBDevice` class in `usb` package should satisfy this requirement.

## API

### Constructor

```ts
static async pickDevice(): Promise<AdbWebBackend | undefined>
public constructor(
device: USBDevice,
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER]
);
```

Request browser to present a list of connected Android devices to let the user choose from.
Create a new instance of `AdbWebBackend` using a `USBDevice` instance you already have.

Returns `undefined` if the user canceled the picker.
`USBDevice` type is from WebUSB API.

## `fromDevice`
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.

### `isSupported()`

```ts
static async fromDevice(device: USBDevice): Promise<AdbWebBackend>
public static isSupported(): boolean;
```

Create an `AdbWebBackend` instance from an exist `USBDevice` instance.
Check if WebUSB API is supported by the browser.

### `requestDevice`

```ts
public static async requestDevice(
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER]
): Promise<AdbWebUsbBackend | undefined>
```

Request access to a connected device from browser. The browser will display a list of devices to the user and let them choose one.

Only available in browsers that support WebUSB API (When `isSupported()` returns `true`).

The `filters` parameter must have `classCode`, `subclassCode` and `protocolCode` fields for selecting the ADB interface. It can also have `vendorId`, `productId` or `serialNumber` fields to limit the displayed device list.

Returns an `AdbWebUsbBackend` instance, or `undefined` if the user cancelled the picker.

## `connect`
### `connect`

```ts
async connect(): Promise<ReadableWritablePair<AdbPacketCore, AdbPacketInit>>
public async connect(): Promise<
ReadableWritablePair<AdbPacketData, AdbPacketInit>
>
```

Connect to a device and create a pair of `AdbPacket` streams.
Claim the device and create a pair of `AdbPacket` streams to the ADB interface.
55 changes: 44 additions & 11 deletions libraries/adb-backend-webusb/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,25 +235,36 @@ export class AdbWebUsbBackendStream
}

export class AdbWebUsbBackend implements AdbBackend {
/**
* Check if WebUSB API is supported by the browser.
*
* @returns `true` if WebUSB is supported by the current browser.
*/
public static isSupported(): boolean {
return !!globalThis.navigator?.usb;
}

public static async getDevices(
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER]
): Promise<AdbWebUsbBackend[]> {
const devices = await window.navigator.usb.getDevices();
return devices.map((device) => new AdbWebUsbBackend(filters, device));
}

/**
* Request access to a connected device from browser.
* The browser will display a list of devices to the user and let them choose one.
*
* Only available in browsers that support WebUSB API (When `isSupported()` returns `true`).
*
* @param filters
* The filters to apply to the device list.
*
* It must have `classCode`, `subclassCode` and `protocolCode` fields for selecting the ADB interface,
* but can also have `vendorId`, `productId` or `serialNumber` fields to limit the displayed device list.
* @returns The `AdbWebUsbBackend` instance if the user selected a device, or `undefined` if the user cancelled the device picker.
*/
public static async requestDevice(
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER]
): Promise<AdbWebUsbBackend | undefined> {
try {
const device = await navigator.usb.requestDevice({
filters,
});
return new AdbWebUsbBackend(filters, device);
return new AdbWebUsbBackend(device, filters);
} catch (e) {
// User cancelled the device picker
if (e instanceof DOMException && e.name === "NotFoundError") {
Expand All @@ -264,6 +275,13 @@ export class AdbWebUsbBackend implements AdbBackend {
}
}

public static async getDevices(
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER]
): Promise<AdbWebUsbBackend[]> {
const devices = await window.navigator.usb.getDevices();
return devices.map((device) => new AdbWebUsbBackend(device, filters));
}

private _filters: AdbDeviceFilter[];
private _device: USBDevice;
public get device() {
Expand All @@ -278,12 +296,27 @@ export class AdbWebUsbBackend implements AdbBackend {
return this._device.productName!;
}

public constructor(filters: AdbDeviceFilter[], device: USBDevice) {
this._filters = filters;
/**
* Create a new instance of `AdbWebBackend` using a `USBDevice` instance you already have.
*
* @param device The `USBDevice` instance you already have.
* @param filters The filters to use when searching for ADB interface. The default value is `[{ classCode: 0xff, subclassCode: 0x42, protocolCode: 0x1 }]`, defined by Google.
*/
public constructor(
device: USBDevice,
filters: AdbDeviceFilter[] = [ADB_DEFAULT_DEVICE_FILTER]
) {
this._device = device;
this._filters = filters;
}

public async connect() {
/**
* Claim the device and create a pair of `AdbPacket` streams to the ADB interface.
* @returns The pair of `AdbPacket` streams.
*/
public async connect(): Promise<
ReadableWritablePair<AdbPacketData, AdbPacketInit>
> {
if (!this._device.opened) {
await this._device.open();
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/adb-backend-ws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Backend for `@yume-chan/adb` using WebSocket API.

Requires WebSockify softwares to bridge the connection between TCP (ADB over Wi-Fi) and WebSocket.

**WARNING:** WebSocket is an unreliable protocol! When send buffer is full, it will throw away any new-coming data, or even cut the connection.
**WARNING:** WebSocket is an unreliable protocol! When send buffer is full, it will throw away any new-coming data, or even cut the connection completely.

Note: This package only demonstrate the possibility. It's not intended to be used in production, thus not published to NPM registry.
15 changes: 14 additions & 1 deletion libraries/adb-credential-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

Generate RSA keys using Web Crypto API ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)) and store them in LocalStorage ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API)).

- [Constructor](#constructor)
- [`generateKey`](#generatekey)
- [`iterateKeys`](#iteratekeys)

## Constructor

```ts
public constructor(localStorageKey = "private-key");
```

Create a new instance of `AdbWebCredentialStore`.

The `localStorageKey` parameter specifies the key to use when reading and writing the private key in LocalStorage.

## `generateKey`

```ts
Expand All @@ -23,4 +34,6 @@ The returned `Uint8Array` is the private key in PKCS #8 format.
*iterateKeys(): Generator<Uint8Array, void, void>
```

Return the stored RSA private key. (This implementation doesn't support storing multiple keys)
Yield the stored RSA private key. `AdbWebCredentialStore` only stores one key, so only one value will be yielded.

This method returns a generator, so `for...of...` loop should be used to read the key.
31 changes: 24 additions & 7 deletions libraries/adb-credential-web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ import {
export default class AdbWebCredentialStore implements AdbCredentialStore {
public readonly localStorageKey: string;

/**
* Create a new instance of `AdbWebCredentialStore`.
*
* @param localStorageKey Specifies the key to use when reading and writing the private key in LocalStorage.
*/
public constructor(localStorageKey = "private-key") {
this.localStorageKey = localStorageKey;
}

public *iterateKeys(): Generator<Uint8Array, void, void> {
const privateKey = window.localStorage.getItem(this.localStorageKey);
if (privateKey) {
yield decodeBase64(privateKey);
}
}

/**
* Generate a RSA private key and store it into LocalStorage.
*
* Calling this method multiple times will overwrite the previous key.
*
* @returns The private key in PKCS #8 format.
*/
public async generateKey(): Promise<Uint8Array> {
const { privateKey: cryptoKey } = await crypto.subtle.generateKey(
{
Expand Down Expand Up @@ -65,4 +70,16 @@ export default class AdbWebCredentialStore implements AdbCredentialStore {

return privateKey;
}

/**
* Yield the stored RSA private key. `AdbWebCredentialStore` only stores one key, so only one value will be yielded.
*
* This method returns a generator, so `for...of...` loop should be used to read the key.
*/
public *iterateKeys(): Generator<Uint8Array, void, void> {
const privateKey = window.localStorage.getItem(this.localStorageKey);
if (privateKey) {
yield decodeBase64(privateKey);
}
}
}
15 changes: 15 additions & 0 deletions libraries/android-bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@
Wrappers for Android built-in executables.

Currently it's bound to `@yume-chan/adb`, maybe one day it can be used with other executors.

## Implemented

* `bugreport`
* `bugreportz`
* `settings`
* Demo mode
* `logcat`

## Planned

- [ ] `bu` ([#354](https://github.com/yume-chan/ya-webadb/issues/354))
- [ ] `input` ([#363](https://github.com/yume-chan/ya-webadb/issues/363))
- [ ] `am` ([#366](https://github.com/yume-chan/ya-webadb/issues/366))
- [ ] `pm` ([#373](https://github.com/yume-chan/ya-webadb/issues/373))
10 changes: 7 additions & 3 deletions libraries/b-tree/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# @yume-chan/b-tree

**WIP**: A B-Tree implmentation in TypeScript.
A simple B-Tree set implementation in TypeScript.

- [x] insert
- [ ] remove
- [x] custom order
- [x] add
- [x] delete
- [x] has
- [x] clear
- [x] iterate
6 changes: 3 additions & 3 deletions libraries/scrcpy/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"comments": {
"none": [
{
"comment": "Add support for new option of 1.24"
"comment": "Add support for Scrcpy server version 1.24"
}
]
}
Expand All @@ -68,7 +68,7 @@
"comments": {
"none": [
{
"comment": "Add support for 1.23"
"comment": "Add support for Scrcpy server version 1.23"
}
]
}
Expand All @@ -86,7 +86,7 @@
"comments": {
"none": [
{
"comment": "Add support for 1.22 new server options"
"comment": "Add support for Scrcpy server version 1.22"
},
{
"comment": "Update to use Web Streams API"
Expand Down
7 changes: 3 additions & 4 deletions libraries/scrcpy/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ Sat, 30 Apr 2022 14:05:48 GMT

### Updates

- Add support for new option of 1.24
- Add support for Scrcpy server version 1.24

## 0.0.13
Thu, 28 Apr 2022 01:23:53 GMT

### Updates

- Add support for 1.23
- Add support for Scrcpy server version 1.23

## 0.0.12
Sun, 03 Apr 2022 11:18:47 GMT
Expand All @@ -51,7 +51,7 @@ Sun, 03 Apr 2022 10:54:15 GMT

### Updates

- Add support for 1.22 new server options
- Add support for Scrcpy server version 1.22
- Update to use Web Streams API
- Improve compatibility with Node.js 12 ESM format
- Workaround a issue with server crash on Samsung ROM (https://github.com/Genymobile/scrcpy/issues/2841)
Expand All @@ -62,4 +62,3 @@ Sun, 03 Apr 2022 10:54:15 GMT
Sun, 09 Jan 2022 15:52:20 GMT

_Initial release_

Loading

0 comments on commit 6425e54

Please sign in to comment.