1
0
mirror of https://github.com/sasjs/server.git synced 2026-04-09 07:03:13 +00:00

feat: compile systemInit and inject to autoExec

This commit is contained in:
Saad Jutt
2021-11-18 03:12:05 +05:00
parent 319743a23e
commit b75139dda5
10 changed files with 169 additions and 33 deletions

View File

@@ -18,10 +18,6 @@ export class ExecutionController {
let program = await readFile(programPath)
Object.keys(vars).forEach(
(key: string) => (program = `%let ${key}=${vars[key]};\n${program}`)
)
const sessionController = getSessionController()
const session = await sessionController.getSession()
@@ -38,7 +34,12 @@ export class ExecutionController {
preProgramVariables?.accessToken ?? 'accessToken'
)
program = `
const varStatments = Object.keys(vars).reduce(
(computed: string, key: string) =>
`${computed}%let ${key}=${vars[key]};\n`,
''
)
const preProgramVarStatments = `
%let _sasjs_tokenfile=${tokenFile};
%let _sasjs_username=${preProgramVariables?.username};
%let _sasjs_userid=${preProgramVariables?.userId};
@@ -47,8 +48,17 @@ export class ExecutionController {
%let _sasjs_apipath=/SASjsApi/stp/execute;
%let _metaperson=&_sasjs_displayname;
%let _metauser=&_sasjs_username;
%let sasjsprocessmode=Stored Program;
%let sasjsprocessmode=Stored Program;`
program = `
/* runtime vars */
${varStatments}
filename _webout "${weboutPath}";
/* dynamic user-provided vars */
${preProgramVarStatments}
/* actual job code */
${program}`
// if no files are uploaded filesNamesMap will be undefined

View File

@@ -2,12 +2,17 @@ import path from 'path'
import { Session } from '../../types'
import { promisify } from 'util'
import { execFile } from 'child_process'
import { getTmpSessionsFolderPath, generateUniqueFileName } from '../../utils'
import {
getTmpSessionsFolderPath,
generateUniqueFileName,
sysInitCompiledPath
} from '../../utils'
import {
deleteFolder,
createFile,
fileExists,
generateTimestamp
generateTimestamp,
readFile
} from '@sasjs/utils'
const execFilePromise = promisify(execFile)
@@ -52,9 +57,13 @@ export class SessionController {
// we clean them up after a predefined period, if unused
this.scheduleSessionDestroy(session)
// Place compiled system init code to autoexec
const compiledSystemInitContent = await readFile(sysInitCompiledPath)
// the autoexec file is executed on SAS startup
const autoExecPath = path.join(sessionFolder, 'autoexec.sas')
await createFile(autoExecPath, autoExecContent)
const contentForAutoExec = `/* compiled systemInit */\n${compiledSystemInitContent}\n/* autoexec */\n${autoExecContent}`
await createFile(autoExecPath, contentForAutoExec)
// create empty code.sas as SAS will not start without a SYSIN
const codePath = path.join(session.path, 'code.sas')

View File

@@ -14,7 +14,6 @@ import {
import { ExecutionController } from './internal'
import { PreProgramVars } from '../types'
import { getTmpFilesFolderPath, makeFilesNamesMap } from '../utils'
import { request } from 'https'
interface ExecuteReturnJsonPayload {
/**

View File

@@ -1,4 +1,4 @@
import { fileExists, readFile } from '@sasjs/utils'
import { readFile } from '@sasjs/utils'
import express from 'express'
import path from 'path'
import { getWebBuildFolderPath } from '../../utils'
@@ -12,22 +12,23 @@ const codeToInject = `
</script>`
webRouter.get('/', async (_, res) => {
const indexHtmlPath = path.join(getWebBuildFolderPath(), 'index.html')
if (!(await fileExists(indexHtmlPath))) {
let content: string
try {
const indexHtmlPath = path.join(getWebBuildFolderPath(), 'index.html')
content = await readFile(indexHtmlPath)
} catch (_) {
return res.send('Web Build is not present')
}
const { MODE } = process.env
if (MODE?.trim() !== 'server') {
const content = await readFile(indexHtmlPath)
const injectedContent = content.replace('</head>', `${codeToInject}</head>`)
res.setHeader('Content-Type', 'text/html')
return res.send(injectedContent)
}
res.sendFile(indexHtmlPath)
return res.send(content)
})
export default webRouter

View File

@@ -1,8 +1,16 @@
import path from 'path'
import { getRealPath } from '@sasjs/utils'
export const apiRoot = path.join(__dirname, '..', '..')
export const codebaseRoot = path.join(apiRoot, '..')
export const sysInitCompiledPath = path.join(
apiRoot,
'sasjsbuild',
'systemInitCompiled.sas'
)
export const getWebBuildFolderPath = () =>
path.join(__dirname, '..', '..', '..', 'web', 'build')
path.join(codebaseRoot, 'web', 'build')
export const getTmpFolderPath = () =>
process.driveLoc ?? getRealPath(path.join(process.cwd(), 'tmp'))