@yume-chan/adb 1.0.0 → 2.0.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 (217) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/LICENSE +1 -1
  3. package/esm/adb.d.ts +7 -6
  4. package/esm/adb.d.ts.map +1 -1
  5. package/esm/adb.js +22 -22
  6. package/esm/adb.js.map +1 -1
  7. package/esm/banner.js.map +1 -1
  8. package/esm/commands/base.d.ts +3 -2
  9. package/esm/commands/base.d.ts.map +1 -1
  10. package/esm/commands/base.js +6 -3
  11. package/esm/commands/base.js.map +1 -1
  12. package/esm/commands/framebuffer.d.ts +6 -6
  13. package/esm/commands/power.d.ts +2 -2
  14. package/esm/commands/power.d.ts.map +1 -1
  15. package/esm/commands/power.js +4 -4
  16. package/esm/commands/power.js.map +1 -1
  17. package/esm/commands/reverse.d.ts +3 -4
  18. package/esm/commands/reverse.d.ts.map +1 -1
  19. package/esm/commands/reverse.js +5 -10
  20. package/esm/commands/reverse.js.map +1 -1
  21. package/esm/commands/subprocess/index.d.ts +3 -2
  22. package/esm/commands/subprocess/index.d.ts.map +1 -1
  23. package/esm/commands/subprocess/index.js +3 -2
  24. package/esm/commands/subprocess/index.js.map +1 -1
  25. package/esm/commands/subprocess/none/index.d.ts +5 -0
  26. package/esm/commands/subprocess/none/index.d.ts.map +1 -0
  27. package/esm/commands/subprocess/none/index.js +5 -0
  28. package/esm/commands/subprocess/none/index.js.map +1 -0
  29. package/esm/commands/subprocess/none/process.d.ts +13 -0
  30. package/esm/commands/subprocess/none/process.d.ts.map +1 -0
  31. package/esm/commands/subprocess/none/process.js +36 -0
  32. package/esm/commands/subprocess/none/process.js.map +1 -0
  33. package/esm/commands/subprocess/none/pty.d.ts +15 -0
  34. package/esm/commands/subprocess/none/pty.d.ts.map +1 -0
  35. package/esm/commands/subprocess/none/pty.js +29 -0
  36. package/esm/commands/subprocess/none/pty.js.map +1 -0
  37. package/esm/commands/subprocess/none/service.d.ts +10 -0
  38. package/esm/commands/subprocess/none/service.d.ts.map +1 -0
  39. package/esm/commands/subprocess/none/service.js +32 -0
  40. package/esm/commands/subprocess/none/service.js.map +1 -0
  41. package/esm/commands/subprocess/none/spawner.d.ts +19 -0
  42. package/esm/commands/subprocess/none/spawner.d.ts.map +1 -0
  43. package/esm/commands/subprocess/none/spawner.js +26 -0
  44. package/esm/commands/subprocess/none/spawner.js.map +1 -0
  45. package/esm/commands/subprocess/pty.d.ts +10 -0
  46. package/esm/commands/subprocess/pty.d.ts.map +1 -0
  47. package/esm/commands/subprocess/pty.js +2 -0
  48. package/esm/commands/subprocess/pty.js.map +1 -0
  49. package/esm/commands/subprocess/service.d.ts +11 -0
  50. package/esm/commands/subprocess/service.d.ts.map +1 -0
  51. package/esm/commands/subprocess/service.js +25 -0
  52. package/esm/commands/subprocess/service.js.map +1 -0
  53. package/esm/commands/subprocess/shell/index.d.ts +6 -0
  54. package/esm/commands/subprocess/shell/index.d.ts.map +1 -0
  55. package/esm/commands/subprocess/shell/index.js +6 -0
  56. package/esm/commands/subprocess/shell/index.js.map +1 -0
  57. package/esm/commands/subprocess/shell/process.d.ts +15 -0
  58. package/esm/commands/subprocess/shell/process.d.ts.map +1 -0
  59. package/esm/commands/subprocess/{protocols/shell.js → shell/process.js} +27 -54
  60. package/esm/commands/subprocess/shell/process.js.map +1 -0
  61. package/esm/commands/subprocess/shell/pty.d.ts +15 -0
  62. package/esm/commands/subprocess/shell/pty.d.ts.map +1 -0
  63. package/esm/commands/subprocess/shell/pty.js +76 -0
  64. package/esm/commands/subprocess/shell/pty.js.map +1 -0
  65. package/esm/commands/subprocess/shell/service.d.ts +14 -0
  66. package/esm/commands/subprocess/shell/service.d.ts.map +1 -0
  67. package/esm/commands/subprocess/shell/service.js +41 -0
  68. package/esm/commands/subprocess/shell/service.js.map +1 -0
  69. package/esm/commands/subprocess/shell/shared.d.ts +19 -0
  70. package/esm/commands/subprocess/shell/shared.d.ts.map +1 -0
  71. package/esm/commands/subprocess/shell/shared.js +15 -0
  72. package/esm/commands/subprocess/shell/shared.js.map +1 -0
  73. package/esm/commands/subprocess/shell/spawner.d.ts +24 -0
  74. package/esm/commands/subprocess/shell/spawner.d.ts.map +1 -0
  75. package/esm/commands/subprocess/shell/spawner.js +38 -0
  76. package/esm/commands/subprocess/shell/spawner.js.map +1 -0
  77. package/esm/commands/subprocess/utils.d.ts +1 -0
  78. package/esm/commands/subprocess/utils.d.ts.map +1 -1
  79. package/esm/commands/subprocess/utils.js +37 -0
  80. package/esm/commands/subprocess/utils.js.map +1 -1
  81. package/esm/commands/sync/list.d.ts +12 -8
  82. package/esm/commands/sync/list.d.ts.map +1 -1
  83. package/esm/commands/sync/list.js +5 -7
  84. package/esm/commands/sync/list.js.map +1 -1
  85. package/esm/commands/sync/pull.d.ts +4 -4
  86. package/esm/commands/sync/pull.d.ts.map +1 -1
  87. package/esm/commands/sync/pull.js +2 -7
  88. package/esm/commands/sync/pull.js.map +1 -1
  89. package/esm/commands/sync/push.d.ts +4 -4
  90. package/esm/commands/sync/request.d.ts +1 -1
  91. package/esm/commands/sync/response.d.ts +5 -5
  92. package/esm/commands/sync/response.d.ts.map +1 -1
  93. package/esm/commands/sync/response.js +2 -1
  94. package/esm/commands/sync/response.js.map +1 -1
  95. package/esm/commands/sync/stat.d.ts +4 -4
  96. package/esm/commands/sync/sync.d.ts +2 -2
  97. package/esm/commands/sync/sync.js +1 -1
  98. package/esm/commands/sync/sync.js.map +1 -1
  99. package/esm/commands/tcpip.d.ts +2 -2
  100. package/esm/commands/tcpip.js +2 -2
  101. package/esm/daemon/dispatcher.d.ts.map +1 -1
  102. package/esm/daemon/dispatcher.js.map +1 -1
  103. package/esm/daemon/packet.d.ts +6 -4
  104. package/esm/daemon/packet.d.ts.map +1 -1
  105. package/esm/daemon/packet.js +3 -5
  106. package/esm/daemon/packet.js.map +1 -1
  107. package/esm/daemon/socket.d.ts +2 -2
  108. package/esm/daemon/socket.d.ts.map +1 -1
  109. package/esm/daemon/socket.js +1 -1
  110. package/esm/daemon/socket.js.map +1 -1
  111. package/esm/device-observer.d.ts +4 -4
  112. package/esm/device-observer.d.ts.map +1 -1
  113. package/esm/server/client.d.ts +15 -57
  114. package/esm/server/client.d.ts.map +1 -1
  115. package/esm/server/client.js +66 -263
  116. package/esm/server/client.js.map +1 -1
  117. package/esm/server/commands/index.d.ts +3 -0
  118. package/esm/server/commands/index.d.ts.map +1 -0
  119. package/esm/server/commands/index.js +3 -0
  120. package/esm/server/commands/index.js.map +1 -0
  121. package/esm/server/commands/m-dns.d.ts +12 -0
  122. package/esm/server/commands/m-dns.d.ts.map +1 -0
  123. package/esm/server/commands/m-dns.js +38 -0
  124. package/esm/server/commands/m-dns.js.map +1 -0
  125. package/esm/server/commands/wireless.d.ts +27 -0
  126. package/esm/server/commands/wireless.d.ts.map +1 -0
  127. package/esm/server/commands/wireless.js +82 -0
  128. package/esm/server/commands/wireless.js.map +1 -0
  129. package/esm/server/index.d.ts +2 -0
  130. package/esm/server/index.d.ts.map +1 -1
  131. package/esm/server/index.js +2 -0
  132. package/esm/server/index.js.map +1 -1
  133. package/esm/server/observer.d.ts +9 -0
  134. package/esm/server/observer.d.ts.map +1 -0
  135. package/esm/server/observer.js +128 -0
  136. package/esm/server/observer.js.map +1 -0
  137. package/esm/server/stream.d.ts +19 -0
  138. package/esm/server/stream.d.ts.map +1 -0
  139. package/esm/server/stream.js +77 -0
  140. package/esm/server/stream.js.map +1 -0
  141. package/esm/server/transport.d.ts +3 -4
  142. package/esm/server/transport.d.ts.map +1 -1
  143. package/esm/server/transport.js +18 -16
  144. package/esm/server/transport.js.map +1 -1
  145. package/esm/utils/index.d.ts +1 -0
  146. package/esm/utils/index.d.ts.map +1 -1
  147. package/esm/utils/index.js +1 -0
  148. package/esm/utils/index.js.map +1 -1
  149. package/esm/utils/ref.d.ts +18 -0
  150. package/esm/utils/ref.d.ts.map +1 -0
  151. package/esm/utils/ref.js +29 -0
  152. package/esm/utils/ref.js.map +1 -0
  153. package/package.json +10 -10
  154. package/src/adb.ts +29 -28
  155. package/src/banner.ts +4 -4
  156. package/src/commands/base.ts +6 -3
  157. package/src/commands/power.ts +6 -6
  158. package/src/commands/reverse.ts +9 -12
  159. package/src/commands/subprocess/index.ts +3 -2
  160. package/src/commands/subprocess/none/index.ts +4 -0
  161. package/src/commands/subprocess/none/process.ts +56 -0
  162. package/src/commands/subprocess/none/pty.ts +45 -0
  163. package/src/commands/subprocess/none/service.ts +42 -0
  164. package/src/commands/subprocess/none/spawner.ts +68 -0
  165. package/src/commands/subprocess/pty.ts +15 -0
  166. package/src/commands/subprocess/service.ts +32 -0
  167. package/src/commands/subprocess/shell/index.ts +5 -0
  168. package/src/commands/subprocess/shell/process.ts +127 -0
  169. package/src/commands/subprocess/shell/pty.ts +112 -0
  170. package/src/commands/subprocess/shell/service.ts +58 -0
  171. package/src/commands/subprocess/shell/shared.ts +25 -0
  172. package/src/commands/subprocess/shell/spawner.ts +90 -0
  173. package/src/commands/subprocess/utils.ts +41 -0
  174. package/src/commands/sync/list.ts +7 -17
  175. package/src/commands/sync/pull.ts +2 -8
  176. package/src/commands/sync/response.ts +6 -4
  177. package/src/commands/sync/sync.ts +1 -1
  178. package/src/commands/tcpip.ts +2 -2
  179. package/src/daemon/dispatcher.ts +7 -4
  180. package/src/daemon/packet.ts +4 -8
  181. package/src/daemon/socket.ts +6 -6
  182. package/src/device-observer.ts +4 -4
  183. package/src/server/client.ts +100 -305
  184. package/src/server/commands/index.ts +2 -0
  185. package/src/server/commands/m-dns.ts +43 -0
  186. package/src/server/commands/wireless.ts +94 -0
  187. package/src/server/index.ts +2 -0
  188. package/src/server/observer.ts +172 -0
  189. package/src/server/stream.ts +100 -0
  190. package/src/server/transport.ts +38 -34
  191. package/src/utils/index.ts +1 -0
  192. package/src/utils/ref.ts +37 -0
  193. package/tsconfig.build.tsbuildinfo +1 -1
  194. package/esm/commands/subprocess/command.d.ts +0 -58
  195. package/esm/commands/subprocess/command.d.ts.map +0 -1
  196. package/esm/commands/subprocess/command.js +0 -89
  197. package/esm/commands/subprocess/command.js.map +0 -1
  198. package/esm/commands/subprocess/protocols/index.d.ts +0 -4
  199. package/esm/commands/subprocess/protocols/index.d.ts.map +0 -1
  200. package/esm/commands/subprocess/protocols/index.js +0 -4
  201. package/esm/commands/subprocess/protocols/index.js.map +0 -1
  202. package/esm/commands/subprocess/protocols/none.d.ts +0 -32
  203. package/esm/commands/subprocess/protocols/none.d.ts.map +0 -1
  204. package/esm/commands/subprocess/protocols/none.js +0 -61
  205. package/esm/commands/subprocess/protocols/none.js.map +0 -1
  206. package/esm/commands/subprocess/protocols/shell.d.ts +0 -42
  207. package/esm/commands/subprocess/protocols/shell.d.ts.map +0 -1
  208. package/esm/commands/subprocess/protocols/shell.js.map +0 -1
  209. package/esm/commands/subprocess/protocols/types.d.ts +0 -49
  210. package/esm/commands/subprocess/protocols/types.d.ts.map +0 -1
  211. package/esm/commands/subprocess/protocols/types.js +0 -2
  212. package/esm/commands/subprocess/protocols/types.js.map +0 -1
  213. package/src/commands/subprocess/command.ts +0 -141
  214. package/src/commands/subprocess/protocols/index.ts +0 -3
  215. package/src/commands/subprocess/protocols/none.ts +0 -81
  216. package/src/commands/subprocess/protocols/shell.ts +0 -171
  217. package/src/commands/subprocess/protocols/types.ts +0 -63
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/commands/subprocess/protocols/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EACR,eAAe,EACf,cAAc,EACd,cAAc,EACjB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,KAAK,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEtD,MAAM,WAAW,qBAAqB;IAClC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC,CAAC;IAE5D;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAE5C;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAE5C;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAE/B;;;;;OAKG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAE3D;;OAEG;IACH,IAAI,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,gCAAgC;IAC7C,+DAA+D;IAC/D,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAEjD,sDAAsD;IACtD,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG,gBAAgB,CAAC,qBAAqB,CAAC,CAAC;IAExE,gDAAgD;IAChD,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,GAAG,gBAAgB,CAAC,qBAAqB,CAAC,CAAC;IAExE,oEAAoE;IACpE,KAAK,MAAM,EAAE,SAAS,GAAG,qBAAqB,CAAC;CAClD"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/commands/subprocess/protocols/types.ts"],"names":[],"mappings":""}
@@ -1,141 +0,0 @@
1
- import { ConcatStringStream, TextDecoderStream } from "@yume-chan/stream-extra";
2
-
3
- import { AdbCommandBase } from "../base.js";
4
-
5
- import type {
6
- AdbSubprocessProtocol,
7
- AdbSubprocessProtocolConstructor,
8
- } from "./protocols/index.js";
9
- import {
10
- AdbSubprocessNoneProtocol,
11
- AdbSubprocessShellProtocol,
12
- } from "./protocols/index.js";
13
-
14
- export interface AdbSubprocessOptions {
15
- /**
16
- * A list of `AdbSubprocessProtocolConstructor`s to be used.
17
- *
18
- * Different `AdbSubprocessProtocol` has different capabilities, thus requires specific adaptations.
19
- * Check their documentations for details.
20
- *
21
- * The first protocol whose `isSupported` returns `true` will be used.
22
- * If no `AdbSubprocessProtocol` is supported, an error will be thrown.
23
- *
24
- * @default [AdbSubprocessShellProtocol, AdbSubprocessNoneProtocol]
25
- */
26
- protocols: AdbSubprocessProtocolConstructor[];
27
- }
28
-
29
- const DEFAULT_OPTIONS: AdbSubprocessOptions = {
30
- protocols: [AdbSubprocessShellProtocol, AdbSubprocessNoneProtocol],
31
- };
32
-
33
- export interface AdbSubprocessWaitResult {
34
- stdout: string;
35
- stderr: string;
36
- exitCode: number;
37
- }
38
-
39
- export class AdbSubprocess extends AdbCommandBase {
40
- async #createProtocol(
41
- mode: "pty" | "raw",
42
- command?: string | string[],
43
- options?: Partial<AdbSubprocessOptions>,
44
- ): Promise<AdbSubprocessProtocol> {
45
- const { protocols } = { ...DEFAULT_OPTIONS, ...options };
46
-
47
- let Constructor: AdbSubprocessProtocolConstructor | undefined;
48
- for (const item of protocols) {
49
- // It's async so can't use `Array#find`
50
- if (await item.isSupported(this.adb)) {
51
- Constructor = item;
52
- break;
53
- }
54
- }
55
-
56
- if (!Constructor) {
57
- throw new Error("No specified protocol is supported by the device");
58
- }
59
-
60
- if (Array.isArray(command)) {
61
- command = command.join(" ");
62
- } else if (command === undefined) {
63
- // spawn the default shell
64
- command = "";
65
- }
66
- return await Constructor[mode](this.adb, command);
67
- }
68
-
69
- /**
70
- * Spawns an executable in PTY mode.
71
- *
72
- * Redirection mode is enough for most simple commands, but PTY mode is required for
73
- * commands that manipulate the terminal, such as `vi` and `less`.
74
- * @param command The command to run. If omitted, the default shell will be spawned.
75
- * @param options The options for creating the `AdbSubprocessProtocol`
76
- * @returns A new `AdbSubprocessProtocol` instance connecting to the spawned process.
77
- */
78
- shell(
79
- command?: string | string[],
80
- options?: Partial<AdbSubprocessOptions>,
81
- ): Promise<AdbSubprocessProtocol> {
82
- return this.#createProtocol("pty", command, options);
83
- }
84
-
85
- /**
86
- * Spawns an executable and redirect the standard input/output stream.
87
- *
88
- * Redirection mode is enough for most simple commands, but PTY mode is required for
89
- * commands that manipulate the terminal, such as `vi` and `less`.
90
- * @param command The command to run, or an array of strings containing both command and args.
91
- * @param options The options for creating the `AdbSubprocessProtocol`
92
- * @returns A new `AdbSubprocessProtocol` instance connecting to the spawned process.
93
- */
94
- spawn(
95
- command: string | string[],
96
- options?: Partial<AdbSubprocessOptions>,
97
- ): Promise<AdbSubprocessProtocol> {
98
- return this.#createProtocol("raw", command, options);
99
- }
100
-
101
- /**
102
- * Spawns a new process, waits until it exits, and returns the entire output.
103
- * @param command The command to run
104
- * @param options The options for creating the `AdbSubprocessProtocol`
105
- * @returns The entire output of the command
106
- */
107
- async spawnAndWait(
108
- command: string | string[],
109
- options?: Partial<AdbSubprocessOptions>,
110
- ): Promise<AdbSubprocessWaitResult> {
111
- const process = await this.spawn(command, options);
112
-
113
- const [stdout, stderr, exitCode] = await Promise.all([
114
- process.stdout
115
- .pipeThrough(new TextDecoderStream())
116
- .pipeThrough(new ConcatStringStream()),
117
- process.stderr
118
- .pipeThrough(new TextDecoderStream())
119
- .pipeThrough(new ConcatStringStream()),
120
- process.exit,
121
- ]);
122
-
123
- return {
124
- stdout,
125
- stderr,
126
- exitCode,
127
- };
128
- }
129
-
130
- /**
131
- * Spawns a new process, waits until it exits, and returns the entire output.
132
- * @param command The command to run
133
- * @returns The entire output of the command
134
- */
135
- async spawnAndWaitLegacy(command: string | string[]): Promise<string> {
136
- const { stdout } = await this.spawnAndWait(command, {
137
- protocols: [AdbSubprocessNoneProtocol],
138
- });
139
- return stdout;
140
- }
141
- }
@@ -1,3 +0,0 @@
1
- export * from "./none.js";
2
- export * from "./shell.js";
3
- export * from "./types.js";
@@ -1,81 +0,0 @@
1
- import type { MaybeConsumable, WritableStream } from "@yume-chan/stream-extra";
2
- import { ReadableStream } from "@yume-chan/stream-extra";
3
-
4
- import type { Adb, AdbSocket } from "../../../adb.js";
5
-
6
- import type { AdbSubprocessProtocol } from "./types.js";
7
-
8
- /**
9
- * The legacy shell
10
- *
11
- * Features:
12
- * * `stderr`: No
13
- * * `exit` exit code: No
14
- * * `resize`: No
15
- */
16
- export class AdbSubprocessNoneProtocol implements AdbSubprocessProtocol {
17
- static isSupported() {
18
- return true;
19
- }
20
-
21
- static async pty(adb: Adb, command: string) {
22
- return new AdbSubprocessNoneProtocol(
23
- await adb.createSocket(`shell:${command}`),
24
- );
25
- }
26
-
27
- static async raw(adb: Adb, command: string) {
28
- // `shell,raw:${command}` also triggers raw mode,
29
- // But is not supported on Android version <7.
30
- return new AdbSubprocessNoneProtocol(
31
- await adb.createSocket(`exec:${command}`),
32
- );
33
- }
34
-
35
- readonly #socket: AdbSocket;
36
-
37
- // Legacy shell forwards all data to stdin.
38
- get stdin(): WritableStream<MaybeConsumable<Uint8Array>> {
39
- return this.#socket.writable;
40
- }
41
-
42
- /**
43
- * Legacy shell mixes stdout and stderr.
44
- */
45
- get stdout(): ReadableStream<Uint8Array> {
46
- return this.#socket.readable;
47
- }
48
-
49
- #stderr: ReadableStream<Uint8Array>;
50
- /**
51
- * `stderr` will always be empty.
52
- */
53
- get stderr(): ReadableStream<Uint8Array> {
54
- return this.#stderr;
55
- }
56
-
57
- #exit: Promise<number>;
58
- get exit() {
59
- return this.#exit;
60
- }
61
-
62
- constructor(socket: AdbSocket) {
63
- this.#socket = socket;
64
-
65
- this.#stderr = new ReadableStream({
66
- start: async (controller) => {
67
- await this.#socket.closed;
68
- controller.close();
69
- },
70
- });
71
- this.#exit = socket.closed.then(() => 0);
72
- }
73
-
74
- resize() {
75
- // Not supported, but don't throw.
76
- }
77
-
78
- async kill() {
79
- await this.#socket.close();
80
- }
81
- }
@@ -1,171 +0,0 @@
1
- import { PromiseResolver } from "@yume-chan/async";
2
- import type {
3
- PushReadableStreamController,
4
- ReadableStream,
5
- WritableStreamDefaultWriter,
6
- } from "@yume-chan/stream-extra";
7
- import {
8
- MaybeConsumable,
9
- PushReadableStream,
10
- StructDeserializeStream,
11
- WritableStream,
12
- } from "@yume-chan/stream-extra";
13
- import type { StructValue } from "@yume-chan/struct";
14
- import { buffer, struct, u32, u8 } from "@yume-chan/struct";
15
-
16
- import type { Adb, AdbSocket } from "../../../adb.js";
17
- import { AdbFeature } from "../../../features.js";
18
- import { encodeUtf8 } from "../../../utils/index.js";
19
-
20
- import type { AdbSubprocessProtocol } from "./types.js";
21
-
22
- export const AdbShellProtocolId = {
23
- Stdin: 0,
24
- Stdout: 1,
25
- Stderr: 2,
26
- Exit: 3,
27
- CloseStdin: 4,
28
- WindowSizeChange: 5,
29
- } as const;
30
-
31
- export type AdbShellProtocolId =
32
- (typeof AdbShellProtocolId)[keyof typeof AdbShellProtocolId];
33
-
34
- // This packet format is used in both directions.
35
- export const AdbShellProtocolPacket = struct(
36
- {
37
- id: u8<AdbShellProtocolId>(),
38
- data: buffer(u32),
39
- },
40
- { littleEndian: true },
41
- );
42
-
43
- type AdbShellProtocolPacket = StructValue<typeof AdbShellProtocolPacket>;
44
-
45
- /**
46
- * Shell v2 a.k.a Shell Protocol
47
- *
48
- * Features:
49
- * * `stderr`: Yes
50
- * * `exit` exit code: Yes
51
- * * `resize`: Yes
52
- */
53
- export class AdbSubprocessShellProtocol implements AdbSubprocessProtocol {
54
- static isSupported(adb: Adb) {
55
- return adb.canUseFeature(AdbFeature.ShellV2);
56
- }
57
-
58
- static async pty(adb: Adb, command: string) {
59
- // TODO: AdbShellSubprocessProtocol: Support setting `XTERM` environment variable
60
- return new AdbSubprocessShellProtocol(
61
- await adb.createSocket(`shell,v2,pty:${command}`),
62
- );
63
- }
64
-
65
- static async raw(adb: Adb, command: string) {
66
- return new AdbSubprocessShellProtocol(
67
- await adb.createSocket(`shell,v2,raw:${command}`),
68
- );
69
- }
70
-
71
- readonly #socket: AdbSocket;
72
- #writer: WritableStreamDefaultWriter<MaybeConsumable<Uint8Array>>;
73
-
74
- #stdin: WritableStream<MaybeConsumable<Uint8Array>>;
75
- get stdin() {
76
- return this.#stdin;
77
- }
78
-
79
- #stdout: ReadableStream<Uint8Array>;
80
- get stdout() {
81
- return this.#stdout;
82
- }
83
-
84
- #stderr: ReadableStream<Uint8Array>;
85
- get stderr() {
86
- return this.#stderr;
87
- }
88
-
89
- readonly #exit = new PromiseResolver<number>();
90
- get exit() {
91
- return this.#exit.promise;
92
- }
93
-
94
- constructor(socket: AdbSocket) {
95
- this.#socket = socket;
96
-
97
- let stdoutController!: PushReadableStreamController<Uint8Array>;
98
- let stderrController!: PushReadableStreamController<Uint8Array>;
99
- this.#stdout = new PushReadableStream<Uint8Array>((controller) => {
100
- stdoutController = controller;
101
- });
102
- this.#stderr = new PushReadableStream<Uint8Array>((controller) => {
103
- stderrController = controller;
104
- });
105
-
106
- socket.readable
107
- .pipeThrough(new StructDeserializeStream(AdbShellProtocolPacket))
108
- .pipeTo(
109
- new WritableStream<AdbShellProtocolPacket>({
110
- write: async (chunk) => {
111
- switch (chunk.id) {
112
- case AdbShellProtocolId.Exit:
113
- this.#exit.resolve(chunk.data[0]!);
114
- break;
115
- case AdbShellProtocolId.Stdout:
116
- await stdoutController.enqueue(chunk.data);
117
- break;
118
- case AdbShellProtocolId.Stderr:
119
- await stderrController.enqueue(chunk.data);
120
- break;
121
- }
122
- },
123
- }),
124
- )
125
- .then(
126
- () => {
127
- stdoutController.close();
128
- stderrController.close();
129
- // If `#exit` has already resolved, this will be a no-op
130
- this.#exit.reject(
131
- new Error("Socket ended without exit message"),
132
- );
133
- },
134
- (e) => {
135
- stdoutController.error(e);
136
- stderrController.error(e);
137
- // If `#exit` has already resolved, this will be a no-op
138
- this.#exit.reject(e);
139
- },
140
- );
141
-
142
- this.#writer = this.#socket.writable.getWriter();
143
-
144
- this.#stdin = new MaybeConsumable.WritableStream<Uint8Array>({
145
- write: async (chunk) => {
146
- await this.#writer.write(
147
- AdbShellProtocolPacket.serialize({
148
- id: AdbShellProtocolId.Stdin,
149
- data: chunk,
150
- }),
151
- );
152
- },
153
- });
154
- }
155
-
156
- async resize(rows: number, cols: number) {
157
- await this.#writer.write(
158
- AdbShellProtocolPacket.serialize({
159
- id: AdbShellProtocolId.WindowSizeChange,
160
- // The "correct" format is `${rows}x${cols},${x_pixels}x${y_pixels}`
161
- // However, according to https://linux.die.net/man/4/tty_ioctl
162
- // `x_pixels` and `y_pixels` are unused, so always sending `0` should be fine.
163
- data: encodeUtf8(`${rows}x${cols},0x0\0`),
164
- }),
165
- );
166
- }
167
-
168
- kill() {
169
- return this.#socket.close();
170
- }
171
- }
@@ -1,63 +0,0 @@
1
- import type { MaybePromiseLike } from "@yume-chan/async";
2
- import type {
3
- MaybeConsumable,
4
- ReadableStream,
5
- WritableStream,
6
- } from "@yume-chan/stream-extra";
7
-
8
- import type { Adb, AdbSocket } from "../../../adb.js";
9
-
10
- export interface AdbSubprocessProtocol {
11
- /**
12
- * A WritableStream that writes to the `stdin` stream.
13
- */
14
- readonly stdin: WritableStream<MaybeConsumable<Uint8Array>>;
15
-
16
- /**
17
- * The `stdout` stream of the process.
18
- */
19
- readonly stdout: ReadableStream<Uint8Array>;
20
-
21
- /**
22
- * The `stderr` stream of the process.
23
- *
24
- * Note: Some `AdbSubprocessProtocol` doesn't separate `stdout` and `stderr`,
25
- * All output will be sent to `stdout`.
26
- */
27
- readonly stderr: ReadableStream<Uint8Array>;
28
-
29
- /**
30
- * A `Promise` that resolves to the exit code of the process.
31
- *
32
- * Note: Some `AdbSubprocessProtocol` doesn't support exit code,
33
- * They will always resolve it with `0`.
34
- */
35
- readonly exit: Promise<number>;
36
-
37
- /**
38
- * Resizes the current shell.
39
- *
40
- * Some `AdbSubprocessProtocol`s may not support resizing
41
- * and will ignore calls to this method.
42
- */
43
- resize(rows: number, cols: number): MaybePromiseLike<void>;
44
-
45
- /**
46
- * Kills the current process.
47
- */
48
- kill(): MaybePromiseLike<void>;
49
- }
50
-
51
- export interface AdbSubprocessProtocolConstructor {
52
- /** Returns `true` if the `adb` instance supports this shell */
53
- isSupported(adb: Adb): MaybePromiseLike<boolean>;
54
-
55
- /** Spawns an executable in PTY (interactive) mode. */
56
- pty(adb: Adb, command: string): MaybePromiseLike<AdbSubprocessProtocol>;
57
-
58
- /** Spawns an executable and pipe the output. */
59
- raw(adb: Adb, command: string): MaybePromiseLike<AdbSubprocessProtocol>;
60
-
61
- /** Creates a new `AdbShell` by attaching to an exist `AdbSocket` */
62
- new (socket: AdbSocket): AdbSubprocessProtocol;
63
- }