1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 19:44:35 +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 PRIVATE_KEY=privkey.pem
FULL_CHAIN=fullchain.pem FULL_CHAIN=fullchain.pem
RUN_TIMES=[sas|js|sas,js|js,sas] default considered as sas
PORT=[5000] default value is 5000 PORT=[5000] default value is 5000
HELMET_CSP_CONFIG_PATH=./csp.config.json if omitted HELMET default will be used 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 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 = const sessionController =
runTime === RunTimeType.SAS runTime === RunTimeType.SAS

View File

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

View File

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