1 | /**
|
2 | * Copyright (c) 2017, Daniel Imms (MIT License).
|
3 | * Copyright (c) 2018, Microsoft Corporation (MIT License).
|
4 | */
|
5 |
|
6 | declare module 'node-pty' {
|
7 | /**
|
8 | * Forks a process as a pseudoterminal.
|
9 | * @param file The file to launch.
|
10 | * @param args The file's arguments as argv (string[]) or in a pre-escaped CommandLine format
|
11 | * (string). Note that the CommandLine option is only available on Windows and is expected to be
|
12 | * escaped properly.
|
13 | * @param options The options of the terminal.
|
14 | * @see CommandLineToArgvW https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx
|
15 | * @see Parsing C++ Comamnd-Line Arguments https://msdn.microsoft.com/en-us/library/17w5ykft.aspx
|
16 | * @see GetCommandLine https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156.aspx
|
17 | */
|
18 | export function spawn(file: string, args: string[] | string, options: IPtyForkOptions | IWindowsPtyForkOptions): IPty;
|
19 |
|
20 | export interface IBasePtyForkOptions {
|
21 |
|
22 | /**
|
23 | * Name of the terminal to be set in environment ($TERM variable).
|
24 | */
|
25 | name?: string;
|
26 |
|
27 | /**
|
28 | * Number of intial cols of the pty.
|
29 | */
|
30 | cols?: number;
|
31 |
|
32 | /**
|
33 | * Number of initial rows of the pty.
|
34 | */
|
35 | rows?: number;
|
36 |
|
37 | /**
|
38 | * Working directory to be set for the child program.
|
39 | */
|
40 | cwd?: string;
|
41 |
|
42 | /**
|
43 | * Environment to be set for the child program.
|
44 | */
|
45 | env?: { [key: string]: string | undefined };
|
46 |
|
47 | /**
|
48 | * String encoding of the underlying pty.
|
49 | * If set, incoming data will be decoded to strings and outgoing strings to bytes applying this encoding.
|
50 | * If unset, incoming data will be delivered as raw bytes (Buffer type).
|
51 | * By default 'utf8' is assumed, to unset it explicitly set it to `null`.
|
52 | */
|
53 | encoding?: string | null;
|
54 |
|
55 | /**
|
56 | * (EXPERIMENTAL)
|
57 | * Whether to enable flow control handling (false by default). If enabled a message of `flowControlPause`
|
58 | * will pause the socket and thus blocking the child program execution due to buffer back pressure.
|
59 | * A message of `flowControlResume` will resume the socket into flow mode.
|
60 | * For performance reasons only a single message as a whole will match (no message part matching).
|
61 | * If flow control is enabled the `flowControlPause` and `flowControlResume` messages are not forwarded to
|
62 | * the underlying pseudoterminal.
|
63 | */
|
64 | handleFlowControl?: boolean;
|
65 |
|
66 | /**
|
67 | * (EXPERIMENTAL)
|
68 | * The string that should pause the pty when `handleFlowControl` is true. Default is XOFF ('\x13').
|
69 | */
|
70 | flowControlPause?: string;
|
71 |
|
72 | /**
|
73 | * (EXPERIMENTAL)
|
74 | * The string that should resume the pty when `handleFlowControl` is true. Default is XON ('\x11').
|
75 | */
|
76 | flowControlResume?: string;
|
77 | }
|
78 |
|
79 | export interface IPtyForkOptions extends IBasePtyForkOptions {
|
80 | /**
|
81 | * Security warning: use this option with great caution,
|
82 | * as opened file descriptors with higher privileges might leak to the child program.
|
83 | */
|
84 | uid?: number;
|
85 | gid?: number;
|
86 | }
|
87 |
|
88 | export interface IWindowsPtyForkOptions extends IBasePtyForkOptions {
|
89 | /**
|
90 | * Whether to use the ConPTY system on Windows. When this is not set, ConPTY will be used when
|
91 | * the Windows build number is >= 18309 (instead of winpty). Note that ConPTY is available from
|
92 | * build 17134 but is too unstable to enable by default.
|
93 | *
|
94 | * This setting does nothing on non-Windows.
|
95 | */
|
96 | useConpty?: boolean;
|
97 |
|
98 | /**
|
99 | * Whether to use PSEUDOCONSOLE_INHERIT_CURSOR in conpty.
|
100 | * @see https://docs.microsoft.com/en-us/windows/console/createpseudoconsole
|
101 | */
|
102 | conptyInheritCursor?: boolean;
|
103 | }
|
104 |
|
105 | /**
|
106 | * An interface representing a pseudoterminal, on Windows this is emulated via the winpty library.
|
107 | */
|
108 | export interface IPty {
|
109 | /**
|
110 | * The process ID of the outer process.
|
111 | */
|
112 | readonly pid: number;
|
113 |
|
114 | /**
|
115 | * The column size in characters.
|
116 | */
|
117 | readonly cols: number;
|
118 |
|
119 | /**
|
120 | * The row size in characters.
|
121 | */
|
122 | readonly rows: number;
|
123 |
|
124 | /**
|
125 | * The title of the active process.
|
126 | */
|
127 | readonly process: string;
|
128 |
|
129 | /**
|
130 | * (EXPERIMENTAL)
|
131 | * Whether to handle flow control. Useful to disable/re-enable flow control during runtime.
|
132 | * Use this for binary data that is likely to contain the `flowControlPause` string by accident.
|
133 | */
|
134 | handleFlowControl: boolean;
|
135 |
|
136 | /**
|
137 | * Adds an event listener for when a data event fires. This happens when data is returned from
|
138 | * the pty.
|
139 | * @returns an `IDisposable` to stop listening.
|
140 | */
|
141 | readonly onData: IEvent<string>;
|
142 |
|
143 | /**
|
144 | * Adds an event listener for when an exit event fires. This happens when the pty exits.
|
145 | * @returns an `IDisposable` to stop listening.
|
146 | */
|
147 | readonly onExit: IEvent<{ exitCode: number, signal?: number }>;
|
148 |
|
149 | /**
|
150 | * Resizes the dimensions of the pty.
|
151 | * @param columns The number of columns to use.
|
152 | * @param rows The number of rows to use.
|
153 | */
|
154 | resize(columns: number, rows: number): void;
|
155 |
|
156 | /**
|
157 | * Clears the pty's internal representation of its buffer. This is a no-op
|
158 | * unless on Windows/ConPTY. This is useful if the buffer is cleared on the
|
159 | * frontend in order to synchronize state with the backend to avoid ConPTY
|
160 | * possibly reprinting the screen.
|
161 | */
|
162 | clear(): void;
|
163 |
|
164 | /**
|
165 | * Writes data to the pty.
|
166 | * @param data The data to write.
|
167 | */
|
168 | write(data: string): void;
|
169 |
|
170 | /**
|
171 | * Kills the pty.
|
172 | * @param signal The signal to use, defaults to SIGHUP. This parameter is not supported on
|
173 | * Windows.
|
174 | * @throws Will throw when signal is used on Windows.
|
175 | */
|
176 | kill(signal?: string): void;
|
177 |
|
178 | /**
|
179 | * Pauses the pty for customizable flow control.
|
180 | */
|
181 | pause(): void;
|
182 |
|
183 | /**
|
184 | * Resumes the pty for customizable flow control.
|
185 | */
|
186 | resume(): void;
|
187 | }
|
188 |
|
189 | /**
|
190 | * An object that can be disposed via a dispose function.
|
191 | */
|
192 | export interface IDisposable {
|
193 | dispose(): void;
|
194 | }
|
195 |
|
196 | /**
|
197 | * An event that can be listened to.
|
198 | * @returns an `IDisposable` to stop listening.
|
199 | */
|
200 | export interface IEvent<T> {
|
201 | (listener: (e: T) => any): IDisposable;
|
202 | }
|
203 | }
|