@yume-chan/adb 0.0.13 → 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 (116) hide show
  1. package/CHANGELOG.json +57 -0
  2. package/CHANGELOG.md +29 -1
  3. package/LICENSE +21 -21
  4. package/README.md +247 -247
  5. package/esm/adb.d.ts +12 -4
  6. package/esm/adb.d.ts.map +1 -1
  7. package/esm/adb.js +29 -19
  8. package/esm/adb.js.map +1 -1
  9. package/esm/commands/reverse.d.ts +9 -12
  10. package/esm/commands/reverse.d.ts.map +1 -1
  11. package/esm/commands/reverse.js +44 -36
  12. package/esm/commands/reverse.js.map +1 -1
  13. package/esm/commands/subprocess/protocols/none.d.ts +2 -1
  14. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  15. package/esm/commands/subprocess/protocols/none.js +10 -9
  16. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  17. package/esm/commands/sync/pull.d.ts.map +1 -1
  18. package/esm/commands/sync/pull.js +6 -3
  19. package/esm/commands/sync/pull.js.map +1 -1
  20. package/esm/commands/sync/stat.d.ts.map +1 -1
  21. package/esm/commands/sync/stat.js +2 -2
  22. package/esm/commands/sync/stat.js.map +1 -1
  23. package/esm/commands/sync/sync.js +1 -1
  24. package/esm/commands/sync/sync.js.map +1 -1
  25. package/esm/socket/dispatcher.d.ts +25 -14
  26. package/esm/socket/dispatcher.d.ts.map +1 -1
  27. package/esm/socket/dispatcher.js +94 -63
  28. package/esm/socket/dispatcher.js.map +1 -1
  29. package/esm/socket/socket.d.ts +18 -4
  30. package/esm/socket/socket.d.ts.map +1 -1
  31. package/esm/socket/socket.js +36 -21
  32. package/esm/socket/socket.js.map +1 -1
  33. package/esm/stream/buffered.d.ts +7 -3
  34. package/esm/stream/buffered.d.ts.map +1 -1
  35. package/esm/stream/buffered.js +69 -49
  36. package/esm/stream/buffered.js.map +1 -1
  37. package/esm/stream/detect.js +1 -0
  38. package/esm/stream/detect.js.map +1 -1
  39. package/esm/stream/detect.polyfill.d.ts +1 -1
  40. package/esm/stream/detect.polyfill.d.ts.map +1 -1
  41. package/esm/stream/detect.polyfill.js +0 -5
  42. package/esm/stream/detect.polyfill.js.map +1 -1
  43. package/esm/stream/transform.d.ts +39 -13
  44. package/esm/stream/transform.d.ts.map +1 -1
  45. package/esm/stream/transform.js +67 -90
  46. package/esm/stream/transform.js.map +1 -1
  47. package/esm/utils/base64.js +5 -5
  48. package/esm/utils/base64.js.map +1 -1
  49. package/package.json +10 -10
  50. package/src/adb.ts +280 -270
  51. package/src/auth.ts +173 -173
  52. package/src/backend.ts +11 -11
  53. package/src/commands/base.ts +11 -11
  54. package/src/commands/framebuffer.ts +73 -73
  55. package/src/commands/index.ts +8 -8
  56. package/src/commands/install.ts +30 -30
  57. package/src/commands/power.ts +54 -54
  58. package/src/commands/reverse.ts +140 -136
  59. package/src/commands/subprocess/index.ts +139 -139
  60. package/src/commands/subprocess/protocols/none.ts +70 -68
  61. package/src/commands/subprocess/protocols/shell.ts +190 -190
  62. package/src/commands/subprocess/protocols/types.ts +59 -59
  63. package/src/commands/subprocess/utils.ts +20 -20
  64. package/src/commands/sync/index.ts +7 -7
  65. package/src/commands/sync/list.ts +86 -86
  66. package/src/commands/sync/pull.ts +46 -43
  67. package/src/commands/sync/push.ts +41 -41
  68. package/src/commands/sync/request.ts +50 -50
  69. package/src/commands/sync/response.ts +64 -64
  70. package/src/commands/sync/stat.ts +151 -150
  71. package/src/commands/sync/sync.ts +181 -181
  72. package/src/commands/tcpip.ts +21 -21
  73. package/src/crypto.ts +296 -296
  74. package/src/features.ts +9 -9
  75. package/src/index.ts +11 -11
  76. package/src/packet.ts +80 -80
  77. package/src/socket/dispatcher.ts +305 -270
  78. package/src/socket/index.ts +2 -2
  79. package/src/socket/socket.ts +155 -136
  80. package/src/stream/buffered.ts +168 -147
  81. package/src/stream/detect.native.ts +362 -362
  82. package/src/stream/detect.polyfill.ts +22 -28
  83. package/src/stream/detect.ts +7 -5
  84. package/src/stream/index.ts +3 -3
  85. package/src/stream/transform.ts +474 -483
  86. package/src/utils/auto-reset-event.ts +41 -41
  87. package/src/utils/base64.ts +306 -306
  88. package/src/utils/index.ts +3 -3
  89. package/tsconfig.build.json +3 -3
  90. package/tsconfig.build.tsbuildinfo +1 -1
  91. package/tsconfig.test.json +9 -10
  92. package/web-streams-polyfill-4.0.0-beta.2.tgz +0 -0
  93. package/esm/commands/subprocess/legacy.d.ts +0 -35
  94. package/esm/commands/subprocess/legacy.d.ts.map +0 -1
  95. package/esm/commands/subprocess/legacy.js +0 -53
  96. package/esm/commands/subprocess/legacy.js.map +0 -1
  97. package/esm/commands/subprocess/protocol.d.ts +0 -39
  98. package/esm/commands/subprocess/protocol.d.ts.map +0 -1
  99. package/esm/commands/subprocess/protocol.js +0 -157
  100. package/esm/commands/subprocess/protocol.js.map +0 -1
  101. package/esm/commands/subprocess/types.d.ts +0 -50
  102. package/esm/commands/subprocess/types.d.ts.map +0 -1
  103. package/esm/commands/subprocess/types.js +0 -2
  104. package/esm/commands/subprocess/types.js.map +0 -1
  105. package/esm/socket/controller.d.ts +0 -37
  106. package/esm/socket/controller.d.ts.map +0 -1
  107. package/esm/socket/controller.js +0 -66
  108. package/esm/socket/controller.js.map +0 -1
  109. package/esm/stream/detect.bak.d.ts +0 -240
  110. package/esm/stream/detect.bak.d.ts.map +0 -1
  111. package/esm/stream/detect.bak.js +0 -60
  112. package/esm/stream/detect.bak.js.map +0 -1
  113. package/esm/utils/encoding.d.ts +0 -3
  114. package/esm/utils/encoding.d.ts.map +0 -1
  115. package/esm/utils/encoding.js +0 -11
  116. package/esm/utils/encoding.js.map +0 -1
