UNPKG

consortium

Version:

Remote control and session sharing CLI for AI coding agents

211 lines (206 loc) • 5.89 kB
'use strict'; var consumable = require('./consumable-jpudCE9S.cjs'); var structDeserialize = require('./struct-deserialize-BJQW83nw.cjs'); var utils = require('./utils-vmOBBBS0.cjs'); class ConcatStringStream { // PERF: rope (concat strings) is faster than `[].join('')` #result = ""; #resolver = new consumable.PromiseResolver(); #writable = new consumable.WritableStream({ write: (chunk) => { this.#result += chunk; }, close: () => { this.#resolver.resolve(this.#result); this.#readableController.enqueue(this.#result); this.#readableController.close(); }, abort: (reason) => { this.#resolver.reject(reason); this.#readableController.error(reason); } }); get writable() { return this.#writable; } #readableController; #readable = new consumable.ReadableStream({ start: (controller) => { this.#readableController = controller; } }); get readable() { return this.#readable; } constructor() { void Object.defineProperties(this.#readable, { then: { get: () => this.#resolver.promise.then.bind(this.#resolver.promise) }, catch: { get: () => this.#resolver.promise.catch.bind(this.#resolver.promise) }, finally: { get: () => this.#resolver.promise.finally.bind(this.#resolver.promise) } }); } } class ConcatBufferStream { #segments = []; #resolver = new consumable.PromiseResolver(); #writable = new consumable.WritableStream({ write: (chunk) => { this.#segments.push(chunk); }, close: () => { let result; let offset = 0; switch (this.#segments.length) { case 0: result = structDeserialize.EmptyUint8Array; break; case 1: result = this.#segments[0]; break; default: result = new Uint8Array(this.#segments.reduce((prev, item) => prev + item.length, 0)); for (const segment of this.#segments) { result.set(segment, offset); offset += segment.length; } break; } this.#resolver.resolve(result); this.#readableController.enqueue(result); this.#readableController.close(); }, abort: (reason) => { this.#resolver.reject(reason); this.#readableController.error(reason); } }); get writable() { return this.#writable; } #readableController; #readable = new consumable.ReadableStream({ start: (controller) => { this.#readableController = controller; } }); get readable() { return this.#readable; } constructor() { void Object.defineProperties(this.#readable, { then: { get: () => this.#resolver.promise.then.bind(this.#resolver.promise) }, catch: { get: () => this.#resolver.promise.catch.bind(this.#resolver.promise) }, finally: { get: () => this.#resolver.promise.finally.bind(this.#resolver.promise) } }); } } class BufferCombiner { #capacity; #buffer; #offset; #available; constructor(size) { this.#capacity = size; this.#buffer = new Uint8Array(size); this.#offset = 0; this.#available = size; } /** * Pushes data to the combiner. * @param data The input data to be split or combined. * @returns * A generator that yields buffers of specified size. * It may yield the same buffer multiple times, consume the data before calling `next`. */ *push(data) { let offset = 0; let available = data.length; if (this.#offset !== 0) { if (available >= this.#available) { this.#buffer.set(data.subarray(0, this.#available), this.#offset); offset += this.#available; available -= this.#available; yield this.#buffer; this.#offset = 0; this.#available = this.#capacity; if (available === 0) { return; } } else { this.#buffer.set(data, this.#offset); this.#offset += available; this.#available -= available; return; } } while (available >= this.#capacity) { const end = offset + this.#capacity; yield data.subarray(offset, end); offset = end; available -= this.#capacity; } if (available > 0) { this.#buffer.set(data.subarray(offset), this.#offset); this.#offset += available; this.#available -= available; } } flush() { if (this.#offset === 0) { return void 0; } const output = this.#buffer.subarray(0, this.#offset); this.#offset = 0; this.#available = this.#capacity; return output; } } class DistributionStream extends consumable.TransformStream { constructor(size, combine = false) { const combiner = combine ? new BufferCombiner(size) : void 0; super({ async transform(chunk, controller) { await utils.tryConsume(chunk, async (chunk2) => { if (combiner) { for (const buffer of combiner.push(chunk2)) { await consumable.Consumable.ReadableStream.enqueue(controller, buffer); } } else { let offset = 0; let available = chunk2.length; while (available > 0) { const end = offset + size; await consumable.Consumable.ReadableStream.enqueue(controller, chunk2.subarray(offset, end)); offset = end; available -= size; } } }); }, flush(controller) { if (combiner) { const data = combiner.flush(); if (data) { controller.enqueue(data); } } } }); } } exports.BufferCombiner = BufferCombiner; exports.ConcatBufferStream = ConcatBufferStream; exports.ConcatStringStream = ConcatStringStream; exports.DistributionStream = DistributionStream;