All files / src env.ts

90.38% Statements 47/52
100% Branches 18/18
100% Functions 15/15
90.38% Lines 47/52

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128                      6x                         24x 67x         24x 41x       6x 6x 6x     29x 29x     6x 6x 6x     29x 29x     6x 6x 6x   6x 6x 6x   6x 6x 11x   6x 11x   6x 6x 11x   6x 2x     7x       2x       29x       1x 1x 1x 1x 1x 1x 1x 1x                   29x 29x           29x 29x         29x                  
import { getBuiltinKVStore, 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 checkKVStoreAvailability() {
  try {
    const kvStore = getBuiltinKVStore()
    const testKeyName = 'kvStoreCheck'
    await kvStore.put(testKeyName, 'true')
    const entry = await kvStore.get(testKeyName)
    const value = await entry?.text()
    await kvStore.delete(testKeyName)
    return value === 'true'
  } catch (e) {
    console.error('Error checking KV store availability:')
    console.error(e)
    return false
  }
}
 
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,
  }
}