chore: format backend server code

This commit is contained in:
James Murdza 2024-10-19 05:25:26 -06:00
parent 1416c225a2
commit ad9457b157
11 changed files with 765 additions and 669 deletions

View File

@ -1,5 +1,7 @@
{ {
"watch": ["src"], "watch": [
"src"
],
"ext": "ts", "ext": "ts",
"exec": "concurrently \"npx tsc --watch\" \"ts-node src/index.ts\"" "exec": "concurrently \"npx tsc --watch\" \"ts-node src/index.ts\""
} }

View File

@ -1,37 +1,33 @@
import { SSHSocketClient, SSHConfig } from "./SSHSocketClient" import { SSHConfig, SSHSocketClient } from "./SSHSocketClient"
export interface DokkuResponse { export interface DokkuResponse {
ok: boolean; ok: boolean
output: string; output: string
} }
export class DokkuClient extends SSHSocketClient { export class DokkuClient extends SSHSocketClient {
constructor(config: SSHConfig) { constructor(config: SSHConfig) {
super( super(config, "/var/run/dokku-daemon/dokku-daemon.sock")
config,
"/var/run/dokku-daemon/dokku-daemon.sock"
)
} }
async sendCommand(command: string): Promise<DokkuResponse> { async sendCommand(command: string): Promise<DokkuResponse> {
try { try {
const response = await this.sendData(command); const response = await this.sendData(command)
if (typeof response !== "string") { if (typeof response !== "string") {
throw new Error("Received data is not a string"); throw new Error("Received data is not a string")
} }
return JSON.parse(response); return JSON.parse(response)
} catch (error: any) { } catch (error: any) {
throw new Error(`Failed to send command: ${error.message}`); throw new Error(`Failed to send command: ${error.message}`)
} }
} }
async listApps(): Promise<string[]> { async listApps(): Promise<string[]> {
const response = await this.sendCommand("apps:list"); const response = await this.sendCommand("apps:list")
return response.output.split("\n").slice(1); // Split by newline and ignore the first line (header) return response.output.split("\n").slice(1) // Split by newline and ignore the first line (header)
} }
} }
export { SSHConfig }; export { SSHConfig }

View File

@ -1,72 +1,72 @@
import { Client } from "ssh2"; import { Client } from "ssh2"
export interface SSHConfig { export interface SSHConfig {
host: string; host: string
port?: number; port?: number
username: string; username: string
privateKey: Buffer; privateKey: Buffer
} }
export class SSHSocketClient { export class SSHSocketClient {
private conn: Client; private conn: Client
private config: SSHConfig; private config: SSHConfig
private socketPath: string; private socketPath: string
private isConnected: boolean = false; private isConnected: boolean = false
constructor(config: SSHConfig, socketPath: string) { constructor(config: SSHConfig, socketPath: string) {
this.conn = new Client(); this.conn = new Client()
this.config = { ...config, port: 22}; this.config = { ...config, port: 22 }
this.socketPath = socketPath; this.socketPath = socketPath
this.setupTerminationHandlers(); this.setupTerminationHandlers()
} }
private setupTerminationHandlers() { private setupTerminationHandlers() {
process.on("SIGINT", this.closeConnection.bind(this)); process.on("SIGINT", this.closeConnection.bind(this))
process.on("SIGTERM", this.closeConnection.bind(this)); process.on("SIGTERM", this.closeConnection.bind(this))
} }
private closeConnection() { private closeConnection() {
console.log("Closing SSH connection..."); console.log("Closing SSH connection...")
this.conn.end(); this.conn.end()
this.isConnected = false; this.isConnected = false
process.exit(0); process.exit(0)
} }
connect(): Promise<void> { connect(): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.conn this.conn
.on("ready", () => { .on("ready", () => {
console.log("SSH connection established"); console.log("SSH connection established")
this.isConnected = true; this.isConnected = true
resolve(); resolve()
}) })
.on("error", (err) => { .on("error", (err) => {
console.error("SSH connection error:", err); console.error("SSH connection error:", err)
this.isConnected = false; this.isConnected = false
reject(err); reject(err)
}) })
.on("close", () => { .on("close", () => {
console.log("SSH connection closed"); console.log("SSH connection closed")
this.isConnected = false; this.isConnected = false
})
.connect(this.config)
}) })
.connect(this.config);
});
} }
sendData(data: string): Promise<string> { sendData(data: string): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this.isConnected) { if (!this.isConnected) {
reject(new Error("SSH connection is not established")); reject(new Error("SSH connection is not established"))
return; return
} }
this.conn.exec( this.conn.exec(
`echo "${data}" | nc -U ${this.socketPath}`, `echo "${data}" | nc -U ${this.socketPath}`,
(err, stream) => { (err, stream) => {
if (err) { if (err) {
reject(err); reject(err)
return; return
} }
stream stream
@ -75,16 +75,16 @@ export class SSHSocketClient {
new Error( new Error(
`Stream closed with code ${code} and signal ${signal}` `Stream closed with code ${code} and signal ${signal}`
) )
); )
}) })
.on("data", (data: Buffer) => { .on("data", (data: Buffer) => {
resolve(data.toString()); resolve(data.toString())
}) })
.stderr.on("data", (data: Buffer) => { .stderr.on("data", (data: Buffer) => {
reject(new Error(data.toString())); reject(new Error(data.toString()))
}); })
} }
); )
}); })
} }
} }

