All files / src env.ts

91.37% Statements 53/58
100% Branches 23/23
100% Functions 17/17
91.37% Lines 53/58

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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                        7x                           28x 67x         28x 157x       7x 7x 7x     23x 23x     7x 7x 7x     28x 28x     7x 7x 7x   7x 7x 7x   7x 7x 11x   7x 11x         7x 7x 7x 11x     7x 2x   7x 7x 11x   7x 11x     14x       2x       44x       7x 7x 7x 7x 7x 7x 7x 7x                   44x 44x           44x 44x         44x                          
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_SEALED_RESULT_TO_KV_STORE_PLUGIN_ENABLED: string | null
  SAVE_EVENT_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_SEALED_RESULT_TO_KV_STORE_PLUGIN_ENABLED: 'false',
  SAVE_EVENT_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'
 
/**
 * @deprecated This config store entry will be removed in later versions. Use SAVE_SEALED_RESULT_TO_KV_STORE_PLUGIN_ENABLED key for the entry.
 */
export const saveToKvStorePluginEnabledVarName = 'SAVE_TO_KV_STORE_PLUGIN_ENABLED'
export const saveSealedResultToKvStorePluginEnabledVarName = 'SAVE_SEALED_RESULT_TO_KV_STORE_PLUGIN_ENABLED'
export const isSaveSealedResultToKvStorePluginEnabledSet = (env: IntegrationEnv) =>
  env.SAVE_SEALED_RESULT_TO_KV_STORE_PLUGIN_ENABLED === 'true' ||
  env.SAVE_SEALED_RESULT_TO_KV_STORE_PLUGIN_ENABLED === 'false'
 
export const isSaveSealedResultToKvStorePluginEnabled = (env: IntegrationEnv) =>
  env[saveSealedResultToKvStorePluginEnabledVarName]?.toLowerCase() === 'true'
 
export const saveEventToKvStorePluginEnabledVarName = 'SAVE_EVENT_TO_KV_STORE_PLUGIN_ENABLED'
export const isSaveEventToKvStorePluginEnabledSet = (env: IntegrationEnv) =>
  env.SAVE_EVENT_TO_KV_STORE_PLUGIN_ENABLED === 'true' || env.SAVE_EVENT_TO_KV_STORE_PLUGIN_ENABLED === 'false'
 
export const isSaveEventToKvStorePluginEnabled = (env: IntegrationEnv) =>
  env[saveEventToKvStorePluginEnabledVarName]?.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(storeName: string) {
  try {
    const kvStore = getBuiltinKVStore(storeName)
    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_SEALED_RESULT_TO_KV_STORE_PLUGIN_ENABLED:
      configStore?.get(saveSealedResultToKvStorePluginEnabledVarName) ??
      configStore?.get(saveToKvStorePluginEnabledVarName) ??
      null,
    SAVE_EVENT_TO_KV_STORE_PLUGIN_ENABLED: configStore?.get(saveEventToKvStorePluginEnabledVarName) ?? null,
    PROXY_SECRET: (await secretStore?.get(proxySecretVarName))?.plaintext() ?? null,
    DECRYPTION_KEY: (await secretStore?.get(decryptionKeyVarName))?.plaintext() ?? null,
  }
}