From fed970b55d202a997606e04ce7332390e6ab3c13 Mon Sep 17 00:00:00 2001 From: Niko PLP Date: Tue, 4 Mar 2025 02:20:31 +0200 Subject: [PATCH] more examples for nodejs SDK --- ng-app/package.json | 2 +- ng-sdk-js/Cargo.toml | 12 +-- ng-sdk-js/app-node/README.md | 42 +++++++++ ng-sdk-js/app-node/index.js | 86 +++++++++++++----- ng-sdk-js/app-node/package-lock.json | 128 --------------------------- ng-sdk-js/app-node/package.json | 2 +- 6 files changed, 114 insertions(+), 158 deletions(-) create mode 100644 ng-sdk-js/app-node/README.md delete mode 100644 ng-sdk-js/app-node/package-lock.json diff --git a/ng-app/package.json b/ng-app/package.json index ce624fc..3769542 100644 --- a/ng-app/package.json +++ b/ng-app/package.json @@ -67,7 +67,7 @@ "immutable-json-patch": "^6.0.1", "katex": "^0.16.11", "lodash.debounce": "4.0.8", - "ng-sdk-js": "workspace:^0.1.1-alpha", + "ng-sdk-js": "workspace:^0.1.1", "prism-themes": "^1.9.0", "prosemirror-model": "^1.7.1", "prosemirror-state": "^1.2.3", diff --git a/ng-sdk-js/Cargo.toml b/ng-sdk-js/Cargo.toml index 5b3b147..ead3845 100644 --- a/ng-sdk-js/Cargo.toml +++ b/ng-sdk-js/Cargo.toml @@ -17,12 +17,12 @@ rust-version.workspace = true wasm-opt = false [package.metadata.scripts] -appdev = "rm -rf pkg && wasm-pack build --target bundler" -app = "rm -rf pkg && wasm-pack build --target bundler" -nodedev = "rm -rf pkg-node && wasm-pack build --dev -t nodejs -d pkg-node && node prepare-node.js" -node = "rm -rf pkg-node && wasm-pack build -t nodejs -d pkg-node && node prepare-node.js" -web = "rm -rf web && wasm-pack build --target web -d web" -webdev = "rm -rf web && wasm-pack build --dev --target web -d web" +appdev = "rm -rf pkg/snippets && wasm-pack build --target bundler" +app = "rm -rf pkg/snippets && wasm-pack build --target bundler" +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" +web = "rm -rf web/snippets && wasm-pack build --target web -d web" +webdev = "rm -rf web/snippets && wasm-pack build --dev --target web -d web" [lib] crate-type = ["cdylib"] diff --git a/ng-sdk-js/app-node/README.md b/ng-sdk-js/app-node/README.md new file mode 100644 index 0000000..eb85732 --- /dev/null +++ b/ng-sdk-js/app-node/README.md @@ -0,0 +1,42 @@ +# app-node + +NodeJS demo client of NextGraph + +## NextGraph + +> NextGraph brings about the convergence of P2P and Semantic Web technologies, towards a decentralized, secure and privacy-preserving cloud, based on CRDTs. +> +> This open source ecosystem provides solutions for end-users (a platform) and software developers (a framework), wishing to use or create **decentralized** apps featuring: **live collaboration** on rich-text documents, peer to peer communication with **end-to-end encryption**, offline-first, **local-first**, portable and interoperable data, total ownership of data and software, security and privacy. Centered on repositories containing **semantic data** (RDF), **rich text**, and structured data formats like **JSON**, synced between peers belonging to permissioned groups of users, it offers strong eventual consistency, thanks to the use of **CRDTs**. Documents can be linked together, signed, shared securely, queried using the **SPARQL** language and organized into sites and containers. +> +> More info here [https://nextgraph.org](https://nextgraph.org) + +## For contributors + +Build the JS SDK + +``` +cd .. +cargo run-script node +``` + +``` +cd app-node +npm install --no-save ../pkg-node +npm start +``` + +Open this URL in browser : [http://localhost:8080](http://localhost:8080) + +## License + +Licensed under either of + +- Apache License, Version 2.0 ([LICENSE-APACHE2](LICENSE-APACHE2) or http://www.apache.org/licenses/LICENSE-2.0) +- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + at your option. + +`SPDX-License-Identifier: Apache-2.0 OR MIT` + +--- + +NextGraph received funding through the [NGI Assure Fund](https://nlnet.nl/assure) and the [NGI Zero Commons Fund](https://nlnet.nl/commonsfund/), both funds established by [NLnet](https://nlnet.nl/) Foundation with financial support from the European Commission's [Next Generation Internet](https://ngi.eu/) programme, under the aegis of DG Communications Networks, Content and Technology under grant agreements No 957073 and No 101092990, respectively. diff --git a/ng-sdk-js/app-node/index.js b/ng-sdk-js/app-node/index.js index 263aad8..b5833f5 100644 --- a/ng-sdk-js/app-node/index.js +++ b/ng-sdk-js/app-node/index.js @@ -12,15 +12,6 @@ const WebSocket = require("ws"); const ng = require("nextgraph"); global.WebSocket = WebSocket; -let config = { - // replace server_peer_id and admin_user_key with your own - // replace client_peer_key with a fresh key generated with `ngcli gen-key` (use the private key) - server_peer_id: "pzx0BqespDc0MjvtYmq1b6PRqc4i1mjYRqVbIXOw2RwA", - admin_user_key: "sB2JMURtgd42pWI4lLxCT_cNle-pfWkOLZQ0XyJiFswA", - client_peer_key: "GRP0QnlzaB8o2vdiBaNoOYDNOFX-uehLZMxeCaG3JA0A", - server_addr: "127.0.0.1:14400" -}; - // get your wallet file as an ArrayBuffer and pass it to wallet_read_file const fs = require('fs'); @@ -43,13 +34,11 @@ ng.wallet_read_file(buffer).then(async (wallet)=>{ "lemon"], [2, 3, 2, 3]); - console.log(opened_wallet); - let user_id = opened_wallet.V0.personal_site; + let user_id_string = opened_wallet.V0.personal_site_id; let wallet_name = opened_wallet.V0.wallet_id; - console.log(user_id) - console.log(wallet_name) + console.log("wallet_name=", wallet_name) let _client = await ng.wallet_import(wallet, opened_wallet, true) @@ -58,31 +47,75 @@ ng.wallet_read_file(buffer).then(async (wallet)=>{ let session_id = session.session_id; console.log(session); - let protected_repo_id = session.protected_store_id.substr(2,44); + let protected_repo_id = session.protected_store_id.substring(2,46); console.log("Session started. protected store ID = ", protected_repo_id) let info = await ng.client_info(); - console.log(info); let connection_status = await ng.user_connect( info, - user_id + user_id_string ); console.log(connection_status); let dump = await ng.rdf_dump(session_id); + console.log("==== DUMP ===="); console.log(dump); + console.log("==== END of DUMP ===="); + + // we create a new document in the protected store of the user. + //let nuri = await ng.doc_create(session_id, "Graph", "data:graph", "protected", protected_repo_id, "store"); + // once you have created a document, you can reuse its Nuri by entering it in the line below, remove the commenting, and comment out the above line + let nuri = "did:ng:o:W6GCQRfQkNTLtSS_2-QhKPJPkhEtLVh-B5lzpWMjGNEA:v:h8ViqyhCYMS2I6IKwPrY6UZi4ougUm1gpM4QnxlmNMQA"; + console.log("nuri=",nuri); + let base = nuri.substring(0,53); + console.log("base=",base); + + await ng.sparql_update(session_id, "INSERT DATA { <> \"An example value1000\". }", nuri ); + + // SELECT + // we use base to replace <> in the subject + + let sparql_result = await ng.sparql_query(session_id, "SELECT ?p ?o ?g WHERE { GRAPH ?g { <> ?p ?o } }", base); + console.log(sparql_result); + for (const q of sparql_result.results.bindings) { + console.log(q); + } + + // specifying a nuri in the query arguments, is equivalent to settings the GRAPH in the WHERE + sparql_result = await ng.sparql_query(session_id, "SELECT ?s ?p ?o WHERE { ?s ?p ?o }", undefined, nuri); + console.log(sparql_result); + for (const q of sparql_result.results.bindings) { + console.log(q); + } - let nuri = await ng.doc_create(session_id, "Graph", "data:graph", "protected", protected_repo_id, "store"); - console.log(nuri); + // base can be omitted if it isn't used - // await ng.sparql_update(session_id, - // "INSERT DATA { \"An example value30\". }", nuri ); + sparql_result = await ng.sparql_query(session_id, "SELECT ?s ?p ?o ?g WHERE { GRAPH ?g { ?s ?p ?o } }"); + console.log(sparql_result); + for (const q of sparql_result.results.bindings) { + console.log(q); + } + + // CONSTRUCT + + let triples = await ng.sparql_query(session_id, `CONSTRUCT { ?s ?p ?o } WHERE { GRAPH <${nuri}> { ?s ?p ?o } }`, base); + for (const q of triples) { + console.log(q.subject.toString(), q.predicate.toString(), q.object.toString()) + } + + // is equivalent to + + triples = await ng.sparql_query(session_id, "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }", base, nuri); + for (const q of triples) { + console.log(q.subject.toString(), q.predicate.toString(), q.object.toString()) + } // cleaning up - await ng.user_disconnect(user_id); - await ng.session_stop(user_id); + await ng.user_disconnect(user_id_string); + + await ng.session_stop(user_id_string); await ng.wallet_close(wallet_name); @@ -94,6 +127,15 @@ ng.wallet_read_file(buffer).then(async (wallet)=>{ console.error(err); }); +// let config = { +// // replace server_peer_id and admin_user_key with your own +// // replace client_peer_key with a fresh key generated with `ngcli gen-key` (use the private key) +// server_peer_id: "pzx0BqespDc0MjvtYmq1b6PRqc4i1mjYRqVbIXOw2RwA", +// admin_user_key: "sB2JMURtgd42pWI4lLxCT_cNle-pfWkOLZQ0XyJiFswA", +// client_peer_key: "GRP0QnlzaB8o2vdiBaNoOYDNOFX-uehLZMxeCaG3JA0A", +// server_addr: "127.0.0.1:14400" +// }; + // ng.init_headless(config).then( async() => { // let session_id; // try { diff --git a/ng-sdk-js/app-node/package-lock.json b/ng-sdk-js/app-node/package-lock.json deleted file mode 100644 index 6626c18..0000000 --- a/ng-sdk-js/app-node/package-lock.json +++ /dev/null @@ -1,128 +0,0 @@ -{ - "name": "ng-app-node", - "version": "0.1.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "ng-app-node", - "version": "0.1.0", - "license": "(MIT OR Apache-2.0)", - "dependencies": { - "nextgraph": "^0.1.1", - "ws": "^8.13.0" - } - }, - "../pkg-node": { - "name": "nextgraph", - "version": "0.1.0", - "extraneous": true, - "license": "MIT/Apache-2.0" - }, - "node_modules/bufferutil": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.7.tgz", - "integrity": "sha512-kukuqc39WOHtdxtw4UScxF/WVnMFVSQVKhtx3AjZJzhd0RGZZldcrfSEbVsWWe6KNH253574cq5F+wpv0G9pJw==", - "hasInstallScript": true, - "optional": true, - "peer": true, - "dependencies": { - "node-gyp-build": "^4.3.0" - }, - "engines": { - "node": ">=6.14.2" - } - }, - "node_modules/nextgraph": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/nextgraph/-/nextgraph-0.1.1.tgz", - "integrity": "sha512-hTRfI9YVZ+nBaqBq6k2fUIzoaSQER5pMdz/eOHYo6XSMJS9CQNRo8Ko0eHb3rj4gZR3r5ox+G6t4IaoCiAsxsw==" - }, - "node_modules/node-gyp-build": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.6.0.tgz", - "integrity": "sha512-NTZVKn9IylLwUzaKjkas1e4u2DLNcV4rdYagA4PWdPwW87Bi7z+BznyKSRwS/761tV/lzCGXplWsiaMjLqP2zQ==", - "optional": true, - "peer": true, - "bin": { - "node-gyp-build": "bin.js", - "node-gyp-build-optional": "optional.js", - "node-gyp-build-test": "build-test.js" - } - }, - "node_modules/utf-8-validate": { - "version": "5.0.10", - "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.10.tgz", - "integrity": "sha512-Z6czzLq4u8fPOyx7TU6X3dvUZVvoJmxSQ+IcrlmagKhilxlhZgxPK6C5Jqbkw1IDUmFTM+cz9QDnnLTwDz/2gQ==", - "hasInstallScript": true, - "optional": true, - "peer": true, - "dependencies": { - "node-gyp-build": "^4.3.0" - }, - "engines": { - "node": ">=6.14.2" - } - }, - "node_modules/ws": { - "version": "8.13.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz", - "integrity": "sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": ">=5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } - } - }, - "dependencies": { - "bufferutil": { - "version": "4.0.7", - "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.7.tgz", - "integrity": "sha512-kukuqc39WOHtdxtw4UScxF/WVnMFVSQVKhtx3AjZJzhd0RGZZldcrfSEbVsWWe6KNH253574cq5F+wpv0G9pJw==", - "optional": true, - "peer": true, - "requires": { - "node-gyp-build": "^4.3.0" - } - }, - "nextgraph": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/nextgraph/-/nextgraph-0.1.1.tgz", - "integrity": "sha512-hTRfI9YVZ+nBaqBq6k2fUIzoaSQER5pMdz/eOHYo6XSMJS9CQNRo8Ko0eHb3rj4gZR3r5ox+G6t4IaoCiAsxsw==" - }, - "node-gyp-build": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.6.0.tgz", - "integrity": "sha512-NTZVKn9IylLwUzaKjkas1e4u2DLNcV4rdYagA4PWdPwW87Bi7z+BznyKSRwS/761tV/lzCGXplWsiaMjLqP2zQ==", - "optional": true, - "peer": true - }, - "utf-8-validate": { - "version": "5.0.10", - "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.10.tgz", - "integrity": "sha512-Z6czzLq4u8fPOyx7TU6X3dvUZVvoJmxSQ+IcrlmagKhilxlhZgxPK6C5Jqbkw1IDUmFTM+cz9QDnnLTwDz/2gQ==", - "optional": true, - "peer": true, - "requires": { - "node-gyp-build": "^4.3.0" - } - }, - "ws": { - "version": "8.13.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz", - "integrity": "sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==", - "requires": {} - } - } -} diff --git a/ng-sdk-js/app-node/package.json b/ng-sdk-js/app-node/package.json index a9789e7..4e884e5 100644 --- a/ng-sdk-js/app-node/package.json +++ b/ng-sdk-js/app-node/package.json @@ -1,6 +1,6 @@ { "name": "ng-app-node", - "version": "0.1.0", + "version": "0.1.1", "description": "NodeJS app example for NextGraph", "main": "index.js", "scripts": {