@yume-chan/adb 0.0.24 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/CHANGELOG.md +42 -1
  2. package/README.md +4 -1
  3. package/esm/adb.d.ts +13 -7
  4. package/esm/adb.d.ts.map +1 -1
  5. package/esm/adb.js +3 -0
  6. package/esm/adb.js.map +1 -1
  7. package/esm/banner.d.ts +7 -6
  8. package/esm/banner.d.ts.map +1 -1
  9. package/esm/banner.js +6 -7
  10. package/esm/banner.js.map +1 -1
  11. package/esm/commands/framebuffer.d.ts +63 -34
  12. package/esm/commands/framebuffer.d.ts.map +1 -1
  13. package/esm/commands/framebuffer.js +35 -33
  14. package/esm/commands/framebuffer.js.map +1 -1
  15. package/esm/commands/reverse.d.ts +12 -12
  16. package/esm/commands/reverse.d.ts.map +1 -1
  17. package/esm/commands/reverse.js +39 -30
  18. package/esm/commands/reverse.js.map +1 -1
  19. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  20. package/esm/commands/subprocess/protocols/none.js +3 -5
  21. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  22. package/esm/commands/subprocess/protocols/shell.d.ts +20 -12
  23. package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
  24. package/esm/commands/subprocess/protocols/shell.js +16 -21
  25. package/esm/commands/subprocess/protocols/shell.js.map +1 -1
  26. package/esm/commands/subprocess/protocols/types.d.ts +6 -6
  27. package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
  28. package/esm/commands/sync/list.d.ts +49 -27
  29. package/esm/commands/sync/list.d.ts.map +1 -1
  30. package/esm/commands/sync/list.js +9 -9
  31. package/esm/commands/sync/list.js.map +1 -1
  32. package/esm/commands/sync/pull.d.ts +7 -6
  33. package/esm/commands/sync/pull.d.ts.map +1 -1
  34. package/esm/commands/sync/pull.js +3 -5
  35. package/esm/commands/sync/pull.js.map +1 -1
  36. package/esm/commands/sync/push.d.ts +22 -16
  37. package/esm/commands/sync/push.d.ts.map +1 -1
  38. package/esm/commands/sync/push.js +12 -16
  39. package/esm/commands/sync/push.js.map +1 -1
  40. package/esm/commands/sync/request.d.ts +19 -17
  41. package/esm/commands/sync/request.d.ts.map +1 -1
  42. package/esm/commands/sync/request.js +14 -18
  43. package/esm/commands/sync/request.js.map +1 -1
  44. package/esm/commands/sync/response.d.ts +18 -18
  45. package/esm/commands/sync/response.d.ts.map +1 -1
  46. package/esm/commands/sync/response.js +20 -19
  47. package/esm/commands/sync/response.js.map +1 -1
  48. package/esm/commands/sync/socket.d.ts +1 -1
  49. package/esm/commands/sync/socket.js +2 -2
  50. package/esm/commands/sync/socket.js.map +1 -1
  51. package/esm/commands/sync/stat.d.ts +75 -51
  52. package/esm/commands/sync/stat.d.ts.map +1 -1
  53. package/esm/commands/sync/stat.js +73 -67
  54. package/esm/commands/sync/stat.js.map +1 -1
  55. package/esm/commands/sync/sync.d.ts +14 -14
  56. package/esm/commands/sync/sync.d.ts.map +1 -1
  57. package/esm/commands/sync/sync.js +4 -7
  58. package/esm/commands/sync/sync.js.map +1 -1
  59. package/esm/commands/tcpip.d.ts +0 -1
  60. package/esm/commands/tcpip.d.ts.map +1 -1
  61. package/esm/commands/tcpip.js +8 -8
  62. package/esm/commands/tcpip.js.map +1 -1
  63. package/esm/daemon/auth.d.ts +8 -7
  64. package/esm/daemon/auth.d.ts.map +1 -1
  65. package/esm/daemon/auth.js +7 -8
  66. package/esm/daemon/auth.js.map +1 -1
  67. package/esm/daemon/crypto.d.ts +1 -1
  68. package/esm/daemon/crypto.d.ts.map +1 -1
  69. package/esm/daemon/crypto.js +1 -0
  70. package/esm/daemon/crypto.js.map +1 -1
  71. package/esm/daemon/device.d.ts +2 -2
  72. package/esm/daemon/device.d.ts.map +1 -1
  73. package/esm/daemon/dispatcher.js +6 -6
  74. package/esm/daemon/dispatcher.js.map +1 -1
  75. package/esm/daemon/packet.d.ts +46 -30
  76. package/esm/daemon/packet.d.ts.map +1 -1
  77. package/esm/daemon/packet.js +24 -21
  78. package/esm/daemon/packet.js.map +1 -1
  79. package/esm/daemon/socket.d.ts +1 -6
  80. package/esm/daemon/socket.d.ts.map +1 -1
  81. package/esm/daemon/socket.js +4 -27
  82. package/esm/daemon/socket.js.map +1 -1
  83. package/esm/daemon/transport.d.ts +14 -14
  84. package/esm/daemon/transport.d.ts.map +1 -1
  85. package/esm/daemon/transport.js +6 -8
  86. package/esm/daemon/transport.js.map +1 -1
  87. package/esm/device-observer.d.ts +10 -0
  88. package/esm/device-observer.d.ts.map +1 -0
  89. package/esm/device-observer.js +2 -0
  90. package/esm/device-observer.js.map +1 -0
  91. package/esm/features.d.ts +12 -11
  92. package/esm/features.d.ts.map +1 -1
  93. package/esm/features.js +11 -12
  94. package/esm/features.js.map +1 -1
  95. package/esm/index.d.ts +1 -0
  96. package/esm/index.d.ts.map +1 -1
  97. package/esm/index.js +1 -0
  98. package/esm/index.js.map +1 -1
  99. package/esm/server/client.d.ts +33 -35
  100. package/esm/server/client.d.ts.map +1 -1
  101. package/esm/server/client.js +136 -201
  102. package/esm/server/client.js.map +1 -1
  103. package/esm/server/index.d.ts +2 -0
  104. package/esm/server/index.d.ts.map +1 -1
  105. package/esm/server/index.js +2 -0
  106. package/esm/server/index.js.map +1 -1
  107. package/esm/server/observer.d.ts +8 -0
  108. package/esm/server/observer.d.ts.map +1 -0
  109. package/esm/server/observer.js +106 -0
  110. package/esm/server/observer.js.map +1 -0
  111. package/esm/server/stream.d.ts +19 -0
  112. package/esm/server/stream.d.ts.map +1 -0
  113. package/esm/server/stream.js +77 -0
  114. package/esm/server/stream.js.map +1 -0
  115. package/esm/server/transport.d.ts +3 -4
  116. package/esm/server/transport.d.ts.map +1 -1
  117. package/esm/server/transport.js +16 -16
  118. package/esm/server/transport.js.map +1 -1
  119. package/esm/utils/base64.d.ts.map +1 -1
  120. package/esm/utils/base64.js +19 -16
  121. package/esm/utils/base64.js.map +1 -1
  122. package/esm/utils/index.d.ts +1 -0
  123. package/esm/utils/index.d.ts.map +1 -1
  124. package/esm/utils/index.js +1 -0
  125. package/esm/utils/index.js.map +1 -1
  126. package/esm/utils/no-op.d.ts.map +1 -1
  127. package/esm/utils/no-op.js +1 -0
  128. package/esm/utils/no-op.js.map +1 -1
  129. package/esm/utils/ref.d.ts +18 -0
  130. package/esm/utils/ref.d.ts.map +1 -0
  131. package/esm/utils/ref.js +29 -0
  132. package/esm/utils/ref.js.map +1 -0
  133. package/package.json +13 -16
  134. package/src/adb.ts +13 -7
  135. package/src/banner.ts +8 -6
  136. package/src/commands/framebuffer.ts +44 -35
  137. package/src/commands/reverse.ts +49 -31
  138. package/src/commands/subprocess/protocols/none.ts +3 -5
  139. package/src/commands/subprocess/protocols/shell.ts +26 -24
  140. package/src/commands/subprocess/protocols/types.ts +6 -6
  141. package/src/commands/sync/list.ts +20 -13
  142. package/src/commands/sync/pull.ts +8 -7
  143. package/src/commands/sync/push.ts +20 -16
  144. package/src/commands/sync/request.ts +18 -19
  145. package/src/commands/sync/response.ts +28 -29
  146. package/src/commands/sync/socket.ts +2 -2
  147. package/src/commands/sync/stat.ts +93 -71
  148. package/src/commands/sync/sync.ts +4 -8
  149. package/src/commands/tcpip.ts +8 -8
  150. package/src/daemon/auth.ts +11 -9
  151. package/src/daemon/crypto.ts +2 -0
  152. package/src/daemon/device.ts +2 -2
  153. package/src/daemon/dispatcher.ts +6 -6
  154. package/src/daemon/packet.ts +37 -25
  155. package/src/daemon/socket.ts +4 -27
  156. package/src/daemon/transport.ts +36 -35
  157. package/src/device-observer.ts +10 -0
  158. package/src/features.ts +13 -11
  159. package/src/index.ts +1 -0
  160. package/src/server/client.ts +181 -242
  161. package/src/server/index.ts +2 -0
  162. package/src/server/observer.ts +144 -0
  163. package/src/server/stream.ts +100 -0
  164. package/src/server/transport.ts +18 -17
  165. package/src/utils/base64.ts +26 -18
  166. package/src/utils/index.ts +1 -0
  167. package/src/utils/no-op.ts +1 -0
  168. package/src/utils/ref.ts +37 -0
  169. package/tsconfig.build.json +1 -12
  170. package/tsconfig.build.tsbuildinfo +1 -1
  171. package/CHANGELOG.json +0 -298
