@yume-chan/adb
Version:
TypeScript implementation of Android Debug Bridge (ADB) protocol.
110 lines (95 loc) • 3.1 kB
text/typescript
import type {
MaybeConsumable,
WritableStreamDefaultWriter,
} from "@yume-chan/stream-extra";
import {
BufferCombiner,
BufferedReadableStream,
Consumable,
} from "@yume-chan/stream-extra";
import type { AsyncExactReadable } from "@yume-chan/struct";
import type { AdbSocket } from "../../adb.js";
import { AutoResetEvent } from "../../utils/index.js";
export class AdbSyncSocketLocked implements AsyncExactReadable {
readonly
readonly
readonly
readonly
readonly
get position() {
return this.
}
constructor(
writer: WritableStreamDefaultWriter<MaybeConsumable<Uint8Array>>,
readable: BufferedReadableStream,
bufferSize: number,
lock: AutoResetEvent,
) {
this.
this.
this.
this.
}
// `#combiner` will reuse the buffer, so we need to use the Consumable pattern
return Consumable.WritableStream.write(this.
}
async flush() {
try {
await this.
const buffer = this.
if (buffer) {
await this.
}
} finally {
this.
}
}
async write(data: Uint8Array) {
try {
await this.
for (const buffer of this.
await this.
}
} finally {
this.
}
}
async readExactly(length: number) {
// The request may still be in the internal buffer.
// Call `flush` to send it before starting reading
await this.flush();
return await this.
}
release(): void {
// In theory, the writer shouldn't leave anything in the buffer,
// but to be safe, call `flush` to throw away any remaining data.
this.
this.
}
async close() {
await this.
}
}
export class AdbSyncSocket {
readonly
readonly
readonly
constructor(socket: AdbSocket, bufferSize: number) {
this.
this.
socket.writable.getWriter(),
new BufferedReadableStream(socket.readable),
bufferSize,
this.
);
}
async lock() {
await this.
return this.
}
async close() {
await this.
await this.
}
}