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

feat: conver single session controller to two controller i.e. SASSessionController and JSSessionController

This commit is contained in:
2022-06-03 17:23:28 +05:00
parent 194eaec7d4
commit 07295aa151
4 changed files with 83 additions and 17 deletions

View File

@@ -1,13 +1,15 @@
import path from 'path'
import { Request, RequestHandler } from 'express'
import multer from 'multer'
import { uuidv4 } from '@sasjs/utils'
import { getSessionController } from '.'
import { uuidv4, fileExists, readFile } from '@sasjs/utils'
import { getSASSessionController, getJSSessionController } from '.'
import { getFilesFolder } from '../../utils'
export class FileUploadController {
private storage = multer.diskStorage({
destination: function (req: Request, file: any, cb: any) {
//Sending the intercepted files to the sessions subfolder
cb(null, req.sasSession?.path)
cb(null, req.sasjsSession?.path)
},
filename: function (req: Request, file: any, cb: any) {
//req_file prefix + unique hash added to sas request files
@@ -20,15 +22,38 @@ export class FileUploadController {
//It will intercept request and generate unique uuid to be used as a subfolder name
//that will store the files uploaded
public preUploadMiddleware: RequestHandler = async (req, res, next) => {
let session
if (process.runTimes.length === 0) {
throw 'No runtime is specified in environment variables.'
}
const sessionController = getSessionController()
session = await sessionController.getSession()
// marking consumed true, so that it's not available
// as readySession for any other request
session.consumed = true
const programPath = req.query._program as string
req.sasSession = session
for (const runTime of process.runTimes) {
const codePath =
path
.join(getFilesFolder(), programPath)
.replace(new RegExp('/', 'g'), path.sep) + runTime
if (await fileExists(programPath)) {
const program = await readFile(codePath)
if (runTime === 'sas') {
const sessionController = getSASSessionController()
const session = await sessionController.getSession()
// marking consumed true, so that it's not available
// as readySession for any other request
session.consumed = true
req.sasjsSession = session
} else if (runTime === 'js') {
const sessionController = getJSSessionController()
const session = await sessionController.getSession()
req.sasjsSession = session
} else {
throw `${runTime} runtime is not implemented yet.`
}
}
}
next()
}

View File

@@ -17,7 +17,7 @@ import {
const execFilePromise = promisify(execFile)
export class SessionController {
export class SASSessionController {
private sessions: Session[] = []
private getReadySessions = (): Session[] =>
@@ -152,12 +152,51 @@ ${autoExecContent}`
}
}
export const getSessionController = (): SessionController => {
if (process.sessionController) return process.sessionController
export class JSSessionController {
public async getSession() {
const sessionId = generateUniqueFileName(generateTimestamp())
const sessionFolder = path.join(getSessionsFolder(), sessionId)
process.sessionController = new SessionController()
const creationTimeStamp = sessionId.split('-').pop() as string
// death time of session is 15 mins from creation
const deathTimeStamp = (
parseInt(creationTimeStamp) +
15 * 60 * 1000 -
1000
).toString()
return process.sessionController
const session: Session = {
id: sessionId,
ready: true,
inUse: true,
consumed: false,
completed: false,
creationTimeStamp,
deathTimeStamp,
path: sessionFolder
}
const headersPath = path.join(session.path, 'stpsrv_header.txt')
await createFile(headersPath, 'Content-type: application/json')
return session
}
}
export const getSASSessionController = (): SASSessionController => {
if (process.sasSessionController) return process.sasSessionController
process.sasSessionController = new SASSessionController()
return process.sasSessionController
}
export const getJSSessionController = (): JSSessionController => {
if (process.jsSessionController) return process.jsSessionController
process.jsSessionController = new JSSessionController()
return process.jsSessionController
}
const autoExecContent = `

View File

@@ -2,6 +2,6 @@ declare namespace Express {
export interface Request {
accessToken?: string
user?: import('../').RequestUser
sasSession?: import('../').Session
sasjsSession?: import('../').Session
}
}

View File

@@ -1,8 +1,10 @@
declare namespace NodeJS {
export interface Process {
runTimes: string[]
sasLoc: string
driveLoc: string
sessionController?: import('../../controllers/internal').SessionController
sasSessionController?: import('../../controllers/internal').SASSessionController
jsSessionController?: import('../../controllers/internal').JSSessionController
appStreamConfig: import('../').AppStreamConfig
logger: import('@sasjs/utils/logger').Logger
}