diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-10-30 08:53:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-30 15:53:08 +0000 |
commit | 09204107d85351dae07a45f6a9684b5b6e573652 (patch) | |
tree | 5a04f3a877e677e382e34684784ddf832838bd77 /ext/node/polyfills/tty.js | |
parent | 1acef755ca8a0a0433a98e4a66433c63ee0a3b09 (diff) |
fix: implement node:tty (#20892)
Fixes #21012
Closes https://github.com/denoland/deno/issues/20855
Fixes https://github.com/denoland/deno/issues/20890
Fixes https://github.com/denoland/deno/issues/20611
Fixes https://github.com/denoland/deno/issues/20336
Fixes `create-svelte` from https://github.com/denoland/deno/issues/17248
Fixes more reports here:
- https://github.com/denoland/deno/issues/6529#issuecomment-1432690559
- https://github.com/denoland/deno/issues/6529#issuecomment-1522059006
- https://github.com/denoland/deno/issues/6529#issuecomment-1695803570
Diffstat (limited to 'ext/node/polyfills/tty.js')
-rw-r--r-- | ext/node/polyfills/tty.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/ext/node/polyfills/tty.js b/ext/node/polyfills/tty.js new file mode 100644 index 000000000..54f8f6eae --- /dev/null +++ b/ext/node/polyfills/tty.js @@ -0,0 +1,83 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +import { ERR_INVALID_FD } from "ext:deno_node/internal/errors.ts"; +import { LibuvStreamWrap } from "ext:deno_node/internal_binding/stream_wrap.ts"; +import { providerType } from "ext:deno_node/internal_binding/async_wrap.ts"; +import { Duplex } from "node:stream"; +const { Error } = globalThis.__bootstrap.primordials; + +// Returns true when the given numeric fd is associated with a TTY and false otherwise. +function isatty(fd) { + if (typeof fd !== "number") { + return false; + } + try { + return Deno.isatty(fd); + } catch (_) { + return false; + } +} + +class TTY extends LibuvStreamWrap { + constructor(handle) { + super(providerType.TTYWRAP, handle); + } +} + +export class ReadStream extends Duplex { + constructor(fd, options) { + if (fd >> 0 !== fd || fd < 0) { + throw new ERR_INVALID_FD(fd); + } + + // We only support `stdin`. + if (fd != 0) throw new Error("Only fd 0 is supported."); + + const tty = new TTY(Deno.stdin); + super({ + readableHighWaterMark: 0, + handle: tty, + manualStart: true, + ...options, + }); + + this.isRaw = false; + this.isTTY = true; + } + + setRawMode(flag) { + flag = !!flag; + this._handle.setRaw(flag); + + this.isRaw = flag; + return this; + } +} + +export class WriteStream extends Duplex { + constructor(fd) { + if (fd >> 0 !== fd || fd < 0) { + throw new ERR_INVALID_FD(fd); + } + + // We only support `stdin`, `stdout` and `stderr`. + if (fd > 2) throw new Error("Only fd 0, 1 and 2 are supported."); + + const tty = new TTY( + fd === 0 ? Deno.stdin : fd === 1 ? Deno.stdout : Deno.stderr, + ); + + super({ + readableHighWaterMark: 0, + handle: tty, + manualStart: true, + }); + + const { columns, rows } = Deno.consoleSize(); + this.columns = columns; + this.rows = rows; + } +} + +export { isatty }; +export default { isatty, WriteStream, ReadStream }; |