mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
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: https://github.com/nodejs/node/pull/57759
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
1 parent
30060e13d3
commit
f5ea06c61f
1 file changed
+25
-6
+25
-6
@@ -822,43 +822,52 @@ For example, the following can be added to a `.bashrc` file:
|
||||
alias node="env NODE_NO_READLINE=1 rlwrap node"
|
||||
```
|
||||
|
||||
### Starting multiple REPL instances against a single running instance
|
||||
### Starting multiple REPL instances in the same process
|
||||
|
||||
It is possible to create and run multiple REPL instances against a single
|
||||
running instance of Node.js that share a single `global` object but have
|
||||
separate I/O interfaces.
|
||||
running instance of Node.js that share a single `global` object (by setting
|
||||
the `useGlobal` option to `true`) but have separate I/O interfaces.
|
||||
|
||||
The following example, for instance, provides separate REPLs on `stdin`, a Unix
|
||||
socket, and a TCP socket:
|
||||
socket, and a TCP socket, all sharing the same `global` object:
|
||||
|
||||
```mjs
|
||||
import net from 'node:net';
|
||||
import repl from 'node:repl';
|
||||
import process from 'node:process';
|
||||
import fs from 'node:fs';
|
||||
|
||||
let connections = 0;
|
||||
|
||||
repl.start({
|
||||
prompt: 'Node.js via stdin> ',
|
||||
useGlobal: true,
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
|
||||
const unixSocketPath = '/tmp/node-repl-sock';
|
||||
|
||||
// If the socket file already exists let's remove it
|
||||
fs.rmSync(unixSocketPath, { force: true });
|
||||
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via Unix socket> ',
|
||||
useGlobal: true,
|
||||
input: socket,
|
||||
output: socket,
|
||||
}).on('exit', () => {
|
||||
socket.end();
|
||||
});
|
||||
}).listen('/tmp/node-repl-sock');
|
||||
}).listen(unixSocketPath);
|
||||
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via TCP socket> ',
|
||||
useGlobal: true,
|
||||
input: socket,
|
||||
output: socket,
|
||||
}).on('exit', () => {
|
||||
@@ -870,29 +879,39 @@ net.createServer((socket) => {
|
||||
```cjs
|
||||
const net = require('node:net');
|
||||
const repl = require('node:repl');
|
||||
const fs = require('node:fs');
|
||||
|
||||
let connections = 0;
|
||||
|
||||
repl.start({
|
||||
prompt: 'Node.js via stdin> ',
|
||||
useGlobal: true,
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
|
||||
const unixSocketPath = '/tmp/node-repl-sock';
|
||||
|
||||
// If the socket file already exists let's remove it
|
||||
fs.rmSync(unixSocketPath, { force: true });
|
||||
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via Unix socket> ',
|
||||
useGlobal: true,
|
||||
input: socket,
|
||||
output: socket,
|
||||
}).on('exit', () => {
|
||||
socket.end();
|
||||
});
|
||||
}).listen('/tmp/node-repl-sock');
|
||||
}).listen(unixSocketPath);
|
||||
|
||||
net.createServer((socket) => {
|
||||
connections += 1;
|
||||
repl.start({
|
||||
prompt: 'Node.js via TCP socket> ',
|
||||
useGlobal: true,
|
||||
input: socket,
|
||||
output: socket,
|
||||
}).on('exit', () => {
|
||||
|
||||
Reference in new issue
Block a user