All files / src/profiles profileZoneFiles.ts

60% Statements 27/45
100% Branches 2/2
87.5% Functions 7/8
60% Lines 27/45

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 1x 1x 1x   1x         4x 4x 4x 4x                             4x     4x     1x                   6x         6x                   6x     1x 5x 5x   5x               5x   5x                           5x 5x 5x   5x 5x 5x                        
// @ts-ignore: Could not find a declaration file for module
import { makeZoneFile, parseZoneFile } from 'zone-file'
import { extractProfile } from './profileTokens'
import { Person } from './profileSchemas/person'
import { Logger } from '../logger'
 
export function makeProfileZoneFile(origin: string, tokenFileUrl: string) {
  if (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: any): string | null {
  if (!zoneFileJson.hasOwnProperty('uri')) {
    return null
  }
  if (!Array.isArray(zoneFileJson.uri)) {
    return null
  }
  if (zoneFileJson.uri.length < 1) {
    return null
  }
  const firstUriRecord = zoneFileJson.uri[0]
 
  if (!firstUriRecord.hasOwnProperty('target')) {
    return null
  }
  let tokenFileUrl = firstUriRecord.target
 
  if (tokenFileUrl.startsWith('https')) {
    // pass
  } else if (tokenFileUrl.startsWith('http')) {
    // pass
  } else {
    tokenFileUrl = `https://${tokenFileUrl}`
  }
 
  return tokenFileUrl
}
 
export function resolveZoneFileToProfile(zoneFile: any, publicKeyOrAddress: string) {
  return new Promise((resolve, reject) => {
    let zoneFileJson = null
    try {
      zoneFileJson = parseZoneFile(zoneFile)
      if (!zoneFileJson.hasOwnProperty('$origin')) {
        zoneFileJson = null
      }
    } catch (e) {
      reject(e)
    }
 
    let tokenFileUrl: string | null = null
    if (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
    }
 
    if (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({})
    }
  })
}