{"version":3,"file":"author-utils.mjs","names":[],"sources":["../src/author-utils.ts"],"sourcesContent":["import _ from 'lodash';\n\nimport type { Author, Manifest } from '@verdaccio/types';\n\nimport { DEFAULT_USER } from './constants';\nimport { stringToMD5 } from './crypto-utils';\n\nexport type AuthorAvatar = Author & { avatar?: string };\n\nexport type AuthorFormat = Author | string | null | void;\n\nconst AVATAR_PROVIDER = 'https://www.gravatar.com/avatar/';\n\nexport const GENERIC_AVATAR =\n  'data:image/svg+xml;utf8,' +\n  encodeURIComponent(\n    '<svg height=\"100\" viewBox=\"-27 24 100 100\" width=\"100\" xmlns=\"http://www.w3.org/' +\n      '2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><circle cx=\"23\" cy=\"7' +\n      '4\" id=\"a\" r=\"50\"/></defs><use fill=\"#F5EEE5\" overflow=\"visible\" xlink:href=\"#a\"/' +\n      '><clipPath id=\"b\"><use overflow=\"visible\" xlink:href=\"#a\"/></clipPath><g clip-pa' +\n      'th=\"url(#b)\"><defs><path d=\"M36 95.9c0 4 4.7 5.2 7.1 5.8 7.6 2 22.8 5.9 22.8 5.9' +\n      ' 3.2 1.1 5.7 3.5 7.1 6.6v9.8H-27v-9.8c1.3-3.1 3.9-5.5 7.1-6.6 0 0 15.2-3.9 22.8-' +\n      '5.9 2.4-.6 7.1-1.8 7.1-5.8V85h26v10.9z\" id=\"c\"/></defs><use fill=\"#E6C19C\" overf' +\n      'low=\"visible\" xlink:href=\"#c\"/><clipPath id=\"d\"><use overflow=\"visible\" xlink:hr' +\n      'ef=\"#c\"/></clipPath><path clip-path=\"url(#d)\" d=\"M23.2 35h.2c3.3 0 8.2.2 11.4 2 ' +\n      '3.3 1.9 7.3 5.6 8.5 12.1 2.4 13.7-2.1 35.4-6.3 42.4-4 6.7-9.8 9.2-13.5 9.4H23h-.' +\n      '1c-3.7-.2-9.5-2.7-13.5-9.4-4.2-7-8.7-28.7-6.3-42.4 1.2-6.5 5.2-10.2 8.5-12.1 3.2' +\n      '-1.8 8.1-2 11.4-2h.2z\" fill=\"#D4B08C\"/></g><path d=\"M22.6 40c19.1 0 20.7 13.8 20' +\n      '.8 15.1 1.1 11.9-3 28.1-6.8 33.7-4 5.9-9.8 8.1-13.5 8.3h-.5c-3.8-.3-9.6-2.5-13.6' +\n      '-8.4-3.8-5.6-7.9-21.8-6.8-33.8C2.3 53.7 3.5 40 22.6 40z\" fill=\"#F2CEA5\"/></svg>'\n  );\n\n/**\n * Formats author field for the web UI.\n * @see https://docs.npmjs.com/files/package.json#author\n */\nexport function formatAuthor(author: AuthorFormat): any {\n  let authorDetails = {\n    name: DEFAULT_USER,\n    email: '',\n    url: '',\n  };\n\n  if (_.isNil(author)) {\n    return authorDetails;\n  }\n\n  if (_.isString(author)) {\n    authorDetails = {\n      ...authorDetails,\n      name: author as string,\n    };\n  }\n\n  if (_.isObject(author)) {\n    authorDetails = {\n      ...authorDetails,\n      ...(author as Author),\n    };\n  }\n\n  return authorDetails;\n}\n\n/**\n * Generate a gravatar url from an email address.\n */\nexport function generateGravatarUrl(email: string | void = '', online: boolean = true): string {\n  if (online && _.isString(email) && _.size(email) > 0) {\n    email = email.trim().toLocaleLowerCase();\n    const emailMD5 = stringToMD5(email);\n    return `${AVATAR_PROVIDER}${emailMD5}`;\n  }\n  return GENERIC_AVATAR;\n}\n\nexport function normalizeContributors(contributors: Author[]): Author[] {\n  if (_.isNil(contributors)) {\n    return [];\n  } else if (contributors && _.isArray(contributors) === false) {\n    return [contributors];\n  } else if (_.isString(contributors)) {\n    return [\n      {\n        name: contributors,\n      },\n    ];\n  }\n\n  return contributors;\n}\n\n/**\n * Add gravatar avatars to the author, contributors and maintainers of a manifest.\n */\nexport function addGravatarSupport(pkgInfo: Manifest, online = true): AuthorAvatar {\n  const pkgInfoCopy = { ...pkgInfo } as any;\n  const author: any = _.get(pkgInfo, 'latest.author', null) as any;\n  const contributors: AuthorAvatar[] = normalizeContributors(\n    _.get(pkgInfo, 'latest.contributors', [])\n  );\n  const maintainers = _.get(pkgInfo, 'latest.maintainers', []);\n\n  // for author.\n  if (author && _.isObject(author)) {\n    const { email } = author as Author;\n    pkgInfoCopy.latest.author.avatar = generateGravatarUrl(email, online);\n  }\n\n  if (author && _.isString(author)) {\n    pkgInfoCopy.latest.author = {\n      avatar: GENERIC_AVATAR,\n      email: '',\n      author,\n    };\n  }\n\n  // for contributors\n  if (_.isEmpty(contributors) === false) {\n    pkgInfoCopy.latest.contributors = contributors.map((contributor): AuthorAvatar => {\n      if (_.isObject(contributor)) {\n        contributor.avatar = generateGravatarUrl(contributor.email, online);\n      } else if (_.isString(contributor)) {\n        contributor = {\n          avatar: GENERIC_AVATAR,\n          email: contributor,\n          name: contributor,\n        };\n      }\n\n      return contributor;\n    });\n  }\n\n  // for maintainers\n  if (_.isEmpty(maintainers) === false) {\n    pkgInfoCopy.latest.maintainers = maintainers.map((maintainer): void => {\n      // @ts-ignore\n      maintainer.avatar = generateGravatarUrl(maintainer.email, online);\n      return maintainer;\n    });\n  }\n\n  return pkgInfoCopy;\n}\n"],"mappings":";;;;;;;;;;;;AAWA,IAAM,kBAAkB;AAExB,IAAa,iBACX,6BACA,mBACE,2pCAcF;;;;;AAMF,SAAgB,aAAa,QAA2B;CACtD,IAAI,gBAAgB;EAClB,MAAM;EACN,OAAO;EACP,KAAK;CACP;CAEA,IAAI,EAAE,MAAM,MAAM,GAChB,OAAO;CAGT,IAAI,EAAE,SAAS,MAAM,GACnB,gBAAgB;EACd,GAAG;EACH,MAAM;CACR;CAGF,IAAI,EAAE,SAAS,MAAM,GACnB,gBAAgB;EACd,GAAG;EACH,GAAI;CACN;CAGF,OAAO;AACT;;;;AAKA,SAAgB,oBAAoB,QAAuB,IAAI,SAAkB,MAAc;CAC7F,IAAI,UAAU,EAAE,SAAS,KAAK,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG;EACpD,QAAQ,MAAM,KAAK,CAAC,CAAC,kBAAkB;EAEvC,OAAO,GAAG,kBADO,YAAY,KACD;CAC9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