From 8cb532b6a1083332b2db06e73e148663841fd34a Mon Sep 17 00:00:00 2001 From: Stephen Akinyemi Date: Mon, 23 May 2022 16:36:19 +0100 Subject: [PATCH] Fix node build, wnfs script and bump version --- Cargo.lock | 4 +- README.md | 26 +++-- crates/fs/Cargo.toml | 4 +- crates/fs/README.md | 8 +- crates/wasm/Cargo.toml | 8 +- crates/wasm/README.md | 24 ++--- crates/wasm/package.json | 21 ----- crates/wasm/test-wnfs/index.js | 11 +++ crates/wasm/test-wnfs/package.json | 11 +++ crates/wasm/test-wnfs/store.js | 36 +++++++ scripts/wnfs.sh | 146 +++++++++++++++++++++++------ 11 files changed, 215 insertions(+), 84 deletions(-) create mode 100644 crates/wasm/test-wnfs/index.js create mode 100644 crates/wasm/test-wnfs/package.json create mode 100644 crates/wasm/test-wnfs/store.js diff --git a/Cargo.lock b/Cargo.lock index 8fc46311..c3725cd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1540,7 +1540,7 @@ dependencies = [ [[package]] name = "wasm-wnfs" -version = "0.1.2" +version = "0.1.5" dependencies = [ "anyhow", "async-trait", @@ -1665,7 +1665,7 @@ checksum = "d19538ccc21819d01deaf88d6a17eae6596a12e9aafdbb97916fb49896d89de9" [[package]] name = "wnfs" -version = "0.1.2" +version = "0.1.5" dependencies = [ "anyhow", "async-recursion", diff --git a/README.md b/README.md index f76a64f4..81802f04 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ A goal of the project is to be easily compiled to WebAssembly to be used in the Creating a new public directory. -```rs +```rust use wnfs::{PublicDirectory, Id}; use chrono::Utc; @@ -55,7 +55,7 @@ println!("id = {}", dir.get_id()); The in-memory files and directories you create with `wnfs` will need to be sealed and stored somewhere. For that, an object that implements the BlockStore trait like [this one](https://github.com/WebNativeFileSystem/rs-wnfs/blob/8bb0fbb457051295f1ed4a4707dc230c04612658/crates/fs/common/blockstore.rs#L42-L62) can be used. -```rs +```rust use wnfs::{PublicDirectory, MemoryBlockStore, ipld::Cid}; use chrono::Utc; @@ -69,7 +69,7 @@ The WNFS API is immutable, therefore, we need to keep track of the updated root Each fs operation returns a possibly updated root directory that subsequent changes can be applied on. -```rs +```rust // ... let dir = Rc::new(dir); @@ -169,9 +169,9 @@ let OpResult { result, .. } = root_dir -- **The _wnfs_ Helper Script** +- **The _wnfs_ Command** - If you are on a Unix platform, you can optionally install the `wnfs` script. + You can optionally set up the `wnfs` script.
Read more @@ -182,7 +182,7 @@ let OpResult { result, .. } = root_dir sh script/wnfs.sh setup ``` - - This lets you run the `wnfs.sh` script with just the `wnfs` command. + - This lets you run the `wnfs.sh` script as a command. ```bash wnfs help @@ -206,8 +206,16 @@ let OpResult { result, .. } = root_dir - Build the project + Check [REQUIREMENTS](#requirements) on how to set up the `wnfs` command. + + ```bash + wnfs build --all + ``` + +- You can also build for specific crates + ```bash - sh scripts/wnfs.sh build + wnfs build --wasm ``` ## Testing the Project @@ -215,11 +223,11 @@ let OpResult { result, .. } = root_dir - Run all tests ```bash - sh scripts/wnfs.sh test + wnfs test --all ``` - Show code coverage ```bash - sh scripts/wnfs.sh coverage + wnfs coverage ``` diff --git a/crates/fs/Cargo.toml b/crates/fs/Cargo.toml index a537e50f..bdd5eff7 100644 --- a/crates/fs/Cargo.toml +++ b/crates/fs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wnfs" -version = "0.1.2" +version = "0.1.5" description = "WebNative filesystem core implementation" keywords = ["wnfs", "webnative", "ipfs", "decentralisation"] categories = [ @@ -10,7 +10,7 @@ categories = [ "wasm", ] license-file = "../../LICENSE" -readme = "README.md" +readme = "Apache-2.0" edition = "2021" repository = "https://github.com/WebNativeFileSystem/rs-wnfs/tree/main/crates/fs" homepage = "https://fission.codes" diff --git a/crates/fs/README.md b/crates/fs/README.md index 3210a1e8..5f1a2cce 100644 --- a/crates/fs/README.md +++ b/crates/fs/README.md @@ -27,8 +27,6 @@

-
:warning: Work in progress :warning:
- ## This crate is a Rust implementation of the primitives for creating and manipulating IPLD graphs that encode WNFS. @@ -45,7 +43,7 @@ A goal of the project is to be easily compiled to WebAssembly to be used in the Creating a new public directory. -```rs +```rust use wnfs::{PublicDirectory, Id}; use chrono::Utc; @@ -55,7 +53,7 @@ println!("id = {}", dir.get_id()); The in-memory files and directories you create with `wnfs` will need to be sealed and stored somewhere. For that, an object that implements the BlockStore trait like [this one](https://github.com/WebNativeFileSystem/rs-wnfs/blob/8bb0fbb457051295f1ed4a4707dc230c04612658/crates/fs/common/blockstore.rs#L42-L62) can be used. -```rs +```rust use wnfs::{PublicDirectory, MemoryBlockStore, ipld::Cid}; use chrono::Utc; @@ -69,7 +67,7 @@ The WNFS API is immutable, therefore, we need to keep track of the updated root Each fs operation returns a possibly updated root directory that subsequent changes can be applied on. -```rs +```rust // ... let dir = Rc::new(dir); diff --git a/crates/wasm/Cargo.toml b/crates/wasm/Cargo.toml index c4c5b05c..4b2c5d63 100644 --- a/crates/wasm/Cargo.toml +++ b/crates/wasm/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "wasm-wnfs" -version = "0.1.2" -description = "WebNative filesystem WebAssembly API" +version = "0.1.5" +description = "WebNative Filesystem API (WebAssembly)" keywords = ["wnfs", "webnative", "ipfs", "decentralisation"] categories = [ "filesystem", @@ -9,7 +9,7 @@ categories = [ "web-programming", "wasm", ] -license-file = "LICENSE" +license = "Apache-2.0" readme = "README.md" edition = "2021" repository = "https://github.com/WebNativeFileSystem/rs-wnfs/tree/main/crates/wasm" @@ -18,8 +18,6 @@ authors = ["The Fission Authors"] [dependencies] wnfs = { path = "../fs", version = "0.1.0" } -# serde_json = "1.0" -# serde = { version = "1.0", features = ["derive"] } wasm-bindgen = { version = "0.2", optional = true, features = ["serde-serialize"] } wasm-bindgen-futures = { version = "0.4", optional = true } js-sys = { version = "0.3", optional = true } diff --git a/crates/wasm/README.md b/crates/wasm/README.md index ffac9b73..8f656fd2 100644 --- a/crates/wasm/README.md +++ b/crates/wasm/README.md @@ -7,7 +7,7 @@ The core of this project is a WebAssembly binary compiled from the [Rust source ## Outline - [Usage](#usage) -- [Building the Project](#building-the-project) +- [Setting up the project](#setting-up-the-project) - [Testing the Project](#testing-the-project) - [Publishing Package](#publishing-package) @@ -75,7 +75,7 @@ var { rootDir } = await rootDir.rm(["pictures", "cats"], store); var { result } = await rootDir.ls(["pictures"], store); ``` -## Building the Project +## Setting up the Project - Install `wasm-pack` @@ -83,7 +83,13 @@ var { result } = await rootDir.ls(["pictures"], store); cargo install wasm-pack ``` -- Install playwrigth binaries +- Install dependencies + + ```bash + yarn + ``` + +- Install playwright binaries ```bash npx playwright install @@ -92,7 +98,7 @@ var { result } = await rootDir.ls(["pictures"], store); - Build project ```bash - wasm-pack build --target web + wasm-pack build ``` ## Testing the Project @@ -105,18 +111,14 @@ var { result } = await rootDir.ls(["pictures"], store); ## Publishing Package -- Generate the compilation artifacts in `pkg` directory +- Build the project ```bash - wasm-pack init + wnfs build --wasm ``` - Publish from the `pkg` directory ```bash - cd pkg - ``` - - ```bash - npm publish --access=public + wasm-pack publish --nodejs ``` diff --git a/crates/wasm/package.json b/crates/wasm/package.json index 63e6021f..fbf6035e 100644 --- a/crates/wasm/package.json +++ b/crates/wasm/package.json @@ -1,25 +1,4 @@ { - "name": "wnfs", - "version": "0.1.1", - "homepage": "https://guide.fission.codes", - "license": "Apache-2.0", - "author": { - "name": "Stephen Akinyemi", - "email": "appcypher@fission.codes" - }, - "keywords": [ - "wnfs", - "web", - "webnative", - "filesystem", - "cryptography", - "web-programming", - "wasm" - ], - "repository": { - "type": "git", - "url": "https://github.com/WebNativeFileSystem/rs-wnfs/tree/main/crates/wasm" - }, "devDependencies": { "@playwright/test": "^1.21.1", "@wasm-tool/wasm-pack-plugin": "^1.6.0", diff --git a/crates/wasm/test-wnfs/index.js b/crates/wasm/test-wnfs/index.js new file mode 100644 index 00000000..9c8e8d16 --- /dev/null +++ b/crates/wasm/test-wnfs/index.js @@ -0,0 +1,11 @@ +import { MemoryBlockStore } from "./store.js"; +import { PublicDirectory } from "wnfs"; + +const time = new Date(); +const dir = new PublicDirectory(time); +const store = new MemoryBlockStore(); + +// Create a /pictures/cats directory. +var { rootDir } = await dir.mkdir(["pictures", "cats"], time, store); + +console.log("Root Id =", rootDir.getId()); diff --git a/crates/wasm/test-wnfs/package.json b/crates/wasm/test-wnfs/package.json new file mode 100644 index 00000000..3878e46e --- /dev/null +++ b/crates/wasm/test-wnfs/package.json @@ -0,0 +1,11 @@ +{ + "name": "test", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "dependencies": { + "multiformats": "^9.6.5", + "wnfs": "../pkg/" + }, + "type": "module" +} diff --git a/crates/wasm/test-wnfs/store.js b/crates/wasm/test-wnfs/store.js new file mode 100644 index 00000000..1ba5d85c --- /dev/null +++ b/crates/wasm/test-wnfs/store.js @@ -0,0 +1,36 @@ +import { CID } from "multiformats/cid"; +import { sha256 } from "multiformats/hashes/sha2"; + +/** A mock CID. */ +const sampleCID = CID.parse( + "bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea" +).bytes; + +/** + * An in-memory block store to simulate IPFS. + * + * IPFS is basically a glorified HashMap. + */ +class MemoryBlockStore { + // private store: Map; + + /** Creates a new in-memory block store. */ + constructor() { + this.store = new Map(); + } + + /** Stores an array of bytes in the block store. */ + async getBlock(cid) { + const decoded_cid = CID.decode(cid); + return this.store.get(decoded_cid.toString()); + } + + /** Retrieves an array of bytes from the block store with given CID. */ + async putBlock(bytes, code) { + const hash = await sha256.digest(bytes); + const cid = CID.create(1, code, hash); + this.store.set(cid.toString(), bytes); + } +} + +export { sampleCID, MemoryBlockStore }; diff --git a/scripts/wnfs.sh b/scripts/wnfs.sh index 5076be02..172176e4 100755 --- a/scripts/wnfs.sh +++ b/scripts/wnfs.sh @@ -1,12 +1,19 @@ #!/bin/bash +set -e # PATHS # Get current working directory current_dir=`pwd` -# Get the absolute path of where script is running from -script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd)" -script_path="$script_dir/wnfs.sh" +# Get the absolute path where current script is running from. +script_path=$(readlink -f $(which $0)) + +# Get the canonical directory of script. +if [[ -L $script_path ]]; then + script_dir=$(dirname $(readlink -f $script_path)) +else + script_dir=$(dirname $script_path) +fi # RETURN VARIABLE ret="" @@ -17,6 +24,7 @@ args="${@:2}" # All arguments except the first # COLORS red='\033[0;31m' green='\033[0;32m' +purple='\033[0;35m' none='\033[0m' # DESCRIPTION: @@ -33,6 +41,9 @@ main() { coverage ) coverage ;; + publish ) + publish + ;; setup ) setup ;; @@ -58,11 +69,12 @@ help() { echo " wnfs [COMMAND] [...args]" echo "" echo "COMMAND:" - echo " * build - build project" - echo " * test - run tests" - echo " * coverage - show code coverage" - echo " * setup - install wnfs script" - echo " * -h, help - print this help message" + echo " * build [--wnfs|--wasm|--all] - build projects" + echo " * test [--wnfs|--wasm|--all] - run tests" + echo " * publish [--wnfs|--wasm|--all] - publish packages" + echo " * coverage [--wnfs|--wasm|--all] - show code coverage" + echo " * setup - install wnfs script" + echo " * help - print this help message" echo "" echo "" } @@ -75,38 +87,82 @@ help() { # Builds the project. # # USAGE: -# wnfs build +# wnfs build [--wnfs|--wasm|--all] # build() { - display_header "💿 BUILDING WNFS PROJECT" + if check_flag --wnfs; then + build_wnfs + elif check_flag --wasm; then + build_wasm + else + build_wnfs + build_wasm + fi +} + +build_wnfs() { + display_header "💿 | BUILDING WNFS PROJECT | 💿" cargo build --release +} - display_header "💿 BUILDING WASM-WNFS PROJECT" - cd crates/wasm && wasm-pack build --target web --release +build_wasm() { + display_header "💿 | BUILDING WASM-WNFS PROJECT | 💿" + echo "script_dir = $script_dir" + cd $script_dir/../crates/wasm + wasm-pack build --target nodejs + sed -i ".bak" -e 's/"name": "wasm-wnfs"/"name": "wnfs"/g' pkg/package.json + rm pkg/package.json.bak } # DESCRIPTION: # Runs tests. # # USAGE: -# wnfs test +# wnfs test [--wnfs|--wasm|--all] # test() { - display_header "🧪 RUNNING WNFS TESTS" + if check_flag --wnfs; then + test_wnfs + elif check_flag --wasm; then + test_wasm + else + test_wnfs + test_wasm + fi +} + +test_wnfs() { + display_header "🧪 | RUNNING WNFS TESTS | 🧪" cargo test -p wnfs --release -- --nocapture +} - display_header "🧪 RUNNING WASM-WNFS TESTS" - cd crates/wasm && yarn playwright test +test_wasm() { + display_header "🧪 | RUNNING WASM-WNFS TESTS | 🧪" + echo "script_dir = $script_dir" + cd $script_dir/../crates/wasm + yarn playwright test } # DESCRIPTION: # Shows the code coverage of the project # # USAGE: -# wnfs coverage +# wnfs coverage [--wnfs|--wasm|--all] # coverage() { - displayln "coverage command not implemented yet" + errorln "coverage command not implemented yet" + exit 1 +} + +# DESCRIPTION: +# Publishes the project. +# +# USAGE: +# wnfs publish [--wnfs|--wasm|--all] +# +publish() { + errorln "publish command not implemented yet" + exit 1 } #------------------------------------------------------------------------------ @@ -150,36 +206,68 @@ get_flag_value() { fi } +# DESCRIPTION: +# Checks if the flag is present in the list of arguments +# +check_flag() { + local found=1 + local key=$1 + + # For every argument in the list of arguments + for arg in $args; do + # Check if any of the argument matches the key provided + if [[ $arg = $key ]]; then + found=0 + break + fi + done + + return $found +} + # DESCRIPTION: # Sets up the cript by making it excutable and available system wide # setup() { - display "Make script executable" + displayln "Make script executable" chmod u+x $script_path - display "Drop a link to it in /usr/local/bin" - ln -s $script_path /usr/local/bin/wnfs + displayln "Drop a link to it in /usr/local/bin" + if ln -s $script_path /usr/local/bin/wnfs; then + successln "Successfully installed" + else + local result=$? + errorln "Failed to install" + exit $result + fi } # DESCRIPTION: -# A custom print function +# Prints a message. # -display() { - printf "::: $1 :::\n" +displayln() { + printf "\n::: $1 :::\n" } # DESCRIPTION: -# A custom print function that starts its output with a newline +# Prints an error message. # -displayln() { - printf "\n::: $1 :::\n" +errorln() { + printf "\n${red}::: $1 :::${none}\n\n" +} + +# DESCRIPTION: +# Prints an success message. +# +successln() { + printf "\n${green}::: $1 :::${none}\n\n" } # DESCRIPTION: -# A custom print function for headers. +# Prints a header message. # display_header() { - printf "\n${green}$1${none}\n\n" + printf "\n${purple}$1${none}\n\n" } main $@