MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / runSpeedtestCommand

Function runSpeedtestCommand

speedlink/speedlink.ts:82–116  ·  view source on GitHub ↗

* Execute a remote or local speedtest command safely using spawn/execFile * to prevent shell injection via user-supplied server config fields.

(
  command: string,
  args: string[],
  options: { timeout: number; env?: NodeJS.ProcessEnv },
  label: string
)

Source from the content-addressed store, hash-verified

80
81/**
82 * Execute a remote or local speedtest command safely using spawn/execFile
83 * to prevent shell injection via user-supplied server config fields.
84 */
85async function runSpeedtestCommand(
86 command: string,
87 args: string[],
88 options: { timeout: number; env?: NodeJS.ProcessEnv },
89 label: string
90): Promise<{ stdout: string; stderr: string }> {
91 return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
92 const child = spawn(command, args, {
93 timeout: options.timeout,
94 env: options.env,
95 killSignal: "SIGKILL" as const,
96 shell: false,
97 });
98 let stdout = "";
99 let stderr = "";
100 child.stdout?.on("data", (data: Buffer) => { stdout += data.toString(); });
101 child.stderr?.on("data", (data: Buffer) => { stderr += data.toString(); });
102 child.on("error", (err: Error) => { reject(err); });
103 child.on("close", (code: number | null) => {
104 if (code === 0) {
105 resolve({ stdout, stderr });
106 } else {
107 const err: any = new Error(`${label} exited with code ${code}: ${stderr || stdout}`);
108 err.stderr = stderr;
109 reject(err);
110 }
111 });
112 // Force-kill on timeout (spawn timeout only signals, doesn't kill by default)
113 const timer = setTimeout(() => {
114 child.kill("SIGKILL");
115 reject(new Error(`${label} timed out after ${options.timeout}ms`));
116 }, options.timeout);
117 child.on("close", () => { clearTimeout(timer); });
118 });
119}

Callers 1

speedtestFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected