mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-19 20:34:36 +00:00
Compare commits
1 Commits
v2.4.0
...
callback-v
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
591e1ebc09 |
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,7 +39,6 @@
|
|||||||
"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",
|
||||||
@@ -52,7 +51,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.36",
|
"typedoc": "^0.20.35",
|
||||||
"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",
|
||||||
@@ -61,12 +60,9 @@
|
|||||||
},
|
},
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sasjs/utils": "^2.14.0",
|
"@sasjs/utils": "^2.10.2",
|
||||||
"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,8 +10,7 @@ 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'
|
||||||
|
|
||||||
@@ -42,7 +41,6 @@ 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 = {
|
||||||
@@ -552,7 +550,10 @@ 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(
|
||||||
@@ -571,12 +572,6 @@ 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,
|
||||||
@@ -585,6 +580,13 @@ export default class SASjs {
|
|||||||
loginRequiredCallback
|
loginRequiredCallback
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return Promise.reject(
|
||||||
|
new ErrorResponse(
|
||||||
|
`Invalid loginRequiredCallback parameter was provided. Expected Callback function but found ${typeof loginRequiredCallback}`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -831,12 +833,6 @@ 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!
|
||||||
|
|||||||
@@ -1,110 +0,0 @@
|
|||||||
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,5 +1,4 @@
|
|||||||
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 {
|
||||||
protected csrfToken: CsrfToken = { headerName: '', value: '' }
|
private csrfToken: CsrfToken = { headerName: '', value: '' }
|
||||||
protected fileUploadCsrfToken: CsrfToken | undefined
|
private fileUploadCsrfToken: CsrfToken | undefined
|
||||||
protected httpClient: AxiosInstance
|
private httpClient: AxiosInstance
|
||||||
|
|
||||||
constructor(protected baseUrl: string, allowInsecure = false) {
|
constructor(private 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 {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
protected getHeaders = (
|
private getHeaders = (
|
||||||
accessToken: string | undefined,
|
accessToken: string | undefined,
|
||||||
contentType: string
|
contentType: string
|
||||||
) => {
|
) => {
|
||||||
@@ -315,7 +315,7 @@ export class RequestClient implements HttpClient {
|
|||||||
return headers
|
return headers
|
||||||
}
|
}
|
||||||
|
|
||||||
protected parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => {
|
private 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 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected parseAndSetCsrfToken = (response: AxiosResponse) => {
|
private 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 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected handleError = async (
|
private 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
|
||||||
}
|
}
|
||||||
|
|
||||||
protected parseResponse<T>(response: AxiosResponse<any>) {
|
private 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 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const throwIfError = (response: AxiosResponse) => {
|
const throwIfError = (response: AxiosResponse) => {
|
||||||
if (response.status === 401) {
|
if (response.status === 401) {
|
||||||
throw new LoginRequiredError()
|
throw new LoginRequiredError()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,121 +0,0 @@
|
|||||||
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