View File

@ -1,82 +1,84 @@
import simpleGit, { SimpleGit } from "simple-git"; import fs from "fs"
import path from "path"; import os from "os"
import fs from "fs"; import path from "path"
import os from "os"; import simpleGit, { SimpleGit } from "simple-git"
export type FileData = { export type FileData = {
id: string; id: string
data: string; data: string
}; }
export class SecureGitClient { export class SecureGitClient {
private gitUrl: string; private gitUrl: string
private sshKeyPath: string; private sshKeyPath: string
constructor(gitUrl: string, sshKeyPath: string) { constructor(gitUrl: string, sshKeyPath: string) {
this.gitUrl = gitUrl; this.gitUrl = gitUrl
this.sshKeyPath = sshKeyPath; this.sshKeyPath = sshKeyPath
} }
async pushFiles(fileData: FileData[], repository: string): Promise<void> { async pushFiles(fileData: FileData[], repository: string): Promise<void> {
let tempDir: string | undefined; let tempDir: string | undefined
try { try {
// Create a temporary directory // Create a temporary directory
tempDir = fs.mkdtempSync(path.posix.join(os.tmpdir(), 'git-push-')); tempDir = fs.mkdtempSync(path.posix.join(os.tmpdir(), "git-push-"))
console.log(`Temporary directory created: ${tempDir}`); console.log(`Temporary directory created: ${tempDir}`)
// Write files to the temporary directory // Write files to the temporary directory
console.log(`Writing ${fileData.length} files.`); console.log(`Writing ${fileData.length} files.`)
for (const { id, data } of fileData) { for (const { id, data } of fileData) {
const filePath = path.posix.join(tempDir, id); const filePath = path.posix.join(tempDir, id)
const dirPath = path.dirname(filePath); const dirPath = path.dirname(filePath)
if (!fs.existsSync(dirPath)) { if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true }); fs.mkdirSync(dirPath, { recursive: true })
} }
fs.writeFileSync(filePath, data); fs.writeFileSync(filePath, data)
} }
// Initialize the simple-git instance with the temporary directory and custom SSH command // Initialize the simple-git instance with the temporary directory and custom SSH command
const git: SimpleGit = simpleGit(tempDir, { const git: SimpleGit = simpleGit(tempDir, {
config: [ config: [
'core.sshCommand=ssh -i ' + this.sshKeyPath + ' -o IdentitiesOnly=yes' "core.sshCommand=ssh -i " +
] this.sshKeyPath +
" -o IdentitiesOnly=yes",
],
}).outputHandler((_command, stdout, stderr) => { }).outputHandler((_command, stdout, stderr) => {
stdout.pipe(process.stdout); stdout.pipe(process.stdout)
stderr.pipe(process.stderr); stderr.pipe(process.stderr)
});; })
// Initialize a new Git repository // Initialize a new Git repository
await git.init(); await git.init()
// Add remote repository // Add remote repository
await git.addRemote("origin", `${this.gitUrl}:${repository}`); await git.addRemote("origin", `${this.gitUrl}:${repository}`)
// Add files to the repository // Add files to the repository
for (const { id, data } of fileData) { for (const { id, data } of fileData) {
await git.add(id); await git.add(id)
} }
// Commit the changes // Commit the changes
await git.commit("Add files."); await git.commit("Add files.")
// Push the changes to the remote repository // Push the changes to the remote repository
await git.push("origin", "master", {'--force': null}); await git.push("origin", "master", { "--force": null })
console.log("Files successfully pushed to the repository"); console.log("Files successfully pushed to the repository")
if (tempDir) { if (tempDir) {
fs.rmSync(tempDir, { recursive: true, force: true }); fs.rmSync(tempDir, { recursive: true, force: true })
console.log(`Temporary directory removed: ${tempDir}`); console.log(`Temporary directory removed: ${tempDir}`)
} }
} catch (error) { } catch (error) {
if (tempDir) { if (tempDir) {
fs.rmSync(tempDir, { recursive: true, force: true }); fs.rmSync(tempDir, { recursive: true, force: true })
console.log(`Temporary directory removed: ${tempDir}`); console.log(`Temporary directory removed: ${tempDir}`)
} }
console.error("Error pushing files to the repository:", error); console.error("Error pushing files to the repository:", error)
throw error; throw error
} }
} }
} }

