|
| 1 | +import { exec } from 'node:child_process' |
| 2 | +import * as fs from 'node:fs/promises' |
| 3 | +import path from 'node:path' |
| 4 | +import { serve } from '@hono/node-server' |
| 5 | +import { zValidator } from '@hono/zod-validator' |
| 6 | +import { Hono } from 'hono' |
| 7 | +import { streamText } from 'hono/streaming' |
| 8 | +import mime from 'mime' |
| 9 | + |
| 10 | +import { ExecParams, FileList, FilesWrite } from '../shared/schema.ts' |
| 11 | + |
| 12 | +process.chdir('workdir') |
| 13 | + |
| 14 | +const app = new Hono() |
| 15 | + |
| 16 | +app.get('/ping', (c) => c.text('pong!')) |
| 17 | + |
| 18 | +/** |
| 19 | + * GET /files/ls |
| 20 | + * |
| 21 | + * Gets all files in a directory |
| 22 | + */ |
| 23 | +app.get('/files/ls', async (c) => { |
| 24 | + const directoriesToRead = ['.'] |
| 25 | + const files: FileList = { resources: [] } |
| 26 | + |
| 27 | + while (directoriesToRead.length > 0) { |
| 28 | + const curr = directoriesToRead.pop() |
| 29 | + if (!curr) { |
| 30 | + throw new Error('Popped empty stack, error while listing directories') |
| 31 | + } |
| 32 | + const fullPath = path.join(process.cwd(), curr) |
| 33 | + const dir = await fs.readdir(fullPath, { withFileTypes: true }) |
| 34 | + for (const dirent of dir) { |
| 35 | + const relPath = path.relative(process.cwd(), `${fullPath}/${dirent.name}`) |
| 36 | + if (dirent.isDirectory()) { |
| 37 | + directoriesToRead.push(dirent.name) |
| 38 | + files.resources.push({ |
| 39 | + uri: `file:///${relPath}`, |
| 40 | + name: dirent.name, |
| 41 | + mimeType: 'inode/directory', |
| 42 | + }) |
| 43 | + } else { |
| 44 | + const mimeType = mime.getType(dirent.name) |
| 45 | + files.resources.push({ |
| 46 | + uri: `file:///${relPath}`, |
| 47 | + name: dirent.name, |
| 48 | + mimeType: mimeType ?? undefined, |
| 49 | + }) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return c.json(files) |
| 55 | +}) |
| 56 | + |
| 57 | +/** |
| 58 | + * GET /files/contents/{filepath} |
| 59 | + * |
| 60 | + * Get the contents of a file or directory |
| 61 | + */ |
| 62 | +app.get('/files/contents/*', async (c) => { |
| 63 | + let reqPath = c.req.path.replace('/files/contents', '') |
| 64 | + reqPath = reqPath.endsWith('/') ? reqPath.substring(0, reqPath.length - 1) : reqPath |
| 65 | + try { |
| 66 | + const mimeType = mime.getType(reqPath) |
| 67 | + const headers = mimeType ? { 'Content-Type': mimeType } : undefined |
| 68 | + const contents = await fs.readFile(path.join(process.cwd(), reqPath)) |
| 69 | + return c.newResponse(contents, 200, headers) |
| 70 | + } catch (e: any) { |
| 71 | + if (e.code) { |
| 72 | + // handle directory |
| 73 | + if (e.code === 'EISDIR') { |
| 74 | + const files: string[] = [] |
| 75 | + const dir = await fs.readdir(path.join(process.cwd(), reqPath), { |
| 76 | + withFileTypes: true, |
| 77 | + }) |
| 78 | + for (const dirent of dir) { |
| 79 | + const relPath = path.relative(process.cwd(), `${reqPath}/${dirent.name}`) |
| 80 | + if (dirent.isDirectory()) { |
| 81 | + files.push(`file:///${relPath}`) |
| 82 | + } else { |
| 83 | + const mimeType = mime.getType(dirent.name) |
| 84 | + files.push(`file:///${relPath}`) |
| 85 | + } |
| 86 | + } |
| 87 | + return c.newResponse(files.join('\n'), 200, { |
| 88 | + 'Content-Type': 'inode/directory', |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + if (e.code === 'ENOENT') { |
| 93 | + return c.notFound() |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + throw e |
| 98 | + } |
| 99 | +}) |
| 100 | + |
| 101 | +/** |
| 102 | + * POST /files/contents |
| 103 | + * |
| 104 | + * Create or update file contents |
| 105 | + */ |
| 106 | +app.post('/files/contents', zValidator('json', FilesWrite), async (c) => { |
| 107 | + const file = c.req.valid('json') |
| 108 | + const reqPath = file.path.endsWith('/') ? file.path.substring(0, file.path.length - 1) : file.path |
| 109 | + try { |
| 110 | + await fs.writeFile(reqPath, file.text) |
| 111 | + return c.newResponse(null, 200) |
| 112 | + } catch (e) { |
| 113 | + return c.newResponse(`Error: ${e}`, 400) |
| 114 | + } |
| 115 | +}) |
| 116 | + |
| 117 | +/** |
| 118 | + * POST /exec |
| 119 | + * |
| 120 | + * Execute a command in a shell |
| 121 | + */ |
| 122 | +app.post('/exec', zValidator('json', ExecParams), (c) => { |
| 123 | + const execParams = c.req.valid('json') |
| 124 | + const proc = exec(execParams.args) |
| 125 | + return streamText(c, async (stream) => { |
| 126 | + return new Promise(async (resolve, reject) => { |
| 127 | + if (proc.stdout) { |
| 128 | + // Stream data from stdout |
| 129 | + proc.stdout.on('data', async (data) => { |
| 130 | + await stream.write(data.toString()) |
| 131 | + }) |
| 132 | + } else { |
| 133 | + await stream.write('WARNING: no stdout stream for process') |
| 134 | + } |
| 135 | + |
| 136 | + if (execParams.streamStderr) { |
| 137 | + if (proc.stderr) { |
| 138 | + proc.stderr.on('data', async (data) => { |
| 139 | + await stream.write(data.toString()) |
| 140 | + }) |
| 141 | + } else { |
| 142 | + await stream.write('WARNING: no stderr stream for process') |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Handle process exit |
| 147 | + proc.on('exit', async (code) => { |
| 148 | + await stream.write(`Process exited with code: ${code}`) |
| 149 | + if (code === 0) { |
| 150 | + stream.close() |
| 151 | + resolve() |
| 152 | + } else { |
| 153 | + console.error(`Process exited with code ${code}`) |
| 154 | + reject(new Error(`Process failed with code ${code}`)) |
| 155 | + } |
| 156 | + }) |
| 157 | + |
| 158 | + proc.on('error', (err) => { |
| 159 | + console.error('Error with process: ', err) |
| 160 | + reject(err) |
| 161 | + }) |
| 162 | + }) |
| 163 | + }) |
| 164 | +}) |
| 165 | + |
| 166 | +serve({ |
| 167 | + fetch: app.fetch, |
| 168 | + port: 8080, |
| 169 | +}) |
0 commit comments