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

Unverified Commit 3a6fd9ca authored by Simon Chan's avatar Simon Chan Committed by GitHub
Browse files

feat(adb): subprocess API v3 (#786)

parent 927d5068
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -144,14 +144,14 @@ export class AdbScrcpyClient<TOptions extends AdbScrcpyOptions<object>> {
            ];

            if (options.spawner) {
                process = await options.spawner.spawn(args);
                process = await options.spawner(args);
            } else {
                process = await adb.subprocess.noneProtocol.spawn(args);
            }

            const output = process.output
                .pipeThrough(new TextDecoderStream())
                .pipeThrough(new SplitStringStream("\n"));
                .pipeThrough(new SplitStringStream("\n", { trimEnd: true }));

            // Must read all streams, otherwise the whole connection will be blocked.
            const lines: string[] = [];
+11 −5
Original line number Diff line number Diff line
@@ -125,10 +125,12 @@ export class Adb implements Closeable {
            .pipeThrough(new ConcatStringStream());
    }

    getProp(key: string): Promise<string> {
        return this.subprocess.noneProtocol
            .spawnWaitText(["getprop", key])
            .then((output) => output.trim());
    async getProp(key: string): Promise<string> {
        const output = await this.subprocess.noneProtocol
            .spawn(["getprop", key])
            .wait()
            .toString();
        return output.trim();
    }

    rm(
@@ -154,7 +156,11 @@ export class Adb implements Closeable {
        // https://android.googlesource.com/platform/packages/modules/adb/+/1a0fb8846d4e6b671c8aa7f137a8c21d7b248716/client/adb_install.cpp#984
        args.push("</dev/null");

        return this.subprocess.noneProtocol.spawnWaitText(args);
        return this.subprocess.noneProtocol
            .spawn(args)
            .wait()
            .toString()
            .then((output) => output.trim());
    }

    async sync(): Promise<AdbSync> {
+5 −2
Original line number Diff line number Diff line
@@ -36,7 +36,10 @@ export class AdbPower extends AdbServiceBase {
    }

    powerOff(): Promise<string> {
        return this.adb.subprocess.noneProtocol.spawnWaitText(["reboot", "-p"]);
        return this.adb.subprocess.noneProtocol
            .spawn(["reboot", "-p"])
            .wait()
            .toString();
    }

    powerButton(longPress = false): Promise<string> {
@@ -46,7 +49,7 @@ export class AdbPower extends AdbServiceBase {
        }
        args.push("POWER");

        return this.adb.subprocess.noneProtocol.spawnWaitText(args);
        return this.adb.subprocess.noneProtocol.spawn(args).wait().toString();
    }

    /**
+1 −0
Original line number Diff line number Diff line
export * from "./none/index.js";
export * from "./service.js";
export * from "./shell/index.js";
export * from "./types.js";
export * from "./utils.js";
+17 −16
Original line number Diff line number Diff line
@@ -2,16 +2,19 @@ import type { Adb } from "../../../adb.js";

import { AdbNoneProtocolProcessImpl } from "./process.js";
import { AdbNoneProtocolPtyProcess } from "./pty.js";
import { AdbNoneProtocolSpawner } from "./spawner.js";
import { adbNoneProtocolSpawner } from "./spawner.js";

export class AdbNoneProtocolSubprocessService extends AdbNoneProtocolSpawner {
export class AdbNoneProtocolSubprocessService {
    readonly #adb: Adb;
    get adb(): Adb {
        return this.#adb;
    }

    constructor(adb: Adb) {
        super(async (command, signal) => {
        this.#adb = adb;
    }

    spawn = adbNoneProtocolSpawner(async (command, signal) => {
        // `shell,raw:${command}` also triggers raw mode,
        // But is not supported on Android version <7.
        const socket = await this.#adb.createSocket(
@@ -25,8 +28,6 @@ export class AdbNoneProtocolSubprocessService extends AdbNoneProtocolSpawner {

        return new AdbNoneProtocolProcessImpl(socket, signal);
    });
        this.#adb = adb;
    }

    async pty(
        command?: string | readonly string[],
Loading