Skip to content

Commit

Permalink
chore(daemon): tiny refactor
Browse files Browse the repository at this point in the history
Signed-off-by: KAS <[email protected]>
  • Loading branch information
truekas committed Jan 10, 2025
1 parent 71c8a2c commit 4f0eaa1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
2 changes: 2 additions & 0 deletions apps/daemon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ const app = new Elysia()
});

console.log(`✨ Stardust daemon is running at ${app.server?.hostname}:${app.server?.port}`);

// eden
export type App = typeof app;
17 changes: 17 additions & 0 deletions apps/daemon/src/session/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ export async function getFile(id: string, name: string) {
const unzipped = Bun.gunzipSync(file.read());
return unzipped;
}

export async function listFiles(id: string) {
const exec = await docker.getContainer(id).exec({
Cmd: ["sh", "-c", "ls /home/stardust/Downloads"],
AttachStdout: true,
AttachStderr: true,
});

const stream = await exec.start({ hijack: true, stdin: true });
const data = await new Promise<string>((res, err) => {
const out: string[] = [];
stream.on("error", err);
stream.on("data", (chunk) => out.push(chunk.toString()));
stream.on("end", () => res(out.join("")));
});
return data;
}
16 changes: 2 additions & 14 deletions apps/daemon/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getConfig } from "~/lib/config/index.js";
import { docker } from "~/lib/docker.js";
import createSession from "./create.js";
import deleteSession from "./delete.js";
import { getFile, sendFile } from "./file.js";
import { getFile, listFiles, sendFile } from "./file.js";
import manageSession from "./manage.js";
import screenshot from "./screenshot.js";
// fill this
Expand Down Expand Up @@ -81,19 +81,7 @@ export default new Elysia({ prefix: "/sessions" })
.group("/:id/files", (app) =>
app
.get("/list", async ({ params: { id } }) => {
const exec = await docker.getContainer(id).exec({
Cmd: ["sh", "-c", "ls /home/stardust/Downloads"],
AttachStdout: true,
AttachStderr: true,
});

const stream = await exec.start({ hijack: true, stdin: true });
const data = await new Promise<string>((res, err) => {
const out: string[] = [];
stream.on("error", err);
stream.on("data", (chunk) => out.push(chunk.toString()));
stream.on("end", () => res(out.join("")));
});
const data = await listFiles(id);
return {
success: true,
data: data.split("\n").filter(Boolean),
Expand Down

0 comments on commit 4f0eaa1

Please sign in to comment.