| 200 | } |
| 201 | |
| 202 | class Proxy implements ProxiedProcessOwner { |
| 203 | readonly commands = new Map<number, ProxiedProcess>() |
| 204 | readonly credentials = credentials() |
| 205 | pid: number | undefined = undefined |
| 206 | ready = false |
| 207 | exited = false |
| 208 | retired = false |
| 209 | /** Done once the proxy process is ready, or fails with the error that stopped it first. */ |
| 210 | readonly started = Deferred.makeUnsafe<void, Error>() |
| 211 | private readonly options: Options |
| 212 | private readonly token = Crypto.randomBytes(tokenLength) |
| 213 | private nextId = 0 |
| 214 | // Starting holds a reference, so the application waits for the proxy process to be ready |
| 215 | private refs = 1 |
| 216 | private directory: string | undefined |
| 217 | private server: Net.Server | undefined |
| 218 | private child: NodeChildProcess.ChildProcess | undefined |
| 219 | // Bounds the wait for `Ready`, and for the reply to each sent request |
| 220 | private readonly timers = new Map<number | "ready", ReturnType<typeof setTimeout>>() |
| 221 | |
| 222 | constructor(options: Options) { |
| 223 | this.options = options |
| 224 | try { |
| 225 | this.directory = Fs.mkdtempSync(Path.join(Os.tmpdir(), "effect-proxy-")) |
| 226 | const socketPath = Path.join(this.directory, "socket") |
| 227 | if (Buffer.byteLength(socketPath) > maxSocketPathLength) { |
| 228 | throw new Error( |
| 229 | `The proxy process socket path ${socketPath} exceeds ${maxSocketPathLength} bytes; set TMPDIR to a shorter directory` |
| 230 | ) |
| 231 | } |
| 232 | this.server = Net.createServer((socket) => this.accept(socket)) |
| 233 | this.server.on("error", (error) => this.end(error)) |
| 234 | this.server.listen(socketPath) |
| 235 | this.server.unref() |
| 236 | const env = options.env ?? { ...globalThis.process.env } |
| 237 | if (options.env === undefined) { |
| 238 | // Commands receive their environment with each request, and the proxy |
| 239 | // process must not load the application's preloads or inspector |
| 240 | delete env.NODE_OPTIONS |
| 241 | } |
| 242 | this.child = NodeChildProcess.spawn( |
| 243 | options.executable ?? globalThis.process.execPath, |
| 244 | options.args ?? ["--input-type=module", "-e", source], |
| 245 | { env, stdio: ["inherit", "inherit", "inherit", "ipc"], windowsHide: true } |
| 246 | ) |
| 247 | this.pid = this.child.pid |
| 248 | this.child.on("message", (message: Message) => this.receive(message)) |
| 249 | this.child.on("error", (error) => this.end(error)) |
| 250 | this.child.on( |
| 251 | "exit", |
| 252 | (code, signal) => |
| 253 | this.end(new Error(`The proxy process exited with ${signal === null ? `code ${code}` : `signal ${signal}`}`)) |
| 254 | ) |
| 255 | this.startTimer("ready") |
| 256 | // The socket path and token are kept out of the arguments, which other |
| 257 | // users can read |
| 258 | this.send({ _tag: "Init", socketPath, token: this.token.toString("hex") }) |
| 259 | this.updateRef() |
nothing calls this directly
no test coverage detected