mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 17:34:34 +00:00
Compare commits
21 Commits
callback-v
...
v2.4.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2147c59314 | ||
|
|
b247da249a | ||
|
|
e79089b880 | ||
|
|
fe907e1c43 | ||
|
|
e95e894365 | ||
|
|
82414d8b8b | ||
|
|
456fa68f0f | ||
|
|
076adc1f6a | ||
|
|
9676488ff2 | ||
|
|
e9affb862d | ||
|
|
e04371510e | ||
|
|
19657a1c12 | ||
|
|
6424c82ac9 | ||
|
|
fcab18191f | ||
|
|
f157612a0e | ||
|
|
b8cb7d52e7 | ||
|
|
d8d1968162 | ||
|
|
0e1d1f1d99 | ||
|
|
0b055dd05f | ||
|
|
ba91c29ba8 | ||
|
|
f8c6318a88 |
@@ -6,7 +6,7 @@ GREEN="\033[1;32m"
|
||||
# temporary file which holds the message).
|
||||
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"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
13655
package-lock.json
generated
13655
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -39,6 +39,7 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^26.0.22",
|
||||
"@types/tough-cookie": "^4.0.0",
|
||||
"cp": "^0.2.0",
|
||||
"dotenv": "^8.2.0",
|
||||
"jest": "^26.6.3",
|
||||
@@ -51,7 +52,7 @@
|
||||
"ts-loader": "^9.1.2",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0",
|
||||
"typedoc": "^0.20.35",
|
||||
"typedoc": "^0.20.36",
|
||||
"typedoc-neo-theme": "^1.1.0",
|
||||
"typedoc-plugin-external-module-name": "^4.0.6",
|
||||
"typescript": "^3.9.9",
|
||||
@@ -60,9 +61,12 @@
|
||||
},
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"@sasjs/utils": "^2.10.2",
|
||||
"@sasjs/utils": "^2.14.0",
|
||||
"axios": "^0.21.1",
|
||||
"axios-cookiejar-support": "^1.0.1",
|
||||
"form-data": "^4.0.0",
|
||||
"https": "^1.0.0"
|
||||
"https": "^1.0.0",
|
||||
"tough-cookie": "^4.0.0",
|
||||
"url": "^0.11.0"
|
||||
}
|
||||
}
|
||||
|
||||
16
src/SASjs.ts
16
src/SASjs.ts
@@ -10,7 +10,8 @@ import {
|
||||
JobExecutor,
|
||||
WebJobExecutor,
|
||||
ComputeJobExecutor,
|
||||
JesJobExecutor
|
||||
JesJobExecutor,
|
||||
Sas9JobExecutor
|
||||
} from './job-execution'
|
||||
import { ErrorResponse } from './types/errors'
|
||||
|
||||
@@ -41,6 +42,7 @@ export default class SASjs {
|
||||
private webJobExecutor: JobExecutor | null = null
|
||||
private computeJobExecutor: JobExecutor | null = null
|
||||
private jesJobExecutor: JobExecutor | null = null
|
||||
private sas9JobExecutor: JobExecutor | null = null
|
||||
|
||||
constructor(config?: any) {
|
||||
this.sasjsConfig = {
|
||||
@@ -569,6 +571,12 @@ export default class SASjs {
|
||||
accessToken
|
||||
)
|
||||
}
|
||||
} else if (
|
||||
config.serverType === ServerType.Sas9 &&
|
||||
config.username &&
|
||||
config.password
|
||||
) {
|
||||
return await this.sas9JobExecutor!.execute(sasJob, data, config)
|
||||
} else {
|
||||
return await this.webJobExecutor!.execute(
|
||||
sasJob,
|
||||
@@ -823,6 +831,12 @@ export default class SASjs {
|
||||
this.sasViyaApiClient!
|
||||
)
|
||||
|
||||
this.sas9JobExecutor = new Sas9JobExecutor(
|
||||
this.sasjsConfig.serverUrl,
|
||||
this.sasjsConfig.serverType!,
|
||||
this.jobsPath
|
||||
)
|
||||
|
||||
this.computeJobExecutor = new ComputeJobExecutor(
|
||||
this.sasjsConfig.serverUrl,
|
||||
this.sasViyaApiClient!
|
||||
|
||||
110
src/job-execution/Sas9JobExecutor.ts
Normal file
110
src/job-execution/Sas9JobExecutor.ts
Normal 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
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from './ComputeJobExecutor'
|
||||
export * from './JesJobExecutor'
|
||||
export * from './JobExecutor'
|
||||
export * from './Sas9JobExecutor'
|
||||
export * from './WebJobExecutor'
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
} from '../types/errors'
|
||||
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
||||
import { prefixMessage } from '@sasjs/utils/error'
|
||||
import { SAS9AuthError } from '../types/errors/SAS9AuthError'
|
||||
|
||||
export interface HttpClient {
|
||||
get<T>(
|
||||
@@ -44,11 +45,11 @@ export interface HttpClient {
|
||||
}
|
||||
|
||||
export class RequestClient implements HttpClient {
|
||||
private csrfToken: CsrfToken = { headerName: '', value: '' }
|
||||
private fileUploadCsrfToken: CsrfToken | undefined
|
||||
private httpClient: AxiosInstance
|
||||
protected csrfToken: CsrfToken = { headerName: '', value: '' }
|
||||
protected fileUploadCsrfToken: CsrfToken | undefined
|
||||
protected httpClient: AxiosInstance
|
||||
|
||||
constructor(private baseUrl: string, allowInsecure = false) {
|
||||
constructor(protected baseUrl: string, allowInsecure = false) {
|
||||
const https = require('https')
|
||||
if (allowInsecure && https.Agent) {
|
||||
this.httpClient = axios.create({
|
||||
@@ -290,7 +291,7 @@ export class RequestClient implements HttpClient {
|
||||
})
|
||||
}
|
||||
|
||||
private getHeaders = (
|
||||
protected getHeaders = (
|
||||
accessToken: string | undefined,
|
||||
contentType: string
|
||||
) => {
|
||||
@@ -315,7 +316,7 @@ export class RequestClient implements HttpClient {
|
||||
return headers
|
||||
}
|
||||
|
||||
private parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => {
|
||||
protected parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => {
|
||||
const token = this.parseCsrfToken(response)
|
||||
|
||||
if (token) {
|
||||
@@ -323,7 +324,7 @@ export class RequestClient implements HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
private parseAndSetCsrfToken = (response: AxiosResponse) => {
|
||||
protected parseAndSetCsrfToken = (response: AxiosResponse) => {
|
||||
const token = this.parseCsrfToken(response)
|
||||
|
||||
if (token) {
|
||||
@@ -347,7 +348,7 @@ export class RequestClient implements HttpClient {
|
||||
}
|
||||
}
|
||||
|
||||
private handleError = async (
|
||||
protected handleError = async (
|
||||
e: any,
|
||||
callback: any,
|
||||
debug: boolean = false
|
||||
@@ -405,7 +406,7 @@ export class RequestClient implements HttpClient {
|
||||
throw e
|
||||
}
|
||||
|
||||
private parseResponse<T>(response: AxiosResponse<any>) {
|
||||
protected parseResponse<T>(response: AxiosResponse<any>) {
|
||||
const etag = response?.headers ? response.headers['etag'] : ''
|
||||
let parsedResponse
|
||||
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) {
|
||||
throw new LoginRequiredError()
|
||||
}
|
||||
@@ -470,6 +471,10 @@ const throwIfError = (response: AxiosResponse) => {
|
||||
throw new AuthorizeError(response.data.message, authorizeRequestUrl)
|
||||
}
|
||||
|
||||
if (response.config?.url?.includes('sasAuthError')) {
|
||||
throw new SAS9AuthError()
|
||||
}
|
||||
|
||||
const error = parseError(response.data as string)
|
||||
|
||||
if (error) {
|
||||
|
||||
121
src/request/Sas9RequestClient.ts
Normal file
121
src/request/Sas9RequestClient.ts
Normal 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)
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
9
src/types/errors/SAS9AuthError.ts
Normal file
9
src/types/errors/SAS9AuthError.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user