@yume-chan/adb 0.0.10 → 0.0.13

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 (342) hide show
  1. package/CHANGELOG.json +50 -0
  2. package/CHANGELOG.md +29 -1
  3. package/LICENSE +1 -1
  4. package/README.md +71 -51
  5. package/esm/adb.d.ts +48 -0
  6. package/esm/adb.d.ts.map +1 -0
  7. package/esm/adb.js +149 -136
  8. package/esm/adb.js.map +1 -1
  9. package/{dts → esm}/auth.d.ts +11 -11
  10. package/esm/auth.d.ts.map +1 -0
  11. package/esm/auth.js +72 -110
  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 +19 -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 +56 -0
  40. package/esm/commands/subprocess/index.d.ts.map +1 -0
  41. package/esm/commands/subprocess/index.js +88 -0
  42. package/esm/commands/subprocess/index.js.map +1 -0
  43. package/esm/commands/subprocess/legacy.d.ts +35 -0
  44. package/esm/commands/subprocess/legacy.d.ts.map +1 -0
  45. package/esm/commands/subprocess/legacy.js +53 -0
  46. package/esm/commands/subprocess/legacy.js.map +1 -0
  47. package/esm/commands/subprocess/protocol.d.ts +39 -0
  48. package/esm/commands/subprocess/protocol.d.ts.map +1 -0
  49. package/esm/commands/subprocess/protocol.js +157 -0
  50. package/esm/commands/subprocess/protocol.js.map +1 -0
  51. package/esm/commands/subprocess/protocols/index.d.ts +4 -0
  52. package/esm/commands/subprocess/protocols/index.d.ts.map +1 -0
  53. package/esm/commands/subprocess/protocols/index.js +4 -0
  54. package/esm/commands/subprocess/protocols/index.js.map +1 -0
  55. package/esm/commands/subprocess/protocols/none.d.ts +35 -0
  56. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -0
  57. package/esm/commands/subprocess/protocols/none.js +53 -0
  58. package/esm/commands/subprocess/protocols/none.js.map +1 -0
  59. package/esm/commands/subprocess/protocols/shell.d.ts +39 -0
  60. package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -0
  61. package/esm/commands/subprocess/protocols/shell.js +155 -0
  62. package/esm/commands/subprocess/protocols/shell.js.map +1 -0
  63. package/esm/commands/subprocess/protocols/types.d.ts +50 -0
  64. package/esm/commands/subprocess/protocols/types.d.ts.map +1 -0
  65. package/esm/commands/{shell → subprocess/protocols}/types.js +0 -0
  66. package/esm/commands/subprocess/protocols/types.js.map +1 -0
  67. package/esm/commands/subprocess/types.d.ts +50 -0
  68. package/esm/commands/subprocess/types.d.ts.map +1 -0
  69. package/esm/commands/subprocess/types.js +2 -0
  70. package/esm/commands/{shell → subprocess}/types.js.map +1 -1
  71. package/{dts/commands/shell → esm/commands/subprocess}/utils.d.ts +0 -0
  72. package/esm/commands/subprocess/utils.d.ts.map +1 -0
  73. package/esm/commands/{shell → subprocess}/utils.js +0 -0
  74. package/esm/commands/subprocess/utils.js.map +1 -0
  75. package/esm/commands/sync/index.d.ts +8 -0
  76. package/esm/commands/sync/index.d.ts.map +1 -0
  77. package/esm/commands/sync/index.js +7 -7
  78. package/esm/commands/sync/index.js.map +1 -1
  79. package/esm/commands/sync/list.d.ts +41 -0
  80. package/esm/commands/sync/list.d.ts.map +1 -0
  81. package/esm/commands/sync/list.js +52 -20
  82. package/esm/commands/sync/list.js.map +1 -1
  83. package/esm/commands/sync/pull.d.ts +11 -0
  84. package/esm/commands/sync/pull.d.ts.map +1 -0
  85. package/esm/commands/sync/pull.js +17 -11
  86. package/esm/commands/sync/pull.js.map +1 -1
  87. package/esm/commands/sync/push.d.ts +8 -0
  88. package/esm/commands/sync/push.d.ts.map +1 -0
  89. package/esm/commands/sync/push.js +19 -37
  90. package/esm/commands/sync/push.js.map +1 -1
  91. package/{dts → esm}/commands/sync/request.d.ts +4 -3
  92. package/esm/commands/sync/request.d.ts.map +1 -0
  93. package/esm/commands/sync/request.js +25 -27
  94. package/esm/commands/sync/request.js.map +1 -1
  95. package/{dts → esm}/commands/sync/response.d.ts +4 -3
  96. package/esm/commands/sync/response.d.ts.map +1 -0
  97. package/esm/commands/sync/response.js +17 -20
  98. package/esm/commands/sync/response.js.map +1 -1
  99. package/{dts → esm}/commands/sync/stat.d.ts +16 -4
  100. package/esm/commands/sync/stat.d.ts.map +1 -0
  101. package/esm/commands/sync/stat.js +38 -30
  102. package/esm/commands/sync/stat.js.map +1 -1
  103. package/esm/commands/sync/sync.d.ts +61 -0
  104. package/esm/commands/sync/sync.d.ts.map +1 -0
  105. package/esm/commands/sync/sync.js +122 -89
  106. package/esm/commands/sync/sync.js.map +1 -1
  107. package/{dts → esm}/commands/tcpip.d.ts +1 -1
  108. package/{dts → esm}/commands/tcpip.d.ts.map +1 -1
  109. package/esm/commands/tcpip.js +14 -19
  110. package/esm/commands/tcpip.js.map +1 -1
  111. package/esm/crypto.d.ts +37 -0
  112. package/esm/crypto.d.ts.map +1 -0
  113. package/esm/crypto.js +77 -62
  114. package/esm/crypto.js.map +1 -1
  115. package/esm/features.d.ts +8 -0
  116. package/esm/features.d.ts.map +1 -0
  117. package/esm/features.js +7 -3
  118. package/esm/features.js.map +1 -1
  119. package/esm/index.d.ts +11 -0
  120. package/esm/index.d.ts.map +1 -0
  121. package/esm/index.js +10 -10
  122. package/esm/index.js.map +1 -1
  123. package/esm/packet.d.ts +47 -0
  124. package/esm/packet.d.ts.map +1 -0
  125. package/esm/packet.js +27 -52
  126. package/esm/packet.js.map +1 -1
  127. package/esm/socket/controller.d.ts +37 -0
  128. package/esm/socket/controller.d.ts.map +1 -0
  129. package/esm/socket/controller.js +52 -66
  130. package/esm/socket/controller.js.map +1 -1
  131. package/esm/socket/dispatcher.d.ts +44 -0
  132. package/esm/socket/dispatcher.d.ts.map +1 -0
  133. package/esm/socket/dispatcher.js +146 -165
  134. package/esm/socket/dispatcher.js.map +1 -1
  135. package/esm/socket/index.d.ts +3 -0
  136. package/esm/socket/index.d.ts.map +1 -0
  137. package/esm/socket/index.js +2 -6
  138. package/esm/socket/index.js.map +1 -1
  139. package/esm/socket/socket.d.ts +46 -0
  140. package/esm/socket/socket.d.ts.map +1 -0
  141. package/esm/socket/socket.js +77 -13
  142. package/esm/socket/socket.js.map +1 -1
  143. package/esm/stream/buffered.d.ts +35 -0
  144. package/esm/stream/buffered.d.ts.map +1 -0
  145. package/esm/stream/buffered.js +123 -0
  146. package/esm/stream/buffered.js.map +1 -0
  147. package/esm/stream/detect.bak.d.ts +240 -0
  148. package/esm/stream/detect.bak.d.ts.map +1 -0
  149. package/esm/stream/detect.bak.js +60 -0
  150. package/esm/stream/detect.bak.js.map +1 -0
  151. package/esm/stream/detect.d.ts +2 -0
  152. package/esm/stream/detect.d.ts.map +1 -0
  153. package/esm/stream/detect.js +5 -0
  154. package/esm/stream/detect.js.map +1 -0
  155. package/esm/stream/detect.native.d.ts +240 -0
  156. package/esm/stream/detect.native.d.ts.map +1 -0
  157. package/esm/stream/detect.native.js +60 -0
  158. package/esm/stream/detect.native.js.map +1 -0
  159. package/esm/stream/detect.polyfill.d.ts +18 -0
  160. package/esm/stream/detect.polyfill.d.ts.map +1 -0
  161. package/esm/stream/detect.polyfill.js +9 -0
  162. package/esm/stream/detect.polyfill.js.map +1 -0
  163. package/esm/stream/index.d.ts +4 -0
  164. package/esm/stream/index.d.ts.map +1 -0
  165. package/esm/stream/index.js +3 -2
  166. package/esm/stream/index.js.map +1 -1
  167. package/esm/stream/transform.d.ts +98 -0
  168. package/esm/stream/transform.d.ts.map +1 -0
  169. package/esm/stream/transform.js +402 -0
  170. package/esm/stream/transform.js.map +1 -0
  171. package/{dts → esm}/utils/auto-reset-event.d.ts +1 -1
  172. package/esm/utils/auto-reset-event.d.ts.map +1 -0
  173. package/esm/utils/auto-reset-event.js +2 -1
  174. package/esm/utils/auto-reset-event.js.map +1 -1
  175. package/esm/utils/base64.d.ts +5 -0
  176. package/esm/utils/base64.d.ts.map +1 -0
  177. package/esm/utils/base64.js +116 -80
  178. package/esm/utils/base64.js.map +1 -1
  179. package/esm/utils/encoding.d.ts +3 -0
  180. package/esm/utils/encoding.d.ts.map +1 -0
  181. package/esm/utils/encoding.js +1 -1
  182. package/esm/utils/encoding.js.map +1 -1
  183. package/esm/utils/index.d.ts +4 -0
  184. package/esm/utils/index.d.ts.map +1 -0
  185. package/esm/utils/index.js +3 -5
  186. package/esm/utils/index.js.map +1 -1
  187. package/package.json +50 -44
  188. package/src/adb.ts +177 -150
  189. package/src/auth.ts +37 -30
  190. package/src/backend.ts +3 -12
  191. package/src/commands/base.ts +1 -1
  192. package/src/commands/framebuffer.ts +4 -4
  193. package/src/commands/index.ts +8 -8
  194. package/src/commands/install.ts +24 -15
  195. package/src/commands/power.ts +54 -0
  196. package/src/commands/reverse.ts +10 -8
  197. package/src/commands/subprocess/index.ts +139 -0
  198. package/src/commands/subprocess/protocols/index.ts +3 -0
  199. package/src/commands/subprocess/protocols/none.ts +68 -0
  200. package/src/commands/subprocess/protocols/shell.ts +190 -0
  201. package/src/commands/subprocess/protocols/types.ts +59 -0
  202. package/src/commands/{shell → subprocess}/utils.ts +0 -0
  203. package/src/commands/sync/index.ts +7 -7
  204. package/src/commands/sync/list.ts +56 -9
  205. package/src/commands/sync/pull.ts +27 -18
  206. package/src/commands/sync/push.ts +25 -30
  207. package/src/commands/sync/request.ts +8 -7
  208. package/src/commands/sync/response.ts +12 -6
  209. package/src/commands/sync/stat.ts +43 -14
  210. package/src/commands/sync/sync.ts +102 -34
  211. package/src/commands/tcpip.ts +3 -3
  212. package/src/crypto.ts +95 -84
  213. package/src/features.ts +6 -2
  214. package/src/index.ts +11 -10
  215. package/src/packet.ts +49 -53
  216. package/src/socket/dispatcher.ts +110 -109
  217. package/src/socket/index.ts +2 -6
  218. package/src/socket/socket.ts +125 -17
  219. package/src/stream/buffered.ts +147 -0
  220. package/src/stream/detect.native.ts +362 -0
  221. package/src/stream/detect.polyfill.ts +28 -0
  222. package/src/stream/detect.ts +5 -0
  223. package/src/stream/index.ts +3 -2
  224. package/src/stream/transform.ts +483 -0
  225. package/src/utils/auto-reset-event.ts +1 -1
  226. package/src/utils/base64.ts +161 -129
  227. package/src/utils/index.ts +3 -5
  228. package/tsconfig.build.json +3 -0
  229. package/tsconfig.build.tsbuildinfo +1 -0
  230. package/tsconfig.test.json +10 -0
  231. package/dts/adb.d.ts +0 -46
  232. package/dts/adb.d.ts.map +0 -1
  233. package/dts/auth.d.ts.map +0 -1
  234. package/dts/backend.d.ts +0 -13
  235. package/dts/backend.d.ts.map +0 -1
  236. package/dts/commands/base.d.ts.map +0 -1
  237. package/dts/commands/demo-mode.d.ts +0 -41
  238. package/dts/commands/demo-mode.d.ts.map +0 -1
  239. package/dts/commands/framebuffer.d.ts.map +0 -1
  240. package/dts/commands/index.d.ts +0 -9
  241. package/dts/commands/index.d.ts.map +0 -1
  242. package/dts/commands/install.d.ts +0 -3
  243. package/dts/commands/install.d.ts.map +0 -1
  244. package/dts/commands/reverse.d.ts.map +0 -1
  245. package/dts/commands/shell/index.d.ts +0 -46
  246. package/dts/commands/shell/index.d.ts.map +0 -1
  247. package/dts/commands/shell/legacy.d.ts +0 -38
  248. package/dts/commands/shell/legacy.d.ts.map +0 -1
  249. package/dts/commands/shell/protocol.d.ts +0 -45
  250. package/dts/commands/shell/protocol.d.ts.map +0 -1
  251. package/dts/commands/shell/types.d.ts +0 -47
  252. package/dts/commands/shell/types.d.ts.map +0 -1
  253. package/dts/commands/shell/utils.d.ts.map +0 -1
  254. package/dts/commands/sync/index.d.ts +0 -8
  255. package/dts/commands/sync/index.d.ts.map +0 -1
  256. package/dts/commands/sync/list.d.ts +0 -17
  257. package/dts/commands/sync/list.d.ts.map +0 -1
  258. package/dts/commands/sync/pull.d.ts +0 -11
  259. package/dts/commands/sync/pull.d.ts.map +0 -1
  260. package/dts/commands/sync/push.d.ts +0 -8
  261. package/dts/commands/sync/push.d.ts.map +0 -1
  262. package/dts/commands/sync/request.d.ts.map +0 -1
  263. package/dts/commands/sync/response.d.ts.map +0 -1
  264. package/dts/commands/sync/stat.d.ts.map +0 -1
  265. package/dts/commands/sync/sync.d.ts +0 -68
  266. package/dts/commands/sync/sync.d.ts.map +0 -1
  267. package/dts/crypto.d.ts +0 -24
  268. package/dts/crypto.d.ts.map +0 -1
  269. package/dts/features.d.ts +0 -6
  270. package/dts/features.d.ts.map +0 -1
  271. package/dts/index.d.ts +0 -11
  272. package/dts/index.d.ts.map +0 -1
  273. package/dts/packet.d.ts +0 -27
  274. package/dts/packet.d.ts.map +0 -1
  275. package/dts/socket/close-event-emitter.d.ts +0 -8
  276. package/dts/socket/close-event-emitter.d.ts.map +0 -1
  277. package/dts/socket/controller.d.ts +0 -43
  278. package/dts/socket/controller.d.ts.map +0 -1
  279. package/dts/socket/data-event-emitter.d.ts +0 -10
  280. package/dts/socket/data-event-emitter.d.ts.map +0 -1
  281. package/dts/socket/dispatcher.d.ts +0 -53
  282. package/dts/socket/dispatcher.d.ts.map +0 -1
  283. package/dts/socket/index.d.ts +0 -7
  284. package/dts/socket/index.d.ts.map +0 -1
  285. package/dts/socket/logger.d.ts +0 -9
  286. package/dts/socket/logger.d.ts.map +0 -1
  287. package/dts/socket/socket.d.ts +0 -22
  288. package/dts/socket/socket.d.ts.map +0 -1
  289. package/dts/stream/buffered-stream.d.ts +0 -28
  290. package/dts/stream/buffered-stream.d.ts.map +0 -1
  291. package/dts/stream/index.d.ts +0 -3
  292. package/dts/stream/index.d.ts.map +0 -1
  293. package/dts/stream/stream.d.ts +0 -15
  294. package/dts/stream/stream.d.ts.map +0 -1
  295. package/dts/utils/auto-reset-event.d.ts.map +0 -1
  296. package/dts/utils/base64.d.ts +0 -7
  297. package/dts/utils/base64.d.ts.map +0 -1
  298. package/dts/utils/chunk.d.ts +0 -3
  299. package/dts/utils/chunk.d.ts.map +0 -1
  300. package/dts/utils/encoding.d.ts +0 -3
  301. package/dts/utils/encoding.d.ts.map +0 -1
  302. package/dts/utils/event-queue.d.ts +0 -35
  303. package/dts/utils/event-queue.d.ts.map +0 -1
  304. package/dts/utils/index.d.ts +0 -6
  305. package/dts/utils/index.d.ts.map +0 -1
  306. package/esm/commands/demo-mode.js +0 -170
  307. package/esm/commands/demo-mode.js.map +0 -1
  308. package/esm/commands/shell/index.js +0 -62
  309. package/esm/commands/shell/index.js.map +0 -1
  310. package/esm/commands/shell/legacy.js +0 -49
  311. package/esm/commands/shell/legacy.js.map +0 -1
  312. package/esm/commands/shell/protocol.js +0 -106
  313. package/esm/commands/shell/protocol.js.map +0 -1
  314. package/esm/commands/shell/utils.js.map +0 -1
  315. package/esm/socket/close-event-emitter.js +0 -28
  316. package/esm/socket/close-event-emitter.js.map +0 -1
  317. package/esm/socket/data-event-emitter.js +0 -25
  318. package/esm/socket/data-event-emitter.js.map +0 -1
  319. package/esm/socket/logger.js +0 -2
  320. package/esm/socket/logger.js.map +0 -1
  321. package/esm/stream/buffered-stream.js +0 -67
  322. package/esm/stream/buffered-stream.js.map +0 -1
  323. package/esm/stream/stream.js +0 -41
  324. package/esm/stream/stream.js.map +0 -1
  325. package/esm/utils/chunk.js +0 -70
  326. package/esm/utils/chunk.js.map +0 -1
  327. package/esm/utils/event-queue.js +0 -104
  328. package/esm/utils/event-queue.js.map +0 -1
  329. package/src/commands/demo-mode.ts +0 -160
  330. package/src/commands/shell/index.ts +0 -87
  331. package/src/commands/shell/legacy.ts +0 -59
  332. package/src/commands/shell/protocol.ts +0 -121
  333. package/src/commands/shell/types.ts +0 -55
  334. package/src/socket/close-event-emitter.ts +0 -28
  335. package/src/socket/controller.ts +0 -105
  336. package/src/socket/data-event-emitter.ts +0 -27
  337. package/src/socket/logger.ts +0 -12
  338. package/src/stream/buffered-stream.ts +0 -92
  339. package/src/stream/stream.ts +0 -50
  340. package/src/utils/chunk.ts +0 -70
  341. package/src/utils/encoding.ts +0 -13
  342. package/src/utils/event-queue.ts +0 -145
