morphbox
Version:
Docker-based AI sandbox for development with Claude integration
57 lines (54 loc) • 1.88 kB
JavaScript
import { e as error, j as json } from './index-3BbzJtgI.js';
import { promises } from 'fs';
import path from 'path';
const WORKSPACE_DIR = process.cwd();
function validatePath(requestPath) {
const normalized = path.normalize(path.join(WORKSPACE_DIR, requestPath));
if (!normalized.startsWith(WORKSPACE_DIR)) {
throw error(403, "Access denied: Path outside workspace directory");
}
return normalized;
}
const GET = async ({ url }) => {
try {
const requestPath = url.searchParams.get("path") || "/";
const fullPath = validatePath(requestPath);
const stats = await promises.stat(fullPath);
if (!stats.isDirectory()) {
throw error(400, "Path is not a directory");
}
const items = await promises.readdir(fullPath, { withFileTypes: true });
const contents = await Promise.all(
items.map(async (item) => {
const itemPath = path.join(fullPath, item.name);
const itemStats = await promises.stat(itemPath);
return {
name: item.name,
type: item.isDirectory() ? "directory" : "file",
path: path.relative(WORKSPACE_DIR, itemPath),
size: itemStats.size,
modified: itemStats.mtime.toISOString(),
created: itemStats.birthtime.toISOString()
};
})
);
return json({
path: path.relative(WORKSPACE_DIR, fullPath),
contents: contents.sort((a, b) => {
if (a.type !== b.type) {
return a.type === "directory" ? -1 : 1;
}
return a.name.localeCompare(b.name);
})
});
} catch (err) {
if (err instanceof Response) throw err;
if (err.code === "ENOENT") {
throw error(404, "Directory not found");
}
console.error("Error listing directory:", err);
throw error(500, "Failed to list directory");
}
};
export { GET };
//# sourceMappingURL=_server.ts-B7glpRbo.js.map