From 1ef298aac665385fb26cbea62aa04cc51a987409 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Wed, 4 Jan 2023 18:54:15 -0700 Subject: [PATCH] Fix some logic and control flow issues --- backend/k8s.js | 8 ++++++-- backend/sandbox-k8s.js | 4 ++-- backend/util.js | 34 ++++++++++++++++++++++++---------- package.json | 1 - yarn.lock | 5 ----- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/backend/k8s.js b/backend/k8s.js index 57c5318..ca5b450 100644 --- a/backend/k8s.js +++ b/backend/k8s.js @@ -284,7 +284,7 @@ export async function initUserSession({ watcher, podName, proxyInfo }) { } } resolve({ - exec: (cmdline, { on, pty }) => { + exec: async (cmdline, { on, pty }) => { // on :: { stdout, stderr, exit, error, close } if (pty) { cmdline = ["/riju-bin/ptyify", ...cmdline]; @@ -312,6 +312,10 @@ export async function initUserSession({ watcher, podName, proxyInfo }) { } ); } + await new Promise((resolve, reject) => { + conn.on("open", resolve); + conn.on("error", reject); + }); conn.on("message", (msg) => { let event, data, text, exitStatus; try { @@ -352,7 +356,7 @@ export async function initUserSession({ watcher, podName, proxyInfo }) { return { stdin: { write: (data) => - conn.write(JSON.stringify({ event: "stdin", data })), + conn.send(JSON.stringify({ event: "stdin", data })), }, }; }, diff --git a/backend/sandbox-k8s.js b/backend/sandbox-k8s.js index 5a529f9..e70052f 100644 --- a/backend/sandbox-k8s.js +++ b/backend/sandbox-k8s.js @@ -57,8 +57,8 @@ async function main() { handlePtyInput: (data) => handlePtyInput(data), handlePtyExit: (_status) => {}, }); - await new Promise((resolve) => { - const exec = session.exec(["bash"], { + await new Promise(async (resolve) => { + const exec = await session.exec(["bash"], { pty: true, on: { stdout: (data) => pty.handlePtyOutput(data), diff --git a/backend/util.js b/backend/util.js index 27d4804..0ba4906 100644 --- a/backend/util.js +++ b/backend/util.js @@ -2,7 +2,6 @@ import { spawn } from "child_process"; import { promises as fs } from "fs"; import process from "process"; -import TailFile from "@logdna/tail-file"; import * as Sentry from "@sentry/node"; import * as tmp from "tmp-promise"; import { v4 as getUUIDOrig } from "uuid"; @@ -188,25 +187,40 @@ export function deptyify({ handlePtyInput, handlePtyExit }) { } }); }); - const input = new TailFile(`${dir.path}/input`, { - encoding: "utf-8", - }); - input.on("data", (data) => handlePtyInput(data)); - input.on("tail_error", logError); - input.on("error", logError); - await input.start(); - const output = await fs.open(`${dir.path}/output`, "w"); const proc = spawn( - "system/out/riju-pty", + `${process.cwd()}/system/out/riju-pty`, ["-f", "sh", "-c", "cat > input & cat output"], { cwd: dir.path, + stdio: "inherit", } ); + await new Promise((resolve, reject) => { + proc.on("spawn", resolve); + proc.on("error", reject); + }); proc.on("exit", (status) => { handlePtyExit(status); triggerDone(); }); + const [input, output] = await new Promise((resolve, reject) => { + setTimeout(() => reject("timed out"), 5000); + resolve( + Promise.all([ + fs.open(`${dir.path}/input`, "r"), + fs.open(`${dir.path}/output`, "w"), + ]) + ); + }); + setTimeout(async () => { + try { + while (true) { + handlePtyInput(await input.read({ encoding: "utf-8" })); + } + } catch (err) { + logError(err); + } + }, 0); resolve({ handlePtyOutput: async (data) => { await output.write(data); diff --git a/package.json b/package.json index ca90f08..0064d3a 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "@babel/preset-env": "^7.12.11", "@balena/dockerignore": "^1.0.2", "@kubernetes/client-node": "^0.18.0", - "@logdna/tail-file": "^3.0.0", "@sentry/node": "^6.11.0", "async-lock": "^1.2.6", "babel-loader": "^8.2.2", diff --git a/yarn.lock b/yarn.lock index 68136cc..21852d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -904,11 +904,6 @@ optionalDependencies: openid-client "^5.3.0" -"@logdna/tail-file@^3.0.0": - version "3.0.0" - resolved "https://registry.yarnpkg.com/@logdna/tail-file/-/tail-file-3.0.0.tgz#0120353e59bf04b318d4861684e95682b0e85071" - integrity sha512-aZy8XzJI9zwl7gbmQa1L5Hr6yJbS062OMQJ7I9sqHD/QMEBfwlhxyytJ4Qe2jAoimefuabopEi4Gk+t/7to6/A== - "@sentry/core@6.11.0": version "6.11.0" resolved "https://registry.yarnpkg.com/@sentry/core/-/core-6.11.0.tgz#40e94043afcf6407a109be26655c77832c64e740"