All files / src/handlers handleStatusPage.ts

100% Statements 49/49
100% Branches 18/18
100% Functions 7/7
100% Lines 49/49

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                          9x 9x 9x 9x 216x   9x       9x 9x 9x       9x       9x               9x               9x 9x 9x 9x   9x 9x   9x 9x 3x         3x 2x           3x 1x             6x             9x 9x 3x           3x 2x           3x 1x           3x         6x           9x       9x                                             9x   9x 9x 9x   9x       9x       10x 1x     9x 9x 9x   9x            
import {
  WorkerEnv,
  isScriptDownloadPathSet,
  isGetResultPathSet,
  isProxySecretSet,
  agentScriptDownloadPathVarName,
  getResultPathVarName,
  proxySecretVarName,
  envHasValidIntegrationPathDepth,
  integrationPathDepthVarName,
} from '../env'
 
function generateNonce() {
  let result = ''
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  const indices = crypto.getRandomValues(new Uint8Array(24))
  for (const index of indices) {
    result += characters[index % characters.length]
  }
  return btoa(result)
}
 
function buildHeaders(styleNonce: string): Headers {
  const headers = new Headers()
  headers.append('Content-Type', 'text/html')
  headers.append(
    'Content-Security-Policy',
    `default-src 'none'; img-src https://fingerprint.com; style-src 'nonce-${styleNonce}'`
  )
  return headers
}
 
function createWorkerVersionElement(): string {
  return `
  <span>
  ℹ️ Worker version: ${__current_worker_version__}
  </span>
  `
}
 
function createContactInformationElement(): string {
  return `
  <span>
  ❓Please contact <a href='mailto:support@fingerprint.com'>support@fingerprint.com</a> with any issues.
  </span>
  `
}
 
function createEnvVarsInformationElement(env: WorkerEnv): string {
  const isScriptDownloadPathAvailable = isScriptDownloadPathSet(env)
  const isGetResultPathAvailable = isGetResultPathSet(env)
  const isProxySecretAvailable = isProxySecretSet(env)
  const isIntegrationPathDepthAvailable = envHasValidIntegrationPathDepth(env)
 
  const isAllRequiredVarsAvailable = isProxySecretAvailable && isIntegrationPathDepthAvailable
  const isAllV3VarsAvailable = isScriptDownloadPathAvailable && isGetResultPathAvailable
 
  let requiredResult = '<h2>Required Variables</h2>'
  if (!isAllRequiredVarsAvailable) {
    requiredResult += `
    <span>
    🚨 The following required environment variables are not defined or invalid:
    </span>
    `
    if (!isProxySecretAvailable) {
      requiredResult += `
      <span>
      πŸ”΄ <strong>${proxySecretVarName} </strong> is not set
      </span>
      `
    }
    if (!isIntegrationPathDepthAvailable) {
      requiredResult += `
      <span>
      πŸ”΄ <strong>${integrationPathDepthVarName} </strong> is not valid. The default value of 1 will be used instead.
      </span>
      `
    }
  } else {
    requiredResult += `
    <span>
     βœ… All required environment variables are set.
    </span>
    `
  }
 
  let v3Result = '<h2>V3 API Variables</h2>'
  if (!isAllV3VarsAvailable) {
    v3Result += `
    <span>
    ⚠️ The following environment variables are not defined or invalid. <br />
    If you are not using the v3 API, these warnings can be safely ignored.
    </span>
    `
    if (!isScriptDownloadPathAvailable) {
      v3Result += `
      <span>
      🟑 <strong>${agentScriptDownloadPathVarName} </strong> is not set
      </span>
      `
    }
    if (!isGetResultPathAvailable) {
      v3Result += `
      <span>
      🟑 <strong>${getResultPathVarName} </strong> is not set
      </span>
      `
    }
    v3Result += `
    <span>
    </span>
    `
  } else {
    v3Result += `
    <span>
    βœ… All v3 API environment variables are set.
    </span>
    `
  }
  return requiredResult + v3Result
}
 
function buildBody(env: WorkerEnv, styleNonce: string): string {
  let body = `
  <html lang='en-US'>
  <head>
    <meta charset='utf-8'/>
    <title>Fingerprint Pro Cloudflare Worker</title>
    <link rel='icon' type='image/x-icon' href='https://fingerprint.com/img/favicon.ico'>
    <style nonce='${styleNonce}'>
      h1, span {
        display: block;
        padding-top: 1em;
        padding-bottom: 1em;
        text-align: center;
      }
      h2 {
        display: block;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h1>Fingerprint Pro Cloudflare Integration</h1>
  `
 
  body += `<span>πŸŽ‰ Your Cloudflare worker is deployed</span>`
 
  body += createWorkerVersionElement()
  body += createEnvVarsInformationElement(env)
  body += createContactInformationElement()
 
  body += `  
  </body>
  </html>
  `
  return body
}
 
export function handleStatusPage(request: Request, env: WorkerEnv): Response {
  if (request.method !== 'GET') {
    return new Response(null, { status: 405 })
  }
 
  const styleNonce = generateNonce()
  const headers = buildHeaders(styleNonce)
  const body = buildBody(env, styleNonce)
 
  return new Response(body, {
    status: 200,
    statusText: 'OK',
    headers,
  })
}