Skip to content

Commit

Permalink
Change happ downloading to use client-side assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbrisebois committed Jun 16, 2023
1 parent d895215 commit 57d7eea
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 123 deletions.
126 changes: 126 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,132 @@ const common = {

return new HoloHash( response );
},

async assembleHapp ( client, release ) {
const happ_manifest = JSON.parse( JSON.stringify( release.manifest ) )
const dna_resources = {};

log.normal("Assemble hApp release package:", release );
for ( let i in release.dnas ) {
const dna_ref = release.dnas[i];

log.normal("Assemble DNA release package:", dna_ref );
const dna_version = await client.call("dnarepo", "dna_library", "get_dna_version", {
"id": dna_ref.version,
});
console.log( dna_version );

const resources = {};
const integrity_zomes = [];
const coordinator_zomes = [];

for ( let zome_ref of dna_version.integrity_zomes ) {
const rpath = `${zome_ref.name}.wasm`;
const wasm_bytes = await common.downloadMemory( client, zome_ref.resource, "dnarepo" );
console.log("Zome %s wasm bytes: %s", zome_ref.name, wasm_bytes.length );
integrity_zomes.push({
"name": zome_ref.name,
"bundled": rpath,
"hash": null,
});
resources[ rpath ] = wasm_bytes;
}

for ( let zome_ref of dna_version.zomes ) {
const rpath = `${zome_ref.name}.wasm`;
const wasm_bytes = await common.downloadMemory( client, zome_ref.resource, "dnarepo" );
console.log("Zome %s wasm bytes: %s", zome_ref.name, wasm_bytes.length );
coordinator_zomes.push({
"name": zome_ref.name,
"bundled": rpath,
"hash": null,
"dependencies": zome_ref.dependencies.map( name => {
return { name };
}),
});
resources[ rpath ] = wasm_bytes;
}

const dna_config = {
"manifest": {
"manifest_version": "1",
"name": dna_ref.role_name,
"integrity": {
"origin_time": dna_version.origin_time,
"network_seed": dna_version.network_seed,
"properties": dna_version.properties,
"zomes": integrity_zomes,
},
"coordinator": {
"zomes": coordinator_zomes,
},
},
resources,
};
console.log( dna_ref.role_name, dna_config );
const msgpacked_bytes = MessagePack.encode( dna_config );
const gzipped_bytes = pako.gzip( msgpacked_bytes );

const rpath = `${dna_ref.role_name}.dna`;
dna_resources[ rpath ] = gzipped_bytes;
happ_manifest.roles[i].dna.bundled = rpath;
log.normal("Finished packing DNA: %s", dna_ref.role_name );
}

console.log( happ_manifest );
const happ_config = {
"manifest": happ_manifest,
"resources": dna_resources,
};
const happ_bytes = pako.gzip( MessagePack.encode( happ_config ) );
log.normal("Finished packing hApp");
return happ_bytes;
},

async assembleWebhapp ( client, name, release ) {
log.normal("Get Webhapp (official) GUI release: %s", release.official_gui );
const gui_release = await client.call("happs", "happ_library", "get_gui_release", {
"id": release.official_gui,
});
console.log( gui_release );

log.normal("Get UI webasset zip: %s", gui_release.web_asset_id );
const file = await client.call("web_assets", "web_assets", "get_file", {
"id": gui_release.web_asset_id,
});
console.log( file );

const ui_bytes = await common.downloadMemory(
client,
file.mere_memory_addr
);
console.log( ui_bytes );

const happ_bytes = await common.assembleHapp( client, release );

const webhapp_config = {
"manifest": {
"manifest_version": "1",
"name": name,
"ui": {
"bundled": "ui.zip"
},
"happ_manifest": {
"bundled": "bundled.happ"
}
},
"resources": {
"ui.zip": ui_bytes,
"bundled.happ": happ_bytes,
},
};
console.log( webhapp_config );
const msgpacked_bytes = MessagePack.encode( webhapp_config );
const gzipped_bytes = pako.gzip( msgpacked_bytes );

log.normal("Finished packing Webhapp");
return gzipped_bytes;
},
};

