Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stage cdn url for tvm stage #90

Merged
merged 4 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/impl/AzureBlobFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ const STREAM_BUFFER_SIZE = 4 * 1024 * 1024 // 4 MB
const STREAM_MAX_CONCURRENCY = 10
const AZURE_STORAGE_DOMAIN = 'blob.core.windows.net'
const DEFAULT_CDN_STORAGE_HOST = 'firefly.azureedge.net'
const STAGE_CDN_STORAGE_HOST = 'firefly-stage.azureedge.net'
const PUBLIC_CONTAINER_SUFFIX = '-public'

const {
getCliEnv, /* function */
STAGE_ENV /* string */
} = require('@adobe/aio-lib-env')

// todo move somewhere else
// eslint-disable-next-line jsdoc/require-jsdoc
function lookupMimeType (filePath) {
Expand Down Expand Up @@ -87,6 +93,10 @@ class AzureBlobFiles extends Files {
/** @private */
this.credentials = utils.clone(credentials)

const env = getCliEnv()
/** @private */
this.defaultHostName = env === STAGE_ENV ? STAGE_CDN_STORAGE_HOST : DEFAULT_CDN_STORAGE_HOST

/**
* @private
* @type {azure.ContainerClient}
Expand Down Expand Up @@ -495,17 +505,18 @@ class AzureBlobFiles extends Files {
return azureURL
}

let hostName = DEFAULT_CDN_STORAGE_HOST
let hostNameToUse = this.defaultHostName

if (this.hasOwnCredentials) {
if (this.credentials.hostName) {
hostName = this.credentials.hostName
hostNameToUse = this.credentials.hostName
} else {
return azureURL
}
}

const index = azureURL.indexOf(AZURE_STORAGE_DOMAIN)
return 'https://' + hostName + azureURL.substring(index + AZURE_STORAGE_DOMAIN.length)
return 'https://' + hostNameToUse + azureURL.substring(index + AZURE_STORAGE_DOMAIN.length)
}

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@adobe/aio-lib-core-errors": "^3.0.0",
"@adobe/aio-lib-core-logging": "^1.1.2",
"@adobe/aio-lib-core-tvm": "^2.1.0",
"@adobe/aio-lib-env": "^1.0.0",
"@azure/storage-blob": "^12.3.0",
"@hapi/joi": "^16.1.7",
"@types/hapi__joi": "^16.0.1",
Expand Down
21 changes: 21 additions & 0 deletions test/impl/AzureBlobFiles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const fakeResponse = jest.fn()

jest.mock('uuid', () => ({ v4: () => 'fake-uuid' }))

const libEnv = require('@adobe/aio-lib-env')
jest.mock('@adobe/aio-lib-env')

const { AzureBlobFiles } = require('../../lib/impl/AzureBlobFiles')
const stream = require('stream')
const cloneDeep = require('lodash.clonedeep')
Expand Down Expand Up @@ -695,6 +698,24 @@ describe('_getUrl', () => {
const url = files._getUrl('fakepath')
expect(url).toEqual(expectedUrl)
})

test('url for stage', async () => {
libEnv.getCliEnv.mockReturnValue('stage')
const files = await AzureBlobFiles.init({ ...fakeUserCredentials }, tvm)

const expectedUrl = 'https://firefly-stage.azureedge.net/fake/fakesub/afile'
const url = files._getUrl('fakepath')
expect(url).toEqual(expectedUrl)
})

test('url for invalid env should default to prod', async () => {
libEnv.getCliEnv.mockReturnValue('stage1')
const files = await AzureBlobFiles.init({ ...fakeUserCredentials }, tvm)

const expectedUrl = 'https://firefly.azureedge.net/fake/fakesub/afile'
const url = files._getUrl('fakepath')
expect(url).toEqual(expectedUrl)
})
})

describe('_getPresignUrl', () => {
Expand Down