mirror of
https://github.com/sasjs/server.git
synced 2025-12-10 19:34:34 +00:00
Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c261745f1d | ||
|
|
d6e527ecf2 | ||
|
|
bc2cff1d0d | ||
|
|
66aa9b5891 | ||
|
|
ca17e7c192 | ||
|
|
73df102422 | ||
|
|
48a9a4dd0e | ||
|
|
4f6f735f5b | ||
|
|
6b6546c7ad | ||
|
|
f94ddc0352 | ||
|
|
03670cf0d6 | ||
|
|
ea2ec97c1c | ||
|
|
832f1156e8 | ||
|
|
5cda9cd5d8 | ||
|
|
5d576aff91 | ||
|
|
a044176054 | ||
|
|
deee34f5fd | ||
|
|
b0723f1444 | ||
|
|
e9519cb3c6 | ||
|
|
0838b8112e | ||
|
|
441f8b7726 | ||
|
|
049a7f4b80 | ||
|
|
3053c68bdf | ||
|
|
76750e864d | ||
|
|
ffcf193b87 | ||
|
|
aa2a1cbe13 | ||
|
|
6f2c53555c | ||
|
|
73d965daf5 | ||
|
|
4f1763db67 | ||
|
|
28222add04 | ||
|
|
068edfd6a5 | ||
|
|
7e8cbbf377 | ||
|
|
1fc1431442 | ||
|
|
3387efbb9a | ||
|
|
e2996b495f | ||
|
|
41c627f93a | ||
|
|
49f5dc7555 | ||
|
|
f6e77f99a4 | ||
|
|
b57dfa429b | ||
| 9586dbb2d0 |
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
@@ -56,4 +56,4 @@ jobs:
|
|||||||
|
|
||||||
- name: Release
|
- name: Release
|
||||||
run: |
|
run: |
|
||||||
GITHUB_TOKEN=${{ secrets.GH_TOKEN }} semantic-release
|
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} semantic-release
|
||||||
|
|||||||
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -1,5 +1,3 @@
|
|||||||
{
|
{
|
||||||
"cSpell.words": [
|
"cSpell.words": ["autoexec", "initialising"]
|
||||||
"autoexec"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
54
CHANGELOG.md
54
CHANGELOG.md
@@ -1,3 +1,57 @@
|
|||||||
|
# [0.39.0](https://github.com/sasjs/server/compare/v0.38.0...v0.39.0) (2024-10-31)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **api:** fixed condition in processProgram ([48a9a4d](https://github.com/sasjs/server/commit/48a9a4dd0e31f84209635382be4ec4bb2c3a9c0c))
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **api:** added session state endpoint ([6b6546c](https://github.com/sasjs/server/commit/6b6546c7ad0833347f8dc4cdba6ad19132f7aaef))
|
||||||
|
|
||||||
|
# [0.38.0](https://github.com/sasjs/server/compare/v0.37.0...v0.38.0) (2024-10-30)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **api:** enabled query params in stp/trigger endpoint ([5cda9cd](https://github.com/sasjs/server/commit/5cda9cd5d8623b7ea2ecd989d7808f47ec866672))
|
||||||
|
|
||||||
|
# [0.37.0](https://github.com/sasjs/server/compare/v0.36.0...v0.37.0) (2024-10-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **stp:** added trigger endpoint ([b0723f1](https://github.com/sasjs/server/commit/b0723f14448d60ffce4f2175cf8a73fc4d4dd0ee))
|
||||||
|
|
||||||
|
# [0.36.0](https://github.com/sasjs/server/compare/v0.35.4...v0.36.0) (2024-10-29)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **code:** added code/trigger API endpoint ([ffcf193](https://github.com/sasjs/server/commit/ffcf193b87d811b166d79af74013776a253b50b0))
|
||||||
|
|
||||||
|
## [0.35.4](https://github.com/sasjs/server/compare/v0.35.3...v0.35.4) (2024-01-15)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **api:** fixed env issue in MacOS executable ([73d965d](https://github.com/sasjs/server/commit/73d965daf54b16c0921e4b18d11a1e6f8650884d))
|
||||||
|
|
||||||
|
## [0.35.3](https://github.com/sasjs/server/compare/v0.35.2...v0.35.3) (2023-11-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* enable embedded LFs in JS STP vars ([7e8cbbf](https://github.com/sasjs/server/commit/7e8cbbf377b27a7f5dd9af0bc6605c01f302f5d9))
|
||||||
|
|
||||||
|
## [0.35.2](https://github.com/sasjs/server/compare/v0.35.1...v0.35.2) (2023-08-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* add _debug as optional query param in swagger apis for GET stp/execute ([9586dbb](https://github.com/sasjs/server/commit/9586dbb2d0d6611061c9efdfb84030144f62c2ee))
|
||||||
|
|
||||||
## [0.35.1](https://github.com/sasjs/server/compare/v0.35.0...v0.35.1) (2023-07-25)
|
## [0.35.1](https://github.com/sasjs/server/compare/v0.35.0...v0.35.1) (2023-07-25)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ LDAP_USERS_BASE_DN = <ou=users,dc=cloudron>
|
|||||||
LDAP_GROUPS_BASE_DN = <ou=groups,dc=cloudron>
|
LDAP_GROUPS_BASE_DN = <ou=groups,dc=cloudron>
|
||||||
|
|
||||||
#default value is 100
|
#default value is 100
|
||||||
MAX_WRONG_ATTEMPTS_BY_IP_PER_DAY=100
|
MAX_WRONG_ATTEMPTS_BY_IP_PER_DAY=100
|
||||||
|
|
||||||
#default value is 10
|
#default value is 10
|
||||||
MAX_CONSECUTIVE_FAILS_BY_USERNAME_AND_IP=10
|
MAX_CONSECUTIVE_FAILS_BY_USERNAME_AND_IP=10
|
||||||
|
|||||||
16
api/package-lock.json
generated
16
api/package-lock.json
generated
@@ -51,7 +51,7 @@
|
|||||||
"adm-zip": "^0.5.9",
|
"adm-zip": "^0.5.9",
|
||||||
"axios": "0.27.2",
|
"axios": "0.27.2",
|
||||||
"csrf": "^3.1.0",
|
"csrf": "^3.1.0",
|
||||||
"dotenv": "^10.0.0",
|
"dotenv": "^16.0.1",
|
||||||
"http-headers-validation": "^0.0.1",
|
"http-headers-validation": "^0.0.1",
|
||||||
"jest": "^27.0.6",
|
"jest": "^27.0.6",
|
||||||
"mongodb-memory-server": "8.11.4",
|
"mongodb-memory-server": "8.11.4",
|
||||||
@@ -4788,12 +4788,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/dotenv": {
|
"node_modules/dotenv": {
|
||||||
"version": "10.0.0",
|
"version": "16.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz",
|
||||||
"integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==",
|
"integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10"
|
"node": ">=12"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/duplexer2": {
|
"node_modules/duplexer2": {
|
||||||
@@ -15217,9 +15217,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dotenv": {
|
"dotenv": {
|
||||||
"version": "10.0.0",
|
"version": "16.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz",
|
||||||
"integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==",
|
"integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"duplexer2": {
|
"duplexer2": {
|
||||||
|
|||||||
@@ -88,7 +88,7 @@
|
|||||||
"adm-zip": "^0.5.9",
|
"adm-zip": "^0.5.9",
|
||||||
"axios": "0.27.2",
|
"axios": "0.27.2",
|
||||||
"csrf": "^3.1.0",
|
"csrf": "^3.1.0",
|
||||||
"dotenv": "^10.0.0",
|
"dotenv": "^16.0.1",
|
||||||
"http-headers-validation": "^0.0.1",
|
"http-headers-validation": "^0.0.1",
|
||||||
"jest": "^27.0.6",
|
"jest": "^27.0.6",
|
||||||
"mongodb-memory-server": "8.11.4",
|
"mongodb-memory-server": "8.11.4",
|
||||||
|
|||||||
@@ -98,17 +98,47 @@ components:
|
|||||||
properties:
|
properties:
|
||||||
code:
|
code:
|
||||||
type: string
|
type: string
|
||||||
description: 'Code of program'
|
description: 'The code to be executed'
|
||||||
example: '* Code HERE;'
|
example: '* Your Code HERE;'
|
||||||
runTime:
|
runTime:
|
||||||
$ref: '#/components/schemas/RunTimeType'
|
$ref: '#/components/schemas/RunTimeType'
|
||||||
description: 'runtime for program'
|
description: 'The runtime for the code - eg SAS, JS, PY or R'
|
||||||
example: js
|
example: js
|
||||||
required:
|
required:
|
||||||
- code
|
- code
|
||||||
- runTime
|
- runTime
|
||||||
type: object
|
type: object
|
||||||
additionalProperties: false
|
additionalProperties: false
|
||||||
|
TriggerCodeResponse:
|
||||||
|
properties:
|
||||||
|
sessionId:
|
||||||
|
type: string
|
||||||
|
description: "`sessionId` is the ID of the session and the name of the temporary folder\nused to store code outputs.<br><br>\nFor SAS, this would be the location of the SASWORK folder.<br><br>\n`sessionId` can be used to poll session state using the\nGET /SASjsApi/session/{sessionId}/state endpoint."
|
||||||
|
example: 20241028074744-54132-1730101664824
|
||||||
|
required:
|
||||||
|
- sessionId
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
|
TriggerCodePayload:
|
||||||
|
properties:
|
||||||
|
code:
|
||||||
|
type: string
|
||||||
|
description: 'The code to be executed'
|
||||||
|
example: '* Your Code HERE;'
|
||||||
|
runTime:
|
||||||
|
$ref: '#/components/schemas/RunTimeType'
|
||||||
|
description: 'The runtime for the code - eg SAS, JS, PY or R'
|
||||||
|
example: sas
|
||||||
|
expiresAfterMins:
|
||||||
|
type: number
|
||||||
|
format: double
|
||||||
|
description: "Amount of minutes after the completion of the job when the session must be\ndestroyed."
|
||||||
|
example: 15
|
||||||
|
required:
|
||||||
|
- code
|
||||||
|
- runTime
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
MemberType.folder:
|
MemberType.folder:
|
||||||
enum:
|
enum:
|
||||||
- folder
|
- folder
|
||||||
@@ -555,6 +585,14 @@ components:
|
|||||||
- needsToUpdatePassword
|
- needsToUpdatePassword
|
||||||
type: object
|
type: object
|
||||||
additionalProperties: false
|
additionalProperties: false
|
||||||
|
SessionState:
|
||||||
|
enum:
|
||||||
|
- initialising
|
||||||
|
- pending
|
||||||
|
- running
|
||||||
|
- completed
|
||||||
|
- failed
|
||||||
|
type: string
|
||||||
ExecutePostRequestPayload:
|
ExecutePostRequestPayload:
|
||||||
properties:
|
properties:
|
||||||
_program:
|
_program:
|
||||||
@@ -563,6 +601,16 @@ components:
|
|||||||
example: /Public/somefolder/some.file
|
example: /Public/somefolder/some.file
|
||||||
type: object
|
type: object
|
||||||
additionalProperties: false
|
additionalProperties: false
|
||||||
|
TriggerProgramResponse:
|
||||||
|
properties:
|
||||||
|
sessionId:
|
||||||
|
type: string
|
||||||
|
description: "`sessionId` is the ID of the session and the name of the temporary folder\nused to store program outputs.<br><br>\nFor SAS, this would be the location of the SASWORK folder.<br><br>\n`sessionId` can be used to poll session state using the\nGET /SASjsApi/session/{sessionId}/state endpoint."
|
||||||
|
example: 20241028074744-54132-1730101664824
|
||||||
|
required:
|
||||||
|
- sessionId
|
||||||
|
type: object
|
||||||
|
additionalProperties: false
|
||||||
LoginPayload:
|
LoginPayload:
|
||||||
properties:
|
properties:
|
||||||
username:
|
username:
|
||||||
@@ -792,7 +840,7 @@ paths:
|
|||||||
- {type: string}
|
- {type: string}
|
||||||
- {type: string, format: byte}
|
- {type: string, format: byte}
|
||||||
description: 'Execute Code on the Specified Runtime'
|
description: 'Execute Code on the Specified Runtime'
|
||||||
summary: 'Run Code and Return Webout Content and Log'
|
summary: "Run Code and Return Webout Content, Log and Print output\nThe order of returned parts of the payload is:\n1. Webout (if present)\n2. Logs UUID (used as separator)\n3. Log\n4. Logs UUID (used as separator)\n5. Print (if present and if the runtime is SAS)\nPlease see"
|
||||||
tags:
|
tags:
|
||||||
- Code
|
- Code
|
||||||
security:
|
security:
|
||||||
@@ -805,6 +853,30 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/ExecuteCodePayload'
|
$ref: '#/components/schemas/ExecuteCodePayload'
|
||||||
|
/SASjsApi/code/trigger:
|
||||||
|
post:
|
||||||
|
operationId: TriggerCode
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Ok
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/TriggerCodeResponse'
|
||||||
|
description: 'Trigger Code on the Specified Runtime'
|
||||||
|
summary: 'Triggers code and returns SessionId immediately - does not wait for job completion'
|
||||||
|
tags:
|
||||||
|
- Code
|
||||||
|
security:
|
||||||
|
-
|
||||||
|
bearerAuth: []
|
||||||
|
parameters: []
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/TriggerCodePayload'
|
||||||
/SASjsApi/drive/deploy:
|
/SASjsApi/drive/deploy:
|
||||||
post:
|
post:
|
||||||
operationId: Deploy
|
operationId: Deploy
|
||||||
@@ -1777,6 +1849,30 @@ paths:
|
|||||||
-
|
-
|
||||||
bearerAuth: []
|
bearerAuth: []
|
||||||
parameters: []
|
parameters: []
|
||||||
|
'/SASjsApi/session/{sessionId}/state':
|
||||||
|
get:
|
||||||
|
operationId: SessionState
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Ok
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/SessionState'
|
||||||
|
description: "The polling endpoint is currently implemented for single-server deployments only.<br>\nLoad balanced / grid topologies will be supported in a future release.<br>\nIf your site requires this, please reach out to SASjs Support."
|
||||||
|
summary: 'Get session state (initialising, pending, running, completed, failed).'
|
||||||
|
tags:
|
||||||
|
- Session
|
||||||
|
security:
|
||||||
|
-
|
||||||
|
bearerAuth: []
|
||||||
|
parameters:
|
||||||
|
-
|
||||||
|
in: path
|
||||||
|
name: sessionId
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
/SASjsApi/stp/execute:
|
/SASjsApi/stp/execute:
|
||||||
get:
|
get:
|
||||||
operationId: ExecuteGetRequest
|
operationId: ExecuteGetRequest
|
||||||
@@ -1789,7 +1885,7 @@ paths:
|
|||||||
anyOf:
|
anyOf:
|
||||||
- {type: string}
|
- {type: string}
|
||||||
- {type: string, format: byte}
|
- {type: string, format: byte}
|
||||||
description: "Trigger a Stored Program using the _program URL parameter.\n\nAccepts URL parameters and file uploads. For more details, see docs:\n\nhttps://server.sasjs.io/storedprograms"
|
description: "Trigger a Stored Program using the _program URL parameter.\n\nAccepts additional URL parameters (converted to session variables)\nand file uploads. For more details, see docs:\n\nhttps://server.sasjs.io/storedprograms"
|
||||||
summary: 'Execute a Stored Program, returns _webout and (optionally) log.'
|
summary: 'Execute a Stored Program, returns _webout and (optionally) log.'
|
||||||
tags:
|
tags:
|
||||||
- STP
|
- STP
|
||||||
@@ -1798,13 +1894,22 @@ paths:
|
|||||||
bearerAuth: []
|
bearerAuth: []
|
||||||
parameters:
|
parameters:
|
||||||
-
|
-
|
||||||
description: 'Location of code in SASjs Drive'
|
description: 'Location of Stored Program in SASjs Drive.'
|
||||||
in: query
|
in: query
|
||||||
name: _program
|
name: _program
|
||||||
required: true
|
required: true
|
||||||
schema:
|
schema:
|
||||||
type: string
|
type: string
|
||||||
example: /Projects/myApp/some/program
|
example: /Projects/myApp/some/program
|
||||||
|
-
|
||||||
|
description: 'Optional query param for setting debug mode (returns the session log in the response body).'
|
||||||
|
in: query
|
||||||
|
name: _debug
|
||||||
|
required: false
|
||||||
|
schema:
|
||||||
|
format: double
|
||||||
|
type: number
|
||||||
|
example: 131
|
||||||
post:
|
post:
|
||||||
operationId: ExecutePostRequest
|
operationId: ExecutePostRequest
|
||||||
responses:
|
responses:
|
||||||
@@ -1838,6 +1943,50 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/ExecutePostRequestPayload'
|
$ref: '#/components/schemas/ExecutePostRequestPayload'
|
||||||
|
/SASjsApi/stp/trigger:
|
||||||
|
post:
|
||||||
|
operationId: TriggerProgram
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Ok
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/TriggerProgramResponse'
|
||||||
|
description: 'Trigger Program on the Specified Runtime.'
|
||||||
|
summary: 'Triggers program and returns SessionId immediately - does not wait for program completion.'
|
||||||
|
tags:
|
||||||
|
- STP
|
||||||
|
security:
|
||||||
|
-
|
||||||
|
bearerAuth: []
|
||||||
|
parameters:
|
||||||
|
-
|
||||||
|
description: 'Location of code in SASjs Drive.'
|
||||||
|
in: query
|
||||||
|
name: _program
|
||||||
|
required: true
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: /Projects/myApp/some/program
|
||||||
|
-
|
||||||
|
description: 'Optional query param for setting debug mode.'
|
||||||
|
in: query
|
||||||
|
name: _debug
|
||||||
|
required: false
|
||||||
|
schema:
|
||||||
|
format: double
|
||||||
|
type: number
|
||||||
|
example: 131
|
||||||
|
-
|
||||||
|
description: 'Optional query param for setting amount of minutes after the completion of the program when the session must be destroyed.'
|
||||||
|
in: query
|
||||||
|
name: expiresAfterMins
|
||||||
|
required: false
|
||||||
|
schema:
|
||||||
|
format: double
|
||||||
|
type: number
|
||||||
|
example: 15
|
||||||
/:
|
/:
|
||||||
get:
|
get:
|
||||||
operationId: Home
|
operationId: Home
|
||||||
|
|||||||
@@ -1,27 +1,57 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
|
import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
|
||||||
import { ExecutionController } from './internal'
|
import { ExecutionController, getSessionController } from './internal'
|
||||||
import {
|
import {
|
||||||
getPreProgramVariables,
|
getPreProgramVariables,
|
||||||
getUserAutoExec,
|
getUserAutoExec,
|
||||||
ModeType,
|
ModeType,
|
||||||
parseLogToArray,
|
|
||||||
RunTimeType
|
RunTimeType
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
|
|
||||||
interface ExecuteCodePayload {
|
interface ExecuteCodePayload {
|
||||||
/**
|
/**
|
||||||
* Code of program
|
* The code to be executed
|
||||||
* @example "* Code HERE;"
|
* @example "* Your Code HERE;"
|
||||||
*/
|
*/
|
||||||
code: string
|
code: string
|
||||||
/**
|
/**
|
||||||
* runtime for program
|
* The runtime for the code - eg SAS, JS, PY or R
|
||||||
* @example "js"
|
* @example "js"
|
||||||
*/
|
*/
|
||||||
runTime: RunTimeType
|
runTime: RunTimeType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TriggerCodePayload {
|
||||||
|
/**
|
||||||
|
* The code to be executed
|
||||||
|
* @example "* Your Code HERE;"
|
||||||
|
*/
|
||||||
|
code: string
|
||||||
|
/**
|
||||||
|
* The runtime for the code - eg SAS, JS, PY or R
|
||||||
|
* @example "sas"
|
||||||
|
*/
|
||||||
|
runTime: RunTimeType
|
||||||
|
/**
|
||||||
|
* Amount of minutes after the completion of the job when the session must be
|
||||||
|
* destroyed.
|
||||||
|
* @example 15
|
||||||
|
*/
|
||||||
|
expiresAfterMins?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TriggerCodeResponse {
|
||||||
|
/**
|
||||||
|
* `sessionId` is the ID of the session and the name of the temporary folder
|
||||||
|
* used to store code outputs.<br><br>
|
||||||
|
* For SAS, this would be the location of the SASWORK folder.<br><br>
|
||||||
|
* `sessionId` can be used to poll session state using the
|
||||||
|
* GET /SASjsApi/session/{sessionId}/state endpoint.
|
||||||
|
* @example "20241028074744-54132-1730101664824"
|
||||||
|
*/
|
||||||
|
sessionId: string
|
||||||
|
}
|
||||||
|
|
||||||
@Security('bearerAuth')
|
@Security('bearerAuth')
|
||||||
@Route('SASjsApi/code')
|
@Route('SASjsApi/code')
|
||||||
@Tags('Code')
|
@Tags('Code')
|
||||||
@@ -44,6 +74,18 @@ export class CodeController {
|
|||||||
): Promise<string | Buffer> {
|
): Promise<string | Buffer> {
|
||||||
return executeCode(request, body)
|
return executeCode(request, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger Code on the Specified Runtime
|
||||||
|
* @summary Triggers code and returns SessionId immediately - does not wait for job completion
|
||||||
|
*/
|
||||||
|
@Post('/trigger')
|
||||||
|
public async triggerCode(
|
||||||
|
@Request() request: express.Request,
|
||||||
|
@Body() body: TriggerCodePayload
|
||||||
|
): Promise<TriggerCodeResponse> {
|
||||||
|
return triggerCode(request, body)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const executeCode = async (
|
const executeCode = async (
|
||||||
@@ -76,3 +118,49 @@ const executeCode = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const triggerCode = async (
|
||||||
|
req: express.Request,
|
||||||
|
{ code, runTime, expiresAfterMins }: TriggerCodePayload
|
||||||
|
): Promise<TriggerCodeResponse> => {
|
||||||
|
const { user } = req
|
||||||
|
const userAutoExec =
|
||||||
|
process.env.MODE === ModeType.Server
|
||||||
|
? user?.autoExec
|
||||||
|
: await getUserAutoExec()
|
||||||
|
|
||||||
|
// get session controller based on runTime
|
||||||
|
const sessionController = getSessionController(runTime)
|
||||||
|
|
||||||
|
// get session
|
||||||
|
const session = await sessionController.getSession()
|
||||||
|
|
||||||
|
// add expiresAfterMins to session if provided
|
||||||
|
if (expiresAfterMins) {
|
||||||
|
// expiresAfterMins.used is set initially to false
|
||||||
|
session.expiresAfterMins = { mins: expiresAfterMins, used: false }
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// call executeProgram method of ExecutionController without awaiting
|
||||||
|
new ExecutionController().executeProgram({
|
||||||
|
program: code,
|
||||||
|
preProgramVariables: getPreProgramVariables(req),
|
||||||
|
vars: { ...req.query, _debug: 131 },
|
||||||
|
otherArgs: { userAutoExec },
|
||||||
|
runTime: runTime,
|
||||||
|
includePrintOutput: true,
|
||||||
|
session // session is provided
|
||||||
|
})
|
||||||
|
|
||||||
|
// return session id
|
||||||
|
return { sessionId: session.id }
|
||||||
|
} catch (err: any) {
|
||||||
|
throw {
|
||||||
|
code: 400,
|
||||||
|
status: 'failure',
|
||||||
|
message: 'Job execution failed.',
|
||||||
|
error: typeof err === 'object' ? err.toString() : err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import path from 'path'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { getSessionController, processProgram } from './'
|
import { getSessionController, processProgram } from './'
|
||||||
import { readFile, fileExists, createFile, readFileBinary } from '@sasjs/utils'
|
import { readFile, fileExists, createFile, readFileBinary } from '@sasjs/utils'
|
||||||
import { PreProgramVars, Session, TreeNode } from '../../types'
|
import { PreProgramVars, Session, TreeNode, SessionState } from '../../types'
|
||||||
import {
|
import {
|
||||||
extractHeaders,
|
extractHeaders,
|
||||||
getFilesFolder,
|
getFilesFolder,
|
||||||
@@ -75,8 +75,7 @@ export class ExecutionController {
|
|||||||
|
|
||||||
const session =
|
const session =
|
||||||
sessionByFileUpload ?? (await sessionController.getSession())
|
sessionByFileUpload ?? (await sessionController.getSession())
|
||||||
session.inUse = true
|
session.state = SessionState.running
|
||||||
session.consumed = true
|
|
||||||
|
|
||||||
const logPath = path.join(session.path, 'log.log')
|
const logPath = path.join(session.path, 'log.log')
|
||||||
const headersPath = path.join(session.path, 'stpsrv_header.txt')
|
const headersPath = path.join(session.path, 'stpsrv_header.txt')
|
||||||
@@ -121,7 +120,7 @@ export class ExecutionController {
|
|||||||
: ''
|
: ''
|
||||||
|
|
||||||
// it should be deleted by scheduleSessionDestroy
|
// it should be deleted by scheduleSessionDestroy
|
||||||
session.inUse = false
|
session.state = SessionState.completed
|
||||||
|
|
||||||
const resultParts = []
|
const resultParts = []
|
||||||
|
|
||||||
@@ -145,7 +144,9 @@ export class ExecutionController {
|
|||||||
return {
|
return {
|
||||||
httpHeaders,
|
httpHeaders,
|
||||||
result:
|
result:
|
||||||
isDebugOn(vars) || session.crashed ? resultParts.join(`\n`) : webout
|
isDebugOn(vars) || session.failureReason
|
||||||
|
? resultParts.join(`\n`)
|
||||||
|
: webout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,8 @@ import { Request, RequestHandler } from 'express'
|
|||||||
import multer from 'multer'
|
import multer from 'multer'
|
||||||
import { uuidv4 } from '@sasjs/utils'
|
import { uuidv4 } from '@sasjs/utils'
|
||||||
import { getSessionController } from '.'
|
import { getSessionController } from '.'
|
||||||
import {
|
import { executeProgramRawValidation, getRunTimeAndFilePath } from '../../utils'
|
||||||
executeProgramRawValidation,
|
import { SessionState } from '../../types'
|
||||||
getRunTimeAndFilePath,
|
|
||||||
RunTimeType
|
|
||||||
} from '../../utils'
|
|
||||||
|
|
||||||
export class FileUploadController {
|
export class FileUploadController {
|
||||||
private storage = multer.diskStorage({
|
private storage = multer.diskStorage({
|
||||||
@@ -56,9 +53,8 @@ export class FileUploadController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const session = await sessionController.getSession()
|
const session = await sessionController.getSession()
|
||||||
// marking consumed true, so that it's not available
|
// change session state to 'running', so that it's not available for any other request
|
||||||
// as readySession for any other request
|
session.state = SessionState.running
|
||||||
session.consumed = true
|
|
||||||
|
|
||||||
req.sasjsSession = session
|
req.sasjsSession = session
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { Session } from '../../types'
|
import { Session, SessionState } from '../../types'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
import { execFile } from 'child_process'
|
import { execFile } from 'child_process'
|
||||||
import {
|
import {
|
||||||
@@ -14,8 +14,7 @@ import {
|
|||||||
createFile,
|
createFile,
|
||||||
fileExists,
|
fileExists,
|
||||||
generateTimestamp,
|
generateTimestamp,
|
||||||
readFile,
|
readFile
|
||||||
isWindows
|
|
||||||
} from '@sasjs/utils'
|
} from '@sasjs/utils'
|
||||||
|
|
||||||
const execFilePromise = promisify(execFile)
|
const execFilePromise = promisify(execFile)
|
||||||
@@ -24,7 +23,9 @@ export class SessionController {
|
|||||||
protected sessions: Session[] = []
|
protected sessions: Session[] = []
|
||||||
|
|
||||||
protected getReadySessions = (): Session[] =>
|
protected getReadySessions = (): Session[] =>
|
||||||
this.sessions.filter((sess: Session) => sess.ready && !sess.consumed)
|
this.sessions.filter(
|
||||||
|
(session: Session) => session.state === SessionState.pending
|
||||||
|
)
|
||||||
|
|
||||||
protected async createSession(): Promise<Session> {
|
protected async createSession(): Promise<Session> {
|
||||||
const sessionId = generateUniqueFileName(generateTimestamp())
|
const sessionId = generateUniqueFileName(generateTimestamp())
|
||||||
@@ -40,19 +41,18 @@ export class SessionController {
|
|||||||
|
|
||||||
const session: Session = {
|
const session: Session = {
|
||||||
id: sessionId,
|
id: sessionId,
|
||||||
ready: true,
|
state: SessionState.pending,
|
||||||
inUse: true,
|
|
||||||
consumed: false,
|
|
||||||
completed: false,
|
|
||||||
creationTimeStamp,
|
creationTimeStamp,
|
||||||
deathTimeStamp,
|
deathTimeStamp,
|
||||||
path: sessionFolder
|
path: sessionFolder
|
||||||
}
|
}
|
||||||
|
|
||||||
const headersPath = path.join(session.path, 'stpsrv_header.txt')
|
const headersPath = path.join(session.path, 'stpsrv_header.txt')
|
||||||
|
|
||||||
await createFile(headersPath, 'content-type: text/html; charset=utf-8')
|
await createFile(headersPath, 'content-type: text/html; charset=utf-8')
|
||||||
|
|
||||||
this.sessions.push(session)
|
this.sessions.push(session)
|
||||||
|
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,6 +67,10 @@ export class SessionController {
|
|||||||
|
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getSessionById(id: string) {
|
||||||
|
return this.sessions.find((session) => session.id === id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SASSessionController extends SessionController {
|
export class SASSessionController extends SessionController {
|
||||||
@@ -84,10 +88,7 @@ export class SASSessionController extends SessionController {
|
|||||||
|
|
||||||
const session: Session = {
|
const session: Session = {
|
||||||
id: sessionId,
|
id: sessionId,
|
||||||
ready: false,
|
state: SessionState.initialising,
|
||||||
inUse: false,
|
|
||||||
consumed: false,
|
|
||||||
completed: false,
|
|
||||||
creationTimeStamp,
|
creationTimeStamp,
|
||||||
deathTimeStamp,
|
deathTimeStamp,
|
||||||
path: sessionFolder
|
path: sessionFolder
|
||||||
@@ -145,13 +146,20 @@ ${autoExecContent}`
|
|||||||
process.sasLoc!.endsWith('sas.exe') ? session.path : ''
|
process.sasLoc!.endsWith('sas.exe') ? session.path : ''
|
||||||
])
|
])
|
||||||
.then(() => {
|
.then(() => {
|
||||||
session.completed = true
|
session.state = SessionState.completed
|
||||||
|
|
||||||
process.logger.info('session completed', session)
|
process.logger.info('session completed', session)
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
session.completed = true
|
session.state = SessionState.failed
|
||||||
session.crashed = err.toString()
|
|
||||||
process.logger.error('session crashed', session.id, session.crashed)
|
session.failureReason = err.toString()
|
||||||
|
|
||||||
|
process.logger.error(
|
||||||
|
'session crashed',
|
||||||
|
session.id,
|
||||||
|
session.failureReason
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
// we have a triggered session - add to array
|
// we have a triggered session - add to array
|
||||||
@@ -168,15 +176,19 @@ ${autoExecContent}`
|
|||||||
const codeFilePath = path.join(session.path, 'code.sas')
|
const codeFilePath = path.join(session.path, 'code.sas')
|
||||||
|
|
||||||
// TODO: don't wait forever
|
// TODO: don't wait forever
|
||||||
while ((await fileExists(codeFilePath)) && !session.crashed) {}
|
while (
|
||||||
|
(await fileExists(codeFilePath)) &&
|
||||||
|
session.state !== SessionState.failed
|
||||||
|
) {}
|
||||||
|
|
||||||
if (session.crashed)
|
if (session.state === SessionState.failed) {
|
||||||
process.logger.error(
|
process.logger.error(
|
||||||
'session crashed! while waiting to be ready',
|
'session crashed! while waiting to be ready',
|
||||||
session.crashed
|
session.failureReason
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
session.ready = true
|
session.state = SessionState.pending
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async deleteSession(session: Session) {
|
private async deleteSession(session: Session) {
|
||||||
@@ -192,14 +204,31 @@ ${autoExecContent}`
|
|||||||
private scheduleSessionDestroy(session: Session) {
|
private scheduleSessionDestroy(session: Session) {
|
||||||
setTimeout(
|
setTimeout(
|
||||||
async () => {
|
async () => {
|
||||||
if (session.inUse) {
|
if (session.state === SessionState.running) {
|
||||||
// adding 10 more minutes
|
// adding 10 more minutes
|
||||||
const newDeathTimeStamp = parseInt(session.deathTimeStamp) + 10 * 1000
|
const newDeathTimeStamp =
|
||||||
|
parseInt(session.deathTimeStamp) + 10 * 60 * 1000
|
||||||
session.deathTimeStamp = newDeathTimeStamp.toString()
|
session.deathTimeStamp = newDeathTimeStamp.toString()
|
||||||
|
|
||||||
this.scheduleSessionDestroy(session)
|
this.scheduleSessionDestroy(session)
|
||||||
} else {
|
} else {
|
||||||
await this.deleteSession(session)
|
const { expiresAfterMins } = session
|
||||||
|
|
||||||
|
// delay session destroy if expiresAfterMins present
|
||||||
|
if (expiresAfterMins && session.state !== SessionState.completed) {
|
||||||
|
// calculate session death time using expiresAfterMins
|
||||||
|
const newDeathTimeStamp =
|
||||||
|
parseInt(session.deathTimeStamp) +
|
||||||
|
expiresAfterMins.mins * 60 * 1000
|
||||||
|
session.deathTimeStamp = newDeathTimeStamp.toString()
|
||||||
|
|
||||||
|
// set expiresAfterMins to true to avoid using it again
|
||||||
|
session.expiresAfterMins!.used = true
|
||||||
|
|
||||||
|
this.scheduleSessionDestroy(session)
|
||||||
|
} else {
|
||||||
|
await this.deleteSession(session)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
parseInt(session.deathTimeStamp) - new Date().getTime() - 100
|
parseInt(session.deathTimeStamp) - new Date().getTime() - 100
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export const createJSProgram = async (
|
|||||||
) => {
|
) => {
|
||||||
const varStatments = Object.keys(vars).reduce(
|
const varStatments = Object.keys(vars).reduce(
|
||||||
(computed: string, key: string) =>
|
(computed: string, key: string) =>
|
||||||
`${computed}const ${key} = '${vars[key]}';\n`,
|
`${computed}const ${key} = \`${vars[key]}\`;\n`,
|
||||||
''
|
''
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { WriteStream, createWriteStream } from 'fs'
|
|||||||
import { execFile } from 'child_process'
|
import { execFile } from 'child_process'
|
||||||
import { once } from 'stream'
|
import { once } from 'stream'
|
||||||
import { createFile, moveFile } from '@sasjs/utils'
|
import { createFile, moveFile } from '@sasjs/utils'
|
||||||
import { PreProgramVars, Session } from '../../types'
|
import { PreProgramVars, Session, SessionState } from '../../types'
|
||||||
import { RunTimeType } from '../../utils'
|
import { RunTimeType } from '../../utils'
|
||||||
import {
|
import {
|
||||||
ExecutionVars,
|
ExecutionVars,
|
||||||
@@ -49,7 +49,7 @@ export const processProgram = async (
|
|||||||
await moveFile(codePath + '.bkp', codePath)
|
await moveFile(codePath + '.bkp', codePath)
|
||||||
|
|
||||||
// we now need to poll the session status
|
// we now need to poll the session status
|
||||||
while (!session.completed) {
|
while (session.state !== SessionState.completed) {
|
||||||
await delay(50)
|
await delay(50)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -114,13 +114,20 @@ export const processProgram = async (
|
|||||||
|
|
||||||
await execFilePromise(executablePath, [codePath], writeStream)
|
await execFilePromise(executablePath, [codePath], writeStream)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
session.completed = true
|
session.state = SessionState.completed
|
||||||
|
|
||||||
process.logger.info('session completed', session)
|
process.logger.info('session completed', session)
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
session.completed = true
|
session.state = SessionState.failed
|
||||||
session.crashed = err.toString()
|
|
||||||
process.logger.error('session crashed', session.id, session.crashed)
|
session.failureReason = err.toString()
|
||||||
|
|
||||||
|
process.logger.error(
|
||||||
|
'session crashed',
|
||||||
|
session.id,
|
||||||
|
session.failureReason
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
// copy the code file to log and end write stream
|
// copy the code file to log and end write stream
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Request, Security, Route, Tags, Example, Get } from 'tsoa'
|
import { Request, Security, Route, Tags, Example, Get } from 'tsoa'
|
||||||
import { UserResponse } from './user'
|
import { UserResponse } from './user'
|
||||||
|
import { getSessionController } from './internal'
|
||||||
|
import { SessionState } from '../types'
|
||||||
|
|
||||||
interface SessionResponse extends UserResponse {
|
interface SessionResponse extends UserResponse {
|
||||||
needsToUpdatePassword: boolean
|
needsToUpdatePassword: boolean
|
||||||
@@ -26,6 +28,18 @@ export class SessionController {
|
|||||||
): Promise<SessionResponse> {
|
): Promise<SessionResponse> {
|
||||||
return session(request)
|
return session(request)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The polling endpoint is currently implemented for single-server deployments only.<br>
|
||||||
|
* Load balanced / grid topologies will be supported in a future release.<br>
|
||||||
|
* If your site requires this, please reach out to SASjs Support.
|
||||||
|
* @summary Get session state (initialising, pending, running, completed, failed).
|
||||||
|
* @example completed
|
||||||
|
*/
|
||||||
|
@Get('/:sessionId/state')
|
||||||
|
public async sessionState(sessionId: string): Promise<SessionState> {
|
||||||
|
return sessionState(sessionId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = (req: express.Request) => ({
|
const session = (req: express.Request) => ({
|
||||||
@@ -35,3 +49,23 @@ const session = (req: express.Request) => ({
|
|||||||
isAdmin: req.user!.isAdmin,
|
isAdmin: req.user!.isAdmin,
|
||||||
needsToUpdatePassword: req.user!.needsToUpdatePassword
|
needsToUpdatePassword: req.user!.needsToUpdatePassword
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const sessionState = (sessionId: string): SessionState => {
|
||||||
|
for (let runTime of process.runTimes) {
|
||||||
|
// get session controller for each available runTime
|
||||||
|
const sessionController = getSessionController(runTime)
|
||||||
|
|
||||||
|
// get session by sessionId
|
||||||
|
const session = sessionController.getSessionById(sessionId)
|
||||||
|
|
||||||
|
// return session state if session was found
|
||||||
|
if (session) {
|
||||||
|
return session.state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw {
|
||||||
|
code: 404,
|
||||||
|
message: `Session with ID '${sessionId}' was not found.`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Request, Security, Route, Tags, Post, Body, Get, Query } from 'tsoa'
|
import { Request, Security, Route, Tags, Post, Body, Get, Query } from 'tsoa'
|
||||||
import { ExecutionController, ExecutionVars } from './internal'
|
import {
|
||||||
|
ExecutionController,
|
||||||
|
ExecutionVars,
|
||||||
|
getSessionController
|
||||||
|
} from './internal'
|
||||||
import {
|
import {
|
||||||
getPreProgramVariables,
|
getPreProgramVariables,
|
||||||
makeFilesNamesMap,
|
makeFilesNamesMap,
|
||||||
@@ -16,6 +20,36 @@ interface ExecutePostRequestPayload {
|
|||||||
_program?: string
|
_program?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TriggerProgramPayload {
|
||||||
|
/**
|
||||||
|
* Location of SAS program.
|
||||||
|
* @example "/Public/somefolder/some.file"
|
||||||
|
*/
|
||||||
|
_program: string
|
||||||
|
/**
|
||||||
|
* Amount of minutes after the completion of the program when the session must be
|
||||||
|
* destroyed.
|
||||||
|
* @example 15
|
||||||
|
*/
|
||||||
|
expiresAfterMins?: number
|
||||||
|
/**
|
||||||
|
* Query param for setting debug mode.
|
||||||
|
*/
|
||||||
|
_debug?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TriggerProgramResponse {
|
||||||
|
/**
|
||||||
|
* `sessionId` is the ID of the session and the name of the temporary folder
|
||||||
|
* used to store program outputs.<br><br>
|
||||||
|
* For SAS, this would be the location of the SASWORK folder.<br><br>
|
||||||
|
* `sessionId` can be used to poll session state using the
|
||||||
|
* GET /SASjsApi/session/{sessionId}/state endpoint.
|
||||||
|
* @example "20241028074744-54132-1730101664824"
|
||||||
|
*/
|
||||||
|
sessionId: string
|
||||||
|
}
|
||||||
|
|
||||||
@Security('bearerAuth')
|
@Security('bearerAuth')
|
||||||
@Route('SASjsApi/stp')
|
@Route('SASjsApi/stp')
|
||||||
@Tags('STP')
|
@Tags('STP')
|
||||||
@@ -23,20 +57,30 @@ export class STPController {
|
|||||||
/**
|
/**
|
||||||
* Trigger a Stored Program using the _program URL parameter.
|
* Trigger a Stored Program using the _program URL parameter.
|
||||||
*
|
*
|
||||||
* Accepts URL parameters and file uploads. For more details, see docs:
|
* Accepts additional URL parameters (converted to session variables)
|
||||||
|
* and file uploads. For more details, see docs:
|
||||||
*
|
*
|
||||||
* https://server.sasjs.io/storedprograms
|
* https://server.sasjs.io/storedprograms
|
||||||
*
|
*
|
||||||
* @summary Execute a Stored Program, returns _webout and (optionally) log.
|
* @summary Execute a Stored Program, returns _webout and (optionally) log.
|
||||||
* @param _program Location of code in SASjs Drive
|
* @param _program Location of Stored Program in SASjs Drive.
|
||||||
|
* @param _debug Optional query param for setting debug mode (returns the session log in the response body).
|
||||||
* @example _program "/Projects/myApp/some/program"
|
* @example _program "/Projects/myApp/some/program"
|
||||||
|
* @example _debug 131
|
||||||
*/
|
*/
|
||||||
@Get('/execute')
|
@Get('/execute')
|
||||||
public async executeGetRequest(
|
public async executeGetRequest(
|
||||||
@Request() request: express.Request,
|
@Request() request: express.Request,
|
||||||
@Query() _program: string
|
@Query() _program: string,
|
||||||
|
@Query() _debug?: number
|
||||||
): Promise<string | Buffer> {
|
): Promise<string | Buffer> {
|
||||||
const vars = request.query as ExecutionVars
|
let vars = request.query as ExecutionVars
|
||||||
|
if (_debug) {
|
||||||
|
vars = {
|
||||||
|
...vars,
|
||||||
|
_debug
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return execute(request, _program, vars)
|
return execute(request, _program, vars)
|
||||||
}
|
}
|
||||||
@@ -68,6 +112,26 @@ export class STPController {
|
|||||||
|
|
||||||
return execute(request, program!, vars, otherArgs)
|
return execute(request, program!, vars, otherArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger Program on the Specified Runtime.
|
||||||
|
* @summary Triggers program and returns SessionId immediately - does not wait for program completion.
|
||||||
|
* @param _program Location of code in SASjs Drive.
|
||||||
|
* @param expiresAfterMins Optional query param for setting amount of minutes after the completion of the program when the session must be destroyed.
|
||||||
|
* @param _debug Optional query param for setting debug mode.
|
||||||
|
* @example _program "/Projects/myApp/some/program"
|
||||||
|
* @example _debug 131
|
||||||
|
* @example expiresAfterMins 15
|
||||||
|
*/
|
||||||
|
@Post('/trigger')
|
||||||
|
public async triggerProgram(
|
||||||
|
@Request() request: express.Request,
|
||||||
|
@Query() _program: string,
|
||||||
|
@Query() _debug?: number,
|
||||||
|
@Query() expiresAfterMins?: number
|
||||||
|
): Promise<TriggerProgramResponse> {
|
||||||
|
return triggerProgram(request, { _program, _debug, expiresAfterMins })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const execute = async (
|
const execute = async (
|
||||||
@@ -106,3 +170,52 @@ const execute = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const triggerProgram = async (
|
||||||
|
req: express.Request,
|
||||||
|
{ _program, _debug, expiresAfterMins }: TriggerProgramPayload
|
||||||
|
): Promise<TriggerProgramResponse> => {
|
||||||
|
try {
|
||||||
|
// put _program query param into vars object
|
||||||
|
const vars: { [key: string]: string | number } = { _program }
|
||||||
|
|
||||||
|
// if present add _debug query param to vars object
|
||||||
|
if (_debug) {
|
||||||
|
vars._debug = _debug
|
||||||
|
}
|
||||||
|
|
||||||
|
// get code path and runTime
|
||||||
|
const { codePath, runTime } = await getRunTimeAndFilePath(_program)
|
||||||
|
|
||||||
|
// get session controller based on runTime
|
||||||
|
const sessionController = getSessionController(runTime)
|
||||||
|
|
||||||
|
// get session
|
||||||
|
const session = await sessionController.getSession()
|
||||||
|
|
||||||
|
// add expiresAfterMins to session if provided
|
||||||
|
if (expiresAfterMins) {
|
||||||
|
// expiresAfterMins.used is set initially to false
|
||||||
|
session.expiresAfterMins = { mins: expiresAfterMins, used: false }
|
||||||
|
}
|
||||||
|
|
||||||
|
// call executeFile method of ExecutionController without awaiting
|
||||||
|
new ExecutionController().executeFile({
|
||||||
|
programPath: codePath,
|
||||||
|
runTime,
|
||||||
|
preProgramVariables: getPreProgramVariables(req),
|
||||||
|
vars,
|
||||||
|
session
|
||||||
|
})
|
||||||
|
|
||||||
|
// return session id
|
||||||
|
return { sessionId: session.id }
|
||||||
|
} catch (err: any) {
|
||||||
|
throw {
|
||||||
|
code: 400,
|
||||||
|
status: 'failure',
|
||||||
|
message: 'Job execution failed.',
|
||||||
|
error: typeof err === 'object' ? err.toString() : err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -285,7 +285,7 @@ const getUser = async (
|
|||||||
username: user.username,
|
username: user.username,
|
||||||
isActive: user.isActive,
|
isActive: user.isActive,
|
||||||
isAdmin: user.isAdmin,
|
isAdmin: user.isAdmin,
|
||||||
autoExec: getAutoExec ? user.autoExec ?? '' : undefined,
|
autoExec: getAutoExec ? (user.autoExec ?? '') : undefined,
|
||||||
groups: user.groups
|
groups: user.groups
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { runCodeValidation } from '../../utils'
|
import { runCodeValidation, triggerCodeValidation } from '../../utils'
|
||||||
import { CodeController } from '../../controllers/'
|
import { CodeController } from '../../controllers/'
|
||||||
|
|
||||||
const runRouter = express.Router()
|
const runRouter = express.Router()
|
||||||
@@ -28,4 +28,22 @@ runRouter.post('/execute', async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
runRouter.post('/trigger', async (req, res) => {
|
||||||
|
const { error, value: body } = triggerCodeValidation(req.body)
|
||||||
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await controller.triggerCode(req, body)
|
||||||
|
|
||||||
|
res.status(200)
|
||||||
|
res.send(response)
|
||||||
|
} catch (err: any) {
|
||||||
|
const statusCode = err.code
|
||||||
|
|
||||||
|
delete err.code
|
||||||
|
|
||||||
|
res.status(statusCode).send(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
export default runRouter
|
export default runRouter
|
||||||
|
|||||||
@@ -1,16 +1,37 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { SessionController } from '../../controllers'
|
import { SessionController } from '../../controllers'
|
||||||
|
import { sessionIdValidation } from '../../utils'
|
||||||
|
|
||||||
const sessionRouter = express.Router()
|
const sessionRouter = express.Router()
|
||||||
|
|
||||||
|
const controller = new SessionController()
|
||||||
|
|
||||||
sessionRouter.get('/', async (req, res) => {
|
sessionRouter.get('/', async (req, res) => {
|
||||||
const controller = new SessionController()
|
|
||||||
try {
|
try {
|
||||||
const response = await controller.session(req)
|
const response = await controller.session(req)
|
||||||
|
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
sessionRouter.get('/:sessionId/state', async (req, res) => {
|
||||||
|
const { error, value: params } = sessionIdValidation(req.params)
|
||||||
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await controller.sessionState(params.sessionId)
|
||||||
|
|
||||||
|
res.status(200)
|
||||||
|
res.send(response)
|
||||||
|
} catch (err: any) {
|
||||||
|
const statusCode = err.code
|
||||||
|
|
||||||
|
delete err.code
|
||||||
|
|
||||||
|
res.status(statusCode).send(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
export default sessionRouter
|
export default sessionRouter
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import {
|
|||||||
SASSessionController
|
SASSessionController
|
||||||
} from '../../../controllers/internal'
|
} from '../../../controllers/internal'
|
||||||
import * as ProcessProgramModule from '../../../controllers/internal/processProgram'
|
import * as ProcessProgramModule from '../../../controllers/internal/processProgram'
|
||||||
import { Session } from '../../../types'
|
import { Session, SessionState } from '../../../types'
|
||||||
|
|
||||||
const clientId = 'someclientID'
|
const clientId = 'someclientID'
|
||||||
|
|
||||||
@@ -493,10 +493,7 @@ const mockedGetSession = async () => {
|
|||||||
|
|
||||||
const session: Session = {
|
const session: Session = {
|
||||||
id: sessionId,
|
id: sessionId,
|
||||||
ready: true,
|
state: SessionState.pending,
|
||||||
inUse: true,
|
|
||||||
consumed: false,
|
|
||||||
completed: false,
|
|
||||||
creationTimeStamp,
|
creationTimeStamp,
|
||||||
deathTimeStamp,
|
deathTimeStamp,
|
||||||
path: sessionFolder
|
path: sessionFolder
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { executeProgramRawValidation } from '../../utils'
|
import {
|
||||||
|
executeProgramRawValidation,
|
||||||
|
triggerProgramValidation
|
||||||
|
} from '../../utils'
|
||||||
import { STPController } from '../../controllers/'
|
import { STPController } from '../../controllers/'
|
||||||
import { FileUploadController } from '../../controllers/internal'
|
import { FileUploadController } from '../../controllers/internal'
|
||||||
|
|
||||||
@@ -13,7 +16,11 @@ stpRouter.get('/execute', async (req, res) => {
|
|||||||
if (error) return res.status(400).send(error.details[0].message)
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await controller.executeGetRequest(req, query._program)
|
const response = await controller.executeGetRequest(
|
||||||
|
req,
|
||||||
|
query._program,
|
||||||
|
query._debug
|
||||||
|
)
|
||||||
|
|
||||||
if (response instanceof Buffer) {
|
if (response instanceof Buffer) {
|
||||||
res.writeHead(200, (req as any).sasHeaders)
|
res.writeHead(200, (req as any).sasHeaders)
|
||||||
@@ -65,4 +72,28 @@ stpRouter.post(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
stpRouter.post('/trigger', async (req, res) => {
|
||||||
|
const { error, value: query } = triggerProgramValidation(req.query)
|
||||||
|
|
||||||
|
if (error) return res.status(400).send(error.details[0].message)
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await controller.triggerProgram(
|
||||||
|
req,
|
||||||
|
query._program,
|
||||||
|
query._debug,
|
||||||
|
query.expiresAfterMins
|
||||||
|
)
|
||||||
|
|
||||||
|
res.status(200)
|
||||||
|
res.send(response)
|
||||||
|
} catch (err: any) {
|
||||||
|
const statusCode = err.code
|
||||||
|
|
||||||
|
delete err.code
|
||||||
|
|
||||||
|
res.status(statusCode).send(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
export default stpRouter
|
export default stpRouter
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
|
export enum SessionState {
|
||||||
|
initialising = 'initialising', // session is initialising and not ready to be used yet
|
||||||
|
pending = 'pending', // session is ready to be used
|
||||||
|
running = 'running', // session is in use
|
||||||
|
completed = 'completed', // session is completed and can be destroyed
|
||||||
|
failed = 'failed' // session failed
|
||||||
|
}
|
||||||
export interface Session {
|
export interface Session {
|
||||||
id: string
|
id: string
|
||||||
ready: boolean
|
state: SessionState
|
||||||
creationTimeStamp: string
|
creationTimeStamp: string
|
||||||
deathTimeStamp: string
|
deathTimeStamp: string
|
||||||
path: string
|
path: string
|
||||||
inUse: boolean
|
expiresAfterMins?: { mins: number; used: boolean }
|
||||||
consumed: boolean
|
failureReason?: string
|
||||||
completed: boolean
|
|
||||||
crashed?: string
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,31 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { createFolder, getAbsolutePath, getRealPath } from '@sasjs/utils'
|
import {
|
||||||
|
createFolder,
|
||||||
|
getAbsolutePath,
|
||||||
|
getRealPath,
|
||||||
|
fileExists
|
||||||
|
} from '@sasjs/utils'
|
||||||
|
import dotenv from 'dotenv'
|
||||||
import { connectDB, getDesktopFields, ModeType, RunTimeType, SECRETS } from '.'
|
import { connectDB, getDesktopFields, ModeType, RunTimeType, SECRETS } from '.'
|
||||||
|
|
||||||
export const setProcessVariables = async () => {
|
export const setProcessVariables = async () => {
|
||||||
|
const { execPath } = process
|
||||||
|
|
||||||
|
// Check if execPath ends with 'api-macos' to determine executable for MacOS.
|
||||||
|
// This is needed to fix picking .env file issue in MacOS executable.
|
||||||
|
if (execPath) {
|
||||||
|
const envPathSplitted = execPath.split(path.sep)
|
||||||
|
|
||||||
|
if (envPathSplitted.pop() === 'api-macos') {
|
||||||
|
const envPath = path.join(envPathSplitted.join(path.sep), '.env')
|
||||||
|
|
||||||
|
// Override environment variables from envPath if file exists
|
||||||
|
if (await fileExists(envPath)) {
|
||||||
|
dotenv.config({ path: envPath, override: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const { MODE, RUN_TIMES } = process.env
|
const { MODE, RUN_TIMES } = process.env
|
||||||
|
|
||||||
if (MODE === ModeType.Server) {
|
if (MODE === ModeType.Server) {
|
||||||
@@ -21,6 +43,7 @@ export const setProcessVariables = async () => {
|
|||||||
if (process.env.NODE_ENV === 'test') {
|
if (process.env.NODE_ENV === 'test') {
|
||||||
process.sasjsRoot = path.join(process.cwd(), 'sasjs_root')
|
process.sasjsRoot = path.join(process.cwd(), 'sasjs_root')
|
||||||
process.driveLoc = path.join(process.cwd(), 'sasjs_root', 'drive')
|
process.driveLoc = path.join(process.cwd(), 'sasjs_root', 'drive')
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +64,9 @@ export const setProcessVariables = async () => {
|
|||||||
|
|
||||||
const { SASJS_ROOT } = process.env
|
const { SASJS_ROOT } = process.env
|
||||||
const absPath = getAbsolutePath(SASJS_ROOT ?? 'sasjs_root', process.cwd())
|
const absPath = getAbsolutePath(SASJS_ROOT ?? 'sasjs_root', process.cwd())
|
||||||
|
|
||||||
await createFolder(absPath)
|
await createFolder(absPath)
|
||||||
|
|
||||||
process.sasjsRoot = getRealPath(absPath)
|
process.sasjsRoot = getRealPath(absPath)
|
||||||
|
|
||||||
const { DRIVE_LOCATION } = process.env
|
const { DRIVE_LOCATION } = process.env
|
||||||
@@ -49,6 +74,7 @@ export const setProcessVariables = async () => {
|
|||||||
DRIVE_LOCATION ?? path.join(process.sasjsRoot, 'drive'),
|
DRIVE_LOCATION ?? path.join(process.sasjsRoot, 'drive'),
|
||||||
process.cwd()
|
process.cwd()
|
||||||
)
|
)
|
||||||
|
|
||||||
await createFolder(absDrivePath)
|
await createFolder(absDrivePath)
|
||||||
process.driveLoc = getRealPath(absDrivePath)
|
process.driveLoc = getRealPath(absDrivePath)
|
||||||
|
|
||||||
@@ -57,7 +83,9 @@ export const setProcessVariables = async () => {
|
|||||||
LOG_LOCATION ?? path.join(process.sasjsRoot, 'logs'),
|
LOG_LOCATION ?? path.join(process.sasjsRoot, 'logs'),
|
||||||
process.cwd()
|
process.cwd()
|
||||||
)
|
)
|
||||||
|
|
||||||
await createFolder(absLogsPath)
|
await createFolder(absLogsPath)
|
||||||
|
|
||||||
process.logsLoc = getRealPath(absLogsPath)
|
process.logsLoc = getRealPath(absLogsPath)
|
||||||
|
|
||||||
process.logsUUID = 'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784'
|
process.logsUUID = 'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784'
|
||||||
|
|||||||
@@ -51,9 +51,8 @@ export const generateFileUploadSasCode = async (
|
|||||||
let fileCount = 0
|
let fileCount = 0
|
||||||
const uploadedFiles: UploadedFiles[] = []
|
const uploadedFiles: UploadedFiles[] = []
|
||||||
|
|
||||||
const sasSessionFolderList: string[] = await listFilesInFolder(
|
const sasSessionFolderList: string[] =
|
||||||
sasSessionFolder
|
await listFilesInFolder(sasSessionFolder)
|
||||||
)
|
|
||||||
sasSessionFolderList.forEach((fileName) => {
|
sasSessionFolderList.forEach((fileName) => {
|
||||||
let fileCountString = fileCount < 100 ? '0' + fileCount : fileCount
|
let fileCountString = fileCount < 100 ? '0' + fileCount : fileCount
|
||||||
fileCountString = fileCount < 10 ? '00' + fileCount : fileCount
|
fileCountString = fileCount < 10 ? '00' + fileCount : fileCount
|
||||||
|
|||||||
@@ -178,9 +178,31 @@ export const runCodeValidation = (data: any): Joi.ValidationResult =>
|
|||||||
runTime: Joi.string().valid(...process.runTimes)
|
runTime: Joi.string().valid(...process.runTimes)
|
||||||
}).validate(data)
|
}).validate(data)
|
||||||
|
|
||||||
|
export const triggerCodeValidation = (data: any): Joi.ValidationResult =>
|
||||||
|
Joi.object({
|
||||||
|
code: Joi.string().required(),
|
||||||
|
runTime: Joi.string().valid(...process.runTimes),
|
||||||
|
expiresAfterMins: Joi.number().greater(0)
|
||||||
|
}).validate(data)
|
||||||
|
|
||||||
export const executeProgramRawValidation = (data: any): Joi.ValidationResult =>
|
export const executeProgramRawValidation = (data: any): Joi.ValidationResult =>
|
||||||
Joi.object({
|
Joi.object({
|
||||||
_program: Joi.string().required()
|
_program: Joi.string().required(),
|
||||||
|
_debug: Joi.number()
|
||||||
})
|
})
|
||||||
.pattern(/^/, Joi.alternatives(Joi.string(), Joi.number()))
|
.pattern(/^/, Joi.alternatives(Joi.string(), Joi.number()))
|
||||||
.validate(data)
|
.validate(data)
|
||||||
|
|
||||||
|
export const triggerProgramValidation = (data: any): Joi.ValidationResult =>
|
||||||
|
Joi.object({
|
||||||
|
_program: Joi.string().required(),
|
||||||
|
_debug: Joi.number(),
|
||||||
|
expiresAfterMins: Joi.number().greater(0)
|
||||||
|
})
|
||||||
|
.pattern(/^/, Joi.alternatives(Joi.string(), Joi.number()))
|
||||||
|
.validate(data)
|
||||||
|
|
||||||
|
export const sessionIdValidation = (data: any): Joi.ValidationResult =>
|
||||||
|
Joi.object({
|
||||||
|
sessionId: Joi.string().required()
|
||||||
|
}).validate(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user