Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update how a plugin's author is determined to handle NPM schema change #2079

Open
wants to merge 2 commits into
base: latest
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions src/modules/plugins/plugins.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export class PluginsService {
// create a cache for storing plugin alias
private pluginAliasCache = new NodeCache({ stdTTL: 86400 });

// create a cache for storing plugin author
private pluginAuthorCache = new NodeCache({ stdTTL: 86400 });

private specialPluginsRetryTimeout: NodeJS.Timeout;

/**
Expand Down Expand Up @@ -366,7 +369,7 @@ export class PluginsService {
homepage: pkg.homepage,
bugs: typeof pkg.bugs === 'object' && pkg.bugs?.url ? pkg.bugs.url : null,
};
plugin.author = (pkg.maintainers && pkg.maintainers.length) ? pkg.maintainers[0].name : null;
plugin.author = await this.getPluginAuthorFromMaintainers(plugin);
plugin.verifiedPlugin = this.verifiedPlugins.includes(pkg.name);
plugin.verifiedPlusPlugin = this.verifiedPlusPlugins.includes(pkg.name);
plugin.icon = this.pluginIcons[pkg.name]
Expand All @@ -382,6 +385,55 @@ export class PluginsService {
}
}

/**
* @param plugin
*/
private async getPluginAuthorFromMaintainers(plugin: HomebridgePlugin): Promise<string> {
// first, attempt to get it from the cache
let author: string = this.pluginAuthorCache.get(plugin.name);
if (author) {
return author;
}

// if it's not in cache, attempt to get it from the latest release
const pkgFromCache = this.npmPluginCache.get(plugin.name);

const pkg: IPackageJson = pkgFromCache || (
await this.httpService.get(`https://registry.npmjs.org/${encodeURIComponent(plugin.name).replace(/%40/g, '@')}/latest`).toPromise()
).data;

if (!pkgFromCache) {
this.npmPluginCache.set(plugin.name, pkg);
}

author = (pkg.maintainers && pkg.maintainers.length) ? pkg.maintainers[0].name : null;

if (author) {
this.pluginAuthorCache.set(plugin.name, author);

return author;
}

// if it's not available on the latest release, attempt get it from the top-level metadata
const lookupFromCache = this.npmPluginCache.get(`lookup-${plugin.name}`);

const lookup: INpmRegistryModule = lookupFromCache || (await (
this.httpService.get(`https://registry.npmjs.org/${encodeURIComponent(plugin.name).replace(/%40/g, '@')}`).toPromise()
)).data;

if (!lookupFromCache) {
this.npmPluginCache.set(`lookup-${plugin.name}`, lookup, 60);
}

author = (lookup.maintainers && lookup.maintainers.length) ? lookup.maintainers[0].name : null;

if (author) {
this.pluginAuthorCache.set(plugin.name, author);
}

return author;
}

/**
* Manage a plugin, install, update or uninstall it
* @param action
Expand Down Expand Up @@ -1322,7 +1374,7 @@ export class PluginsService {
homepage: pkg.homepage,
bugs: typeof pkg.bugs === 'object' && pkg.bugs?.url ? pkg.bugs.url : null,
};
plugin.author = (pkg.maintainers && pkg.maintainers.length) ? pkg.maintainers[0].name : null;
plugin.author = await this.getPluginAuthorFromMaintainers(plugin);
plugin.engines = pkg.engines;
} catch (e) {
if (e.response?.status !== 404) {
Expand Down
Loading