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

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

refactor(struct): improve tree-shaking

parent e8d59b23
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
import { BufferedReadableStream } from "@yume-chan/stream-extra";
import type { StructValue } from "@yume-chan/struct";
import { buffer, Struct, StructEmptyError, u32 } from "@yume-chan/struct";
import { buffer, struct, StructEmptyError, u32 } from "@yume-chan/struct";

import type { Adb } from "../adb.js";

const Version = new Struct({ version: u32 }, { littleEndian: true });
const Version = struct({ version: u32 }, { littleEndian: true });

export const AdbFrameBufferV1 = new Struct(
export const AdbFrameBufferV1 = struct(
    {
        bpp: u32,
        size: u32,
@@ -27,7 +27,7 @@ export const AdbFrameBufferV1 = new Struct(

export type AdbFrameBufferV1 = StructValue<typeof AdbFrameBufferV1>;

export const AdbFrameBufferV2 = new Struct(
export const AdbFrameBufferV2 = struct(
    {
        bpp: u32,
        colorSpace: u32,
+18 −16
Original line number Diff line number Diff line
@@ -2,10 +2,10 @@

import { BufferedReadableStream } from "@yume-chan/stream-extra";
import {
    ExactReadableEndedError,
    Struct,
    encodeUtf8,
    ExactReadableEndedError,
    string,
    struct,
} from "@yume-chan/struct";

import type { Adb, AdbIncomingSocketHandler } from "../adb.js";
@@ -19,7 +19,7 @@ export interface AdbForwardListener {
    remoteName: string;
}

const AdbReverseStringResponse = new Struct(
const AdbReverseStringResponse = struct(
    {
        length: string(4),
        content: string({
@@ -38,7 +38,6 @@ const AdbReverseStringResponse = new Struct(
export class AdbReverseError extends Error {
    constructor(message: string) {
        super(message);
        Object.setPrototypeOf(this, new.target.prototype);
    }
}

@@ -50,7 +49,9 @@ export class AdbReverseNotSupportedError extends AdbReverseError {
    }
}

const AdbReverseErrorResponse = new Struct(AdbReverseStringResponse.fields, {
const AdbReverseErrorResponse = struct(
    /* #__PURE__ */ (() => AdbReverseStringResponse.fields)(),
    {
        littleEndian: true,
        postDeserialize: (value) => {
            // https://issuetracker.google.com/issues/37066218
@@ -62,7 +63,8 @@ const AdbReverseErrorResponse = new Struct(AdbReverseStringResponse.fields, {
                throw new AdbReverseError(value.content);
            }
        },
});
    },
);

// Like `hexToNumber`, it's much faster than first converting `buffer` to a string
function decimalToNumber(buffer: Uint8Array) {
+2 −2
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ import {
    WritableStream,
} from "@yume-chan/stream-extra";
import type { StructValue } from "@yume-chan/struct";
import { Struct, buffer, u32, u8 } from "@yume-chan/struct";
import { buffer, struct, u32, u8 } from "@yume-chan/struct";

import type { Adb, AdbSocket } from "../../../adb.js";
import { AdbFeature } from "../../../features.js";
@@ -32,7 +32,7 @@ export type AdbShellProtocolId =
    (typeof AdbShellProtocolId)[keyof typeof AdbShellProtocolId];

// This packet format is used in both directions.
export const AdbShellProtocolPacket = new Struct(
export const AdbShellProtocolPacket = struct(
    {
        id: u8.as<AdbShellProtocolId>(),
        data: buffer(u32),
+7 −7
Original line number Diff line number Diff line
import type { StructValue } from "@yume-chan/struct";
import { Struct, string, u32 } from "@yume-chan/struct";
import { string, struct, u32 } from "@yume-chan/struct";

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

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

export type AdbSyncEntryResponse = StructValue<typeof AdbSyncEntryResponse>;

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

+2 −2
Original line number Diff line number Diff line
import type { ReadableStream } from "@yume-chan/stream-extra";
import { PushReadableStream } from "@yume-chan/stream-extra";
import type { StructValue } from "@yume-chan/struct";
import { buffer, Struct, u32 } from "@yume-chan/struct";
import { buffer, struct, u32 } from "@yume-chan/struct";

import { AdbSyncRequestId, adbSyncWriteRequest } from "./request.js";
import { adbSyncReadResponses, AdbSyncResponseId } from "./response.js";
import type { AdbSyncSocket } from "./socket.js";

export const AdbSyncDataResponse = new Struct(
export const AdbSyncDataResponse = struct(
    { data: buffer(u32) },
    { littleEndian: true },
);
Loading