-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,092 additions
and
186 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name: func() -> string | ||
|
||
description: func() -> string | ||
|
||
version: func() -> s64 | ||
|
||
permissions: func() -> list<string> | ||
|
||
min-engine-version: func() -> s64 | ||
|
||
max-engine-version: func() -> s64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
get-storage-path: func(data-path: string) -> string | ||
|
||
get-file-path: func(data-path: string, block-id: string) -> string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,85 @@ | ||
mod blockstorage; | ||
mod path_provider; | ||
mod peer_db; | ||
|
||
pub mod web; | ||
|
||
pub use blockstorage::BlockStorage; | ||
use std::path::PathBuf; | ||
|
||
use log::error; | ||
pub use path_provider::*; | ||
|
||
use wit_bindgen_wasmer::wasmer::{imports, Cranelift, Module, Store}; | ||
|
||
use self::manifest::Manifest; | ||
|
||
wit_bindgen_wasmer::import!("assets/plugins/manifest.wit"); | ||
|
||
pub fn load_plugins() -> Vec<Plugin> { | ||
let engine = Cranelift::default(); | ||
let mut store = Store::new(engine); | ||
let plugins_dir = PathBuf::from("plugins"); | ||
if plugins_dir.exists() { | ||
let list = std::fs::read_dir(plugins_dir).unwrap(); | ||
let plugins = list.filter_map(|entry| { | ||
let path = entry.unwrap().path(); | ||
if path.is_file() && path.extension().unwrap() == "wasm" { | ||
Some(path) | ||
} else { | ||
None | ||
} | ||
}); | ||
let mut plugins_loaded = Vec::new(); | ||
for plugin in plugins { | ||
let bytes = std::fs::read(&plugin).unwrap(); | ||
let module = Module::new(&store, &bytes); | ||
if let Ok(module) = module { | ||
let mut imports = imports! {}; | ||
let funs = Manifest::instantiate(&mut store, &module, &mut imports); | ||
if let Ok((manifest, _)) = funs { | ||
let name = manifest.name(&mut store).unwrap(); | ||
let description = manifest.description(&mut store).unwrap(); | ||
let version = manifest.version(&mut store).unwrap(); | ||
let permissions = manifest | ||
.permissions(&mut store) | ||
.unwrap() | ||
.into_iter() | ||
.map(|s| match s.as_str() { | ||
"path_provider" => Permission::PathProvider, | ||
_ => unimplemented!(), | ||
}) | ||
.collect(); | ||
let path = plugin.clone(); | ||
let plugin = Plugin { | ||
name, | ||
description, | ||
version, | ||
permissions, | ||
path, | ||
}; | ||
plugins_loaded.push(plugin); | ||
} else { | ||
let error = funs.err().unwrap(); | ||
error!("Failed to load plugin {:?}", error); | ||
} | ||
} | ||
} | ||
plugins_loaded | ||
} else { | ||
vec![] | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug)] | ||
pub struct Plugin { | ||
name: String, | ||
description: String, | ||
version: i64, | ||
permissions: Vec<Permission>, | ||
path: PathBuf, | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
enum Permission { | ||
PathProvider, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use std::ops::DerefMut; | ||
|
||
use mut_static::MutStatic; | ||
use wit_bindgen_wasmer::wasmer::{imports, Cranelift, Module, Store}; | ||
|
||
use crate::environment::{ENV, PATH_PROVIDER_PLUGINS, PLUGINS}; | ||
|
||
use self::path_provider::PathProvider; | ||
|
||
use super::Permission; | ||
use log::error; | ||
|
||
wit_bindgen_wasmer::import!("assets/plugins/path_provider.wit"); | ||
|
||
pub fn get_storage_path() -> String { | ||
let mut plugin = (*PATH_PROVIDER_PLUGINS).write().unwrap(); | ||
let plugin = plugin.deref_mut().first_mut().unwrap(); | ||
let path = plugin | ||
.provider | ||
.get_storage_path(&mut plugin.store, &(&*ENV).data_path.display().to_string()); | ||
if path.is_err() { | ||
error!("Failed to get storage path"); | ||
} | ||
path.unwrap() | ||
} | ||
|
||
pub fn get_file_path(block_id: &str) -> String { | ||
let mut plugin = (*PATH_PROVIDER_PLUGINS).write().unwrap(); | ||
let plugin = plugin.deref_mut().first_mut().unwrap(); | ||
let path = plugin.provider.get_file_path( | ||
&mut plugin.store, | ||
&(&*ENV).data_path.display().to_string(), | ||
block_id, | ||
); | ||
if path.is_err() { | ||
error!("Failed to get storage path"); | ||
} | ||
path.unwrap() | ||
} | ||
|
||
pub fn load_path_provider_plugins() -> MutStatic<Vec<PathProviderPlugin>> { | ||
// let plugins_dir = PathBuf::from("plugins"); | ||
let plugins = (&*PLUGINS) | ||
.iter() | ||
.filter(|p| p.permissions.contains(&Permission::PathProvider)); | ||
let mut plugins_loaded = Vec::new(); | ||
for plugin in plugins { | ||
let engine = Cranelift::default(); | ||
let mut store = Store::new(engine); | ||
let bytes = std::fs::read(&plugin.path).unwrap(); | ||
let module = Module::new(&store, &bytes); | ||
if let Ok(module) = module { | ||
let mut imports = imports! {}; | ||
let funs = PathProvider::instantiate(&mut store, &module, &mut imports); | ||
if let Ok((provider, _)) = funs { | ||
let plugin = PathProviderPlugin { | ||
store, | ||
module, | ||
provider, | ||
}; | ||
plugins_loaded.push(plugin); | ||
} else { | ||
let error = funs.err().unwrap(); | ||
error!("Failed to load plugin {:?}", error); | ||
} | ||
} | ||
} | ||
MutStatic::from(plugins_loaded) | ||
} | ||
|
||
#[allow(unused)] | ||
pub struct PathProviderPlugin { | ||
store: Store, | ||
module: Module, | ||
provider: PathProvider, | ||
} |