|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from "fs"; |
| 3 | +import { spawn, spawnSync } from "child_process"; |
| 4 | +import path from "path"; |
| 5 | +import { arch, platform } from "os"; |
| 6 | +import { VERSIONS } from "./versions"; |
| 7 | + |
| 8 | +const PACKAGE_VERSION = `rpc-router ${VERSIONS.RPC_ROUTER}`; |
| 9 | + |
| 10 | +function getBinaryVersion(location: string): [string | null, string | null] { |
| 11 | + const result = spawnSync(location, ["--version"]); |
| 12 | + const error: string | null = |
| 13 | + (result.error && result.error.toString()) || |
| 14 | + (result.stderr.length > 0 && result.stderr.toString().trim()) || |
| 15 | + null; |
| 16 | + return [error, result.stdout && result.stdout.toString().trim()]; |
| 17 | +} |
| 18 | + |
| 19 | +function getExePath(): string { |
| 20 | + let os: string = platform(); |
| 21 | + let extension = ""; |
| 22 | + if (["win32", "cygwin"].includes(os)) { |
| 23 | + os = "windows"; |
| 24 | + extension = ".exe"; |
| 25 | + } |
| 26 | + const binaryName = `@magicblock-labs/rpc-router-${os}-${arch()}/bin/rpc-router${extension}`; |
| 27 | + try { |
| 28 | + return require.resolve(binaryName); |
| 29 | + } catch (e) { |
| 30 | + throw new Error( |
| 31 | + `Couldn't find application binary inside node_modules for ${os}-${arch()}, expected location: ${binaryName}`, |
| 32 | + ); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function runWithForwardedExit(child: ReturnType<typeof spawn>): void { |
| 37 | + child.on("exit", (code: number | null, signal: NodeJS.Signals | null) => { |
| 38 | + process.on("exit", () => { |
| 39 | + if (signal) { |
| 40 | + process.kill(process.pid, signal); |
| 41 | + } else if (code !== null) { |
| 42 | + process.exit(code); |
| 43 | + } |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + process.on("SIGINT", () => { |
| 48 | + child.kill("SIGINT"); |
| 49 | + child.kill("SIGTERM"); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +function runRpcRouter(location: string): void { |
| 54 | + const args = process.argv.slice(2); |
| 55 | + const env = { |
| 56 | + ...process.env, |
| 57 | + }; |
| 58 | + const ephemeralValidator = spawn(location, args, { stdio: "inherit", env}); |
| 59 | + runWithForwardedExit(ephemeralValidator); |
| 60 | +} |
| 61 | + |
| 62 | +function tryPackageRpcRouter(): boolean { |
| 63 | + try { |
| 64 | + const path = getExePath(); |
| 65 | + runRpcRouter(path); |
| 66 | + return true; |
| 67 | + } catch (e) { |
| 68 | + console.error( |
| 69 | + "Failed to run rpc-router from package:", |
| 70 | + e instanceof Error ? e.message : e, |
| 71 | + ); |
| 72 | + return false; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function trySystemRpcRouter(): void { |
| 77 | + const absolutePath = process.env.PATH?.split(path.delimiter) |
| 78 | + .filter((dir) => dir !== path.dirname(process.argv[1])) |
| 79 | + .find((dir) => { |
| 80 | + try { |
| 81 | + fs.accessSync(`${dir}/rpc-router`, fs.constants.X_OK); |
| 82 | + return true; |
| 83 | + } catch { |
| 84 | + return false; |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + if (!absolutePath) { |
| 89 | + console.error( |
| 90 | + `Could not find globally installed rpc-router, please install with cargo.`, |
| 91 | + ); |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | + |
| 95 | + const absoluteBinaryPath = `${absolutePath}/rpc-router`; |
| 96 | + const [error, binaryVersion] = getBinaryVersion(absoluteBinaryPath); |
| 97 | +
|
| 98 | + if (error !== null) { |
| 99 | + console.error(`Failed to get version of global binary: ${error}`); |
| 100 | + return; |
| 101 | + } |
| 102 | + if (binaryVersion !== PACKAGE_VERSION) { |
| 103 | + console.error( |
| 104 | + `Globally installed rpc-router version is not correct. Expected "${PACKAGE_VERSION}", found "${binaryVersion}".`, |
| 105 | + ); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + runRpcRouter(absoluteBinaryPath); |
| 110 | +} |
| 111 | + |
| 112 | +// If the first argument is our special command, run the test validator and exit. |
| 113 | +tryPackageRpcRouter() || trySystemRpcRouter(); |
0 commit comments