@yume-chan/adb 0.0.18 → 0.0.20

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 (241) hide show
  1. package/CHANGELOG.json +54 -0
  2. package/CHANGELOG.md +25 -1
  3. package/README.md +220 -103
  4. package/esm/adb.d.ts +43 -60
  5. package/esm/adb.d.ts.map +1 -1
  6. package/esm/adb.js +68 -249
  7. package/esm/adb.js.map +1 -1
  8. package/esm/banner.d.ts +17 -0
  9. package/esm/banner.d.ts.map +1 -0
  10. package/esm/banner.js +67 -0
  11. package/esm/banner.js.map +1 -0
  12. package/esm/commands/base.d.ts +6 -6
  13. package/esm/commands/base.d.ts.map +1 -1
  14. package/esm/commands/base.js +8 -8
  15. package/esm/commands/framebuffer.d.ts +53 -53
  16. package/esm/commands/framebuffer.d.ts.map +1 -1
  17. package/esm/commands/framebuffer.js +46 -46
  18. package/esm/commands/framebuffer.js.map +1 -1
  19. package/esm/commands/index.d.ts +7 -8
  20. package/esm/commands/index.d.ts.map +1 -1
  21. package/esm/commands/index.js +7 -8
  22. package/esm/commands/index.js.map +1 -1
  23. package/esm/commands/power.d.ts +22 -22
  24. package/esm/commands/power.js +48 -48
  25. package/esm/commands/reverse.d.ts +40 -28
  26. package/esm/commands/reverse.d.ts.map +1 -1
  27. package/esm/commands/reverse.js +130 -100
  28. package/esm/commands/reverse.js.map +1 -1
  29. package/esm/commands/subprocess/command.d.ts +57 -51
  30. package/esm/commands/subprocess/command.d.ts.map +1 -1
  31. package/esm/commands/subprocess/command.js +86 -80
  32. package/esm/commands/subprocess/command.js.map +1 -1
  33. package/esm/commands/subprocess/index.d.ts +3 -3
  34. package/esm/commands/subprocess/index.js +3 -3
  35. package/esm/commands/subprocess/protocols/index.d.ts +3 -3
  36. package/esm/commands/subprocess/protocols/index.js +3 -3
  37. package/esm/commands/subprocess/protocols/none.d.ts +30 -35
  38. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  39. package/esm/commands/subprocess/protocols/none.js +65 -55
  40. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  41. package/esm/commands/subprocess/protocols/shell.d.ts +33 -38
  42. package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
  43. package/esm/commands/subprocess/protocols/shell.js +172 -162
  44. package/esm/commands/subprocess/protocols/shell.js.map +1 -1
  45. package/esm/commands/subprocess/protocols/types.d.ts +48 -49
  46. package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
  47. package/esm/commands/subprocess/protocols/types.js +1 -1
  48. package/esm/commands/subprocess/utils.d.ts +1 -1
  49. package/esm/commands/subprocess/utils.js +18 -18
  50. package/esm/commands/sync/index.d.ts +8 -7
  51. package/esm/commands/sync/index.d.ts.map +1 -1
  52. package/esm/commands/sync/index.js +8 -7
  53. package/esm/commands/sync/index.js.map +1 -1
  54. package/esm/commands/sync/list.d.ts +42 -40
  55. package/esm/commands/sync/list.d.ts.map +1 -1
  56. package/esm/commands/sync/list.js +67 -47
  57. package/esm/commands/sync/list.js.map +1 -1
  58. package/esm/commands/sync/pull.d.ts +13 -11
  59. package/esm/commands/sync/pull.d.ts.map +1 -1
  60. package/esm/commands/sync/pull.js +35 -37
  61. package/esm/commands/sync/pull.js.map +1 -1
  62. package/esm/commands/sync/push.d.ts +57 -7
  63. package/esm/commands/sync/push.d.ts.map +1 -1
  64. package/esm/commands/sync/push.js +89 -22
  65. package/esm/commands/sync/push.js.map +1 -1
  66. package/esm/commands/sync/request.d.ts +26 -23
  67. package/esm/commands/sync/request.d.ts.map +1 -1
  68. package/esm/commands/sync/request.js +40 -42
  69. package/esm/commands/sync/request.js.map +1 -1
  70. package/esm/commands/sync/response.d.ts +19 -19
  71. package/esm/commands/sync/response.d.ts.map +1 -1
  72. package/esm/commands/sync/response.js +54 -54
  73. package/esm/commands/sync/response.js.map +1 -1
  74. package/esm/commands/sync/socket.d.ts +22 -0
  75. package/esm/commands/sync/socket.d.ts.map +1 -0
  76. package/esm/commands/sync/socket.js +69 -0
  77. package/esm/commands/sync/socket.js.map +1 -0
  78. package/esm/commands/sync/stat.d.ts +72 -72
  79. package/esm/commands/sync/stat.d.ts.map +1 -1
  80. package/esm/commands/sync/stat.js +116 -104
  81. package/esm/commands/sync/stat.js.map +1 -1
  82. package/esm/commands/sync/sync.d.ts +65 -60
  83. package/esm/commands/sync/sync.d.ts.map +1 -1
  84. package/esm/commands/sync/sync.js +123 -149
  85. package/esm/commands/sync/sync.js.map +1 -1
  86. package/esm/commands/tcpip.d.ts +5 -5
  87. package/esm/commands/tcpip.d.ts.map +1 -1
  88. package/esm/commands/tcpip.js +20 -18
  89. package/esm/commands/tcpip.js.map +1 -1
  90. package/esm/{auth.d.ts → daemon/auth.d.ts} +50 -51
  91. package/esm/daemon/auth.d.ts.map +1 -0
  92. package/esm/{auth.js → daemon/auth.js} +95 -96
  93. package/esm/daemon/auth.js.map +1 -0
  94. package/esm/daemon/connection.d.ts +11 -0
  95. package/esm/daemon/connection.d.ts.map +1 -0
  96. package/esm/daemon/connection.js +2 -0
  97. package/esm/daemon/connection.js.map +1 -0
  98. package/esm/{crypto.d.ts → daemon/crypto.d.ts} +36 -36
  99. package/esm/daemon/crypto.d.ts.map +1 -0
  100. package/esm/{crypto.js → daemon/crypto.js} +244 -248
  101. package/esm/daemon/crypto.js.map +1 -0
  102. package/esm/daemon/device.d.ts +9 -0
  103. package/esm/daemon/device.d.ts.map +1 -0
  104. package/esm/daemon/device.js +2 -0
  105. package/esm/daemon/device.js.map +1 -0
  106. package/esm/daemon/dispatcher.d.ts +42 -0
  107. package/esm/daemon/dispatcher.d.ts.map +1 -0
  108. package/esm/{socket → daemon}/dispatcher.js +225 -234
  109. package/esm/daemon/dispatcher.js.map +1 -0
  110. package/esm/daemon/index.d.ts +8 -0
  111. package/esm/daemon/index.d.ts.map +1 -0
  112. package/esm/daemon/index.js +8 -0
  113. package/esm/daemon/index.js.map +1 -0
  114. package/esm/{packet.d.ts → daemon/packet.d.ts} +45 -46
  115. package/esm/daemon/packet.d.ts.map +1 -0
  116. package/esm/{packet.js → daemon/packet.js} +43 -48
  117. package/esm/daemon/packet.js.map +1 -0
  118. package/esm/daemon/socket.d.ts +58 -0
  119. package/esm/daemon/socket.d.ts.map +1 -0
  120. package/esm/{socket → daemon}/socket.js +121 -118
  121. package/esm/daemon/socket.js.map +1 -0
  122. package/esm/daemon/transport.d.ts +45 -0
  123. package/esm/daemon/transport.d.ts.map +1 -0
  124. package/esm/daemon/transport.js +184 -0
  125. package/esm/daemon/transport.js.map +1 -0
  126. package/esm/features.d.ts +10 -7
  127. package/esm/features.d.ts.map +1 -1
  128. package/esm/features.js +13 -10
  129. package/esm/features.js.map +1 -1
  130. package/esm/index.d.ts +7 -9
  131. package/esm/index.d.ts.map +1 -1
  132. package/esm/index.js +7 -9
  133. package/esm/index.js.map +1 -1
  134. package/esm/server/client.d.ts +78 -0
  135. package/esm/server/client.d.ts.map +1 -0
  136. package/esm/server/client.js +329 -0
  137. package/esm/server/client.js.map +1 -0
  138. package/esm/server/index.d.ts +3 -0
  139. package/esm/server/index.d.ts.map +1 -0
  140. package/esm/server/index.js +3 -0
  141. package/esm/server/index.js.map +1 -0
  142. package/esm/server/transport.d.ts +19 -0
  143. package/esm/server/transport.d.ts.map +1 -0
  144. package/esm/server/transport.js +44 -0
  145. package/esm/server/transport.js.map +1 -0
  146. package/esm/utils/auto-reset-event.d.ts +8 -9
  147. package/esm/utils/auto-reset-event.d.ts.map +1 -1
  148. package/esm/utils/auto-reset-event.js +33 -33
  149. package/esm/utils/auto-reset-event.js.map +1 -1
  150. package/esm/utils/base64.d.ts +27 -27
  151. package/esm/utils/base64.js +246 -246
  152. package/esm/utils/base64.js.map +1 -1
  153. package/esm/utils/conditional-variable.d.ts +8 -0
  154. package/esm/utils/conditional-variable.d.ts.map +1 -0
  155. package/esm/utils/conditional-variable.js +34 -0
  156. package/esm/utils/conditional-variable.js.map +1 -0
  157. package/esm/utils/hex.d.ts +3 -0
  158. package/esm/utils/hex.d.ts.map +1 -0
  159. package/esm/utils/hex.js +55 -0
  160. package/esm/utils/hex.js.map +1 -0
  161. package/esm/utils/index.d.ts +6 -3
  162. package/esm/utils/index.d.ts.map +1 -1
  163. package/esm/utils/index.js +6 -3
  164. package/esm/utils/index.js.map +1 -1
  165. package/esm/utils/no-op.d.ts +7 -0
  166. package/esm/utils/no-op.d.ts.map +1 -0
  167. package/esm/utils/no-op.js +12 -0
  168. package/esm/utils/no-op.js.map +1 -0
  169. package/package.json +14 -12
  170. package/src/adb.ts +47 -248
  171. package/src/banner.ts +82 -0
  172. package/src/commands/base.ts +1 -1
  173. package/src/commands/framebuffer.ts +4 -4
  174. package/src/commands/index.ts +7 -8
  175. package/src/commands/power.ts +2 -2
  176. package/src/commands/reverse.ts +90 -70
  177. package/src/commands/subprocess/command.ts +12 -4
  178. package/src/commands/subprocess/index.ts +3 -3
  179. package/src/commands/subprocess/protocols/index.ts +3 -3
  180. package/src/commands/subprocess/protocols/none.ts +36 -23
  181. package/src/commands/subprocess/protocols/shell.ts +94 -74
  182. package/src/commands/subprocess/protocols/types.ts +7 -7
  183. package/src/commands/subprocess/utils.ts +1 -1
  184. package/src/commands/sync/index.ts +8 -7
  185. package/src/commands/sync/list.ts +44 -25
  186. package/src/commands/sync/pull.ts +40 -45
  187. package/src/commands/sync/push.ts +158 -39
  188. package/src/commands/sync/request.ts +21 -16
  189. package/src/commands/sync/response.ts +10 -9
  190. package/src/commands/sync/socket.ts +99 -0
  191. package/src/commands/sync/stat.ts +47 -42
  192. package/src/commands/sync/sync.ts +73 -116
  193. package/src/commands/tcpip.ts +9 -7
  194. package/src/{auth.ts → daemon/auth.ts} +24 -23
  195. package/src/{crypto.ts → daemon/crypto.ts} +25 -33
  196. package/src/daemon/device.ts +14 -0
  197. package/src/{socket → daemon}/dispatcher.ts +95 -117
  198. package/src/daemon/index.ts +7 -0
  199. package/src/daemon/packet.ts +75 -0
  200. package/src/{socket → daemon}/socket.ts +71 -56
  201. package/src/daemon/transport.ts +269 -0
  202. package/src/features.ts +9 -6
  203. package/src/index.ts +3 -5
  204. package/src/server/client.ts +476 -0
  205. package/src/server/index.ts +2 -0
  206. package/src/server/transport.ts +77 -0
  207. package/src/utils/auto-reset-event.ts +14 -15
  208. package/src/utils/conditional-variable.ts +45 -0
  209. package/src/utils/hex.ts +58 -0
  210. package/src/utils/index.ts +6 -3
  211. package/src/utils/no-op.ts +12 -0
  212. package/tsconfig.build.json +12 -1
  213. package/tsconfig.build.tsbuildinfo +1 -1
  214. package/esm/auth.d.ts.map +0 -1
  215. package/esm/auth.js.map +0 -1
  216. package/esm/backend.d.ts +0 -9
  217. package/esm/backend.d.ts.map +0 -1
  218. package/esm/backend.js +0 -2
  219. package/esm/backend.js.map +0 -1
  220. package/esm/commands/install.d.ts +0 -4
  221. package/esm/commands/install.d.ts.map +0 -1
  222. package/esm/commands/install.js +0 -29
  223. package/esm/commands/install.js.map +0 -1
  224. package/esm/crypto.d.ts.map +0 -1
  225. package/esm/crypto.js.map +0 -1
  226. package/esm/packet.d.ts.map +0 -1
  227. package/esm/packet.js.map +0 -1
  228. package/esm/socket/dispatcher.d.ts +0 -60
  229. package/esm/socket/dispatcher.d.ts.map +0 -1
  230. package/esm/socket/dispatcher.js.map +0 -1
  231. package/esm/socket/index.d.ts +0 -3
  232. package/esm/socket/index.d.ts.map +0 -1
  233. package/esm/socket/index.js +0 -3
  234. package/esm/socket/index.js.map +0 -1
  235. package/esm/socket/socket.d.ts +0 -60
  236. package/esm/socket/socket.d.ts.map +0 -1
  237. package/esm/socket/socket.js.map +0 -1
  238. package/src/backend.ts +0 -14
  239. package/src/commands/install.ts +0 -40
  240. package/src/packet.ts +0 -90
  241. package/src/socket/index.ts +0 -2
