mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-14 15:40:06 +00:00
docs(session-manager): added method descriptions
This commit is contained in:
@@ -21,6 +21,7 @@ export class SessionManager {
|
|||||||
if (serverUrl) isUrl(serverUrl)
|
if (serverUrl) isUrl(serverUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// INFO: session pool
|
||||||
private sessions: Session[] = []
|
private sessions: Session[] = []
|
||||||
private currentContext: Context | null = null
|
private currentContext: Context | null = null
|
||||||
private settingContext: boolean = false
|
private settingContext: boolean = false
|
||||||
@@ -38,7 +39,12 @@ export class SessionManager {
|
|||||||
this._debug = value
|
this._debug = value
|
||||||
}
|
}
|
||||||
|
|
||||||
private isSessionValid(session: Session) {
|
/**
|
||||||
|
* Checks if session is valid. Session is considered valid if time since it's creation is less than 'sessionInactiveTimeout' attribute.
|
||||||
|
* @param session - session object.
|
||||||
|
* @returns - boolean indicating if session is valid.
|
||||||
|
*/
|
||||||
|
private isSessionValid(session: Session): boolean {
|
||||||
if (!session) return false
|
if (!session) return false
|
||||||
|
|
||||||
const secondsSinceSessionCreation =
|
const secondsSinceSessionCreation =
|
||||||
@@ -55,17 +61,33 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private removeSessionFromPull(session: Session) {
|
/**
|
||||||
|
* Removes session from pool of hot sessions.
|
||||||
|
* @param session - session object.
|
||||||
|
* @returns - void.
|
||||||
|
*/
|
||||||
|
private removeSessionFromPool(session: Session): void {
|
||||||
this.sessions = this.sessions.filter((ses) => ses.id !== session.id)
|
this.sessions = this.sessions.filter((ses) => ses.id !== session.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
private removeExpiredSessions() {
|
/**
|
||||||
|
* Filters session pool to remain only valid sessions.
|
||||||
|
* @param session - session object.
|
||||||
|
* @returns - void.
|
||||||
|
*/
|
||||||
|
private removeExpiredSessions(): void {
|
||||||
this.sessions = this.sessions.filter((session) =>
|
this.sessions = this.sessions.filter((session) =>
|
||||||
this.isSessionValid(session)
|
this.isSessionValid(session)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private throwErrors(errors: (Error | string)[], prefix?: string) {
|
/**
|
||||||
|
* Throws set of errors as a single error.
|
||||||
|
* @param errors - array of errors or string.
|
||||||
|
* @param prefix - an optional final error prefix.
|
||||||
|
* @returns - never.
|
||||||
|
*/
|
||||||
|
private throwErrors(errors: (Error | string)[], prefix?: string): never {
|
||||||
throw prefix
|
throw prefix
|
||||||
? prefixMessage(new Error(errors.join('. ')), prefix)
|
? prefixMessage(new Error(errors.join('. ')), prefix)
|
||||||
: new Error(
|
: new Error(
|
||||||
@@ -77,6 +99,13 @@ export class SessionManager {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns session.
|
||||||
|
* If there is a hot session available, it will be returned immediately and an asynchronous request to create new hot session will be submitted.
|
||||||
|
* If there is no available session, 2 session creation requests will be submitted. The session is returned once it is created and ready.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves with a session.
|
||||||
|
*/
|
||||||
async getSession(accessToken?: string) {
|
async getSession(accessToken?: string) {
|
||||||
const errors: (Error | string)[] = []
|
const errors: (Error | string)[] = []
|
||||||
let isErrorThrown = false
|
let isErrorThrown = false
|
||||||
@@ -94,7 +123,7 @@ export class SessionManager {
|
|||||||
if (this.sessions.length) {
|
if (this.sessions.length) {
|
||||||
const session = this.sessions[0]
|
const session = this.sessions[0]
|
||||||
|
|
||||||
this.removeSessionFromPull(session)
|
this.removeSessionFromPool(session)
|
||||||
|
|
||||||
this.createSessions(accessToken).catch((err) => {
|
this.createSessions(accessToken).catch((err) => {
|
||||||
errors.push(err)
|
errors.push(err)
|
||||||
@@ -120,7 +149,7 @@ export class SessionManager {
|
|||||||
|
|
||||||
const session = this.sessions.pop()!
|
const session = this.sessions.pop()!
|
||||||
|
|
||||||
this.removeSessionFromPull(session)
|
this.removeSessionFromPool(session)
|
||||||
|
|
||||||
throwIfError()
|
throwIfError()
|
||||||
|
|
||||||
@@ -128,19 +157,31 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns error message based on the response from SAS API.
|
||||||
|
* @param err - an optional access token.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - an error message.
|
||||||
|
*/
|
||||||
private getErrorMessage(
|
private getErrorMessage(
|
||||||
err: any,
|
err: ErrorResponse,
|
||||||
url: string,
|
url: string,
|
||||||
method: 'GET' | 'POST' | 'DELETE'
|
method: 'GET' | 'POST' | 'DELETE'
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
`${method} request to ${url} failed with status code ${
|
`${method} request to ${url} failed with status code ${
|
||||||
err?.response?.status || 'unknown'
|
err.response.status || 'unknown'
|
||||||
}. ` + err?.response?.data?.message || ''
|
}. ` + err.response.data.message || ''
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async clearSession(id: string, accessToken?: string) {
|
/**
|
||||||
|
* Deletes session.
|
||||||
|
* @param id - a session id.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves when session is deleted.
|
||||||
|
*/
|
||||||
|
async clearSession(id: string, accessToken?: string): Promise<void> {
|
||||||
const url = `/compute/sessions/${id}`
|
const url = `/compute/sessions/${id}`
|
||||||
|
|
||||||
return await this.requestClient
|
return await this.requestClient
|
||||||
@@ -148,7 +189,7 @@ export class SessionManager {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
this.sessions = this.sessions.filter((s) => s.id !== id)
|
this.sessions = this.sessions.filter((s) => s.id !== id)
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err: ErrorResponse) => {
|
||||||
throw prefixMessage(
|
throw prefixMessage(
|
||||||
this.getErrorMessage(err, url, 'DELETE'),
|
this.getErrorMessage(err, url, 'DELETE'),
|
||||||
'Error while deleting session. '
|
'Error while deleting session. '
|
||||||
@@ -156,7 +197,12 @@ export class SessionManager {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createSessions(accessToken?: string) {
|
/**
|
||||||
|
* Creates sessions in amount equal to MAX_SESSION_COUNT.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves when required amount of sessions is created.
|
||||||
|
*/
|
||||||
|
private async createSessions(accessToken?: string): Promise<void> {
|
||||||
const errors: (Error | string)[] = []
|
const errors: (Error | string)[] = []
|
||||||
|
|
||||||
if (!this.sessions.length) {
|
if (!this.sessions.length) {
|
||||||
@@ -172,6 +218,10 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits for the current context to be set.
|
||||||
|
* @returns - a promise which resolves when current context is set.
|
||||||
|
*/
|
||||||
private async waitForCurrentContext(): Promise<void> {
|
private async waitForCurrentContext(): Promise<void> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const timer = setInterval(() => {
|
const timer = setInterval(() => {
|
||||||
@@ -186,7 +236,14 @@ export class SessionManager {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private async createAndWaitForSession(accessToken?: string) {
|
/**
|
||||||
|
* Creates and waits for session to be ready.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves with a session.
|
||||||
|
*/
|
||||||
|
private async createAndWaitForSession(
|
||||||
|
accessToken?: string
|
||||||
|
): Promise<Session> {
|
||||||
if (!this.currentContext) {
|
if (!this.currentContext) {
|
||||||
if (!this.settingContext) {
|
if (!this.settingContext) {
|
||||||
await this.setCurrentContext(accessToken)
|
await this.setCurrentContext(accessToken)
|
||||||
@@ -215,7 +272,12 @@ export class SessionManager {
|
|||||||
return createdSession
|
return createdSession
|
||||||
}
|
}
|
||||||
|
|
||||||
private async setCurrentContext(accessToken?: string) {
|
/**
|
||||||
|
* Sets current context.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves when current context is set.
|
||||||
|
*/
|
||||||
|
private async setCurrentContext(accessToken?: string): Promise<void> {
|
||||||
if (!this.currentContext) {
|
if (!this.currentContext) {
|
||||||
const url = `${this.serverUrl}/compute/contexts?limit=10000`
|
const url = `${this.serverUrl}/compute/contexts?limit=10000`
|
||||||
|
|
||||||
@@ -253,6 +315,13 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits for session to be ready.
|
||||||
|
* @param session - a session object.
|
||||||
|
* @param etag - an etag that can be a string or null.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves with a session state.
|
||||||
|
*/
|
||||||
private async waitForSession(
|
private async waitForSession(
|
||||||
session: Session,
|
session: Session,
|
||||||
etag: string | null,
|
etag: string | null,
|
||||||
@@ -326,11 +395,21 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets session state.
|
||||||
|
* @param url - a URL to get session state.
|
||||||
|
* @param etag - an etag string.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves with a result string and response status.
|
||||||
|
*/
|
||||||
private async getSessionState(
|
private async getSessionState(
|
||||||
url: string,
|
url: string,
|
||||||
etag: string,
|
etag: string,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
) {
|
): Promise<{
|
||||||
|
result: string
|
||||||
|
responseStatus: number
|
||||||
|
}> {
|
||||||
return await this.requestClient
|
return await this.requestClient
|
||||||
.get(url, accessToken, 'text/plain', { 'If-None-Match': etag })
|
.get(url, accessToken, 'text/plain', { 'If-None-Match': etag })
|
||||||
.then((res) => ({
|
.then((res) => ({
|
||||||
@@ -345,7 +424,22 @@ export class SessionManager {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async getVariable(sessionId: string, variable: string, accessToken?: string) {
|
/**
|
||||||
|
* Gets variable.
|
||||||
|
* @param sessionId - a session id.
|
||||||
|
* @param variable - a variable string.
|
||||||
|
* @param accessToken - an optional access token.
|
||||||
|
* @returns - a promise which resolves with a result that confirms to SessionVariable interface, etag string and status code.
|
||||||
|
*/
|
||||||
|
async getVariable(
|
||||||
|
sessionId: string,
|
||||||
|
variable: string,
|
||||||
|
accessToken?: string
|
||||||
|
): Promise<{
|
||||||
|
result: SessionVariable
|
||||||
|
etag: string
|
||||||
|
status: number
|
||||||
|
}> {
|
||||||
const url = `${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`
|
const url = `${this.serverUrl}/compute/sessions/${sessionId}/variables/${variable}`
|
||||||
|
|
||||||
return await this.requestClient
|
return await this.requestClient
|
||||||
|
|||||||
Reference in New Issue
Block a user