CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

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