# All files / src/profiles profileZoneFiles.js

60.78% Statements 31/51
50% Branches 11/22
87.5% Functions 7/8
60.78% Lines 31/51

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

    4x

    4x
    4x
    4x
    4x

    4x

    4x

    4x

    6x

    6x

    6x

    6x

    6x

    6x

    6x

    6x

    5x
    5x
    5x
    5x
    5x

    5x
    5x
    5x

    5x
    5x
    5x
    5x

    5x
    5x
    5x

     import { makeZoneFile, parseZoneFile } from 'zone-file'
    import { extractProfile } from './profileTokens'
    import { Person } from './index'

    import { Logger } from '../logger'

    export function makeProfileZoneFile(origin, tokenFileUrl) {
      Iif (tokenFileUrl.indexOf('://') < 0) {
        throw new Error('Invalid token file url')
      }

      const urlScheme = tokenFileUrl.split('://')[0]
      const urlParts = tokenFileUrl.split('://')[1].split('/')
      const domain = urlParts[0]
      const pathname = `/${urlParts.slice(1).join('/')}`

      const zoneFile = {
        $origin: origin,
        $ttl: 3600,
        uri: [
          {
            name: '_http._tcp',
            priority: 10,
            weight: 1,
            target: `${urlScheme}://${domain}${pathname}`
          }
        ]
      }

      const zoneFileTemplate = '{$origin}\n{$ttl}\n{uri}\n'

      return makeZoneFile(zoneFile, zoneFileTemplate)
    }

    export function getTokenFileUrl(zoneFileJson) {
      Iif (!zoneFileJson.hasOwnProperty('uri')) {
        return null
      }
      Iif (!Array.isArray(zoneFileJson.uri)) {
        return null
      }
      Iif (zoneFileJson.uri.length < 1) {
        return null
      }
      const firstUriRecord = zoneFileJson.uri[0]

      Iif (!firstUriRecord.hasOwnProperty('target')) {
        return null
      }
      let tokenFileUrl = firstUriRecord.target

      Eif (tokenFileUrl.startsWith('https')) {
        // pass
      } else if (tokenFileUrl.startsWith('http')) {
        // pass
      } else {
        tokenFileUrl = `https://${tokenFileUrl}`
      }

      return tokenFileUrl
    }

    export function resolveZoneFileToProfile(zoneFile, publicKeyOrAddress) {
      return new Promise((resolve, reject) => {
        let zoneFileJson = null
        try {
          zoneFileJson = parseZoneFile(zoneFile)
          Iif (!zoneFileJson.hasOwnProperty('$origin')) {
            zoneFileJson = null
          }
        } catch (e) {
          reject(e)
        }

        let tokenFileUrl = null
        Eif (zoneFileJson && Object.keys(zoneFileJson).length > 0) {
          tokenFileUrl = getTokenFileUrl(zoneFileJson)
        } else {
          let profile = null
          try {
            profile = JSON.parse(zoneFile)
            profile = Person.fromLegacyFormat(profile).profile()
          } catch (error) {
            reject(error)
          }
          resolve(profile)
          return
        }

        Eif (tokenFileUrl) {
          fetch(tokenFileUrl)
            .then(response => response.text())
            .then(responseText => JSON.parse(responseText))
            .then((responseJson) => {
              const tokenRecords = responseJson
              const profile = extractProfile(tokenRecords[0].token, publicKeyOrAddress)
              resolve(profile)
            })
            .catch((error) => {
              Logger.error(`resolveZoneFileToProfile: error fetching token file ${tokenFileUrl}`, error)
              reject(error)
            })
        } else {
          Logger.debug('Token file url not found. Resolving to blank profile.')
          resolve({})
        }
      })
    }