Skip to content

Commit 95b0f9d

Browse files
doc: clarify the multi REPL example
clarify the example presented where multiple REPL instances are run in the same process by: - making its title unambiguous - clarifying that they share the same `global` object (which currently is a bit ambiguous/half implied) - making sure that they do share the same `global` object (via `useGlobal` set to `true`) - they delete the unix socket if present (so that people running the code twice don't get confused/annoyed that the second time it doesn't properly work) PR-URL: nodejs#57759 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 86f86a2 commit 95b0f9d

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

doc/api/repl.md

+25-6
Original file line numberDiff line numberDiff line change
@@ -822,43 +822,52 @@ For example, the following can be added to a `.bashrc` file:
822822
alias node="env NODE_NO_READLINE=1 rlwrap node"
823823
```
824824

825-
### Starting multiple REPL instances against a single running instance
825+
### Starting multiple REPL instances in the same process
826826

827827
It is possible to create and run multiple REPL instances against a single
828-
running instance of Node.js that share a single `global` object but have
829-
separate I/O interfaces.
828+
running instance of Node.js that share a single `global` object (by setting
829+
the `useGlobal` option to `true`) but have separate I/O interfaces.
830830

831831
The following example, for instance, provides separate REPLs on `stdin`, a Unix
832-
socket, and a TCP socket:
832+
socket, and a TCP socket, all sharing the same `global` object:
833833

834834
```mjs
835835
import net from 'node:net';
836836
import repl from 'node:repl';
837837
import process from 'node:process';
838+
import fs from 'node:fs';
838839

839840
let connections = 0;
840841

841842
repl.start({
842843
prompt: 'Node.js via stdin> ',
844+
useGlobal: true,
843845
input: process.stdin,
844846
output: process.stdout,
845847
});
846848

849+
const unixSocketPath = '/tmp/node-repl-sock';
850+
851+
// If the socket file already exists let's remove it
852+
fs.rmSync(unixSocketPath, { force: true });
853+
847854
net.createServer((socket) => {
848855
connections += 1;
849856
repl.start({
850857
prompt: 'Node.js via Unix socket> ',
858+
useGlobal: true,
851859
input: socket,
852860
output: socket,
853861
}).on('exit', () => {
854862
socket.end();
855863
});
856-
}).listen('/tmp/node-repl-sock');
864+
}).listen(unixSocketPath);
857865

858866
net.createServer((socket) => {
859867
connections += 1;
860868
repl.start({
861869
prompt: 'Node.js via TCP socket> ',
870+
useGlobal: true,
862871
input: socket,
863872
output: socket,
864873
}).on('exit', () => {
@@ -870,29 +879,39 @@ net.createServer((socket) => {
870879
```cjs
871880
const net = require('node:net');
872881
const repl = require('node:repl');
882+
const fs = require('node:fs');
883+
873884
let connections = 0;
874885

875886
repl.start({
876887
prompt: 'Node.js via stdin> ',
888+
useGlobal: true,
877889
input: process.stdin,
878890
output: process.stdout,
879891
});
880892

893+
const unixSocketPath = '/tmp/node-repl-sock';
894+
895+
// If the socket file already exists let's remove it
896+
fs.rmSync(unixSocketPath, { force: true });
897+
881898
net.createServer((socket) => {
882899
connections += 1;
883900
repl.start({
884901
prompt: 'Node.js via Unix socket> ',
902+
useGlobal: true,
885903
input: socket,
886904
output: socket,
887905
}).on('exit', () => {
888906
socket.end();
889907
});
890-
}).listen('/tmp/node-repl-sock');
908+
}).listen(unixSocketPath);
891909

892910
net.createServer((socket) => {
893911
connections += 1;
894912
repl.start({
895913
prompt: 'Node.js via TCP socket> ',
914+
useGlobal: true,
896915
input: socket,
897916
output: socket,
898917
}).on('exit', () => {

0 commit comments

Comments
 (0)