bde148f286
* wip * some extra fixes * stuff kinda works? * need to figure out mocks * need to figure out mocks * fix db listener * gqlgen fix * minor gqlgen watch adjustment * lint fixes * delete old codegen file * converting migrations to ESM * getModuleDIrectory * vitest sort of works * added back ts-vitest * resolve gql double load * fixing test timeout configs * TSC lint fix * fix automate tests * moar debugging * debugging * more debugging * codegen update * server works * yargs migrated * chore(server): getting rid of global mocks for Server ESM (#5046) * got rid of email mock * got rid of comment mocks * got rid of multi region mocks * got rid of stripe mock * admin override mock updated * removed final mock * fixing import.meta.resolve calls * another import.meta.resolve fix * added requested test * nyc ESM fix * removed unneeded deps + linting * yarn lock forgot to commit * tryna fix flakyness * email capture util fix * sendEmail fix * fix TSX check * sender transporter fix + CR comments * merge main fix * test fixx * circleci fix * gqlgen bigint fix * error formatter fix * more error formatting improvements * esmloader added to Dockerfile * more dockerfile fixes * bg jobs fix
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import { GetAvailableRegionConfig } from '@/modules/multiregion/domain/operations'
|
|
import { packageRoot } from '@/bootstrap'
|
|
import path from 'node:path'
|
|
|
|
import {
|
|
getMultiRegionConfigPath,
|
|
isDevOrTestEnv,
|
|
isTestEnv
|
|
} from '@/modules/shared/helpers/envHelper'
|
|
import { type Optional } from '@speckle/shared'
|
|
import { isMultiRegionEnabled } from '@/modules/multiregion/helpers'
|
|
import {
|
|
MainRegionConfig,
|
|
MultiRegionConfig,
|
|
loadMultiRegionsConfig
|
|
} from '@speckle/shared/environment/db'
|
|
import { TestOnlyLogicError } from '@/modules/shared/errors'
|
|
import { PartialDeep } from 'type-fest'
|
|
import { merge } from 'lodash-es'
|
|
|
|
let multiRegionConfig: Optional<MultiRegionConfig> = undefined
|
|
|
|
const emptyConfig = (): MultiRegionConfig => ({
|
|
main: {
|
|
postgres: { connectionUri: '' },
|
|
blobStorage: {
|
|
accessKey: '',
|
|
secretKey: '',
|
|
endpoint: '',
|
|
s3Region: '',
|
|
bucket: '',
|
|
createBucketIfNotExists: true
|
|
}
|
|
},
|
|
regions: {}
|
|
})
|
|
|
|
export const getMultiRegionConfig = async (): Promise<MultiRegionConfig> => {
|
|
// Only for non region enabled dev envs
|
|
const emptyReturn = () => emptyConfig()
|
|
|
|
if (!multiRegionConfig) {
|
|
const relativePath = getMultiRegionConfigPath({ unsafe: isDevOrTestEnv() })
|
|
if (!relativePath) return emptyReturn()
|
|
|
|
const configPath = path.resolve(packageRoot, relativePath)
|
|
|
|
try {
|
|
multiRegionConfig = await loadMultiRegionsConfig({
|
|
path: configPath
|
|
})
|
|
} catch (e) {
|
|
if (isDevOrTestEnv() && !isMultiRegionEnabled()) {
|
|
return emptyReturn()
|
|
} else {
|
|
throw e
|
|
}
|
|
}
|
|
}
|
|
|
|
return multiRegionConfig
|
|
}
|
|
|
|
export const getMainRegionConfig = async (): Promise<MainRegionConfig> => {
|
|
return (await getMultiRegionConfig()).main
|
|
}
|
|
|
|
export const getAvailableRegionConfig: GetAvailableRegionConfig = async () => {
|
|
return (await getMultiRegionConfig()).regions
|
|
}
|
|
|
|
export const getDefaultProjectRegionKey = async (): Promise<string | null> => {
|
|
if (!isMultiRegionEnabled()) return null
|
|
const defaultRegionKey = (await getMultiRegionConfig()).defaultProjectRegionKey
|
|
return defaultRegionKey ?? null
|
|
}
|
|
|
|
export const setMultiRegionConfig = (
|
|
config: Optional<PartialDeep<MultiRegionConfig>>
|
|
) => {
|
|
if (!isTestEnv()) {
|
|
throw new TestOnlyLogicError()
|
|
}
|
|
|
|
multiRegionConfig = config ? merge({}, emptyConfig(), config) : undefined
|
|
}
|