MCPcopy Create free account
hub / github.com/Effect-TS/effect / run

Function run

packages/platform/node-shared/src/internal/proxyProcessWorker.ts:37–101  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

35
36/** @internal */
37export const run = (): void => {
38 if (typeof process.send !== "function") {
39 process.stderr.write("The proxy process must be started by NodeChildProcessSpawner.layerProxyProcess\n")
40 process.exitCode = 1
41 return
42 }
43 const send = (message: object) => {
44 if (process.connected) process.send?.(message)
45 }
46 const serializeError = (error: NodeJS.ErrnoException) => ({
47 message: error.message,
48 code: error.code,
49 errno: error.errno,
50 syscall: error.syscall,
51 path: error.path
52 })
53 const connect = (socketPath: string, token: string, id: number, fd: number) =>
54 new Promise<Net.Socket>((resolve, reject) => {
55 const socket = Net.connect(socketPath)
56 socket.pause()
57 socket.once("error", reject)
58 socket.once("connect", () => {
59 const header = Buffer.alloc(headerLength)
60 header.write(token, "hex")
61 header.writeUInt32BE(id, tokenLength)
62 header.writeUInt32BE(fd, tokenLength + 4)
63 socket.write(header, (error) => error ? reject(error) : resolve(socket))
64 })
65 })
66 const spawn = async (socketPath: string, token: string, { args, command, id, options }: Request) => {
67 const stdio = await Promise.allSettled(
68 options.stdio.map((mode, fd) => mode === "pipe" ? connect(socketPath, token, id, fd) : mode)
69 )
70 const values = stdio.map((result) => result.status === "fulfilled" ? result.value : undefined)
71 const sockets = values.filter((value) => value instanceof Net.Socket)
72 try {
73 const failure = stdio.find((result) => result.status === "rejected")
74 if (failure) throw failure.reason
75 // The command shares the file status flags of each socket, and libuv only
76 // makes fds 0 to 2 blocking
77 for (const socket of sockets) {
78 // A socket that has lost its handle is rejected by `spawn`
79 const errno = (socket as SocketWithHandle)._handle?.setBlocking(true)
80 if (errno) throw new Error(`setBlocking failed: ${Util.getSystemErrorName(errno)}`)
81 }
82 const child = ChildProcess.spawn(command, args, { ...options, stdio: values as Array<Net.Socket | "inherit"> })
83 child.on("error", (error) => send({ _tag: "Error", id, error: serializeError(error) }))
84 child.on("spawn", () => send({ _tag: "Spawned", id, pid: child.pid }))
85 child.on("exit", (code, signal) => send({ _tag: "Exit", id, code, signal }))
86 } catch (error) {
87 send({ _tag: "Error", id, error: serializeError(error as NodeJS.ErrnoException) })
88 } finally {
89 // The command holds its own copies of the sockets
90 for (const socket of sockets) socket.destroy()
91 }
92 }
93 // The application decides how to handle these. Once it has disconnected,
94 // the proxy process exits when its commands have exited.

Callers

nothing calls this directly

Calls 5

onceMethod · 0.80
spawnFunction · 0.70
sendFunction · 0.70
writeMethod · 0.65
onMethod · 0.65

Tested by

no test coverage detected