1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/ext/node/polyfills/tty.js
Divy Srivastava f62e22a699
fix(ext/node): tty streams extends net socket (#21026)
Workaround the circular references issue by using a initializer function
to give tty stream class to `initStdin`.

Fixes https://github.com/denoland/deno/issues/21024
Fixes https://github.com/denoland/deno/issues/20611
Fixes https://github.com/denoland/deno/issues/20890
Fixes https://github.com/denoland/deno/issues/20336

`create-svelte` works now:
```
divy@mini /t/a> ~/gh/deno/target/debug/deno run -A --unstable --reload npm:create-svelte@latest sveltekit-deno

create-svelte version 5.1.1

┌  Welcome to SvelteKit!
│
◇  Which Svelte app template?
│  Skeleton project
│
◇  Add type checking with TypeScript?
│  Yes, using JavaScript with JSDoc comments
│
◇  Select additional options (use arrow keys/space bar)
│  none
│
└  Your project is ready!

✔ Type-checked JavaScript
  https://www.typescriptlang.org/tsconfig#checkJs

Install community-maintained integrations:
  https://github.com/svelte-add/svelte-add

Next steps:
  1: cd sveltekit-deno
  2: npm install
  3: git init && git add -A && git commit -m "Initial commit" (optional)
  4: npm run dev -- --open

To close the dev server, hit Ctrl-C

Stuck? Visit us at https://svelte.dev/chat
```

---------

Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
2023-10-31 17:24:43 +05:30

86 lines
2 KiB
JavaScript

// 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 { Socket } from "node:net";
import { setReadStream } from "ext:deno_node/_process/streams.mjs";
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 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;
}
}
export { isatty };
export default { isatty, WriteStream, ReadStream };