All files / src env.ts

90.24% Statements 37/41
100% Branches 18/18
85.71% Functions 12/14
90.24% Lines 37/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                      5x                         20x 47x         20x 8x       5x 5x 5x     20x 20x     5x 5x 5x     20x 20x     5x 5x 5x   5x 5x 5x   5x 5x 2x   5x 2x   5x 5x 2x   5x       7x               20x         20x 20x           20x 20x         20x                  
import { getConfigStore, getSecretStore } from './utils/getStore'
 
export type IntegrationEnv = {
  AGENT_SCRIPT_DOWNLOAD_PATH: string | null
  GET_RESULT_PATH: string | null
  PROXY_SECRET: string | null
  OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED: string | null
  DECRYPTION_KEY: string | null
  SAVE_TO_KV_STORE_PLUGIN_ENABLED: string | null
}
 
const Defaults: IntegrationEnv = {
  AGENT_SCRIPT_DOWNLOAD_PATH: 'agent',
  GET_RESULT_PATH: 'result',
  PROXY_SECRET: null,
  OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED: 'false',
  DECRYPTION_KEY: null,
  SAVE_TO_KV_STORE_PLUGIN_ENABLED: 'false',
}
 
function getVarOrDefault(
  variable: keyof IntegrationEnv,
  defaults: IntegrationEnv
): (env: IntegrationEnv) => string | null {
  return function (env: IntegrationEnv): string | null {
    return (env[variable] || defaults[variable]) as string | null
  }
}
 
function isVarSet(variable: keyof IntegrationEnv): (env: IntegrationEnv) => boolean {
  return function (env: IntegrationEnv): boolean {
    return Boolean(env[variable]?.trim())
  }
}
 
export const agentScriptDownloadPathVarName = 'AGENT_SCRIPT_DOWNLOAD_PATH'
const getAgentPathVar = getVarOrDefault(agentScriptDownloadPathVarName, Defaults)
export const isScriptDownloadPathSet = isVarSet(agentScriptDownloadPathVarName)
 
export function getScriptDownloadPath(env: IntegrationEnv): string {
  const agentPathVar = getAgentPathVar(env)
  return `/${agentPathVar}`
}
 
export const getResultPathVarName = 'GET_RESULT_PATH'
const getGetResultPathVar = getVarOrDefault(getResultPathVarName, Defaults)
export const isGetResultPathSet = isVarSet(getResultPathVarName)
 
export function getGetResultPath(env: IntegrationEnv): string {
  const getResultPathVar = getGetResultPathVar(env)
  return `/${getResultPathVar}(/.*)?`
}
 
export const proxySecretVarName = 'PROXY_SECRET'
const getProxySecretVar = getVarOrDefault(proxySecretVarName, Defaults)
export const isProxySecretSet = isVarSet(proxySecretVarName)
 
export const decryptionKeyVarName = 'DECRYPTION_KEY'
const getDecryptionKeyVar = getVarOrDefault(decryptionKeyVarName, Defaults)
export const isDecryptionKeySet = isVarSet(decryptionKeyVarName)
 
export const openClientResponseVarName = 'OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED'
export const isOpenClientResponseSet = (env: IntegrationEnv) =>
  env.OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED === 'true' || env.OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED === 'false'
 
export const isOpenClientResponseEnabled = (env: IntegrationEnv) =>
  env[openClientResponseVarName]?.toLowerCase() === 'true'
 
export const saveToKvStorePluginEnabledVarName = 'SAVE_TO_KV_STORE_PLUGIN_ENABLED'
export const isSaveToKvStorePluginEnabledSet = (env: IntegrationEnv) =>
  env.SAVE_TO_KV_STORE_PLUGIN_ENABLED === 'true' || env.SAVE_TO_KV_STORE_PLUGIN_ENABLED === 'false'
 
export const isSaveToKvStorePluginEnabled = (env: IntegrationEnv) =>
  env[saveToKvStorePluginEnabledVarName]?.toLowerCase() === 'true'
 
export function getProxySecret(env: IntegrationEnv): string | null {
  return getProxySecretVar(env)
}
 
export function getDecryptionKey(env: IntegrationEnv): string | null {
  return getDecryptionKeyVar(env)
}
 
export function getStatusPagePath(): string {
  return `/status`
}
 
export async function getEnvObject(): Promise<IntegrationEnv> {
  let configStore
  try {
    configStore = getConfigStore()
  } catch (e) {
    console.error(e)
  }
 
  let secretStore
  try {
    secretStore = getSecretStore()
  } catch (e) {
    console.error(e)
  }
 
  return {
    AGENT_SCRIPT_DOWNLOAD_PATH: configStore?.get(agentScriptDownloadPathVarName) ?? null,
    GET_RESULT_PATH: configStore?.get(getResultPathVarName) ?? null,
    OPEN_CLIENT_RESPONSE_PLUGINS_ENABLED: configStore?.get(openClientResponseVarName) ?? null,
    SAVE_TO_KV_STORE_PLUGIN_ENABLED: configStore?.get(saveToKvStorePluginEnabledVarName) ?? null,
    PROXY_SECRET: (await secretStore?.get(proxySecretVarName))?.plaintext() ?? null,
    DECRYPTION_KEY: (await secretStore?.get(decryptionKeyVarName))?.plaintext() ?? null,
  }
}