module.exports = common;
132 changes: 10 additions & 122 deletions src/happ_release_controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ module.exports = async function ( client ) {

"download_error": null,
"download_webhapp_error": null,
"downloading_happ": false,
"downloading_webhapp": false,
};
},
Expand Down Expand Up @@ -269,141 +270,28 @@ module.exports = async function ( client ) {
this.$openstate.read( this.happ_datapath );
},
async downloadPackageBytes () {
const bytes = await this.$openstate.get( this.bundle_datapath, {
"rememberState": false,
});
this.downloading_happ = true;
await this.delay();
log.normal("Download hApp package:", this.happ, this.release );
const bytes = await common.assembleHapp( client, this.release );

this.download( this.package_filename, bytes );
this.downloading_happ = false;
},
async oldDownloadWebhappPackageBytes () {
log.normal("Download Webhapp package:", this.happ, this.release );
const bytes = await this.$openstate.get( this.webhapp_bundle_datapath, {
const bytes = await this.$openstate.get( this.webhapp_bundle_datapath, {
"rememberState": false,
});
this.download( "old_"+this.package_webhapp_filename, bytes );
},
async downloadWebhappPackageBytes () {
this.downloading_webhapp = true;
this.downloading_webhapp = true;
await this.delay();
try {
log.normal("Get Webhapp (official) GUI release: %s", this.release.official_gui );
const gui_release = await client.call("happs", "happ_library", "get_gui_release", {
"id": this.release.official_gui,
});
console.log( gui_release );

log.normal("Get UI webasset zip: %s", gui_release.web_asset_id );
const file = await client.call("web_assets", "web_assets", "get_file", {
"id": gui_release.web_asset_id,
});
console.log( file );

const ui_bytes = await common.downloadMemory(
client,
file.mere_memory_addr
);
console.log( ui_bytes );

const happ_manifest = JSON.parse( JSON.stringify( this.release.manifest ) )
const dna_resources = {};

log.normal("Assemble hApp release package:", this.release );
for ( let i in this.release.dnas ) {
const dna_ref = this.release.dnas[i];

log.normal("Assemble DNA release package:", dna_ref );
const dna_version = await client.call("dnarepo", "dna_library", "get_dna_version", {
"id": dna_ref.version,
});
console.log( dna_version );

const resources = {};
const integrity_zomes = [];
const coordinator_zomes = [];

for ( let zome_ref of dna_version.integrity_zomes ) {
const rpath = `${zome_ref.name}.wasm`;
const wasm_bytes = await common.downloadMemory( client, zome_ref.resource, "dnarepo" );
console.log("Zome %s wasm bytes: %s", zome_ref.name, wasm_bytes.length );
integrity_zomes.push({
"name": zome_ref.name,
"bundled": rpath,
"hash": null,
});
resources[ rpath ] = wasm_bytes;
}

for ( let zome_ref of dna_version.zomes ) {
const rpath = `${zome_ref.name}.wasm`;
const wasm_bytes = await common.downloadMemory( client, zome_ref.resource, "dnarepo" );
console.log("Zome %s wasm bytes: %s", zome_ref.name, wasm_bytes.length );
coordinator_zomes.push({
"name": zome_ref.name,
"bundled": rpath,
"hash": null,
"dependencies": zome_ref.dependencies.map( name => {
return { name };
}),
});
resources[ rpath ] = wasm_bytes;
}

const dna_config = {
"manifest": {
"manifest_version": "1",
"name": dna_ref.role_name,
"integrity": {
"origin_time": dna_version.origin_time,
"network_seed": dna_version.network_seed,
"properties": dna_version.properties,
"zomes": integrity_zomes,
},
"coordinator": {
"zomes": coordinator_zomes,
},
},
resources,
};
console.log( dna_ref.role_name, dna_config );
const msgpacked_bytes = MessagePack.encode( dna_config );
const gzipped_bytes = pako.gzip( msgpacked_bytes );

const rpath = `${dna_ref.role_name}.dna`;
dna_resources[ rpath ] = gzipped_bytes;
happ_manifest.roles[i].dna.bundled = rpath;
log.normal("Finished packing DNA: %s", dna_ref.role_name );
}

console.log( happ_manifest );
const happ_config = {
"manifest": happ_manifest,
"resources": dna_resources,
};
const happ_bytes = pako.gzip( MessagePack.encode( happ_config ) );
log.normal("Finished packing hApp");

const webhapp_config = {
"manifest": {
"manifest_version": "1",
"name": this.happ.title,
"ui": {
"bundled": "ui.zip"
},
"happ_manifest": {
"bundled": "bundled.happ"
}
},
"resources": {
"ui.zip": ui_bytes,
"bundled.happ": happ_bytes,
},
};
console.log( webhapp_config );
const msgpacked_bytes = MessagePack.encode( webhapp_config );
const gzipped_bytes = pako.gzip( msgpacked_bytes );

log.normal("Download Webhapp package:", this.happ, this.release );
this.download( this.package_webhapp_filename, gzipped_bytes );
const bytes = await common.assembleWebhapp( client, this.happ.title, this.release );
this.download( this.package_webhapp_filename, bytes );
} catch (err) {
console.log( err );
alert(`${err}`);
Expand Down
3 changes: 2 additions & 1 deletion static/templates/happs/releases/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ <h4 class="mt-5">DNAs</h4>
<loading :when="!$release.current">
<div class="mt-5">
<a class="btn btn-primary w-100" @click="downloadPackageBytes()">
<span v-if="$package_bytes.reading"
<!-- <span v-if="$package_bytes.reading" -->
<span v-if="downloading_happ"
class="spinner-border spinner-border-sm me-3"></span>
<i v-else class="bi-download"></i>
Download {{ package_filename }}
Expand Down

0 comments on commit 57d7eea

Please sign in to comment.