All files / proxy/utils paths.ts

100% Statements 11/11
87.5% Branches 7/8
100% Functions 4/4
100% Lines 11/11

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        5x     5x                       9x 9x 9x   9x 9x                     62x 62x                   62x                     62x    
import { Region } from '../model/index.ts'
import { isTruthy } from './is-truthy.ts'
 
// In V4, the ingress path is the root path
export const V4_INGRESS_PATH = '/.*'
 
// Path used for CDN request in ingress
export const INGRESS_CDN_PATH = 'web'
 
/**
 * Constructs the endpoint URL for the agent based on the provided query parameters.
 *
 * @param {URLSearchParams} params - The query parameters containing information such as `apiKey`, `loaderVersion`, and `version`.
 *                                  - `apiKey`: The API key used to identify the agent (default is an empty string if not provided).
 *                                  - `loaderVersion`: The version of the loader, if provided, it is included in the endpoint path.
 *                                  - `version`: The API version to use; defaults to '3' if not specified.
 * @return {string} The constructed URL for the agent endpoint.
 */
export function getV3AgentPath(params: URLSearchParams): string {
  const apiKey = params.get('apiKey') ?? ''
  const loaderVersion = params.get('loaderVersion')
  const version = params.get('version') ?? '3'
 
  const lv: string = isTruthy(loaderVersion) ? `/loader_v${loaderVersion}.js` : ''
  return `/v${version}/${apiKey}${lv}`
}
 
/**
 * Constructs the full ingress API host URL based on the specified region and base host.
 *
 * @param {Region} region - The region to determine the appropriate API host prefix.
 * @param {string} baseHost - The base host domain of the API.
 * @return {string} The constructed API host URL.
 */
export function getIngressAPIHost(region: Region, baseHost: string): string {
  const prefix = region === Region.us ? '' : `${region}.`
  return `https://${prefix}${baseHost}`
}
 
/**
 * Splits a given URI into its individual path segments, excluding empty segments.
 *
 * @param {string} uri - The URI to be split into segments.
 * @return {string[]} An array of non-empty path segments derived from the URI.
 */
export function getPathSegments(uri: string): string[] {
  return uri.split('/').filter(Boolean)
}
 
/**
 * Extracts the ingress path from the given URI by removing a specified number of path segments from the start.
 *
 * @param {string} uri - The input URI from which the ingress path is extracted.
 * @param {number} behaviorPathNestLevel - The number of nesting levels to remove from the beginning of the path.
 * @return {string} The resulting ingress path segments after removing the specified number of path segments.
 */
export function extractIngressPath(uri: string, behaviorPathNestLevel: number): string[] {
  return getPathSegments(uri).slice(behaviorPathNestLevel)
}