fix: make direct sockets backend work again

This commit is contained in:
Simon Chan 2022-03-31 13:11:12 +08:00
parent 4f4096444e
commit 4c4580e2d7
No known key found for this signature in database
GPG key ID: 8F75717685A974FB
7 changed files with 348 additions and 299 deletions

View file

@ -1,4 +1,4 @@
import type { AdbBackend, ReadableStream, WritableStream } from '@yume-chan/adb';
import { AdbBackend, ReadableStream, WrapReadableStream, WrapWritableStream, WritableStream } from '@yume-chan/adb';
declare global {
interface TCPSocket {
@ -50,10 +50,29 @@ export default class AdbDirectSocketsBackend implements AdbBackend {
}
public async connect() {
return await navigator.openTCPSocket({
const { readable, writable } = await navigator.openTCPSocket({
remoteAddress: this.host,
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>({
async start() {
return {
readable,
state: undefined,
};
}
}),
writable: new WrapWritableStream({
async start() {
return {
writable,
state: undefined,
};
}
}),
};
}
}