@nodesecure/scanner
Version:
A package API to run a static analysis of your module's dependencies.
48 lines • 1.76 kB
JavaScript
// Import Third-party Dependencies
import * as npmRegistrySDK from "@nodesecure/npm-registry-sdk";
export async function fetchNpmAvatars(metadata) {
const contributors = [
...metadata.maintainers,
...metadata.publishers,
...(metadata.author ? [metadata.author] : [])
];
const avatarCache = new Map();
await Promise.all(contributors.map((contributor) => enrichContributorWithAvatar(contributor, avatarCache)));
// Backfill missing avatars: some contributors may have failed username lookup
// but their email might match a cached avatar from a successful contributor
contributors
.filter((contributor) => !contributor.npmAvatar && contributor.email)
.forEach((contributor) => {
const cachedAvatar = avatarCache.get(contributor.email);
if (cachedAvatar) {
contributor.npmAvatar = cachedAvatar;
}
});
}
async function enrichContributorWithAvatar(contributor, avatarCache) {
if (trySetAvatarFromCache(contributor, avatarCache)) {
return;
}
try {
const profile = await npmRegistrySDK.user(contributor.name, { perPage: 1 });
contributor.npmAvatar = profile.avatars.small;
if (contributor.email && contributor.npmAvatar) {
avatarCache.set(contributor.email, contributor.npmAvatar);
}
}
catch {
contributor.npmAvatar = undefined;
}
}
function trySetAvatarFromCache(contributor, avatarCache) {
if (!contributor.email) {
return false;
}
const cachedAvatar = avatarCache.get(contributor.email);
if (cachedAvatar) {
contributor.npmAvatar = cachedAvatar;
return true;
}
return false;
}
//# sourceMappingURL=fetchNpmAvatars.js.map