1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/ext/node/polyfills/tty.js
Nathan Whitaker 9df917c0be
chore: upgrade deno_core (#22725)
<!--
Before submitting a PR, please read
https://docs.deno.com/runtime/manual/references/contributing

1. Give the PR a descriptive title.

  Examples of good title:
    - fix(std/http): Fix race condition in server
    - docs(console): Update docstrings
    - feat(doc): Handle nested reexports

  Examples of bad title:
    - fix #7123
    - update docs
    - fix bugs

2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
   all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->
2024-03-06 15:08:10 -08:00

99 lines
2.3 KiB
JavaScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { core, primordials } from "ext:core/mod.js";
const {
Error,
} = primordials;
const {
isTerminal,
} = core;
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 { Socket } from "node:net";
import { setReadStream } from "ext:deno_node/_process/streams.mjs";
// 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 {
/**
* TODO: Treat `fd` as real file descriptors. Currently, `rid` 0, 1, 2
* correspond to `fd` 0, 1, 2 (stdin, stdout, stderr). This may change in
* the future.
*/
return isTerminal(fd);
} catch (_) {
return false;
}
}
class TTY extends LibuvStreamWrap {
constructor(handle) {
super(providerType.TTYWRAP, handle);
}
}
export class ReadStream extends Socket {
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;
}
}
setReadStream(ReadStream);
export class WriteStream extends Socket {
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;
this.isTTY = true;
}
}
export { isatty };
export default { isatty, WriteStream, ReadStream };