@yume-chan/adb 0.0.15 → 0.0.16

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 (103) hide show
  1. package/CHANGELOG.json +21 -0
  2. package/CHANGELOG.md +11 -1
  3. package/LICENSE +21 -21
  4. package/README.md +247 -247
  5. package/esm/adb.d.ts +7 -0
  6. package/esm/adb.d.ts.map +1 -1
  7. package/esm/adb.js +7 -0
  8. package/esm/adb.js.map +1 -1
  9. package/esm/commands/subprocess/protocols/none.d.ts +1 -0
  10. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  11. package/esm/commands/subprocess/protocols/none.js +6 -5
  12. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  13. package/esm/commands/sync/stat.d.ts.map +1 -1
  14. package/esm/commands/sync/stat.js.map +1 -1
  15. package/esm/socket/dispatcher.d.ts +4 -1
  16. package/esm/socket/dispatcher.d.ts.map +1 -1
  17. package/esm/socket/dispatcher.js +11 -8
  18. package/esm/socket/dispatcher.js.map +1 -1
  19. package/esm/socket/socket.d.ts +6 -1
  20. package/esm/socket/socket.d.ts.map +1 -1
  21. package/esm/socket/socket.js +17 -7
  22. package/esm/socket/socket.js.map +1 -1
  23. package/esm/stream/buffered.d.ts +6 -2
  24. package/esm/stream/buffered.d.ts.map +1 -1
  25. package/esm/stream/buffered.js +67 -47
  26. package/esm/stream/buffered.js.map +1 -1
  27. package/esm/stream/detect.js +1 -0
  28. package/esm/stream/detect.js.map +1 -1
  29. package/esm/stream/detect.polyfill.d.ts.map +1 -1
  30. package/esm/stream/detect.polyfill.js +0 -5
  31. package/esm/stream/detect.polyfill.js.map +1 -1
  32. package/esm/stream/transform.d.ts +20 -0
  33. package/esm/stream/transform.d.ts.map +1 -1
  34. package/esm/stream/transform.js +7 -1
  35. package/esm/stream/transform.js.map +1 -1
  36. package/package.json +10 -10
  37. package/src/adb.ts +280 -273
  38. package/src/auth.ts +173 -173
  39. package/src/backend.ts +11 -11
  40. package/src/commands/base.ts +11 -11
  41. package/src/commands/framebuffer.ts +73 -73
  42. package/src/commands/index.ts +8 -8
  43. package/src/commands/install.ts +30 -30
  44. package/src/commands/power.ts +54 -54
  45. package/src/commands/reverse.ts +140 -140
  46. package/src/commands/subprocess/index.ts +139 -139
  47. package/src/commands/subprocess/protocols/none.ts +70 -68
  48. package/src/commands/subprocess/protocols/shell.ts +190 -190
  49. package/src/commands/subprocess/protocols/types.ts +59 -59
  50. package/src/commands/subprocess/utils.ts +20 -20
  51. package/src/commands/sync/index.ts +7 -7
  52. package/src/commands/sync/list.ts +86 -86
  53. package/src/commands/sync/pull.ts +46 -46
  54. package/src/commands/sync/push.ts +41 -41
  55. package/src/commands/sync/request.ts +50 -50
  56. package/src/commands/sync/response.ts +64 -64
  57. package/src/commands/sync/stat.ts +151 -150
  58. package/src/commands/sync/sync.ts +181 -181
  59. package/src/commands/tcpip.ts +21 -21
  60. package/src/crypto.ts +296 -296
  61. package/src/features.ts +9 -9
  62. package/src/index.ts +11 -11
  63. package/src/packet.ts +80 -80
  64. package/src/socket/dispatcher.ts +305 -304
  65. package/src/socket/index.ts +2 -2
  66. package/src/socket/socket.ts +155 -144
  67. package/src/stream/buffered.ts +168 -147
  68. package/src/stream/detect.native.ts +362 -362
  69. package/src/stream/detect.polyfill.ts +22 -28
  70. package/src/stream/detect.ts +7 -5
  71. package/src/stream/index.ts +3 -3
  72. package/src/stream/transform.ts +474 -447
  73. package/src/utils/auto-reset-event.ts +41 -41
  74. package/src/utils/base64.ts +306 -306
  75. package/src/utils/index.ts +3 -3
  76. package/tsconfig.build.json +3 -3
  77. package/tsconfig.build.tsbuildinfo +1 -1
  78. package/tsconfig.test.json +9 -10
  79. package/web-streams-polyfill-4.0.0-beta.2.tgz +0 -0
  80. package/esm/commands/subprocess/legacy.d.ts +0 -35
  81. package/esm/commands/subprocess/legacy.d.ts.map +0 -1
  82. package/esm/commands/subprocess/legacy.js +0 -53
  83. package/esm/commands/subprocess/legacy.js.map +0 -1
  84. package/esm/commands/subprocess/protocol.d.ts +0 -39
  85. package/esm/commands/subprocess/protocol.d.ts.map +0 -1
  86. package/esm/commands/subprocess/protocol.js +0 -157
  87. package/esm/commands/subprocess/protocol.js.map +0 -1
  88. package/esm/commands/subprocess/types.d.ts +0 -50
  89. package/esm/commands/subprocess/types.d.ts.map +0 -1
  90. package/esm/commands/subprocess/types.js +0 -2
  91. package/esm/commands/subprocess/types.js.map +0 -1
  92. package/esm/socket/controller.d.ts +0 -37
  93. package/esm/socket/controller.d.ts.map +0 -1
  94. package/esm/socket/controller.js +0 -66
  95. package/esm/socket/controller.js.map +0 -1
  96. package/esm/stream/detect.bak.d.ts +0 -240
  97. package/esm/stream/detect.bak.d.ts.map +0 -1
  98. package/esm/stream/detect.bak.js +0 -60
  99. package/esm/stream/detect.bak.js.map +0 -1
  100. package/esm/utils/encoding.d.ts +0 -3
  101. package/esm/utils/encoding.d.ts.map +0 -1
  102. package/esm/utils/encoding.js +0 -11
  103. package/esm/utils/encoding.js.map +0 -1