@@ -1,10 +1,6 @@
1
1
  import { getUint32LittleEndian } from "@yume-chan/no-data-view";
2
- import type {
3
- AsyncExactReadable,
4
- StructLike,
5
- StructValueType,
6
- } from "@yume-chan/struct";
7
- import Struct, { decodeUtf8 } from "@yume-chan/struct";
2
+ import type { AsyncExactReadable, StructLike } from "@yume-chan/struct";
3
+ import { decodeUtf8, string, struct, u32 } from "@yume-chan/struct";
8
4
 
9
5
  function encodeAsciiUnchecked(value: string): Uint8Array {
10
6
  const result = new Uint8Array(value.length);
@@ -18,32 +14,37 @@ function encodeAsciiUnchecked(value: string): Uint8Array {
18
14
  * Encode ID to numbers for faster comparison
19
15
  * @param value A 4-character string
20
16
  * @returns A 32-bit integer by encoding the string as little-endian
17
+ *
18
+ * #__NO_SIDE_EFFECTS__
21
19
  */
22
20
  export function adbSyncEncodeId(value: string): number {
23
21
  const buffer = encodeAsciiUnchecked(value);
24
22
  return getUint32LittleEndian(buffer, 0);
25
23
  }
26
24
 
27
- export namespace AdbSyncResponseId {
28
- export const Entry = adbSyncEncodeId("DENT");
29
- export const Entry2 = adbSyncEncodeId("DNT2");
30
- export const Lstat = adbSyncEncodeId("STAT");
31
- export const Stat = adbSyncEncodeId("STA2");
32
- export const Lstat2 = adbSyncEncodeId("LST2");
33
- export const Done = adbSyncEncodeId("DONE");
34
- export const Data = adbSyncEncodeId("DATA");
35
- export const Ok = adbSyncEncodeId("OKAY");
36
- export const Fail = adbSyncEncodeId("FAIL");
37
- }
25
+ export const AdbSyncResponseId = {
26
+ Entry: adbSyncEncodeId("DENT"),
27
+ Entry2: adbSyncEncodeId("DNT2"),
28
+ Lstat: adbSyncEncodeId("STAT"),
29
+ Stat: adbSyncEncodeId("STA2"),
30
+ Lstat2: adbSyncEncodeId("LST2"),
31
+ Done: adbSyncEncodeId("DONE"),
32
+ Data: adbSyncEncodeId("DATA"),
33
+ Ok: adbSyncEncodeId("OKAY"),
34
+ Fail: adbSyncEncodeId("FAIL"),
35
+ };
38
36
 
39
37
  export class AdbSyncError extends Error {}
40
38
 
41
- export const AdbSyncFailResponse = new Struct({ littleEndian: true })
42
- .uint32("messageLength")
43
- .string("message", { lengthField: "messageLength" })
44
- .postDeserialize((object) => {
45
- throw new AdbSyncError(object.message);
46
- });
39
+ export const AdbSyncFailResponse = struct(
40
+ { message: string(u32) },
41
+ {
42
+ littleEndian: true,
43
+ postDeserialize(value) {
44
+ throw new AdbSyncError(value.message);
45
+ },
46
+ },
47
+ );
47
48
 
48
49
  export async function adbSyncReadResponse<T>(
49
50
  stream: AsyncExactReadable,
@@ -68,13 +69,11 @@ export async function adbSyncReadResponse<T>(
68
69
  }
69
70
  }
70
71
 
71
- export async function* adbSyncReadResponses<
72
- T extends Struct<object, PropertyKey, object, unknown>,
73
- >(
72
+ export async function* adbSyncReadResponses<T>(
74
73
  stream: AsyncExactReadable,
75
74
  id: number | string,
76
- type: T,
77
- ): AsyncGenerator<StructValueType<T>, void, void> {
75
+ type: StructLike<T>,
76
+ ): AsyncGenerator<T, void, void> {
78
77
  if (typeof id === "string") {
79
78
  id = adbSyncEncodeId(id);
80
79
  }
@@ -93,7 +92,7 @@ export async function* adbSyncReadResponses<
93
92
  await stream.readExactly(type.size);
94
93
  return;
95
94
  case id:
96
- yield (await type.deserialize(stream)) as StructValueType<T>;
95
+ yield await type.deserialize(stream);
97
96
  break;
98
97
  default:
99
98
  throw new Error(
@@ -35,9 +35,9 @@ export class AdbSyncSocketLocked implements AsyncExactReadable {
35
35
  this.#combiner = new BufferCombiner(bufferSize);
36
36
  }
37
37
 
38
- async #write(buffer: Uint8Array) {
38
+ #write(buffer: Uint8Array) {
39
39
  // `#combiner` will reuse the buffer, so we need to use the Consumable pattern
40
- await Consumable.WritableStream.write(this.#writer, buffer);
40
+ return Consumable.WritableStream.write(this.#writer, buffer);
41
41
  }
42
42
 
43
43
  async flush() {
@@ -1,15 +1,18 @@
1
- import Struct, { placeholder } from "@yume-chan/struct";
1
+ import type { StructValue } from "@yume-chan/struct";
2
+ import { struct, u32, u64 } from "@yume-chan/struct";
2
3
 
3
4
  import { AdbSyncRequestId, adbSyncWriteRequest } from "./request.js";
4
5
  import { AdbSyncResponseId, adbSyncReadResponse } from "./response.js";
5
6
  import type { AdbSyncSocket } from "./socket.js";
6
7
 
7
8
  // https://github.com/python/cpython/blob/4e581d64b8aff3e2eda99b12f080c877bb78dfca/Lib/stat.py#L36
8
- export enum LinuxFileType {
9
- Directory = 0o04,
10
- File = 0o10,
11
- Link = 0o12,
12
- }
9
+ export const LinuxFileType = {
10
+ Directory: 0o04,
11
+ File: 0o10,
12
+ Link: 0o12,
13
+ } as const;
14
+
15
+ export type LinuxFileType = (typeof LinuxFileType)[keyof typeof LinuxFileType];
13
16
 
14
17
  export interface AdbSyncStat {
15
18
  mode: number;
@@ -24,79 +27,98 @@ export interface AdbSyncStat {
24
27
  ctime?: bigint;
25
28
  }
26
29
 
27
- export const AdbSyncLstatResponse = new Struct({ littleEndian: true })
28
- .int32("mode")
29
- .int32("size")
30
- .int32("mtime")
31
- .extra({
32
- get type() {
33
- return (this.mode >> 12) as LinuxFileType;
30
+ export const AdbSyncLstatResponse = struct(
31
+ { mode: u32, size: u32, mtime: u32 },
32
+ {
33
+ littleEndian: true,
34
+ extra: {
35
+ get type(): LinuxFileType {
36
+ return (this.mode >> 12) as LinuxFileType;
37
+ },
38
+ get permission(): number {
39
+ return this.mode & 0b00001111_11111111;
40
+ },
34
41
  },
35
- get permission() {
36
- return this.mode & 0b00001111_11111111;
42
+ postDeserialize(value) {
43
+ if (value.mode === 0 && value.size === 0 && value.mtime === 0) {
44
+ throw new Error("lstat error");
45
+ }
46
+ return value;
37
47
  },
38
- })
39
- .postDeserialize((object) => {
40
- if (object.mode === 0 && object.size === 0 && object.mtime === 0) {
41
- throw new Error("lstat error");
42
- }
43
- });
48
+ },
49
+ );
44
50
 
45
- export type AdbSyncLstatResponse =
46
- (typeof AdbSyncLstatResponse)["TDeserializeResult"];
51
+ export type AdbSyncLstatResponse = StructValue<typeof AdbSyncLstatResponse>;
47
52
 
48
- export enum AdbSyncStatErrorCode {
49
- SUCCESS = 0,
50
- EACCES = 13,
51
- EEXIST = 17,
52
- EFAULT = 14,
53
- EFBIG = 27,
54
- EINTR = 4,
55
- EINVAL = 22,
56
- EIO = 5,
57
- EISDIR = 21,
58
- ELOOP = 40,
59
- EMFILE = 24,
60
- ENAMETOOLONG = 36,
61
- ENFILE = 23,
62
- ENOENT = 2,
63
- ENOMEM = 12,
64
- ENOSPC = 28,
65
- ENOTDIR = 20,
66
- EOVERFLOW = 75,
67
- EPERM = 1,
68
- EROFS = 30,
69
- ETXTBSY = 26,
70
- }
53
+ export const AdbSyncStatErrorCode = {
54
+ SUCCESS: 0,
55
+ EACCES: 13,
56
+ EEXIST: 17,
57
+ EFAULT: 14,
58
+ EFBIG: 27,
59
+ EINTR: 4,
60
+ EINVAL: 22,
61
+ EIO: 5,
62
+ EISDIR: 21,
63
+ ELOOP: 40,
64
+ EMFILE: 24,
65
+ ENAMETOOLONG: 36,
66
+ ENFILE: 23,
67
+ ENOENT: 2,
68
+ ENOMEM: 12,
69
+ ENOSPC: 28,
70
+ ENOTDIR: 20,
71
+ EOVERFLOW: 75,
72
+ EPERM: 1,
73
+ EROFS: 30,
74
+ ETXTBSY: 26,
75
+ } as const;
71
76
 
72
- export const AdbSyncStatResponse = new Struct({ littleEndian: true })
73
- .uint32("error", placeholder<AdbSyncStatErrorCode>())
74
- .uint64("dev")
75
- .uint64("ino")
76
- .uint32("mode")
77
- .uint32("nlink")
78
- .uint32("uid")
79
- .uint32("gid")
80
- .uint64("size")
81
- .uint64("atime")
82
- .uint64("mtime")
83
- .uint64("ctime")
84
- .extra({
85
- get type() {
86
- return (this.mode >> 12) as LinuxFileType;
77
+ export type AdbSyncStatErrorCode =
78
+ (typeof AdbSyncStatErrorCode)[keyof typeof AdbSyncStatErrorCode];
79
+
80
+ const AdbSyncStatErrorName = /* #__PURE__ */ (() =>
81
+ Object.fromEntries(
82
+ Object.entries(AdbSyncStatErrorCode).map(([key, value]) => [
83
+ value,
84
+ key,
85
+ ]),
86
+ ))();
87
+
88
+ export const AdbSyncStatResponse = struct(
89
+ {
90
+ error: u32<AdbSyncStatErrorCode>(),
91
+ dev: u64,
92
+ ino: u64,
93
+ mode: u32,
94
+ nlink: u32,
95
+ uid: u32,
96
+ gid: u32,
97
+ size: u64,
98
+ atime: u64,
99
+ mtime: u64,
100
+ ctime: u64,
101
+ },
102
+ {
103
+ littleEndian: true,
104
+ extra: {
105
+ get type(): LinuxFileType {
106
+ return (this.mode >> 12) as LinuxFileType;
107
+ },
108
+ get permission(): number {
109
+ return this.mode & 0b00001111_11111111;
110
+ },
87
111
  },
88
- get permission() {
89
- return this.mode & 0b00001111_11111111;
112
+ postDeserialize(value) {
113
+ if (value.error) {
114
+ throw new Error(AdbSyncStatErrorName[value.error]);
115
+ }
116
+ return value;
90
117
  },
91
- })
92
- .postDeserialize((object) => {
93
- if (object.error) {
94
- throw new Error(AdbSyncStatErrorCode[object.error]);
95
- }
96
- });
118
+ },
119
+ );
97
120
 
98
- export type AdbSyncStatResponse =
99
- (typeof AdbSyncStatResponse)["TDeserializeResult"];
121
+ export type AdbSyncStatResponse = StructValue<typeof AdbSyncStatResponse>;
100
122
 
101
123
  export async function adbSyncLstat(
102
124
  socket: AdbSyncSocket,
@@ -1,4 +1,3 @@
1
- import { AutoDisposable } from "@yume-chan/event";
2
1
  import type { MaybeConsumable, ReadableStream } from "@yume-chan/stream-extra";
3
2
 
4
3
  import type { Adb, AdbSocket } from "../../adb.js";
@@ -39,7 +38,7 @@ export interface AdbSyncWriteOptions {
39
38
  dryRun?: boolean;
40
39
  }
41
40
 
42
- export class AdbSync extends AutoDisposable {
41
+ export class AdbSync {
43
42
  protected _adb: Adb;
44
43
  protected _socket: AdbSyncSocket;
45
44
 
@@ -70,8 +69,6 @@ export class AdbSync extends AutoDisposable {
70
69
  }
71
70
 
72
71
  constructor(adb: Adb, socket: AdbSocket) {
73
- super();
74
-
75
72
  this._adb = adb;
76
73
  this._socket = new AdbSyncSocket(socket, adb.maxPayloadSize);
77
74
 
@@ -117,7 +114,7 @@ export class AdbSync extends AutoDisposable {
117
114
  try {
118
115
  await this.lstat(path + "/");
119
116
  return true;
120
- } catch (e) {
117
+ } catch {
121
118
  return false;
122
119
  }
123
120
  }
@@ -172,8 +169,7 @@ export class AdbSync extends AutoDisposable {
172
169
  return this._socket.lock();
173
170
  }
174
171
 
175
- override async dispose() {
176
- super.dispose();
177
- await this._socket.close();
172
+ dispose() {
173
+ return this._socket.close();
178
174
  }
179
175
  }
@@ -20,14 +20,14 @@ export interface AdbTcpIpListenAddresses {
20
20
  persistPort: number | undefined;
21
21
  }
22
22
 
23
- export class AdbTcpIpCommand extends AdbCommandBase {
24
- #parsePort(value: string): number | undefined {
25
- if (!value || value === "0") {
26
- return undefined;
27
- }
28
- return Number.parseInt(value, 10);
23
+ function parsePort(value: string): number | undefined {
24
+ if (!value || value === "0") {
25
+ return undefined;
29
26
  }
27
+ return Number.parseInt(value, 10);
28
+ }
30
29
 
30
+ export class AdbTcpIpCommand extends AdbCommandBase {
31
31
  async getListenAddresses(): Promise<AdbTcpIpListenAddresses> {
32
32
  const serviceListenAddresses = await this.adb.getProp(
33
33
  "service.adb.listen_addrs",
@@ -40,8 +40,8 @@ export class AdbTcpIpCommand extends AdbCommandBase {
40
40
  serviceListenAddresses != ""
41
41
  ? serviceListenAddresses.split(",")
42
42
  : [],
43
- servicePort: this.#parsePort(servicePort),
44
- persistPort: this.#parsePort(persistPort),
43
+ servicePort: parsePort(servicePort),
44
+ persistPort: parsePort(persistPort),
45
45
  };
46
46
  }
47
47
 
@@ -1,7 +1,7 @@
1
+ import type { MaybePromiseLike } from "@yume-chan/async";
1
2
  import { PromiseResolver } from "@yume-chan/async";
2
3
  import type { Disposable } from "@yume-chan/event";
3
- import type { ValueOrPromise } from "@yume-chan/struct";
4
- import { EMPTY_UINT8_ARRAY } from "@yume-chan/struct";
4
+ import { EmptyUint8Array } from "@yume-chan/struct";
5
5
 
6
6
  import {
7
7
  calculateBase64EncodedLength,
@@ -33,7 +33,7 @@ export interface AdbCredentialStore {
33
33
  /**
34
34
  * Generates and stores a RSA private key with modulus length `2048` and public exponent `65537`.
35
35
  */
36
- generateKey(): ValueOrPromise<AdbPrivateKey>;
36
+ generateKey(): MaybePromiseLike<AdbPrivateKey>;
37
37
 
38
38
  /**
39
39
  * Synchronously or asynchronously iterates through all stored RSA private keys.
@@ -43,11 +43,13 @@ export interface AdbCredentialStore {
43
43
  iterateKeys(): AdbKeyIterable;
44
44
  }
45
45
 
46
- export enum AdbAuthType {
47
- Token = 1,
48
- Signature = 2,
49
- PublicKey = 3,
50
- }
46
+ export const AdbAuthType = {
47
+ Token: 1,
48
+ Signature: 2,
49
+ PublicKey: 3,
50
+ } as const;
51
+
52
+ export type AdbAuthType = (typeof AdbAuthType)[keyof typeof AdbAuthType];
51
53
 
52
54
  export interface AdbAuthenticator {
53
55
  /**
@@ -114,7 +116,7 @@ export const AdbPublicKeyAuthenticator: AdbAuthenticator = async function* (
114
116
 
115
117
  const nameBuffer = privateKey.name?.length
116
118
  ? encodeUtf8(privateKey.name)
117
- : EMPTY_UINT8_ARRAY;
119
+ : EmptyUint8Array;
118
120
  const publicKeyBuffer = new Uint8Array(
119
121
  publicKeyBase64Length +
120
122
  (nameBuffer.length ? nameBuffer.length + 1 : 0) + // Space character + name
@@ -38,6 +38,8 @@ export function getBigUint(
38
38
  * @param littleEndian If `false` or `undefined`, a big-endian value should be written,
39
39
  * otherwise a little-endian value should be written.
40
40
  */
41
+
42
+ // eslint-disable-next-line @typescript-eslint/max-params
41
43
  export function setBigUint(
42
44
  array: Uint8Array,
43
45
  byteOffset: number,
@@ -1,5 +1,5 @@
1
+ import type { MaybePromiseLike } from "@yume-chan/async";
1
2
  import type { Consumable, ReadableWritablePair } from "@yume-chan/stream-extra";
2
- import type { ValueOrPromise } from "@yume-chan/struct";
3
3
 
4
4
  import type { AdbPacketData, AdbPacketInit } from "./packet.js";
5
5
 
@@ -8,7 +8,7 @@ export interface AdbDaemonDevice {
8
8
 
9
9
  readonly name: string | undefined;
10
10
 
11
- connect(): ValueOrPromise<
11
+ connect(): MaybePromiseLike<
12
12
  ReadableWritablePair<AdbPacketData, Consumable<AdbPacketInit>>
13
13
  >;
14
14
  }
@@ -16,7 +16,7 @@ import {
16
16
  Consumable,
17
17
  WritableStream,
18
18
  } from "@yume-chan/stream-extra";
19
- import { EMPTY_UINT8_ARRAY, decodeUtf8, encodeUtf8 } from "@yume-chan/struct";
19
+ import { EmptyUint8Array, decodeUtf8, encodeUtf8 } from "@yume-chan/struct";
20
20
 
21
21
  import type { AdbIncomingSocketHandler, AdbSocket, Closeable } from "../adb.js";
22
22
 
@@ -259,7 +259,7 @@ export class AdbPacketDispatcher implements Closeable {
259
259
  AdbCommand.Close,
260
260
  packet.arg1,
261
261
  packet.arg0,
262
- EMPTY_UINT8_ARRAY,
262
+ EmptyUint8Array,
263
263
  );
264
264
  }
265
265
 
@@ -271,7 +271,7 @@ export class AdbPacketDispatcher implements Closeable {
271
271
  payload = new Uint8Array(4);
272
272
  setUint32LittleEndian(payload, 0, ackBytes);
273
273
  } else {
274
- payload = EMPTY_UINT8_ARRAY;
274
+ payload = EmptyUint8Array;
275
275
  }
276
276
 
277
277
  return this.sendPacket(AdbCommand.Okay, localId, remoteId, payload);
@@ -312,7 +312,7 @@ export class AdbPacketDispatcher implements Closeable {
312
312
  AdbCommand.Close,
313
313
  0,
314
314
  remoteId,
315
- EMPTY_UINT8_ARRAY,
315
+ EmptyUint8Array,
316
316
  );
317
317
  return;
318
318
  }
@@ -334,12 +334,12 @@ export class AdbPacketDispatcher implements Closeable {
334
334
  remoteId,
335
335
  this.options.initialDelayedAckBytes,
336
336
  );
337
- } catch (e) {
337
+ } catch {
338
338
  await this.sendPacket(
339
339
  AdbCommand.Close,
340
340
  0,
341
341
  remoteId,
342
- EMPTY_UINT8_ARRAY,
342
+ EmptyUint8Array,
343
343
  );
344
344
  }
345
345
  }
@@ -1,32 +1,43 @@
1
1
  import { Consumable, TransformStream } from "@yume-chan/stream-extra";
2
- import Struct from "@yume-chan/struct";
2
+ import type { StructInit, StructValue } from "@yume-chan/struct";
3
+ import { buffer, s32, struct, u32 } from "@yume-chan/struct";
3
4
 
4
- export enum AdbCommand {
5
- Auth = 0x48545541, // 'AUTH'
6
- Close = 0x45534c43, // 'CLSE'
7
- Connect = 0x4e584e43, // 'CNXN'
8
- Okay = 0x59414b4f, // 'OKAY'
9
- Open = 0x4e45504f, // 'OPEN'
10
- Write = 0x45545257, // 'WRTE'
11
- }
5
+ export const AdbCommand = {
6
+ Auth: 0x48545541, // 'AUTH'
7
+ Close: 0x45534c43, // 'CLSE'
8
+ Connect: 0x4e584e43, // 'CNXN'
9
+ Okay: 0x59414b4f, // 'OKAY'
10
+ Open: 0x4e45504f, // 'OPEN'
11
+ Write: 0x45545257, // 'WRTE'
12
+ } as const;
13
+
14
+ export type AdbCommand = (typeof AdbCommand)[keyof typeof AdbCommand];
12
15
 
13
- export const AdbPacketHeader = new Struct({ littleEndian: true })
14
- .uint32("command")
15
- .uint32("arg0")
16
- .uint32("arg1")
17
- .uint32("payloadLength")
18
- .uint32("checksum")
19
- .int32("magic");
16
+ export const AdbPacketHeader = struct(
17
+ {
18
+ command: u32,
19
+ arg0: u32,
20
+ arg1: u32,
21
+ payloadLength: u32,
22
+ checksum: u32,
23
+ magic: s32,
24
+ },
25
+ { littleEndian: true },
26
+ );
20
27
 
21
- export type AdbPacketHeader = (typeof AdbPacketHeader)["TDeserializeResult"];
28
+ export type AdbPacketHeader = StructValue<typeof AdbPacketHeader>;
22
29
 
23
- type AdbPacketHeaderInit = (typeof AdbPacketHeader)["TInit"];
30
+ type AdbPacketHeaderInit = StructInit<typeof AdbPacketHeader>;
24
31
 
25
- export const AdbPacket = new Struct({ littleEndian: true })
26
- .concat(AdbPacketHeader)
27
- .uint8Array("payload", { lengthField: "payloadLength" });
32
+ export const AdbPacket = struct(
33
+ /* #__PURE__ */ (() => ({
34
+ ...AdbPacketHeader.fields,
35
+ payload: buffer("payloadLength"),
36
+ }))(),
37
+ { littleEndian: true },
38
+ );
28
39
 
29
- export type AdbPacket = (typeof AdbPacket)["TDeserializeResult"];
40
+ export type AdbPacket = StructValue<typeof AdbPacket>;
30
41
 
31
42
  /**
32
43
  * `AdbPacketData` contains all the useful fields of `AdbPacket`.
@@ -39,11 +50,11 @@ export type AdbPacket = (typeof AdbPacket)["TDeserializeResult"];
39
50
  * so `AdbSocket#writable#write` only needs `AdbPacketData`.
40
51
  */
41
52
  export type AdbPacketData = Omit<
42
- (typeof AdbPacket)["TInit"],
53
+ StructInit<typeof AdbPacket>,
43
54
  "checksum" | "magic"
44
55
  >;
45
56
 
46
- export type AdbPacketInit = (typeof AdbPacket)["TInit"];
57
+ export type AdbPacketInit = StructInit<typeof AdbPacket>;
47
58
 
48
59
  export function calculateChecksum(payload: Uint8Array): number {
49
60
  return payload.reduce((result, item) => result + item, 0);
@@ -61,9 +72,10 @@ export class AdbPacketSerializeStream extends TransformStream<
61
72
  const init = chunk as AdbPacketInit & AdbPacketHeaderInit;
62
73
  init.payloadLength = init.payload.length;
63
74
 
75
+ AdbPacketHeader.serialize(init, headerBuffer);
64
76
  await Consumable.ReadableStream.enqueue(
65
77
  controller,
66
- AdbPacketHeader.serialize(init, headerBuffer),
78
+ headerBuffer,
67
79
  );
68
80
 
69
81
  if (init.payloadLength) {
@@ -7,7 +7,7 @@ import type {
7
7
  WritableStreamDefaultController,
8
8
  } from "@yume-chan/stream-extra";
9
9
  import { MaybeConsumable, PushReadableStream } from "@yume-chan/stream-extra";
10
- import { EMPTY_UINT8_ARRAY } from "@yume-chan/struct";
10
+ import { EmptyUint8Array } from "@yume-chan/struct";
11
11
 
12
12
  import type { AdbSocket } from "../adb.js";
13
13
 
@@ -138,20 +138,7 @@ export class AdbDaemonSocketController
138
138
  }
139
139
 
140
140
  async enqueue(data: Uint8Array) {
141
- // Consumers can `cancel` the `readable` if they are not interested in future data.
142
- // Throw away the data if that happens.
143
- if (this.#readableController.abortSignal.aborted) {
144
- return;
145
- }
146
-
147
- try {
148
- await this.#readableController.enqueue(data);
149
- } catch (e) {
150
- if (this.#readableController.abortSignal.aborted) {
151
- return;
152
- }
153
- throw e;
154
- }
141
+ await this.#readableController.enqueue(data);
155
142
  }
156
143
 
157
144
  public ack(bytes: number) {
@@ -177,28 +164,18 @@ export class AdbDaemonSocketController
177
164
  AdbCommand.Close,
178
165
  this.localId,
179
166
  this.remoteId,
180
- EMPTY_UINT8_ARRAY,
167
+ EmptyUint8Array,
181
168
  );
182
169
  }
183
170
 
184
171
  dispose() {
185
- try {
186
- this.#readableController.close();
187
- } catch {
188
- // ignore
189
- }
190
-
172
+ this.#readableController.close();
191
173
  this.#closedPromise.resolve();
192
174
  }
193
175
  }
194
176
 
195
177
  /**
196
178
  * A duplex stream representing a socket to ADB daemon.
197
- *
198
- * To close it, call either `socket.close()`,
199
- * `socket.readable.cancel()`, `socket.readable.getReader().cancel()`,
200
- * `socket.writable.abort()`, `socket.writable.getWriter().abort()`,
201
- * `socket.writable.close()` or `socket.writable.getWriter().close()`.
202
179
  */
203
180
  export class AdbDaemonSocket implements AdbDaemonSocketInfo, AdbSocket {
204
181
  #controller: AdbDaemonSocketController;