mirror of
https://github.com/fluencelabs/aqua-ipfs
synced 2025-04-24 15:32:15 +00:00
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:
parent
55414f6a2f
commit
32238f18c2
@ -1,8 +1,8 @@
|
||||
# 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
|
||||
1. Go to `local-network`
|
||||
2. Run `docker compose up -d` to start Fluence cluster of 3 nodes
|
||||
3. Go back to `../example`
|
||||
4. Run `npm run start:local`
|
||||
To run example locally:
|
||||
1. Spin up local environment through [Fluence CLI](https://github.com/fluencelabs/cli)'s `local` command
|
||||
2. Pass local fluence peers multi addresses to `main` in `index.ts`
|
||||
3. (Optional) Change `IPFS_MULTIADDR` to address of your preferred IPFS node
|
||||
|
@ -1,3 +1,4 @@
|
||||
module Exports
|
||||
aqua Exports
|
||||
|
||||
import put, get_from, set_timeout from "@fluencelabs/aqua-ipfs/ipfs-api.aqua"
|
||||
export put, get_from, set_timeout
|
||||
|
122
example/index.ts
122
example/index.ts
@ -13,94 +13,72 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { Fluence, testNet, Relay } from "@fluencelabs/js-client";
|
||||
|
||||
import { put, get_from, set_timeout } from "./generated/export";
|
||||
import { Fluence } from "@fluencelabs/fluence";
|
||||
import { Node, testNet } from "@fluencelabs/fluence-network-environment";
|
||||
import { put, get_from, set_timeout } from "./generated/export.js";
|
||||
|
||||
const { create, urlSource } = require("ipfs-http-client");
|
||||
const all = require("it-all");
|
||||
const uint8ArrayConcat = require("uint8arrays/concat");
|
||||
import { multiaddr } from "@multiformats/multiaddr";
|
||||
import { create } from "kubo-rpc-client";
|
||||
import all from "it-all";
|
||||
import uint8ArrayConcat from "uint8arrays/concat.js";
|
||||
|
||||
let local: Node[] = [
|
||||
{
|
||||
peerId: "12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK",
|
||||
multiaddr:
|
||||
"/ip4/127.0.0.1/tcp/9990/ws/p2p/12D3KooWHBG9oaVx4i3vi6c1rSBUm7MLBmyGmmbHoZ23pmjDCnvK",
|
||||
},
|
||||
{
|
||||
peerId: "12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv",
|
||||
multiaddr:
|
||||
"/ip4/127.0.0.1/tcp/9991/ws/p2p/12D3KooWRABanQHUn28dxavN9ZS1zZghqoZVAYtFpoN7FdtoGTFv",
|
||||
},
|
||||
{
|
||||
peerId: "12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
|
||||
multiaddr:
|
||||
"/ip4/127.0.0.1/tcp/9992/ws/p2p/12D3KooWFpQ7LHxcC9FEBUh3k4nSCC12jBhijJv3gJbi7wsNYzJ5",
|
||||
},
|
||||
];
|
||||
// Multi address of the IPFS node
|
||||
// we will work with through the Fluence Network
|
||||
const IPFS_MULTIADDR = multiaddr("/dns4/ipfs.fluence.dev/tcp/5001");
|
||||
|
||||
/**
|
||||
* @param environment - array of fluence network nodes (two are needed)
|
||||
* @note Pass addresses of local nodes to experiment locally
|
||||
*/
|
||||
async function main(environment: Relay[]) {
|
||||
const relay = environment[0];
|
||||
const node = environment[1];
|
||||
|
||||
const ipfs = await create({ url: IPFS_MULTIADDR });
|
||||
console.log("📗 Created IPFS HTTP Client");
|
||||
|
||||
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(
|
||||
"📗 created a fluence peer %s with relay %s",
|
||||
Fluence.getStatus().peerId,
|
||||
Fluence.getStatus().relayPeerId
|
||||
"📗 Created a Fluence Peer %s with Relay %s",
|
||||
client.getPeerId(),
|
||||
client.getRelayPeerId()
|
||||
);
|
||||
|
||||
let ipfsAddr = "https://stage.fluence.dev:15001";
|
||||
let ipfsMultiaddr =
|
||||
"/ip4/134.209.186.43/tcp/5001/p2p/12D3KooWEhCqQ9NBnmtSfNeXSNfhgccmH86xodkCUxZNEXab6pkw";
|
||||
const ipfs = create(ipfsAddr);
|
||||
console.log("📗 created ipfs client");
|
||||
// default IPFS timeout is 1 sec,
|
||||
// set to 10 secs to retrieve file from remote node
|
||||
await set_timeout(node.peerId, 10);
|
||||
console.log("📘 Ipfs.set_timeout");
|
||||
|
||||
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(
|
||||
environment[2].peerId,
|
||||
file.cid.toString(),
|
||||
ipfsMultiaddr,
|
||||
{ ttl: 10000 }
|
||||
node.peerId,
|
||||
added.cid.toString(),
|
||||
IPFS_MULTIADDR.toString(),
|
||||
{ ttl: 20000 }
|
||||
);
|
||||
console.log("📘 Ipfs.get", getResult);
|
||||
console.log("📘 Ipfs.get_from", getResult);
|
||||
|
||||
let putResult = await put(environment[2].peerId, getResult.path, {
|
||||
ttl: 10000,
|
||||
let putResult = await put(node.peerId, getResult.path, {
|
||||
ttl: 20000,
|
||||
});
|
||||
console.log("📘 Ipfs.put", putResult);
|
||||
|
||||
return;
|
||||
await ipfs.stop();
|
||||
}
|
||||
|
||||
let args = process.argv.slice(2);
|
||||
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)
|
||||
main(testNet)
|
||||
.then(() => process.exit(0))
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
|
15828
example/package-lock.json
generated
15828
example/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,11 @@
|
||||
{
|
||||
"type": "module",
|
||||
"name": "ipfs-aqua-example",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple example of how to use ipfs-aqua in TS",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"compile-aqua": "aqua -i aqua -o generated",
|
||||
"compile-aqua": "fluence aqua -i aqua -o generated",
|
||||
"prebuild": "npm run compile-aqua",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
@ -13,12 +14,11 @@
|
||||
"author": "Fluence Labs",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@fluencelabs/aqua-ipfs": "file:../aqua",
|
||||
"@fluencelabs/aqua-ipfs": "^0.5.29",
|
||||
"@fluencelabs/aqua-lib": "^0.9.0",
|
||||
"@fluencelabs/aqua": "0.7.4-322",
|
||||
"@fluencelabs/fluence": "^0.23.0",
|
||||
"@fluencelabs/fluence-network-environment": "^1.1.2",
|
||||
"ipfs-http-client": "^50.1.2",
|
||||
"@fluencelabs/js-client": "^0.4.2",
|
||||
"kubo-rpc-client": "^3.0.2",
|
||||
"@multiformats/multiaddr": "^12.1.1",
|
||||
"it-all": "^1.0.5",
|
||||
"uint8arrays": "^2.1.5"
|
||||
},
|
||||
|
@ -5,7 +5,7 @@
|
||||
/* Basic Options */
|
||||
// "incremental": true, /* Enable incremental compilation */
|
||||
"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. */
|
||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||
// "checkJs": true, /* Report errors in .js files. */
|
||||
@ -41,7 +41,7 @@
|
||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||
|
||||
/* 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. */
|
||||
// "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. */
|
||||
|
Loading…
x
Reference in New Issue
Block a user