@@ -1,304 +1,305 @@
1
- import { AsyncOperationManager, PromiseResolver } from '@yume-chan/async';
2
- import type { RemoveEventListener } from '@yume-chan/event';
3
- import type { ValueOrPromise } from "@yume-chan/struct";
4
-
5
- import { AdbCommand, calculateChecksum, type AdbPacketData, type AdbPacketInit } from '../packet.js';
6
- import { AbortController, WritableStream, WritableStreamDefaultWriter, type ReadableWritablePair } from '../stream/index.js';
7
- import { decodeUtf8, encodeUtf8 } from '../utils/index.js';
8
- import { AdbSocket, AdbSocketController } from './socket.js';
9
-
10
- const EmptyUint8Array = new Uint8Array(0);
11
-
12
- export interface AdbPacketDispatcherOptions {
13
- calculateChecksum: boolean;
14
- /**
15
- * Before Android 9.0, ADB uses `char*` to parse service string,
16
- * thus requires a null character to terminate.
17
- *
18
- * Usually it should have the same value as `calculateChecksum`.
19
- */
20
- appendNullToServiceString: boolean;
21
- maxPayloadSize: number;
22
- }
23
-
24
- export type AdbIncomingSocketHandler = (socket: AdbSocket) => ValueOrPromise<boolean>;
25
-
26
- export interface Closeable {
27
- close(): ValueOrPromise<void>;
28
- }
29
-
30
- /**
31
- * The dispatcher is the "dumb" part of the connection handling logic.
32
- *
33
- * Except some options to change some minor behaviors,
34
- * its only job is forwarding packets between authenticated underlying streams
35
- * and abstracted socket objects.
36
- *
37
- * The `Adb` class is responsible for doing the authentication,
38
- * negotiating the options, and has shortcuts to high-level services.
39
- */
40
- export class AdbPacketDispatcher implements Closeable {
41
- // ADB socket id starts from 1
42
- // (0 means open failed)
43
- private readonly initializers = new AsyncOperationManager(1);
44
- private readonly sockets = new Map<number, AdbSocketController>();
45
-
46
- private _writer!: WritableStreamDefaultWriter<AdbPacketInit>;
47
-
48
- public readonly options: AdbPacketDispatcherOptions;
49
-
50
- private _closed = false;
51
- private _disconnected = new PromiseResolver<void>();
52
- public get disconnected() { return this._disconnected.promise; }
53
-
54
- private _incomingSocketHandlers: Set<AdbIncomingSocketHandler> = new Set();
55
-
56
- private _abortController = new AbortController();
57
-
58
- public constructor(
59
- connection: ReadableWritablePair<AdbPacketData, AdbPacketInit>,
60
- options: AdbPacketDispatcherOptions
61
- ) {
62
- this.options = options;
63
-
64
- connection.readable
65
- .pipeTo(new WritableStream({
66
- write: async (packet) => {
67
- switch (packet.command) {
68
- case AdbCommand.OK:
69
- this.handleOk(packet);
70
- break;
71
- case AdbCommand.Close:
72
- await this.handleClose(packet);
73
- break;
74
- case AdbCommand.Write:
75
- if (this.sockets.has(packet.arg1)) {
76
- await this.sockets.get(packet.arg1)!.enqueue(packet.payload);
77
- await this.sendPacket(AdbCommand.OK, packet.arg1, packet.arg0);
78
- break;
79
- }
80
- throw new Error(`Unknown local socket id: ${packet.arg1}`);
81
- case AdbCommand.Open:
82
- await this.handleOpen(packet);
83
- break;
84
- default:
85
- // Junk data may only appear in the authentication phase,
86
- // since the dispatcher only works after authentication,
87
- // all packets should have a valid command.
88
- // (although it's possible that Adb added new commands in the future)
89
- throw new Error(`Unknown command: ${packet.command.toString(16)}`);
90
- }
91
- },
92
- }), {
93
- // There are multiple reasons for the pipe to stop,
94
- // (device disconnection, protocol error, or user abortion)
95
- // if the underlying streams are still open,
96
- // it's still possible to create another ADB connection.
97
- // So don't close `readable` here.
98
- preventCancel: true,
99
- signal: this._abortController.signal,
100
- })
101
- .then(() => {
102
- this.dispose();
103
- }, (e) => {
104
- // https://github.com/MattiasBuelens/web-streams-polyfill/issues/115
105
- // `e` is always `AbortError` (instead of what I give in `abortController.abort()`)
106
- // so we can't check if `e` is a real error.
107
- if (!this._closed) {
108
- this._disconnected.reject(e);
109
- }
110
- this.dispose();
111
- });
112
-
113
- this._writer = connection.writable.getWriter();
114
- }
115
-
116
- private handleOk(packet: AdbPacketData) {
117
- if (this.initializers.resolve(packet.arg1, packet.arg0)) {
118
- // Device successfully created the socket
119
- return;
120
- }
121
-
122
- const socket = this.sockets.get(packet.arg1);
123
- if (socket) {
124
- // Device has received last `WRTE` to the socket
125
- socket.ack();
126
- return;
127
- }
128
-
129
- // Maybe the device is responding to a packet of last connection
130
- // Tell the device to close the socket
131
- this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
132
- }
133
-
134
- private async handleClose(packet: AdbPacketData) {
135
- // From https://android.googlesource.com/platform/packages/modules/adb/+/65d18e2c1cc48b585811954892311b28a4c3d188/adb.cpp#459
136
- /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
137
- * a failed OPEN only. However, due to a bug in previous ADB
138
- * versions, CLOSE(0, remote-id, "") was also used for normal
139
- * CLOSE() operations.
140
- */
141
-
142
- // If the socket is still pending
143
- if (packet.arg0 === 0 &&
144
- this.initializers.reject(packet.arg1, new Error('Socket open failed'))) {
145
- // Device failed to create the socket
146
- // (unknown service string, failed to execute command, etc.)
147
- // it doesn't break the connection,
148
- // so only reject the socket creation promise,
149
- // don't throw an error here.
150
- return;
151
- }
152
-
153
- // Ignore `arg0` and search for the socket
154
- const socket = this.sockets.get(packet.arg1);
155
- if (socket) {
156
- // The device want to close the socket
157
- if (!socket.closed) {
158
- await this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
159
- }
160
- socket.dispose();
161
- this.sockets.delete(packet.arg1);
162
- return;
163
- }
164
-
165
- // TODO: adb: is double closing an socket a catastrophic error?
166
- // If the client sends two `CLSE` packets for one socket,
167
- // the device may also respond with two `CLSE` packets.
168
- }
169
-
170
- public addIncomingSocketHandler(handler: AdbIncomingSocketHandler): RemoveEventListener {
171
- this._incomingSocketHandlers.add(handler);
172
- const remove = () => {
173
- this._incomingSocketHandlers.delete(handler);
174
- };
175
- remove.dispose = remove;
176
- return remove;
177
- }
178
-
179
- private async handleOpen(packet: AdbPacketData) {
180
- // AsyncOperationManager doesn't support get and skip an ID
181
- // Use `add` + `resolve` to simulate this behavior
182
- const [localId] = this.initializers.add<number>();
183
- this.initializers.resolve(localId, undefined);
184
-
185
- const remoteId = packet.arg0;
186
- const serviceString = decodeUtf8(packet.payload);
187
-
188
- const controller = new AdbSocketController({
189
- dispatcher: this,
190
- localId,
191
- remoteId,
192
- localCreated: false,
193
- serviceString,
194
- });
195
-
196
- for (const handler of this._incomingSocketHandlers) {
197
- if (await handler(controller.socket)) {
198
- this.sockets.set(localId, controller);
199
- await this.sendPacket(AdbCommand.OK, localId, remoteId);
200
- return;
201
- }
202
- }
203
-
204
- await this.sendPacket(AdbCommand.Close, 0, remoteId);
205
- }
206
-
207
- public async createSocket(serviceString: string): Promise<AdbSocket> {
208
- if (this.options.appendNullToServiceString) {
209
- serviceString += '\0';
210
- }
211
-
212
- const [localId, initializer] = this.initializers.add<number>();
213
- await this.sendPacket(
214
- AdbCommand.Open,
215
- localId,
216
- 0,
217
- serviceString
218
- );
219
-
220
- // Fulfilled by `handleOk`
221
- const remoteId = await initializer;
222
- const controller = new AdbSocketController({
223
- dispatcher: this,
224
- localId,
225
- remoteId,
226
- localCreated: true,
227
- serviceString,
228
- });
229
- this.sockets.set(localId, controller);
230
-
231
- return controller.socket;
232
- }
233
-
234
- public sendPacket(packet: AdbPacketInit): Promise<void>;
235
- public sendPacket(
236
- command: AdbCommand,
237
- arg0: number,
238
- arg1: number,
239
- payload?: string | Uint8Array
240
- ): Promise<void>;
241
- public async sendPacket(
242
- packetOrCommand: AdbPacketInit | AdbCommand,
243
- arg0?: number,
244
- arg1?: number,
245
- payload: string | Uint8Array = EmptyUint8Array,
246
- ): Promise<void> {
247
- let init: AdbPacketData;
248
- if (arg0 === undefined) {
249
- init = packetOrCommand as AdbPacketInit;
250
- } else {
251
- if (typeof payload === 'string') {
252
- payload = encodeUtf8(payload);
253
- }
254
-
255
- init = {
256
- command: packetOrCommand as AdbCommand,
257
- arg0: arg0 as number,
258
- arg1: arg1 as number,
259
- payload,
260
- };
261
- }
262
-
263
- if (init.payload &&
264
- init.payload.byteLength > this.options.maxPayloadSize) {
265
- throw new Error('payload too large');
266
- }
267
-
268
- if (this.options.calculateChecksum) {
269
- calculateChecksum(init);
270
- } else {
271
- (init as AdbPacketInit).checksum = 0;
272
- }
273
-
274
- await this._writer.ready;
275
- await this._writer.write(init as AdbPacketInit);
276
- }
277
-
278
- public async close() {
279
- // Send `CLSE` packets for all sockets
280
- await Promise.all(
281
- Array.from(
282
- this.sockets.values(),
283
- socket => socket.close(),
284
- )
285
- );
286
-
287
- // Stop receiving
288
- // It's possible that we haven't received all `CLSE` confirm packets,
289
- // but it doesn't matter, the next connection can cope with them.
290
- this._closed = true;
291
- this._abortController.abort();
292
- this._writer.releaseLock();
293
-
294
- // `pipe().then()` will call `dispose`
295
- }
296
-
297
- private dispose() {
298
- for (const socket of this.sockets.values()) {
299
- socket.dispose();
300
- }
301
-
302
- this._disconnected.resolve();
303
- }
304
- }
1
+ import { AsyncOperationManager, PromiseResolver } from '@yume-chan/async';
2
+ import type { RemoveEventListener } from '@yume-chan/event';
3
+ import { EMPTY_UINT8_ARRAY, type ValueOrPromise } from "@yume-chan/struct";
4
+
5
+ import { AdbCommand, calculateChecksum, type AdbPacketData, type AdbPacketInit } from '../packet.js';
6
+ import { AbortController, WritableStream, WritableStreamDefaultWriter, type ReadableWritablePair } from '../stream/index.js';
7
+ import { decodeUtf8, encodeUtf8 } from '../utils/index.js';
8
+ import { AdbSocket, AdbSocketController } from './socket.js';
9
+
10
+ export interface AdbPacketDispatcherOptions {
11
+ calculateChecksum: boolean;
12
+ /**
13
+ * Before Android 9.0, ADB uses `char*` to parse service string,
14
+ * thus requires a null character to terminate.
15
+ *
16
+ * Usually it should have the same value as `calculateChecksum`.
17
+ */
18
+ appendNullToServiceString: boolean;
19
+ maxPayloadSize: number;
20
+ }
21
+
22
+ export type AdbIncomingSocketHandler = (socket: AdbSocket) => ValueOrPromise<boolean>;
23
+
24
+ export interface Closeable {
25
+ close(): ValueOrPromise<void>;
26
+ }
27
+
28
+ /**
29
+ * The dispatcher is the "dumb" part of the connection handling logic.
30
+ *
31
+ * Except some options to change some minor behaviors,
32
+ * its only job is forwarding packets between authenticated underlying streams
33
+ * and abstracted socket objects.
34
+ *
35
+ * The `Adb` class is responsible for doing the authentication,
36
+ * negotiating the options, and has shortcuts to high-level services.
37
+ */
38
+ export class AdbPacketDispatcher implements Closeable {
39
+ // ADB socket id starts from 1
40
+ // (0 means open failed)
41
+ private readonly initializers = new AsyncOperationManager(1);
42
+ /**
43
+ * Socket local ID to the socket controller.
44
+ */
45
+ private readonly sockets = new Map<number, AdbSocketController>();
46
+
47
+ private _writer!: WritableStreamDefaultWriter<AdbPacketInit>;
48
+
49
+ public readonly options: AdbPacketDispatcherOptions;
50
+
51
+ private _closed = false;
52
+ private _disconnected = new PromiseResolver<void>();
53
+ public get disconnected() { return this._disconnected.promise; }
54
+
55
+ private _incomingSocketHandlers: Set<AdbIncomingSocketHandler> = new Set();
56
+
57
+ private _abortController = new AbortController();
58
+
59
+ public constructor(
60
+ connection: ReadableWritablePair<AdbPacketData, AdbPacketInit>,
61
+ options: AdbPacketDispatcherOptions
62
+ ) {
63
+ this.options = options;
64
+
65
+ connection.readable
66
+ .pipeTo(new WritableStream({
67
+ write: async (packet) => {
68
+ switch (packet.command) {
69
+ case AdbCommand.OK:
70
+ this.handleOk(packet);
71
+ break;
72
+ case AdbCommand.Close:
73
+ await this.handleClose(packet);
74
+ break;
75
+ case AdbCommand.Write:
76
+ if (this.sockets.has(packet.arg1)) {
77
+ await this.sockets.get(packet.arg1)!.enqueue(packet.payload);
78
+ await this.sendPacket(AdbCommand.OK, packet.arg1, packet.arg0);
79
+ break;
80
+ }
81
+ throw new Error(`Unknown local socket id: ${packet.arg1}`);
82
+ case AdbCommand.Open:
83
+ await this.handleOpen(packet);
84
+ break;
85
+ default:
86
+ // Junk data may only appear in the authentication phase,
87
+ // since the dispatcher only works after authentication,
88
+ // all packets should have a valid command.
89
+ // (although it's possible that Adb added new commands in the future)
90
+ throw new Error(`Unknown command: ${packet.command.toString(16)}`);
91
+ }
92
+ },
93
+ }), {
94
+ // There are multiple reasons for the pipe to stop,
95
+ // (device disconnection, protocol error, or user abortion)
96
+ // if the underlying streams are still open,
97
+ // it's still possible to create another ADB connection.
98
+ // So don't close `readable` here.
99
+ preventCancel: true,
100
+ signal: this._abortController.signal,
101
+ })
102
+ .then(() => {
103
+ this.dispose();
104
+ }, (e) => {
105
+ // https://github.com/MattiasBuelens/web-streams-polyfill/issues/115
106
+ // `e` is always `AbortError` (instead of what I give in `abortController.abort()`)
107
+ // so we can't check if `e` is a real error.
108
+ if (!this._closed) {
109
+ this._disconnected.reject(e);
110
+ }
111
+ this.dispose();
112
+ });
113
+
114
+ this._writer = connection.writable.getWriter();
115
+ }
116
+
117
+ private handleOk(packet: AdbPacketData) {
118
+ if (this.initializers.resolve(packet.arg1, packet.arg0)) {
119
+ // Device successfully created the socket
120
+ return;
121
+ }
122
+
123
+ const socket = this.sockets.get(packet.arg1);
124
+ if (socket) {
125
+ // Device has received last `WRTE` to the socket
126
+ socket.ack();
127
+ return;
128
+ }
129
+
130
+ // Maybe the device is responding to a packet of last connection
131
+ // Tell the device to close the socket
132
+ this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
133
+ }
134
+
135
+ private async handleClose(packet: AdbPacketData) {
136
+ // If the socket is still pending
137
+ if (packet.arg0 === 0 &&
138
+ this.initializers.reject(packet.arg1, new Error('Socket open failed'))) {
139
+ // Device failed to create the socket
140
+ // (unknown service string, failed to execute command, etc.)
141
+ // it doesn't break the connection,
142
+ // so only reject the socket creation promise,
143
+ // don't throw an error here.
144
+ return;
145
+ }
146
+
147
+ // From https://android.googlesource.com/platform/packages/modules/adb/+/65d18e2c1cc48b585811954892311b28a4c3d188/adb.cpp#459
148
+ /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
149
+ * a failed OPEN only. However, due to a bug in previous ADB
150
+ * versions, CLOSE(0, remote-id, "") was also used for normal
151
+ * CLOSE() operations.
152
+ */
153
+
154
+ // Ignore `arg0` and search for the socket
155
+ const socket = this.sockets.get(packet.arg1);
156
+ if (socket) {
157
+ // The device want to close the socket
158
+ if (!socket.closed) {
159
+ await this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
160
+ }
161
+ socket.dispose();
162
+ this.sockets.delete(packet.arg1);
163
+ return;
164
+ }
165
+
166
+ // TODO: adb: is double closing an socket a catastrophic error?
167
+ // If the client sends two `CLSE` packets for one socket,
168
+ // the device may also respond with two `CLSE` packets.
169
+ }
170
+
171
+ public addIncomingSocketHandler(handler: AdbIncomingSocketHandler): RemoveEventListener {
172
+ this._incomingSocketHandlers.add(handler);
173
+ const remove = () => {
174
+ this._incomingSocketHandlers.delete(handler);
175
+ };
176
+ remove.dispose = remove;
177
+ return remove;
178
+ }
179
+
180
+ private async handleOpen(packet: AdbPacketData) {
181
+ // AsyncOperationManager doesn't support get and skip an ID
182
+ // Use `add` + `resolve` to simulate this behavior
183
+ const [localId] = this.initializers.add<number>();
184
+ this.initializers.resolve(localId, undefined);
185
+
186
+ const remoteId = packet.arg0;
187
+ const serviceString = decodeUtf8(packet.payload);
188
+
189
+ const controller = new AdbSocketController({
190
+ dispatcher: this,
191
+ localId,
192
+ remoteId,
193
+ localCreated: false,
194
+ serviceString,
195
+ });
196
+
197
+ for (const handler of this._incomingSocketHandlers) {
198
+ if (await handler(controller.socket)) {
199
+ this.sockets.set(localId, controller);
200
+ await this.sendPacket(AdbCommand.OK, localId, remoteId);
201
+ return;
202
+ }
203
+ }
204
+
205
+ await this.sendPacket(AdbCommand.Close, 0, remoteId);
206
+ }
207
+
208
+ public async createSocket(serviceString: string): Promise<AdbSocket> {
209
+ if (this.options.appendNullToServiceString) {
210
+ serviceString += '\0';
211
+ }
212
+
213
+ const [localId, initializer] = this.initializers.add<number>();
214
+ await this.sendPacket(
215
+ AdbCommand.Open,
216
+ localId,
217
+ 0,
218
+ serviceString
219
+ );
220
+
221
+ // Fulfilled by `handleOk`
222
+ const remoteId = await initializer;
223
+ const controller = new AdbSocketController({
224
+ dispatcher: this,
225
+ localId,
226
+ remoteId,
227
+ localCreated: true,
228
+ serviceString,
229
+ });
230
+ this.sockets.set(localId, controller);
231
+
232
+ return controller.socket;
233
+ }
234
+
235
+ public sendPacket(packet: AdbPacketInit): Promise<void>;
236
+ public sendPacket(
237
+ command: AdbCommand,
238
+ arg0: number,
239
+ arg1: number,
240
+ payload?: string | Uint8Array
241
+ ): Promise<void>;
242
+ public async sendPacket(
243
+ packetOrCommand: AdbPacketInit | AdbCommand,
244
+ arg0?: number,
245
+ arg1?: number,
246
+ payload: string | Uint8Array = EMPTY_UINT8_ARRAY,
247
+ ): Promise<void> {
248
+ let init: AdbPacketData;
249
+ if (arg0 === undefined) {
250
+ init = packetOrCommand as AdbPacketInit;
251
+ } else {
252
+ if (typeof payload === 'string') {
253
+ payload = encodeUtf8(payload);
254
+ }
255
+
256
+ init = {
257
+ command: packetOrCommand as AdbCommand,
258
+ arg0: arg0 as number,
259
+ arg1: arg1 as number,
260
+ payload,
261
+ };
262
+ }
263
+
264
+ if (init.payload &&
265
+ init.payload.byteLength > this.options.maxPayloadSize) {
266
+ throw new Error('payload too large');
267
+ }
268
+
269
+ if (this.options.calculateChecksum) {
270
+ calculateChecksum(init);
271
+ } else {
272
+ (init as AdbPacketInit).checksum = 0;
273
+ }
274
+
275
+ await this._writer.ready;
276
+ await this._writer.write(init as AdbPacketInit);
277
+ }
278
+
279
+ public async close() {
280
+ // Send `CLSE` packets for all sockets
281
+ await Promise.all(
282
+ Array.from(
283
+ this.sockets.values(),
284
+ socket => socket.close(),
285
+ )
286
+ );
287
+
288
+ // Stop receiving
289
+ // It's possible that we haven't received all `CLSE` confirm packets,
290
+ // but it doesn't matter, the next connection can cope with them.
291
+ this._closed = true;
292
+ this._abortController.abort();
293
+ this._writer.releaseLock();
294
+
295
+ // `pipe().then()` will call `dispose`
296
+ }
297
+
298
+ private dispose() {
299
+ for (const socket of this.sockets.values()) {
300
+ socket.dispose();
301
+ }
302
+
303
+ this._disconnected.resolve();
304
+ }
305
+ }
@@ -1,2 +1,2 @@
1
- export * from './socket.js';
2
- export * from './dispatcher.js';
1
+ export * from './socket.js';
2
+ export * from './dispatcher.js';