1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-10 19:34:34 +00:00

fix: code fixes for executing program from program path including file extension

This commit is contained in:
2022-06-15 16:18:07 +05:00
parent 81501d17ab
commit 53854d0012
4 changed files with 45 additions and 14 deletions

View File

@@ -6,6 +6,8 @@ PROTOCOL=[http|https] default considered as http
PRIVATE_KEY=privkey.pem
FULL_CHAIN=fullchain.pem
RUN_TIMES=[sas|js|sas,js|js,sas] default considered as sas
PORT=[5000] default value is 5000
HELMET_CSP_CONFIG_PATH=./csp.config.json if omitted HELMET default will be used

View File

@@ -32,7 +32,17 @@ export class FileUploadController {
const programPath = (query?._program ?? body?._program) as string
const { runTime } = await getRunTimeAndFilePath(programPath)
let runTime
try {
;({ runTime } = await getRunTimeAndFilePath(programPath))
} catch (err: any) {
res.status(400).send({
status: 'failure',
message: 'Job execution failed',
error: typeof err === 'object' ? err.toString() : err
})
}
const sessionController =
runTime === RunTimeType.SAS

View File

@@ -131,9 +131,9 @@ const executeReturnRaw = async (
): Promise<string | Buffer> => {
const query = req.query as ExecutionVars
const { codePath, runTime } = await getRunTimeAndFilePath(_program)
try {
const { codePath, runTime } = await getRunTimeAndFilePath(_program)
const { result, httpHeaders } =
(await new ExecutionController().executeFile({
programPath: codePath,
@@ -169,13 +169,13 @@ const executeReturnJson = async (
req: express.Request,
_program: string
): Promise<ExecuteReturnJsonResponse> => {
const { codePath, runTime } = await getRunTimeAndFilePath(_program)
const filesNamesMap = req.files?.length
? makeFilesNamesMap(req.files as MulterFile[])
: null
try {
const { codePath, runTime } = await getRunTimeAndFilePath(_program)
const { webout, log, httpHeaders } =
(await new ExecutionController().executeFile({
programPath: codePath,

View File

@@ -1,18 +1,37 @@
import path from 'path'
import { fileExists } from '@sasjs/utils'
import { getFilesFolder } from './file'
import { RunTimeType } from '.'
export const getRunTimeAndFilePath = async (programPath: string) => {
for (const runTime of process.runTimes) {
const codePath =
path
.join(getFilesFolder(), programPath)
.replace(new RegExp('/', 'g'), path.sep) +
'.' +
runTime
const ext = path.extname(programPath)
// if program path is provided with extension we should split that into code path and ext as run time
if (ext) {
const runTime = ext.slice(1)
const runTimeTypes = Object.values(RunTimeType)
if (await fileExists(codePath)) return { codePath, runTime }
if (!runTimeTypes.includes(runTime as RunTimeType)) {
throw `The '${runTime}' runtime is not supported.`
}
const codePath = path
.join(getFilesFolder(), programPath)
.replace(new RegExp('/', 'g'), path.sep)
if (await fileExists(codePath)) {
return { codePath, runTime: runTime as RunTimeType }
}
} else {
for (const runTime of process.runTimes) {
const codePath =
path
.join(getFilesFolder(), programPath)
.replace(new RegExp('/', 'g'), path.sep) +
'.' +
runTime
if (await fileExists(codePath)) return { codePath, runTime }
}
}
throw `The Program at (${programPath}) does not exist.`
}