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

Allow semver ranges #2011

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions packages/installer/src/dappGet/dappGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export async function dappGet(
* It will not use the fetch or resolver module and only
* fetch the first level dependencies of the request
*/
// TODO: Add catch here?
if (options && options.BYPASS_RESOLVER)
return await dappGetBasic(dappnodeInstaller, req);

Expand Down
117 changes: 94 additions & 23 deletions packages/installer/src/dappGet/fetch/DappGetFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Dependencies } from "@dappnode/types";
import { validRange, satisfies, valid } from "semver";
import { Dependencies, InstalledPackageData } from "@dappnode/types";
import { validRange, satisfies, valid, maxSatisfying } from "semver";
import { DappnodeInstaller } from "../../dappnodeInstaller.js";
import { listPackageNoThrow } from "@dappnode/dockerapi";
import { listPackages } from "@dappnode/dockerapi";

export class DappGetFetcher {
/**
* Fetches the dependencies of a given DNP name and version
* Injects the optional dependencies if the package is installed
* Fetches the dependencies of a given DNP name and version.
* Injects the optional dependencies if the package is installed.
* @returns dependencies:
* { dnp-name-1: "semverRange", dnp-name-2: "/ipfs/Qmf53..."}
*/
Expand All @@ -17,31 +17,24 @@ export class DappGetFetcher {
): Promise<Dependencies> {
const manifest = await dappnodeInstaller.getManifestFromDir(name, version);
const dependencies = manifest.dependencies || {};
const optionalDependencies = manifest.optionalDependencies || {};
const installedPackages = await listPackages();

const optionalDependencies = manifest.optionalDependencies;
if (optionalDependencies) {
// Iterate over optional dependencies and inject them if installed
for (const [
optionalDependencyName,
optionalDependencyVersion,
] of Object.entries(optionalDependencies)) {
const optionalDependency = await listPackageNoThrow({
dnpName: optionalDependencyName,
});
if (!optionalDependency) continue;
dependencies[optionalDependencyName] = optionalDependencyVersion;
}
}
this.mergeOptionalDependencies(dependencies, optionalDependencies, installedPackages);
this.filterSatisfiedDependencies(dependencies, installedPackages);
await this.defineExactVersions(dependencies, dappnodeInstaller);

console.log(`Resolved dependencies to install for ${name}@${version}: ${JSON.stringify(dependencies)}`);

return dependencies;
}

/**
* Fetches the available versions given a request.
* Will fetch the versions from different places according the type of version range:
* Will fetch the versions from different places according to the type of version range:
* - valid semver range: Fetch the valid versions from APM
* - valid semver version (not range): Return that version
* - unvalid semver version ("/ipfs/Qmre4..."): Asume it's the only version
* - invalid semver version ("/ipfs/Qmre4..."): Assume it's the only version
*
* @param kwargs: {
* name: Name of package i.e. "kovan.dnp.dappnode.eth"
Expand Down Expand Up @@ -78,7 +71,85 @@ export class DappGetFetcher {
.filter((version) => satisfies(version, versionRange));
}
}
// Case 3. unvalid semver version ("/ipfs/Qmre4..."): Asume it's the only version
// Case 3. invalid semver version ("/ipfs/Qmre4..."): Assume it's the only version
return [versionRange];
}
}

/**
* Merges optional dependencies into the main dependencies object if the corresponding
* packages are installed.
*
* @param dependencies The main dependencies object to be merged into.
* @param optionalDependencies The optional dependencies to be checked and merged.
* @param installedPackages The list of currently installed packages.
*/
private mergeOptionalDependencies(
dependencies: Dependencies,
dappnodedev marked this conversation as resolved.
Show resolved Hide resolved
optionalDependencies: Dependencies,
installedPackages: InstalledPackageData[]
): void {
for (const [optionalDepName, optionalDepVersion] of Object.entries(optionalDependencies)) {
const isInstalled = installedPackages.some(
(installedPackage) => installedPackage.dnpName === optionalDepName
);

if (isInstalled) {
dependencies[optionalDepName] = optionalDepVersion;
}
}
}

/**
* Processes the dependencies by first filtering out those that are already satisfied by installed packages
* and then converting any remaining semver ranges to appropriate APM versions.
*
* @param dependencies The main dependencies object to be processed.
* @param installedPackages The list of currently installed packages.
*/
private filterSatisfiedDependencies(
dependencies: Dependencies,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me weird deleting the dependency if its installed already. It must be checked still if it satisfies the version range defined

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would take a different approach than deleting the dependency, it is still a dependency so whenever its going to be installed that code should bypass if already installed and version satisfies

installedPackages: InstalledPackageData[]
): void {
for (const [depName, depVersion] of Object.entries(dependencies)) {
const installedPackage = installedPackages.find(
(pkg) => pkg.dnpName === depName
);

if (!validRange(depVersion))
throw new Error(`Invalid semver notation for dependency ${depName}: ${depVersion}`);

// Remove dependency if it is already satisfied by an installed package
if (installedPackage && satisfies(installedPackage.version, depVersion)) {
console.log(
`Dependency ${depName} is already installed with version ${installedPackage.version}`
);
delete dependencies[depName];
}
}
}

private async defineExactVersions(
dependencies: Dependencies,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a description to the function also providing an example of how looks the object before and after?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Done

dappnodeInstaller: DappnodeInstaller
): Promise<void> {

for (const [depName, depVersion] of Object.entries(dependencies)) {

if (validRange(depVersion)) {

dappnodedev marked this conversation as resolved.
Show resolved Hide resolved
const pkgPublishments = await dappnodeInstaller.fetchApmVersionsState(depName);

const pkgVersions = Object.values(pkgPublishments)
.map(({ version }) => version);

const maxSatisfyingVersion = maxSatisfying(pkgVersions, depVersion);

if (!maxSatisfyingVersion) {
throw new Error(`Could not find any satisfying versions for ${depName}`);
}

dependencies[depName] = maxSatisfyingVersion;
}
}
}
}
Loading