1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-12 09:44:36 +00:00

Compare commits

..

37 Commits

Author SHA1 Message Date
Yury Shkoda
e36cd785e8 Merge pull request #410 from sasjs/macro-vars
feat(variables): added macro variables to executeComputeJob method
2021-06-08 14:50:33 +03:00
Yury Shkoda
bdb1ffb2ef chore(cleanup): removed console.log 2021-06-08 13:40:35 +03:00
Yury Shkoda
84090661cf chore(git): Merge branch 'master' of https://github.com/sasjs/adapter into macro-vars 2021-06-08 13:31:46 +03:00
Yury Shkoda
68e14bbf05 feat(variables): added macro variables to executeComputeJob method 2021-06-08 13:03:02 +03:00
Allan Bowe
e4f23334d3 Merge pull request #407 from sasjs/fix-built-package
fix(build): provide process module for compatibility with browser
2021-06-08 11:03:46 +03:00
Krishna Acondy
5593963b89 fix(build): provide process module for compatibility with browser 2021-06-08 08:42:48 +01:00
Krishna Acondy
83fa82108b Merge pull request #401 from sasjs/dependabot/npm_and_yarn/sasjs/utils-2.17.1
chore(deps): bump @sasjs/utils from 2.10.2 to 2.17.1
2021-06-07 09:08:59 +01:00
dependabot-preview[bot]
4018cf95ba chore(deps): bump @sasjs/utils from 2.10.2 to 2.17.1
Bumps [@sasjs/utils](https://github.com/sasjs/utils) from 2.10.2 to 2.17.1.
- [Release notes](https://github.com/sasjs/utils/releases)
- [Commits](https://github.com/sasjs/utils/compare/v2.10.2...v2.17.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-06-07 07:58:28 +00:00
Krishna Acondy
173b6e3e8d Merge pull request #400 from sasjs/sas9-execute-code
feat(sas9-support): execute arbitrary code on SAS9 servers
2021-06-07 08:56:56 +01:00
Krishna Acondy
0ed5447aff chore(sas9-api): fix filename 2021-06-07 08:45:50 +01:00
Krishna Acondy
6344a906d8 chore(tests): fix tests - remove done callback 2021-06-07 08:37:44 +01:00
Krishna Acondy
2032aacba3 chore(deps): update package versions 2021-06-04 08:59:15 +01:00
Krishna Acondy
fadccfc94c chore(refactor): upgrade utils, refactor to use timestamp generator 2021-06-04 08:40:27 +01:00
Krishna Acondy
551e4e43c1 feat(sas9-support): execute arbitrary code on SAS9 using SASjs runner 2021-06-04 08:37:50 +01:00
3fff4f9c4d Merge pull request #395 from sasjs/makeErr
fix: adding makeErr for SAS 9 in sajss-tests
2021-06-02 15:46:39 +02:00
Allan Bowe
3f119432db fix: adding makeErr for SAS 9 in sajss-tests 2021-06-02 16:44:09 +03:00
Allan Bowe
2147c59314 Merge pull request #388 from sasjs/sas9-auth-error
fix(sas9-support): Throw error when invalid credentials are supplied
2021-05-30 08:51:24 +03:00
Krishna Acondy
b247da249a chore(git-hooks): allow numbers in commit message 2021-05-28 08:52:18 +01:00
Krishna Acondy
e79089b880 fix(sas9-support): throw error with invalid credentials 2021-05-28 08:52:00 +01:00
Krishna Acondy
fe907e1c43 Merge pull request #384 from sasjs/sas9-support
feat(sas9-support): add support for SAS9 job execution outside of the browser
2021-05-28 07:46:47 +01:00
Allan Bowe
e95e894365 Merge branch 'master' into sas9-support 2021-05-27 12:29:30 +03:00
Allan Bowe
82414d8b8b Merge pull request #379 from sasjs/dependabot/npm_and_yarn/sasjs/utils-2.14.0
chore(deps): bump @sasjs/utils from 2.10.2 to 2.14.0
2021-05-27 12:29:13 +03:00
Allan Bowe
456fa68f0f Merge branch 'master' into dependabot/npm_and_yarn/sasjs/utils-2.14.0 2021-05-27 11:55:31 +03:00
Allan Bowe
076adc1f6a Merge pull request #334 from sasjs/dependabot/npm_and_yarn/typedoc-0.20.36
chore(deps-dev): bump typedoc from 0.20.35 to 0.20.36
2021-05-27 11:54:52 +03:00
Krishna Acondy
9676488ff2 chore(refactor): remove unnecessary variables, use jobs path from config 2021-05-27 08:40:50 +01:00
Krishna Acondy
e9affb862d chore(merge): update branch 2021-05-27 08:32:11 +01:00
Krishna Acondy
e04371510e chore(update): update branch with changes from master 2021-05-27 08:30:20 +01:00
dependabot-preview[bot]
19657a1c12 chore(deps-dev): bump typedoc from 0.20.35 to 0.20.36
Bumps [typedoc](https://github.com/TypeStrong/TypeDoc) from 0.20.35 to 0.20.36.
- [Release notes](https://github.com/TypeStrong/TypeDoc/releases)
- [Commits](https://github.com/TypeStrong/TypeDoc/compare/v0.20.35...v0.20.36)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-05-27 07:22:22 +00:00
dependabot-preview[bot]
6424c82ac9 chore(deps): bump @sasjs/utils from 2.10.2 to 2.14.0
Bumps [@sasjs/utils](https://github.com/sasjs/utils) from 2.10.2 to 2.14.0.
- [Release notes](https://github.com/sasjs/utils/releases)
- [Commits](https://github.com/sasjs/utils/compare/v2.10.2...v2.14.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-05-27 07:22:21 +00:00
Allan Bowe
fcab18191f Merge pull request #382 from sasjs/dependabot/npm_and_yarn/browserslist-4.16.6
chore(deps): [security] bump browserslist from 4.16.4 to 4.16.6
2021-05-27 10:20:21 +03:00
Krishna Acondy
f157612a0e Merge branch 'master' into sas9-support 2021-05-27 08:16:49 +01:00
Krishna Acondy
b8cb7d52e7 chore(*): remove unused loader 2021-05-27 08:08:47 +01:00
Krishna Acondy
d8d1968162 chore(*): fix formatting 2021-05-27 08:06:21 +01:00
Krishna Acondy
0e1d1f1d99 chore(dep): remove unused dependency 2021-05-27 08:04:19 +01:00
Krishna Acondy
0b055dd05f feat(sas9-support): add support for SAS9 via username/password login 2021-05-27 08:00:15 +01:00
dependabot-preview[bot]
ba91c29ba8 chore(deps): [security] bump browserslist from 4.16.4 to 4.16.6
Bumps [browserslist](https://github.com/browserslist/browserslist) from 4.16.4 to 4.16.6. **This update includes a security fix.**
- [Release notes](https://github.com/browserslist/browserslist/releases)
- [Changelog](https://github.com/browserslist/browserslist/blob/main/CHANGELOG.md)
- [Commits](https://github.com/browserslist/browserslist/compare/4.16.4...4.16.6)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2021-05-26 08:24:56 +00:00
Krishna Acondy
f8c6318a88 chore(*): attempt SAS9 job executor 2021-05-11 08:15:48 +01:00
18 changed files with 3065 additions and 2376 deletions

View File

@@ -6,7 +6,7 @@ GREEN="\033[1;32m"
# temporary file which holds the message). # temporary file which holds the message).
commit_message=$(cat "$1") commit_message=$(cat "$1")
if (echo "$commit_message" | grep -Eq "^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z \-]+\))?!?: .+$") then if (echo "$commit_message" | grep -Eq "^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9 \-]+\))?!?: .+$") then
echo "${GREEN} ✔ Commit message meets Conventional Commit standards" echo "${GREEN} ✔ Commit message meets Conventional Commit standards"
exit 0 exit 0
fi fi

4860
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -38,31 +38,36 @@
}, },
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/jest": "^26.0.22", "@types/jest": "^26.0.23",
"@types/tough-cookie": "^4.0.0",
"cp": "^0.2.0", "cp": "^0.2.0",
"dotenv": "^8.2.0", "dotenv": "^10.0.0",
"jest": "^26.6.3", "jest": "^27.0.4",
"jest-extended": "^0.11.5", "jest-extended": "^0.11.5",
"path": "^0.12.7", "path": "^0.12.7",
"process": "^0.11.10",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"semantic-release": "^17.4.2", "semantic-release": "^17.4.3",
"terser-webpack-plugin": "^4.2.3", "terser-webpack-plugin": "^5.1.3",
"ts-jest": "^25.5.1", "ts-jest": "^27.0.2",
"ts-loader": "^9.1.2", "ts-loader": "^9.2.2",
"tslint": "^6.1.3", "tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0", "tslint-config-prettier": "^1.18.0",
"typedoc": "^0.20.35", "typedoc": "^0.20.36",
"typedoc-neo-theme": "^1.1.0", "typedoc-neo-theme": "^1.1.1",
"typedoc-plugin-external-module-name": "^4.0.6", "typedoc-plugin-external-module-name": "^4.0.6",
"typescript": "^3.9.9", "typescript": "^4.3.2",
"webpack": "^5.33.2", "webpack": "^5.38.1",
"webpack-cli": "^4.7.0" "webpack-cli": "^4.7.0"
}, },
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"@sasjs/utils": "^2.10.2", "@sasjs/utils": "^2.17.1",
"axios": "^0.21.1", "axios": "^0.21.1",
"axios-cookiejar-support": "^1.0.1",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"https": "^1.0.0" "https": "^1.0.0",
"tough-cookie": "^4.0.0",
"url": "^0.11.0"
} }
} }

View File

@@ -72,6 +72,10 @@ parmcards4;
%webout(CLOSE) %webout(CLOSE)
;;;; ;;;;
%mm_createwebservice(path=/Public/app/common,name=sendArr) %mm_createwebservice(path=/Public/app/common,name=sendArr)
parmcards4;
let he who hath understanding, reckon the number of the beast
;;;;
%mm_createwebservice(path=/Public/app/common,name=makeErr)
``` ```
### SAS Viya ### SAS Viya

View File

@@ -1,4 +1,6 @@
import axios, { AxiosInstance } from 'axios' import { generateTimestamp } from '@sasjs/utils/time'
import * as NodeFormData from 'form-data'
import { Sas9RequestClient } from './request/Sas9RequestClient'
import { isUrl } from './utils' import { isUrl } from './utils'
/** /**
@@ -6,11 +8,11 @@ import { isUrl } from './utils'
* *
*/ */
export class SAS9ApiClient { export class SAS9ApiClient {
private httpClient: AxiosInstance private requestClient: Sas9RequestClient
constructor(private serverUrl: string) { constructor(private serverUrl: string, private jobsPath: string) {
if (serverUrl) isUrl(serverUrl) if (serverUrl) isUrl(serverUrl)
this.httpClient = axios.create({ baseURL: this.serverUrl }) this.requestClient = new Sas9RequestClient(serverUrl, false)
} }
/** /**
@@ -33,27 +35,52 @@ export class SAS9ApiClient {
/** /**
* Executes code on a SAS9 server. * Executes code on a SAS9 server.
* @param linesOfCode - an array of code lines to execute. * @param linesOfCode - an array of code lines to execute.
* @param serverName - the server to execute the code on. * @param userName - the user name to log into the current SAS server.
* @param repositoryName - the repository to execute the code in. * @param password - the password to log into the current SAS server.
*/ */
public async executeScript( public async executeScript(
linesOfCode: string[], linesOfCode: string[],
serverName: string, userName: string,
repositoryName: string password: string
) { ) {
const requestPayload = linesOfCode.join('\n') await this.requestClient.login(userName, password, this.jobsPath)
const executeScriptResponse = await this.httpClient.put( const formData = generateFileUploadForm(linesOfCode.join('\n'))
`/sas/servers/${serverName}/cmd?repositoryName=${repositoryName}`,
`command=${requestPayload}`, const codeInjectorPath = `/User Folders/${userName}/My Folder/sasjs/runner`
{ const contentType =
headers: { 'multipart/form-data; boundary=' + formData.getBoundary()
Accept: 'application/json' const contentLength = formData.getLengthSync()
},
responseType: 'text' const headers = {
'cache-control': 'no-cache',
Accept: '*/*',
'Content-Type': contentType,
'Content-Length': contentLength,
Connection: 'keep-alive'
} }
const storedProcessUrl = `${this.jobsPath}/?${
'_program=' + codeInjectorPath + '&_debug=log'
}`
const response = await this.requestClient.post(
storedProcessUrl,
formData,
undefined,
contentType,
headers
) )
return executeScriptResponse.data return response.result as string
} }
} }
const generateFileUploadForm = (data: any): NodeFormData => {
const formData = new NodeFormData()
const filename = `sasjs-execute-sas9-${generateTimestamp('')}.sas`
formData.append(filename, data, {
filename,
contentType: 'text/plain'
})
return formData
}

View File

@@ -28,7 +28,7 @@ import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
import { Logger, LogLevel } from '@sasjs/utils/logger' import { Logger, LogLevel } from '@sasjs/utils/logger'
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired' import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
import { RequestClient } from './request/RequestClient' import { RequestClient } from './request/RequestClient'
import { SasAuthResponse } from '@sasjs/utils/types' import { SasAuthResponse, MacroVar } from '@sasjs/utils/types'
import { prefixMessage } from '@sasjs/utils/error' import { prefixMessage } from '@sasjs/utils/error'
/** /**
@@ -271,6 +271,7 @@ export class SASViyaApiClient {
* @param waitForResult - when set to true, function will return the session * @param waitForResult - when set to true, function will return the session
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job. * @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
* @param variables - an object that represents macro variables.
*/ */
public async executeScript( public async executeScript(
jobPath: string, jobPath: string,
@@ -282,7 +283,8 @@ export class SASViyaApiClient {
expectWebout = false, expectWebout = false,
waitForResult = true, waitForResult = true,
pollOptions?: PollOptions, pollOptions?: PollOptions,
printPid = false printPid = false,
variables?: MacroVar
): Promise<any> { ): Promise<any> {
try { try {
const headers: any = { const headers: any = {
@@ -356,6 +358,8 @@ export class SASViyaApiClient {
: jobPath : jobPath
} }
if (variables) jobVariables = { ...jobVariables, ...variables }
let files: any[] = [] let files: any[] = []
if (data) { if (data) {
@@ -719,13 +723,11 @@ export class SASViyaApiClient {
let formData let formData
if (typeof FormData === 'undefined') { if (typeof FormData === 'undefined') {
formData = new NodeFormData() formData = new NodeFormData()
formData.append('grant_type', 'authorization_code')
formData.append('code', authCode)
} else { } else {
formData = new FormData() formData = new FormData()
}
formData.append('grant_type', 'authorization_code') formData.append('grant_type', 'authorization_code')
formData.append('code', authCode) formData.append('code', authCode)
}
const authResponse = await this.requestClient const authResponse = await this.requestClient
.post( .post(
@@ -814,6 +816,7 @@ export class SASViyaApiClient {
* @param expectWebout - a boolean indicating whether to expect a _webout response. * @param expectWebout - a boolean indicating whether to expect a _webout response.
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job. * @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
* @param variables - an object that represents macro variables.
*/ */
public async executeComputeJob( public async executeComputeJob(
sasJob: string, sasJob: string,
@@ -824,7 +827,8 @@ export class SASViyaApiClient {
waitForResult = true, waitForResult = true,
expectWebout = false, expectWebout = false,
pollOptions?: PollOptions, pollOptions?: PollOptions,
printPid = false printPid = false,
variables?: MacroVar
) { ) {
if (isRelativePath(sasJob) && !this.rootFolderName) { if (isRelativePath(sasJob) && !this.rootFolderName) {
throw new Error( throw new Error(
@@ -903,7 +907,8 @@ export class SASViyaApiClient {
expectWebout, expectWebout,
waitForResult, waitForResult,
pollOptions, pollOptions,
printPid printPid,
variables
) )
} }

View File

@@ -4,13 +4,14 @@ import { SASViyaApiClient } from './SASViyaApiClient'
import { SAS9ApiClient } from './SAS9ApiClient' import { SAS9ApiClient } from './SAS9ApiClient'
import { FileUploader } from './FileUploader' import { FileUploader } from './FileUploader'
import { AuthManager } from './auth' import { AuthManager } from './auth'
import { ServerType } from '@sasjs/utils/types' import { ServerType, MacroVar } from '@sasjs/utils/types'
import { RequestClient } from './request/RequestClient' import { RequestClient } from './request/RequestClient'
import { import {
JobExecutor, JobExecutor,
WebJobExecutor, WebJobExecutor,
ComputeJobExecutor, ComputeJobExecutor,
JesJobExecutor JesJobExecutor,
Sas9JobExecutor
} from './job-execution' } from './job-execution'
import { ErrorResponse } from './types/errors' import { ErrorResponse } from './types/errors'
@@ -41,6 +42,7 @@ export default class SASjs {
private webJobExecutor: JobExecutor | null = null private webJobExecutor: JobExecutor | null = null
private computeJobExecutor: JobExecutor | null = null private computeJobExecutor: JobExecutor | null = null
private jesJobExecutor: JobExecutor | null = null private jesJobExecutor: JobExecutor | null = null
private sas9JobExecutor: JobExecutor | null = null
constructor(config?: any) { constructor(config?: any) {
this.sasjsConfig = { this.sasjsConfig = {
@@ -57,15 +59,15 @@ export default class SASjs {
public async executeScriptSAS9( public async executeScriptSAS9(
linesOfCode: string[], linesOfCode: string[],
serverName: string, userName: string,
repositoryName: string password: string
) { ) {
this.isMethodSupported('executeScriptSAS9', ServerType.Sas9) this.isMethodSupported('executeScriptSAS9', ServerType.Sas9)
return await this.sas9ApiClient?.executeScript( return await this.sas9ApiClient?.executeScript(
linesOfCode, linesOfCode,
serverName, userName,
repositoryName password
) )
} }
@@ -550,10 +552,7 @@ export default class SASjs {
...this.sasjsConfig, ...this.sasjsConfig,
...config ...config
} }
if (
typeof loginRequiredCallback === 'function' ||
typeof loginRequiredCallback === 'undefined'
) {
if (config.serverType === ServerType.SasViya && config.contextName) { if (config.serverType === ServerType.SasViya && config.contextName) {
if (config.useComputeApi) { if (config.useComputeApi) {
return await this.computeJobExecutor!.execute( return await this.computeJobExecutor!.execute(
@@ -572,6 +571,12 @@ export default class SASjs {
accessToken accessToken
) )
} }
} else if (
config.serverType === ServerType.Sas9 &&
config.username &&
config.password
) {
return await this.sas9JobExecutor!.execute(sasJob, data, config)
} else { } else {
return await this.webJobExecutor!.execute( return await this.webJobExecutor!.execute(
sasJob, sasJob,
@@ -580,13 +585,6 @@ export default class SASjs {
loginRequiredCallback loginRequiredCallback
) )
} }
} else {
return Promise.reject(
new ErrorResponse(
`Invalid loginRequiredCallback parameter was provided. Expected Callback function but found ${typeof loginRequiredCallback}`
)
)
}
} }
/** /**
@@ -626,7 +624,7 @@ export default class SASjs {
) )
sasApiClient.debug = this.sasjsConfig.debug sasApiClient.debug = this.sasjsConfig.debug
} else if (this.sasjsConfig.serverType === ServerType.Sas9) { } else if (this.sasjsConfig.serverType === ServerType.Sas9) {
sasApiClient = new SAS9ApiClient(serverUrl) sasApiClient = new SAS9ApiClient(serverUrl, this.jobsPath)
} }
} else { } else {
let sasClientConfig: any = null let sasClientConfig: any = null
@@ -673,6 +671,7 @@ export default class SASjs {
* @param waitForResult - a boolean that indicates whether the function needs to wait for execution to complete. * @param waitForResult - a boolean that indicates whether the function needs to wait for execution to complete.
* @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }. * @param pollOptions - an object that represents poll interval(milliseconds) and maximum amount of attempts. Object example: { MAX_POLL_COUNT: 24 * 60 * 60, POLL_INTERVAL: 1000 }.
* @param printPid - a boolean that indicates whether the function should print (PID) of the started job. * @param printPid - a boolean that indicates whether the function should print (PID) of the started job.
* @param variables - an object that represents macro variables.
*/ */
public async startComputeJob( public async startComputeJob(
sasJob: string, sasJob: string,
@@ -681,7 +680,8 @@ export default class SASjs {
accessToken?: string, accessToken?: string,
waitForResult?: boolean, waitForResult?: boolean,
pollOptions?: PollOptions, pollOptions?: PollOptions,
printPid = false printPid = false,
variables?: MacroVar
) { ) {
config = { config = {
...this.sasjsConfig, ...this.sasjsConfig,
@@ -704,7 +704,8 @@ export default class SASjs {
!!waitForResult, !!waitForResult,
false, false,
pollOptions, pollOptions,
printPid printPid,
variables
) )
} }
@@ -815,7 +816,11 @@ export default class SASjs {
if (this.sasjsConfig.serverType === ServerType.Sas9) { if (this.sasjsConfig.serverType === ServerType.Sas9) {
if (this.sas9ApiClient) if (this.sas9ApiClient)
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl) this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl)
else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl) else
this.sas9ApiClient = new SAS9ApiClient(
this.sasjsConfig.serverUrl,
this.jobsPath
)
} }
this.fileUploader = new FileUploader( this.fileUploader = new FileUploader(
@@ -833,6 +838,12 @@ export default class SASjs {
this.sasViyaApiClient! this.sasViyaApiClient!
) )
this.sas9JobExecutor = new Sas9JobExecutor(
this.sasjsConfig.serverUrl,
this.sasjsConfig.serverType!,
this.jobsPath
)
this.computeJobExecutor = new ComputeJobExecutor( this.computeJobExecutor = new ComputeJobExecutor(
this.sasjsConfig.serverUrl, this.sasjsConfig.serverUrl,
this.sasViyaApiClient! this.sasViyaApiClient!

View File

@@ -57,7 +57,7 @@ describe('AuthManager', () => {
expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?') expect((authManager as any).logoutUrl).toEqual('/SASLogon/logout?')
}) })
it('should call the auth callback and return when already logged in', async (done) => { it('should call the auth callback and return when already logged in', async () => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -77,10 +77,9 @@ describe('AuthManager', () => {
expect(loginResponse.isLoggedIn).toBeTruthy() expect(loginResponse.isLoggedIn).toBeTruthy()
expect(loginResponse.userName).toEqual(userName) expect(loginResponse.userName).toEqual(userName)
expect(authCallback).toHaveBeenCalledTimes(1) expect(authCallback).toHaveBeenCalledTimes(1)
done()
}) })
it('should post a login request to the server if not logged in', async (done) => { it('should post a login request to the server if not logged in', async () => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -121,10 +120,9 @@ describe('AuthManager', () => {
} }
) )
expect(authCallback).toHaveBeenCalledTimes(1) expect(authCallback).toHaveBeenCalledTimes(1)
done()
}) })
it('should parse and submit the authorisation form when necessary', async (done) => { it('should parse and submit the authorisation form when necessary', async () => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -160,10 +158,9 @@ describe('AuthManager', () => {
expect(requestClient.authorize).toHaveBeenCalledWith( expect(requestClient.authorize).toHaveBeenCalledWith(
mockLoginAuthoriseRequiredResponse mockLoginAuthoriseRequiredResponse
) )
done()
}) })
it('should check and return session information if logged in', async (done) => { it('should check and return session information if logged in', async () => {
const authManager = new AuthManager( const authManager = new AuthManager(
serverUrl, serverUrl,
serverType, serverType,
@@ -189,7 +186,5 @@ describe('AuthManager', () => {
} }
} }
) )
done()
}) })
}) })

View File

@@ -0,0 +1,110 @@
import { ServerType } from '@sasjs/utils/types'
import * as NodeFormData from 'form-data'
import { ErrorResponse } from '../types/errors'
import { convertToCSV, isRelativePath } from '../utils'
import { BaseJobExecutor } from './JobExecutor'
import { Sas9RequestClient } from '../request/Sas9RequestClient'
/**
* Job executor for SAS9 servers for use in Node.js environments.
* Initiates login with the provided username and password from the config
* The cookies are stored in the request client and used in subsequent
* job execution requests.
*/
export class Sas9JobExecutor extends BaseJobExecutor {
private requestClient: Sas9RequestClient
constructor(
serverUrl: string,
serverType: ServerType,
private jobsPath: string
) {
super(serverUrl, serverType)
this.requestClient = new Sas9RequestClient(serverUrl, false)
}
async execute(sasJob: string, data: any, config: any) {
const program = isRelativePath(sasJob)
? config.appLoc
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
: sasJob
: sasJob
let apiUrl = `${config.serverUrl}${this.jobsPath}?${'_program=' + program}`
apiUrl = `${apiUrl}${
config.username && config.password
? '&_username=' + config.username + '&_password=' + config.password
: ''
}`
let requestParams = {
...this.getRequestParams(config)
}
let formData = new NodeFormData()
if (data) {
try {
formData = generateFileUploadForm(formData, data)
} catch (e) {
return Promise.reject(new ErrorResponse(e?.message, e))
}
}
for (const key in requestParams) {
if (requestParams.hasOwnProperty(key)) {
formData.append(key, requestParams[key])
}
}
await this.requestClient.login(
config.username,
config.password,
this.jobsPath
)
const contentType =
data && Object.keys(data).length
? 'multipart/form-data; boundary=' + (formData as any)._boundary
: 'text/plain'
return await this.requestClient!.post(
apiUrl,
formData,
undefined,
contentType,
{
Accept: '*/*',
Connection: 'Keep-Alive'
}
)
}
private getRequestParams(config: any): any {
const requestParams: any = {}
if (config.debug) {
requestParams['_debug'] = 131
}
return requestParams
}
}
const generateFileUploadForm = (
formData: NodeFormData,
data: any
): NodeFormData => {
for (const tableName in data) {
const name = tableName
const csv = convertToCSV(data[tableName])
if (csv === 'ERROR: LARGE STRING LENGTH') {
throw new Error(
'The max length of a string value in SASjs is 32765 characters.'
)
}
formData.append(name, csv, {
filename: `${name}.csv`,
contentType: 'application/csv'
})
}
return formData
}

View File

@@ -1,4 +1,5 @@
export * from './ComputeJobExecutor' export * from './ComputeJobExecutor'
export * from './JesJobExecutor' export * from './JesJobExecutor'
export * from './JobExecutor' export * from './JobExecutor'
export * from './Sas9JobExecutor'
export * from './WebJobExecutor' export * from './WebJobExecutor'

View File

@@ -10,6 +10,7 @@ import {
} from '../types/errors' } from '../types/errors'
import { parseWeboutResponse } from '../utils/parseWeboutResponse' import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { prefixMessage } from '@sasjs/utils/error' import { prefixMessage } from '@sasjs/utils/error'
import { SAS9AuthError } from '../types/errors/SAS9AuthError'
export interface HttpClient { export interface HttpClient {
get<T>( get<T>(
@@ -44,11 +45,11 @@ export interface HttpClient {
} }
export class RequestClient implements HttpClient { export class RequestClient implements HttpClient {
private csrfToken: CsrfToken = { headerName: '', value: '' } protected csrfToken: CsrfToken = { headerName: '', value: '' }
private fileUploadCsrfToken: CsrfToken | undefined protected fileUploadCsrfToken: CsrfToken | undefined
private httpClient: AxiosInstance protected httpClient: AxiosInstance
constructor(private baseUrl: string, allowInsecure = false) { constructor(protected baseUrl: string, allowInsecure = false) {
const https = require('https') const https = require('https')
if (allowInsecure && https.Agent) { if (allowInsecure && https.Agent) {
this.httpClient = axios.create({ this.httpClient = axios.create({
@@ -290,7 +291,7 @@ export class RequestClient implements HttpClient {
}) })
} }
private getHeaders = ( protected getHeaders = (
accessToken: string | undefined, accessToken: string | undefined,
contentType: string contentType: string
) => { ) => {
@@ -315,7 +316,7 @@ export class RequestClient implements HttpClient {
return headers return headers
} }
private parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => { protected parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => {
const token = this.parseCsrfToken(response) const token = this.parseCsrfToken(response)
if (token) { if (token) {
@@ -323,7 +324,7 @@ export class RequestClient implements HttpClient {
} }
} }
private parseAndSetCsrfToken = (response: AxiosResponse) => { protected parseAndSetCsrfToken = (response: AxiosResponse) => {
const token = this.parseCsrfToken(response) const token = this.parseCsrfToken(response)
if (token) { if (token) {
@@ -347,7 +348,7 @@ export class RequestClient implements HttpClient {
} }
} }
private handleError = async ( protected handleError = async (
e: any, e: any,
callback: any, callback: any,
debug: boolean = false debug: boolean = false
@@ -405,7 +406,7 @@ export class RequestClient implements HttpClient {
throw e throw e
} }
private parseResponse<T>(response: AxiosResponse<any>) { protected parseResponse<T>(response: AxiosResponse<any>) {
const etag = response?.headers ? response.headers['etag'] : '' const etag = response?.headers ? response.headers['etag'] : ''
let parsedResponse let parsedResponse
let includeSAS9Log: boolean = false let includeSAS9Log: boolean = false
@@ -439,7 +440,7 @@ export class RequestClient implements HttpClient {
} }
} }
const throwIfError = (response: AxiosResponse) => { export const throwIfError = (response: AxiosResponse) => {
if (response.status === 401) { if (response.status === 401) {
throw new LoginRequiredError() throw new LoginRequiredError()
} }
@@ -470,6 +471,10 @@ const throwIfError = (response: AxiosResponse) => {
throw new AuthorizeError(response.data.message, authorizeRequestUrl) throw new AuthorizeError(response.data.message, authorizeRequestUrl)
} }
if (response.config?.url?.includes('sasAuthError')) {
throw new SAS9AuthError()
}
const error = parseError(response.data as string) const error = parseError(response.data as string)
if (error) { if (error) {

View File

@@ -0,0 +1,121 @@
import { AxiosRequestConfig } from 'axios'
import axiosCookieJarSupport from 'axios-cookiejar-support'
import * as tough from 'tough-cookie'
import { prefixMessage } from '@sasjs/utils/error'
import { RequestClient, throwIfError } from './RequestClient'
/**
* Specific request client for SAS9 in Node.js environments.
* Handles redirects and cookie management.
*/
export class Sas9RequestClient extends RequestClient {
constructor(baseUrl: string, allowInsecure = false) {
super(baseUrl, allowInsecure)
this.httpClient.defaults.maxRedirects = 0
this.httpClient.defaults.validateStatus = (status) =>
status >= 200 && status < 303
if (axiosCookieJarSupport) {
axiosCookieJarSupport(this.httpClient)
this.httpClient.defaults.jar = new tough.CookieJar()
}
}
public async login(username: string, password: string, jobsPath: string) {
const codeInjectorPath = `/User Folders/${username}/My Folder/sasjs/runner`
if (this.httpClient.defaults.jar) {
;(this.httpClient.defaults.jar as tough.CookieJar).removeAllCookies()
await this.get(
`${jobsPath}?_program=${codeInjectorPath}&_username=${username}&_password=${password}`,
undefined,
'text/plain'
)
}
}
public async get<T>(
url: string,
accessToken: string | undefined,
contentType: string = 'application/json',
overrideHeaders: { [key: string]: string | number } = {},
debug: boolean = false
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
...overrideHeaders
}
const requestConfig: AxiosRequestConfig = {
headers,
responseType: contentType === 'text/plain' ? 'text' : 'json',
withCredentials: true
}
if (contentType === 'text/plain') {
requestConfig.transformResponse = undefined
}
return this.httpClient
.get<T>(url, requestConfig)
.then((response) => {
if (response.status === 302) {
return this.get(
response.headers['location'],
accessToken,
contentType
)
}
throwIfError(response)
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(
e,
() =>
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
(err) => {
throw prefixMessage(
err,
'Error while executing handle error callback. '
)
}
),
debug
).catch((err) => {
throw prefixMessage(err, 'Error while handling error. ')
})
})
}
public post<T>(
url: string,
data: any,
accessToken: string | undefined,
contentType = 'application/json',
overrideHeaders: { [key: string]: string | number } = {}
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
...overrideHeaders
}
return this.httpClient
.post<T>(url, data, { headers, withCredentials: true })
.then(async (response) => {
if (response.status === 302) {
return await this.get(
response.headers['location'],
undefined,
contentType,
overrideHeaders
)
}
throwIfError(response)
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(e, () =>
this.post<T>(url, data, accessToken, contentType, overrideHeaders)
)
})
}
}

View File

@@ -1,3 +1,7 @@
/**
* @jest-environment jsdom
*/
import { FileUploader } from '../FileUploader' import { FileUploader } from '../FileUploader'
import { UploadFile } from '../types' import { UploadFile } from '../types'
import { RequestClient } from '../request/RequestClient' import { RequestClient } from '../request/RequestClient'
@@ -35,41 +39,40 @@ describe('FileUploader', () => {
new RequestClient('https://sample.server.com') new RequestClient('https://sample.server.com')
) )
it('should upload successfully', async (done) => { it('should upload successfully', async () => {
const sasJob = 'test/upload' const sasJob = 'test/upload'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.resolve({ data: sampleResponse }) Promise.resolve({ data: sampleResponse })
) )
fileUploader.uploadFile(sasJob, files, params).then((res: any) => { const res = await fileUploader.uploadFile(sasJob, files, params)
expect(res).toEqual(JSON.parse(sampleResponse)) expect(res).toEqual(JSON.parse(sampleResponse))
done()
})
}) })
it('should an error when no files are provided', async (done) => { it('should an error when no files are provided', async () => {
const sasJob = 'test/upload' const sasJob = 'test/upload'
const files: UploadFile[] = [] const files: UploadFile[] = []
const params = { table: 'libtable' } const params = { table: 'libtable' }
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => { const err = await fileUploader
.uploadFile(sasJob, files, params)
.catch((err: any) => err)
expect(err.error.message).toEqual('At least one file must be provided.') expect(err.error.message).toEqual('At least one file must be provided.')
done()
})
}) })
it('should throw an error when no sasJob is provided', async (done) => { it('should throw an error when no sasJob is provided', async () => {
const sasJob = '' const sasJob = ''
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => { const err = await fileUploader
.uploadFile(sasJob, files, params)
.catch((err: any) => err)
expect(err.error.message).toEqual('sasJob must be provided.') expect(err.error.message).toEqual('sasJob must be provided.')
done()
})
}) })
it('should throw an error when login is required', async (done) => { it('should throw an error when login is required', async () => {
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.resolve({ data: '<form action="Logon">' }) Promise.resolve({ data: '<form action="Logon">' })
) )
@@ -77,15 +80,13 @@ describe('FileUploader', () => {
const sasJob = 'test' const sasJob = 'test'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => { const err = await fileUploader
expect(err.error.message).toEqual( .uploadFile(sasJob, files, params)
'You must be logged in to upload a file.' .catch((err: any) => err)
) expect(err.error.message).toEqual('You must be logged in to upload a file.')
done()
})
}) })
it('should throw an error when invalid JSON is returned by the server', async (done) => { it('should throw an error when invalid JSON is returned by the server', async () => {
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.resolve({ data: '{invalid: "json"' }) Promise.resolve({ data: '{invalid: "json"' })
) )
@@ -93,13 +94,13 @@ describe('FileUploader', () => {
const sasJob = 'test' const sasJob = 'test'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => { const err = await fileUploader
.uploadFile(sasJob, files, params)
.catch((err: any) => err)
expect(err.error.message).toEqual('File upload request failed.') expect(err.error.message).toEqual('File upload request failed.')
done()
})
}) })
it('should throw an error when the server request fails', async (done) => { it('should throw an error when the server request fails', async () => {
mockedAxios.post.mockImplementation(() => mockedAxios.post.mockImplementation(() =>
Promise.reject({ data: '{message: "Server error"}' }) Promise.reject({ data: '{message: "Server error"}' })
) )
@@ -107,10 +108,9 @@ describe('FileUploader', () => {
const sasJob = 'test' const sasJob = 'test'
const { files, params } = prepareFilesAndParams() const { files, params } = prepareFilesAndParams()
fileUploader.uploadFile(sasJob, files, params).catch((err: any) => { const err = await fileUploader
.uploadFile(sasJob, files, params)
.catch((err: any) => err)
expect(err.error.message).toEqual('File upload request failed.') expect(err.error.message).toEqual('File upload request failed.')
done()
})
}) })
}) })

View File

@@ -14,7 +14,7 @@ describe('FolderOperations', () => {
beforeEach(() => {}) beforeEach(() => {})
it('should move and rename folder', async (done) => { it('should move and rename folder', async () => {
mockFetchResponse(false) mockFetchResponse(false)
let res: any = await sasViyaApiClient.moveFolder( let res: any = await sasViyaApiClient.moveFolder(
@@ -26,11 +26,9 @@ describe('FolderOperations', () => {
expect(res.folder.name).toEqual('newName') expect(res.folder.name).toEqual('newName')
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder') expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
done()
}) })
it('should move and keep the name of folder', async (done) => { it('should move and keep the name of folder', async () => {
mockFetchResponse(true) mockFetchResponse(true)
let res: any = await sasViyaApiClient.moveFolder( let res: any = await sasViyaApiClient.moveFolder(
@@ -42,11 +40,9 @@ describe('FolderOperations', () => {
expect(res.folder.name).toEqual('oldName') expect(res.folder.name).toEqual('oldName')
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder') expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
done()
}) })
it('should only rename folder', async (done) => { it('should only rename folder', async () => {
mockFetchResponse(false) mockFetchResponse(false)
let res: any = await sasViyaApiClient.moveFolder( let res: any = await sasViyaApiClient.moveFolder(
@@ -58,8 +54,6 @@ describe('FolderOperations', () => {
expect(res.folder.name).toEqual('newName') expect(res.folder.name).toEqual('newName')
expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder') expect(res.folder.parentFolderUri.split('=')[1]).toEqual('/Test/toFolder')
done()
}) })
}) })

View File

@@ -1,6 +1,6 @@
import { parseGeneratedCode } from '../../utils/index' import { parseGeneratedCode } from '../../utils/index'
it('should parse generated code', async (done) => { it('should parse generated code', () => {
expect(sampleResponse).toBeTruthy() expect(sampleResponse).toBeTruthy()
const parsedGeneratedCode = parseGeneratedCode(sampleResponse) const parsedGeneratedCode = parseGeneratedCode(sampleResponse)
@@ -15,8 +15,6 @@ it('should parse generated code', async (done) => {
expect(generatedCodeLines[2].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy() expect(generatedCodeLines[2].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
expect(generatedCodeLines[3].startsWith('MPRINT(MM_WEBRIGHT)')).toBeTruthy() expect(generatedCodeLines[3].startsWith('MPRINT(MM_WEBRIGHT)')).toBeTruthy()
expect(generatedCodeLines[4].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy() expect(generatedCodeLines[4].startsWith('MPRINT(MM_WEBOUT)')).toBeTruthy()
done()
}) })
/* tslint:disable */ /* tslint:disable */

View File

@@ -1,6 +1,6 @@
import { parseSourceCode } from '../../utils/index' import { parseSourceCode } from '../../utils/index'
it('should parse SAS9 source code', async (done) => { it('should parse SAS9 source code', async () => {
expect(sampleResponse).toBeTruthy() expect(sampleResponse).toBeTruthy()
const parsedSourceCode = parseSourceCode(sampleResponse) const parsedSourceCode = parseSourceCode(sampleResponse)
@@ -15,8 +15,6 @@ it('should parse SAS9 source code', async (done) => {
expect(sourceCodeLines[2].startsWith('8')).toBeTruthy() expect(sourceCodeLines[2].startsWith('8')).toBeTruthy()
expect(sourceCodeLines[3].startsWith('9')).toBeTruthy() expect(sourceCodeLines[3].startsWith('9')).toBeTruthy()
expect(sourceCodeLines[4].startsWith('10')).toBeTruthy() expect(sourceCodeLines[4].startsWith('10')).toBeTruthy()
done()
}) })
/* tslint:disable */ /* tslint:disable */

View File

@@ -0,0 +1,9 @@
export class SAS9AuthError extends Error {
constructor() {
super(
'The credentials you provided cannot be authenticated. Please provide a valid set of credentials.'
)
this.name = 'AuthorizeError'
Object.setPrototypeOf(this, SAS9AuthError.prototype)
}
}

View File

@@ -7,11 +7,10 @@ const browserConfig = {
devtool: 'inline-source-map', devtool: 'inline-source-map',
mode: 'production', mode: 'production',
optimization: { optimization: {
minimize: true,
minimizer: [ minimizer: [
new terserPlugin({ new terserPlugin({
cache: true,
parallel: true, parallel: true,
sourceMap: true,
terserOptions: {} terserOptions: {}
}) })
] ]
@@ -41,6 +40,9 @@ const browserConfig = {
filename: null, filename: null,
exclude: [/node_modules/], exclude: [/node_modules/],
test: /\.ts($|\?)/i test: /\.ts($|\?)/i
}),
new webpack.ProvidePlugin({
process: 'process/browser'
}) })
] ]
} }