Skip to content

Commit

Permalink
Adding NpmAvatar (#201)
Browse files Browse the repository at this point in the history
* add npmAvatar
  • Loading branch information
kishore881 authored Jan 4, 2024
1 parent 7cca481 commit adc2974
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/npmRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import crypto from "node:crypto";

// Import Third-party Dependencies
import semver from "semver";
import { packument, packumentVersion } from "@nodesecure/npm-registry-sdk";
import { packument, packumentVersion, user as npmUserProfile } from "@nodesecure/npm-registry-sdk";

// Import Internal Dependencies
import { parseAuthor, getLinks } from "./utils/index.js";
Expand Down Expand Up @@ -102,6 +102,7 @@ export async function packageMetadata(name, version, options) {
}
}

await addNpmAvatar(metadata);
Object.assign(ref.versions[version], { links: getLinks(pkg.versions[version]) });
Object.assign(ref.metadata, metadata);
}
Expand Down Expand Up @@ -134,3 +135,34 @@ function getPackumentVersionIntegrity(packumentVersion) {
.update(JSON.stringify(integrityObj))
.digest("hex");
}

async function addNpmAvatar(metadata) {
const contributors = [metadata.author, ...metadata.maintainers, ...metadata.publishers];
const emailToAvatar = {};

const promises = contributors.map((contributor) => {
if (contributor.email && emailToAvatar[contributor.email]) {
contributor.npmAvatar = emailToAvatar[contributor.email];

return Promise.resolve();
}

return npmUserProfile(contributor.name, { perPage: 1 }).then((profile) => {
contributor.npmAvatar = profile.avatars.small;
if (contributor.email && contributor.npmAvatar) {
emailToAvatar[contributor.email] = contributor.npmAvatar;
}
}).catch(() => {
contributor.npmAvatar = null;
});
});

await Promise.all(promises);

// back fill npmAvatar if any name property was not npm username in first pass
for (const contributor of contributors) {
if (!contributor.npmAvatar && contributor.email && emailToAvatar[contributor.email]) {
contributor.npmAvatar = emailToAvatar[contributor.email];
}
}
}
4 changes: 4 additions & 0 deletions test/depWalker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function cleanupPayload(payload) {
delete verDescriptor.composition.files;
delete verDescriptor.composition.required_files;
}
for (const contributor of [pkg.metadata.author, ...pkg.metadata.publishers, ...pkg.metadata.maintainers]) {
// this is a dynamic property
delete contributor.npmAvatar;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/npmRegistry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test("registry.packageMetadata", async() => {
assert.deepEqual(ref.versions["1.5.0"].flags, ["isOutdated"]);
assert.strictEqual(logger.count("registry"), 1);

assert.deepEqual(ref.metadata.author, { name: "SlimIO" });
assert.strictEqual(ref.metadata.author.name, "SlimIO");
assert.strictEqual(ref.metadata.homepage, "https://github.com/SlimIO/is#readme");
assert.ok(semver.gt(ref.metadata.lastVersion, "1.5.0"));

Expand Down
7 changes: 7 additions & 0 deletions types/scanner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ declare namespace Scanner {
name: string;
email?: string;
url?: string;
npmAvatar?: string;
}

export interface Maintainer {
name: string;
email: string;
npmAvatar?: string;
}

export interface Publisher {
Expand All @@ -38,6 +40,11 @@ declare namespace Scanner {
* @example 2021-08-10T20:45:08.342Z
*/
at: string;
/**
* Path to publisher's avatar on "https://www.npmjs.com"
* @example /npm-avatar/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.LwimMJA3puF3ioGeS-tfczR3370GXBZMIL-bdpu4hOU
*/
npmAvatar?: string;
}

export interface DependencyLinks {
Expand Down

0 comments on commit adc2974

Please sign in to comment.