development of SDK while testing with 3rd party app

refactor
Niko PLP 1 day ago
parent fa87233fc9
commit d23759bb1c
  1. 1
      Cargo.lock
  2. 3
      Cargo.toml
  3. 1
      engine/broker/Cargo.toml
  4. 1
      engine/broker/auth/index.html
  5. 1
      engine/broker/auth/package.json
  6. 6
      engine/broker/auth/src/App.svelte
  7. 4
      engine/broker/auth/src/routes/Home.svelte
  8. 2
      engine/broker/auth/vite.config.js
  9. 31
      engine/broker/src/server_ws.rs
  10. 2
      infra/ngnet/Cargo.toml
  11. 19
      infra/ngnet/README.md
  12. 9
      infra/ngnet/auth/src/main.ts
  13. 2
      infra/ngnet/bootstrap/src/App.svelte
  14. 4
      infra/ngnet/redir/src/lib/Home.svelte
  15. 18
      infra/ngnet/src/main.rs
  16. 3
      package.json
  17. 2
      sdk/js/lib-wasm/Cargo.toml
  18. 42
      sdk/js/lib-wasm/DEV.md
  19. 1
      sdk/js/web/package.json
  20. 6
      sdk/js/web/src/index.ts

1
Cargo.lock generated

@ -3691,6 +3691,7 @@ dependencies = [
"ng-storage-rocksdb", "ng-storage-rocksdb",
"ng-verifier", "ng-verifier",
"once_cell", "once_cell",
"reqwest 0.11.27",
"rust-embed", "rust-embed",
"serde", "serde",
"serde_bare", "serde_bare",

@ -77,4 +77,5 @@ opt-level = 2
[workspace.metadata.scripts] [workspace.metadata.scripts]
libwasm = "cd sdk/js/lib-wasm && cargo run-script app && cd ../../.." libwasm = "cd sdk/js/lib-wasm && cargo run-script app && cd ../../.."
libwasmdev = "cd sdk/js/lib-wasm && cargo run-script appdev && cd ../../.."

@ -26,6 +26,7 @@ async-trait = "0.1.64"
rust-embed = { version = "6.7.0", features = ["include-exclude","debug-embed"] } rust-embed = { version = "6.7.0", features = ["include-exclude","debug-embed"] }
urlencoding = "2.1.3" urlencoding = "2.1.3"
blake3 = "1.3.1" blake3 = "1.3.1"
reqwest = { version = "0.11.18", features = ["blocking", "native-tls-vendored"] }
ng-async-tungstenite = { version = "0.22.2", git = "https://git.nextgraph.org/NextGraph/async-tungstenite.git", branch = "nextgraph", features = [ ng-async-tungstenite = { version = "0.22.2", git = "https://git.nextgraph.org/NextGraph/async-tungstenite.git", branch = "nextgraph", features = [
"async-std-runtime", "async-std-runtime",
] } ] }

@ -11,6 +11,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<base href="http://localhost:14401" />
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NextGraph Auth</title> <title>NextGraph Auth</title>

@ -6,6 +6,7 @@
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vite build --base=./ && node prepare-app-file.cjs && shx rm -rf ./dist/assets", "build": "vite build --base=./ && node prepare-app-file.cjs && shx rm -rf ./dist/assets",
"dev3": "cross-env NG_DEV3=1 vite",
"builddev": "cross-env NG_DEV=1 vite build --base=./ && shx rm -rf ./dist/assets && shx mkdir -p ../../../app/nextgraph/public_dev && cp ./dist/index.html ../../../app/nextgraph/public_dev/appauth.html", "builddev": "cross-env NG_DEV=1 vite build --base=./ && shx rm -rf ./dist/assets && shx mkdir -p ../../../app/nextgraph/public_dev && cp ./dist/index.html ../../../app/nextgraph/public_dev/appauth.html",
"preview": "vite preview" "preview": "vite preview"
}, },

