@yume-chan/adb
Version:
TypeScript implementation of Android Debug Bridge (ADB) protocol.
90 lines • 3.34 kB
JavaScript
import { PromiseResolver } from "@yume-chan/async";
import { MaybeConsumable, PushReadableStream, StructDeserializeStream, WritableStream, } from "@yume-chan/stream-extra";
import { AdbShellProtocolId, AdbShellProtocolPacket } from "./shared.js";
export class AdbShellProtocolProcessImpl {
get stdin() {
return this.
}
get stdout() {
return this.
}
get stderr() {
return this.
}
get exited() {
return this.
}
constructor(socket, signal) {
this.
let stdoutController;
let stderrController;
this.
stdoutController = controller;
});
this.
stderrController = controller;
});
const exited = new PromiseResolver();
this.
socket.readable
.pipeThrough(new StructDeserializeStream(AdbShellProtocolPacket))
.pipeTo(new WritableStream({
write: async (chunk) => {
switch (chunk.id) {
case AdbShellProtocolId.Exit:
exited.resolve(chunk.data[0]);
break;
case AdbShellProtocolId.Stdout:
await stdoutController.enqueue(chunk.data);
break;
case AdbShellProtocolId.Stderr:
await stderrController.enqueue(chunk.data);
break;
default:
// Ignore unknown messages like Google ADB does
// https://cs.android.com/android/platform/superproject/main/+/main:packages/modules/adb/daemon/shell_service.cpp;l=684;drc=61197364367c9e404c7da6900658f1b16c42d0da
break;
}
},
}))
.then(() => {
stdoutController.close();
stderrController.close();
// If `exited` has already settled, this will be a no-op
exited.reject(new Error("Socket ended without exit message"));
}, (e) => {
stdoutController.error(e);
stderrController.error(e);
// If `exited` has already settled, this will be a no-op
exited.reject(e);
});
if (signal) {
// `signal` won't affect `this.stdout` and `this.stderr`
// So remaining data can still be read
// (call `controller.error` will discard all pending data)
signal.addEventListener("abort", () => {
exited.reject(signal.reason);
this.
});
}
this.
this.
write: async (chunk) => {
await this.
id: AdbShellProtocolId.Stdin,
data: chunk,
}));
},
});
}
kill() {
return this.
}
}
//# sourceMappingURL=process.js.map