@yume-chan/adb 0.0.10 → 0.0.11

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 (318) hide show
  1. package/CHANGELOG.json +26 -0
  2. package/CHANGELOG.md +15 -1
  3. package/LICENSE +1 -1
  4. package/README.md +56 -43
  5. package/esm/adb.d.ts +48 -0
  6. package/esm/adb.d.ts.map +1 -0
  7. package/esm/adb.js +141 -146
  8. package/esm/adb.js.map +1 -1
  9. package/{dts → esm}/auth.d.ts +8 -8
  10. package/esm/auth.d.ts.map +1 -0
  11. package/esm/auth.js +68 -108
  12. package/esm/auth.js.map +1 -1
  13. package/esm/backend.d.ts +9 -0
  14. package/esm/backend.d.ts.map +1 -0
  15. package/{dts → esm}/commands/base.d.ts +1 -1
  16. package/esm/commands/base.d.ts.map +1 -0
  17. package/esm/commands/base.js +1 -0
  18. package/esm/commands/base.js.map +1 -1
  19. package/{dts → esm}/commands/framebuffer.d.ts +3 -3
  20. package/esm/commands/framebuffer.d.ts.map +1 -0
  21. package/esm/commands/framebuffer.js +15 -18
  22. package/esm/commands/framebuffer.js.map +1 -1
  23. package/esm/commands/index.d.ts +9 -0
  24. package/esm/commands/index.d.ts.map +1 -0
  25. package/esm/commands/index.js +8 -8
  26. package/esm/commands/index.js.map +1 -1
  27. package/esm/commands/install.d.ts +4 -0
  28. package/esm/commands/install.d.ts.map +1 -0
  29. package/esm/commands/install.js +21 -13
  30. package/esm/commands/install.js.map +1 -1
  31. package/esm/commands/power.d.ts +23 -0
  32. package/esm/commands/power.d.ts.map +1 -0
  33. package/esm/commands/power.js +45 -0
  34. package/esm/commands/power.js.map +1 -0
  35. package/{dts → esm}/commands/reverse.d.ts +3 -3
  36. package/esm/commands/reverse.d.ts.map +1 -0
  37. package/esm/commands/reverse.js +49 -60
  38. package/esm/commands/reverse.js.map +1 -1
  39. package/esm/commands/subprocess/index.d.ts +57 -0
  40. package/esm/commands/subprocess/index.d.ts.map +1 -0
  41. package/esm/commands/subprocess/index.js +83 -0
  42. package/esm/commands/subprocess/index.js.map +1 -0
  43. package/esm/commands/subprocess/legacy.d.ts +28 -0
  44. package/esm/commands/subprocess/legacy.d.ts.map +1 -0
  45. package/esm/commands/subprocess/legacy.js +44 -0
  46. package/esm/commands/subprocess/legacy.js.map +1 -0
  47. package/esm/commands/subprocess/protocol.d.ts +38 -0
  48. package/esm/commands/subprocess/protocol.d.ts.map +1 -0
  49. package/esm/commands/subprocess/protocol.js +155 -0
  50. package/esm/commands/subprocess/protocol.js.map +1 -0
  51. package/esm/commands/subprocess/types.d.ts +47 -0
  52. package/esm/commands/subprocess/types.d.ts.map +1 -0
  53. package/esm/commands/{shell → subprocess}/types.js +0 -0
  54. package/esm/commands/{shell → subprocess}/types.js.map +1 -1
  55. package/{dts/commands/shell → esm/commands/subprocess}/utils.d.ts +0 -0
  56. package/esm/commands/subprocess/utils.d.ts.map +1 -0
  57. package/esm/commands/{shell → subprocess}/utils.js +0 -0
  58. package/esm/commands/subprocess/utils.js.map +1 -0
  59. package/esm/commands/sync/index.d.ts +8 -0
  60. package/esm/commands/sync/index.d.ts.map +1 -0
  61. package/esm/commands/sync/index.js +7 -7
  62. package/esm/commands/sync/index.js.map +1 -1
  63. package/{dts → esm}/commands/sync/list.d.ts +4 -4
  64. package/esm/commands/sync/list.d.ts.map +1 -0
  65. package/esm/commands/sync/list.js +16 -19
  66. package/esm/commands/sync/list.js.map +1 -1
  67. package/esm/commands/sync/pull.d.ts +11 -0
  68. package/esm/commands/sync/pull.d.ts.map +1 -0
  69. package/esm/commands/sync/pull.js +17 -11
  70. package/esm/commands/sync/pull.js.map +1 -1
  71. package/esm/commands/sync/push.d.ts +8 -0
  72. package/esm/commands/sync/push.d.ts.map +1 -0
  73. package/esm/commands/sync/push.js +19 -37
  74. package/esm/commands/sync/push.js.map +1 -1
  75. package/{dts → esm}/commands/sync/request.d.ts +3 -3
  76. package/esm/commands/sync/request.d.ts.map +1 -0
  77. package/esm/commands/sync/request.js +24 -27
  78. package/esm/commands/sync/request.js.map +1 -1
  79. package/{dts → esm}/commands/sync/response.d.ts +2 -2
  80. package/esm/commands/sync/response.d.ts.map +1 -0
  81. package/esm/commands/sync/response.js +16 -20
  82. package/esm/commands/sync/response.js.map +1 -1
  83. package/{dts → esm}/commands/sync/stat.d.ts +4 -4
  84. package/esm/commands/sync/stat.d.ts.map +1 -0
  85. package/esm/commands/sync/stat.js +22 -27
  86. package/esm/commands/sync/stat.js.map +1 -1
  87. package/esm/commands/sync/sync.d.ts +83 -0
  88. package/esm/commands/sync/sync.d.ts.map +1 -0
  89. package/esm/commands/sync/sync.js +92 -89
  90. package/esm/commands/sync/sync.js.map +1 -1
  91. package/{dts → esm}/commands/tcpip.d.ts +1 -1
  92. package/{dts → esm}/commands/tcpip.d.ts.map +1 -1
  93. package/esm/commands/tcpip.js +14 -19
  94. package/esm/commands/tcpip.js.map +1 -1
  95. package/{dts → esm}/crypto.d.ts +7 -7
  96. package/esm/crypto.d.ts.map +1 -0
  97. package/esm/crypto.js +26 -29
  98. package/esm/crypto.js.map +1 -1
  99. package/{dts → esm}/features.d.ts +0 -0
  100. package/{dts → esm}/features.d.ts.map +0 -0
  101. package/esm/features.js +1 -1
  102. package/esm/features.js.map +1 -1
  103. package/esm/index.d.ts +11 -0
  104. package/esm/index.d.ts.map +1 -0
  105. package/esm/index.js +10 -10
  106. package/esm/index.js.map +1 -1
  107. package/esm/packet.d.ts +28 -0
  108. package/esm/packet.d.ts.map +1 -0
  109. package/esm/packet.js +26 -51
  110. package/esm/packet.js.map +1 -1
  111. package/esm/socket/controller.d.ts +37 -0
  112. package/esm/socket/controller.d.ts.map +1 -0
  113. package/esm/socket/controller.js +52 -66
  114. package/esm/socket/controller.js.map +1 -1
  115. package/{dts → esm}/socket/dispatcher.d.ts +10 -23
  116. package/esm/socket/dispatcher.d.ts.map +1 -0
  117. package/esm/socket/dispatcher.js +148 -164
  118. package/esm/socket/dispatcher.js.map +1 -1
  119. package/esm/socket/index.d.ts +3 -0
  120. package/esm/socket/index.d.ts.map +1 -0
  121. package/esm/socket/index.js +2 -6
  122. package/esm/socket/index.js.map +1 -1
  123. package/esm/socket/socket.d.ts +34 -0
  124. package/esm/socket/socket.d.ts.map +1 -0
  125. package/esm/socket/socket.js +69 -14
  126. package/esm/socket/socket.js.map +1 -1
  127. package/esm/stream/buffered.d.ts +35 -0
  128. package/esm/stream/buffered.d.ts.map +1 -0
  129. package/esm/stream/buffered.js +123 -0
  130. package/esm/stream/buffered.js.map +1 -0
  131. package/esm/stream/detect.bak.d.ts +240 -0
  132. package/esm/stream/detect.bak.d.ts.map +1 -0
  133. package/esm/stream/detect.bak.js +60 -0
  134. package/esm/stream/detect.bak.js.map +1 -0
  135. package/esm/stream/detect.d.ts +18 -0
  136. package/esm/stream/detect.d.ts.map +1 -0
  137. package/esm/stream/detect.js +6 -0
  138. package/esm/stream/detect.js.map +1 -0
  139. package/esm/stream/detect.native.d.ts +240 -0
  140. package/esm/stream/detect.native.d.ts.map +1 -0
  141. package/esm/stream/detect.native.js +60 -0
  142. package/esm/stream/detect.native.js.map +1 -0
  143. package/esm/stream/detect.polyfill.d.ts +18 -0
  144. package/esm/stream/detect.polyfill.d.ts.map +1 -0
  145. package/esm/stream/detect.polyfill.js +6 -0
  146. package/esm/stream/detect.polyfill.js.map +1 -0
  147. package/esm/stream/index.d.ts +4 -0
  148. package/esm/stream/index.d.ts.map +1 -0
  149. package/esm/stream/index.js +3 -2
  150. package/esm/stream/index.js.map +1 -1
  151. package/esm/stream/transform.d.ts +98 -0
  152. package/esm/stream/transform.d.ts.map +1 -0
  153. package/esm/stream/transform.js +352 -0
  154. package/esm/stream/transform.js.map +1 -0
  155. package/{dts → esm}/utils/auto-reset-event.d.ts +1 -1
  156. package/esm/utils/auto-reset-event.d.ts.map +1 -0
  157. package/esm/utils/auto-reset-event.js +2 -1
  158. package/esm/utils/auto-reset-event.js.map +1 -1
  159. package/esm/utils/base64.d.ts +5 -0
  160. package/esm/utils/base64.d.ts.map +1 -0
  161. package/esm/utils/base64.js +116 -80
  162. package/esm/utils/base64.js.map +1 -1
  163. package/esm/utils/encoding.d.ts +3 -0
  164. package/{dts → esm}/utils/encoding.d.ts.map +1 -1
  165. package/esm/utils/encoding.js +1 -1
  166. package/esm/utils/encoding.js.map +1 -1
  167. package/esm/utils/index.d.ts +4 -0
  168. package/esm/utils/index.d.ts.map +1 -0
  169. package/esm/utils/index.js +3 -5
  170. package/esm/utils/index.js.map +1 -1
  171. package/package.json +50 -44
  172. package/src/adb.ts +147 -143
  173. package/src/auth.ts +23 -19
  174. package/src/backend.ts +3 -12
  175. package/src/commands/base.ts +1 -1
  176. package/src/commands/framebuffer.ts +4 -4
  177. package/src/commands/index.ts +8 -8
  178. package/src/commands/install.ts +26 -15
  179. package/src/commands/power.ts +54 -0
  180. package/src/commands/reverse.ts +10 -8
  181. package/src/commands/subprocess/index.ts +124 -0
  182. package/src/commands/subprocess/legacy.ts +58 -0
  183. package/src/commands/subprocess/protocol.ts +185 -0
  184. package/src/commands/subprocess/types.ts +55 -0
  185. package/src/commands/{shell → subprocess}/utils.ts +0 -0
  186. package/src/commands/sync/index.ts +7 -7
  187. package/src/commands/sync/list.ts +7 -6
  188. package/src/commands/sync/pull.ts +27 -18
  189. package/src/commands/sync/push.ts +25 -30
  190. package/src/commands/sync/request.ts +7 -7
  191. package/src/commands/sync/response.ts +4 -4
  192. package/src/commands/sync/stat.ts +8 -6
  193. package/src/commands/sync/sync.ts +64 -32
  194. package/src/commands/tcpip.ts +3 -3
  195. package/src/crypto.ts +38 -43
  196. package/src/index.ts +11 -10
  197. package/src/packet.ts +37 -52
  198. package/src/socket/dispatcher.ts +90 -103
  199. package/src/socket/index.ts +2 -6
  200. package/src/socket/socket.ts +117 -16
  201. package/src/stream/buffered.ts +147 -0
  202. package/src/stream/detect.native.ts +362 -0
  203. package/src/stream/detect.polyfill.ts +25 -0
  204. package/src/stream/detect.ts +25 -0
  205. package/src/stream/index.ts +3 -2
  206. package/src/stream/transform.ts +427 -0
  207. package/src/utils/auto-reset-event.ts +1 -1
  208. package/src/utils/base64.ts +161 -129
  209. package/src/utils/encoding.ts +3 -3
  210. package/src/utils/index.ts +3 -5
  211. package/tsconfig.build.json +3 -0
  212. package/tsconfig.build.tsbuildinfo +1 -0
  213. package/tsconfig.test.json +10 -0
  214. package/dts/adb.d.ts +0 -46
  215. package/dts/adb.d.ts.map +0 -1
  216. package/dts/auth.d.ts.map +0 -1
  217. package/dts/backend.d.ts +0 -13
  218. package/dts/backend.d.ts.map +0 -1
  219. package/dts/commands/base.d.ts.map +0 -1
  220. package/dts/commands/demo-mode.d.ts +0 -41
  221. package/dts/commands/demo-mode.d.ts.map +0 -1
  222. package/dts/commands/framebuffer.d.ts.map +0 -1
  223. package/dts/commands/index.d.ts +0 -9
  224. package/dts/commands/index.d.ts.map +0 -1
  225. package/dts/commands/install.d.ts +0 -3
  226. package/dts/commands/install.d.ts.map +0 -1
  227. package/dts/commands/reverse.d.ts.map +0 -1
  228. package/dts/commands/shell/index.d.ts +0 -46
  229. package/dts/commands/shell/index.d.ts.map +0 -1
  230. package/dts/commands/shell/legacy.d.ts +0 -38
  231. package/dts/commands/shell/legacy.d.ts.map +0 -1
  232. package/dts/commands/shell/protocol.d.ts +0 -45
  233. package/dts/commands/shell/protocol.d.ts.map +0 -1
  234. package/dts/commands/shell/types.d.ts +0 -47
  235. package/dts/commands/shell/types.d.ts.map +0 -1
  236. package/dts/commands/shell/utils.d.ts.map +0 -1
  237. package/dts/commands/sync/index.d.ts +0 -8
  238. package/dts/commands/sync/index.d.ts.map +0 -1
  239. package/dts/commands/sync/list.d.ts.map +0 -1
  240. package/dts/commands/sync/pull.d.ts +0 -11
  241. package/dts/commands/sync/pull.d.ts.map +0 -1
  242. package/dts/commands/sync/push.d.ts +0 -8
  243. package/dts/commands/sync/push.d.ts.map +0 -1
  244. package/dts/commands/sync/request.d.ts.map +0 -1
  245. package/dts/commands/sync/response.d.ts.map +0 -1
  246. package/dts/commands/sync/stat.d.ts.map +0 -1
  247. package/dts/commands/sync/sync.d.ts +0 -68
  248. package/dts/commands/sync/sync.d.ts.map +0 -1
  249. package/dts/crypto.d.ts.map +0 -1
  250. package/dts/index.d.ts +0 -11
  251. package/dts/index.d.ts.map +0 -1
  252. package/dts/packet.d.ts +0 -27
  253. package/dts/packet.d.ts.map +0 -1
  254. package/dts/socket/close-event-emitter.d.ts +0 -8
  255. package/dts/socket/close-event-emitter.d.ts.map +0 -1
  256. package/dts/socket/controller.d.ts +0 -43
  257. package/dts/socket/controller.d.ts.map +0 -1
  258. package/dts/socket/data-event-emitter.d.ts +0 -10
  259. package/dts/socket/data-event-emitter.d.ts.map +0 -1
  260. package/dts/socket/dispatcher.d.ts.map +0 -1
  261. package/dts/socket/index.d.ts +0 -7
  262. package/dts/socket/index.d.ts.map +0 -1
  263. package/dts/socket/logger.d.ts +0 -9
  264. package/dts/socket/logger.d.ts.map +0 -1
  265. package/dts/socket/socket.d.ts +0 -22
  266. package/dts/socket/socket.d.ts.map +0 -1
  267. package/dts/stream/buffered-stream.d.ts +0 -28
  268. package/dts/stream/buffered-stream.d.ts.map +0 -1
  269. package/dts/stream/index.d.ts +0 -3
  270. package/dts/stream/index.d.ts.map +0 -1
  271. package/dts/stream/stream.d.ts +0 -15
  272. package/dts/stream/stream.d.ts.map +0 -1
  273. package/dts/utils/auto-reset-event.d.ts.map +0 -1
  274. package/dts/utils/base64.d.ts +0 -7
  275. package/dts/utils/base64.d.ts.map +0 -1
  276. package/dts/utils/chunk.d.ts +0 -3
  277. package/dts/utils/chunk.d.ts.map +0 -1
  278. package/dts/utils/encoding.d.ts +0 -3
  279. package/dts/utils/event-queue.d.ts +0 -35
  280. package/dts/utils/event-queue.d.ts.map +0 -1
  281. package/dts/utils/index.d.ts +0 -6
  282. package/dts/utils/index.d.ts.map +0 -1
  283. package/esm/commands/demo-mode.js +0 -170
  284. package/esm/commands/demo-mode.js.map +0 -1
  285. package/esm/commands/shell/index.js +0 -62
  286. package/esm/commands/shell/index.js.map +0 -1
  287. package/esm/commands/shell/legacy.js +0 -49
  288. package/esm/commands/shell/legacy.js.map +0 -1
  289. package/esm/commands/shell/protocol.js +0 -106
  290. package/esm/commands/shell/protocol.js.map +0 -1
  291. package/esm/commands/shell/utils.js.map +0 -1
  292. package/esm/socket/close-event-emitter.js +0 -28
  293. package/esm/socket/close-event-emitter.js.map +0 -1
  294. package/esm/socket/data-event-emitter.js +0 -25
  295. package/esm/socket/data-event-emitter.js.map +0 -1
  296. package/esm/socket/logger.js +0 -2
  297. package/esm/socket/logger.js.map +0 -1
  298. package/esm/stream/buffered-stream.js +0 -67
  299. package/esm/stream/buffered-stream.js.map +0 -1
  300. package/esm/stream/stream.js +0 -41
  301. package/esm/stream/stream.js.map +0 -1
  302. package/esm/utils/chunk.js +0 -70
  303. package/esm/utils/chunk.js.map +0 -1
  304. package/esm/utils/event-queue.js +0 -104
  305. package/esm/utils/event-queue.js.map +0 -1
  306. package/src/commands/demo-mode.ts +0 -160
  307. package/src/commands/shell/index.ts +0 -87
  308. package/src/commands/shell/legacy.ts +0 -59
  309. package/src/commands/shell/protocol.ts +0 -121
  310. package/src/commands/shell/types.ts +0 -55
  311. package/src/socket/close-event-emitter.ts +0 -28
  312. package/src/socket/controller.ts +0 -105
  313. package/src/socket/data-event-emitter.ts +0 -27
  314. package/src/socket/logger.ts +0 -12
  315. package/src/stream/buffered-stream.ts +0 -92
  316. package/src/stream/stream.ts +0 -50
  317. package/src/utils/chunk.ts +0 -70
  318. package/src/utils/event-queue.ts +0 -145
