针对单个正在运行的实例启动多个 REPL 实例


¥Starting multiple REPL instances against a single running instance

可以针对共享单个 global 对象但具有单独 I/O 接口的单个​​运行 Node.js 实例创建和运行多个 REPL 实例。

¥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.Servernet.Socket 实例上运行 "full-featured" (terminal) REPL 的示例,请参阅:https://gist.github.com/TooTallNate/2209310

¥For an example of running a "full-featured" (terminal) REPL over a net.Server and net.Socket instance, see: https://gist.github.com/TooTallNate/2209310.

有关在 curl(1) 上运行 REPL 实例的示例,请参阅:https://gist.github.com/TooTallNate/2053342

¥For an example of running a REPL instance over curl(1), see: https://gist.github.com/TooTallNate/2053342.

此示例仅用于教育目的,以演示如何使用不同的 I/O 流启动 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.