Deno 等同于 'peek' for Reader/Conn?

Deno equivalent to 'peek' for Reader/Conn?

是否有 clean/idiomatic 方法来检查是否有任何数据可供读取(例如在 Conn corresponding to a TCP connection) before calling .read(...) 中?如果有 1 个或更多字节可用,则可以安全地假设承诺read返回的不会永远停留在pending状态。

就上下文而言,我刚刚开始学习 Deno,正在制作一个简单的 telnet 类脚本。我最初试图在同一个异步函数中处理来自 stdin 的输入(将通过连接写出)和接收到的数据(将被显示)。在那种情况下,我必须小心不要 await conn.read(...) 因为这可能永远无法解决(其他主机可能不会再发送任何东西)。我知道我可以将它们分开或者使用 Promise.race 来等待来自任何一个的输入。但是,我仍然想知道是否有类似 peek function/method/utility 的东西可以提供。

BufReader has a peek method (src):

peek() returns the next n bytes without advancing the reader. The bytes stop being valid at the next read call.

When the end of the underlying stream is reached, but there are unread bytes left in the buffer, those bytes are returned. If there are no bytes left in the buffer, it returns null.

If an error is encountered before n bytes are available, peek() throws an error with the partial property set to a slice of the buffer that contains the bytes that were available before the error occurred.