@@ -1,121 +0,0 @@
1
- import { EventEmitter } from "@yume-chan/event";
2
- import Struct, { placeholder } from "@yume-chan/struct";
3
- import type { Adb } from "../../adb";
4
- import { AdbFeatures } from "../../features";
5
- import type { AdbSocket } from "../../socket";
6
- import { AdbBufferedStream } from "../../stream";
7
- import { encodeUtf8 } from "../../utils";
8
- import type { AdbShell } from "./types";
9
-
10
- export enum AdbShellProtocolId {
11
- Stdin,
12
- Stdout,
13
- Stderr,
14
- Exit,
15
- CloseStdin,
16
- WindowSizeChange,
17
- }
18
-
19
- // This packet format is used in both direction.
20
- const AdbShellProtocolPacket = new Struct({ littleEndian: true })
21
- .uint8('id', placeholder<AdbShellProtocolId>())
22
- .uint32('length')
23
- .arrayBuffer('data', { lengthField: 'length' });
24
-
25
- function assertUnreachable(x: never): never {
26
- throw new Error("Unreachable");
27
- }
28
-
29
- /**
30
- * Shell v2 a.k.a Shell Protocol
31
- *
32
- * Features:
33
- * * `onStderr`: Yes
34
- * * `onExit` exit code: Yes
35
- * * `resize`: Yes
36
- */
37
- export class AdbShellProtocol implements AdbShell {
38
- public static isSupported(adb: Adb) {
39
- return adb.features!.includes(AdbFeatures.ShellV2);
40
- }
41
-
42
- public static async spawn(adb: Adb, command: string) {
43
- // TODO: the service string may support more parameters
44
- return new AdbShellProtocol(await adb.createSocket(`shell,v2,pty:${command}`));
45
- }
46
-
47
- private readonly stream: AdbBufferedStream;
48
-
49
- private readonly stdoutEvent = new EventEmitter<ArrayBuffer>();
50
- public get onStdout() { return this.stdoutEvent.event; }
51
-
52
- private readonly stderrEvent = new EventEmitter<ArrayBuffer>();
53
- public get onStderr() { return this.stderrEvent.event; }
54
-
55
- private readonly exitEvent = new EventEmitter<number>();
56
- public get onExit() { return this.exitEvent.event; }
57
-
58
- public constructor(socket: AdbSocket) {
59
- this.stream = new AdbBufferedStream(socket);
60
- this.readData();
61
- }
62
-
63
- private async readData() {
64
- while (true) {
65
- try {
66
- const packet = await AdbShellProtocolPacket.deserialize(this.stream);
67
- switch (packet.id) {
68
- case AdbShellProtocolId.Stdout:
69
- this.stdoutEvent.fire(packet.data);
70
- break;
71
- case AdbShellProtocolId.Stderr:
72
- this.stderrEvent.fire(packet.data);
73
- break;
74
- case AdbShellProtocolId.Exit:
75
- this.exitEvent.fire(new Uint8Array(packet.data)[0]);
76
- break;
77
- case AdbShellProtocolId.CloseStdin:
78
- case AdbShellProtocolId.Stdin:
79
- case AdbShellProtocolId.WindowSizeChange:
80
- // These ids are client-to-server
81
- throw new Error('unreachable');
82
- default:
83
- assertUnreachable(packet.id);
84
- }
85
- } catch {
86
- return;
87
- }
88
- }
89
- }
90
-
91
- public async write(data: ArrayBuffer) {
92
- this.stream.write(
93
- AdbShellProtocolPacket.serialize(
94
- {
95
- id: AdbShellProtocolId.Stdin,
96
- data,
97
- }
98
- )
99
- );
100
- }
101
-
102
- public async resize(rows: number, cols: number) {
103
- await this.stream.write(
104
- AdbShellProtocolPacket.serialize(
105
- {
106
- id: AdbShellProtocolId.WindowSizeChange,
107
- data: encodeUtf8(
108
- // The "correct" format is `${rows}x${cols},${x_pixels}x${y_pixels}`
109
- // However, according to https://linux.die.net/man/4/tty_ioctl
110
- // `x_pixels` and `y_pixels` are not used, so always pass `0` is fine.
111
- `${rows}x${cols},0x0\0`
112
- ),
113
- }
114
- )
115
- );
116
- }
117
-
118
- public kill() {
119
- return this.stream.close();
120
- }
121
- }
@@ -1,55 +0,0 @@
1
- import type { Event } from "@yume-chan/event";
2
- import type { ValueOrPromise } from "@yume-chan/struct";
3
- import type { Adb } from "../../adb";
4
- import type { AdbSocket } from "../../socket";
5
-
6
- export interface AdbShell {
7
- /**
8
- * Notifies that new data has been written into the `stdout` stream.
9
- */
10
- readonly onStdout: Event<ArrayBuffer>;
11
-
12
- /**
13
- * Notifies that new data has been written into the `stderr` stream.
14
- *
15
- * Some `AdbShell`s may not support separate output streams
16
- * and will always fire the `onStdout` event instead.
17
- */
18
- readonly onStderr: Event<ArrayBuffer>;
19
-
20
- /**
21
- * Notifies that the current process has exited.
22
- *
23
- * The event arg is the exit code.
24
- * Some `AdbShell`s may not support returning exit code and will always return `0` instead.
25
- */
26
- readonly onExit: Event<number>;
27
-
28
- /**
29
- * Writes raw PTY data into the `stdin` stream.
30
- */
31
- write(data: ArrayBuffer): Promise<void>;
32
-
33
- /**
34
- * Resizes the current shell.
35
- *
36
- * Some `AdbShell`s may not support resizing and will always ignore calls to this method.
37
- */
38
- resize(rows: number, cols: number): ValueOrPromise<void>;
39
-
40
- /**
41
- * Kills the current process.
42
- */
43
- kill(): ValueOrPromise<void>;
44
- }
45
-
46
- export interface AdbShellConstructor {
47
- /** Returns `true` if the `adb` instance supports this shell */
48
- isSupported(adb: Adb): ValueOrPromise<boolean>;
49
-
50
- /** Creates a new `AdbShell` using the specified `Adb` and `command` */
51
- spawn(adb: Adb, command: string): ValueOrPromise<AdbShell>;
52
-
53
- /** Creates a new `AdbShell` by attaching to an exist `AdbSocket` */
54
- new(socket: AdbSocket): AdbShell;
55
- }
@@ -1,28 +0,0 @@
1
- import { EventEmitter, EventListenerInfo, RemoveEventListener } from '@yume-chan/event';
2
-
3
- const NoopRemoveEventListener: RemoveEventListener = () => { };
4
- NoopRemoveEventListener.dispose = NoopRemoveEventListener;
5
-
6
- export class CloseEventEmitter extends EventEmitter<void, void>{
7
- private closed = false;
8
-
9
- protected override addEventListener(info: EventListenerInfo<void, void>) {
10
- if (this.closed) {
11
- info.listener.apply(info.thisArg, [undefined, ...info.args]);
12
- return NoopRemoveEventListener;
13
- } else {
14
- return super.addEventListener(info);
15
- }
16
- }
17
-
18
- public override fire() {
19
- super.fire();
20
- this.closed = true;
21
- this.listeners.length = 0;
22
- }
23
-
24
- public override dispose() {
25
- this.fire();
26
- super.dispose();
27
- }
28
- }
@@ -1,105 +0,0 @@
1
- import { AutoDisposable } from '@yume-chan/event';
2
- import { AdbBackend } from '../backend';
3
- import { AdbCommand } from '../packet';
4
- import { AutoResetEvent, chunkArrayLike } from '../utils';
5
- import { CloseEventEmitter } from './close-event-emitter';
6
- import { DataEventEmitter } from './data-event-emitter';
7
- import { AdbPacketDispatcher } from './dispatcher';
8
-
9
- export interface AdbSocketInfo {
10
- backend: AdbBackend;
11
-
12
- localId: number;
13
-
14
- remoteId: number;
15
-
16
- localCreated: boolean;
17
-
18
- serviceString: string;
19
- }
20
-
21
- export interface AdbSocketConstructionOptions {
22
- dispatcher: AdbPacketDispatcher;
23
-
24
- localId: number;
25
-
26
- remoteId: number;
27
-
28
- localCreated: boolean;
29
-
30
- serviceString: string;
31
- }
32
-
33
- export class AdbSocketController extends AutoDisposable implements AdbSocketInfo {
34
- private readonly writeChunkLock = this.addDisposable(new AutoResetEvent());
35
- private readonly writeLock = this.addDisposable(new AutoResetEvent());
36
-
37
- private readonly dispatcher!: AdbPacketDispatcher;
38
- public get backend() { return this.dispatcher.backend; }
39
-
40
- public readonly localId!: number;
41
- public readonly remoteId!: number;
42
- public readonly localCreated!: boolean;
43
- public readonly serviceString!: string;
44
-
45
- public readonly dataEvent = this.addDisposable(new DataEventEmitter<ArrayBuffer>());
46
-
47
- private _closed = false;
48
- public get closed() { return this._closed; }
49
-
50
- private readonly closeEvent = this.addDisposable(new CloseEventEmitter());
51
- public get onClose() { return this.closeEvent.event; }
52
-
53
- public constructor(options: AdbSocketConstructionOptions) {
54
- super();
55
- Object.assign(this, options);
56
- }
57
-
58
- private async writeChunk(data: ArrayBuffer): Promise<void> {
59
- try {
60
- // Wait for an ack packet
61
- await this.writeChunkLock.wait();
62
- } catch {
63
- // Lock has been disposed, which means the socket has been closed
64
- throw new Error('Can not write after closed');
65
- }
66
-
67
- await this.dispatcher.sendPacket(AdbCommand.Write, this.localId, this.remoteId, data);
68
- }
69
-
70
- public async write(data: ArrayBuffer): Promise<void> {
71
- try {
72
- // Keep write operations in order
73
- await this.writeLock.wait();
74
- } catch {
75
- // Lock has been disposed, which means the socket has been closed
76
- throw new Error('Can not write after closed');
77
- }
78
-
79
- for await (const chunk of chunkArrayLike(data, this.dispatcher.maxPayloadSize)) {
80
- await this.writeChunk(chunk);
81
- }
82
- this.writeLock.notify();
83
- }
84
-
85
- public ack() {
86
- this.writeChunkLock.notify();
87
- }
88
-
89
- public async close(): Promise<void> {
90
- if (!this._closed) {
91
- // Immediately cancel all pending writes
92
- this.writeLock.dispose();
93
- this.writeChunkLock.dispose();
94
-
95
- await this.dispatcher.sendPacket(AdbCommand.Close, this.localId, this.remoteId);
96
- this._closed = true;
97
- }
98
- }
99
-
100
- public override dispose() {
101
- this._closed = true;
102
- this.closeEvent.fire();
103
- super.dispose();
104
- }
105
- }
@@ -1,27 +0,0 @@
1
- import { PromiseResolver } from '@yume-chan/async';
2
- import { Event, EventEmitter, EventListenerInfo } from '@yume-chan/event';
3
- import { ValueOrPromise } from '@yume-chan/struct';
4
-
5
- export type AsyncEventResult = ValueOrPromise<void>;
6
-
7
- export type AsyncEvent<TEvent> = Event<TEvent, AsyncEventResult>;
8
-
9
- export class DataEventEmitter<TEvent> extends EventEmitter<TEvent, AsyncEventResult> {
10
- private dispatchLock = new PromiseResolver<void>();
11
-
12
- protected override addEventListener(info: EventListenerInfo<TEvent, AsyncEventResult>) {
13
- const remove = super.addEventListener(info);
14
- if (this.listeners.length === 1) {
15
- this.dispatchLock.resolve();
16
- }
17
- return remove;
18
- }
19
-
20
- public override async fire(e: TEvent) {
21
- await this.dispatchLock.promise;
22
-
23
- for (const info of this.listeners) {
24
- await info.listener.apply(info.thisArg, [e, ...info.args]);
25
- }
26
- }
27
- }
@@ -1,12 +0,0 @@
1
- import { AdbPacket, AdbPacketInit } from '../packet';
2
- import { AdbSocket } from './socket';
3
-
4
- export interface AdbLogger {
5
- onIncomingPacket?(packet: AdbPacket): void;
6
-
7
- onOutgoingPacket?(packet: AdbPacketInit): void;
8
-
9
- onSocketOpened?(socket: AdbSocket): void;
10
-
11
- onSocketClosed?(socket: AdbSocket): void;
12
- }
@@ -1,92 +0,0 @@
1
- import { StructAsyncDeserializeStream } from '@yume-chan/struct';
2
- import { AdbSocket, AdbSocketInfo } from '../socket';
3
- import { AdbSocketStream } from './stream';
4
-
5
- export interface Stream {
6
- /**
7
- * @param length A hint of how much data should be read.
8
- * @returns Data, which can be either more or less than `length`
9
- */
10
- read(length: number): ArrayBuffer | Promise<ArrayBuffer>;
11
-
12
- close?(): void;
13
- }
14
-
15
- export class BufferedStream<T extends Stream> {
16
- private buffer: Uint8Array | undefined;
17
-
18
- protected readonly stream: T;
19
-
20
- public constructor(stream: T) {
21
- this.stream = stream;
22
- }
23
-
24
- public async read(length: number): Promise<ArrayBuffer> {
25
- let array: Uint8Array;
26
- let index: number;
27
- if (this.buffer) {
28
- const buffer = this.buffer;
29
- if (buffer.byteLength > length) {
30
- this.buffer = buffer.subarray(length);
31
- return buffer.slice(0, length).buffer;
32
- }
33
-
34
- array = new Uint8Array(length);
35
- array.set(buffer);
36
- index = buffer.byteLength;
37
- this.buffer = undefined;
38
- } else {
39
- const buffer = await this.stream.read(length);
40
- if (buffer.byteLength === length) {
41
- return buffer;
42
- }
43
-
44
- if (buffer.byteLength > length) {
45
- this.buffer = new Uint8Array(buffer, length);
46
- return buffer.slice(0, length);
47
- }
48
-
49
- array = new Uint8Array(length);
50
- array.set(new Uint8Array(buffer), 0);
51
- index = buffer.byteLength;
52
- }
53
-
54
- while (index < length) {
55
- const left = length - index;
56
-
57
- const buffer = await this.stream.read(left);
58
- if (buffer.byteLength > left) {
59
- array.set(new Uint8Array(buffer, 0, left), index);
60
- this.buffer = new Uint8Array(buffer, left);
61
- return array.buffer;
62
- }
63
-
64
- array.set(new Uint8Array(buffer), index);
65
- index += buffer.byteLength;
66
- }
67
-
68
- return array.buffer;
69
- }
70
-
71
- public close() {
72
- this.stream.close?.();
73
- }
74
- }
75
-
76
- export class AdbBufferedStream
77
- extends BufferedStream<AdbSocketStream>
78
- implements AdbSocketInfo, StructAsyncDeserializeStream {
79
- public get backend() { return this.stream.backend; }
80
- public get localId() { return this.stream.localId; }
81
- public get remoteId() { return this.stream.remoteId; }
82
- public get localCreated() { return this.stream.localCreated; }
83
- public get serviceString() { return this.stream.serviceString; }
84
-
85
- public constructor(socket: AdbSocket) {
86
- super(new AdbSocketStream(socket));
87
- }
88
-
89
- public write(data: ArrayBuffer): Promise<void> {
90
- return this.stream.write(data);
91
- }
92
- }
@@ -1,50 +0,0 @@
1
- import { once } from '@yume-chan/event';
2
- import { ValueOrPromise } from '@yume-chan/struct';
3
- import { AdbSocket, AdbSocketInfo } from '../socket';
4
- import { EventQueue } from '../utils';
5
-
6
- export class AdbSocketStream implements AdbSocketInfo {
7
- private socket: AdbSocket;
8
-
9
- private queue: EventQueue<ArrayBuffer>;
10
-
11
- public get backend() { return this.socket.backend; }
12
- public get localId() { return this.socket.localId; }
13
- public get remoteId() { return this.socket.remoteId; }
14
- public get localCreated() { return this.socket.localCreated; }
15
- public get serviceString() { return this.socket.serviceString; }
16
-
17
- public constructor(socket: AdbSocket) {
18
- this.socket = socket;
19
-
20
- this.queue = new EventQueue<ArrayBuffer>({
21
- highWaterMark: 16 * 1024,
22
- });
23
-
24
- this.socket.onData((buffer): ValueOrPromise<void> => {
25
- if (!this.queue.enqueue(buffer, buffer.byteLength)) {
26
- return once(this.queue.onDrain);
27
- }
28
- });
29
-
30
- this.socket.onClose(() => {
31
- this.queue.end();
32
- });
33
- }
34
-
35
- public async read(): Promise<ArrayBuffer> {
36
- try {
37
- return await this.queue.dequeue();
38
- } catch {
39
- throw new Error('Can not read after AdbSocketStream has been closed');
40
- }
41
- }
42
-
43
- public write(data: ArrayBuffer): Promise<void> {
44
- return this.socket.write(data);
45
- }
46
-
47
- close(): void {
48
- this.socket.close();
49
- }
50
- }
@@ -1,70 +0,0 @@
1
- export function* chunkArrayLike(
2
- value: ArrayLike<number> | ArrayBufferLike,
3
- size: number
4
- ): Generator<ArrayBuffer, void, void> {
5
- if ('length' in value) {
6
- value = new Uint8Array(value).buffer;
7
- }
8
-
9
- if (value.byteLength <= size) {
10
- return yield value;
11
- }
12
-
13
- for (let i = 0; i < value.byteLength; i += size) {
14
- yield value.slice(i, i + size);
15
- }
16
- }
17
-
18
- export async function* chunkAsyncIterable(
19
- value: AsyncIterable<ArrayBuffer>,
20
- size: number
21
- ): AsyncGenerator<ArrayBuffer, void, void> {
22
- let result = new Uint8Array(size);
23
- let index = 0;
24
- for await (let buffer of value) {
25
- // `result` has some data, `result + buffer` is enough
26
- if (index !== 0 && index + buffer.byteLength >= size) {
27
- const remainder = size - index;
28
- result.set(new Uint8Array(buffer, 0, remainder), index);
29
- yield result.buffer;
30
-
31
- result = new Uint8Array(size);
32
- index = 0;
33
-
34
- if (buffer.byteLength > remainder) {
35
- // `buffer` still has some data
36
- buffer = buffer.slice(remainder);
37
- } else {
38
- continue;
39
- }
40
- }
41
-
42
- // `result` is empty, `buffer` alone is enough
43
- if (buffer.byteLength >= size) {
44
- let remainder = false;
45
- for (const chunk of chunkArrayLike(buffer, size)) {
46
- if (chunk.byteLength === size) {
47
- yield chunk;
48
- continue;
49
- }
50
-
51
- // `buffer` still has some data
52
- remainder = true;
53
- buffer = chunk;
54
- }
55
-
56
- if (!remainder) {
57
- continue;
58
- }
59
- }
60
-
61
- // `result` has some data but `result + buffer` is still not enough
62
- // or after previous steps `buffer` still has some data
63
- result.set(new Uint8Array(buffer), index);
64
- index += buffer.byteLength;
65
- }
66
-
67
- if (index !== 0) {
68
- yield result.buffer.slice(0, index);
69
- }
70
- }