start ws server file fetching logic

This commit is contained in:
Ishaan Dey
2024-04-26 02:10:37 -04:00
parent a49de2294d
commit 4e7d6d1a97
6 changed files with 164 additions and 39 deletions

View File

@ -0,0 +1,56 @@
import { R2Files, Sandbox, TFile, TFolder, User } from "./types"
const getSandboxFiles = async (id: string) => {
const sandboxRes = await fetch(
`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`
)
const sandboxData: R2Files = await sandboxRes.json()
const paths = sandboxData.objects.map((obj) => obj.key)
return processFiles(paths, id)
}
const processFiles = (paths: string[], id: string): (TFile | TFolder)[] => {
const root: TFolder = { id: "/", type: "folder", name: "/", children: [] }
paths.forEach((path) => {
const allParts = path.split("/")
if (allParts[1] !== id) {
console.log("invalid path!!!!")
return
}
const parts = allParts.slice(2)
let current: TFolder = root
for (let i = 0; i < parts.length; i++) {
const part = parts[i]
const isFile = i === parts.length - 1 && part.includes(".")
const existing = current.children.find((child) => child.name === part)
if (existing) {
if (!isFile) {
current = existing as TFolder
}
} else {
if (isFile) {
const file: TFile = { id: path, type: "file", name: part }
current.children.push(file)
} else {
const folder: TFolder = {
id: path,
type: "folder",
name: part,
children: [],
}
current.children.push(folder)
current = folder
}
}
}
})
return root.children
}
export default getSandboxFiles

View File

@ -5,6 +5,7 @@ import { Server } from "socket.io"
import { z } from "zod"
import { User } from "./types"
import getSandboxFiles from "./getSandboxFiles"
dotenv.config()
@ -39,11 +40,9 @@ io.use(async (socket, next) => {
return
}
const query = parseQuery.data
const { sandboxId, userId, type } = parseQuery.data
const dbUser = await fetch(
`http://localhost:8787/api/user?id=${query.userId}`
)
const dbUser = await fetch(`http://localhost:8787/api/user?id=${userId}`)
const dbUserJSON = (await dbUser.json()) as User
console.log("dbUserJSON:", dbUserJSON)
@ -54,7 +53,7 @@ io.use(async (socket, next) => {
return
}
const sandbox = dbUserJSON.sandbox.find((s) => s.id === query.sandboxId)
const sandbox = dbUserJSON.sandbox.find((s) => s.id === sandboxId)
if (!sandbox) {
console.log("Invalid credentials.")
@ -62,33 +61,25 @@ io.use(async (socket, next) => {
return
}
const data = {
userId: query.userId,
sandboxId: query.sandboxId,
type: query.type,
init: sandbox.init,
socket.data = {
id: sandboxId,
type,
userId,
}
socket.data = data
next()
})
io.on("connection", async (socket) => {
const data = socket.data as {
userId: string
sandboxId: string
id: string
type: "node" | "react"
init: boolean
}
console.log("init:", data.init)
const sandboxFiles = await getSandboxFiles(data.id)
if (!data.init) {
// const dbUser = await fetch(
// `http://localhost:8787/sandbox/${data.sandboxId}/init`
// )
}
// fetch all file data TODO
// socket.emit("loaded", {
// rootContent: await fetchDir("/workspace", "")

View File

@ -11,7 +11,35 @@ export type Sandbox = {
id: string
name: string
type: "react" | "node"
init: boolean
bucket: string | null
userId: string
}
export type TFolder = {
id: string
type: "folder"
name: string
children: (TFile | TFolder)[]
}
export type TFile = {
id: string
type: "file"
name: string
}
export type R2Files = {
objects: R2FileData[]
truncated: boolean
delimitedPrefixes: any[]
}
export type R2FileData = {
storageClass: string
uploaded: string
checksums: any
httpEtag: string
etag: string
size: number
version: string
key: string
}