Skip to content

Commit

Permalink
refactor(daemon): switch moduleres to bundler
Browse files Browse the repository at this point in the history
feat(daemon): more options for config file loading+help message
  • Loading branch information
IncognitoTGT committed Jan 11, 2025
1 parent 6e9d24d commit 1e97490
Show file tree
Hide file tree
Showing 20 changed files with 3,498 additions and 2,833 deletions.
2 changes: 1 addition & 1 deletion apps/daemon/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { App } from "~/index.ts";
export { App } from "~/index";
3 changes: 2 additions & 1 deletion apps/daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "1.0.50",
"scripts": {
"start": "./stardustd",
"build:debug": "bun build src/index.ts --compile --sourcemap --outfile stardustd",
"build": "bun build src/index.ts --compile --minify --outfile stardustd",
"dev": "bun run --watch src/index.ts",
"check-types": "tsc --noEmit",
Expand All @@ -26,5 +27,5 @@
"bun-types": "latest",
"ts-json-schema-generator": "^2.3.0"
},
"module": "src/index.js"
"module": "src/index.ts"
}
2 changes: 1 addition & 1 deletion apps/daemon/src/auth-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context } from "elysia";
import generateToken from "~/lib/auth-token.js";
import generateToken from "~/lib/auth-token";
const configuredToken = generateToken();
export function authCheck(c: Context) {
const authHeader = c.request.headers.get("Authorization");
Expand Down
8 changes: 8 additions & 0 deletions apps/daemon/src/help-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if (process.argv.includes("--help") || process.argv.includes("-h") || process.argv.includes("help")) {
console.log(`
✨ Stardust daemon by spaceness
Options
--config: path to a stardustd config file. Defaults to [executable path]/config.yml or (HOME || XDG_CONFIG_HOME)/.config/stardustd.yml
`);
process.exit(0);
}
19 changes: 11 additions & 8 deletions apps/daemon/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import "~/lib/config/validate.js";
import "./help-message";
import { getConfig, validateConfig } from "~/lib/config";
if (!validateConfig(getConfig())) {
console.error("Invalid configuration");
process.exit(1);
}
import { Elysia } from "elysia";
import { docker } from "~/lib/docker.js";
import checkDockerNetwork from "~/lib/network-check.js";
import checkSystemService from "~/lib/service-check.js";
import sessionHandler from "~/session/index.js";
import { authCheck } from "./auth-middleware.js";
import { getConfig } from "./lib/config/index.js";
import { authCheck } from "~/auth-middleware";
import { docker } from "~/lib/docker";
import checkDockerNetwork from "~/lib/network-check";
import checkSystemService from "~/lib/service-check";
import sessionHandler from "~/session";
await checkDockerNetwork();
const config = getConfig();
if (typeof config.service !== "boolean" || config.service === true) {
checkSystemService();
}

const app = new Elysia()
.get("/", async (c) => {
return {
Expand Down
6 changes: 3 additions & 3 deletions apps/daemon/src/lib/auth-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { randomBytes } from "node:crypto";
import { dump } from "js-yaml";
import { getConfig } from "./config/index.js";
import type { Config } from "./config/types.js";
import { getConfig } from "./config/index";
import type { Config } from "./config/types.d.ts";

export default function generateToken() {
const config = getConfig();
Expand All @@ -17,7 +17,7 @@ export default function generateToken() {
token,
} satisfies Config);
writer.write(`${newConfig}\n`);
writer.write("# yaml-language-server: $schema=schema.json");
writer.write("# yaml-language-server: $schema=schemaon");
writer.end();
console.log("✨ Stardust: Token generated: %s", token);
return token;
Expand Down
37 changes: 33 additions & 4 deletions apps/daemon/src/lib/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
import { parseArgs } from "node:util";
import { Ajv } from "ajv";
import { load } from "js-yaml";
import defaultSchema from "~/../schema.json";
import type { Config } from "./types.d.ts";
import type { Config } from "./types";
const {
values: { config: cmdConfig = "" },
} = parseArgs({
args: Bun.argv,
options: {
config: {
type: "string",
},
},
strict: true,
allowPositionals: true,
});
let loadedConfig: unknown;
try {
loadedConfig = load((await Bun.file(`${process.cwd()}/config.yml`).text()) || "");
} catch {
console.log("Invalid or no config");
for (const path of [
cmdConfig,
`${process.cwd()}/config.yaml`,
`${process.cwd()}/config.yml`,
`${Bun.env.XDG_CONFIG_HOME || Bun.env.HOME}/.config/stardustd.yaml`,
`${Bun.env.XDG_CONFIG_HOME || Bun.env.HOME}/.config/stardustd.yml`,
]) {
const file = Bun.file(path);
if (await file.exists()) {
loadedConfig = load(await file.text());
console.log("✨ Stardust: Loaded config from %s", path);
break;
}
}
if (!loadedConfig) {
throw new Error("Config file not found");
}
} catch (e) {
console.error("✨ Stardust: Invalid or no config", e);
process.exit(1);
}
export function getConfig<T = Config>(): T {
Expand Down
5 changes: 0 additions & 5 deletions apps/daemon/src/lib/config/validate.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/daemon/src/lib/docker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Dockerode from "dockerode";
import { getConfig } from "~/lib/config/index.js";
import { getConfig } from "~/lib/config";
const { docker: config } = getConfig();
const socketConfig: Dockerode.DockerOptions = { socketPath: config.socket || "/var/run/docker.sock" };
const httpConfig: Dockerode.DockerOptions = {
Expand Down
4 changes: 2 additions & 2 deletions apps/daemon/src/lib/network-check.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getConfig } from "~/lib/config/index.js";
import { docker } from "~/lib/docker.js";
import { getConfig } from "~/lib/config";
import { docker } from "~/lib/docker";
const { docker: config } = getConfig();

export default async function checkDockerNetwork() {
Expand Down
4 changes: 2 additions & 2 deletions apps/daemon/src/session/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomBytes } from "node:crypto";
import { getConfig } from "~/lib/config/index.js";
import { docker } from "~/lib/docker.js";
import { getConfig } from "~/lib/config";
import { docker } from "~/lib/docker";

export default async function createSession({
workspace,
Expand Down
2 changes: 1 addition & 1 deletion apps/daemon/src/session/delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { docker } from "~/lib/docker.js";
import { docker } from "~/lib/docker";

export default async function deleteSession(id: string) {
await docker.getContainer(id).remove({ force: true });
Expand Down
2 changes: 1 addition & 1 deletion apps/daemon/src/session/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { docker } from "~/lib/docker.js";
import { docker } from "~/lib/docker";

export async function sendFile(id: string, name: string, file: Uint8Array) {
const container = docker.getContainer(id);
Expand Down
14 changes: 7 additions & 7 deletions apps/daemon/src/session/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Elysia, t } from "elysia";
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, listFiles, sendFile } from "./file.js";
import manageSession from "./manage.js";
import screenshot from "./screenshot.js";
import { getConfig } from "~/lib/config";
import { docker } from "~/lib/docker";
import createSession from "./create";
import deleteSession from "./delete";
import { getFile, listFiles, sendFile } from "./file";
import manageSession from "./manage";
import screenshot from "./screenshot";
export default new Elysia({ prefix: "/sessions" })
.put(
"/create",
Expand Down
2 changes: 1 addition & 1 deletion apps/daemon/src/session/manage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Dockerode from "dockerode";
import { docker } from "~/lib/docker.js";
import { docker } from "~/lib/docker";
export default async function manageSession(id: string, action: keyof Dockerode.Container) {
const container = docker.getContainer(id);
await container[action]();
Expand Down
2 changes: 1 addition & 1 deletion apps/daemon/src/session/screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { docker } from "~/lib/docker.js";
import { docker } from "~/lib/docker";
export default async function screenshot(id: string) {
const container = docker.getContainer(id);
const exec = await container.exec({
Expand Down
3 changes: 1 addition & 2 deletions apps/daemon/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"extends": "@stardust/tsconfig/base.json",
"compilerOptions": {
"moduleResolution": "nodenext",
"module": "NodeNext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"paths": {
"~/*": ["./src/*"]
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/(main)/sessions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ManageSessionButton = ({
action={async () => {
"use server";
const node = getNode(session);
await node.sessions({ id: session.id }).patch({ action });
await node.sessions(session).patch({ action });
if (redirectToView) redirect(`/view/${session.id}`);
else revalidatePath("/sessions");
}}
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/lib/session/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function createSession(workspace: string) {
})
.returning()
.catch(async (e) => {
await node.sessions({ ...container }).delete();
await node.sessions(container).delete();
throw e;
});
}
Loading

0 comments on commit 1e97490

Please sign in to comment.