Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/api/v2/exec.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2024 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Run code in a project.7*/89import callProject from "@cocalc/server/projects/call";10import isCollaborator from "@cocalc/server/projects/is-collaborator";11import getAccountId from "lib/account/get-account";12import getParams from "lib/api/get-params";13import { apiRoute, apiRouteOperation } from "lib/api";14import { ExecInputSchema, ExecOutputSchema } from "lib/api/schema/exec";1516async function handle(req, res) {17try {18res.json(await get(req));19} catch (err) {20res.json({ error: `${err.message}` });21return;22}23}2425async function get(req) {26const account_id = await getAccountId(req);27if (!account_id) {28throw Error("must be signed in");29}30// See ExecOpts from @cocalc/util/db-schema/projects31const {32project_id,33compute_server_id,34filesystem,35path,36command,37args,38timeout,39max_output,40bash,41aggregate,42err_on_exit,43env,44async_call,45async_get,46async_stats,47async_await,48} = getParams(req);4950if (!(await isCollaborator({ account_id, project_id }))) {51throw Error("user must be a collaborator on the project");52}5354const resp = await callProject({55account_id,56project_id,57mesg: {58event: "project_exec",59project_id,60compute_server_id,61filesystem,62path,63command,64args,65timeout,66max_output,67bash,68aggregate,69err_on_exit,70env,71async_call,72async_get,73async_stats,74async_await,75},76});77// event and id don't make sense for http post api78delete resp.event;79delete resp.id;80return resp;81}8283export default apiRoute({84exec: apiRouteOperation({85method: "POST",86openApiOperation: {87tags: ["Utils"],88},89})90.input({91contentType: "application/json",92body: ExecInputSchema,93})94.outputs([95{96status: 200,97contentType: "application/octet-stream",98body: ExecOutputSchema,99},100])101.handler(handle),102});103104105