mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 19:34:34 +00:00
chore: code refactor for getting session controller
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
import {
|
||||
SASSessionController,
|
||||
JSSessionController,
|
||||
getSASSessionController,
|
||||
getJSSessionController,
|
||||
processProgram
|
||||
} from './'
|
||||
import { getSessionController, processProgram } from './'
|
||||
import { readFile, fileExists, createFile, readFileBinary } from '@sasjs/utils'
|
||||
import { PreProgramVars, Session, TreeNode } from '../../types'
|
||||
import {
|
||||
@@ -78,19 +72,7 @@ export class ExecutionController {
|
||||
session: sessionByFileUpload,
|
||||
runTime
|
||||
}: ExecuteProgramParams): Promise<ExecuteReturnRaw | ExecuteReturnJson> {
|
||||
let sessionController: SASSessionController | JSSessionController
|
||||
|
||||
switch (runTime) {
|
||||
case RunTimeType.SAS:
|
||||
sessionController = getSASSessionController()
|
||||
break
|
||||
case RunTimeType.JS:
|
||||
sessionController = getJSSessionController()
|
||||
break
|
||||
|
||||
default:
|
||||
throw new Error('No Runtime is configured1')
|
||||
}
|
||||
const sessionController = getSessionController(runTime)
|
||||
|
||||
const session =
|
||||
sessionByFileUpload ?? (await sessionController.getSession())
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import { Request, RequestHandler } from 'express'
|
||||
import multer from 'multer'
|
||||
import { uuidv4 } from '@sasjs/utils'
|
||||
import {
|
||||
SASSessionController,
|
||||
JSSessionController,
|
||||
getSASSessionController,
|
||||
getJSSessionController
|
||||
} from '.'
|
||||
import { getSessionController } from '.'
|
||||
import {
|
||||
executeProgramRawValidation,
|
||||
getRunTimeAndFilePath,
|
||||
@@ -42,25 +37,22 @@ export class FileUploadController {
|
||||
try {
|
||||
;({ runTime } = await getRunTimeAndFilePath(programPath))
|
||||
} catch (err: any) {
|
||||
res.status(400).send({
|
||||
return res.status(400).send({
|
||||
status: 'failure',
|
||||
message: 'Job execution failed',
|
||||
error: typeof err === 'object' ? err.toString() : err
|
||||
})
|
||||
}
|
||||
|
||||
let sessionController: SASSessionController | JSSessionController
|
||||
|
||||
switch (runTime) {
|
||||
case RunTimeType.SAS:
|
||||
sessionController = getSASSessionController()
|
||||
break
|
||||
case RunTimeType.JS:
|
||||
sessionController = getJSSessionController()
|
||||
break
|
||||
|
||||
default:
|
||||
return res.status(400).send('No Runtime is configured1')
|
||||
let sessionController
|
||||
try {
|
||||
sessionController = getSessionController(runTime)
|
||||
} catch (err: any) {
|
||||
return res.status(400).send({
|
||||
status: 'failure',
|
||||
message: err.message,
|
||||
error: typeof err === 'object' ? err.toString() : err
|
||||
})
|
||||
}
|
||||
|
||||
const session = await sessionController.getSession()
|
||||
|
||||
@@ -5,7 +5,8 @@ import { execFile } from 'child_process'
|
||||
import {
|
||||
getSessionsFolder,
|
||||
generateUniqueFileName,
|
||||
sysInitCompiledPath
|
||||
sysInitCompiledPath,
|
||||
RunTimeType
|
||||
} from '../../utils'
|
||||
import {
|
||||
deleteFolder,
|
||||
@@ -193,7 +194,21 @@ export class JSSessionController extends SessionController {
|
||||
}
|
||||
}
|
||||
|
||||
export const getSASSessionController = (): SASSessionController => {
|
||||
export const getSessionController = (
|
||||
runTime: RunTimeType
|
||||
): SASSessionController | JSSessionController => {
|
||||
if (runTime === RunTimeType.SAS) {
|
||||
return getSASSessionController()
|
||||
}
|
||||
|
||||
if (runTime === RunTimeType.JS) {
|
||||
return getJSSessionController()
|
||||
}
|
||||
|
||||
throw new Error('No Runtime is configured')
|
||||
}
|
||||
|
||||
const getSASSessionController = (): SASSessionController => {
|
||||
if (process.sasSessionController) return process.sasSessionController
|
||||
|
||||
process.sasSessionController = new SASSessionController()
|
||||
@@ -201,7 +216,7 @@ export const getSASSessionController = (): SASSessionController => {
|
||||
return process.sasSessionController
|
||||
}
|
||||
|
||||
export const getJSSessionController = (): JSSessionController => {
|
||||
const getJSSessionController = (): JSSessionController => {
|
||||
if (process.jsSessionController) return process.jsSessionController
|
||||
|
||||
process.jsSessionController = new JSSessionController()
|
||||
|
||||
Reference in New Issue
Block a user