@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.
- package/CHANGELOG.json +21 -0
- package/CHANGELOG.md +11 -1
- package/LICENSE +21 -21
- package/README.md +247 -247
- package/esm/adb.d.ts +7 -0
- package/esm/adb.d.ts.map +1 -1
- package/esm/adb.js +7 -0
- package/esm/adb.js.map +1 -1
- package/esm/commands/subprocess/protocols/none.d.ts +1 -0
- package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/none.js +6 -5
- package/esm/commands/subprocess/protocols/none.js.map +1 -1
- package/esm/commands/sync/stat.d.ts.map +1 -1
- package/esm/commands/sync/stat.js.map +1 -1
- package/esm/socket/dispatcher.d.ts +4 -1
- package/esm/socket/dispatcher.d.ts.map +1 -1
- package/esm/socket/dispatcher.js +11 -8
- package/esm/socket/dispatcher.js.map +1 -1
- package/esm/socket/socket.d.ts +6 -1
- package/esm/socket/socket.d.ts.map +1 -1
- package/esm/socket/socket.js +17 -7
- package/esm/socket/socket.js.map +1 -1
- package/esm/stream/buffered.d.ts +6 -2
- package/esm/stream/buffered.d.ts.map +1 -1
- package/esm/stream/buffered.js +67 -47
- 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.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 +20 -0
- package/esm/stream/transform.d.ts.map +1 -1
- package/esm/stream/transform.js +7 -1
- package/esm/stream/transform.js.map +1 -1
- package/package.json +10 -10
- package/src/adb.ts +280 -273
- 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 -140
- 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 -46
- 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 -304
- package/src/socket/index.ts +2 -2
- package/src/socket/socket.ts +155 -144
- 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 -447
- 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
|
@@ -1,68 +1,70 @@
|
|
|
1
|
-
import type { Adb } from "../../../adb.js";
|
|
2
|
-
import type { AdbSocket } from "../../../socket/index.js";
|
|
3
|
-
import { DuplexStreamFactory, ReadableStream } from "../../../stream/index.js";
|
|
4
|
-
import type { AdbSubprocessProtocol } from "./types.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The legacy shell
|
|
8
|
-
*
|
|
9
|
-
* Features:
|
|
10
|
-
* * `stderr`: No
|
|
11
|
-
* * `exit` exit code: No
|
|
12
|
-
* * `resize`: No
|
|
13
|
-
*/
|
|
14
|
-
export class AdbSubprocessNoneProtocol implements AdbSubprocessProtocol {
|
|
15
|
-
public static isSupported() { return true; }
|
|
16
|
-
|
|
17
|
-
public static async pty(adb: Adb, command: string) {
|
|
18
|
-
return new AdbSubprocessNoneProtocol(await adb.createSocket(`shell:${command}`));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public static async raw(adb: Adb, command: string) {
|
|
22
|
-
// `shell,raw:${command}` also triggers raw mode,
|
|
23
|
-
// But is not supported before Android 7.
|
|
24
|
-
return new AdbSubprocessNoneProtocol(await adb.createSocket(`exec:${command}`));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
private readonly socket: AdbSocket;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
public
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
1
|
+
import type { Adb } from "../../../adb.js";
|
|
2
|
+
import type { AdbSocket } from "../../../socket/index.js";
|
|
3
|
+
import { DuplexStreamFactory, ReadableStream } from "../../../stream/index.js";
|
|
4
|
+
import type { AdbSubprocessProtocol } from "./types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The legacy shell
|
|
8
|
+
*
|
|
9
|
+
* Features:
|
|
10
|
+
* * `stderr`: No
|
|
11
|
+
* * `exit` exit code: No
|
|
12
|
+
* * `resize`: No
|
|
13
|
+
*/
|
|
14
|
+
export class AdbSubprocessNoneProtocol implements AdbSubprocessProtocol {
|
|
15
|
+
public static isSupported() { return true; }
|
|
16
|
+
|
|
17
|
+
public static async pty(adb: Adb, command: string) {
|
|
18
|
+
return new AdbSubprocessNoneProtocol(await adb.createSocket(`shell:${command}`));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public static async raw(adb: Adb, command: string) {
|
|
22
|
+
// `shell,raw:${command}` also triggers raw mode,
|
|
23
|
+
// But is not supported before Android 7.
|
|
24
|
+
return new AdbSubprocessNoneProtocol(await adb.createSocket(`exec:${command}`));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private readonly socket: AdbSocket;
|
|
28
|
+
|
|
29
|
+
private readonly duplex: DuplexStreamFactory<Uint8Array, Uint8Array>;
|
|
30
|
+
|
|
31
|
+
// Legacy shell forwards all data to stdin.
|
|
32
|
+
public get stdin() { return this.socket.writable; }
|
|
33
|
+
|
|
34
|
+
private _stdout: ReadableStream<Uint8Array>;
|
|
35
|
+
/**
|
|
36
|
+
* Legacy shell mixes stdout and stderr.
|
|
37
|
+
*/
|
|
38
|
+
public get stdout() { return this._stdout; }
|
|
39
|
+
|
|
40
|
+
private _stderr: ReadableStream<Uint8Array>;
|
|
41
|
+
/**
|
|
42
|
+
* `stderr` will always be empty.
|
|
43
|
+
*/
|
|
44
|
+
public get stderr() { return this._stderr; }
|
|
45
|
+
|
|
46
|
+
private _exit: Promise<number>;
|
|
47
|
+
public get exit() { return this._exit; }
|
|
48
|
+
|
|
49
|
+
public constructor(socket: AdbSocket) {
|
|
50
|
+
this.socket = socket;
|
|
51
|
+
|
|
52
|
+
this.duplex = new DuplexStreamFactory<Uint8Array, Uint8Array>({
|
|
53
|
+
close: async () => {
|
|
54
|
+
await this.socket.close();
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
this._stdout = this.duplex.wrapReadable(this.socket.readable);
|
|
59
|
+
this._stderr = this.duplex.wrapReadable(new ReadableStream());
|
|
60
|
+
this._exit = this.duplex.closed.then(() => 0);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public resize() {
|
|
64
|
+
// Not supported
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public kill() {
|
|
68
|
+
return this.duplex.close();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -1,190 +1,190 @@
|
|
|
1
|
-
import { PromiseResolver } from "@yume-chan/async";
|
|
2
|
-
import Struct, { placeholder, type StructValueType } from "@yume-chan/struct";
|
|
3
|
-
import type { Adb } from "../../../adb.js";
|
|
4
|
-
import { AdbFeatures } from "../../../features.js";
|
|
5
|
-
import type { AdbSocket } from "../../../socket/index.js";
|
|
6
|
-
import { pipeFrom, PushReadableStream, ReadableStream, StructDeserializeStream, StructSerializeStream, TransformStream, WritableStream, WritableStreamDefaultWriter, type PushReadableStreamController } from "../../../stream/index.js";
|
|
7
|
-
import { encodeUtf8 } from "../../../utils/index.js";
|
|
8
|
-
import type { AdbSubprocessProtocol } from "./types.js";
|
|
9
|
-
|
|
10
|
-
export enum AdbShellProtocolId {
|
|
11
|
-
Stdin,
|
|
12
|
-
Stdout,
|
|
13
|
-
Stderr,
|
|
14
|
-
Exit,
|
|
15
|
-
CloseStdin,
|
|
16
|
-
WindowSizeChange,
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// This packet format is used in both direction.
|
|
20
|
-
const AdbShellProtocolPacket =
|
|
21
|
-
new Struct({ littleEndian: true })
|
|
22
|
-
.uint8('id', placeholder<AdbShellProtocolId>())
|
|
23
|
-
.uint32('length')
|
|
24
|
-
.uint8Array('data', { lengthField: 'length' });
|
|
25
|
-
|
|
26
|
-
type AdbShellProtocolPacketInit = typeof AdbShellProtocolPacket['TInit'];
|
|
27
|
-
|
|
28
|
-
type AdbShellProtocolPacket = StructValueType<typeof AdbShellProtocolPacket>;
|
|
29
|
-
|
|
30
|
-
class StdinSerializeStream extends TransformStream<Uint8Array, AdbShellProtocolPacketInit>{
|
|
31
|
-
constructor() {
|
|
32
|
-
super({
|
|
33
|
-
transform(chunk, controller) {
|
|
34
|
-
controller.enqueue({
|
|
35
|
-
id: AdbShellProtocolId.Stdin,
|
|
36
|
-
data: chunk,
|
|
37
|
-
});
|
|
38
|
-
},
|
|
39
|
-
flush() {
|
|
40
|
-
// TODO: AdbShellSubprocessProtocol: support closing stdin
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
class StdoutDeserializeStream extends TransformStream<AdbShellProtocolPacket, Uint8Array>{
|
|
47
|
-
constructor(type: AdbShellProtocolId.Stdout | AdbShellProtocolId.Stderr) {
|
|
48
|
-
super({
|
|
49
|
-
transform(chunk, controller) {
|
|
50
|
-
if (chunk.id === type) {
|
|
51
|
-
controller.enqueue(chunk.data);
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class MultiplexStream<T>{
|
|
59
|
-
private _readable: PushReadableStream<T>;
|
|
60
|
-
private _readableController!: PushReadableStreamController<T>;
|
|
61
|
-
public get readable() { return this._readable; }
|
|
62
|
-
|
|
63
|
-
private _activeCount = 0;
|
|
64
|
-
|
|
65
|
-
constructor() {
|
|
66
|
-
this._readable = new PushReadableStream(controller => {
|
|
67
|
-
this._readableController = controller;
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public createWriteable() {
|
|
72
|
-
return new WritableStream<T>({
|
|
73
|
-
start: () => {
|
|
74
|
-
this._activeCount += 1;
|
|
75
|
-
},
|
|
76
|
-
write: async (chunk) => {
|
|
77
|
-
await this._readableController.enqueue(chunk);
|
|
78
|
-
},
|
|
79
|
-
abort: async (e) => {
|
|
80
|
-
this._activeCount -= 1;
|
|
81
|
-
if (this._activeCount === 0) {
|
|
82
|
-
this._readableController.close();
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
close: async () => {
|
|
86
|
-
this._activeCount -= 1;
|
|
87
|
-
if (this._activeCount === 0) {
|
|
88
|
-
this._readableController.close();
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Shell v2 a.k.a Shell Protocol
|
|
97
|
-
*
|
|
98
|
-
* Features:
|
|
99
|
-
* * `stderr`: Yes
|
|
100
|
-
* * `exit` exit code: Yes
|
|
101
|
-
* * `resize`: Yes
|
|
102
|
-
*/
|
|
103
|
-
export class AdbSubprocessShellProtocol implements AdbSubprocessProtocol {
|
|
104
|
-
public static isSupported(adb: Adb) {
|
|
105
|
-
return adb.features!.includes(AdbFeatures.ShellV2);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public static async pty(adb: Adb, command: string) {
|
|
109
|
-
// TODO: AdbShellSubprocessProtocol: Support setting `XTERM` environment variable
|
|
110
|
-
return new AdbSubprocessShellProtocol(await adb.createSocket(`shell,v2,pty:${command}`));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public static async raw(adb: Adb, command: string) {
|
|
114
|
-
return new AdbSubprocessShellProtocol(await adb.createSocket(`shell,v2,raw:${command}`));
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private readonly _socket: AdbSocket;
|
|
118
|
-
private _socketWriter: WritableStreamDefaultWriter<AdbShellProtocolPacketInit>;
|
|
119
|
-
|
|
120
|
-
private _stdin: WritableStream<Uint8Array>;
|
|
121
|
-
public get stdin() { return this._stdin; }
|
|
122
|
-
|
|
123
|
-
private _stdout: ReadableStream<Uint8Array>;
|
|
124
|
-
public get stdout() { return this._stdout; }
|
|
125
|
-
|
|
126
|
-
private _stderr: ReadableStream<Uint8Array>;
|
|
127
|
-
public get stderr() { return this._stderr; }
|
|
128
|
-
|
|
129
|
-
private readonly _exit = new PromiseResolver<number>();
|
|
130
|
-
public get exit() { return this._exit.promise; }
|
|
131
|
-
|
|
132
|
-
public constructor(socket: AdbSocket) {
|
|
133
|
-
this._socket = socket;
|
|
134
|
-
|
|
135
|
-
// Check this image to help you understand the stream graph
|
|
136
|
-
// cspell: disable-next-line
|
|
137
|
-
// https://www.plantuml.com/plantuml/png/bL91QiCm4Bpx5SAdv90lb1JISmiw5XzaQKf5PIkiLZIqzEyLSg8ks13gYtOykpFhiOw93N6UGjVDqK7rZsxKqNw0U_NTgVAy4empOy2mm4_olC0VEVEE47GUpnGjKdgXoD76q4GIEpyFhOwP_m28hW0NNzxNUig1_JdW0bA7muFIJDco1daJ_1SAX9bgvoPJPyIkSekhNYctvIGXrCH6tIsPL5fs-s6J5yc9BpWXhKtNdF2LgVYPGM_6GlMwfhWUsIt4lbScANrwlgVVUifPSVi__t44qStnwPvZwobdSmHHlL57p2vFuHS0
|
|
138
|
-
|
|
139
|
-
// TODO: AdbShellSubprocessProtocol: Optimize stream graph
|
|
140
|
-
|
|
141
|
-
const [stdout, stderr] = socket.readable
|
|
142
|
-
.pipeThrough(new StructDeserializeStream(AdbShellProtocolPacket))
|
|
143
|
-
.pipeThrough(new TransformStream<AdbShellProtocolPacket, AdbShellProtocolPacket>({
|
|
144
|
-
transform: (chunk, controller) => {
|
|
145
|
-
if (chunk.id === AdbShellProtocolId.Exit) {
|
|
146
|
-
this._exit.resolve(new Uint8Array(chunk.data)[0]!);
|
|
147
|
-
// We can let `StdoutDeserializeStream` to process `AdbShellProtocolId.Exit`,
|
|
148
|
-
// but since we need this `TransformStream` to capture the exit code anyway,
|
|
149
|
-
// terminating child streams here is killing two birds with one stone.
|
|
150
|
-
controller.terminate();
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
controller.enqueue(chunk);
|
|
154
|
-
}
|
|
155
|
-
}))
|
|
156
|
-
.tee();
|
|
157
|
-
this._stdout = stdout
|
|
158
|
-
.pipeThrough(new StdoutDeserializeStream(AdbShellProtocolId.Stdout));
|
|
159
|
-
this._stderr = stderr
|
|
160
|
-
.pipeThrough(new StdoutDeserializeStream(AdbShellProtocolId.Stderr));
|
|
161
|
-
|
|
162
|
-
const multiplexer = new MultiplexStream<AdbShellProtocolPacketInit>();
|
|
163
|
-
multiplexer.readable
|
|
164
|
-
.pipeThrough(new StructSerializeStream(AdbShellProtocolPacket))
|
|
165
|
-
.pipeTo(socket.writable);
|
|
166
|
-
|
|
167
|
-
this._stdin = pipeFrom(
|
|
168
|
-
multiplexer.createWriteable(),
|
|
169
|
-
new StdinSerializeStream()
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
this._socketWriter = multiplexer.createWriteable().getWriter();
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public async resize(rows: number, cols: number) {
|
|
176
|
-
await this._socketWriter.write({
|
|
177
|
-
id: AdbShellProtocolId.WindowSizeChange,
|
|
178
|
-
data: encodeUtf8(
|
|
179
|
-
// The "correct" format is `${rows}x${cols},${x_pixels}x${y_pixels}`
|
|
180
|
-
// However, according to https://linux.die.net/man/4/tty_ioctl
|
|
181
|
-
// `x_pixels` and `y_pixels` are not used, so always passing `0` is fine.
|
|
182
|
-
`${rows}x${cols},0x0\0`
|
|
183
|
-
),
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
public kill() {
|
|
188
|
-
return this._socket.close();
|
|
189
|
-
}
|
|
190
|
-
}
|
|
1
|
+
import { PromiseResolver } from "@yume-chan/async";
|
|
2
|
+
import Struct, { placeholder, type StructValueType } from "@yume-chan/struct";
|
|
3
|
+
import type { Adb } from "../../../adb.js";
|
|
4
|
+
import { AdbFeatures } from "../../../features.js";
|
|
5
|
+
import type { AdbSocket } from "../../../socket/index.js";
|
|
6
|
+
import { pipeFrom, PushReadableStream, ReadableStream, StructDeserializeStream, StructSerializeStream, TransformStream, WritableStream, WritableStreamDefaultWriter, type PushReadableStreamController } from "../../../stream/index.js";
|
|
7
|
+
import { encodeUtf8 } from "../../../utils/index.js";
|
|
8
|
+
import type { AdbSubprocessProtocol } from "./types.js";
|
|
9
|
+
|
|
10
|
+
export enum AdbShellProtocolId {
|
|
11
|
+
Stdin,
|
|
12
|
+
Stdout,
|
|
13
|
+
Stderr,
|
|
14
|
+
Exit,
|
|
15
|
+
CloseStdin,
|
|
16
|
+
WindowSizeChange,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// This packet format is used in both direction.
|
|
20
|
+
const AdbShellProtocolPacket =
|
|
21
|
+
new Struct({ littleEndian: true })
|
|
22
|
+
.uint8('id', placeholder<AdbShellProtocolId>())
|
|
23
|
+
.uint32('length')
|
|
24
|
+
.uint8Array('data', { lengthField: 'length' });
|
|
25
|
+
|
|
26
|
+
type AdbShellProtocolPacketInit = typeof AdbShellProtocolPacket['TInit'];
|
|
27
|
+
|
|
28
|
+
type AdbShellProtocolPacket = StructValueType<typeof AdbShellProtocolPacket>;
|
|
29
|
+
|
|
30
|
+
class StdinSerializeStream extends TransformStream<Uint8Array, AdbShellProtocolPacketInit>{
|
|
31
|
+
constructor() {
|
|
32
|
+
super({
|
|
33
|
+
transform(chunk, controller) {
|
|
34
|
+
controller.enqueue({
|
|
35
|
+
id: AdbShellProtocolId.Stdin,
|
|
36
|
+
data: chunk,
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
flush() {
|
|
40
|
+
// TODO: AdbShellSubprocessProtocol: support closing stdin
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
class StdoutDeserializeStream extends TransformStream<AdbShellProtocolPacket, Uint8Array>{
|
|
47
|
+
constructor(type: AdbShellProtocolId.Stdout | AdbShellProtocolId.Stderr) {
|
|
48
|
+
super({
|
|
49
|
+
transform(chunk, controller) {
|
|
50
|
+
if (chunk.id === type) {
|
|
51
|
+
controller.enqueue(chunk.data);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class MultiplexStream<T>{
|
|
59
|
+
private _readable: PushReadableStream<T>;
|
|
60
|
+
private _readableController!: PushReadableStreamController<T>;
|
|
61
|
+
public get readable() { return this._readable; }
|
|
62
|
+
|
|
63
|
+
private _activeCount = 0;
|
|
64
|
+
|
|
65
|
+
constructor() {
|
|
66
|
+
this._readable = new PushReadableStream(controller => {
|
|
67
|
+
this._readableController = controller;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public createWriteable() {
|
|
72
|
+
return new WritableStream<T>({
|
|
73
|
+
start: () => {
|
|
74
|
+
this._activeCount += 1;
|
|
75
|
+
},
|
|
76
|
+
write: async (chunk) => {
|
|
77
|
+
await this._readableController.enqueue(chunk);
|
|
78
|
+
},
|
|
79
|
+
abort: async (e) => {
|
|
80
|
+
this._activeCount -= 1;
|
|
81
|
+
if (this._activeCount === 0) {
|
|
82
|
+
this._readableController.close();
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
close: async () => {
|
|
86
|
+
this._activeCount -= 1;
|
|
87
|
+
if (this._activeCount === 0) {
|
|
88
|
+
this._readableController.close();
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Shell v2 a.k.a Shell Protocol
|
|
97
|
+
*
|
|
98
|
+
* Features:
|
|
99
|
+
* * `stderr`: Yes
|
|
100
|
+
* * `exit` exit code: Yes
|
|
101
|
+
* * `resize`: Yes
|
|
102
|
+
*/
|
|
103
|
+
export class AdbSubprocessShellProtocol implements AdbSubprocessProtocol {
|
|
104
|
+
public static isSupported(adb: Adb) {
|
|
105
|
+
return adb.features!.includes(AdbFeatures.ShellV2);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public static async pty(adb: Adb, command: string) {
|
|
109
|
+
// TODO: AdbShellSubprocessProtocol: Support setting `XTERM` environment variable
|
|
110
|
+
return new AdbSubprocessShellProtocol(await adb.createSocket(`shell,v2,pty:${command}`));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public static async raw(adb: Adb, command: string) {
|
|
114
|
+
return new AdbSubprocessShellProtocol(await adb.createSocket(`shell,v2,raw:${command}`));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private readonly _socket: AdbSocket;
|
|
118
|
+
private _socketWriter: WritableStreamDefaultWriter<AdbShellProtocolPacketInit>;
|
|
119
|
+
|
|
120
|
+
private _stdin: WritableStream<Uint8Array>;
|
|
121
|
+
public get stdin() { return this._stdin; }
|
|
122
|
+
|
|
123
|
+
private _stdout: ReadableStream<Uint8Array>;
|
|
124
|
+
public get stdout() { return this._stdout; }
|
|
125
|
+
|
|
126
|
+
private _stderr: ReadableStream<Uint8Array>;
|
|
127
|
+
public get stderr() { return this._stderr; }
|
|
128
|
+
|
|
129
|
+
private readonly _exit = new PromiseResolver<number>();
|
|
130
|
+
public get exit() { return this._exit.promise; }
|
|
131
|
+
|
|
132
|
+
public constructor(socket: AdbSocket) {
|
|
133
|
+
this._socket = socket;
|
|
134
|
+
|
|
135
|
+
// Check this image to help you understand the stream graph
|
|
136
|
+
// cspell: disable-next-line
|
|
137
|
+
// https://www.plantuml.com/plantuml/png/bL91QiCm4Bpx5SAdv90lb1JISmiw5XzaQKf5PIkiLZIqzEyLSg8ks13gYtOykpFhiOw93N6UGjVDqK7rZsxKqNw0U_NTgVAy4empOy2mm4_olC0VEVEE47GUpnGjKdgXoD76q4GIEpyFhOwP_m28hW0NNzxNUig1_JdW0bA7muFIJDco1daJ_1SAX9bgvoPJPyIkSekhNYctvIGXrCH6tIsPL5fs-s6J5yc9BpWXhKtNdF2LgVYPGM_6GlMwfhWUsIt4lbScANrwlgVVUifPSVi__t44qStnwPvZwobdSmHHlL57p2vFuHS0
|
|
138
|
+
|
|
139
|
+
// TODO: AdbShellSubprocessProtocol: Optimize stream graph
|
|
140
|
+
|
|
141
|
+
const [stdout, stderr] = socket.readable
|
|
142
|
+
.pipeThrough(new StructDeserializeStream(AdbShellProtocolPacket))
|
|
143
|
+
.pipeThrough(new TransformStream<AdbShellProtocolPacket, AdbShellProtocolPacket>({
|
|
144
|
+
transform: (chunk, controller) => {
|
|
145
|
+
if (chunk.id === AdbShellProtocolId.Exit) {
|
|
146
|
+
this._exit.resolve(new Uint8Array(chunk.data)[0]!);
|
|
147
|
+
// We can let `StdoutDeserializeStream` to process `AdbShellProtocolId.Exit`,
|
|
148
|
+
// but since we need this `TransformStream` to capture the exit code anyway,
|
|
149
|
+
// terminating child streams here is killing two birds with one stone.
|
|
150
|
+
controller.terminate();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
controller.enqueue(chunk);
|
|
154
|
+
}
|
|
155
|
+
}))
|
|
156
|
+
.tee();
|
|
157
|
+
this._stdout = stdout
|
|
158
|
+
.pipeThrough(new StdoutDeserializeStream(AdbShellProtocolId.Stdout));
|
|
159
|
+
this._stderr = stderr
|
|
160
|
+
.pipeThrough(new StdoutDeserializeStream(AdbShellProtocolId.Stderr));
|
|
161
|
+
|
|
162
|
+
const multiplexer = new MultiplexStream<AdbShellProtocolPacketInit>();
|
|
163
|
+
multiplexer.readable
|
|
164
|
+
.pipeThrough(new StructSerializeStream(AdbShellProtocolPacket))
|
|
165
|
+
.pipeTo(socket.writable);
|
|
166
|
+
|
|
167
|
+
this._stdin = pipeFrom(
|
|
168
|
+
multiplexer.createWriteable(),
|
|
169
|
+
new StdinSerializeStream()
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
this._socketWriter = multiplexer.createWriteable().getWriter();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public async resize(rows: number, cols: number) {
|
|
176
|
+
await this._socketWriter.write({
|
|
177
|
+
id: AdbShellProtocolId.WindowSizeChange,
|
|
178
|
+
data: encodeUtf8(
|
|
179
|
+
// The "correct" format is `${rows}x${cols},${x_pixels}x${y_pixels}`
|
|
180
|
+
// However, according to https://linux.die.net/man/4/tty_ioctl
|
|
181
|
+
// `x_pixels` and `y_pixels` are not used, so always passing `0` is fine.
|
|
182
|
+
`${rows}x${cols},0x0\0`
|
|
183
|
+
),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public kill() {
|
|
188
|
+
return this._socket.close();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import type { ValueOrPromise } from "@yume-chan/struct";
|
|
2
|
-
import type { Adb } from "../../../adb.js";
|
|
3
|
-
import type { AdbSocket } from "../../../socket/index.js";
|
|
4
|
-
import type { ReadableStream, WritableStream } from "../../../stream/index.js";
|
|
5
|
-
|
|
6
|
-
export interface AdbSubprocessProtocol {
|
|
7
|
-
/**
|
|
8
|
-
* A WritableStream that writes to the `stdin` pipe.
|
|
9
|
-
*/
|
|
10
|
-
readonly stdin: WritableStream<Uint8Array>;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The `stdout` pipe of the process.
|
|
14
|
-
*/
|
|
15
|
-
readonly stdout: ReadableStream<Uint8Array>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* The `stderr` pipe of the process.
|
|
19
|
-
*
|
|
20
|
-
* Note: Some `AdbSubprocessProtocol` doesn't separate `stdout` and `stderr`,
|
|
21
|
-
* All output will be sent to `stdout`.
|
|
22
|
-
*/
|
|
23
|
-
readonly stderr: ReadableStream<Uint8Array>;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* A `Promise` that resolves to the exit code of the process.
|
|
27
|
-
*
|
|
28
|
-
* Note: Some `AdbSubprocessProtocol` doesn't support exit code,
|
|
29
|
-
* They will always resolve it with `0`.
|
|
30
|
-
*/
|
|
31
|
-
readonly exit: Promise<number>;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Resizes the current shell.
|
|
35
|
-
*
|
|
36
|
-
* Some `AdbSubprocessProtocol`s may not support resizing
|
|
37
|
-
* and will ignore calls to this method.
|
|
38
|
-
*/
|
|
39
|
-
resize(rows: number, cols: number): ValueOrPromise<void>;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Kills the current process.
|
|
43
|
-
*/
|
|
44
|
-
kill(): ValueOrPromise<void>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface AdbSubprocessProtocolConstructor {
|
|
48
|
-
/** Returns `true` if the `adb` instance supports this shell */
|
|
49
|
-
isSupported(adb: Adb): ValueOrPromise<boolean>;
|
|
50
|
-
|
|
51
|
-
/** Spawns an executable in PTY (interactive) mode. */
|
|
52
|
-
pty(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
|
|
53
|
-
|
|
54
|
-
/** Spawns an executable and pipe the output. */
|
|
55
|
-
raw(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
|
|
56
|
-
|
|
57
|
-
/** Creates a new `AdbShell` by attaching to an exist `AdbSocket` */
|
|
58
|
-
new(socket: AdbSocket): AdbSubprocessProtocol;
|
|
59
|
-
}
|
|
1
|
+
import type { ValueOrPromise } from "@yume-chan/struct";
|
|
2
|
+
import type { Adb } from "../../../adb.js";
|
|
3
|
+
import type { AdbSocket } from "../../../socket/index.js";
|
|
4
|
+
import type { ReadableStream, WritableStream } from "../../../stream/index.js";
|
|
5
|
+
|
|
6
|
+
export interface AdbSubprocessProtocol {
|
|
7
|
+
/**
|
|
8
|
+
* A WritableStream that writes to the `stdin` pipe.
|
|
9
|
+
*/
|
|
10
|
+
readonly stdin: WritableStream<Uint8Array>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The `stdout` pipe of the process.
|
|
14
|
+
*/
|
|
15
|
+
readonly stdout: ReadableStream<Uint8Array>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The `stderr` pipe of the process.
|
|
19
|
+
*
|
|
20
|
+
* Note: Some `AdbSubprocessProtocol` doesn't separate `stdout` and `stderr`,
|
|
21
|
+
* All output will be sent to `stdout`.
|
|
22
|
+
*/
|
|
23
|
+
readonly stderr: ReadableStream<Uint8Array>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A `Promise` that resolves to the exit code of the process.
|
|
27
|
+
*
|
|
28
|
+
* Note: Some `AdbSubprocessProtocol` doesn't support exit code,
|
|
29
|
+
* They will always resolve it with `0`.
|
|
30
|
+
*/
|
|
31
|
+
readonly exit: Promise<number>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resizes the current shell.
|
|
35
|
+
*
|
|
36
|
+
* Some `AdbSubprocessProtocol`s may not support resizing
|
|
37
|
+
* and will ignore calls to this method.
|
|
38
|
+
*/
|
|
39
|
+
resize(rows: number, cols: number): ValueOrPromise<void>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Kills the current process.
|
|
43
|
+
*/
|
|
44
|
+
kill(): ValueOrPromise<void>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface AdbSubprocessProtocolConstructor {
|
|
48
|
+
/** Returns `true` if the `adb` instance supports this shell */
|
|
49
|
+
isSupported(adb: Adb): ValueOrPromise<boolean>;
|
|
50
|
+
|
|
51
|
+
/** Spawns an executable in PTY (interactive) mode. */
|
|
52
|
+
pty(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
|
|
53
|
+
|
|
54
|
+
/** Spawns an executable and pipe the output. */
|
|
55
|
+
raw(adb: Adb, command: string): ValueOrPromise<AdbSubprocessProtocol>;
|
|
56
|
+
|
|
57
|
+
/** Creates a new `AdbShell` by attaching to an exist `AdbSocket` */
|
|
58
|
+
new(socket: AdbSocket): AdbSubprocessProtocol;
|
|
59
|
+
}
|