mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 19:34:34 +00:00
71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
import express from 'express'
|
|
import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
|
|
import { ExecutionController } from './internal'
|
|
import {
|
|
getPreProgramVariables,
|
|
getUserAutoExec,
|
|
ModeType,
|
|
parseLogToArray,
|
|
RunTimeType
|
|
} from '../utils'
|
|
|
|
interface ExecuteCodePayload {
|
|
/**
|
|
* Code of program
|
|
* @example "* Code HERE;"
|
|
*/
|
|
code: string
|
|
/**
|
|
* runtime for program
|
|
* @example "js"
|
|
*/
|
|
runTime: RunTimeType
|
|
}
|
|
|
|
@Security('bearerAuth')
|
|
@Route('SASjsApi/code')
|
|
@Tags('Code')
|
|
export class CodeController {
|
|
/**
|
|
* Execute Code on the Specified Runtime
|
|
* @summary Run Code and Return Webout Content and Log
|
|
*/
|
|
@Post('/execute')
|
|
public async executeCode(
|
|
@Request() request: express.Request,
|
|
@Body() body: ExecuteCodePayload
|
|
): Promise<string | Buffer> {
|
|
return executeCode(request, body)
|
|
}
|
|
}
|
|
|
|
const executeCode = async (
|
|
req: express.Request,
|
|
{ code, runTime }: ExecuteCodePayload
|
|
) => {
|
|
const { user } = req
|
|
const userAutoExec =
|
|
process.env.MODE === ModeType.Server
|
|
? user?.autoExec
|
|
: await getUserAutoExec()
|
|
|
|
try {
|
|
const { result } = await new ExecutionController().executeProgram({
|
|
program: code,
|
|
preProgramVariables: getPreProgramVariables(req),
|
|
vars: { ...req.query, _debug: 131 },
|
|
otherArgs: { userAutoExec },
|
|
runTime: runTime
|
|
})
|
|
|
|
return result
|
|
} catch (err: any) {
|
|
throw {
|
|
code: 400,
|
|
status: 'failure',
|
|
message: 'Job execution failed.',
|
|
error: typeof err === 'object' ? err.toString() : err
|
|
}
|
|
}
|
|
}
|