Skip to content
File

Blob: src/node/tty.ts

typescript156 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4// Copyright Joyent, Inc. and other Node contributors.
5//
6// Permission is hereby granted, free of charge, to any person obtaining a
7// copy of this software and associated documentation files (the
8// "Software"), to deal in the Software without restriction, including
9// without limitation the rights to use, copy, modify, merge, publish,
10// distribute, sublicense, and/or sell copies of the Software, and to permit
11// persons to whom the Software is furnished to do so, subject to the
12// following conditions:
13//
14// The above copyright notice and this permission notice shall be included
15// in all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23// USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25import { Socket } from 'node-internal:internal_net';
26 
27import type {
28 ReadStream as ReadStreamType,
29 WriteStream as WriteStreamType,
30} from 'node:tty';
31 
32import type { SocketConstructorOpts } from 'node:net';
33 
34// Extended types to include fd property which exists at runtime
35interface ReadStreamInstance extends ReadStreamType {
36 fd: number;
37}
38 
39interface WriteStreamInstance extends WriteStreamType {
40 fd: number;
41}
42 
43export function isatty(_fd: number): boolean {
44 return false;
45}
46 
47export function ReadStream(
48 this: ReadStreamInstance,
49 fd: number,
50 _options: SocketConstructorOpts = {}
51): ReadStreamInstance {
52 this.fd = fd;
53 this.isRaw = false;
54 this.isTTY = false;
55 return this;
56}
57 
58Object.setPrototypeOf(ReadStream.prototype, Socket.prototype);
59Object.setPrototypeOf(ReadStream, Socket);
60 
61// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
62ReadStream.prototype.setRawMode = function (
63 this: ReadStreamInstance,
64 mode: boolean
65): ReadStreamInstance {
66 this.isRaw = mode;
67 return this;
68};
69 
70export function WriteStream(
71 this: WriteStreamInstance,
72 fd: number,
73 _options: SocketConstructorOpts = {}
74): WriteStreamInstance {
75 this.fd = fd;
76 this.columns = 0;
77 this.rows = 0;
78 return this;
79}
80 
81Object.setPrototypeOf(WriteStream.prototype, Socket.prototype);
82Object.setPrototypeOf(WriteStream, Socket);
83 
84// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
85WriteStream.prototype.isTTY = true;
86 
87// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
88WriteStream.prototype.getColorDepth = function (): number {
89 return 8; // In Node.js, this means 256 colors, but we don't support colors at all.
90};
91 
92// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
93WriteStream.prototype.hasColors = function (): boolean {
94 return false;
95};
96 
97// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
98WriteStream.prototype._refreshSize = function (): void {
99 // no-op
100};
101 
102// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
103WriteStream.prototype.cursorTo = function (
104 _x: number,
105 _y?: number | (() => void),
106 _callback?: () => void
107): boolean {
108 if (typeof _y === 'function') {
109 _y();
110 } else if (typeof _callback === 'function') {
111 _callback();
112 }
113 return false;
114};
115 
116// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
117WriteStream.prototype.moveCursor = function (
118 _dx: number,
119 _dy: number,
120 _callback?: () => void
121): boolean {
122 if (typeof _callback === 'function') {
123 _callback();
124 }
125 return false;
126};
127 
128// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
129WriteStream.prototype.clearLine = function (
130 _dir: number,
131 _callback?: () => void
132): boolean {
133 if (typeof _callback === 'function') {
134 _callback();
135 }
136 return false;
137};
138 
139// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
140WriteStream.prototype.clearScreenDown = function (
141 _callback?: () => void
142): boolean {
143 if (typeof _callback === 'function') {
144 _callback();
145 }
146 return false;
147};
148 
149// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
150WriteStream.prototype.getWindowSize = function (): [number, number] {
151 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
152 return [this.columns ?? 0, this.rows ?? 0];
153};
154 
155export default { isatty, ReadStream, WriteStream };