@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.
- package/CHANGELOG.json +57 -0
- package/CHANGELOG.md +29 -1
- package/LICENSE +21 -21
- package/README.md +247 -247
- package/esm/adb.d.ts +12 -4
- package/esm/adb.d.ts.map +1 -1
- package/esm/adb.js +29 -19
- package/esm/adb.js.map +1 -1
- package/esm/commands/reverse.d.ts +9 -12
- package/esm/commands/reverse.d.ts.map +1 -1
- package/esm/commands/reverse.js +44 -36
- package/esm/commands/reverse.js.map +1 -1
- package/esm/commands/subprocess/protocols/none.d.ts +2 -1
- package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/none.js +10 -9
- package/esm/commands/subprocess/protocols/none.js.map +1 -1
- package/esm/commands/sync/pull.d.ts.map +1 -1
- package/esm/commands/sync/pull.js +6 -3
- package/esm/commands/sync/pull.js.map +1 -1
- package/esm/commands/sync/stat.d.ts.map +1 -1
- package/esm/commands/sync/stat.js +2 -2
- package/esm/commands/sync/stat.js.map +1 -1
- package/esm/commands/sync/sync.js +1 -1
- package/esm/commands/sync/sync.js.map +1 -1
- package/esm/socket/dispatcher.d.ts +25 -14
- package/esm/socket/dispatcher.d.ts.map +1 -1
- package/esm/socket/dispatcher.js +94 -63
- package/esm/socket/dispatcher.js.map +1 -1
- package/esm/socket/socket.d.ts +18 -4
- package/esm/socket/socket.d.ts.map +1 -1
- package/esm/socket/socket.js +36 -21
- package/esm/socket/socket.js.map +1 -1
- package/esm/stream/buffered.d.ts +7 -3
- package/esm/stream/buffered.d.ts.map +1 -1
- package/esm/stream/buffered.js +69 -49
- package/esm/stream/buffered.js.map +1 -1
- package/esm/stream/detect.js +1 -0
- package/esm/stream/detect.js.map +1 -1
- package/esm/stream/detect.polyfill.d.ts +1 -1
- package/esm/stream/detect.polyfill.d.ts.map +1 -1
- package/esm/stream/detect.polyfill.js +0 -5
- package/esm/stream/detect.polyfill.js.map +1 -1
- package/esm/stream/transform.d.ts +39 -13
- package/esm/stream/transform.d.ts.map +1 -1
- package/esm/stream/transform.js +67 -90
- package/esm/stream/transform.js.map +1 -1
- package/esm/utils/base64.js +5 -5
- package/esm/utils/base64.js.map +1 -1
- package/package.json +10 -10
- package/src/adb.ts +280 -270
- package/src/auth.ts +173 -173
- package/src/backend.ts +11 -11
- package/src/commands/base.ts +11 -11
- package/src/commands/framebuffer.ts +73 -73
- package/src/commands/index.ts +8 -8
- package/src/commands/install.ts +30 -30
- package/src/commands/power.ts +54 -54
- package/src/commands/reverse.ts +140 -136
- package/src/commands/subprocess/index.ts +139 -139
- package/src/commands/subprocess/protocols/none.ts +70 -68
- package/src/commands/subprocess/protocols/shell.ts +190 -190
- package/src/commands/subprocess/protocols/types.ts +59 -59
- package/src/commands/subprocess/utils.ts +20 -20
- package/src/commands/sync/index.ts +7 -7
- package/src/commands/sync/list.ts +86 -86
- package/src/commands/sync/pull.ts +46 -43
- package/src/commands/sync/push.ts +41 -41
- package/src/commands/sync/request.ts +50 -50
- package/src/commands/sync/response.ts +64 -64
- package/src/commands/sync/stat.ts +151 -150
- package/src/commands/sync/sync.ts +181 -181
- package/src/commands/tcpip.ts +21 -21
- package/src/crypto.ts +296 -296
- package/src/features.ts +9 -9
- package/src/index.ts +11 -11
- package/src/packet.ts +80 -80
- package/src/socket/dispatcher.ts +305 -270
- package/src/socket/index.ts +2 -2
- package/src/socket/socket.ts +155 -136
- package/src/stream/buffered.ts +168 -147
- package/src/stream/detect.native.ts +362 -362
- package/src/stream/detect.polyfill.ts +22 -28
- package/src/stream/detect.ts +7 -5
- package/src/stream/index.ts +3 -3
- package/src/stream/transform.ts +474 -483
- package/src/utils/auto-reset-event.ts +41 -41
- package/src/utils/base64.ts +306 -306
- package/src/utils/index.ts +3 -3
- package/tsconfig.build.json +3 -3
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.test.json +9 -10
- package/web-streams-polyfill-4.0.0-beta.2.tgz +0 -0
- package/esm/commands/subprocess/legacy.d.ts +0 -35
- package/esm/commands/subprocess/legacy.d.ts.map +0 -1
- package/esm/commands/subprocess/legacy.js +0 -53
- package/esm/commands/subprocess/legacy.js.map +0 -1
- package/esm/commands/subprocess/protocol.d.ts +0 -39
- package/esm/commands/subprocess/protocol.d.ts.map +0 -1
- package/esm/commands/subprocess/protocol.js +0 -157
- package/esm/commands/subprocess/protocol.js.map +0 -1
- package/esm/commands/subprocess/types.d.ts +0 -50
- package/esm/commands/subprocess/types.d.ts.map +0 -1
- package/esm/commands/subprocess/types.js +0 -2
- package/esm/commands/subprocess/types.js.map +0 -1
- package/esm/socket/controller.d.ts +0 -37
- package/esm/socket/controller.d.ts.map +0 -1
- package/esm/socket/controller.js +0 -66
- package/esm/socket/controller.js.map +0 -1
- package/esm/stream/detect.bak.d.ts +0 -240
- package/esm/stream/detect.bak.d.ts.map +0 -1
- package/esm/stream/detect.bak.js +0 -60
- package/esm/stream/detect.bak.js.map +0 -1
- package/esm/utils/encoding.d.ts +0 -3
- package/esm/utils/encoding.d.ts.map +0 -1
- package/esm/utils/encoding.js +0 -11
- package/esm/utils/encoding.js.map +0 -1
package/src/socket/dispatcher.ts
CHANGED
|
@@ -1,270 +1,305 @@
|
|
|
1
|
-
import { AsyncOperationManager, PromiseResolver } from '@yume-chan/async';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
private readonly
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
localId,
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
this.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
+
}
|
package/src/socket/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './socket.js';
|
|
2
|
-
export * from './dispatcher.js';
|
|
1
|
+
export * from './socket.js';
|
|
2
|
+
export * from './dispatcher.js';
|