Skip to content

Commit

Permalink
Implement download zome package
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbrisebois committed Sep 11, 2024
1 parent da8b2c2 commit 2f91a39
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 3 deletions.
5 changes: 5 additions & 0 deletions dnas/zomehub/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use zomehub_types::{
ZomeEntry,
ZomePackageEntry,
ZomePackageVersionEntry,
ApiCompatibility,

mere_memory_types,
};
Expand Down Expand Up @@ -76,6 +77,7 @@ impl TryInto<ZomeAsset> for EntryHash {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct CreateZomePackageInput {
pub name: String,
pub title: String,
pub description: String,
pub zome_type: ZomeType,

Expand All @@ -95,6 +97,7 @@ impl TryFrom<CreateZomePackageInput> for ZomePackageEntry {
Ok(
Self {
name: input.name,
title: input.title,
description: input.description,
zome_type: input.zome_type,
maintainer: input.maintainer
Expand All @@ -118,6 +121,7 @@ pub struct CreateZomePackageVersionInput {
// optional
pub changelog: Option<String>,
pub source_code_revision_uri: Option<String>,
pub api_compatibility: ApiCompatibility,

// Common fields
#[serde(default)]
Expand All @@ -135,6 +139,7 @@ impl TryFrom<CreateZomePackageVersionInput> for ZomePackageVersionEntry {

changelog: input.changelog,
source_code_revision_uri: input.source_code_revision_uri,
api_compatibility: input.api_compatibility,
metadata: input.metadata,
}
)
Expand Down
1 change: 1 addition & 0 deletions dnas/zomehub/types/src/zome_package_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use hdi::prelude::*;
#[derive(Clone)]
pub struct ZomePackageEntry {
pub name: String,
pub title: String,
pub description: String,
pub zome_type: ZomeType,
pub maintainer: Authority,
Expand Down
13 changes: 13 additions & 0 deletions dnas/zomehub/types/src/zome_package_version_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ use std::collections::BTreeMap;
use hdi::prelude::*;


#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct ApiCompatibilityBuiltWith {
pub hdi_version: String,
pub hdk_version: Option<String>, // Only required for coordinator zomes
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct ApiCompatibility {
pub build_with: ApiCompatibilityBuiltWith,
pub tested_with: String,
}


//
// Zome Package Version Entry
Expand All @@ -20,6 +32,7 @@ pub struct ZomePackageVersionEntry {
// Optional
pub changelog: Option<String>,
pub source_code_revision_uri: Option<String>,
pub api_compatibility: ApiCompatibility,

// Common fields
pub metadata: BTreeMap<String, rmpv::Value>,
Expand Down
25 changes: 25 additions & 0 deletions dnas/zomehub/zomelets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ export const ZomeHubCSRZomelet = new Zomelet({

return new ZomePackage( result, this );
},
async get_zome_package_by_name ( input ) {
const result = await this.call( input );

return new ZomePackage( result, this );
},
async get_zome_package_entry ( input ) {
const result = await this.call( new AnyDhtHash( input ) );

Expand Down Expand Up @@ -218,6 +223,14 @@ export const ZomeHubCSRZomelet = new Zomelet({
//
// Virtual functions
//
async form_zome_entry ( bytes ) {
return {
"zome_type": ZOME_TYPES.INTEGRITY,
"mere_memory_addr": new EntryHash( input.mere_memory_addr ),
"file_size": bytes.length,
"hash": await this.zomes.mere_memory_api.calculate_hash( bytes ),
};
},
async save_integrity ( bytes ) {
const addr = await this.zomes.mere_memory_api.save( bytes );

Expand Down Expand Up @@ -260,6 +273,18 @@ export const ZomeHubCSRZomelet = new Zomelet({

return versions;
},
async download_zome_package ( input ) {
const zome_package = await this.functions.get_zome_package_by_name( input );
const versions = await this.functions.get_zome_package_versions_sorted( zome_package.$id );
const latest_version = versions[0];
const zome = await this.functions.get_zome( latest_version.zome_entry );

return [
zome_package,
latest_version,
zome,
];
},
}, {
"zomes": {
"mere_memory_api": MereMemoryZomelet,
Expand Down
8 changes: 8 additions & 0 deletions dnas/zomehub/zomelets/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const MaintainerType = String;

export const ZomePackageStruct = {
"name": String,
"title": String,
"description": String,
"zome_type": String,
"maintainer": {
Expand Down Expand Up @@ -131,6 +132,13 @@ export const ZomePackageVersionStruct = {
"zome_entry": EntryHash,
"changelog": OptionType( String ),
"source_code_revision_uri": OptionType( String ),
"api_compatibility": {
"build_with": {
"hdi_version": String,
"hdk_version": OptionType( String ),
},
"tested_with": String,
},
"metadata": Object,
};

Expand Down
27 changes: 26 additions & 1 deletion tests/integration/zomehub/zome_package_versions_suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function ( args_fn ) {

let pack1;
let pack1_v1;
let pack1_name;

before(async function () {
({
Expand All @@ -38,8 +39,12 @@ export default function ( args_fn ) {
zome1,
} = args_fn());

const title = faker.commerce.productName();
pack1_name = title.toLowerCase(/\s/g, '-');

pack1 = await zomehub_csr.create_zome_package({
"name": faker.commerce.productName(),
"name": pack1_name,
title,
"description": faker.lorem.paragraphs( 2 ),
"zome_type": "integrity",
});
Expand All @@ -51,6 +56,13 @@ export default function ( args_fn ) {
"for_package": pack1.$id,
"zome_entry": zome1_addr,
"source_code_revision_uri": faker.internet.url(),
"api_compatibility": {
"build_with": {
"hdi_version": faker.system.semver(),
"hdk_version": faker.system.semver(),
},
"tested_with": faker.system.semver(),
},
});

log.normal("Create Zome package version: %s", json.debug(pack1_v1) );
Expand All @@ -70,6 +82,13 @@ export default function ( args_fn ) {
"for_package": pack1.$id,
"zome_entry": zome1_addr,
"source_code_revision_uri": faker.internet.url(),
"api_compatibility": {
"build_with": {
"hdi_version": faker.system.semver(),
"hdk_version": null,
},
"tested_with": faker.system.semver(),
},
});
}

Expand Down Expand Up @@ -101,6 +120,12 @@ export default function ( args_fn ) {
log.normal("Version links: %s", json.debug(version_links) );
});

it("should download latest version", async function () {
const latest_version = await zomehub_csr.download_zome_package( pack1_name );

log.normal("Latest package version: %s", json.debug(latest_version) );
});

linearSuite("Errors", function () {

});
Expand Down
15 changes: 13 additions & 2 deletions tests/integration/zomehub/zome_packages_suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export default function ( args_fn ) {
let zomehub_csr;
let zome1_addr, zome1;

let pack1;
let pack1
let pack1_name;

before(async function () {
({
Expand All @@ -39,8 +40,12 @@ export default function ( args_fn ) {
});

it("should create Zome Package entry", async function () {
const title = faker.commerce.productName();
pack1_name = title.toLowerCase(/\s/g, '-');

pack1 = await zomehub_csr.create_zome_package({
"name": faker.commerce.productName(),
"name": pack1_name,
title,
"description": faker.lorem.paragraphs( 2 ),
"zome_type": "integrity",
});
Expand All @@ -67,6 +72,12 @@ export default function ( args_fn ) {
expect( package_list ).to.have.length( 1 );
});

it("should get Zome Package by name", async function () {
const zome_package = await zomehub_csr.get_zome_package_by_name( pack1_name );

log.normal("Get Zome package: %s", json.debug(zome_package) );
});

linearSuite("Errors", function () {

});
Expand Down
4 changes: 4 additions & 0 deletions zomes/zomehub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub enum LinkTypes {
AgentToZomePackage,
AgentToZomePackageVersion,

NameToZomePackage,

ZomePackageToZomePackageVersion,
}

Expand All @@ -71,6 +73,8 @@ impl TryFrom<String> for LinkTypes {
"AgentToZomePackage" => LinkTypes::AgentToZomePackage,
"AgentToZomePackageVersion" => LinkTypes::AgentToZomePackageVersion,

"NameToZomePackage" => LinkTypes::NameToZomePackage,

"ZomePackageToZomePackageVersion" => LinkTypes::ZomePackageToZomePackageVersion,

_ => return Err(guest_error!(format!("Unknown LinkTypes variant: {}", name ))),
Expand Down
19 changes: 19 additions & 0 deletions zomes/zomehub_csr/src/zome_package_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use hdk::prelude::*;
use hdk_extensions::{
must_get,
hdi_extensions::{
guest_error,
ScopedTypeConnector,
AnyLinkableHashTransformer,
},
};
use zomehub::{
Expand All @@ -34,6 +36,9 @@ fn create_zome_package_entry(input: ZomePackageEntry) -> ExternResult<Entity<Zom
MY_ZOME_PACKS_ANCHOR.create_link_if_not_exists( &entity.address, () )?;

// TODO: Link from package name
let anchor_path = Path::from( vec![ Component::from(input.name.as_bytes().to_vec()) ] ).path_entry_hash()?;
let name_anchor = LinkBase::new( anchor_path, LinkTypes::NameToZomePackage );
name_anchor.create_link_if_not_exists( &entity.id, () )?;

Ok( entity )
}
Expand Down Expand Up @@ -71,6 +76,20 @@ pub fn get_zome_package(addr: EntityId) -> ExternResult<Entity<ZomePackageEntry>
}


#[hdk_extern]
pub fn get_zome_package_by_name(name: String) -> ExternResult<Entity<ZomePackageEntry>> {
let anchor_path = Path::from( vec![ Component::from(name.as_bytes().to_vec()) ] ).path_entry_hash()?;
let name_anchor = LinkBase::new( anchor_path, LinkTypes::NameToZomePackage );
let package_link = name_anchor.get_links( None )?.first()
.ok_or(guest_error!(format!(
"No package found for name '{}'",
name
)))?.to_owned();

Ok( get_entity( &package_link.target.must_be_action_hash()? )? )
}


#[hdk_extern]
fn get_zome_packages_for_agent(maybe_agent_id: Option<AgentPubKey>) ->
ExternResult<Vec<Entity<ZomePackageEntry>>>
Expand Down

0 comments on commit 2f91a39

Please sign in to comment.