mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-05 02:39:26 +02:00
feat: migrate more things to streams
This commit is contained in:
parent
b7725567a6
commit
ef57682ec3
20 changed files with 2239 additions and 2007 deletions
|
@ -1,5 +1,20 @@
|
|||
import { AdbBackend, ReadableStream, ReadableWritablePair, TransformStream, WritableStream } from '@yume-chan/adb';
|
||||
|
||||
/**
|
||||
* Transform an `ArrayBufferView` stream to an `ArrayBuffer` stream.
|
||||
*
|
||||
* The view must wrap the whole buffer (`byteOffset === 0` && `byteLength === buffer.byteLength`).
|
||||
*/
|
||||
export class ExtractViewBufferStream extends TransformStream<ArrayBufferView, ArrayBuffer>{
|
||||
constructor() {
|
||||
super({
|
||||
transform(chunk, controller) {
|
||||
controller.enqueue(chunk.buffer);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface TCPSocket {
|
||||
close(): Promise<void>;
|
||||
|
@ -32,13 +47,7 @@ declare global {
|
|||
export class AdbDirectSocketsBackendStreams implements ReadableWritablePair<ArrayBuffer, ArrayBuffer>{
|
||||
private socket: TCPSocket;
|
||||
|
||||
private _readableTransformStream = new TransformStream<Uint8Array, ArrayBuffer>({
|
||||
transform(chunk, controller) {
|
||||
// Although spec didn't say,
|
||||
// the chunk always has `byteOffset` of 0 and `byteLength` same as its buffer
|
||||
controller.enqueue(chunk.buffer);
|
||||
},
|
||||
});
|
||||
private _readableTransformStream: ExtractViewBufferStream;
|
||||
public get readable(): ReadableStream<ArrayBuffer> {
|
||||
return this._readableTransformStream.readable;
|
||||
}
|
||||
|
@ -49,6 +58,10 @@ export class AdbDirectSocketsBackendStreams implements ReadableWritablePair<Arra
|
|||
|
||||
constructor(socket: TCPSocket) {
|
||||
this.socket = socket;
|
||||
|
||||
// Although Direct Sockets spec didn't say,
|
||||
// WebTransport spec and File spec all have the `Uint8Array` wraps the while `ArrayBuffer`.
|
||||
this._readableTransformStream = new ExtractViewBufferStream();
|
||||
this.socket.readable.pipeTo(this._readableTransformStream.writable);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue