All files / src/utils livekit-region.js

100% Statements 9/9
88.88% Branches 16/18
100% Functions 2/2
100% Lines 9/9

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                    1x                                                                                                                                                       14x 14x 6x   8x       4x 2x           2x             1x  
/**
 * LiveKit multi-region routing.
 *
 * getRegion(req)        - determines region ('asia' or 'eu') from CF-IPCountry header.
 * getRegionConfig(region) - returns { url, apiKey, apiSecret } for the given region.
 *
 * CF-IPCountry is set by Cloudflare on proxied requests. Direct-to-origin
 * requests may not have this header; they default to Asia (Singapore).
 */
 
const EU_COUNTRIES = new Set([
  'GB',
  'DE',
  'FR',
  'IT',
  'ES',
  'NL',
  'BE',
  'AT',
  'CH',
  'PT',
  'IE',
  'LU',
  'MC',
  'LI',
  'SE',
  'NO',
  'DK',
  'FI',
  'IS',
  'PL',
  'CZ',
  'SK',
  'HU',
  'RO',
  'BG',
  'HR',
  'SI',
  'RS',
  'BA',
  'ME',
  'MK',
  'AL',
  'XK',
  'UA',
  'BY',
  'MD',
  'LT',
  'LV',
  'EE',
  'RU',
  'TR',
  'SA',
  'AE',
  'QA',
  'KW',
  'BH',
  'OM',
  'JO',
  'LB',
  'IQ',
  'IL',
  'PS',
  'YE',
  'SY',
  'IR',
  'EG',
  'LY',
  'TN',
  'DZ',
  'MA',
  'ZA',
  'NG',
  'KE',
  'GH',
  'ET',
  'TZ',
  'UG',
  'CI',
  'SN',
  'CM',
  'PK',
  'AF',
]);
 
function getRegion(req) {
  const country = req.headers['cf-ipcountry'];
  if (country && EU_COUNTRIES.has(country)) {
    return 'eu';
  }
  return 'asia';
}
 
function getRegionConfig(region) {
  if (region === 'eu') {
    return {
      url: process.env.LIVEKIT_URL_EU || 'wss://livekit-eu.shytalk.shyden.co.uk',
      apiKey: process.env.LIVEKIT_KEY_EU || process.env.LIVEKIT_API_KEY,
      apiSecret: process.env.LIVEKIT_SECRET_EU || process.env.LIVEKIT_API_SECRET,
    };
  }
  return {
    url: process.env.LIVEKIT_URL_ASIA || 'wss://livekit.shytalk.shyden.co.uk',
    apiKey: process.env.LIVEKIT_KEY_ASIA || process.env.LIVEKIT_API_KEY,
    apiSecret: process.env.LIVEKIT_SECRET_ASIA || process.env.LIVEKIT_API_SECRET,
  };
}
 
module.exports = { getRegion, getRegionConfig };