Skip to content

Commit

Permalink
fix: ACNA-2889 - fix endpoints in library (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
shazron committed Mar 26, 2024
1 parent 788d1ed commit 1bc25f7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
40 changes: 35 additions & 5 deletions lib/AdobeState.js
Expand Up @@ -131,11 +131,42 @@ class AdobeState {
/** @private */
this.region = region
/** @private */
this.endpoint = ADOBE_STATE_STORE_ENDPOINT[getCliEnv()]
this.endpoint = this.getRegionalEndpoint(ADOBE_STATE_STORE_ENDPOINT[getCliEnv()], region)
/** @private */
this.fetchRetry = new HttpExponentialBackoff()
}

/**
* Tests if an endpoint is a local endpoint.
*
* @param {string} endpoint the endpoint to test
* @returns {boolean} true if it is a local endpoint
*/
isLocalEndpoint (endpoint) {
return (
endpoint.startsWith('localhost') ||
endpoint.startsWith('127.0.0.1') ||
endpoint.startsWith('host.docker.internal')
)
}

/**
* Gets the regional endpoint for an endpoint.
*
* @param {string} endpoint the endpoint to test
* @param {string} region the region to set
* @returns {string} the endpoint, with the correct region
*/
getRegionalEndpoint (endpoint, region) {
if (this.isLocalEndpoint(endpoint) || region === ADOBE_STATE_STORE_REGIONS[0]) {
return endpoint
}

const pattern = /-amer/gi
const replacement = `-${region}`
return endpoint.replaceAll(pattern, replacement)
}

/**
* Creates a request url.
*
Expand All @@ -145,15 +176,14 @@ class AdobeState {
* @returns {string} the constructed request url
*/
createRequestUrl (key, queryObject = {}) {
const isLocal = this.endpoint.startsWith('localhost') || this.endpoint.startsWith('127.0.0.1') || this.endpoint.startsWith('host.docker.internal')
const isLocal = this.isLocalEndpoint(this.endpoint)
const protocol = isLocal ? 'http' : 'https'
const regionSubdomain = isLocal ? '' : `${this.region}.`
let urlString

if (key) {
urlString = `${protocol}://${regionSubdomain}${this.endpoint}/${API_VERSION}/containers/${this.namespace}/data/${key}`
urlString = `${protocol}://${this.endpoint}/${API_VERSION}/containers/${this.namespace}/data/${key}`
} else {
urlString = `${protocol}://${regionSubdomain}${this.endpoint}/${API_VERSION}/containers/${this.namespace}`
urlString = `${protocol}://${this.endpoint}/${API_VERSION}/containers/${this.namespace}`
}

logger.debug('requestUrl string', urlString)
Expand Down
2 changes: 2 additions & 0 deletions lib/constants.js
Expand Up @@ -15,6 +15,8 @@ const { isInternalToAdobeRuntime } = require('./utils')

// gets these values if the keys are set in the environment, if not it will use the defaults set
// omit the protocol (https)
// the endpoints must have the region encoded as '-region', in this case it is the default region 'amer'
// (see ADOBE_STATE_STORE_REGIONS first element default)
const {
ADOBE_STATE_STORE_ENDPOINT_PROD = 'storage-state-amer.app-builder.adp.adobe.io',
ADOBE_STATE_STORE_ENDPOINT_STAGE = 'storage-state-amer.stg.app-builder.corp.adp.adobe.io',
Expand Down
12 changes: 6 additions & 6 deletions test/AdobeState.test.js
Expand Up @@ -34,8 +34,8 @@ const fakeCredentials = {

const myConstants = {
ADOBE_STATE_STORE_ENDPOINT: {
prod: 'prod-server',
stage: 'stage-server'
prod: 'prod-server-amer',
stage: 'stage-server-amer'
}
}

Expand Down Expand Up @@ -386,7 +386,7 @@ describe('private methods', () => {
const store = await AdobeState.init(fakeCredentials)

const url = store.createRequestUrl()
expect(url).toEqual(`https://${DEFAULT_REGION}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}`)
expect(url).toEqual(`https://prod-server-${DEFAULT_REGION}/${API_VERSION}/containers/${fakeCredentials.namespace}`)
})

test('no params, localhost endpoint', async () => {
Expand Down Expand Up @@ -422,7 +422,7 @@ describe('private methods', () => {
const store = await AdobeState.init(fakeCredentials)

const url = store.createRequestUrl(key)
expect(url).toEqual(`https://${DEFAULT_REGION}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}`)
expect(url).toEqual(`https://stage-server-${DEFAULT_REGION}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}`)
})

test('key set, some query params', async () => {
Expand All @@ -438,7 +438,7 @@ describe('private methods', () => {
const store = await AdobeState.init(fakeCredentials)

const url = store.createRequestUrl(key, queryParams)
expect(url).toEqual(`https://${DEFAULT_REGION}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}?${querystring.stringify(queryParams)}`)
expect(url).toEqual(`https://stage-server-${DEFAULT_REGION}/${API_VERSION}/containers/${fakeCredentials.namespace}/data/${key}?${querystring.stringify(queryParams)}`)
})

test('no params, region set', async () => {
Expand All @@ -450,7 +450,7 @@ describe('private methods', () => {
const store = await AdobeState.init({ ...fakeCredentials, region })

const url = store.createRequestUrl()
expect(url).toEqual(`https://${region}.${myConstants.ADOBE_STATE_STORE_ENDPOINT[env]}/${API_VERSION}/containers/${fakeCredentials.namespace}`)
expect(url).toEqual(`https://prod-server-${region}/${API_VERSION}/containers/${fakeCredentials.namespace}`)
})

test('no params, region invalid', async () => {
Expand Down

0 comments on commit 1bc25f7

Please sign in to comment.