@@ -1,270 +1,305 @@
1
- import { AsyncOperationManager, PromiseResolver } from '@yume-chan/async';
2
- import { AutoDisposable, EventEmitter } from '@yume-chan/event';
3
- import { AdbCommand, calculateChecksum, type AdbPacketData, type AdbPacketInit } from '../packet.js';
4
- import { AbortController, WritableStream, WritableStreamDefaultWriter, type ReadableWritablePair } from '../stream/index.js';
5
- import { decodeUtf8, encodeUtf8 } from '../utils/index.js';
6
- import { AdbSocket, AdbSocketController } from './socket.js';
7
-
8
- export interface AdbIncomingSocketEventArgs {
9
- handled: boolean;
10
-
11
- packet: AdbPacketData;
12
-
13
- serviceString: string;
14
-
15
- socket: AdbSocket;
16
- }
17
-
18
- const EmptyUint8Array = new Uint8Array(0);
19
-
20
- export interface AdbPacketDispatcherOptions {
21
- calculateChecksum: boolean;
22
- /**
23
- * Before Android 9.0, ADB uses `char*` to parse service string,
24
- * thus requires a null character to terminate.
25
- *
26
- * Usually it should have the same value as `calculateChecksum`.
27
- */
28
- appendNullToServiceString: boolean;
29
- maxPayloadSize: number;
30
- }
31
-
32
- export class AdbPacketDispatcher extends AutoDisposable {
33
- // ADB socket id starts from 1
34
- // (0 means open failed)
35
- private readonly initializers = new AsyncOperationManager(1);
36
- private readonly sockets = new Map<number, AdbSocketController>();
37
-
38
- private _writer!: WritableStreamDefaultWriter<AdbPacketInit>;
39
-
40
- public readonly options: AdbPacketDispatcherOptions;
41
-
42
- private _disconnected = new PromiseResolver<void>();
43
- public get disconnected() { return this._disconnected.promise; }
44
-
45
- private readonly incomingSocketEvent = this.addDisposable(new EventEmitter<AdbIncomingSocketEventArgs>());
46
- public get onIncomingSocket() { return this.incomingSocketEvent.event; }
47
-
48
- private _abortController = new AbortController();
49
-
50
- public constructor(
51
- connection: ReadableWritablePair<AdbPacketData, AdbPacketInit>,
52
- options: AdbPacketDispatcherOptions
53
- ) {
54
- super();
55
-
56
- this.options = options;
57
-
58
- connection.readable
59
- .pipeTo(new WritableStream({
60
- write: async (packet) => {
61
- try {
62
- switch (packet.command) {
63
- case AdbCommand.OK:
64
- this.handleOk(packet);
65
- return;
66
- case AdbCommand.Close:
67
- await this.handleClose(packet);
68
- return;
69
- case AdbCommand.Write:
70
- if (this.sockets.has(packet.arg1)) {
71
- await this.sockets.get(packet.arg1)!.enqueue(packet.payload);
72
- await this.sendPacket(AdbCommand.OK, packet.arg1, packet.arg0);
73
- }
74
-
75
- // Maybe the device is responding to a packet of last connection
76
- // Just ignore it
77
- return;
78
- case AdbCommand.Open:
79
- await this.handleOpen(packet);
80
- return;
81
- }
82
- } catch (e) {
83
- // Throw error here will stop the pipe
84
- // But won't close `readable` because of `preventCancel: true`
85
- throw e;
86
- }
87
- },
88
- }), {
89
- preventCancel: false,
90
- signal: this._abortController.signal,
91
- })
92
- .then(() => {
93
- this.dispose();
94
- }, (e) => {
95
- this._disconnected.reject(e);
96
- this.dispose();
97
- });
98
-
99
- this._writer = connection.writable.getWriter();
100
- }
101
-
102
- private handleOk(packet: AdbPacketData) {
103
- if (this.initializers.resolve(packet.arg1, packet.arg0)) {
104
- // Device successfully created the socket
105
- return;
106
- }
107
-
108
- const socket = this.sockets.get(packet.arg1);
109
- if (socket) {
110
- // Device has received last `WRTE` to the socket
111
- socket.ack();
112
- return;
113
- }
114
-
115
- // Maybe the device is responding to a packet of last connection
116
- // Tell the device to close the socket
117
- this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
118
- }
119
-
120
- private async handleClose(packet: AdbPacketData) {
121
- // From https://android.googlesource.com/platform/packages/modules/adb/+/65d18e2c1cc48b585811954892311b28a4c3d188/adb.cpp#459
122
- /* According to protocol.txt, p->msg.arg0 might be 0 to indicate
123
- * a failed OPEN only. However, due to a bug in previous ADB
124
- * versions, CLOSE(0, remote-id, "") was also used for normal
125
- * CLOSE() operations.
126
- */
127
-
128
- // So don't return if `reject` didn't find a pending socket
129
- if (packet.arg0 === 0 &&
130
- this.initializers.reject(packet.arg1, new Error('Socket open failed'))) {
131
- // Device failed to create the socket
132
- return;
133
- }
134
-
135
- const socket = this.sockets.get(packet.arg1);
136
- if (socket) {
137
- // The device want to close the socket
138
- if (!socket.closed) {
139
- await this.sendPacket(AdbCommand.Close, packet.arg1, packet.arg0);
140
- }
141
- socket.dispose();
142
- this.sockets.delete(packet.arg1);
143
- return;
144
- }
145
-
146
- // Maybe the device is responding to a packet of last connection
147
- // Just ignore it
148
- }
149
-
150
- private async handleOpen(packet: AdbPacketData) {
151
- // AsyncOperationManager doesn't support get and skip an ID
152
- // Use `add` + `resolve` to simulate this behavior
153
- const [localId] = this.initializers.add<number>();
154
- this.initializers.resolve(localId, undefined);
155
-
156
- const remoteId = packet.arg0;
157
- const serviceString = decodeUtf8(packet.payload);
158
-
159
- const controller = new AdbSocketController({
160
- dispatcher: this,
161
- localId,
162
- remoteId,
163
- localCreated: false,
164
- serviceString,
165
- });
166
-
167
- const args: AdbIncomingSocketEventArgs = {
168
- handled: false,
169
- packet,
170
- serviceString,
171
- socket: controller.socket,
172
- };
173
- this.incomingSocketEvent.fire(args);
174
-
175
- if (args.handled) {
176
- this.sockets.set(localId, controller);
177
- await this.sendPacket(AdbCommand.OK, localId, remoteId);
178
- } else {
179
- await this.sendPacket(AdbCommand.Close, 0, remoteId);
180
- }
181
- }
182
-
183
- public async createSocket(serviceString: string): Promise<AdbSocket> {
184
- if (this.options.appendNullToServiceString) {
185
- serviceString += '\0';
186
- }
187
-
188
- const [localId, initializer] = this.initializers.add<number>();
189
- await this.sendPacket(
190
- AdbCommand.Open,
191
- localId,
192
- 0,
193
- serviceString
194
- );
195
-
196
- // Fulfilled by `handleOk`
197
- const remoteId = await initializer;
198
- const controller = new AdbSocketController({
199
- dispatcher: this,
200
- localId,
201
- remoteId,
202
- localCreated: true,
203
- serviceString,
204
- });
205
- this.sockets.set(localId, controller);
206
-
207
- return controller.socket;
208
- }
209
-
210
- public sendPacket(packet: AdbPacketInit): Promise<void>;
211
- public sendPacket(
212
- command: AdbCommand,
213
- arg0: number,
214
- arg1: number,
215
- payload?: string | Uint8Array
216
- ): Promise<void>;
217
- public async sendPacket(
218
- packetOrCommand: AdbPacketInit | AdbCommand,
219
- arg0?: number,
220
- arg1?: number,
221
- payload: string | Uint8Array = EmptyUint8Array,
222
- ): Promise<void> {
223
- let init: AdbPacketData;
224
- if (arg0 === undefined) {
225
- init = packetOrCommand as AdbPacketInit;
226
- } else {
227
- if (typeof payload === 'string') {
228
- payload = encodeUtf8(payload);
229
- }
230
-
231
- init = {
232
- command: packetOrCommand as AdbCommand,
233
- arg0: arg0 as number,
234
- arg1: arg1 as number,
235
- payload,
236
- };
237
- }
238
-
239
- if (init.payload &&
240
- init.payload.byteLength > this.options.maxPayloadSize) {
241
- throw new Error('payload too large');
242
- }
243
-
244
- if (this.options.calculateChecksum) {
245
- calculateChecksum(init);
246
- } else {
247
- (init as AdbPacketInit).checksum = 0;
248
- }
249
-
250
- await this._writer.write(init as AdbPacketInit);
251
- }
252
-
253
- public override dispose() {
254
- for (const socket of this.sockets.values()) {
255
- socket.dispose();
256
- }
257
- this.sockets.clear();
258
-
259
- try {
260
- // Stop pipes
261
- this._abortController.abort();
262
- } catch { }
263
-
264
- this._writer.releaseLock();
265
-
266
- this._disconnected.resolve();
267
-
268
- super.dispose();
269
- }
270
- }
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';