针对单个正在运行的实例启动多个 REPL 实例
【Starting multiple REPL instances against a single running instance】
可以针对单个运行中的 Node.js 实例创建并运行多个 REPL 实例,这些实例共享同一个 global 对象,但具有独立的输入/输出接口。
【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.】
例如,下面的例子在 stdin、Unix 套接字和 TCP 套接字上提供了独立的 REPL:
【The following example, for instance, provides separate REPLs on stdin, a Unix
socket, and a TCP socket:】
import net from 'node:net';
import repl from 'node:repl';
import process from 'node:process';
let connections = 0;
repl.start({
prompt: 'Node.js via stdin> ',
input: process.stdin,
output: process.stdout,
});
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via Unix socket> ',
input: socket,
output: socket,
}).on('exit', () => {
socket.end();
});
}).listen('/tmp/node-repl-sock');
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via TCP socket> ',
input: socket,
output: socket,
}).on('exit', () => {
socket.end();
});
}).listen(5001);const net = require('node:net');
const repl = require('node:repl');
let connections = 0;
repl.start({
prompt: 'Node.js via stdin> ',
input: process.stdin,
output: process.stdout,
});
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via Unix socket> ',
input: socket,
output: socket,
}).on('exit', () => {
socket.end();
});
}).listen('/tmp/node-repl-sock');
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via TCP socket> ',
input: socket,
output: socket,
}).on('exit', () => {
socket.end();
});
}).listen(5001);从命令行运行此应用将会在标准输入上启动一个 REPL。其他 REPL 客户端可以通过 Unix 套接字或 TCP 套接字进行连接。例如,telnet 对连接 TCP 套接字非常有用,而 socat 可以用于连接 Unix 和 TCP 套接字。
【Running this application from the command line will start a REPL on stdin.
Other REPL clients may connect through the Unix socket or TCP socket. telnet,
for instance, is useful for connecting to TCP sockets, while socat can be used
to connect to both Unix and TCP sockets.】
通过从基于 Unix 套接字的服务器而不是标准输入启动 REPL,可以在不中断的情况下连接到一个长期运行的 Node.js 进程。
【By starting a REPL from a Unix socket-based server instead of stdin, it is possible to connect to a long-running Node.js process without restarting it.】
关于在 net.Server 和 net.Socket 实例上运行“全功能”(terminal)REPL 的示例,请参见:
https://gist.github.com/TooTallNate/2209310。
有关在 curl(1) 上运行 REPL 实例的示例,请参见:
https://gist.github.com/TooTallNate/2053342。
此示例纯粹用于教育目的,用于演示如何使用不同的输入/输出流启动 Node.js REPL。它不应在生产环境或任何关注安全性的情况下使用,除非采用额外的保护措施。如果需要在实际应用中实现 REPL,请考虑替代方法以降低这些风险,例如使用安全的输入机制并避免开放网络接口。
【This example is intended purely for educational purposes to demonstrate how Node.js REPLs can be started using different I/O streams. It should not be used in production environments or any context where security is a concern without additional protective measures. If you need to implement REPLs in a real-world application, consider alternative approaches that mitigate these risks, such as using secure input mechanisms and avoiding open network interfaces.】