@@ -1,21 +1,30 @@
1
- import { Adb } from "../adb";
2
- import { escapeArg } from "./shell";
1
+ import type { Adb } from "../adb.js";
2
+ import { WrapWritableStream, WritableStream } from "../stream/index.js";
3
+ import { escapeArg } from "./subprocess/index.js";
4
+ import type { AdbSync } from "./sync/index.js";
3
5
 
4
- export async function install(
6
+ export function install(
5
7
  adb: Adb,
6
- apk: ArrayLike<number> | ArrayBufferLike | AsyncIterable<ArrayBuffer>,
7
- onProgress?: (uploaded: number) => void,
8
- ): Promise<void> {
9
- const filename = `/data/local/tmp/${Math.random().toString().substr(2)}.apk`;
8
+ ): WritableStream<Uint8Array> {
9
+ const filename = `/data/local/tmp/${Math.random().toString().substring(2)}.apk`;
10
10
 
11
- // Upload apk file to tmp folder
12
- const sync = await adb.sync();
13
- await sync.write(filename, apk, undefined, undefined, onProgress);
14
- sync.dispose();
11
+ let sync!: AdbSync;
12
+ return new WrapWritableStream<Uint8Array>({
13
+ async start() {
14
+ // TODO: install: support other install apk methods (streaming, etc.)
15
15
 
16
- // Invoke `pm install` to install it
17
- await adb.childProcess.exec('pm', 'install', escapeArg(filename));
16
+ // Upload apk file to tmp folder
17
+ sync = await adb.sync();
18
+ return sync.write(filename, undefined, undefined);
19
+ },
20
+ async close() {
21
+ sync.dispose();
18
22
 
19
- // Remove the temp file
20
- await adb.rm(filename);
23
+ // Invoke `pm install` to install it
24
+ await adb.subprocess.spawnAndWaitLegacy(['pm', 'install', escapeArg(filename)]);
25
+
26
+ // Remove the temp file
27
+ await adb.rm(filename);
28
+ }
29
+ });
21
30
  }
@@ -0,0 +1,54 @@
1
+ // cspell: ignore bootloader
2
+ // cspell: ignore fastboot
3
+ // cspell: ignore keyevent
4
+ // cspell: ignore longpress
5
+
6
+ import { AdbCommandBase } from "./base.js";
7
+
8
+ export class AdbPower extends AdbCommandBase {
9
+ public reboot(name: string = '') {
10
+ return this.adb.createSocketAndWait(`reboot:${name}`);
11
+ }
12
+
13
+ public bootloader() {
14
+ return this.reboot('bootloader');
15
+ }
16
+
17
+ public fastboot() {
18
+ return this.reboot('fastboot');
19
+ }
20
+
21
+ public recovery() {
22
+ return this.reboot('recovery');
23
+ }
24
+
25
+ public sideload() {
26
+ return this.reboot('sideload');
27
+ }
28
+
29
+ /**
30
+ * Reboot to Qualcomm Emergency Download (EDL) Mode.
31
+ *
32
+ * Only works on some Qualcomm devices.
33
+ */
34
+ public qualcommEdlMode() {
35
+ return this.reboot('edl');
36
+ }
37
+
38
+ public powerOff() {
39
+ return this.adb.subprocess.spawnAndWaitLegacy(['reboot', '-p']);
40
+ }
41
+
42
+ public powerButton(longPress: boolean = false) {
43
+ return this.adb.subprocess.spawnAndWaitLegacy(['input', 'keyevent', longPress ? '--longpress POWER' : 'POWER']);
44
+ }
45
+
46
+ /**
47
+ * Reboot to Samsung Odin download mode.
48
+ *
49
+ * Only works on Samsung devices.
50
+ */
51
+ public samsungOdin() {
52
+ return this.reboot('download');
53
+ }
54
+ }
@@ -1,12 +1,14 @@
1
+ // cspell: ignore killforward
2
+
1
3
  import { AutoDisposable } from '@yume-chan/event';
2
4
  import Struct from '@yume-chan/struct';
3
- import type { AdbPacket } from '../packet';
4
- import type { AdbIncomingSocketEventArgs, AdbPacketDispatcher, AdbSocket } from '../socket';
5
- import { AdbBufferedStream } from '../stream';
6
- import { decodeUtf8 } from "../utils";
5
+ import type { AdbPacketData } from '../packet.js';
6
+ import type { AdbIncomingSocketEventArgs, AdbPacketDispatcher, AdbSocket } from '../socket/index.js';
7
+ import { AdbBufferedStream } from '../stream/index.js';
8
+ import { decodeUtf8 } from "../utils/index.js";
7
9
 
8
10
  export interface AdbReverseHandler {
9
- onSocket(packet: AdbPacket, socket: AdbSocket): void;
11
+ onSocket(packet: AdbPacketData, socket: AdbSocket): void;
10
12
  }
11
13
 
12
14
  export interface AdbForwardListener {
@@ -50,8 +52,8 @@ export class AdbReverseCommand extends AutoDisposable {
50
52
  return;
51
53
  }
52
54
 
53
- const address = decodeUtf8(e.packet.payload!);
54
- // tcp:1234\0
55
+ const address = decodeUtf8(e.packet.payload);
56
+ // Address format: `tcp:12345\0`
55
57
  const port = Number.parseInt(address.substring(4));
56
58
  if (this.localPortToHandler.has(port)) {
57
59
  this.localPortToHandler.get(port)!.onSocket(e.packet, e.socket);
@@ -78,7 +80,7 @@ export class AdbReverseCommand extends AutoDisposable {
78
80
 
79
81
  const response = await AdbReverseStringResponse.deserialize(stream);
80
82
  return response.content!.split('\n').map(line => {
81
- const [deviceSerial, localName, remoteName] = line.split(' ');
83
+ const [deviceSerial, localName, remoteName] = line.split(' ') as [string, string, string];
82
84
  return { deviceSerial, localName, remoteName };
83
85
  });
84
86
 
@@ -0,0 +1,139 @@
1
+ import type { Adb } from '../../adb.js';
2
+ import { DecodeUtf8Stream, GatherStringStream } from "../../stream/index.js";
3
+ import { AdbSubprocessNoneProtocol, AdbSubprocessShellProtocol, type AdbSubprocessProtocol, type AdbSubprocessProtocolConstructor } from './protocols/index.js';
4
+
5
+ export * from './protocols/index.js';
6
+ export * from './utils.js';
7
+
8
+ export interface AdbSubprocessOptions {
9
+ /**
10
+ * A list of `AdbSubprocessProtocolConstructor`s to be used.
11
+ *
12
+ * Different `AdbSubprocessProtocol` has different capabilities, thus requires specific adaptations.
13
+ * Check their documentations for details.
14
+ *
15
+ * The first protocol whose `isSupported` returns `true` will be used.
16
+ * If no `AdbSubprocessProtocol` is supported, an error will be thrown.
17
+ *
18
+ * @default [AdbSubprocessShellProtocol, AdbSubprocessNoneProtocol]
19
+ */
20
+ protocols: AdbSubprocessProtocolConstructor[];
21
+ }
22
+
23
+ const DEFAULT_OPTIONS: AdbSubprocessOptions = {
24
+ protocols: [AdbSubprocessShellProtocol, AdbSubprocessNoneProtocol],
25
+ };
26
+
27
+ export interface AdbSubprocessWaitResult {
28
+ stdout: string;
29
+ stderr: string;
30
+ exitCode: number;
31
+ }
32
+
33
+ export class AdbSubprocess {
34
+ public readonly adb: Adb;
35
+
36
+ public constructor(adb: Adb) {
37
+ this.adb = adb;
38
+ }
39
+
40
+ private 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 (interactive) mode.
71
+ * @param command The command to run. If omitted, the default shell will be spawned.
72
+ * @param options The options for creating the `AdbSubprocessProtocol`
73
+ * @returns A new `AdbSubprocessProtocol` instance connecting to the spawned process.
74
+ */
75
+ public shell(
76
+ command?: string | string[],
77
+ options?: Partial<AdbSubprocessOptions>
78
+ ): Promise<AdbSubprocessProtocol> {
79
+ return this.createProtocol('pty', command, options);
80
+ }
81
+
82
+ /**
83
+ * Spawns an executable and pipe the output.
84
+ * @param command The command to run, or an array of strings containing both command and args.
85
+ * @param options The options for creating the `AdbSubprocessProtocol`
86
+ * @returns A new `AdbSubprocessProtocol` instance connecting to the spawned process.
87
+ */
88
+ public spawn(
89
+ command: string | string[],
90
+ options?: Partial<AdbSubprocessOptions>
91
+ ): Promise<AdbSubprocessProtocol> {
92
+ return this.createProtocol('raw', command, options);
93
+ }
94
+
95
+ /**
96
+ * Spawns a new process, waits until it exits, and returns the entire output.
97
+ * @param command The command to run
98
+ * @param options The options for creating the `AdbSubprocessProtocol`
99
+ * @returns The entire output of the command
100
+ */
101
+ public async spawnAndWait(
102
+ command: string | string[],
103
+ options?: Partial<AdbSubprocessOptions>
104
+ ): Promise<AdbSubprocessWaitResult> {
105
+ const shell = await this.spawn(command, options);
106
+
107
+ const stdout = new GatherStringStream();
108
+ const stderr = new GatherStringStream();
109
+
110
+ const [, , exitCode] = await Promise.all([
111
+ shell.stdout
112
+ .pipeThrough(new DecodeUtf8Stream())
113
+ .pipeTo(stdout),
114
+ shell.stderr
115
+ .pipeThrough(new DecodeUtf8Stream())
116
+ .pipeTo(stderr),
117
+ shell.exit
118
+ ]);
119
+
120
+ return {
121
+ stdout: stdout.result,
122
+ stderr: stderr.result,
123
+ exitCode,
124
+ };
125
+ }
126
+
127
+ /**
128
+ * Spawns a new process, waits until it exits, and returns the entire output.
129
+ * @param command The command to run
130
+ * @returns The entire output of the command
131
+ */
132
+ public async spawnAndWaitLegacy(command: string | string[]): Promise<string> {
133
+ const { stdout } = await this.spawnAndWait(
134
+ command,
135
+ { protocols: [AdbSubprocessNoneProtocol] }
136
+ );
137
+ return stdout;
138
+ }
139
+ }
@@ -0,0 +1,3 @@
1
+ export * from './none.js';
2
+ export * from './shell.js';
3
+ export * from './types.js';
@@ -0,0 +1,68 @@
1
+ import type { Adb } from "../../../adb.js";
2
+ import type { AdbSocket } from "../../../socket/index.js";
3
+ import { DuplexStreamFactory, type ReadableStream } from "../../../stream/index.js";
4
+ import type { AdbSubprocessProtocol } from "./types.js";
5
+
6
+ /**
7
+ * The legacy shell
8
+ *
9
+ * Features:
10
+ * * `stderr`: No
11
+ * * `exit` exit code: No
12
+ * * `resize`: No
13
+ */
14
+ export class AdbSubprocessNoneProtocol implements AdbSubprocessProtocol {
15
+ public static isSupported() { return true; }
16
+
17
+ public static async pty(adb: Adb, command: string) {
18
+ return new AdbSubprocessNoneProtocol(await adb.createSocket(`shell:${command}`));
19
+ }
20
+
21
+ public static async raw(adb: Adb, command: string) {
22
+ // Native ADB client doesn't allow none protocol + raw mode,
23
+ // But ADB daemon supports it.
24
+ return new AdbSubprocessNoneProtocol(await adb.createSocket(`shell,raw:${command}`));
25
+ }
26
+
27
+ private readonly socket: AdbSocket;
28
+
29
+ // Legacy shell forwards all data to stdin.
30
+ public get stdin() { return this.socket.writable; }
31
+
32
+ private _stdout: ReadableStream<Uint8Array>;
33
+ /**
34
+ * Legacy shell mixes stdout and stderr.
35
+ */
36
+ public get stdout() { return this._stdout; }
37
+
38
+ private _stderr: ReadableStream<Uint8Array>;
39
+ /**
40
+ * `stderr` will always be empty.
41
+ */
42
+ public get stderr() { return this._stderr; }
43
+
44
+ private _exit: Promise<number>;
45
+ public get exit() { return this._exit; }
46
+
47
+ public constructor(socket: AdbSocket) {
48
+ this.socket = socket;
49
+
50
+ const factory = new DuplexStreamFactory<Uint8Array, Uint8Array>({
51
+ close: async () => {
52
+ await this.socket.close();
53
+ },
54
+ });
55
+
56
+ this._stdout = factory.createWrapReadable(this.socket.readable);
57
+ this._stderr = factory.createReadable();
58
+ this._exit = factory.closed.then(() => 0);
59
+ }
60
+
61
+ public resize() {
62
+ // Not supported
63
+ }
64
+
65
+ public kill() {
66
+ return this.socket.close();
67
+ }
68
+ }
@@ -0,0 +1,190 @@
1
+ import { PromiseResolver } from "@yume-chan/async";
2
+ import Struct, { placeholder, type StructValueType } from "@yume-chan/struct";
3
+ import type { Adb } from "../../../adb.js";
4
+ import { AdbFeatures } from "../../../features.js";
5
+ import type { AdbSocket } from "../../../socket/index.js";
6
+ import { pipeFrom, PushReadableStream, ReadableStream, StructDeserializeStream, StructSerializeStream, TransformStream, WritableStream, WritableStreamDefaultWriter, type PushReadableStreamController } from "../../../stream/index.js";
7
+ import { encodeUtf8 } from "../../../utils/index.js";
8
+ import type { AdbSubprocessProtocol } from "./types.js";
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 =
21
+ new Struct({ littleEndian: true })
22
+ .uint8('id', placeholder<AdbShellProtocolId>())
23
+ .uint32('length')
24
+ .uint8Array('data', { lengthField: 'length' });
25
+
26
+ type AdbShellProtocolPacketInit = typeof AdbShellProtocolPacket['TInit'];
27
+
28
+ type AdbShellProtocolPacket = StructValueType<typeof AdbShellProtocolPacket>;
29
+
30
+ class StdinSerializeStream extends TransformStream<Uint8Array, AdbShellProtocolPacketInit>{
31
+ constructor() {
32
+ super({
33
+ transform(chunk, controller) {
34
+ controller.enqueue({
35
+ id: AdbShellProtocolId.Stdin,
36
+ data: chunk,
37
+ });
38
+ },
39
+ flush() {
40
+ // TODO: AdbShellSubprocessProtocol: support closing stdin
41
+ }
42
+ });
43
+ }
44
+ }
45
+
46
+ class StdoutDeserializeStream extends TransformStream<AdbShellProtocolPacket, Uint8Array>{
47
+ constructor(type: AdbShellProtocolId.Stdout | AdbShellProtocolId.Stderr) {
48
+ super({
49
+ transform(chunk, controller) {
50
+ if (chunk.id === type) {
51
+ controller.enqueue(chunk.data);
52
+ }
53
+ },
54
+ });
55
+ }
56
+ }
57
+
58
+ class MultiplexStream<T>{
59
+ private _readable: PushReadableStream<T>;
60
+ private _readableController!: PushReadableStreamController<T>;
61
+ public get readable() { return this._readable; }
62
+
63
+ private _activeCount = 0;
64
+
65
+ constructor() {
66
+ this._readable = new PushReadableStream(controller => {
67
+ this._readableController = controller;
68
+ });
69
+ }
70
+
71
+ public createWriteable() {
72
+ return new WritableStream<T>({
73
+ start: () => {
74
+ this._activeCount += 1;
75
+ },
76
+ write: async (chunk) => {
77
+ await this._readableController.enqueue(chunk);
78
+ },
79
+ abort: async (e) => {
80
+ this._activeCount -= 1;
81
+ if (this._activeCount === 0) {
82
+ this._readableController.close();
83
+ }
84
+ },
85
+ close: async () => {
86
+ this._activeCount -= 1;
87
+ if (this._activeCount === 0) {
88
+ this._readableController.close();
89
+ }
90
+ },
91
+ });
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Shell v2 a.k.a Shell Protocol
97
+ *
98
+ * Features:
99
+ * * `stderr`: Yes
100
+ * * `exit` exit code: Yes
101
+ * * `resize`: Yes
102
+ */
103
+ export class AdbSubprocessShellProtocol implements AdbSubprocessProtocol {
104
+ public static isSupported(adb: Adb) {
105
+ return adb.features!.includes(AdbFeatures.ShellV2);
106
+ }
107
+
108
+ public static async pty(adb: Adb, command: string) {
109
+ // TODO: AdbShellSubprocessProtocol: Support setting `XTERM` environment variable
110
+ return new AdbSubprocessShellProtocol(await adb.createSocket(`shell,v2,pty:${command}`));
111
+ }
112
+
113
+ public static async raw(adb: Adb, command: string) {
114
+ return new AdbSubprocessShellProtocol(await adb.createSocket(`shell,v2,raw:${command}`));
115
+ }
116
+
117
+ private readonly _socket: AdbSocket;
118
+ private _socketWriter: WritableStreamDefaultWriter<AdbShellProtocolPacketInit>;
119
+
120
+ private _stdin: WritableStream<Uint8Array>;
121
+ public get stdin() { return this._stdin; }
122
+
123
+ private _stdout: ReadableStream<Uint8Array>;
124
+ public get stdout() { return this._stdout; }
125
+
126
+ private _stderr: ReadableStream<Uint8Array>;
127
+ public get stderr() { return this._stderr; }
128
+
129
+ private readonly _exit = new PromiseResolver<number>();
130
+ public get exit() { return this._exit.promise; }
131
+
132
+ public constructor(socket: AdbSocket) {
133
+ this._socket = socket;
134
+
135
+ // Check this image to help you understand the stream graph
136
+ // cspell: disable-next-line
137
+ // https://www.plantuml.com/plantuml/png/bL91QiCm4Bpx5SAdv90lb1JISmiw5XzaQKf5PIkiLZIqzEyLSg8ks13gYtOykpFhiOw93N6UGjVDqK7rZsxKqNw0U_NTgVAy4empOy2mm4_olC0VEVEE47GUpnGjKdgXoD76q4GIEpyFhOwP_m28hW0NNzxNUig1_JdW0bA7muFIJDco1daJ_1SAX9bgvoPJPyIkSekhNYctvIGXrCH6tIsPL5fs-s6J5yc9BpWXhKtNdF2LgVYPGM_6GlMwfhWUsIt4lbScANrwlgVVUifPSVi__t44qStnwPvZwobdSmHHlL57p2vFuHS0
138
+
139
+ // TODO: AdbShellSubprocessProtocol: Optimize stream graph
140
+
141
+ const [stdout, stderr] = socket.readable
142
+ .pipeThrough(new StructDeserializeStream(AdbShellProtocolPacket))
143
+ .pipeThrough(new TransformStream<AdbShellProtocolPacket, AdbShellProtocolPacket>({
144
+ transform: (chunk, controller) => {
145
+ if (chunk.id === AdbShellProtocolId.Exit) {
146
+ this._exit.resolve(new Uint8Array(chunk.data)[0]!);
147
+ // We can let `StdoutDeserializeStream` to process `AdbShellProtocolId.Exit`,
148
+ // but since we need this `TransformStream` to capture the exit code anyway,
149
+ // terminating child streams here is killing two birds with one stone.
150
+ controller.terminate();
151
+ return;
152
+ }
153
+ controller.enqueue(chunk);
154
+ }
155
+ }))
156
+ .tee();
157
+ this._stdout = stdout
158
+ .pipeThrough(new StdoutDeserializeStream(AdbShellProtocolId.Stdout));
159
+ this._stderr = stderr
160
+ .pipeThrough(new StdoutDeserializeStream(AdbShellProtocolId.Stderr));
161
+
162
+ const multiplexer = new MultiplexStream<AdbShellProtocolPacketInit>();
163
+ multiplexer.readable
164
+ .pipeThrough(new StructSerializeStream(AdbShellProtocolPacket))
165
+ .pipeTo(socket.writable);
166
+
167
+ this._stdin = pipeFrom(
168
+ multiplexer.createWriteable(),
169
+ new StdinSerializeStream()
170
+ );
171
+
172
+ this._socketWriter = multiplexer.createWriteable().getWriter();
173
+ }
174
+
175
+ public async resize(rows: number, cols: number) {
176
+ await this._socketWriter.write({
177
+ id: AdbShellProtocolId.WindowSizeChange,
178
+ data: encodeUtf8(
179
+ // The "correct" format is `${rows}x${cols},${x_pixels}x${y_pixels}`
180
+ // However, according to https://linux.die.net/man/4/tty_ioctl
181
+ // `x_pixels` and `y_pixels` are not used, so always passing `0` is fine.
182
+ `${rows}x${cols},0x0\0`
183
+ ),
184
+ });
185
+ }
186
+
187
+ public kill() {
188
+ return this._socket.close();
189
+ }
190
+ }
@@ -0,0 +1,59 @@
1
+ import type { ValueOrPromise } from "@yume-chan/struct";
2
+ import type { Adb } from "../../../adb.js";
3
+ import type { AdbSocket } from "../../../socket/index.js";
4
+ import type { ReadableStream, WritableStream } from "../../../stream/index.js";
5
+
6
+ export interface AdbSubprocessProtocol {
7
+ /**
8
+ * A WritableStream that writes to the `stdin` pipe.
9
+ */
10
+ readonly stdin: WritableStream<Uint8Array>;
11
+
12
+ /**
13
+ * The `stdout` pipe of the process.
14
+ */
15
+ readonly stdout: ReadableStream<Uint8Array>;
16
+
17
+ /**
18
+ * The `stderr` pipe of the process.
19
+ *
20
+ * Note: Some `AdbSubprocessProtocol` doesn't separate `stdout` and `stderr`,
21
+ * All output will be sent to `stdout`.
22
+ */
23
+ readonly stderr: ReadableStream<Uint8Array>;
24
+
25
+ /**
26
+ * A `Promise` that resolves to the exit code of the process.
27
+ *
28
+ * Note: Some `AdbSubprocessProtocol` doesn't support exit code,
29
+ * They will always resolve it with `0`.
30
+ */
31
+ readonly exit: Promise<number>;
32
+
33
+ /**
34
+ * Resizes the current shell.
35
+ *
36
+ * Some `AdbSubprocessProtocol`s may not support resizing
37
+ * and will ignore calls to this method.
38
+ */
39
+ resize(rows: number, cols: number): ValueOrPromise<void>;
40
+
41
+ /**
42
+ * Kills the current process.
43
+ */
44
+ kill(): ValueOrPromise<void>;
45
+ }
46
+
47
+ export interface AdbSubprocessProtocolConstructor {
48
+ /** Returns `true` if the `adb` instance supports this shell */
49
+ isSupported(adb: Adb): ValueOrPromise<boolean>;
50
+
51
+ /** Spawns an executable in PTY (interactive) mode. */
52
+ pty(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
53
+
54
+ /** Spawns an executable and pipe the output. */
55
+ raw(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
56
+
57
+ /** Creates a new `AdbShell` by attaching to an exist `AdbSocket` */
58
+ new(socket: AdbSocket): AdbSubprocessProtocol;
59
+ }
File without changes
@@ -1,7 +1,7 @@
1
- export * from './list';
2
- export * from './pull';
3
- export * from './request';
4
- export * from './response';
5
- export * from './push';
6
- export * from './stat';
7
- export * from './sync';
1
+ export * from './list.js';
2
+ export * from './pull.js';
3
+ export * from './request.js';
4
+ export * from './response.js';
5
+ export * from './push.js';
6
+ export * from './stat.js';
7
+ export * from './sync.js';