UNPKG

18.2 kBTypeScriptView Raw
1declare module 'node:repl' {
2 export * from 'repl';
3}
4
5declare module 'repl' {
6 import { Interface, Completer, AsyncCompleter } from 'node:readline';
7 import { Context } from 'node:vm';
8 import { InspectOptions } from 'node:util';
9
10 interface ReplOptions {
11 /**
12 * The input prompt to display.
13 * Default: `"> "`
14 */
15 prompt?: string;
16 /**
17 * The `Readable` stream from which REPL input will be read.
18 * Default: `process.stdin`
19 */
20 input?: NodeJS.ReadableStream;
21 /**
22 * The `Writable` stream to which REPL output will be written.
23 * Default: `process.stdout`
24 */
25 output?: NodeJS.WritableStream;
26 /**
27 * If `true`, specifies that the output should be treated as a TTY terminal, and have
28 * ANSI/VT100 escape codes written to it.
29 * Default: checking the value of the `isTTY` property on the output stream upon
30 * instantiation.
31 */
32 terminal?: boolean;
33 /**
34 * The function to be used when evaluating each given line of input.
35 * Default: an async wrapper for the JavaScript `eval()` function. An `eval` function can
36 * error with `repl.Recoverable` to indicate the input was incomplete and prompt for
37 * additional lines.
38 *
39 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_default_evaluation
40 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_custom_evaluation_functions
41 */
42 eval?: REPLEval;
43 /**
44 * Defines if the repl prints output previews or not.
45 * @default `true` Always `false` in case `terminal` is falsy.
46 */
47 preview?: boolean;
48 /**
49 * If `true`, specifies that the default `writer` function should include ANSI color
50 * styling to REPL output. If a custom `writer` function is provided then this has no
51 * effect.
52 * Default: the REPL instance's `terminal` value.
53 */
54 useColors?: boolean;
55 /**
56 * If `true`, specifies that the default evaluation function will use the JavaScript
57 * `global` as the context as opposed to creating a new separate context for the REPL
58 * instance. The node CLI REPL sets this value to `true`.
59 * Default: `false`.
60 */
61 useGlobal?: boolean;
62 /**
63 * If `true`, specifies that the default writer will not output the return value of a
64 * command if it evaluates to `undefined`.
65 * Default: `false`.
66 */
67 ignoreUndefined?: boolean;
68 /**
69 * The function to invoke to format the output of each command before writing to `output`.
70 * Default: a wrapper for `util.inspect`.
71 *
72 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_customizing_repl_output
73 */
74 writer?: REPLWriter;
75 /**
76 * An optional function used for custom Tab auto completion.
77 *
78 * @see https://nodejs.org/dist/latest-v11.x/docs/api/readline.html#readline_use_of_the_completer_function
79 */
80 completer?: Completer | AsyncCompleter;
81 /**
82 * A flag that specifies whether the default evaluator executes all JavaScript commands in
83 * strict mode or default (sloppy) mode.
84 * Accepted values are:
85 * - `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode.
86 * - `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is equivalent to
87 * prefacing every repl statement with `'use strict'`.
88 */
89 replMode?: typeof REPL_MODE_SLOPPY | typeof REPL_MODE_STRICT;
90 /**
91 * Stop evaluating the current piece of code when `SIGINT` is received, i.e. `Ctrl+C` is
92 * pressed. This cannot be used together with a custom `eval` function.
93 * Default: `false`.
94 */
95 breakEvalOnSigint?: boolean;
96 }
97
98 type REPLEval = (this: REPLServer, evalCmd: string, context: Context, file: string, cb: (err: Error | null, result: any) => void) => void;
99 type REPLWriter = (this: REPLServer, obj: any) => string;
100
101 /**
102 * This is the default "writer" value, if none is passed in the REPL options,
103 * and it can be overridden by custom print functions.
104 */
105 const writer: REPLWriter & { options: InspectOptions };
106
107 type REPLCommandAction = (this: REPLServer, text: string) => void;
108
109 interface REPLCommand {
110 /**
111 * Help text to be displayed when `.help` is entered.
112 */
113 help?: string;
114 /**
115 * The function to execute, optionally accepting a single string argument.
116 */
117 action: REPLCommandAction;
118 }
119
120 /**
121 * Provides a customizable Read-Eval-Print-Loop (REPL).
122 *
123 * Instances of `repl.REPLServer` will accept individual lines of user input, evaluate those
124 * according to a user-defined evaluation function, then output the result. Input and output
125 * may be from `stdin` and `stdout`, respectively, or may be connected to any Node.js `stream`.
126 *
127 * Instances of `repl.REPLServer` support automatic completion of inputs, simplistic Emacs-style
128 * line editing, multi-line inputs, ANSI-styled output, saving and restoring current REPL session
129 * state, error recovery, and customizable evaluation functions.
130 *
131 * Instances of `repl.REPLServer` are created using the `repl.start()` method and _should not_
132 * be created directly using the JavaScript `new` keyword.
133 *
134 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_repl
135 */
136 class REPLServer extends Interface {
137 /**
138 * The `vm.Context` provided to the `eval` function to be used for JavaScript
139 * evaluation.
140 */
141 readonly context: Context;
142 /**
143 * @deprecated since v14.3.0 - Use `input` instead.
144 */
145 readonly inputStream: NodeJS.ReadableStream;
146 /**
147 * @deprecated since v14.3.0 - Use `output` instead.
148 */
149 readonly outputStream: NodeJS.WritableStream;
150 /**
151 * The `Readable` stream from which REPL input will be read.
152 */
153 readonly input: NodeJS.ReadableStream;
154 /**
155 * The `Writable` stream to which REPL output will be written.
156 */
157 readonly output: NodeJS.WritableStream;
158 /**
159 * The commands registered via `replServer.defineCommand()`.
160 */
161 readonly commands: NodeJS.ReadOnlyDict<REPLCommand>;
162 /**
163 * A value indicating whether the REPL is currently in "editor mode".
164 *
165 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_commands_and_special_keys
166 */
167 readonly editorMode: boolean;
168 /**
169 * A value indicating whether the `_` variable has been assigned.
170 *
171 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
172 */
173 readonly underscoreAssigned: boolean;
174 /**
175 * The last evaluation result from the REPL (assigned to the `_` variable inside of the REPL).
176 *
177 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
178 */
179 readonly last: any;
180 /**
181 * A value indicating whether the `_error` variable has been assigned.
182 *
183 * @since v9.8.0
184 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
185 */
186 readonly underscoreErrAssigned: boolean;
187 /**
188 * The last error raised inside the REPL (assigned to the `_error` variable inside of the REPL).
189 *
190 * @since v9.8.0
191 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_assignment_of_the_underscore_variable
192 */
193 readonly lastError: any;
194 /**
195 * Specified in the REPL options, this is the function to be used when evaluating each
196 * given line of input. If not specified in the REPL options, this is an async wrapper
197 * for the JavaScript `eval()` function.
198 */
199 readonly eval: REPLEval;
200 /**
201 * Specified in the REPL options, this is a value indicating whether the default
202 * `writer` function should include ANSI color styling to REPL output.
203 */
204 readonly useColors: boolean;
205 /**
206 * Specified in the REPL options, this is a value indicating whether the default `eval`
207 * function will use the JavaScript `global` as the context as opposed to creating a new
208 * separate context for the REPL instance.
209 */
210 readonly useGlobal: boolean;
211 /**
212 * Specified in the REPL options, this is a value indicating whether the default `writer`
213 * function should output the result of a command if it evaluates to `undefined`.
214 */
215 readonly ignoreUndefined: boolean;
216 /**
217 * Specified in the REPL options, this is the function to invoke to format the output of
218 * each command before writing to `outputStream`. If not specified in the REPL options,
219 * this will be a wrapper for `util.inspect`.
220 */
221 readonly writer: REPLWriter;
222 /**
223 * Specified in the REPL options, this is the function to use for custom Tab auto-completion.
224 */
225 readonly completer: Completer | AsyncCompleter;
226 /**
227 * Specified in the REPL options, this is a flag that specifies whether the default `eval`
228 * function should execute all JavaScript commands in strict mode or default (sloppy) mode.
229 * Possible values are:
230 * - `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode.
231 * - `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is equivalent to
232 * prefacing every repl statement with `'use strict'`.
233 */
234 readonly replMode: typeof REPL_MODE_SLOPPY | typeof REPL_MODE_STRICT;
235
236 /**
237 * NOTE: According to the documentation:
238 *
239 * > Instances of `repl.REPLServer` are created using the `repl.start()` method and
240 * > _should not_ be created directly using the JavaScript `new` keyword.
241 *
242 * `REPLServer` cannot be subclassed due to implementation specifics in NodeJS.
243 *
244 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_class_replserver
245 */
246 private constructor();
247
248 /**
249 * Used to add new `.`-prefixed commands to the REPL instance. Such commands are invoked
250 * by typing a `.` followed by the `keyword`.
251 *
252 * @param keyword The command keyword (_without_ a leading `.` character).
253 * @param cmd The function to invoke when the command is processed.
254 *
255 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_replserver_definecommand_keyword_cmd
256 */
257 defineCommand(keyword: string, cmd: REPLCommandAction | REPLCommand): void;
258 /**
259 * Readies the REPL instance for input from the user, printing the configured `prompt` to a
260 * new line in the `output` and resuming the `input` to accept new input.
261 *
262 * When multi-line input is being entered, an ellipsis is printed rather than the 'prompt'.
263 *
264 * This method is primarily intended to be called from within the action function for
265 * commands registered using the `replServer.defineCommand()` method.
266 *
267 * @param preserveCursor When `true`, the cursor placement will not be reset to `0`.
268 */
269 displayPrompt(preserveCursor?: boolean): void;
270 /**
271 * Clears any command that has been buffered but not yet executed.
272 *
273 * This method is primarily intended to be called from within the action function for
274 * commands registered using the `replServer.defineCommand()` method.
275 *
276 * @since v9.0.0
277 */
278 clearBufferedCommand(): void;
279
280 /**
281 * Initializes a history log file for the REPL instance. When executing the
282 * Node.js binary and using the command line REPL, a history file is initialized
283 * by default. However, this is not the case when creating a REPL
284 * programmatically. Use this method to initialize a history log file when working
285 * with REPL instances programmatically.
286 * @param path The path to the history file
287 */
288 setupHistory(path: string, cb: (err: Error | null, repl: this) => void): void;
289
290 /**
291 * events.EventEmitter
292 * 1. close - inherited from `readline.Interface`
293 * 2. line - inherited from `readline.Interface`
294 * 3. pause - inherited from `readline.Interface`
295 * 4. resume - inherited from `readline.Interface`
296 * 5. SIGCONT - inherited from `readline.Interface`
297 * 6. SIGINT - inherited from `readline.Interface`
298 * 7. SIGTSTP - inherited from `readline.Interface`
299 * 8. exit
300 * 9. reset
301 */
302
303 addListener(event: string, listener: (...args: any[]) => void): this;
304 addListener(event: "close", listener: () => void): this;
305 addListener(event: "line", listener: (input: string) => void): this;
306 addListener(event: "pause", listener: () => void): this;
307 addListener(event: "resume", listener: () => void): this;
308 addListener(event: "SIGCONT", listener: () => void): this;
309 addListener(event: "SIGINT", listener: () => void): this;
310 addListener(event: "SIGTSTP", listener: () => void): this;
311 addListener(event: "exit", listener: () => void): this;
312 addListener(event: "reset", listener: (context: Context) => void): this;
313
314 emit(event: string | symbol, ...args: any[]): boolean;
315 emit(event: "close"): boolean;
316 emit(event: "line", input: string): boolean;
317 emit(event: "pause"): boolean;
318 emit(event: "resume"): boolean;
319 emit(event: "SIGCONT"): boolean;
320 emit(event: "SIGINT"): boolean;
321 emit(event: "SIGTSTP"): boolean;
322 emit(event: "exit"): boolean;
323 emit(event: "reset", context: Context): boolean;
324
325 on(event: string, listener: (...args: any[]) => void): this;
326 on(event: "close", listener: () => void): this;
327 on(event: "line", listener: (input: string) => void): this;
328 on(event: "pause", listener: () => void): this;
329 on(event: "resume", listener: () => void): this;
330 on(event: "SIGCONT", listener: () => void): this;
331 on(event: "SIGINT", listener: () => void): this;
332 on(event: "SIGTSTP", listener: () => void): this;
333 on(event: "exit", listener: () => void): this;
334 on(event: "reset", listener: (context: Context) => void): this;
335
336 once(event: string, listener: (...args: any[]) => void): this;
337 once(event: "close", listener: () => void): this;
338 once(event: "line", listener: (input: string) => void): this;
339 once(event: "pause", listener: () => void): this;
340 once(event: "resume", listener: () => void): this;
341 once(event: "SIGCONT", listener: () => void): this;
342 once(event: "SIGINT", listener: () => void): this;
343 once(event: "SIGTSTP", listener: () => void): this;
344 once(event: "exit", listener: () => void): this;
345 once(event: "reset", listener: (context: Context) => void): this;
346
347 prependListener(event: string, listener: (...args: any[]) => void): this;
348 prependListener(event: "close", listener: () => void): this;
349 prependListener(event: "line", listener: (input: string) => void): this;
350 prependListener(event: "pause", listener: () => void): this;
351 prependListener(event: "resume", listener: () => void): this;
352 prependListener(event: "SIGCONT", listener: () => void): this;
353 prependListener(event: "SIGINT", listener: () => void): this;
354 prependListener(event: "SIGTSTP", listener: () => void): this;
355 prependListener(event: "exit", listener: () => void): this;
356 prependListener(event: "reset", listener: (context: Context) => void): this;
357
358 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
359 prependOnceListener(event: "close", listener: () => void): this;
360 prependOnceListener(event: "line", listener: (input: string) => void): this;
361 prependOnceListener(event: "pause", listener: () => void): this;
362 prependOnceListener(event: "resume", listener: () => void): this;
363 prependOnceListener(event: "SIGCONT", listener: () => void): this;
364 prependOnceListener(event: "SIGINT", listener: () => void): this;
365 prependOnceListener(event: "SIGTSTP", listener: () => void): this;
366 prependOnceListener(event: "exit", listener: () => void): this;
367 prependOnceListener(event: "reset", listener: (context: Context) => void): this;
368 }
369
370 /**
371 * A flag passed in the REPL options. Evaluates expressions in sloppy mode.
372 */
373 const REPL_MODE_SLOPPY: unique symbol;
374
375 /**
376 * A flag passed in the REPL options. Evaluates expressions in strict mode.
377 * This is equivalent to prefacing every repl statement with `'use strict'`.
378 */
379 const REPL_MODE_STRICT: unique symbol;
380
381 /**
382 * Creates and starts a `repl.REPLServer` instance.
383 *
384 * @param options The options for the `REPLServer`. If `options` is a string, then it specifies
385 * the input prompt.
386 */
387 function start(options?: string | ReplOptions): REPLServer;
388
389 /**
390 * Indicates a recoverable error that a `REPLServer` can use to support multi-line input.
391 *
392 * @see https://nodejs.org/dist/latest-v10.x/docs/api/repl.html#repl_recoverable_errors
393 */
394 class Recoverable extends SyntaxError {
395 err: Error;
396
397 constructor(err: Error);
398 }
399}