refactor(adb): let backends deserialize packets by themselves for better optimization

This commit is contained in:
Simon Chan 2022-04-03 12:49:38 +08:00
parent 38a76a2e0c
commit 8a521c8d93
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD
11 changed files with 61 additions and 70 deletions

View file

@ -1,4 +1,4 @@
import { AdbBackend, ReadableStream, WrapReadableStream, WrapWritableStream, WritableStream } from '@yume-chan/adb';
import { AdbBackend, AdbPacket, AdbPacketSerializeStream, pipeFrom, ReadableStream, StructDeserializeStream, WrapReadableStream, WrapWritableStream, WritableStream } from '@yume-chan/adb';
declare global {
interface TCPSocket {
@ -55,6 +55,7 @@ export default class AdbDirectSocketsBackend implements AdbBackend {
remotePort: this.port,
noDelay: true,
});
// Native streams can't `pipeTo()` or `pipeThrough()` polyfilled streams, so we need to wrap them
return {
readable: new WrapReadableStream<Uint8Array, ReadableStream<Uint8Array>, void>({
@ -64,15 +65,15 @@ export default class AdbDirectSocketsBackend implements AdbBackend {
state: undefined,
};
}
}),
writable: new WrapWritableStream({
}).pipeThrough(new StructDeserializeStream(AdbPacket)),
writable: pipeFrom(new WrapWritableStream({
async start() {
return {
writable,
state: undefined,
};
}
}),
}), new AdbPacketSerializeStream()),
};
}
}