IPC 连接的识别路径


net.connect(), net.createConnection(), server.listen()socket.connect() 使用一个 path 参数来识别 IPC 端点。

在 Unix 上,本地域也称为 Unix 域。 参数 path 是文件系统路径名。 它会被截断为依赖于系统的 sizeof(sockaddr_un.sun_path) - 1 的长度。 典型值在 Linux 上为 107,在 macOS 上为 103。 如果一个 Node.js API 的抽象创建了 Unix 域 socket,则它也可以 unlink 该 Unix 域 socket。 例如,net.createServer() 可以创建 Unix 域 socket,而 server.close() 可以 unlink 它。 但是,如果用户在这些抽象之外创建 Unix 域 socket,则用户需要自己删除它。 当 Node.js API 创建 Unix 域 socket 但该程序随后崩溃时,情况也是如此。 简而言之,Unix 域 socket 会在文件系统中可见,且持续到被 unlink。

在 Windows 上,本地域通过命名管道实现。路径必须是以 \\?\pipe\\\.\pipe\ 为入口。路径允许任何字符,但后面的字符可能会对管道名称进行一些处理,例如解析 .. 序列。尽管如此,管道空间是平面的。管道不会持续,当最后一次引用关闭时,管道就会被删除。

JavaScript 字符串转义需要使用双反斜杠指定路径,例如:

net.createServer().listen(
  path.join('\\\\?\\pipe', process.cwd(), 'myctl'));

net.connect(), net.createConnection(), server.listen() and socket.connect() take a path parameter to identify IPC endpoints.

On Unix, the local domain is also known as the Unix domain. The path is a filesystem pathname. It gets truncated to an OS-dependent length of sizeof(sockaddr_un.sun_path) - 1. Typical values are 107 bytes on Linux and 103 bytes on macOS. If a Node.js API abstraction creates the Unix domain socket, it will unlink the Unix domain socket as well. For example, net.createServer() may create a Unix domain socket and server.close() will unlink it. But if a user creates the Unix domain socket outside of these abstractions, the user will need to remove it. The same applies when a Node.js API creates a Unix domain socket but the program then crashes. In short, a Unix domain socket will be visible in the filesystem and will persist until unlinked.

On Windows, the local domain is implemented using a named pipe. The path must refer to an entry in \\?\pipe\ or \\.\pipe\. Any characters are permitted, but the latter may do some processing of pipe names, such as resolving .. sequences. Despite how it might look, the pipe namespace is flat. Pipes will not persist. They are removed when the last reference to them is closed. Unlike Unix domain sockets, Windows will close and remove the pipe when the owning process exits.

JavaScript string escaping requires paths to be specified with extra backslash escaping such as:

net.createServer().listen(
  path.join('\\\\?\\pipe', process.cwd(), 'myctl'));