@ -90,11 +90,11 @@
// wallets.set(walls); // wallets.set(walls);
// }; // };
const redirect_server = import.meta.env.NG_REDIR_SERVER || "nextgraph.net"; const redirect_server = import.meta.env.NG_REDIR_SERVER || "nextgraph.net";
const iframe_config = import.meta.env.DEV ? {src:"http://localhost:14404/?o=", origin: "http://localhost:14404"} : const iframe_config =
import.meta.env.NG_DEV3 ? {src:"http://127.0.0.1:3033/auth/?o=", origin: "http://127.0.0.1:3033"} :
import.meta.env.DEV ? {src:"http://localhost:14404/?o=", origin: "http://localhost:14404"} :
import.meta.env.NG_DEV ? {src:"http://localhost:1421/auth.html?o=", origin: "http://localhost:1421"} : import.meta.env.NG_DEV ? {src:"http://localhost:1421/auth.html?o=", origin: "http://localhost:1421"} :
{src:`https://${redirect_server}/auth/?o=`, origin: `https://${redirect_server}`} ; {src:`https://${redirect_server}/auth/?o=`, origin: `https://${redirect_server}`} ;
// to test ngnet
//const iframe_config = {src:"http://127.0.0.1:3033/auth/?o=", origin: "http://127.0.0.1:3033"};
onMount(async () => { onMount(async () => {

@ -25,6 +25,7 @@
derived, derived,
cannot_load_offline, cannot_load_offline,
} from "@ng-org/ui-common/store"; } from "@ng-org/ui-common/store";
import { origin } from "../store";
let display_login_create = !$has_wallets || !$active_wallet; let display_login_create = !$has_wallets || !$active_wallet;
let unsubscribe; let unsubscribe;
@ -40,6 +41,9 @@
push("#/wallet/login"); push("#/wallet/login");
} }
}); });
if (import.meta.env.NG_DEV3 && $origin) {
push("#/?o="+encodeURIComponent($origin));
}
}); });
onDestroy(() => { onDestroy(() => {

@ -23,7 +23,7 @@ const jsToBottom = () => {
export default defineConfig({ export default defineConfig({
envPrefix: ["VITE_", "NG_"], envPrefix: ["VITE_", "NG_"],
server: { server: {
port: 14401 port: 14401,
}, },
worker: { worker: {
format: 'es', format: 'es',

@ -13,6 +13,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; use std::collections::HashSet;
use std::io::Read;
use std::net::IpAddr; use std::net::IpAddr;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
@ -245,6 +246,7 @@ fn upgrade_ws_or_serve_app(
} }
if serve_app && (remote.is_private() || remote.is_loopback()) { if serve_app && (remote.is_private() || remote.is_loopback()) {
log_debug!("GET {}", uri.path_and_query().unwrap().as_str());
if uri == "/" { if uri == "/" {
log_debug!("Serving the app"); log_debug!("Serving the app");
let sha_file = App::get("index.sha256").unwrap(); let sha_file = App::get("index.sha256").unwrap();
@ -272,8 +274,7 @@ fn upgrade_ws_or_serve_app(
.body(Some(file.data.to_vec())) .body(Some(file.data.to_vec()))
.unwrap(); .unwrap();
return Err(res); return Err(res);
} else if uri.path() == "/auth/" { } else if std::env::var("NG_DEV3").is_ok() && uri.path().starts_with("/auth") {
log_debug!("Serving auth app");
// if referer.is_none() || referer.unwrap().to_str().is_err() || referer.unwrap().to_str().unwrap() != "https://nextgraph.net/" { // if referer.is_none() || referer.unwrap().to_str().is_err() || referer.unwrap().to_str().unwrap() != "https://nextgraph.net/" {
// return Err(make_error(StatusCode::FORBIDDEN)); // return Err(make_error(StatusCode::FORBIDDEN));
// } // }
@ -290,6 +291,32 @@ fn upgrade_ws_or_serve_app(
// } // }
// None => return Err(make_error(StatusCode::BAD_REQUEST)), // None => return Err(make_error(StatusCode::BAD_REQUEST)),
// }; // };
let path = format!(
"http://localhost:14401{}",
uri.path_and_query()
.unwrap()
.as_str()
.get(5..)
.unwrap_or("")
);
log_debug!("SERVING AUTH APP from {path}");
match reqwest::blocking::get(path) {
Err(e) => return Err(make_error(StatusCode::NOT_FOUND)),
Ok(mut res) => {
use std::io::{Cursor, Write};
let mut builder = Response::builder().status(res.status());
for (name, value) in res.headers().into_iter() {
builder = builder.header(name, value);
}
let mut buffer: Vec<u8> = vec![];
let mut cursor = Cursor::new(&mut buffer);
res.copy_to(&mut cursor);
return Err(builder.body(Some(buffer)).unwrap());
}
}
} else if uri == "/auth/" {
log_debug!("Serving auth app");
let sha_file = AppAuth::get("index.sha256").unwrap(); let sha_file = AppAuth::get("index.sha256").unwrap();
let sha = format!( let sha = format!(
"\"{}\"", "\"{}\"",

@ -15,6 +15,8 @@ rust-version.workspace = true
[package.metadata.scripts] [package.metadata.scripts]
buildfront = "pnpm -C ./web build && pnpm -C ./bootstrap build && pnpm -C ./auth build && pnpm -C ./redir build" buildfront = "pnpm -C ./web build && pnpm -C ./bootstrap build && pnpm -C ./auth build && pnpm -C ./redir build"
buildfrontdev = "pnpm -C ./auth builddev && pnpm -C ./bootstrap builddev && pnpm -C ./redir builddev && pnpm -C ./web build"
buildfrontdev3 = "pnpm -C ./auth build && pnpm -C ./web build && pnpm -C ./bootstrap builddev && pnpm -C ./redir build"
[dependencies] [dependencies]
serde = { version = "1.0.142", features = ["derive"] } serde = { version = "1.0.142", features = ["derive"] }

@ -11,27 +11,28 @@ npm install -g pnpm
pnpm install pnpm install
``` ```
## Dev ## Dev (of the app)
compile the 3 front-ends, in dev mode compile the 4 front-ends, in dev mode
``` ```
pnpm -C ./auth builddev cargo run-script buildfrontdev
pnpm -C ./bootstrap builddev
pnpm -C ./redir builddev
``` ```
```bash ```bash
cd web
pnpm run dev --host
# In another terminal... in the folder ngnet
cargo watch -c -w src -x run cargo watch -c -w src -x run
``` ```
> Currently, the ngnet server api is listening on http://127.0.0.1:3033 only, which might cause you trouble with Android emulator (hardcoded in `main.rs`). > Currently, the ngnet server api is listening on http://127.0.0.1:3033 only, which might cause you trouble with Android emulator (hardcoded in `main.rs`).
> If you need to test from a (virtual) android device, you can use adb to tunnel the connection like: [`adb reverse tcp:3033 tcp:3033`](https://justinchips.medium.com/proxying-adb-client-connections-2ab495f774eb). > If you need to test from a (virtual) android device, you can use adb to tunnel the connection like: [`adb reverse tcp:3033 tcp:3033`](https://justinchips.medium.com/proxying-adb-client-connections-2ab495f774eb).
## Dev (of the SDK)
```
cargo run-script buildfrontdev3
cargo run
```
## Prod ## Prod
``` ```

@ -9,11 +9,12 @@
const searchParams = new URLSearchParams(window.location.search); const searchParams = new URLSearchParams(window.location.search);
let o = searchParams.get("o"); let o = searchParams.get("o");
let parent_origin = (new URL(o)).origin;
let web_origin; let web_origin;
let web_redirect; let web_redirect;
let wallet_port; let wallet_port;
let web_origin_host; //let web_origin_host;
let session; let session;
async function rpc( method:string, port: MessagePort, args?: any) : Promise<any> { async function rpc( method:string, port: MessagePort, args?: any) : Promise<any> {
@ -25,16 +26,16 @@ async function rpc( method:string, port: MessagePort, args?: any) : Promise<any>
window.addEventListener("message", async (event)=>{ window.addEventListener("message", async (event)=>{
//console.log("ngnet auth got msg from", event.origin, event.data); //console.log("ngnet auth got msg from", event.origin, event.data);
const { method, port } = event.data; const { method, port } = event.data;
if (event.origin === o) { if (event.origin === parent_origin) {
if (event.data.ready) return; if (event.data.ready) return;
if ( method === "init" ) { if ( method === "init" ) {
web_redirect = event.data.manifest.origin; web_redirect = event.data.manifest.origin;
let url = new URL(web_redirect); let url = new URL(web_redirect);
web_origin = url.origin; web_origin = url.origin;
web_origin_host = url.host; //web_origin_host = url.host;
session = event.data.session; session = event.data.session;
port.onclose = () => { port.onclose = () => {
console.error("BSP parent window closed its port with nextgraph.net"); console.error("BSP parent window closed its port with us, te redirecting server");
}; };
wallet_port = port; wallet_port = port;

@ -36,7 +36,7 @@
const bsp_list = [ const bsp_list = [
"https://nextgraph.eu", "https://nextgraph.eu",
"https://nextgraph.one", "https://nextgraph.one",
"https://stage1.nextgraph.eu", "https://stage1.nextgraph.eu"
]; ];
let channel; let channel;

@ -39,8 +39,8 @@
let broker = broker_info[1]; let broker = broker_info[1];
broker_name = broker_info[0]; broker_name = broker_info[0];
let url; let url;
if (import.meta.env.DEV && broker.localhost === 1421) { if ( import.meta.env.DEV && broker.localhost === 1421 ) {
// dev mode // dev mode
url = "http://localhost:14401/"; url = "http://localhost:14401/";
} else if (import.meta.env.NG_DEV && broker.localhost === 14400) { } else if (import.meta.env.NG_DEV && broker.localhost === 14400) {
// dev mode // dev mode

@ -136,13 +136,19 @@ async fn main() -> anyhow::Result<()> {
.map(|reply, p: HashMap<String, String>| match p.get("o") { .map(|reply, p: HashMap<String, String>| match p.get("o") {
Some(obj) => { Some(obj) => {
let decoded = obj.trim(); let decoded = obj.trim();
if BSP_DETAILS.get(decoded).is_none() let mut allowed = false;
&& decoded != "http://localhost:14400" #[cfg(debug_assertions)]
&& decoded != "http://localhost:1421"
// if decoded.eq("*")
// || (!decoded.starts_with("http://") && !decoded.starts_with("https://"))
// || decoded.len() < 11
{ {
if decoded == "http://localhost:14401" {
allowed = true;
}
}
allowed = allowed
|| BSP_DETAILS.get(decoded).is_some()
|| decoded == "http://localhost:14400"
|| decoded == "http://localhost:1421";
if !allowed {
warp::http::StatusCode::BAD_REQUEST.into_response() warp::http::StatusCode::BAD_REQUEST.into_response()
} else { } else {
let reply = warp::reply::with_header( let reply = warp::reply::with_header(

@ -2,7 +2,8 @@
"name": "ng-org", "name": "ng-org",
"private": true, "private": true,
"version": "0.1.2", "version": "0.1.2",
"scripts": { "scripts": {
"buildfrontdev3": "cargo run-script libwasmdev && pnpm -C ./sdk/js/web builddev3 && pnpm -C ./engine/broker/auth build",
"buildfront": "cargo run-script libwasm && pnpm -C ./app/nextgraph install && pnpm -C ./app/nextgraph webbuild && pnpm -C ./sdk/js/web build && pnpm -C ./engine/broker/auth install && pnpm -C ./engine/broker/auth build", "buildfront": "cargo run-script libwasm && pnpm -C ./app/nextgraph install && pnpm -C ./app/nextgraph webbuild && pnpm -C ./sdk/js/web build && pnpm -C ./engine/broker/auth install && pnpm -C ./engine/broker/auth build",
"buildfrontdev": "pnpm -C ./engine/broker/auth builddev && pnpm -C ./infra/ngnet/bootstrap builddev && pnpm -C ./infra/ngnet/auth builddev && pnpm -C ./infra/ngnet/redir builddev && pnpm -C ./sdk/js/web builddev" "buildfrontdev": "pnpm -C ./engine/broker/auth builddev && pnpm -C ./infra/ngnet/bootstrap builddev && pnpm -C ./infra/ngnet/auth builddev && pnpm -C ./infra/ngnet/redir builddev && pnpm -C ./sdk/js/web builddev"
}, },

@ -17,7 +17,7 @@ rust-version.workspace = true
wasm-opt = false wasm-opt = false
[package.metadata.scripts] [package.metadata.scripts]
appdev = "rm -rf pkg/snippets && wasm-pack build --dev --target bundler && node prepare-web.js" appdev = "wasm-pack build --dev --target bundler && node prepare-web.js"
app = "rm -rf pkg/snippets && wasm-pack build --target bundler && node prepare-web.js" app = "rm -rf pkg/snippets && wasm-pack build --target bundler && node prepare-web.js"
nodedev = "rm -rf pkg-node/snippets && wasm-pack build --dev -t nodejs -d pkg-node && node prepare-node.js" nodedev = "rm -rf pkg-node/snippets && wasm-pack build --dev -t nodejs -d pkg-node && node prepare-node.js"
node = "rm -rf pkg-node/snippets && wasm-pack build -t nodejs -d pkg-node && node prepare-node.js" node = "rm -rf pkg-node/snippets && wasm-pack build -t nodejs -d pkg-node && node prepare-node.js"

@ -42,6 +42,48 @@ Or automated testing with headless chrome:
wasm-pack test --chrome --headless wasm-pack test --chrome --headless
``` ```
## Developing against a third party webapp
in a separate terminal, from the root of the mono-repo :
```
pnpm buildfrontdev3
cd engine/broker/auth
pnpm dev3
```
in a separate terminal, from the root of the mono-repo :
```
cd infra/ngnet
cargo run-script buildfrontdev3
cargo run
```
start your local ngd
```
export NG_DEV3=1; cargo run -r -p ngd -- -vv --save-key -l 14400
// then log in to your account by opening
http://localhost:14400
```
finally, start your local third party webapp you will use to test the WASM SDK
```
// this is up to you. by example :
cd sdk/js/examples/multi-framework-signals
pnpm dev
// then open that app in your browser
```
every time you modify the SDK, re-run (at the root of momo-repo) :
```
cargo run-script libwasmdev
// or in the sdk/js/lib-wasm folder run cargo run-script appdev
```
## Production build ## Production build
``` ```

@ -31,6 +31,7 @@
"build": "tsc && vite build", "build": "tsc && vite build",
"builddevlocal": "tsc && cross-env NG_DEV=1 vite build", "builddevlocal": "tsc && cross-env NG_DEV=1 vite build",
"builddev": "tsc && cross-env NG_DEV_LOCAL_BROKER=1 vite build", "builddev": "tsc && cross-env NG_DEV_LOCAL_BROKER=1 vite build",
"builddev3": "tsc && cross-env NG_DEV3=1 vite build",
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {

@ -12,11 +12,11 @@ import { createAsyncProxy } from "async-proxy";
let initialized = false; let initialized = false;
const redirect_server = import.meta.env.NG_REDIR_SERVER || "nextgraph.net"; const redirect_server = import.meta.env.NG_REDIR_SERVER || "nextgraph.net";
const config = import.meta.env.NG_DEV ? {redirect:"http://localhost:14402/#/?o=", origin: "http://localhost:14404"} : const config =
import.meta.env.NG_DEV3 ? {redirect:"http://127.0.0.1:3033/redir/#/?o=", origin: "http://127.0.0.1:3033"} :
import.meta.env.NG_DEV ? {redirect:"http://localhost:14402/#/?o=", origin: "http://localhost:14404"} :
import.meta.env.NG_DEV_LOCAL_BROKER ? {redirect:"http://localhost:1421/redir.html#/?o=", origin: "http://localhost:1421"} : import.meta.env.NG_DEV_LOCAL_BROKER ? {redirect:"http://localhost:1421/redir.html#/?o=", origin: "http://localhost:1421"} :
{redirect:`https://${redirect_server}/redir/#/?o=`, origin: `https://${redirect_server}`} ; {redirect:`https://${redirect_server}/redir/#/?o=`, origin: `https://${redirect_server}`} ;
// to test ngnet
//const config = {redirect:"http://127.0.0.1:3033/auth/#/?o=", origin: "http://127.0.0.1:3033"};
export const init = async function(callback:Function | null, singleton:boolean, access_requests:any) { export const init = async function(callback:Function | null, singleton:boolean, access_requests:any) {
if (!window) throw new Error("init(callback,..) can only be called from a browser's window"); if (!window) throw new Error("init(callback,..) can only be called from a browser's window");

Loading…
Cancel
Save