@@ -1,235 +1,226 @@
1
- import { AsyncOperationManager, PromiseResolver } from "@yume-chan/async";
2
- import { AbortController, WritableStream, } from "@yume-chan/stream-extra";
3
- import { EMPTY_UINT8_ARRAY } from "@yume-chan/struct";
4
- import { AdbCommand, calculateChecksum, } from "../packet.js";
5
- import { decodeUtf8, encodeUtf8 } from "../utils/index.js";
6
- import { AdbSocketController } from "./socket.js";
7
- /**
8
- * The dispatcher is the "dumb" part of the connection handling logic.
9
- *
10
- * Except some options to change some minor behaviors,
11
- * its only job is forwarding packets between authenticated underlying streams
12
- * and abstracted socket objects.
13
- *
14
- * The `Adb` class is responsible for doing the authentication,
15
- * negotiating the options, and has shortcuts to high-level services.
16
- */
17
- export class AdbPacketDispatcher {
18
- // ADB socket id starts from 1
19
- // (0 means open failed)
20
- initializers = new AsyncOperationManager(1);
21
- /**
22
- * Socket local ID to the socket controller.
23
- */
24
- sockets = new Map();
25
- _writer;
26
- options;
27
- _closed = false;
28
- _disconnected = new PromiseResolver();
29
- get disconnected() {
30
- return this._disconnected.promise;
31
- }
32
- _incomingSocketHandlers = new Set();
33
- _abortController = new AbortController();
34
- constructor(connection, options) {
35
- this.options = options;
36
- connection.readable
37
- .pipeTo(new WritableStream({
38
- write: async (packet) => {
39
- switch (packet.command) {
40
- case AdbCommand.OK:
41
- this.handleOk(packet);
42
- break;
43
- case AdbCommand.Close:
44
- await this.handleClose(packet);
45
- break;
46
- case AdbCommand.Write:
47
- if (this.sockets.has(packet.arg1)) {
48
- await this.sockets
49
- .get(packet.arg1)
50
- .enqueue(packet.payload);
51
- await this.sendPacket(AdbCommand.OK, packet.arg1, packet.arg0);
52
- break;
53
- }
54
- throw new Error(`Unknown local socket id: ${packet.arg1}`);
55
- case AdbCommand.Open:
56
- await this.handleOpen(packet);
57
- break;
58
- default:
59
- // Junk data may only appear in the authentication phase,
60
- // since the dispatcher only works after authentication,
61
- // all packets should have a valid command.
62
- // (although it's possible that Adb added new commands in the future)
63
- throw new Error(`Unknown command: ${packet.command.toString(16)}`);
64
- }
65
- },
66
- }), {
67
- // There are multiple reasons for the pipe to stop,
68
- // (device disconnection, protocol error, or user abortion)
69
- // if the underlying streams are still open,
70
- // it's still possible to create another ADB connection.
71
- // So don't close `readable` here.
72
- preventCancel: true,
73
- signal: this._abortController.signal,
74
- })
75
- .then(() => {
76
- this.dispose();
77
- }, (e) => {
78
- if (!this._closed) {
79
- this._disconnected.reject(e);
80
- }
81
- this.dispose();
82
- });
83
- this._writer = connection.writable.getWriter();
84
- }
85
- handleOk(packet) {
86
- if (this.initializers.resolve(packet.arg1, packet.arg0)) {
87
- // Device successfully created the socket
88
- return;
89
- }
90
- const socket = this.sockets.get(packet.arg1);
91
- if (socket) {
92
- // Device has received last `WRTE` to the socket
93
- socket.ack();
94
- return;
95
- }
96
- // Maybe the device is responding to a packet of last connection
97
- // Tell the device to close the socket
98
- void this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
99
- }
100
- async handleClose(packet) {
101
- // If the socket is still pending
102
- if (packet.arg0 === 0 &&
103
- this.initializers.reject(packet.arg1, new Error("Socket open failed"))) {
104
- // Device failed to create the socket
105
- // (unknown service string, failed to execute command, etc.)
106
- // it doesn't break the connection,
107
- // so only reject the socket creation promise,
108
- // don't throw an error here.
109
- return;
110
- }
111
- // From https://android.googlesource.com/platform/packages/modules/adb/+/65d18e2c1cc48b585811954892311b28a4c3d188/adb.cpp#459
112
- /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
113
- * a failed OPEN only. However, due to a bug in previous ADB
114
- * versions, CLOSE(0, remote-id, "") was also used for normal
115
- * CLOSE() operations.
116
- */
117
- // Ignore `arg0` and search for the socket
118
- const socket = this.sockets.get(packet.arg1);
119
- if (socket) {
120
- // The device want to close the socket
121
- if (!socket.closed) {
122
- await this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
123
- }
124
- await socket.dispose();
125
- this.sockets.delete(packet.arg1);
126
- return;
127
- }
128
- // TODO: adb: is double closing an socket a catastrophic error?
129
- // If the client sends two `CLSE` packets for one socket,
130
- // the device may also respond with two `CLSE` packets.
131
- }
132
- /**
133
- * Add a handler for incoming socket.
134
- * @param handler A function to call with new incoming sockets. It must return `true` if it accepts the socket.
135
- * @returns A function to remove the handler.
136
- */
137
- onIncomingSocket(handler) {
138
- this._incomingSocketHandlers.add(handler);
139
- const remove = () => {
140
- this._incomingSocketHandlers.delete(handler);
141
- };
142
- remove.dispose = remove;
143
- return remove;
144
- }
145
- async handleOpen(packet) {
146
- // `AsyncOperationManager` doesn't support skipping IDs
147
- // Use `add` + `resolve` to simulate this behavior
148
- const [localId] = this.initializers.add();
149
- this.initializers.resolve(localId, undefined);
150
- const remoteId = packet.arg0;
151
- const serviceString = decodeUtf8(packet.payload);
152
- const controller = new AdbSocketController({
153
- dispatcher: this,
154
- localId,
155
- remoteId,
156
- localCreated: false,
157
- serviceString,
158
- });
159
- for (const handler of this._incomingSocketHandlers) {
160
- if (await handler(controller.socket)) {
161
- this.sockets.set(localId, controller);
162
- await this.sendPacket(AdbCommand.OK, localId, remoteId);
163
- return;
164
- }
165
- }
166
- await this.sendPacket(AdbCommand.Close, 0, remoteId);
167
- }
168
- async createSocket(serviceString) {
169
- if (this.options.appendNullToServiceString) {
170
- serviceString += "\0";
171
- }
172
- const [localId, initializer] = this.initializers.add();
173
- await this.sendPacket(AdbCommand.Open, localId, 0, serviceString);
174
- // Fulfilled by `handleOk`
175
- const remoteId = await initializer;
176
- const controller = new AdbSocketController({
177
- dispatcher: this,
178
- localId,
179
- remoteId,
180
- localCreated: true,
181
- serviceString,
182
- });
183
- this.sockets.set(localId, controller);
184
- return controller.socket;
185
- }
186
- async sendPacket(packetOrCommand, arg0, arg1, payload = EMPTY_UINT8_ARRAY) {
187
- let init;
188
- if (arg0 === undefined) {
189
- init = packetOrCommand;
190
- }
191
- else {
192
- if (typeof payload === "string") {
193
- payload = encodeUtf8(payload);
194
- }
195
- init = {
196
- command: packetOrCommand,
197
- arg0: arg0,
198
- arg1: arg1,
199
- payload,
200
- };
201
- }
202
- if (init.payload &&
203
- init.payload.byteLength > this.options.maxPayloadSize) {
204
- throw new Error("payload too large");
205
- }
206
- if (this.options.calculateChecksum) {
207
- calculateChecksum(init);
208
- }
209
- else {
210
- init.checksum = 0;
211
- }
212
- await this._writer.ready;
213
- await this._writer.write(init);
214
- }
215
- async close() {
216
- // Send `CLSE` packets for all sockets
217
- await Promise.all(Array.from(this.sockets.values(), (socket) => socket.close()));
218
- // Stop receiving
219
- // It's possible that we haven't received all `CLSE` confirm packets,
220
- // but it doesn't matter, the next connection can cope with them.
221
- this._closed = true;
222
- this._abortController.abort();
223
- this._writer.releaseLock();
224
- // `pipe().then()` will call `dispose`
225
- }
226
- dispose() {
227
- for (const socket of this.sockets.values()) {
228
- socket.dispose().catch((e) => {
229
- void e;
230
- });
231
- }
232
- this._disconnected.resolve();
233
- }
234
- }
1
+ import { AsyncOperationManager, PromiseResolver } from "@yume-chan/async";
2
+ import { AbortController, ConsumableWritableStream, WritableStream, } from "@yume-chan/stream-extra";
3
+ import { EMPTY_UINT8_ARRAY } from "@yume-chan/struct";
4
+ import { decodeUtf8, encodeUtf8, unreachable } from "../utils/index.js";
5
+ import { AdbCommand, calculateChecksum } from "./packet.js";
6
+ import { AdbDaemonSocketController } from "./socket.js";
7
+ /**
8
+ * The dispatcher is the "dumb" part of the connection handling logic.
9
+ *
10
+ * Except some options to change some minor behaviors,
11
+ * its only job is forwarding packets between authenticated underlying streams
12
+ * and abstracted socket objects.
13
+ *
14
+ * The `Adb` class is responsible for doing the authentication,
15
+ * negotiating the options, and has shortcuts to high-level services.
16
+ */
17
+ export class AdbPacketDispatcher {
18
+ // ADB socket id starts from 1
19
+ // (0 means open failed)
20
+ #initializers = new AsyncOperationManager(1);
21
+ /**
22
+ * Socket local ID to the socket controller.
23
+ */
24
+ #sockets = new Map();
25
+ #writer;
26
+ options;
27
+ #closed = false;
28
+ #disconnected = new PromiseResolver();
29
+ get disconnected() {
30
+ return this.#disconnected.promise;
31
+ }
32
+ #incomingSocketHandlers = new Map();
33
+ #readAbortController = new AbortController();
34
+ constructor(connection, options) {
35
+ this.options = options;
36
+ connection.readable
37
+ .pipeTo(new WritableStream({
38
+ write: async (packet) => {
39
+ switch (packet.command) {
40
+ case AdbCommand.OK:
41
+ this.handleOk(packet);
42
+ break;
43
+ case AdbCommand.Close:
44
+ await this.handleClose(packet);
45
+ break;
46
+ case AdbCommand.Write:
47
+ if (this.#sockets.has(packet.arg1)) {
48
+ await this.#sockets
49
+ .get(packet.arg1)
50
+ .enqueue(packet.payload);
51
+ await this.sendPacket(AdbCommand.OK, packet.arg1, packet.arg0);
52
+ break;
53
+ }
54
+ throw new Error(`Unknown local socket id: ${packet.arg1}`);
55
+ case AdbCommand.Open:
56
+ await this.handleOpen(packet);
57
+ break;
58
+ default:
59
+ // Junk data may only appear in the authentication phase,
60
+ // since the dispatcher only works after authentication,
61
+ // all packets should have a valid command.
62
+ // (although it's possible that Adb added new commands in the future)
63
+ throw new Error(`Unknown command: ${packet.command.toString(16)}`);
64
+ }
65
+ },
66
+ }), {
67
+ // There are multiple reasons for the pipe to stop,
68
+ // (device disconnection, protocol error, or user abortion)
69
+ // if the underlying streams are still open,
70
+ // it's still possible to create another ADB connection.
71
+ // So don't close `readable` here.
72
+ preventCancel: true,
73
+ signal: this.#readAbortController.signal,
74
+ })
75
+ .then(() => {
76
+ this.dispose();
77
+ }, (e) => {
78
+ if (!this.#closed) {
79
+ this.#disconnected.reject(e);
80
+ }
81
+ this.dispose();
82
+ });
83
+ this.#writer = connection.writable.getWriter();
84
+ }
85
+ handleOk(packet) {
86
+ if (this.#initializers.resolve(packet.arg1, packet.arg0)) {
87
+ // Device successfully created the socket
88
+ return;
89
+ }
90
+ const socket = this.#sockets.get(packet.arg1);
91
+ if (socket) {
92
+ // Device has received last `WRTE` to the socket
93
+ socket.ack();
94
+ return;
95
+ }
96
+ // Maybe the device is responding to a packet of last connection
97
+ // Tell the device to close the socket
98
+ void this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
99
+ }
100
+ async handleClose(packet) {
101
+ // If the socket is still pending
102
+ if (packet.arg0 === 0 &&
103
+ this.#initializers.reject(packet.arg1, new Error("Socket open failed"))) {
104
+ // Device failed to create the socket
105
+ // (unknown service string, failed to execute command, etc.)
106
+ // it doesn't break the connection,
107
+ // so only reject the socket creation promise,
108
+ // don't throw an error here.
109
+ return;
110
+ }
111
+ // From https://android.googlesource.com/platform/packages/modules/adb/+/65d18e2c1cc48b585811954892311b28a4c3d188/adb.cpp#459
112
+ /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
113
+ * a failed OPEN only. However, due to a bug in previous ADB
114
+ * versions, CLOSE(0, remote-id, "") was also used for normal
115
+ * CLOSE() operations.
116
+ */
117
+ // Ignore `arg0` and search for the socket
118
+ const socket = this.#sockets.get(packet.arg1);
119
+ if (socket) {
120
+ // The device want to close the socket
121
+ if (!socket.closed) {
122
+ await this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
123
+ }
124
+ await socket.dispose();
125
+ this.#sockets.delete(packet.arg1);
126
+ return;
127
+ }
128
+ // TODO: adb: is double closing an socket a catastrophic error?
129
+ // If the client sends two `CLSE` packets for one socket,
130
+ // the device may also respond with two `CLSE` packets.
131
+ }
132
+ addReverseTunnel(service, handler) {
133
+ this.#incomingSocketHandlers.set(service, handler);
134
+ }
135
+ removeReverseTunnel(address) {
136
+ this.#incomingSocketHandlers.delete(address);
137
+ }
138
+ clearReverseTunnels() {
139
+ this.#incomingSocketHandlers.clear();
140
+ }
141
+ async handleOpen(packet) {
142
+ // `AsyncOperationManager` doesn't support skipping IDs
143
+ // Use `add` + `resolve` to simulate this behavior
144
+ const [localId] = this.#initializers.add();
145
+ this.#initializers.resolve(localId, undefined);
146
+ const remoteId = packet.arg0;
147
+ let service = decodeUtf8(packet.payload);
148
+ if (service.endsWith("\0")) {
149
+ service = service.substring(0, service.length - 1);
150
+ }
151
+ const handler = this.#incomingSocketHandlers.get(service);
152
+ if (!handler) {
153
+ await this.sendPacket(AdbCommand.Close, 0, remoteId);
154
+ return;
155
+ }
156
+ const controller = new AdbDaemonSocketController({
157
+ dispatcher: this,
158
+ localId,
159
+ remoteId,
160
+ localCreated: false,
161
+ service,
162
+ });
163
+ try {
164
+ await handler(controller.socket);
165
+ this.#sockets.set(localId, controller);
166
+ await this.sendPacket(AdbCommand.OK, localId, remoteId);
167
+ }
168
+ catch (e) {
169
+ await this.sendPacket(AdbCommand.Close, 0, remoteId);
170
+ }
171
+ }
172
+ async createSocket(service) {
173
+ if (this.options.appendNullToServiceString) {
174
+ service += "\0";
175
+ }
176
+ const [localId, initializer] = this.#initializers.add();
177
+ await this.sendPacket(AdbCommand.Open, localId, 0, service);
178
+ // Fulfilled by `handleOk`
179
+ const remoteId = await initializer;
180
+ const controller = new AdbDaemonSocketController({
181
+ dispatcher: this,
182
+ localId,
183
+ remoteId,
184
+ localCreated: true,
185
+ service,
186
+ });
187
+ this.#sockets.set(localId, controller);
188
+ return controller.socket;
189
+ }
190
+ async sendPacket(command, arg0, arg1, payload = EMPTY_UINT8_ARRAY) {
191
+ if (typeof payload === "string") {
192
+ payload = encodeUtf8(payload);
193
+ }
194
+ if (payload.byteLength > this.options.maxPayloadSize) {
195
+ throw new Error("payload too large");
196
+ }
197
+ await ConsumableWritableStream.write(this.#writer, {
198
+ command,
199
+ arg0,
200
+ arg1,
201
+ payload,
202
+ checksum: this.options.calculateChecksum
203
+ ? calculateChecksum(payload)
204
+ : 0,
205
+ magic: command ^ 0xffffffff,
206
+ });
207
+ }
208
+ async close() {
209
+ // Send `CLSE` packets for all sockets
210
+ await Promise.all(Array.from(this.#sockets.values(), (socket) => socket.close()));
211
+ // Stop receiving
212
+ // It's possible that we haven't received all `CLSE` confirm packets,
213
+ // but it doesn't matter, the next connection can cope with them.
214
+ this.#closed = true;
215
+ this.#readAbortController.abort();
216
+ this.#writer.releaseLock();
217
+ // `pipe().then()` will call `dispose`
218
+ }
219
+ dispose() {
220
+ for (const socket of this.#sockets.values()) {
221
+ socket.dispose().catch(unreachable);
222
+ }
223
+ this.#disconnected.resolve();
224
+ }
225
+ }
235
226
  //# sourceMappingURL=dispatcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dispatcher.js","sourceRoot":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export * from "./dispatcher.js";
5
+ export * from "./packet.js";
6
+ export * from "./socket.js";
7
+ export * from "./transport.js";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/daemon/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from "./auth.js";
2
+ export * from "./crypto.js";
3
+ export * from "./device.js";
4
+ export * from "./dispatcher.js";
5
+ export * from "./packet.js";
6
+ export * from "./socket.js";
7
+ export * from "./transport.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/daemon/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC"}
@@ -1,47 +1,46 @@
1
- import { TransformStream } from "@yume-chan/stream-extra";
2
- import Struct from "@yume-chan/struct";
3
- export declare enum AdbCommand {
4
- Auth = 1213486401,
5
- Close = 1163086915,
6
- Connect = 1314410051,
7
- OK = 1497451343,
8
- Open = 1313165391,
9
- Write = 1163154007
10
- }
11
- export declare const AdbPacketHeader: Struct<{
12
- command: number;
13
- arg0: number;
14
- arg1: number;
15
- payloadLength: number;
16
- checksum: number;
17
- magic: number;
18
- }, never, Record<never, never>, undefined>;
19
- export type AdbPacketHeader = typeof AdbPacketHeader["TDeserializeResult"];
20
- export declare const AdbPacket: Struct<{
21
- command: number;
22
- arg0: number;
23
- arg1: number;
24
- payloadLength: number;
25
- checksum: number;
26
- magic: number;
27
- payload: Uint8Array;
28
- }, "payloadLength", Record<never, never>, undefined>;
29
- export type AdbPacket = typeof AdbPacket["TDeserializeResult"];
30
- /**
31
- * `AdbPacketData` contains all the useful fields of `AdbPacket`.
32
- *
33
- * `AdbBackend#connect` will return a `ReadableStream<AdbPacketData>`,
34
- * so each backend can encode `AdbPacket` in different ways.
35
- *
36
- * `AdbBackend#connect` will return a `WritableStream<AdbPacketInit>`,
37
- * however, `AdbPacketDispatcher` will transform `AdbPacketData` to `AdbPacketInit` for you,
38
- * so `AdbSocket#writable#write` only needs `AdbPacketData`.
39
- */
40
- export type AdbPacketData = Omit<typeof AdbPacket["TInit"], "checksum" | "magic">;
41
- export type AdbPacketInit = Omit<typeof AdbPacket["TInit"], "magic">;
42
- export declare function calculateChecksum(payload: Uint8Array): number;
43
- export declare function calculateChecksum(init: AdbPacketData): AdbPacketInit;
44
- export declare class AdbPacketSerializeStream extends TransformStream<AdbPacketInit, Uint8Array> {
45
- constructor();
46
- }
1
+ import { ConsumableTransformStream } from "@yume-chan/stream-extra";
2
+ import Struct from "@yume-chan/struct";
3
+ export declare enum AdbCommand {
4
+ Auth = 1213486401,
5
+ Close = 1163086915,
6
+ Connect = 1314410051,
7
+ OK = 1497451343,
8
+ Open = 1313165391,
9
+ Write = 1163154007
10
+ }
11
+ export declare const AdbPacketHeader: Struct<{
12
+ command: number;
13
+ arg0: number;
14
+ arg1: number;
15
+ payloadLength: number;
16
+ checksum: number;
17
+ magic: number;
18
+ }, never, Record<never, never>, undefined>;
19
+ export type AdbPacketHeader = (typeof AdbPacketHeader)["TDeserializeResult"];
20
+ export declare const AdbPacket: Struct<{
21
+ command: number;
22
+ arg0: number;
23
+ arg1: number;
24
+ payloadLength: number;
25
+ checksum: number;
26
+ magic: number;
27
+ payload: Uint8Array;
28
+ }, "payloadLength", Record<never, never>, undefined>;
29
+ export type AdbPacket = (typeof AdbPacket)["TDeserializeResult"];
30
+ /**
31
+ * `AdbPacketData` contains all the useful fields of `AdbPacket`.
32
+ *
33
+ * `AdvDaemonConnection#connect` will return a `ReadableStream<AdbPacketData>`,
34
+ * allow each connection to encode `AdbPacket` in different methods.
35
+ *
36
+ * `AdbDaemonConnection#connect` will return a `WritableStream<AdbPacketInit>`,
37
+ * however, `AdbDaemonTransport` will transform `AdbPacketData` to `AdbPacketInit` for you,
38
+ * so `AdbSocket#writable#write` only needs `AdbPacketData`.
39
+ */
40
+ export type AdbPacketData = Omit<(typeof AdbPacket)["TInit"], "checksum" | "magic">;
41
+ export type AdbPacketInit = (typeof AdbPacket)["TInit"];
42
+ export declare function calculateChecksum(payload: Uint8Array): number;
43
+ export declare class AdbPacketSerializeStream extends ConsumableTransformStream<AdbPacketInit, Uint8Array> {
44
+ constructor();
45
+ }
47
46
  //# sourceMappingURL=packet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"packet.d.ts","sourceRoot":"","sources":["../../src/daemon/packet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,MAAM,MAAM,mBAAmB,CAAC;AAEvC,oBAAY,UAAU;IAClB,IAAI,aAAa;IACjB,KAAK,aAAa;IAClB,OAAO,aAAa;IACpB,EAAE,aAAa;IACf,IAAI,aAAa;IACjB,KAAK,aAAa;CACrB;AAED,eAAO,MAAM,eAAe;;;;;;;0CAMT,CAAC;AAEpB,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,oBAAoB,CAAC,CAAC;AAI7E,eAAO,MAAM,SAAS;;;;;;;;oDAEsC,CAAC;AAE7D,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,oBAAoB,CAAC,CAAC;AAEjE;;;;;;;;;GASG;AACH,MAAM,MAAM,aAAa,GAAG,IAAI,CAC5B,CAAC,OAAO,SAAS,CAAC,CAAC,OAAO,CAAC,EAC3B,UAAU,GAAG,OAAO,CACvB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC;AAExD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,UAAU,GAAG,MAAM,CAE7D;AAED,qBAAa,wBAAyB,SAAQ,yBAAyB,CACnE,aAAa,EACb,UAAU,CACb;;CAoBA"}