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

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

feat(struct): allow structs to be used as fields directly (#741)

parent d3019ce7
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
---
"@yume-chan/struct": major
---

Refactor struct package to allow `struct`s to be used as `field`
+1 −25
Original line number Diff line number Diff line
@@ -19,8 +19,7 @@ import {
    ReadableStream,
    pipeFrom,
} from "@yume-chan/stream-extra";
import type { ExactReadable } from "@yume-chan/struct";
import { EmptyUint8Array } from "@yume-chan/struct";
import { EmptyUint8Array, Uint8ArrayExactReadable } from "@yume-chan/struct";

import { DeviceBusyError as _DeviceBusyError } from "./error.js";
import type { UsbInterfaceFilter, UsbInterfaceIdentifier } from "./utils.js";
@@ -52,29 +51,6 @@ export function mergeDefaultAdbInterfaceFilter(
    }
}

class Uint8ArrayExactReadable implements ExactReadable {
    #data: Uint8Array;
    #position: number;

    get position() {
        return this.#position;
    }

    constructor(data: Uint8Array) {
        this.#data = data;
        this.#position = 0;
    }

    readExactly(length: number): Uint8Array {
        const result = this.#data.subarray(
            this.#position,
            this.#position + length,
        );
        this.#position += length;
        return result;
    }
}

export class AdbDaemonWebUsbConnection
    implements ReadableWritablePair<AdbPacketData, Consumable<AdbPacketInit>>
{
+5 −4
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import { BufferedReadableStream } from "@yume-chan/stream-extra";
import {
    encodeUtf8,
    ExactReadableEndedError,
    extend,
    string,
    struct,
} from "@yume-chan/struct";
@@ -49,11 +50,11 @@ export class AdbReverseNotSupportedError extends AdbReverseError {
    }
}

const AdbReverseErrorResponse = struct(
    /* #__PURE__ */ (() => AdbReverseStringResponse.fields)(),
const AdbReverseErrorResponse = extend(
    AdbReverseStringResponse,
    {},
    {
        littleEndian: true,
        postDeserialize: (value) => {
        postDeserialize(value) {
            // https://issuetracker.google.com/issues/37066218
            // ADB on Android <9 can't create reverse tunnels when connected wirelessly (ADB over Wi-Fi),
            // and returns this confusing "more than one device/emulator" error.
+7 −17
Original line number Diff line number Diff line
import type { StructValue } from "@yume-chan/struct";
import { string, struct, u32 } from "@yume-chan/struct";
import { extend, string, u32 } from "@yume-chan/struct";

import { AdbSyncRequestId, adbSyncWriteRequest } from "./request.js";
import { AdbSyncResponseId, adbSyncReadResponses } from "./response.js";
@@ -15,25 +15,15 @@ export interface AdbSyncEntry extends AdbSyncStat {
    name: string;
}

export const AdbSyncEntryResponse = /* #__PURE__ */ (() =>
    struct(
        {
            ...AdbSyncLstatResponse.fields,
export const AdbSyncEntryResponse = extend(AdbSyncLstatResponse, {
    name: string(u32),
        },
        { littleEndian: true, extra: AdbSyncLstatResponse.extra },
    ))();
});

export type AdbSyncEntryResponse = StructValue<typeof AdbSyncEntryResponse>;

export const AdbSyncEntry2Response = /* #__PURE__ */ (() =>
    struct(
        {
            ...AdbSyncStatResponse.fields,
export const AdbSyncEntry2Response = extend(AdbSyncStatResponse, {
    name: string(u32),
        },
        { littleEndian: true, extra: AdbSyncStatResponse.extra },
    ))();
});

export type AdbSyncEntry2Response = StructValue<typeof AdbSyncEntry2Response>;

+3 −3
Original line number Diff line number Diff line
import { getUint32LittleEndian } from "@yume-chan/no-data-view";
import type { AsyncExactReadable, StructLike } from "@yume-chan/struct";
import type { AsyncExactReadable, StructDeserializer } from "@yume-chan/struct";
import { decodeUtf8, string, struct, u32 } from "@yume-chan/struct";

function encodeAsciiUnchecked(value: string): Uint8Array {
@@ -49,7 +49,7 @@ export const AdbSyncFailResponse = struct(
export async function adbSyncReadResponse<T>(
    stream: AsyncExactReadable,
    id: number | string,
    type: StructLike<T>,
    type: StructDeserializer<T>,
): Promise<T> {
    if (typeof id === "string") {
        id = adbSyncEncodeId(id);
@@ -72,7 +72,7 @@ export async function adbSyncReadResponse<T>(
export async function* adbSyncReadResponses<T>(
    stream: AsyncExactReadable,
    id: number | string,
    type: StructLike<T>,
    type: StructDeserializer<T>,
): AsyncGenerator<T, void, void> {
    if (typeof id === "string") {
        id = adbSyncEncodeId(id);
Loading