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

Unverified Commit 57917317 authored by Simon Chan's avatar Simon Chan
Browse files

refactor(adb): simplify sending packet flow

parent 190b24ad
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -111,7 +111,8 @@ export class Adb implements Closeable {
        async function sendPacket(init: AdbPacketData) {
            // Always send checksum in auth steps
            // Because we don't know if the device needs it or not.
            await writer.write(calculateChecksum(init));
            (init as AdbPacketInit).checksum = calculateChecksum(init.payload);
            await writer.write(init as AdbPacketInit);
        }

        let banner: string;
+5 −3
Original line number Diff line number Diff line
@@ -150,9 +150,11 @@ export interface AdbSyncPushOptions extends AdbSyncPushV2Options {
export function adbSyncPush(options: AdbSyncPushOptions) {
    if (options.v2) {
        return adbSyncPushV2(options);
    } else if (options.dryRun) {
    }

    if (options.dryRun) {
        throw new Error("dryRun is not supported in v1");
    } else {
        return adbSyncPushV1(options);
    }

    return adbSyncPushV1(options);
}
+3 −17
Original line number Diff line number Diff line
@@ -43,22 +43,10 @@ export type AdbPacketData = Omit<
    "checksum" | "magic"
>;

// All fields except `magic`, which can be calculated in `AdbPacketSerializeStream`
export type AdbPacketInit = Omit<(typeof AdbPacket)["TInit"], "magic">;
export type AdbPacketInit = (typeof AdbPacket)["TInit"];

export function calculateChecksum(payload: Uint8Array): number;
export function calculateChecksum(init: AdbPacketData): AdbPacketInit;
export function calculateChecksum(
    payload: Uint8Array | AdbPacketData
): number | AdbPacketInit {
    if (payload instanceof Uint8Array) {
export function calculateChecksum(payload: Uint8Array): number {
    return payload.reduce((result, item) => result + item, 0);
    } else {
        (payload as AdbPacketInit).checksum = calculateChecksum(
            payload.payload
        );
        return payload as AdbPacketInit;
    }
}

export class AdbPacketSerializeStream extends TransformStream<
@@ -69,8 +57,6 @@ export class AdbPacketSerializeStream extends TransformStream<
        super({
            transform: (init, controller) => {
                // This syntax is ugly, but I don't want to create a new object.
                (init as unknown as AdbPacketHeaderInit).magic =
                    init.command ^ 0xffffffff;
                (init as unknown as AdbPacketHeaderInit).payloadLength =
                    init.payload.byteLength;

+14 −33
Original line number Diff line number Diff line
@@ -270,49 +270,30 @@ export class AdbPacketDispatcher implements Closeable {
        return controller.socket;
    }

    public sendPacket(packet: AdbPacketInit): Promise<void>;
    public sendPacket(
    public async sendPacket(
        command: AdbCommand,
        arg0: number,
        arg1: number,
        payload?: string | Uint8Array
    ): Promise<void>;
    public async sendPacket(
        packetOrCommand: AdbPacketInit | AdbCommand,
        arg0?: number,
        arg1?: number,
        payload: string | Uint8Array = EMPTY_UINT8_ARRAY
    ): Promise<void> {
        let init: AdbPacketData;
        if (arg0 === undefined) {
            init = packetOrCommand as AdbPacketInit;
        } else {
        if (typeof payload === "string") {
            payload = encodeUtf8(payload);
        }

            init = {
                command: packetOrCommand as AdbCommand,
                arg0: arg0,
                arg1: arg1 as number,
                payload,
            };
        }

        if (
            init.payload &&
            init.payload.byteLength > this.options.maxPayloadSize
        ) {
        if (payload.byteLength > this.options.maxPayloadSize) {
            throw new Error("payload too large");
        }

        if (this.options.calculateChecksum) {
            calculateChecksum(init);
        } else {
            (init as AdbPacketInit).checksum = 0;
        }

        await this._writer.write(init as AdbPacketInit);
        await this._writer.write({
            command,
            arg0,
            arg1,
            payload,
            checksum: this.options.calculateChecksum
                ? calculateChecksum(payload)
                : 0,
            magic: command ^ 0xffffffff,
        });
    }

    public async close() {