// import Queue from "../protocols/core/Queue.js"; // const empty = Buffer.alloc(0); // class BufferList { // get size() { // return this.length; // } // private list =[]; // private length = 0; // add(buffer: Buffer) { // this.list.push(buffer); // this.length += buffer.length; // return this; // } // asBuffer() { // return Buffer.concat(this.list); // } // toString(encoding: BufferEncoding) { // return this.asBuffer().toString(encoding); // } // clear() { // this.length = 0; // this.list.length = 0; // } // } // export class BufferStream { // store: Queue = new Queue(); // start: number = 0; // unread: Buffer = empty; // get isDataAvailable() { // return !!this.store.peek; // } // onData = (data) => this.store.enqueue(data); // enqueue(buffer: Buffer) { // this.store.enqueue(buffer); // return this; // } // error(e: Error) { // this.store.error(e); // } // async read(size: number, signal?: AbortSignal) { // if (size <= this.unread.length) { // // read this... // const slice = this.unread.subarray(0, size); // this.unread = size === this.unread.length // ? empty // : this.unread.subarray(size); // return slice; // } // const pending = new BufferList(); // pending.add(this.unread); // this.unread = empty; // size -= pending.size; // for(;;) { // if (signal?.aborted) { // throw new Error("aborted"); // } // const peek = await this.store.waitForPeek(signal); // const bufLen = this.start + size; // if (bufLen <= peek.length) { // pending.add(peek.subarray(this.start, bufLen)); // const buffer = pending.asBuffer(); // this.start = bufLen; // size -= bufLen; // this.unread = empty; // pending.clear(); // if (bufLen === peek.length) { // this.store.dequeue(); // this.start = 0; // } // return buffer; // } // const remaining = this.start ? peek.subarray(this.start) : peek; // pending.add(remaining); // size -= remaining.length; // this.store.dequeue(); // this.start = 0; // } // } // async readTill(sep : string | number | Buffer = 10, encoding: BufferEncoding = "utf-8", trimR = false, signal?: AbortSignal) { // for(;;) { // const peek = await this.store.waitForPeek(signal); // if (signal?.aborted) { // throw new Error("aborted"); // } // const index = peek.indexOf(sep, this.start); // if (index === -1) { // const remaining = this.start ? peek.subarray(this.start) : peek; // this.unread = Buffer.concat([ this.unread, remaining ]); // this.start = 0; // this.store.dequeue(); // continue; // } // if (index === 0) { // // this is a special case... // const last = this.unread.length - 1; // if (trimR && this.unread.at(last) === 13) { // const t = this.unread.toString(encoding, 0, last); // this.unread = empty; // this.start = 1; // return t; // } // } // const subarray = peek.subarray(this.start, trimR && index && peek.at(index-1) === 13 ? index -1 : index); // const token = this.unread.length === 0 // ? subarray // : Buffer.concat([this.unread, subarray]); // // this is last... // if (index + 1 === peek.length) { // this.start = 0; // this.store.dequeue(); // } else { // this.start = index + 1; // } // this.unread = empty; // return token.toString(encoding); // } // } // }