Skip to content

Commit

Permalink
Merge pull request #215 from ZenVoich/package-requirements
Browse files Browse the repository at this point in the history
Package requirements
  • Loading branch information
ZenVoich authored Mar 29, 2024
2 parents b8ee0a2 + 0ca8d75 commit 218b7df
Show file tree
Hide file tree
Showing 30 changed files with 352 additions and 151 deletions.
6 changes: 3 additions & 3 deletions backend/main/PackagePublisher.mo
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import PackageUtils "./utils/package-utils";

module {
type PackageVersion = Types.PackageVersion;
type PackageConfigV2 = Types.PackageConfigV2;
type PackageConfigV3 = Types.PackageConfigV3;
type PackageFileStats = Types.PackageFileStats;
type TestStats = Types.TestStats;
type FileId = Types.FileId;
Expand All @@ -31,7 +31,7 @@ module {
type PublishingPackage = {
time : Time.Time;
user : Principal;
config : PackageConfigV2;
config : PackageConfigV3;
storage : Principal;
};
type PublishingFile = {
Expand All @@ -51,7 +51,7 @@ module {
let publishingFileHashers = TrieMap.TrieMap<FileId, Sha256.Digest>(Text.equal, Text.hash);
let publishingBenchmarks = TrieMap.TrieMap<PublishingId, Benchmarks>(Text.equal, Text.hash);

public func startPublish(caller : Principal, config : PackageConfigV2) : async Result.Result<PublishingId, PublishingErr> {
public func startPublish(caller : Principal, config : PackageConfigV3) : async Result.Result<PublishingId, PublishingErr> {
if (Principal.isAnonymous(caller)) {
return #err("Unauthorized");
};
Expand Down
102 changes: 73 additions & 29 deletions backend/main/main-canister.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Result "mo:base/Result";
import Debug "mo:base/Debug";
import Principal "mo:base/Principal";
import Order "mo:base/Order";
import Option "mo:base/Option";
import Blob "mo:base/Blob";

import IC "mo:ic";
Expand Down Expand Up @@ -43,6 +44,7 @@ actor class Main() {
public type FileId = Types.FileId;
public type Err = Text.Text;
public type PackageConfigV2 = Types.PackageConfigV2;
public type PackageConfigV3 = Types.PackageConfigV3;
public type PackagePublication = Types.PackagePublication;
public type PackageDetails = Types.PackageDetails;
public type PackageSummary = Types.PackageSummary;
Expand All @@ -60,9 +62,11 @@ actor class Main() {

var packageVersions = TrieMap.TrieMap<PackageName, [PackageVersion]>(Text.equal, Text.hash);
var packageOwners = TrieMap.TrieMap<PackageName, Principal>(Text.equal, Text.hash);
var highestConfigs = TrieMap.TrieMap<PackageName, PackageConfigV2>(Text.equal, Text.hash);
var highestConfigsV2 = TrieMap.TrieMap<PackageName, PackageConfigV2>(Text.equal, Text.hash);
var highestConfigs = TrieMap.TrieMap<PackageName, PackageConfigV3>(Text.equal, Text.hash);

var packageConfigs = TrieMap.TrieMap<PackageId, PackageConfigV2>(Text.equal, Text.hash);
var packageConfigsV2 = TrieMap.TrieMap<PackageId, PackageConfigV2>(Text.equal, Text.hash);
var packageConfigs = TrieMap.TrieMap<PackageId, PackageConfigV3>(Text.equal, Text.hash);
var packagePublications = TrieMap.TrieMap<PackageId, PackagePublication>(Text.equal, Text.hash);
var fileIdsByPackage = TrieMap.TrieMap<PackageId, [FileId]>(Text.equal, Text.hash);
var hashByFileId = TrieMap.TrieMap<FileId, Blob>(Text.equal, Text.hash);
Expand Down Expand Up @@ -128,14 +132,19 @@ actor class Main() {
context = Blob.fromArray([]);
};

func _verifyPackageRepo(config : PackageConfigV2) : async Result.Result<(), Err> {
func _verifyPackageRepo(config : PackageConfigV3) : async Result.Result<(), Err> {
await verifyPackageRepository(config.name, config.repository, transform);
};

// PUBLIC

// Publication
public shared ({caller}) func startPublish(config : PackageConfigV2) : async Result.Result<PublishingId, Err> {
public shared ({caller}) func startPublish(configPub : Types.PackageConfigV3_Publishing) : async Result.Result<PublishingId, Err> {
let config : PackageConfigV3 = {
configPub with
requirements = Option.get(configPub.requirements, []);
};

let pubRes = await packagePublisher.startPublish(caller, config);
if (Result.isErr(pubRes)) {
return pubRes;
Expand Down Expand Up @@ -513,12 +522,12 @@ actor class Main() {
let backupManager = Backup.BackupManager(backupStateV2, {maxBackups = 20});

type BackupChunk = {
#v6 : {
#v7 : {
#packagePublications : [(PackageId, PackagePublication)];
#packageVersions : [(PackageName, [PackageVersion])];
#packageOwners : [(PackageName, Principal)];
#packageConfigs : [(PackageId, PackageConfigV2)];
#highestConfigs : [(PackageName, PackageConfigV2)];
#packageConfigs : [(PackageId, PackageConfigV3)];
#highestConfigs : [(PackageName, PackageConfigV3)];
#fileIdsByPackage : [(PackageId, [FileId])];
#hashByFileId : [(FileId, Blob)];
#packageFileStats : [(PackageId, PackageFileStats)];
Expand All @@ -542,22 +551,22 @@ actor class Main() {
};

func _backup() : async () {
let backup = backupManager.NewBackup("v6");
let backup = backupManager.NewBackup("v7");
await backup.startBackup();
await backup.uploadChunk(to_candid(#v6(#packagePublications(Iter.toArray(packagePublications.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#packageVersions(Iter.toArray(packageVersions.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#packageOwners(Iter.toArray(packageOwners.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#fileIdsByPackage(Iter.toArray(fileIdsByPackage.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#hashByFileId(Iter.toArray(hashByFileId.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#packageFileStats(Iter.toArray(packageFileStats.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#packageTestStats(Iter.toArray(packageTestStats.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#packageBenchmarks(Iter.toArray(packageBenchmarks.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#packageNotes(Iter.toArray(packageNotes.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#downloadLog(downloadLog.toStable())) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#storageManager(storageManager.toStable())) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#users(users.toStable())) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#highestConfigs(Iter.toArray(highestConfigs.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v6(#packageConfigs(Iter.toArray(packageConfigs.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packagePublications(Iter.toArray(packagePublications.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packageVersions(Iter.toArray(packageVersions.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packageOwners(Iter.toArray(packageOwners.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#fileIdsByPackage(Iter.toArray(fileIdsByPackage.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#hashByFileId(Iter.toArray(hashByFileId.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packageFileStats(Iter.toArray(packageFileStats.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packageTestStats(Iter.toArray(packageTestStats.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packageBenchmarks(Iter.toArray(packageBenchmarks.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packageNotes(Iter.toArray(packageNotes.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#downloadLog(downloadLog.toStable())) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#storageManager(storageManager.toStable())) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#users(users.toStable())) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#highestConfigs(Iter.toArray(highestConfigs.entries()))) : BackupChunk));
await backup.uploadChunk(to_candid(#v7(#packageConfigs(Iter.toArray(packageConfigs.entries()))) : BackupChunk));
await backup.finishBackup();
};

Expand All @@ -567,7 +576,7 @@ actor class Main() {
assert(Utils.isAdmin(caller));

await backupManager.restore(backupId, func(blob : Blob) {
let ?#v6(chunk) : ?BackupChunk = from_candid(blob) else Debug.trap("Failed to restore chunk");
let ?#v7(chunk) : ?BackupChunk = from_candid(blob) else Debug.trap("Failed to restore chunk");

switch (chunk) {
case (#packagePublications(packagePublicationsStable)) {
Expand Down Expand Up @@ -609,10 +618,10 @@ actor class Main() {
users.loadStable(usersStable);
};
case (#highestConfigs(highestConfigsStable)) {
highestConfigs := TrieMap.fromEntries<PackageName, PackageConfigV2>(highestConfigsStable.vals(), Text.equal, Text.hash);
highestConfigs := TrieMap.fromEntries<PackageName, PackageConfigV3>(highestConfigsStable.vals(), Text.equal, Text.hash);
};
case (#packageConfigs(packageConfigsStable)) {
packageConfigs := TrieMap.fromEntries<PackageId, PackageConfigV2>(packageConfigsStable.vals(), Text.equal, Text.hash);
packageConfigs := TrieMap.fromEntries<PackageId, PackageConfigV3>(packageConfigsStable.vals(), Text.equal, Text.hash);
};
};
});
Expand All @@ -638,9 +647,14 @@ actor class Main() {
stable var packagePublicationsStable : [(PackageId, PackagePublication)] = [];
stable var packageVersionsStable : [(PackageName, [PackageVersion])] = [];
stable var packageOwnersStable : [(PackageName, Principal)] = [];

// todo: remove
stable var packageConfigsStableV2 : [(PackageId, PackageConfigV2)] = [];
stable var highestConfigsStableV2 : [(PackageName, PackageConfigV2)] = [];

stable var packageConfigsStableV3 : [(PackageId, PackageConfigV3)] = [];
stable var highestConfigsStableV3 : [(PackageName, PackageConfigV3)] = [];

stable var fileIdsByPackageStable : [(PackageId, [FileId])] = [];
stable var hashByFileIdStable : [(FileId, Blob)] = [];
stable var packageFileStatsStable : [(PackageId, PackageFileStats)] = [];
Expand All @@ -666,17 +680,47 @@ actor class Main() {
storageManagerStable := storageManager.toStable();
usersStable := users.toStable();

highestConfigsStableV2 := Iter.toArray(highestConfigs.entries());
packageConfigsStableV2 := Iter.toArray(packageConfigs.entries());
// todo: remove
highestConfigsStableV2 := Iter.toArray(highestConfigsV2.entries());
packageConfigsStableV2 := Iter.toArray(packageConfigsV2.entries());

highestConfigsStableV3 := Iter.toArray(highestConfigs.entries());
packageConfigsStableV3 := Iter.toArray(packageConfigs.entries());
};

system func postupgrade() {
packageConfigs := TrieMap.fromEntries<PackageId, PackageConfigV2>(packageConfigsStableV2.vals(), Text.equal, Text.hash);
// todo: remove
packageConfigsV2 := TrieMap.fromEntries<PackageId, PackageConfigV2>(packageConfigsStableV2.vals(), Text.equal, Text.hash);
packageConfigsStableV2 := [];

highestConfigs := TrieMap.fromEntries<PackageName, PackageConfigV2>(highestConfigsStableV2.vals(), Text.equal, Text.hash);
// todo: remove
highestConfigsV2 := TrieMap.fromEntries<PackageName, PackageConfigV2>(highestConfigsStableV2.vals(), Text.equal, Text.hash);
highestConfigsStableV2 := [];

// todo: remove
if (packageConfigsStableV3.size() == 0) {
func config2to3(configV2 : PackageConfigV2) : PackageConfigV3 {
{
configV2 with
requirements = [];
};
};
// migrate packageConfigs v2 -> v3
packageConfigsStableV3 := Iter.map<(PackageId, PackageConfigV2), (PackageId, PackageConfigV3)>(packageConfigsV2.entries(), func((packageId, configV2)) {
(packageId, config2to3(configV2));
}) |> Iter.toArray(_);
// migrate highestConfigs v2 -> v3
highestConfigsStableV3 := Iter.map<(PackageId, PackageConfigV2), (PackageId, PackageConfigV3)>(highestConfigsV2.entries(), func((name, configV2)) {
(name, config2to3(configV2));
})|> Iter.toArray(_);
};

packageConfigs := TrieMap.fromEntries<PackageId, PackageConfigV3>(packageConfigsStableV3.vals(), Text.equal, Text.hash);
packageConfigsStableV3 := [];

highestConfigs := TrieMap.fromEntries<PackageName, PackageConfigV3>(highestConfigsStableV3.vals(), Text.equal, Text.hash);
highestConfigsStableV3 := [];

packagePublications := TrieMap.fromEntries<PackageId, PackagePublication>(packagePublicationsStable.vals(), Text.equal, Text.hash);
packagePublicationsStable := [];

Expand Down
16 changes: 8 additions & 8 deletions backend/main/registry/Registry.mo
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ module {
public type PackageVersion = Types.PackageVersion;
public type PackageId = Types.PackageId;
public type FileId = Types.FileId;
public type PackageConfigV2 = Types.PackageConfigV2;
public type PackageConfigV3 = Types.PackageConfigV3;
public type PackagePublication = Types.PackagePublication;
public type PackageFileStats = Types.PackageFileStats;
public type TestStats = Types.TestStats;
public type Benchmarks = Types.Benchmarks;

type NewPackageReleaseArgs = {
userId : Principal;
config : PackageConfigV2;
config : PackageConfigV3;
notes : Text;
storageId : Principal;
fileIds : [FileId];
Expand All @@ -35,8 +35,8 @@ module {
public class Registry(
packageVersions : TrieMap.TrieMap<PackageName, [PackageVersion]>,
packageOwners : TrieMap.TrieMap<PackageName, Principal>,
highestConfigs : TrieMap.TrieMap<PackageName, PackageConfigV2>,
packageConfigs : TrieMap.TrieMap<PackageId, PackageConfigV2>,
highestConfigs : TrieMap.TrieMap<PackageName, PackageConfigV3>,
packageConfigs : TrieMap.TrieMap<PackageId, PackageConfigV3>,
packagePublications : TrieMap.TrieMap<PackageId, PackagePublication>,
fileIdsByPackage : TrieMap.TrieMap<PackageId, [FileId]>,
hashByFileId : TrieMap.TrieMap<FileId, Blob>,
Expand Down Expand Up @@ -90,7 +90,7 @@ module {
packageNotes.put(packageId, newRelease.notes);
};

func _updateHighestConfig(config : PackageConfigV2) {
func _updateHighestConfig(config : PackageConfigV3) {
switch (getHighestVersion(config.name)) {
case (?ver) {
if (Semver.compare(config.version, ver) == #greater) {
Expand All @@ -107,11 +107,11 @@ module {
// All packages
// -----------------------------

public func getHighestConfigs() : [PackageConfigV2] {
public func getHighestConfigs() : [PackageConfigV3] {
Iter.toArray(highestConfigs.vals());
};

public func getAllConfigs() : [PackageConfigV2] {
public func getAllConfigs() : [PackageConfigV3] {
Iter.toArray(packageConfigs.vals());
};

Expand Down Expand Up @@ -141,7 +141,7 @@ module {
// By package name and version
// -----------------------------

public func getPackageConfig(name : PackageName, version : PackageVersion) : ?PackageConfigV2 {
public func getPackageConfig(name : PackageName, version : PackageVersion) : ?PackageConfigV3 {
packageConfigs.get(name # "@" # version);
};

Expand Down
6 changes: 3 additions & 3 deletions backend/main/registry/getPackageChanges.mo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module {
public type PackageName = Types.PackageName;
public type PackageVersion = Types.PackageVersion;
public type DependencyV2 = Types.DependencyV2;
public type PackageConfigV2 = Types.PackageConfigV2;
public type PackageConfigV3 = Types.PackageConfigV3;
public type TestStats = Types.TestStats;
public type TestsChanges = Types.TestsChanges;
public type PackageChanges = Types.PackageChanges;
Expand Down Expand Up @@ -61,7 +61,7 @@ module {
};

// deps changes
public func _computeDepsChangesBetween(prevPackageConfig : ?PackageConfigV2, curPackageConfig : ?PackageConfigV2) : [DepChange] {
public func _computeDepsChangesBetween(prevPackageConfig : ?PackageConfigV3, curPackageConfig : ?PackageConfigV3) : [DepChange] {
let oldDeps = switch (prevPackageConfig) {
case (?config) config.dependencies;
case (null) [];
Expand All @@ -74,7 +74,7 @@ module {
};

// dev deps changes
public func _computeDevDepsChangesBetween(prevPackageConfig : ?PackageConfigV2, curPackageConfig : ?PackageConfigV2) : [DepChange] {
public func _computeDevDepsChangesBetween(prevPackageConfig : ?PackageConfigV3, curPackageConfig : ?PackageConfigV3) : [DepChange] {
let oldDeps = switch (prevPackageConfig) {
case (?config) config.devDependencies;
case (null) [];
Expand Down
14 changes: 7 additions & 7 deletions backend/main/registry/getPackageDetails.mo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module {
public type PackageDetails = Types.PackageDetails;
public type PackageSummaryWithChanges = Types.PackageSummaryWithChanges;
public type DependencyV2 = Types.DependencyV2;
public type PackageConfigV2 = Types.PackageConfigV2;
public type PackageConfigV3 = Types.PackageConfigV3;

// package details that appear on the package page
public func getPackageDetails(registry : Registry.Registry, users : Users.Users, downloadLog : DownloadLog.DownloadLog, name : PackageName, version : PackageVersion) : ?PackageDetails {
Expand Down Expand Up @@ -62,7 +62,7 @@ module {

// dependents
func _getPackageDependents(name : PackageName) : [PackageSummary] {
func isDependent(config : PackageConfigV2) : Bool {
func isDependent(config : PackageConfigV3) : Bool {
let dependent = Option.isSome(Array.find<DependencyV2>(config.dependencies, func(dep : DependencyV2) {
dep.name == name and dep.repo == "";
}));
Expand All @@ -72,17 +72,17 @@ module {
dependent or devDependent;
};

let dependentConfigs = Array.filter<PackageConfigV2>(registry.getAllConfigs(), isDependent);
let dependentConfigs = Array.filter<PackageConfigV3>(registry.getAllConfigs(), isDependent);

let pkgHash = func(a : PackageConfigV2) : Hash.Hash {
let pkgHash = func(a : PackageConfigV3) : Hash.Hash {
Text.hash(a.name);
};
let pkgEqual = func(a : PackageConfigV2, b : PackageConfigV2) : Bool {
let pkgEqual = func(a : PackageConfigV3, b : PackageConfigV3) : Bool {
a.name == b.name;
};
let unique = TrieSet.toArray(TrieSet.fromArray<PackageConfigV2>(dependentConfigs, pkgHash, pkgEqual)).vals();
let unique = TrieSet.toArray(TrieSet.fromArray<PackageConfigV3>(dependentConfigs, pkgHash, pkgEqual)).vals();

let summaries = Iter.map<PackageConfigV2, PackageSummary>(unique, func(config) {
let summaries = Iter.map<PackageConfigV3, PackageSummary>(unique, func(config) {
let ?summary = getPackageSummary(registry, users, downloadLog, config.name, config.version) else Debug.trap("Package '" # name # "' not found");
summary;
});
Expand Down
4 changes: 2 additions & 2 deletions backend/main/registry/searchInRegistry.mo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {getPackageSummary} "./getPackageSummary";

module {
public type PackageSummary = Types.PackageSummary;
public type PackageConfigV2 = Types.PackageConfigV2;
public type PackageConfigV3 = Types.PackageConfigV3;
public type PageCount = Nat;

public func searchInRegistry(registry : Registry.Registry, users : Users.Users, downloadLog : DownloadLog.DownloadLog, searchText : Text, limitOpt : ?Nat, pageIndexOpt : ?Nat) : ([PackageSummary], PageCount) {
Expand All @@ -26,7 +26,7 @@ module {
assert(searchText.size() <= 100);

type ConfigWithPoints = {
config : PackageConfigV2;
config : PackageConfigV3;
sortingPoints : Nat;
};
let matchedConfigs = Buffer.Buffer<ConfigWithPoints>(0);
Expand Down
Loading

0 comments on commit 218b7df

Please sign in to comment.