Download src/gateway/desktop/socket-reader.test-support.ts from SaylorTwift/openclaw: direct link, hf CLI and curl.
- Browser
- Download file 790 Bytes
-
https://huggingface.co/SaylorTwift/openclaw/resolve/main/src/gateway/desktop/socket-reader.test-support.ts
- Command line
-
hf download hf://SaylorTwift/openclaw/src/gateway/desktop/socket-reader.test-support.ts
-
curl -L -o socket-reader.test-support.ts https://huggingface.co/SaylorTwift/openclaw/resolve/main/src/gateway/desktop/socket-reader.test-support.ts
790 Bytes
| import type net from "node:net"; | |
| export class SocketReader { | |
| private buffered = Buffer.alloc(0); | |
| private readonly waiters = new Set<() => void>(); | |
| constructor(socket: net.Socket) { | |
| socket.on("data", (chunk) => { | |
| this.buffered = Buffer.concat([ | |
| this.buffered, | |
| Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk), | |
| ]); | |
| for (const waiter of this.waiters) { | |
| waiter(); | |
| } | |
| this.waiters.clear(); | |
| }); | |
| } | |
| async readExactly(length: number): Promise<Buffer> { | |
| while (this.buffered.length < length) { | |
| await new Promise<void>((resolve) => { | |
| this.waiters.add(resolve); | |
| }); | |
| } | |
| const value = this.buffered.subarray(0, length); | |
| this.buffered = this.buffered.subarray(length); | |
| return value; | |
| } | |
| } | |