@yume-chan/adb 2.6.2 → 3.0.0-beta.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 (451) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +1 -1
  3. package/esm/adb.d.ts +18 -15
  4. package/esm/adb.d.ts.map +1 -1
  5. package/esm/adb.js +18 -11
  6. package/esm/adb.js.map +1 -1
  7. package/esm/banner.d.ts +1 -1
  8. package/esm/banner.d.ts.map +1 -1
  9. package/esm/banner.js.map +1 -1
  10. package/esm/daemon/auth/authenticator.d.ts +28 -0
  11. package/esm/daemon/auth/authenticator.d.ts.map +1 -0
  12. package/esm/daemon/auth/authenticator.js +108 -0
  13. package/esm/daemon/auth/authenticator.js.map +1 -0
  14. package/esm/daemon/auth/index.d.ts +5 -0
  15. package/esm/daemon/auth/index.d.ts.map +1 -0
  16. package/esm/daemon/auth/index.js +5 -0
  17. package/esm/daemon/auth/index.js.map +1 -0
  18. package/esm/daemon/auth/packet-processor.d.ts +42 -0
  19. package/esm/daemon/auth/packet-processor.d.ts.map +1 -0
  20. package/esm/daemon/auth/packet-processor.js +129 -0
  21. package/esm/daemon/auth/packet-processor.js.map +1 -0
  22. package/esm/daemon/auth/public-key.d.ts +8 -0
  23. package/esm/daemon/auth/public-key.d.ts.map +1 -0
  24. package/esm/daemon/auth/public-key.js +74 -0
  25. package/esm/daemon/auth/public-key.js.map +1 -0
  26. package/esm/daemon/auth/type.d.ts +5 -0
  27. package/esm/daemon/auth/type.d.ts.map +1 -0
  28. package/esm/daemon/auth/type.js +2 -0
  29. package/esm/daemon/auth/type.js.map +1 -0
  30. package/esm/daemon/crypto.d.ts +6 -5
  31. package/esm/daemon/crypto.d.ts.map +1 -1
  32. package/esm/daemon/crypto.js +23 -72
  33. package/esm/daemon/crypto.js.map +1 -1
  34. package/esm/daemon/dispatcher.d.ts +3 -3
  35. package/esm/daemon/dispatcher.d.ts.map +1 -1
  36. package/esm/daemon/dispatcher.js +11 -4
  37. package/esm/daemon/dispatcher.js.map +1 -1
  38. package/esm/daemon/index.d.ts +1 -1
  39. package/esm/daemon/index.d.ts.map +1 -1
  40. package/esm/daemon/index.js +1 -1
  41. package/esm/daemon/index.js.map +1 -1
  42. package/esm/daemon/socket.d.ts +3 -3
  43. package/esm/daemon/socket.d.ts.map +1 -1
  44. package/esm/daemon/transport.d.ts +13 -65
  45. package/esm/daemon/transport.d.ts.map +1 -1
  46. package/esm/daemon/transport.js +18 -140
  47. package/esm/daemon/transport.js.map +1 -1
  48. package/esm/device-observer.d.ts +2 -3
  49. package/esm/device-observer.d.ts.map +1 -1
  50. package/esm/features-value.d.ts +40 -0
  51. package/esm/features-value.d.ts.map +1 -0
  52. package/esm/features-value.js +46 -0
  53. package/esm/features-value.js.map +1 -0
  54. package/esm/features.d.ts +4 -12
  55. package/esm/features.d.ts.map +1 -1
  56. package/esm/features.js +27 -13
  57. package/esm/features.js.map +1 -1
  58. package/esm/index.d.ts +1 -1
  59. package/esm/index.d.ts.map +1 -1
  60. package/esm/index.js +1 -1
  61. package/esm/index.js.map +1 -1
  62. package/esm/server/client.d.ts +7 -7
  63. package/esm/server/client.d.ts.map +1 -1
  64. package/esm/server/client.js +69 -86
  65. package/esm/server/client.js.map +1 -1
  66. package/esm/server/commands/wireless.js +1 -1
  67. package/esm/server/commands/wireless.js.map +1 -1
  68. package/esm/server/{stream.d.ts → data-connection.d.ts} +5 -5
  69. package/esm/server/data-connection.d.ts.map +1 -0
  70. package/esm/server/{stream.js → data-connection.js} +4 -4
  71. package/esm/server/data-connection.js.map +1 -0
  72. package/esm/server/index.d.ts +1 -1
  73. package/esm/server/index.d.ts.map +1 -1
  74. package/esm/server/index.js +1 -1
  75. package/esm/server/index.js.map +1 -1
  76. package/esm/server/observer.d.ts.map +1 -1
  77. package/esm/server/observer.js +14 -13
  78. package/esm/server/observer.js.map +1 -1
  79. package/esm/server/transport.d.ts +4 -6
  80. package/esm/server/transport.d.ts.map +1 -1
  81. package/esm/server/transport.js +9 -24
  82. package/esm/server/transport.js.map +1 -1
  83. package/esm/service/framebuffer.d.ts.map +1 -0
  84. package/esm/service/framebuffer.js.map +1 -0
  85. package/esm/{commands → service}/index.d.ts +1 -2
  86. package/esm/service/index.d.ts.map +1 -0
  87. package/esm/{commands → service}/index.js +1 -2
  88. package/esm/service/index.js.map +1 -0
  89. package/esm/{commands → service}/power.d.ts +4 -2
  90. package/esm/service/power.d.ts.map +1 -0
  91. package/esm/{commands → service}/power.js +11 -5
  92. package/esm/service/power.js.map +1 -0
  93. package/esm/service/reverse/error.d.ts +14 -0
  94. package/esm/service/reverse/error.d.ts.map +1 -0
  95. package/esm/service/reverse/error.js +21 -0
  96. package/esm/service/reverse/error.js.map +1 -0
  97. package/esm/service/reverse/index.d.ts +3 -0
  98. package/esm/service/reverse/index.d.ts.map +1 -0
  99. package/esm/service/reverse/index.js +4 -0
  100. package/esm/service/reverse/index.js.map +1 -0
  101. package/esm/service/reverse/response.d.ts +2 -0
  102. package/esm/service/reverse/response.d.ts.map +1 -0
  103. package/esm/service/reverse/response.js +30 -0
  104. package/esm/service/reverse/response.js.map +1 -0
  105. package/esm/service/reverse/service.d.ts +59 -0
  106. package/esm/service/reverse/service.d.ts.map +1 -0
  107. package/esm/{commands/reverse.js → service/reverse/service.js} +16 -45
  108. package/esm/service/reverse/service.js.map +1 -0
  109. package/esm/{commands → service}/subprocess/index.d.ts +1 -0
  110. package/esm/service/subprocess/index.d.ts.map +1 -0
  111. package/esm/{commands → service}/subprocess/index.js +1 -0
  112. package/esm/service/subprocess/index.js.map +1 -0
  113. package/esm/service/subprocess/none/index.d.ts.map +1 -0
  114. package/esm/service/subprocess/none/index.js.map +1 -0
  115. package/esm/{commands → service}/subprocess/none/process.d.ts +2 -2
  116. package/esm/service/subprocess/none/process.d.ts.map +1 -0
  117. package/esm/service/subprocess/none/process.js.map +1 -0
  118. package/esm/{commands → service}/subprocess/none/pty.d.ts +4 -4
  119. package/esm/service/subprocess/none/pty.d.ts.map +1 -0
  120. package/esm/{commands → service}/subprocess/none/pty.js +1 -1
  121. package/esm/service/subprocess/none/pty.js.map +1 -0
  122. package/esm/service/subprocess/none/service.d.ts +10 -0
  123. package/esm/service/subprocess/none/service.d.ts.map +1 -0
  124. package/esm/service/subprocess/none/service.js +44 -0
  125. package/esm/service/subprocess/none/service.js.map +1 -0
  126. package/esm/service/subprocess/none/spawner.d.ts +15 -0
  127. package/esm/service/subprocess/none/spawner.d.ts.map +1 -0
  128. package/esm/service/subprocess/none/spawner.js +31 -0
  129. package/esm/service/subprocess/none/spawner.js.map +1 -0
  130. package/esm/{commands → service}/subprocess/pty.d.ts +1 -1
  131. package/esm/service/subprocess/pty.d.ts.map +1 -0
  132. package/esm/{commands → service}/subprocess/pty.js.map +1 -1
  133. package/esm/service/subprocess/service.d.ts.map +1 -0
  134. package/esm/{commands → service}/subprocess/service.js +1 -1
  135. package/esm/service/subprocess/service.js.map +1 -0
  136. package/esm/service/subprocess/shell/index.d.ts +5 -0
  137. package/esm/{commands/subprocess/none → service/subprocess/shell}/index.d.ts.map +1 -1
  138. package/esm/service/subprocess/shell/index.js +6 -0
  139. package/esm/{commands/subprocess/none → service/subprocess/shell}/index.js.map +1 -1
  140. package/esm/{commands → service}/subprocess/shell/process.d.ts +2 -2
  141. package/esm/service/subprocess/shell/process.d.ts.map +1 -0
  142. package/esm/{commands → service}/subprocess/shell/process.js +4 -6
  143. package/esm/service/subprocess/shell/process.js.map +1 -0
  144. package/esm/{commands → service}/subprocess/shell/pty.d.ts +4 -4
  145. package/esm/service/subprocess/shell/pty.d.ts.map +1 -0
  146. package/esm/{commands → service}/subprocess/shell/pty.js +11 -9
  147. package/esm/service/subprocess/shell/pty.js.map +1 -0
  148. package/esm/service/subprocess/shell/service.d.ts +34 -0
  149. package/esm/service/subprocess/shell/service.d.ts.map +1 -0
  150. package/esm/service/subprocess/shell/service.js +52 -0
  151. package/esm/service/subprocess/shell/service.js.map +1 -0
  152. package/esm/service/subprocess/shell/shared.d.ts +2 -0
  153. package/esm/service/subprocess/shell/shared.d.ts.map +1 -0
  154. package/esm/{commands → service}/subprocess/shell/shared.js +2 -0
  155. package/esm/service/subprocess/shell/shared.js.map +1 -0
  156. package/esm/{commands → service}/subprocess/shell/spawner.d.ts +3 -7
  157. package/esm/service/subprocess/shell/spawner.d.ts.map +1 -0
  158. package/esm/service/subprocess/shell/spawner.js +45 -0
  159. package/esm/service/subprocess/shell/spawner.js.map +1 -0
  160. package/esm/service/subprocess/types.d.ts +14 -0
  161. package/esm/service/subprocess/types.d.ts.map +1 -0
  162. package/esm/service/subprocess/types.js +2 -0
  163. package/esm/service/subprocess/types.js.map +1 -0
  164. package/esm/service/subprocess/utils.d.ts +39 -0
  165. package/esm/service/subprocess/utils.d.ts.map +1 -0
  166. package/esm/service/subprocess/utils.js +142 -0
  167. package/esm/service/subprocess/utils.js.map +1 -0
  168. package/esm/service/sync/android.d.ts +34 -0
  169. package/esm/service/sync/android.d.ts.map +1 -0
  170. package/esm/service/sync/android.js +34 -0
  171. package/esm/service/sync/android.js.map +1 -0
  172. package/esm/service/sync/compression/compression.d.ts +17 -0
  173. package/esm/service/sync/compression/compression.d.ts.map +1 -0
  174. package/esm/service/sync/compression/compression.js +92 -0
  175. package/esm/service/sync/compression/compression.js.map +1 -0
  176. package/esm/service/sync/compression/format-ns.d.ts +5 -0
  177. package/esm/service/sync/compression/format-ns.d.ts.map +1 -0
  178. package/esm/service/sync/compression/format-ns.js +5 -0
  179. package/esm/service/sync/compression/format-ns.js.map +1 -0
  180. package/esm/service/sync/compression/format.d.ts +4 -0
  181. package/esm/service/sync/compression/format.d.ts.map +1 -0
  182. package/esm/service/sync/compression/format.js +3 -0
  183. package/esm/service/sync/compression/format.js.map +1 -0
  184. package/esm/service/sync/compression/index.d.ts +2 -0
  185. package/esm/service/sync/compression/index.d.ts.map +1 -0
  186. package/esm/service/sync/compression/index.js +2 -0
  187. package/esm/service/sync/compression/index.js.map +1 -0
  188. package/esm/service/sync/id/common.d.ts +17 -0
  189. package/esm/service/sync/id/common.d.ts.map +1 -0
  190. package/esm/service/sync/id/common.js +29 -0
  191. package/esm/service/sync/id/common.js.map +1 -0
  192. package/esm/service/sync/id/index.d.ts +4 -0
  193. package/esm/service/sync/id/index.d.ts.map +1 -0
  194. package/esm/service/sync/id/index.js +6 -0
  195. package/esm/service/sync/id/index.js.map +1 -0
  196. package/esm/service/sync/id/request.d.ts +7 -0
  197. package/esm/service/sync/id/request.d.ts.map +1 -0
  198. package/esm/service/sync/id/request.js +9 -0
  199. package/esm/service/sync/id/request.js.map +1 -0
  200. package/esm/service/sync/id/response.d.ts +6 -0
  201. package/esm/service/sync/id/response.d.ts.map +1 -0
  202. package/esm/service/sync/id/response.js +8 -0
  203. package/esm/service/sync/id/response.js.map +1 -0
  204. package/esm/service/sync/index-ns.d.ts +7 -0
  205. package/esm/service/sync/index-ns.d.ts.map +1 -0
  206. package/esm/service/sync/index-ns.js +7 -0
  207. package/esm/service/sync/index-ns.js.map +1 -0
  208. package/esm/service/sync/index.d.ts +3 -0
  209. package/esm/service/sync/index.d.ts.map +1 -0
  210. package/esm/service/sync/index.js +3 -0
  211. package/esm/service/sync/index.js.map +1 -0
  212. package/esm/service/sync/request/index.d.ts +5 -0
  213. package/esm/service/sync/request/index.d.ts.map +1 -0
  214. package/esm/service/sync/request/index.js +5 -0
  215. package/esm/service/sync/request/index.js.map +1 -0
  216. package/esm/{commands/sync → service/sync/request}/list.d.ts +21 -19
  217. package/esm/service/sync/request/list.d.ts.map +1 -0
  218. package/esm/service/sync/request/list.js +77 -0
  219. package/esm/service/sync/request/list.js.map +1 -0
  220. package/esm/service/sync/request/pull.d.ts +12 -0
  221. package/esm/service/sync/request/pull.d.ts.map +1 -0
  222. package/esm/service/sync/request/pull.js +28 -0
  223. package/esm/service/sync/request/pull.js.map +1 -0
  224. package/esm/service/sync/request/push.d.ts +54 -0
  225. package/esm/service/sync/request/push.d.ts.map +1 -0
  226. package/esm/service/sync/request/push.js +95 -0
  227. package/esm/service/sync/request/push.js.map +1 -0
  228. package/esm/{commands/sync → service/sync/request}/stat.d.ts +40 -50
  229. package/esm/service/sync/request/stat.d.ts.map +1 -0
  230. package/esm/service/sync/request/stat.js +89 -0
  231. package/esm/service/sync/request/stat.js.map +1 -0
  232. package/esm/service/sync/service.d.ts +123 -0
  233. package/esm/service/sync/service.d.ts.map +1 -0
  234. package/esm/service/sync/service.js +170 -0
  235. package/esm/service/sync/service.js.map +1 -0
  236. package/esm/service/sync/socket-pool.d.ts +11 -0
  237. package/esm/service/sync/socket-pool.d.ts.map +1 -0
  238. package/esm/service/sync/socket-pool.js +80 -0
  239. package/esm/service/sync/socket-pool.js.map +1 -0
  240. package/esm/service/sync/socket.d.ts +40 -0
  241. package/esm/service/sync/socket.d.ts.map +1 -0
  242. package/esm/service/sync/socket.js +153 -0
  243. package/esm/service/sync/socket.js.map +1 -0
  244. package/esm/{commands → service}/tcpip.d.ts +4 -2
  245. package/esm/service/tcpip.d.ts.map +1 -0
  246. package/esm/{commands → service}/tcpip.js +10 -7
  247. package/esm/service/tcpip.js.map +1 -0
  248. package/esm/utils/array-buffer.d.ts.map +1 -1
  249. package/esm/utils/array-buffer.js +2 -3
  250. package/esm/utils/array-buffer.js.map +1 -1
  251. package/esm/utils/base64.d.ts.map +1 -1
  252. package/esm/utils/base64.js +22 -10
  253. package/esm/utils/base64.js.map +1 -1
  254. package/esm/utils/index.d.ts +1 -0
  255. package/esm/utils/index.d.ts.map +1 -1
  256. package/esm/utils/index.js +1 -0
  257. package/esm/utils/index.js.map +1 -1
  258. package/esm/utils/md5.d.ts +8 -0
  259. package/esm/utils/md5.d.ts.map +1 -0
  260. package/esm/utils/md5.js +149 -0
  261. package/esm/utils/md5.js.map +1 -0
  262. package/package.json +9 -9
  263. package/src/adb.ts +44 -35
  264. package/src/banner.ts +2 -1
  265. package/src/daemon/auth/authenticator.ts +186 -0
  266. package/src/daemon/auth/index.ts +4 -0
  267. package/src/daemon/auth/packet-processor.ts +212 -0
  268. package/src/daemon/auth/public-key.ts +112 -0
  269. package/src/daemon/auth/type.ts +5 -0
  270. package/src/daemon/crypto.ts +36 -101
  271. package/src/daemon/dispatcher.ts +15 -8
  272. package/src/daemon/index.ts +1 -1
  273. package/src/daemon/socket.ts +3 -3
  274. package/src/daemon/transport.ts +35 -253
  275. package/src/device-observer.ts +3 -3
  276. package/src/features-value.ts +47 -0
  277. package/src/features.ts +31 -14
  278. package/src/index.ts +1 -1
  279. package/src/server/client.ts +94 -93
  280. package/src/server/commands/wireless.ts +1 -1
  281. package/src/server/{stream.ts → data-connection.ts} +4 -4
  282. package/src/server/index.ts +1 -1
  283. package/src/server/observer.ts +21 -19
  284. package/src/server/transport.ts +13 -34
  285. package/src/{commands → service}/index.ts +1 -2
  286. package/src/{commands → service}/power.ts +14 -5
  287. package/src/service/reverse/error.ts +35 -0
  288. package/src/service/reverse/index.ts +3 -0
  289. package/src/service/reverse/response.ts +38 -0
  290. package/src/{commands/reverse.ts → service/reverse/service.ts} +32 -72
  291. package/src/{commands → service}/subprocess/index.ts +1 -0
  292. package/src/{commands → service}/subprocess/none/process.ts +3 -3
  293. package/src/{commands → service}/subprocess/none/pty.ts +5 -5
  294. package/src/service/subprocess/none/service.ts +57 -0
  295. package/src/service/subprocess/none/spawner.ts +81 -0
  296. package/src/{commands → service}/subprocess/pty.ts +1 -1
  297. package/src/{commands → service}/subprocess/service.ts +1 -1
  298. package/src/{commands → service}/subprocess/shell/index.ts +1 -1
  299. package/src/{commands → service}/subprocess/shell/process.ts +6 -7
  300. package/src/{commands → service}/subprocess/shell/pty.ts +15 -13
  301. package/src/service/subprocess/shell/service.ts +75 -0
  302. package/src/{commands → service}/subprocess/shell/shared.ts +5 -0
  303. package/src/service/subprocess/shell/spawner.ts +109 -0
  304. package/src/service/subprocess/types.ts +19 -0
  305. package/src/service/subprocess/utils.ts +166 -0
  306. package/src/service/sync/android.ts +43 -0
  307. package/src/service/sync/compression/compression.ts +126 -0
  308. package/src/service/sync/compression/format-ns.ts +4 -0
  309. package/src/service/sync/compression/format.ts +5 -0
  310. package/src/service/sync/compression/index.ts +1 -0
  311. package/src/service/sync/id/common.ts +33 -0
  312. package/src/service/sync/id/index.ts +7 -0
  313. package/src/service/sync/id/request.ts +12 -0
  314. package/src/service/sync/id/response.ts +11 -0
  315. package/src/service/sync/index-ns.ts +6 -0
  316. package/src/service/sync/index.ts +2 -0
  317. package/src/service/sync/request/index.ts +4 -0
  318. package/src/service/sync/request/list.ts +110 -0
  319. package/src/service/sync/request/pull.ts +49 -0
  320. package/src/service/sync/request/push.ts +184 -0
  321. package/src/service/sync/request/stat.ts +127 -0
  322. package/src/service/sync/service.ts +255 -0
  323. package/src/service/sync/socket-pool.ts +97 -0
  324. package/src/service/sync/socket.ts +201 -0
  325. package/src/{commands → service}/tcpip.ts +13 -7
  326. package/src/utils/array-buffer.ts +2 -3
  327. package/src/utils/base64.ts +24 -10
  328. package/src/utils/index.ts +1 -0
  329. package/src/utils/md5.ts +185 -0
  330. package/yume-chan-adb-3.0.0-beta.0.tgz +0 -0
  331. package/CHANGELOG.md +0 -257
  332. package/esm/commands/base.d.ts +0 -8
  333. package/esm/commands/base.d.ts.map +0 -1
  334. package/esm/commands/base.js +0 -12
  335. package/esm/commands/base.js.map +0 -1
  336. package/esm/commands/framebuffer.d.ts.map +0 -1
  337. package/esm/commands/framebuffer.js.map +0 -1
  338. package/esm/commands/index.d.ts.map +0 -1
  339. package/esm/commands/index.js.map +0 -1
  340. package/esm/commands/power.d.ts.map +0 -1
  341. package/esm/commands/power.js.map +0 -1
  342. package/esm/commands/reverse.d.ts +0 -48
  343. package/esm/commands/reverse.d.ts.map +0 -1
  344. package/esm/commands/reverse.js.map +0 -1
  345. package/esm/commands/subprocess/index.d.ts.map +0 -1
  346. package/esm/commands/subprocess/index.js.map +0 -1
  347. package/esm/commands/subprocess/none/process.d.ts.map +0 -1
  348. package/esm/commands/subprocess/none/process.js.map +0 -1
  349. package/esm/commands/subprocess/none/pty.d.ts.map +0 -1
  350. package/esm/commands/subprocess/none/pty.js.map +0 -1
  351. package/esm/commands/subprocess/none/service.d.ts +0 -10
  352. package/esm/commands/subprocess/none/service.d.ts.map +0 -1
  353. package/esm/commands/subprocess/none/service.js +0 -34
  354. package/esm/commands/subprocess/none/service.js.map +0 -1
  355. package/esm/commands/subprocess/none/spawner.d.ts +0 -19
  356. package/esm/commands/subprocess/none/spawner.d.ts.map +0 -1
  357. package/esm/commands/subprocess/none/spawner.js +0 -26
  358. package/esm/commands/subprocess/none/spawner.js.map +0 -1
  359. package/esm/commands/subprocess/pty.d.ts.map +0 -1
  360. package/esm/commands/subprocess/service.d.ts.map +0 -1
  361. package/esm/commands/subprocess/service.js.map +0 -1
  362. package/esm/commands/subprocess/shell/index.d.ts +0 -6
  363. package/esm/commands/subprocess/shell/index.d.ts.map +0 -1
  364. package/esm/commands/subprocess/shell/index.js +0 -6
  365. package/esm/commands/subprocess/shell/index.js.map +0 -1
  366. package/esm/commands/subprocess/shell/process.d.ts.map +0 -1
  367. package/esm/commands/subprocess/shell/process.js.map +0 -1
  368. package/esm/commands/subprocess/shell/pty.d.ts.map +0 -1
  369. package/esm/commands/subprocess/shell/pty.js.map +0 -1
  370. package/esm/commands/subprocess/shell/service.d.ts +0 -14
  371. package/esm/commands/subprocess/shell/service.d.ts.map +0 -1
  372. package/esm/commands/subprocess/shell/service.js +0 -41
  373. package/esm/commands/subprocess/shell/service.js.map +0 -1
  374. package/esm/commands/subprocess/shell/shared.d.ts +0 -19
  375. package/esm/commands/subprocess/shell/shared.d.ts.map +0 -1
  376. package/esm/commands/subprocess/shell/shared.js.map +0 -1
  377. package/esm/commands/subprocess/shell/spawner.d.ts.map +0 -1
  378. package/esm/commands/subprocess/shell/spawner.js +0 -38
  379. package/esm/commands/subprocess/shell/spawner.js.map +0 -1
  380. package/esm/commands/subprocess/utils.d.ts +0 -3
  381. package/esm/commands/subprocess/utils.d.ts.map +0 -1
  382. package/esm/commands/subprocess/utils.js +0 -56
  383. package/esm/commands/subprocess/utils.js.map +0 -1
  384. package/esm/commands/sync/index.d.ts +0 -9
  385. package/esm/commands/sync/index.d.ts.map +0 -1
  386. package/esm/commands/sync/index.js +0 -9
  387. package/esm/commands/sync/index.js.map +0 -1
  388. package/esm/commands/sync/list.d.ts.map +0 -1
  389. package/esm/commands/sync/list.js +0 -64
  390. package/esm/commands/sync/list.js.map +0 -1
  391. package/esm/commands/sync/pull.d.ts +0 -12
  392. package/esm/commands/sync/pull.d.ts.map +0 -1
  393. package/esm/commands/sync/pull.js +0 -33
  394. package/esm/commands/sync/pull.js.map +0 -1
  395. package/esm/commands/sync/push.d.ts +0 -61
  396. package/esm/commands/sync/push.d.ts.map +0 -1
  397. package/esm/commands/sync/push.js +0 -83
  398. package/esm/commands/sync/push.js.map +0 -1
  399. package/esm/commands/sync/request.d.ts +0 -24
  400. package/esm/commands/sync/request.d.ts.map +0 -1
  401. package/esm/commands/sync/request.js +0 -32
  402. package/esm/commands/sync/request.js.map +0 -1
  403. package/esm/commands/sync/response.d.ts +0 -28
  404. package/esm/commands/sync/response.d.ts.map +0 -1
  405. package/esm/commands/sync/response.js +0 -81
  406. package/esm/commands/sync/response.js.map +0 -1
  407. package/esm/commands/sync/socket.d.ts +0 -22
  408. package/esm/commands/sync/socket.d.ts.map +0 -1
  409. package/esm/commands/sync/socket.js +0 -78
  410. package/esm/commands/sync/socket.js.map +0 -1
  411. package/esm/commands/sync/stat.d.ts.map +0 -1
  412. package/esm/commands/sync/stat.js +0 -121
  413. package/esm/commands/sync/stat.js.map +0 -1
  414. package/esm/commands/sync/sync.d.ts +0 -82
  415. package/esm/commands/sync/sync.d.ts.map +0 -1
  416. package/esm/commands/sync/sync.js +0 -138
  417. package/esm/commands/sync/sync.js.map +0 -1
  418. package/esm/commands/tcpip.d.ts.map +0 -1
  419. package/esm/commands/tcpip.js.map +0 -1
  420. package/esm/daemon/auth.d.ts +0 -54
  421. package/esm/daemon/auth.d.ts.map +0 -1
  422. package/esm/daemon/auth.js +0 -104
  423. package/esm/daemon/auth.js.map +0 -1
  424. package/esm/server/stream.d.ts.map +0 -1
  425. package/esm/server/stream.js.map +0 -1
  426. package/src/commands/base.ts +0 -15
  427. package/src/commands/subprocess/none/service.ts +0 -45
  428. package/src/commands/subprocess/none/spawner.ts +0 -68
  429. package/src/commands/subprocess/shell/service.ts +0 -58
  430. package/src/commands/subprocess/shell/spawner.ts +0 -90
  431. package/src/commands/subprocess/utils.ts +0 -61
  432. package/src/commands/sync/index.ts +0 -8
  433. package/src/commands/sync/list.ts +0 -99
  434. package/src/commands/sync/pull.ts +0 -55
  435. package/src/commands/sync/push.ts +0 -172
  436. package/src/commands/sync/request.ts +0 -53
  437. package/src/commands/sync/response.ts +0 -105
  438. package/src/commands/sync/socket.ts +0 -109
  439. package/src/commands/sync/stat.ts +0 -177
  440. package/src/commands/sync/sync.ts +0 -175
  441. package/src/daemon/auth.ts +0 -203
  442. package/tsconfig.build.tsbuildinfo +0 -1
  443. /package/esm/{commands → service}/framebuffer.d.ts +0 -0
  444. /package/esm/{commands → service}/framebuffer.js +0 -0
  445. /package/esm/{commands → service}/subprocess/none/index.d.ts +0 -0
  446. /package/esm/{commands → service}/subprocess/none/index.js +0 -0
  447. /package/esm/{commands → service}/subprocess/none/process.js +0 -0
  448. /package/esm/{commands → service}/subprocess/pty.js +0 -0
  449. /package/esm/{commands → service}/subprocess/service.d.ts +0 -0
  450. /package/src/{commands → service}/framebuffer.ts +0 -0
  451. /package/src/{commands → service}/subprocess/none/index.ts +0 -0
