识别 IPC 连接的路径
¥Identifying paths for IPC connections
net.connect()
、net.createConnection()
、server.listen()
、socket.connect()
采用 path
参数标识 IPC 端点。
¥net.connect()
, net.createConnection()
, server.listen()
, and
socket.connect()
take a path
parameter to identify IPC endpoints.
在 Unix 上,本地域也称为 Unix 域。路径是文件系统路径名。当路径名的长度大于 sizeof(sockaddr_un.sun_path)
的长度时,它将抛出错误。典型的值为 Linux 上的 107 字节和 macOS 上的 103 字节。如果 Node.js API 抽象创建了 Unix 域套接字,则它也会取消链接 Unix 域套接字。例如,net.createServer()
可以会创建 Unix 域套接字,而 server.close()
将取消链接它。但是如果用户在这些抽象之外创建了 Unix 域套接字,则用户将需要删除它。这同样适用于 Node.js API 创建 Unix 域套接字但程序随后崩溃的情况。简而言之,Unix 域套接字将在文件系统中可见,并且会一直存在直到取消链接。在 Linux 上,你可以通过在路径开头添加 \0
来使用 Unix 抽象套接字,例如 \0abstract
。Unix 抽象套接字的路径在文件系统中不可见,当所有对套接字的打开引用都关闭时,它将自动消失。
¥On Unix, the local domain is also known as the Unix domain. The path is a
file system pathname. It will throw an error when the length of pathname is
greater than the length of sizeof(sockaddr_un.sun_path)
. 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 file system and
will persist until unlinked. On Linux, You can use Unix abstract socket by adding
\0
to the beginning of the path, such as \0abstract
. The path to the Unix
abstract socket is not visible in the file system and it will disappear automatically
when all open references to the socket are closed.
在 Windows 上,本地域是使用命名管道实现的。该路径必须引用 \\?\pipe\
或 \\.\pipe\
中的条目。允许使用任何字符,但后者可能会对管道名称进行一些处理,例如解析 ..
序列。不管它看起来如何,管道命名空间是扁平的。管道不会持续存在。当对它们的最后一个引用关闭时,它们将被删除。与 Unix 域套接字不同,Windows 将在拥有进程退出时关闭并删除管道。
¥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 字符串转义需要使用额外的反斜杠转义来指定路径,例如:
¥JavaScript string escaping requires paths to be specified with extra backslash escaping such as:
net.createServer().listen(
path.join('\\\\?\\pipe', process.cwd(), 'myctl'));