| 41 | ) {} |
| 42 | |
| 43 | const readAvailable = ( |
| 44 | conn: Duplex, |
| 45 | chunkSize: number |
| 46 | ): Arr.NonEmptyReadonlyArray<Uint8Array | string> | undefined => { |
| 47 | const first = conn.read() as Uint8Array | string | null |
| 48 | if (first === null) return undefined |
| 49 | const out: [Uint8Array | string, ...Array<Uint8Array | string>] = [first] |
| 50 | let chunk: Uint8Array | string | null |
| 51 | // read() may synchronously refill the buffer; leave further reads to the next pull. |
| 52 | while (out.length < chunkSize && (chunk = conn.read() as Uint8Array | string | null) !== null) { |
| 53 | out.push(chunk) |
| 54 | } |
| 55 | return out |
| 56 | } |
| 57 | |
| 58 | const toBuffers = (input: string | Uint8Array | ReadonlyArray<string | Uint8Array>): Array<Buffer> => |
| 59 | Arr.map(Arr.ensure(input), (value) => Buffer.from(value)) |