11/*
2- * Copyright (C)2014-2020 Haxe Foundation
2+ * Copyright (C)2014-2026 Haxe Foundation
33 *
44 * Permission is hereby granted, free of charge, to any person obtaining a
55 * copy of this software and associated documentation files (the "Software"),
2323package js .node .repl ;
2424
2525import haxe .DynamicAccess ;
26+ import haxe .extern .EitherType ;
2627import js .lib .Error ;
27- import js .node .events .EventEmitter ;
28+ import js .node .Repl .ReplOptions ;
29+ import js .node .events .EventEmitter .Event ;
30+ import js .node .readline .Interface ;
31+ import js .node .stream .Readable .IReadable ;
32+ import js .node .stream .Writable .IWritable ;
2833
2934/**
30- Enumeration of events emitted by the `REPLServer` objects.
35+ Enumeration of events emitted by `REPLServer` objects.
3136**/
3237enum abstract REPLServerEvent <T : haxe. Constraints . Function >(Event <T >) to Event <T > {
3338 /**
3439 The `'exit'` event is emitted when the REPL is exited either by receiving the `.exit` command as input,
35- the user pressing `<ctrl>-C` twice to signal `SIGINT`, or by pressing `<ctrl>-D` to signal 'end' on the input stream.
40+ the user pressing Ctrl+C twice to signal `SIGINT`, or by pressing Ctrl+D to signal `'end'` on the input stream.
41+
42+ @see https://nodejs.org/docs/latest-v24.x/api/repl.html#event-exit
3643 **/
37- var Exit : REPLServerEvent <Void -> Void > = " exit" ;
44+ var Exit : REPLServerEvent <() -> Void > = " exit" ;
3845
3946 /**
4047 The `'reset'` event is emitted when the REPL's context is reset.
48+ This occurs whenever the `.clear` command is received as input unless the REPL is using the default evaluator
49+ and the `REPLServer` instance was created with the `useGlobal` option set to `true`.
4150
4251 // TODO(section-5): type context beyond DynamicAccess<Dynamic>
52+
53+ @see https://nodejs.org/docs/latest-v24.x/api/repl.html#event-reset
4354 **/
4455 var Reset : REPLServerEvent <(context : DynamicAccess <Dynamic >) -> Void > = " reset" ;
4556}
4657
4758/**
48- Instances of `repl.REPLServer` are created using the `repl.start()` method and should not be created directly using
49- the JavaScript `new` keyword.
59+ Instances of `repl.REPLServer` are created using `repl.start()` or directly with `new REPLServer(options)`.
60+
61+ Extends `readline.Interface`.
5062
5163 @see https://nodejs.org/docs/latest-v24.x/api/repl.html#class-replserver
5264**/
5365@:jsRequire (" repl" , " REPLServer" )
54- extern class REPLServer extends EventEmitter <REPLServer > {
66+ extern class REPLServer extends Interface {
67+ /**
68+ Creates a new `REPLServer` instance. Prefer `Repl.start()` in most cases.
69+ **/
70+ function new (? options : ReplOptions );
71+
5572 /**
56- It is possible to expose a variable to the REPL explicitly by assigning it to the `context` object associated
57- with each `REPLServer`.
73+ The `vm` / REPL context object provided to the `eval` function.
5874
5975 // TODO(section-5): type context beyond DynamicAccess<Dynamic>
6076 **/
6177 var context (default , null ): DynamicAccess <Dynamic >;
6278
79+ /**
80+ The `Readable` stream from which REPL input will be read.
81+ **/
82+ var input (default , null ): IReadable ;
83+
84+ /**
85+ The `Writable` stream to which REPL output will be written.
86+ **/
87+ var output (default , null ): IWritable ;
88+
89+ /**
90+ Deprecated alias for `input`. Deprecated since: v14.3.0.
91+ **/
92+ @:deprecated (" Use input instead" )
93+ var inputStream (default , null ): IReadable ;
94+
95+ /**
96+ Deprecated alias for `output`. Deprecated since: v14.3.0.
97+ **/
98+ @:deprecated (" Use output instead" )
99+ var outputStream (default , null ): IWritable ;
100+
101+ /**
102+ Commands registered via `defineCommand()`.
103+ **/
104+ var commands (default , null ): DynamicAccess <EitherType <REPLCommand , (rest : String ) -> Void >>;
105+
63106 /**
64107 The `replServer.defineCommand()` method is used to add new `.`-prefixed commands to the REPL instance.
65108 **/
66109 @:overload (function (keyword : String , cmd : (rest : String ) -> Void ): Void {})
67- function defineCommand (keyword : String , cmd : REPLServerOptions ): Void ;
110+ function defineCommand (keyword : String , cmd : REPLCommand ): Void ;
68111
69112 /**
70- The `replServer.displayPrompt()` method readies the REPL instance for input from the user.
113+ The `replServer.displayPrompt()` method readies the REPL instance for input from the user,
114+ printing the configured `prompt` to a new line in `output` and resuming `input`.
115+
116+ When multi-line input is being entered, a pipe `'|'` is printed rather than the prompt.
71117 **/
72118 function displayPrompt (? preserveCursor : Bool ): Void ;
73119
@@ -78,21 +124,57 @@ extern class REPLServer extends EventEmitter<REPLServer> {
78124
79125 /**
80126 Initializes a history log file for the REPL instance.
127+
128+ When `historyConfig` is a string, it is the path to the history file.
129+ Since Node.js v24.2.0, an options object may be passed instead.
81130 **/
131+ @:overload (function (historyConfig : REPLServerHistoryOptions , ? callback : (err : Null <Error >, repl : Null <REPLServer >) -> Void ): Void {})
82132 function setupHistory (historyPath : String , callback : (err : Null <Error >, repl : Null <REPLServer >) -> Void ): Void ;
83133}
84134
135+ /**
136+ Object form of `replServer.setupHistory` (Node.js v24.2.0+).
137+
138+ @see https://nodejs.org/docs/latest-v24.x/api/repl.html#replserversetuphistoryhistoryconfig-callback
139+ **/
140+ typedef REPLServerHistoryOptions = {
141+ /**
142+ The path to the history file.
143+ **/
144+ @:optional var filePath : String ;
145+
146+ /**
147+ Maximum number of history lines retained. Set to `0` to disable history.
148+ Only applies when `terminal` is `true`. Default: `30`.
149+ **/
150+ @:optional var size : Int ;
151+
152+ /**
153+ If `true`, when a new input line duplicates an older one, the older line is removed. Default: `false`.
154+ **/
155+ @:optional var removeHistoryDuplicates : Bool ;
156+
157+ /**
158+ Called when history writes are ready or upon error.
159+ May be used instead of the `callback` argument to `setupHistory`.
160+ **/
161+ @:optional var onHistoryFileLoaded : (err : Null <Error >, repl : Null <REPLServer >) -> Void ;
162+ }
163+
85164/**
86165 Options object used by `REPLServer.defineCommand`.
87166**/
88- typedef REPLServerOptions = {
167+ typedef REPLCommand = {
89168 /**
90169 Help text to be displayed when `.help` is entered.
91170 **/
92171 @:optional var help : String ;
93172
94173 /**
95- The function to execute.
174+ The function to execute, optionally accepting a single string argument .
96175 **/
97176 var action : (rest : String ) -> Void ;
98177}
178+
179+ /** @deprecated Use `REPLCommand` instead. **/
180+ typedef REPLServerOptions = REPLCommand ;
0 commit comments