View File

@ -1,13 +1,13 @@
import { Sandbox, ProcessHandle } from "e2b"; import { ProcessHandle, Sandbox } from "e2b"
// Terminal class to manage a pseudo-terminal (PTY) in a sandbox environment // Terminal class to manage a pseudo-terminal (PTY) in a sandbox environment
export class Terminal { export class Terminal {
private pty: ProcessHandle | undefined; // Holds the PTY process handle private pty: ProcessHandle | undefined // Holds the PTY process handle
private sandbox: Sandbox; // Reference to the sandbox environment private sandbox: Sandbox // Reference to the sandbox environment
// Constructor initializes the Terminal with a sandbox // Constructor initializes the Terminal with a sandbox
constructor(sandbox: Sandbox) { constructor(sandbox: Sandbox) {
this.sandbox = sandbox; this.sandbox = sandbox
} }
// Initialize the terminal with specified rows, columns, and data handler // Initialize the terminal with specified rows, columns, and data handler
@ -16,9 +16,9 @@ export class Terminal {
cols = 80, cols = 80,
onData, onData,
}: { }: {
rows?: number; rows?: number
cols?: number; cols?: number
onData: (responseData: string) => void; onData: (responseData: string) => void
}): Promise<void> { }): Promise<void> {
// Create a new PTY process // Create a new PTY process
this.pty = await this.sandbox.pty.create({ this.pty = await this.sandbox.pty.create({
@ -26,35 +26,38 @@ export class Terminal {
cols, cols,
timeout: 0, timeout: 0,
onData: (data: Uint8Array) => { onData: (data: Uint8Array) => {
onData(new TextDecoder().decode(data)); // Convert received data to string and pass to handler onData(new TextDecoder().decode(data)) // Convert received data to string and pass to handler
}, },
}); })
} }
// Send data to the terminal // Send data to the terminal
async sendData(data: string) { async sendData(data: string) {
if (this.pty) { if (this.pty) {
await this.sandbox.pty.sendInput(this.pty.pid, new TextEncoder().encode(data)); await this.sandbox.pty.sendInput(
this.pty.pid,
new TextEncoder().encode(data)
)
} else { } else {
console.log("Cannot send data because pty is not initialized."); console.log("Cannot send data because pty is not initialized.")
} }
} }
// Resize the terminal // Resize the terminal
async resize(size: { cols: number; rows: number }): Promise<void> { async resize(size: { cols: number; rows: number }): Promise<void> {
if (this.pty) { if (this.pty) {
await this.sandbox.pty.resize(this.pty.pid, size); await this.sandbox.pty.resize(this.pty.pid, size)
} else { } else {
console.log("Cannot resize terminal because pty is not initialized."); console.log("Cannot resize terminal because pty is not initialized.")
} }
} }
// Close the terminal, killing the PTY process and stopping the input stream // Close the terminal, killing the PTY process and stopping the input stream
async close(): Promise<void> { async close(): Promise<void> {
if (this.pty) { if (this.pty) {
await this.pty.kill(); await this.pty.kill()
} else { } else {
console.log("Cannot kill pty because it is not initialized."); console.log("Cannot kill pty because it is not initialized.")
} }
} }
} }

View File