@@ -0,0 +1,49 @@
1
+ import { ReadableStream } from "@yume-chan/stream-extra";
2
+ import type { StructValue } from "@yume-chan/struct";
3
+ import { buffer, struct, u32 } from "@yume-chan/struct";
4
+
5
+ import { RequestId, ResponseId } from "../id/index.js";
6
+ import type { SocketPool } from "../socket-pool.js";
7
+ import { Error as AdbSyncError } from "../socket.js";
8
+
9
+ export const DataResponse = struct(
10
+ { data: buffer(u32) },
11
+ { littleEndian: true },
12
+ );
13
+
14
+ export type DataResponse = StructValue<typeof DataResponse>;
15
+
16
+ export async function* generator(
17
+ pool: SocketPool,
18
+ path: string,
19
+ ): AsyncGenerator<Uint8Array, void, void> {
20
+ const socket = await pool.acquire();
21
+ let completed = false;
22
+ let error: unknown;
23
+
24
+ try {
25
+ await socket.writeRequest(RequestId.Receive, path);
26
+ for await (const packet of socket.readResponses(
27
+ ResponseId.Data,
28
+ DataResponse,
29
+ )) {
30
+ yield packet.data;
31
+ }
32
+ completed = true;
33
+ } catch (e) {
34
+ error = e;
35
+ throw e;
36
+ } finally {
37
+ await pool.release(
38
+ socket,
39
+ !(completed || error instanceof AdbSyncError),
40
+ );
41
+ }
42
+ }
43
+
44
+ export function stream(
45
+ pool: SocketPool,
46
+ path: string,
47
+ ): ReadableStream<Uint8Array> {
48
+ return ReadableStream.from(generator(pool, path));
49
+ }
@@ -0,0 +1,184 @@
1
+ import type { ReadableStream } from "@yume-chan/stream-extra";
2
+ import {
3
+ AbortController,
4
+ DistributionStream,
5
+ MaybeConsumable,
6
+ } from "@yume-chan/stream-extra";
7
+ import { struct, u32 } from "@yume-chan/struct";
8
+
9
+ import { NOOP } from "../../../utils/index.js";
10
+ import { LinuxFileType } from "../android.js";
11
+ import { Compression } from "../compression/index.js";
12
+ import { RequestId, ResponseId } from "../id/index.js";
13
+ import type { SocketPool } from "../socket-pool.js";
14
+ import type { Socket } from "../socket.js";
15
+ import { Error as AdbSyncError } from "../socket.js";
16
+
17
+ export const MaxPacketSize = 64 * 1024;
18
+
19
+ export const OkResponse = struct({ unused: u32 }, { littleEndian: true });
20
+
21
+ // eslint-disable-next-line @typescript-eslint/max-params
22
+ async function pipeFileData(
23
+ socket: Socket,
24
+ file: ReadableStream<MaybeConsumable<Uint8Array>>,
25
+ packetSize: number,
26
+ mtime: number,
27
+ compression: Compression.Format,
28
+ ): Promise<void> {
29
+ if (compression !== Compression.Format.None) {
30
+ const stream = Compression.createCompressionStream(compression);
31
+ void file
32
+ .pipeTo(new MaybeConsumable.WrapWritableStream(stream.writable))
33
+ .catch(NOOP);
34
+ file = stream.readable;
35
+ }
36
+
37
+ // Read and write in parallel,
38
+ // allow error response to abort the write.
39
+ const abortController = new AbortController();
40
+ file.pipeThrough(new DistributionStream(packetSize, true))
41
+ .pipeTo(
42
+ new MaybeConsumable.WritableStream({
43
+ write(chunk) {
44
+ return socket.writeRequest(RequestId.Data, chunk);
45
+ },
46
+ }),
47
+ { signal: abortController.signal },
48
+ )
49
+ .then(async () => {
50
+ await socket.writeRequest(RequestId.Done, mtime);
51
+ await socket.flush();
52
+ }, NOOP);
53
+
54
+ try {
55
+ await socket.readResponse(ResponseId.Ok, OkResponse);
56
+ } catch (e) {
57
+ abortController.abort(e);
58
+ throw e;
59
+ }
60
+ }
61
+
62
+ export interface SendV1Options {
63
+ pool: SocketPool;
64
+ filename: string;
65
+ file: ReadableStream<MaybeConsumable<Uint8Array>>;
66
+ type?: LinuxFileType | undefined;
67
+ permission?: number | undefined;
68
+ mtime?: number | undefined;
69
+ packetSize?: number | undefined;
70
+ }
71
+
72
+ export function sendV1({
73
+ pool,
74
+ filename,
75
+ file,
76
+ type = LinuxFileType.File,
77
+ permission = 0o666,
78
+ mtime = (Date.now() / 1000) | 0,
79
+ packetSize = MaxPacketSize,
80
+ }: SendV1Options) {
81
+ return pool.withSocket(async (socket) => {
82
+ const mode = (type << 12) | permission;
83
+ const pathAndMode = `${filename},${mode.toString()}`;
84
+ await socket.writeRequest(RequestId.Send, pathAndMode);
85
+ await pipeFileData(
86
+ socket,
87
+ file,
88
+ packetSize,
89
+ mtime,
90
+ Compression.Format.None,
91
+ );
92
+ });
93
+ }
94
+
95
+ export const SendV2Flags = {
96
+ None: 0,
97
+ Brotli: 1,
98
+ Lz4: 2,
99
+ Zstd: 4,
100
+ DryRun: 0x80000000,
101
+ } as const;
102
+
103
+ export type SendV2Flags = (typeof SendV2Flags)[keyof typeof SendV2Flags];
104
+
105
+ export const SendV2Request = struct(
106
+ { id: u32, mode: u32, flags: u32 },
107
+ { littleEndian: true },
108
+ );
109
+
110
+ export interface SendV2Options extends SendV1Options {
111
+ compression?: Compression.Format | undefined;
112
+
113
+ /**
114
+ * Don't write the file to disk. Requires the `sendrecv_v2` feature.
115
+ *
116
+ * It was used during ADB development to benchmark the performance of
117
+ * compression algorithms.
118
+ */
119
+ dryRun?: boolean | undefined;
120
+ }
121
+
122
+ export function sendV2({
123
+ pool,
124
+ filename,
125
+ file,
126
+ type = LinuxFileType.File,
127
+ permission = 0o666,
128
+ mtime = (Date.now() / 1000) | 0,
129
+ packetSize = MaxPacketSize,
130
+ compression = Compression.Format.None,
131
+ dryRun = false,
132
+ }: SendV2Options) {
133
+ return pool.withSocket(async (socket) => {
134
+ let flags: SendV2Flags = SendV2Flags.None;
135
+ switch (compression) {
136
+ case Compression.Format.Brotli:
137
+ flags |= SendV2Flags.Brotli;
138
+ break;
139
+ case Compression.Format.Lz4:
140
+ flags |= SendV2Flags.Lz4;
141
+ break;
142
+ case Compression.Format.Zstd:
143
+ flags |= SendV2Flags.Zstd;
144
+ break;
145
+ }
146
+ if (dryRun) {
147
+ flags |= SendV2Flags.DryRun;
148
+ }
149
+
150
+ await socket.writeRequest(RequestId.SendV2, filename);
151
+ await socket.write(
152
+ SendV2Request.serialize({
153
+ id: RequestId.SendV2,
154
+ mode: (type << 12) | permission,
155
+ flags,
156
+ }),
157
+ );
158
+
159
+ await pipeFileData(socket, file, packetSize, mtime, compression);
160
+ });
161
+ }
162
+
163
+ export interface SendOptions extends SendV2Options {
164
+ version: 1 | 2;
165
+ }
166
+
167
+ export function send(options: SendOptions) {
168
+ if (options.version === 2) {
169
+ return sendV2(options);
170
+ }
171
+
172
+ if (options.dryRun) {
173
+ throw new AdbSyncError("dryRun is not supported in v1");
174
+ }
175
+
176
+ if (
177
+ options.compression !== undefined &&
178
+ options.compression !== Compression.Format.None
179
+ ) {
180
+ throw new AdbSyncError("compression is not supported in v1");
181
+ }
182
+
183
+ return sendV1(options);
184
+ }
@@ -0,0 +1,127 @@
1
+ import type { StructValue } from "@yume-chan/struct";
2
+ import { struct, u32, u64 } from "@yume-chan/struct";
3
+
4
+ import type { AndroidSyscallErrorCode, LinuxFileType } from "../android.js";
5
+ import { AndroidSyscallErrorNameMap } from "../android.js";
6
+ import { RequestId, ResponseId } from "../id/index.js";
7
+ import type { SocketPool } from "../socket-pool.js";
8
+ import { Error as AdbSyncError } from "../socket.js";
9
+
10
+ export const LstatResponse = struct(
11
+ { mode: u32, size: u32, mtime: u32 },
12
+ {
13
+ littleEndian: true,
14
+ extra: {
15
+ get type(): LinuxFileType {
16
+ return (this.mode >> 12) as LinuxFileType;
17
+ },
18
+ get permission(): number {
19
+ return this.mode & 0b00001111_11111111;
20
+ },
21
+ },
22
+ postDeserialize(value) {
23
+ if (value.mode === 0 && value.size === 0 && value.mtime === 0) {
24
+ throw new AdbSyncError("lstat error");
25
+ }
26
+ return value;
27
+ },
28
+ },
29
+ );
30
+
31
+ export type LstatResponse = StructValue<typeof LstatResponse>;
32
+
33
+ export function lstatV1(
34
+ pool: SocketPool,
35
+ path: string,
36
+ ): Promise<LstatResponse> {
37
+ return pool.withSocket(async (socket) => {
38
+ await socket.writeRequest(RequestId.Lstat, path);
39
+ return await socket.readResponse(ResponseId.Lstat, LstatResponse);
40
+ });
41
+ }
42
+
43
+ export const StatResponse = struct(
44
+ {
45
+ error: u32<AndroidSyscallErrorCode>(),
46
+ dev: u64,
47
+ ino: u64,
48
+ mode: u32,
49
+ nlink: u32,
50
+ uid: u32,
51
+ gid: u32,
52
+ size: u64,
53
+ atime: u64,
54
+ mtime: u64,
55
+ ctime: u64,
56
+ },
57
+ {
58
+ littleEndian: true,
59
+ extra: {
60
+ get type(): LinuxFileType {
61
+ return (this.mode >> 12) as LinuxFileType;
62
+ },
63
+ get permission(): number {
64
+ return this.mode & 0b00001111_11111111;
65
+ },
66
+ },
67
+ postDeserialize(value) {
68
+ if (value.error) {
69
+ throw new AdbSyncError(AndroidSyscallErrorNameMap[value.error]);
70
+ }
71
+ return value;
72
+ },
73
+ },
74
+ );
75
+
76
+ export type StatResponse = StructValue<typeof StatResponse>;
77
+
78
+ export function lstatV2(pool: SocketPool, path: string) {
79
+ return pool.withSocket(async (socket) => {
80
+ await socket.writeRequest(RequestId.LstatV2, path);
81
+ return await socket.readResponse(ResponseId.LstatV2, StatResponse);
82
+ });
83
+ }
84
+
85
+ export async function lstat(
86
+ pool: SocketPool,
87
+ path: string,
88
+ { version }: { version: 1 | 2 },
89
+ ): Promise<Stat> {
90
+ if (version === 2) {
91
+ return await lstatV2(pool, path);
92
+ } else {
93
+ const response = await lstatV1(pool, path);
94
+ return {
95
+ mode: response.mode,
96
+ // Convert to `BigInt` to make it compatible with `StatResponse`
97
+ size: BigInt(response.size),
98
+ mtime: BigInt(response.mtime),
99
+ get type() {
100
+ return response.type;
101
+ },
102
+ get permission() {
103
+ return response.permission;
104
+ },
105
+ };
106
+ }
107
+ }
108
+
109
+ export interface Stat {
110
+ mode: number;
111
+ size: bigint;
112
+ mtime: bigint;
113
+ get type(): LinuxFileType;
114
+ get permission(): number;
115
+
116
+ uid?: number;
117
+ gid?: number;
118
+ atime?: bigint;
119
+ ctime?: bigint;
120
+ }
121
+
122
+ export function stat(pool: SocketPool, path: string): Promise<StatResponse> {
123
+ return pool.withSocket(async (socket) => {
124
+ await socket.writeRequest(RequestId.Stat, path);
125
+ return await socket.readResponse(ResponseId.Stat, StatResponse);
126
+ });
127
+ }
@@ -0,0 +1,255 @@
1
+ import type {
2
+ AbortSignal,
3
+ MaybeConsumable,
4
+ ReadableStream,
5
+ } from "@yume-chan/stream-extra";
6
+
7
+ import type { Adb } from "../../adb.js";
8
+ import { AdbFeature } from "../../features.js";
9
+ import { escapeArg } from "../subprocess/index.js";
10
+
11
+ import type { LinuxFileType } from "./android.js";
12
+ import { Compression } from "./compression/index.js";
13
+ import { OpenDir, Receive, Send, Stat } from "./request/index.js";
14
+ import { SocketPool } from "./socket-pool.js";
15
+ import type { Socket } from "./socket.js";
16
+ import { Error as AdbSyncError } from "./socket.js";
17
+
18
+ export interface ServiceOptions {
19
+ /**
20
+ * Maximum number of sockets to keep in the pool for reuse.
21
+ * Extra sockets beyond this limit will be closed when released.
22
+ * @default 4
23
+ */
24
+ maxPoolSize?: number | undefined;
25
+
26
+ /**
27
+ * Time in milliseconds to keep idle sockets in the pool before closing them.
28
+ * Set to 0 to disable idle timeout (sockets will remain until pool is disposed).
29
+ * @default 60000 (1 minute)
30
+ */
31
+ idleTimeout?: number | undefined;
32
+
33
+ /**
34
+ * Abort signal to dispose the socket pool when aborted.
35
+ */
36
+ signal?: AbortSignal | undefined;
37
+ }
38
+
39
+ /**
40
+ * A simplified `dirname` function that only handles absolute unix paths.
41
+ * @param path an absolute unix path
42
+ * @returns the directory name of the input path
43
+ */
44
+ export function dirname(path: string): string {
45
+ const end = path.lastIndexOf("/");
46
+ if (end === -1) {
47
+ throw new Error(`Invalid absolute unix path: ${path}`);
48
+ }
49
+ if (end === 0) {
50
+ return "/";
51
+ }
52
+ return path.substring(0, end);
53
+ }
54
+
55
+ export interface WriteOptions {
56
+ filename: string;
57
+ file: ReadableStream<MaybeConsumable<Uint8Array>>;
58
+ type?: LinuxFileType | undefined;
59
+ permission?: number | undefined;
60
+ mtime?: number | undefined;
61
+ /**
62
+ * The format to compress the file stream for sending.
63
+ *
64
+ * If the device doesn't support compressed sending, it will be ignored.
65
+ *
66
+ * If the device or current runtime doesn't support the specified format,
67
+ * an Error will be thrown.
68
+ * {@link Compression.canUseFormat} can be used to check if
69
+ * the device and current runtime both supports the format.
70
+ */
71
+ compression?: Compression.Format | undefined;
72
+ dryRun?: boolean | undefined;
73
+ }
74
+
75
+ export class Service {
76
+ protected _adb: Adb;
77
+ readonly #socketPool: SocketPool;
78
+
79
+ readonly #supportsStat2: boolean;
80
+ readonly #supportsLs2: boolean;
81
+ readonly #fixedPushMkdir: boolean;
82
+ readonly #supportsSendReceive2: boolean;
83
+ readonly #needPushMkdirWorkaround: boolean;
84
+
85
+ get supportsStat2(): boolean {
86
+ return this.#supportsStat2;
87
+ }
88
+
89
+ get supportsLs2(): boolean {
90
+ return this.#supportsLs2;
91
+ }
92
+
93
+ get fixedPushMkdir(): boolean {
94
+ return this.#fixedPushMkdir;
95
+ }
96
+
97
+ get supportsSendReceive2(): boolean {
98
+ return this.#supportsSendReceive2;
99
+ }
100
+
101
+ get needPushMkdirWorkaround(): boolean {
102
+ return this.#needPushMkdirWorkaround;
103
+ }
104
+
105
+ constructor(adb: Adb, options?: ServiceOptions) {
106
+ this._adb = adb;
107
+ this.#socketPool = new SocketPool(
108
+ adb,
109
+ options?.maxPoolSize,
110
+ options?.idleTimeout,
111
+ );
112
+
113
+ // Dispose the socket pool when the abort signal is triggered
114
+ options?.signal?.addEventListener("abort", () => {
115
+ void this.#socketPool.dispose();
116
+ });
117
+
118
+ this.#supportsStat2 = adb.canUseFeature(AdbFeature.Stat2);
119
+ this.#supportsLs2 = adb.canUseFeature(AdbFeature.Ls2);
120
+ this.#fixedPushMkdir = adb.canUseFeature(AdbFeature.FixedPushMkdir);
121
+ this.#supportsSendReceive2 = adb.canUseFeature(AdbFeature.SendReceive2);
122
+ // https://android.googlesource.com/platform/packages/modules/adb/+/91768a57b7138166e0a3d11f79cd55909dda7014/client/file_sync_client.cpp#1361
123
+ this.#needPushMkdirWorkaround =
124
+ this._adb.canUseFeature(AdbFeature.Shell2) && !this.fixedPushMkdir;
125
+ }
126
+
127
+ /**
128
+ * Acquires a socket from the pool for direct use.
129
+ *
130
+ * The socket must be returned to the pool using {@link releaseSocket} after use.
131
+ *
132
+ * @returns A socket from the pool
133
+ */
134
+ async acquireSocket() {
135
+ return await this.#socketPool.acquire();
136
+ }
137
+
138
+ /**
139
+ * Returns a socket to the pool after use.
140
+ *
141
+ * @param socket The socket to return to the pool
142
+ * @param discard Whether to discard the socket instead of returning it to the pool
143
+ */
144
+ async releaseSocket(socket: Socket, discard = false) {
145
+ await this.#socketPool.release(socket, discard);
146
+ }
147
+
148
+ /**
149
+ * Gets information of a file or folder.
150
+ *
151
+ * If `path` points to a symbolic link, the returned information is about the link itself (with `type` being `LinuxFileType.Link`).
152
+ */
153
+ lstat(path: string): Promise<Stat.Stat> {
154
+ return Stat.lstat(this.#socketPool, path, {
155
+ version: this.#supportsStat2 ? 2 : 1,
156
+ });
157
+ }
158
+
159
+ /**
160
+ * Gets the information of a file or folder.
161
+ *
162
+ * If `path` points to a symbolic link, it will be resolved and the returned information is about the target (with `type` being `LinuxFileType.File` or `LinuxFileType.Directory`).
163
+ */
164
+ stat(path: string) {
165
+ if (!this.#supportsStat2) {
166
+ throw new Error("Not supported");
167
+ }
168
+
169
+ return Stat.stat(this.#socketPool, path);
170
+ }
171
+
172
+ /**
173
+ * Checks if `path` is a directory, or a symbolic link to a directory.
174
+ *
175
+ * This uses `lstat` internally, thus works on all Android versions.
176
+ */
177
+ async isDirectory(path: string): Promise<boolean> {
178
+ try {
179
+ await this.lstat(path + "/");
180
+ return true;
181
+ } catch (e) {
182
+ if (e instanceof AdbSyncError) {
183
+ return false;
184
+ } else {
185
+ throw e;
186
+ }
187
+ }
188
+ }
189
+
190
+ opendir(path: string): AsyncGenerator<OpenDir.Entry, void, void> {
191
+ return OpenDir.opendir(this.#socketPool, path, {
192
+ version: this.supportsLs2 ? 2 : 1,
193
+ });
194
+ }
195
+
196
+ async readdir(path: string) {
197
+ // TODO: Convert to `Array.fromAsync`
198
+ const results: OpenDir.Entry[] = [];
199
+ for await (const entry of this.opendir(path)) {
200
+ results.push(entry);
201
+ }
202
+ return results;
203
+ }
204
+
205
+ /**
206
+ * Reads the content of a file on device.
207
+ *
208
+ * @param filename The full path of the file on device to read.
209
+ * @returns A `ReadableStream` that contains the file content.
210
+ */
211
+ read(filename: string): ReadableStream<Uint8Array> {
212
+ return Receive.stream(this.#socketPool, filename);
213
+ }
214
+
215
+ /**
216
+ * Writes a file on device. If the file name already exists, it will be overwritten.
217
+ *
218
+ * @param options The content and options of the file to write.
219
+ */
220
+ async write(options: WriteOptions): Promise<void> {
221
+ if (this.needPushMkdirWorkaround) {
222
+ // It may fail if `filename` already exists.
223
+ // Ignore the result.
224
+ // TODO: sync: test push mkdir workaround (need an Android 8 device)
225
+ await this._adb.subprocess.noneProtocol
226
+ .spawn(["mkdir", "-p", escapeArg(dirname(options.filename))])
227
+ .wait();
228
+ }
229
+
230
+ if (this.supportsSendReceive2) {
231
+ if (options.compression === undefined) {
232
+ options.compression = Compression.chooseFormat(
233
+ this._adb,
234
+ Compression.Mode.Compress,
235
+ );
236
+ } else if (
237
+ !Compression.canUseFormat(
238
+ this._adb,
239
+ options.compression,
240
+ Compression.Mode.Compress,
241
+ )
242
+ ) {
243
+ throw new Error(
244
+ `Compression type ${options.compression} is not supported`,
245
+ );
246
+ }
247
+ }
248
+
249
+ await Send.send({
250
+ version: this.supportsSendReceive2 ? 2 : 1,
251
+ pool: this.#socketPool,
252
+ ...options,
253
+ });
254
+ }
255
+ }
@@ -0,0 +1,97 @@
1
+ import type { Adb } from "../../adb.js";
2
+
3
+ import { Error as AdbSyncError, Socket } from "./socket.js";
4
+
5
+ export class SocketPool {
6
+ readonly #adb: Adb;
7
+ readonly #maxSize: number;
8
+ readonly #idleTimeout: number;
9
+ readonly #availableSockets: Socket[] = [];
10
+ readonly #inUseSockets = new Set<Socket>();
11
+ #closed = false;
12
+
13
+ constructor(adb: Adb, maxSize = 4, idleTimeout = 60000) {
14
+ this.#adb = adb;
15
+ this.#maxSize = maxSize;
16
+ this.#idleTimeout = idleTimeout;
17
+ }
18
+
19
+ async acquire(): Promise<Socket> {
20
+ if (this.#closed) {
21
+ throw new Error("SocketPool is closed");
22
+ }
23
+ // Try to reuse an available socket
24
+ if (this.#availableSockets.length > 0) {
25
+ const socket = this.#availableSockets.pop()!;
26
+ // Clear the idle timeout
27
+ socket.clearIdleTimer();
28
+ this.#inUseSockets.add(socket);
29
+ return socket;
30
+ }
31
+
32
+ // Create a new socket
33
+ const adbSocket = await this.#adb.createSocket("sync:");
34
+ if (this.#closed) {
35
+ await adbSocket.close();
36
+ throw new Error("SocketPool is closed");
37
+ }
38
+ const socket = new Socket(adbSocket, this.#adb.maxPayloadSize);
39
+ this.#inUseSockets.add(socket);
40
+ return socket;
41
+ }
42
+
43
+ async withSocket<T>(fn: (socket: Socket) => Promise<T>): Promise<T> {
44
+ const socket = await this.acquire();
45
+ try {
46
+ const result = await fn(socket);
47
+ await this.release(socket);
48
+ return result;
49
+ } catch (e) {
50
+ await this.release(socket, !(e instanceof AdbSyncError));
51
+ throw e;
52
+ }
53
+ }
54
+
55
+ async release(socket: Socket, discard = false): Promise<void> {
56
+ this.#inUseSockets.delete(socket);
57
+
58
+ // If discarding or we already have enough sockets in the pool, close this socket
59
+ if (discard || this.#availableSockets.length >= this.#maxSize) {
60
+ await socket.close();
61
+ return;
62
+ }
63
+
64
+ // Otherwise, return it to the pool and start idle timeout
65
+ socket.startIdleTimer(this.#idleTimeout, () => {
66
+ this.#closeIdleSocket(socket);
67
+ });
68
+ this.#availableSockets.push(socket);
69
+ }
70
+
71
+ #closeIdleSocket(socket: Socket): void {
72
+ // Remove from available sockets
73
+ const index = this.#availableSockets.indexOf(socket);
74
+ if (index !== -1) {
75
+ this.#availableSockets.splice(index, 1);
76
+ void socket.close();
77
+ }
78
+ }
79
+
80
+ async dispose(): Promise<void> {
81
+ this.#closed = true;
82
+
83
+ // Close all available sockets
84
+ const closePromises = this.#availableSockets.map((socket) =>
85
+ socket.close(),
86
+ );
87
+ this.#availableSockets.length = 0;
88
+
89
+ // Force close all in-use sockets immediately
90
+ const inUseClosePromises = Array.from(this.#inUseSockets).map(
91
+ (socket) => socket.close(),
92
+ );
93
+ this.#inUseSockets.clear();
94
+
95
+ await Promise.all([...closePromises, ...inUseClosePromises]);
96
+ }
97
+ }