fix(aqua-ipfs): Update example (#159)

* Use aqua keyword, use FCLI

* Add new line

* Use js-client

* Fix index.ts

* Fix compile

* Remove any

* Use helia, update addr

* Return to ipfs-http-client

* Update README

* Use js-kubo-rpc-client

* Update README and comments

---------

Co-authored-by: Akim Mamedov <akim99999999@gmail.com>
This commit is contained in:
InversionSpaces 2024-01-11 10:03:39 +01:00 committed by GitHub
parent 55414f6a2f
commit 32238f18c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 2990 additions and 12989 deletions

View File

@ -1,8 +1,8 @@
# Run example on TestNet # Run example on TestNet
While in `example` directory, `npm start` will run `index.ts` against Fluence TestNet While in `example` directory, `npm run start` will run `index.ts` against Fluence TestNet
# Run example locally # Run example locally
1. Go to `local-network` To run example locally:
2. Run `docker compose up -d` to start Fluence cluster of 3 nodes 1. Spin up local environment through [Fluence CLI](https://github.com/fluencelabs/cli)'s `local` command
3. Go back to `../example` 2. Pass local fluence peers multi addresses to `main` in `index.ts`
4. Run `npm run start:local` 3. (Optional) Change `IPFS_MULTIADDR` to address of your preferred IPFS node

View File

@ -1,3 +1,4 @@
module Exports aqua Exports
import put, get_from, set_timeout from "@fluencelabs/aqua-ipfs/ipfs-api.aqua" import put, get_from, set_timeout from "@fluencelabs/aqua-ipfs/ipfs-api.aqua"
export put, get_from, set_timeout export put, get_from, set_timeout

View File

@ -13,94 +13,72 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { Fluence, testNet, Relay } from "@fluencelabs/js-client";
import { put, get_from, set_timeout } from "./generated/export"; import { put, get_from, set_timeout } from "./generated/export.js";
import { Fluence } from "@fluencelabs/fluence";
import { Node, testNet } from "@fluencelabs/fluence-network-environment";
const { create, urlSource } = require("ipfs-http-client"); import { multiaddr } from "@multiformats/multiaddr";
const all = require("it-all"); import { create } from "kubo-rpc-client";
const uint8ArrayConcat = require("uint8arrays/concat"); import all from "it-all";
import uint8ArrayConcat from "uint8arrays/concat.js";
let local: Node[] = [ // Multi address of the IPFS node
{ // we will work with through the Fluence Network
peerId: "12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK", const IPFS_MULTIADDR = multiaddr("/dns4/ipfs.fluence.dev/tcp/5001");
multiaddr:
"/ip4/127.0.0.1/tcp/9990/ws/p2p/12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK", /**
}, * @param environment - array of fluence network nodes (two are needed)
{ * @note Pass addresses of local nodes to experiment locally
peerId: "12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv", */
multiaddr: async function main(environment: Relay[]) {
"/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv", const relay = environment[0];
}, const node = environment[1];
{
peerId: "12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5", const ipfs = await create({ url: IPFS_MULTIADDR });
multiaddr: console.log("📗 Created IPFS HTTP Client");
"/ip4/127.0.0.1/tcp/9992/ws/p2p/12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
}, const content = "Hola, Fluence!";
]; const encoder = new TextEncoder();
const added = await ipfs.add(encoder.encode(content));
console.log("📗 Uploaded content, got CID:", added.cid.toString());
let stream = await ipfs.cat(added.path);
let data = uint8ArrayConcat(await all(stream));
const decoder = new TextDecoder();
console.log("📗 Retrieved content: ", decoder.decode(data));
await Fluence.connect(relay);
const client = Fluence.getClient();
async function main(environment: Node[]) {
// setLogLevel('DEBUG');
await Fluence.start({ connectTo: environment[1] });
console.log( console.log(
"📗 created a fluence peer %s with relay %s", "📗 Created a Fluence Peer %s with Relay %s",
Fluence.getStatus().peerId, client.getPeerId(),
Fluence.getStatus().relayPeerId client.getRelayPeerId()
); );
let ipfsAddr = "https://stage.fluence.dev:15001"; // default IPFS timeout is 1 sec,
let ipfsMultiaddr = // set to 10 secs to retrieve file from remote node
"/ip4/134.209.186.43/tcp/5001/p2p/12D3KooWEhCqQ9NBnmtSfNeXSNfhgccmH86xodkCUxZNEXab6pkw"; await set_timeout(node.peerId, 10);
const ipfs = create(ipfsAddr); console.log("📘 Ipfs.set_timeout");
console.log("📗 created ipfs client");
await ipfs.id();
console.log("📗 connected to ipfs");
let source = urlSource(
"https://images.adsttc.com/media/images/5ecd/d4ac/b357/65c6/7300/009d/large_jpg/02C.jpg?1590547607"
);
const file = await ipfs.add(source);
console.log("📗 uploaded file:", file);
let files = await ipfs.get(file.cid);
for await (const file of files) {
const content = uint8ArrayConcat(await all(file.content));
console.log("📗 downloaded file of length ", content.length);
}
// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
await set_timeout(environment[2].peerId, 10);
console.log("📘 file hash: ", file.cid);
let getResult = await get_from( let getResult = await get_from(
environment[2].peerId, node.peerId,
file.cid.toString(), added.cid.toString(),
ipfsMultiaddr, IPFS_MULTIADDR.toString(),
{ ttl: 10000 } { ttl: 20000 }
); );
console.log("📘 Ipfs.get", getResult); console.log("📘 Ipfs.get_from", getResult);
let putResult = await put(environment[2].peerId, getResult.path, { let putResult = await put(node.peerId, getResult.path, {
ttl: 10000, ttl: 20000,
}); });
console.log("📘 Ipfs.put", putResult); console.log("📘 Ipfs.put", putResult);
return; await ipfs.stop();
} }
let args = process.argv.slice(2); main(testNet)
var environment: Node[];
if (args.length >= 1 && args[0] == "local") {
environment = local;
console.log("📘 Will connect to local nodes");
} else {
environment = testNet;
console.log("📘 Will connect to testNet");
}
main(environment)
.then(() => process.exit(0)) .then(() => process.exit(0))
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);

15826
example/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,10 +1,11 @@
{ {
"type": "module",
"name": "ipfs-aqua-example", "name": "ipfs-aqua-example",
"version": "1.0.0", "version": "1.0.0",
"description": "A simple example of how to use ipfs-aqua in TS", "description": "A simple example of how to use ipfs-aqua in TS",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"compile-aqua": "aqua -i aqua -o generated", "compile-aqua": "fluence aqua -i aqua -o generated",
"prebuild": "npm run compile-aqua", "prebuild": "npm run compile-aqua",
"build": "tsc", "build": "tsc",
"start": "node dist/index.js", "start": "node dist/index.js",
@ -13,12 +14,11 @@
"author": "Fluence Labs", "author": "Fluence Labs",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@fluencelabs/aqua-ipfs": "file:../aqua", "@fluencelabs/aqua-ipfs": "^0.5.29",
"@fluencelabs/aqua-lib": "^0.9.0", "@fluencelabs/aqua-lib": "^0.9.0",
"@fluencelabs/aqua": "0.7.4-322", "@fluencelabs/js-client": "^0.4.2",
"@fluencelabs/fluence": "^0.23.0", "kubo-rpc-client": "^3.0.2",
"@fluencelabs/fluence-network-environment": "^1.1.2", "@multiformats/multiaddr": "^12.1.1",
"ipfs-http-client": "^50.1.2",
"it-all": "^1.0.5", "it-all": "^1.0.5",
"uint8arrays": "^2.1.5" "uint8arrays": "^2.1.5"
}, },

View File

@ -5,7 +5,7 @@
/* Basic Options */ /* Basic Options */
// "incremental": true, /* Enable incremental compilation */ // "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ "module": "ES2020", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */ // "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */ // "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */ // "checkJs": true, /* Report errors in .js files. */
@ -41,7 +41,7 @@
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */ /* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */