| 209 | const chunkSize = Size(chunkSize_) |
| 210 | |
| 211 | function loop( |
| 212 | totalBytesRead: bigint |
| 213 | ): Channel.Channel<Chunk.Chunk<Uint8Array>, unknown, Error.PlatformError, unknown, void, unknown> { |
| 214 | if (bytesToRead !== undefined && bytesToRead <= totalBytesRead) { |
| 215 | return Channel.void |
| 216 | } |
| 217 | |
| 218 | const toRead = bytesToRead !== undefined && (bytesToRead - totalBytesRead) < chunkSize |
| 219 | ? bytesToRead - totalBytesRead |
| 220 | : chunkSize |
| 221 | |
| 222 | return Channel.flatMap( |
| 223 | file.readAlloc(toRead), |
| 224 | Option.match({ |
| 225 | onNone: () => Channel.void, |
| 226 | onSome: (buf) => |
| 227 | Channel.flatMap( |
| 228 | Channel.write(Chunk.of(buf)), |
| 229 | (_) => loop(totalBytesRead + BigInt(buf.length)) |
| 230 | ) |
| 231 | }) |
| 232 | ) |
| 233 | } |
| 234 | |
| 235 | return Stream.bufferChunks( |
| 236 | Stream.fromChannel(loop(BigInt(0))), |