@ -1,14 +1,7 @@
import * as dotenv from "dotenv"; import * as dotenv from "dotenv"
import { import { R2Files, TFile, TFileData, TFolder } from "./types"
R2FileBody,
R2Files,
Sandbox,
TFile,
TFileData,
TFolder,
} from "./types";
dotenv.config(); dotenv.config()
export const getSandboxFiles = async (id: string) => { export const getSandboxFiles = async (id: string) => {
const res = await fetch( const res = await fetch(
@ -18,13 +11,13 @@ export const getSandboxFiles = async (id: string) => {
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
} }
); )
const data: R2Files = await res.json(); const data: R2Files = await res.json()
const paths = data.objects.map((obj) => obj.key); const paths = data.objects.map((obj) => obj.key)
const processedFiles = await processFiles(paths, id); const processedFiles = await processFiles(paths, id)
return processedFiles; return processedFiles
}; }
export const getFolder = async (folderId: string) => { export const getFolder = async (folderId: string) => {
const res = await fetch( const res = await fetch(
@ -34,39 +27,39 @@ export const getFolder = async (folderId: string) => {
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
} }
); )
const data: R2Files = await res.json(); const data: R2Files = await res.json()
return data.objects.map((obj) => obj.key); return data.objects.map((obj) => obj.key)
};
const processFiles = async (paths: string[], id: string) => {
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] };
const fileData: TFileData[] = [];
paths.forEach((path) => {
const allParts = path.split("/");
if (allParts[1] !== id) {
return;
} }
const parts = allParts.slice(2); const processFiles = async (paths: string[], id: string) => {
let current: TFolder = root; const root: TFolder = { id: "/", type: "folder", name: "/", children: [] }
const fileData: TFileData[] = []
paths.forEach((path) => {
const allParts = path.split("/")
if (allParts[1] !== id) {
return
}
const parts = allParts.slice(2)
let current: TFolder = root
for (let i = 0; i < parts.length; i++) { for (let i = 0; i < parts.length; i++) {
const part = parts[i]; const part = parts[i]
const isFile = i === parts.length - 1 && part.length; const isFile = i === parts.length - 1 && part.length
const existing = current.children.find((child) => child.name === part); const existing = current.children.find((child) => child.name === part)
if (existing) { if (existing) {
if (!isFile) { if (!isFile) {
current = existing as TFolder; current = existing as TFolder
} }
} else { } else {
if (isFile) { if (isFile) {
const file: TFile = { id: path, type: "file", name: part }; const file: TFile = { id: path, type: "file", name: part }
current.children.push(file); current.children.push(file)
fileData.push({ id: path, data: "" }); fileData.push({ id: path, data: "" })
} else { } else {
const folder: TFolder = { const folder: TFolder = {
// id: path, // todo: wrong id. for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css // id: path, // todo: wrong id. for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
@ -74,26 +67,26 @@ const processFiles = async (paths: string[], id: string) => {
type: "folder", type: "folder",
name: part, name: part,
children: [], children: [],
}; }
current.children.push(folder); current.children.push(folder)
current = folder; current = folder
} }
} }
} }
}); })
await Promise.all( await Promise.all(
fileData.map(async (file) => { fileData.map(async (file) => {
const data = await fetchFileContent(file.id); const data = await fetchFileContent(file.id)
file.data = data; file.data = data
}) })
); )
return { return {
files: root.children, files: root.children,
fileData, fileData,
}; }
}; }
const fetchFileContent = async (fileId: string): Promise<string> => { const fetchFileContent = async (fileId: string): Promise<string> => {
try { try {
@ -104,13 +97,13 @@ const fetchFileContent = async (fileId: string): Promise<string> => {
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
} }
); )
return await fileRes.text(); return await fileRes.text()
} catch (error) { } catch (error) {
console.error("ERROR fetching file:", error); console.error("ERROR fetching file:", error)
return ""; return ""
}
} }
};
export const createFile = async (fileId: string) => { export const createFile = async (fileId: string) => {
const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api`, { const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api`, {
@ -120,9 +113,9 @@ export const createFile = async (fileId: string) => {
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
body: JSON.stringify({ fileId }), body: JSON.stringify({ fileId }),
}); })
return res.ok; return res.ok
}; }
export const renameFile = async ( export const renameFile = async (
fileId: string, fileId: string,
@ -136,9 +129,9 @@ export const renameFile = async (
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
body: JSON.stringify({ fileId, newFileId, data }), body: JSON.stringify({ fileId, newFileId, data }),
}); })
return res.ok; return res.ok
}; }
export const saveFile = async (fileId: string, data: string) => { export const saveFile = async (fileId: string, data: string) => {
const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api/save`, { const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api/save`, {
@ -148,9 +141,9 @@ export const saveFile = async (fileId: string, data: string) => {
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
body: JSON.stringify({ fileId, data }), body: JSON.stringify({ fileId, data }),
}); })
return res.ok; return res.ok
}; }
export const deleteFile = async (fileId: string) => { export const deleteFile = async (fileId: string) => {
const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api`, { const res = await fetch(`${process.env.STORAGE_WORKER_URL}/api`, {
@ -160,9 +153,9 @@ export const deleteFile = async (fileId: string) => {
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
body: JSON.stringify({ fileId }), body: JSON.stringify({ fileId }),
}); })
return res.ok; return res.ok
}; }
export const getProjectSize = async (id: string) => { export const getProjectSize = async (id: string) => {
const res = await fetch( const res = await fetch(
@ -172,6 +165,6 @@ export const getProjectSize = async (id: string) => {
Authorization: `${process.env.WORKERS_KEY}`, Authorization: `${process.env.WORKERS_KEY}`,
}, },
} }
); )
return (await res.json()).size; return (await res.json()).size
}; }

File diff suppressed because it is too large Load Diff

View File

@ -1,70 +1,70 @@
// DB Types // DB Types
export type User = { export type User = {
id: string; id: string
name: string; name: string
email: string; email: string
generations: number; generations: number
sandbox: Sandbox[]; sandbox: Sandbox[]
usersToSandboxes: UsersToSandboxes[]; usersToSandboxes: UsersToSandboxes[]
}; }
export type Sandbox = { export type Sandbox = {
id: string; id: string
name: string; name: string
type: "react" | "node"; type: "react" | "node"
visibility: "public" | "private"; visibility: "public" | "private"
createdAt: Date; createdAt: Date
userId: string; userId: string
usersToSandboxes: UsersToSandboxes[]; usersToSandboxes: UsersToSandboxes[]
}; }
export type UsersToSandboxes = { export type UsersToSandboxes = {
userId: string; userId: string
sandboxId: string; sandboxId: string
sharedOn: Date; sharedOn: Date
}; }
export type TFolder = { export type TFolder = {
id: string; id: string
type: "folder"; type: "folder"
name: string; name: string
children: (TFile | TFolder)[]; children: (TFile | TFolder)[]
}; }
export type TFile = { export type TFile = {
id: string; id: string
type: "file"; type: "file"
name: string; name: string
}; }
export type TFileData = { export type TFileData = {
id: string; id: string
data: string; data: string
}; }
export type R2Files = { export type R2Files = {
objects: R2FileData[]; objects: R2FileData[]
truncated: boolean; truncated: boolean
delimitedPrefixes: any[]; delimitedPrefixes: any[]
}; }
export type R2FileData = { export type R2FileData = {
storageClass: string; storageClass: string
uploaded: string; uploaded: string
checksums: any; checksums: any
httpEtag: string; httpEtag: string
etag: string; etag: string
size: number; size: number
version: string; version: string
key: string; key: string
}; }
export type R2FileBody = R2FileData & { export type R2FileBody = R2FileData & {
body: ReadableStream; body: ReadableStream
bodyUsed: boolean; bodyUsed: boolean
arrayBuffer: Promise<ArrayBuffer>; arrayBuffer: Promise<ArrayBuffer>
text: Promise<string>; text: Promise<string>
json: Promise<any>; json: Promise<any>
blob: Promise<Blob>; blob: Promise<Blob>
}; }

View File

@ -1,23 +1,23 @@
export class LockManager { export class LockManager {
private locks: { [key: string]: Promise<any> }; private locks: { [key: string]: Promise<any> }
constructor() { constructor() {
this.locks = {}; this.locks = {}
} }
async acquireLock<T>(key: string, task: () => Promise<T>): Promise<T> { async acquireLock<T>(key: string, task: () => Promise<T>): Promise<T> {
if (!this.locks[key]) { if (!this.locks[key]) {
this.locks[key] = new Promise<T>(async (resolve, reject) => { this.locks[key] = new Promise<T>(async (resolve, reject) => {
try { try {
const result = await task(); const result = await task()
resolve(result); resolve(result)
} catch (error) { } catch (error) {
reject(error); reject(error)
} finally { } finally {
delete this.locks[key]; delete this.locks[key]
} }
}); })
} }
return await this.locks[key]; return await this.locks[key]
} }
} }