mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-12 17:54:35 +00:00
Compare commits
18 Commits
callback-v
...
v2.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe907e1c43 | ||
|
|
e95e894365 | ||
|
|
82414d8b8b | ||
|
|
456fa68f0f | ||
|
|
076adc1f6a | ||
|
|
9676488ff2 | ||
|
|
e9affb862d | ||
|
|
e04371510e | ||
|
|
19657a1c12 | ||
|
|
6424c82ac9 | ||
|
|
fcab18191f | ||
|
|
f157612a0e | ||
|
|
b8cb7d52e7 | ||
|
|
d8d1968162 | ||
|
|
0e1d1f1d99 | ||
|
|
0b055dd05f | ||
|
|
ba91c29ba8 | ||
|
|
f8c6318a88 |
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",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^26.0.22",
|
"@types/jest": "^26.0.22",
|
||||||
|
"@types/tough-cookie": "^4.0.0",
|
||||||
"cp": "^0.2.0",
|
"cp": "^0.2.0",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"jest": "^26.6.3",
|
"jest": "^26.6.3",
|
||||||
@@ -51,7 +52,7 @@
|
|||||||
"ts-loader": "^9.1.2",
|
"ts-loader": "^9.1.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.0",
|
||||||
"typedoc-plugin-external-module-name": "^4.0.6",
|
"typedoc-plugin-external-module-name": "^4.0.6",
|
||||||
"typescript": "^3.9.9",
|
"typescript": "^3.9.9",
|
||||||
@@ -60,9 +61,12 @@
|
|||||||
},
|
},
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sasjs/utils": "^2.10.2",
|
"@sasjs/utils": "^2.14.0",
|
||||||
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/SASjs.ts
28
src/SASjs.ts
@@ -10,7 +10,8 @@ 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 = {
|
||||||
@@ -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}`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -833,6 +831,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!
|
||||||
|
|||||||
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 './ComputeJobExecutor'
|
||||||
export * from './JesJobExecutor'
|
export * from './JesJobExecutor'
|
||||||
export * from './JobExecutor'
|
export * from './JobExecutor'
|
||||||
|
export * from './Sas9JobExecutor'
|
||||||
export * from './WebJobExecutor'
|
export * from './WebJobExecutor'
|
||||||
|
|||||||
@@ -44,11 +44,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 +290,7 @@ export class RequestClient implements HttpClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private getHeaders = (
|
protected getHeaders = (
|
||||||
accessToken: string | undefined,
|
accessToken: string | undefined,
|
||||||
contentType: string
|
contentType: string
|
||||||
) => {
|
) => {
|
||||||
@@ -315,7 +315,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 +323,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 +347,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 +405,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 +439,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()
|
||||||
}
|
}
|
||||||
|
|||||||
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)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user