mirror of
https://github.com/fluencelabs/examples
synced 2025-06-13 01:51:20 +00:00
Update all js-related examples to the latest version of JS SDK and Aqua compiler (#20)
This commit is contained in:
@ -1,110 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { provideFile } from "./provider";
|
||||
|
||||
import { set_timeout } from "@fluencelabs/aqua-ipfs-ts";
|
||||
import { createClient, setLogLevel } from "@fluencelabs/fluence";
|
||||
import { stage, krasnodar, Node, testNet } from "@fluencelabs/fluence-network-environment";
|
||||
import { deploy_service, put_file_size, remove_service } from "@fluencelabs/ipfs-execution-aqua";
|
||||
import { globSource, urlSource }from 'ipfs-http-client';
|
||||
|
||||
async function main(environment: Node[]) {
|
||||
// setLogLevel('DEBUG');
|
||||
let providerHost = environment[0];
|
||||
let providerClient = await createClient(providerHost);
|
||||
console.log("📘 uploading .wasm to node %s", providerHost.multiaddr);
|
||||
let path = globSource('../service/artifacts/process_files.wasm');
|
||||
let { file, swarmAddr, rpcAddr } = await provideFile(path, providerClient);
|
||||
console.log("📗 swarmAddr", swarmAddr);
|
||||
console.log("📗 rpcAddr", rpcAddr);
|
||||
|
||||
const fluence = await createClient(environment[1]);
|
||||
console.log("📗 created a fluence client %s with relay %s", fluence.selfPeerId, fluence.relayPeerId);
|
||||
|
||||
// default IPFS timeout is 1 sec, set to 10 secs to retrieve file from remote node
|
||||
await set_timeout(fluence, environment[2].peerId, 10);
|
||||
|
||||
console.log("\n\n📘 Will deploy ProcessFiles service");
|
||||
var service_id = await deploy_service(
|
||||
fluence,
|
||||
environment[2].peerId, file.cid.toString(), rpcAddr,
|
||||
(label, error) => { console.error("📕 deploy_service failed: ", label, error) },
|
||||
{ ttl: 10000 }
|
||||
)
|
||||
service_id = fromOption(service_id);
|
||||
if (service_id === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("📗 ProcessFiles service is now deployed and available as", service_id);
|
||||
|
||||
console.log("\n\n📘 Will upload file & calculate its size");
|
||||
let { file: newFile } = await provideFile(urlSource("https://i.imgur.com/NZgK6DB.png"), providerClient);
|
||||
var putResult = await put_file_size(
|
||||
fluence,
|
||||
environment[2].peerId, newFile.cid.toString(), rpcAddr, service_id,
|
||||
fileSize => console.log("📗 Calculated file size:", fileSize),
|
||||
(label, error) => { console.error("📕 put_file_size failed: ", label, error) },
|
||||
{ ttl: 10000 }
|
||||
)
|
||||
putResult = fromOption(putResult);
|
||||
if (putResult !== null) {
|
||||
console.log("📗 File size is saved to IPFS:", putResult);
|
||||
}
|
||||
|
||||
let result = await remove_service(fluence, environment[2].peerId, service_id);
|
||||
console.log("📗 ProcessFiles service removed", result);
|
||||
return;
|
||||
}
|
||||
|
||||
function fromOption<T>(opt: T | T[] | null): T | null {
|
||||
if (Array.isArray(opt)) {
|
||||
if (opt.length === 0) { return null; }
|
||||
|
||||
opt = opt[0];
|
||||
}
|
||||
if (opt === null) { return null; }
|
||||
|
||||
return opt;
|
||||
}
|
||||
|
||||
let args = process.argv.slice(2);
|
||||
var environment: Node[];
|
||||
if (args.length >= 1 && args[0] == "testnet") {
|
||||
environment = testNet;
|
||||
console.log("📘 Will connect to testNet");
|
||||
} else if (args[0] == "stage") {
|
||||
environment = stage;
|
||||
console.log("📘 Will connect to stage");
|
||||
} else if (args[0] == "krasnodar") {
|
||||
environment = krasnodar;
|
||||
console.log("📘 Will connect to krasnodar");
|
||||
} else if (args[0] == "testnet") {
|
||||
environment = testNet;
|
||||
console.log("📘 Will connect to testNet");
|
||||
} else {
|
||||
throw "Specify environment";
|
||||
}
|
||||
|
||||
main(environment)
|
||||
.then(() => process.exit(0))
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
6790
aqua-examples/aqua-ipfs-integration/nodejs/package-lock.json
generated
6790
aqua-examples/aqua-ipfs-integration/nodejs/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -5,9 +5,7 @@
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"prestart:local": "npm run build",
|
||||
"start:local": "node dist/demo.js local",
|
||||
"prestart:remote": "npm run build",
|
||||
"start:remote": "node dist/demo.js krasnodar",
|
||||
"start": "npm run start:remote"
|
||||
},
|
||||
@ -25,17 +23,17 @@
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@fluencelabs/ipfs-execution-aqua": "file:../aqua",
|
||||
"@fluencelabs/aqua-ipfs-ts": "0.4.2",
|
||||
"@fluencelabs/fluence": "0.10.1",
|
||||
"@fluencelabs/aqua": "0.3.0-225",
|
||||
"@fluencelabs/fluence": "0.12.0",
|
||||
"@fluencelabs/fluence-network-environment": "1.0.10",
|
||||
"@fluencelabs/aqua-lib": "0.1.14",
|
||||
"@fluencelabs/aqua-ipfs": "0.4.2",
|
||||
"ipfs-http-client": "^50.1.2",
|
||||
"it-all": "^1.0.5",
|
||||
"uint8arrays": "^2.1.5",
|
||||
"multiaddr": "^10.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^3.9.5",
|
||||
"@fluencelabs/aqua": "0.2.2-221"
|
||||
"typescript": "^3.9.5"
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +0,0 @@
|
||||
import { create, CID }from 'ipfs-http-client';
|
||||
import { AddResult } from 'ipfs-core-types/src/root';
|
||||
import { Multiaddr, protocols } from 'multiaddr';
|
||||
import { get_external_swarm_multiaddr, get_external_api_multiaddr } from "@fluencelabs/aqua-ipfs-ts";
|
||||
import { FluenceClient } from "@fluencelabs/fluence";
|
||||
|
||||
export async function provideFile(source: any, provider: FluenceClient): Promise<{ file: AddResult, swarmAddr: string, rpcAddr: string }> {
|
||||
var swarmAddr;
|
||||
var result = await get_external_swarm_multiaddr(provider, provider.relayPeerId!, { ttl: 20000 });
|
||||
if (result.success) {
|
||||
swarmAddr = result.multiaddr;
|
||||
} else {
|
||||
console.error("Failed to retrieve external swarm multiaddr from %s: ", provider.relayPeerId);
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
var rpcAddr;
|
||||
var result = await get_external_api_multiaddr(provider, provider.relayPeerId!);
|
||||
if (result.success) {
|
||||
rpcAddr = result.multiaddr;
|
||||
} else {
|
||||
console.error("Failed to retrieve external api multiaddr from %s: ", provider.relayPeerId);
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
var rpcMaddr = new Multiaddr(rpcAddr).decapsulateCode(protocols.names.p2p.code);
|
||||
// HACK: `as any` is needed because ipfs-http-client forgot to add `| Multiaddr` to the `create` types
|
||||
const ipfs = create(rpcMaddr as any);
|
||||
console.log("📗 created ipfs client to %s", rpcMaddr);
|
||||
|
||||
await ipfs.id();
|
||||
console.log("📗 connected to ipfs");
|
||||
|
||||
const file = await ipfs.add(source);
|
||||
console.log("📗 uploaded file:", file);
|
||||
|
||||
// To download the file, uncomment the following code:
|
||||
// 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);
|
||||
// }
|
||||
|
||||
return { file, swarmAddr, rpcAddr };
|
||||
}
|
140
aqua-examples/aqua-ipfs-integration/nodejs/src/demo.ts
Normal file
140
aqua-examples/aqua-ipfs-integration/nodejs/src/demo.ts
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2020 Fluence Labs Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { provideFile } from "./provider";
|
||||
|
||||
import { Fluence, FluencePeer, setLogLevel } from "@fluencelabs/fluence";
|
||||
import {
|
||||
stage,
|
||||
krasnodar,
|
||||
Node,
|
||||
testNet,
|
||||
} from "@fluencelabs/fluence-network-environment";
|
||||
import {
|
||||
deploy_service,
|
||||
put_file_size,
|
||||
remove_service,
|
||||
set_timeout,
|
||||
} from "@fluencelabs/ipfs-execution-aqua";
|
||||
|
||||
import { globSource, urlSource } from "ipfs-http-client";
|
||||
|
||||
async function main(environment: Node[]) {
|
||||
// setLogLevel('DEBUG');
|
||||
let providerHost = environment[0];
|
||||
let providerClient = new FluencePeer();
|
||||
await providerClient.start({ connectTo: providerHost });
|
||||
console.log("📘 uploading .wasm to node %s", providerHost.multiaddr);
|
||||
let path = globSource("../service/artifacts/process_files.wasm");
|
||||
let { file, swarmAddr, rpcAddr } = await provideFile(path, providerClient);
|
||||
console.log("📗 swarmAddr", swarmAddr);
|
||||
console.log("📗 rpcAddr", rpcAddr);
|
||||
|
||||
await Fluence.start({ connectTo: environment[1] });
|
||||
console.log(
|
||||
"📗 created a fluence client %s with relay %s",
|
||||
Fluence.getStatus().peerId,
|
||||
Fluence.getStatus().relayPeerId
|
||||
);
|
||||
|
||||
// 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("\n\n📘 Will deploy ProcessFiles service");
|
||||
var service_id = await deploy_service(
|
||||
environment[2].peerId,
|
||||
file.cid.toString(),
|
||||
rpcAddr,
|
||||
(label, error) => {
|
||||
console.error("📕 deploy_service failed: ", label, error);
|
||||
},
|
||||
{ ttl: 10000 }
|
||||
);
|
||||
service_id = fromOption(service_id);
|
||||
if (service_id === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(
|
||||
"📗 ProcessFiles service is now deployed and available as",
|
||||
service_id
|
||||
);
|
||||
|
||||
console.log("\n\n📘 Will upload file & calculate its size");
|
||||
let { file: newFile } = await provideFile(
|
||||
urlSource("https://i.imgur.com/NZgK6DB.png"),
|
||||
providerClient
|
||||
);
|
||||
var putResult = await put_file_size(
|
||||
environment[2].peerId,
|
||||
newFile.cid.toString(),
|
||||
rpcAddr,
|
||||
service_id,
|
||||
(fileSize) => console.log("📗 Calculated file size:", fileSize),
|
||||
(label, error) => {
|
||||
console.error("📕 put_file_size failed: ", label, error);
|
||||
},
|
||||
{ ttl: 10000 }
|
||||
);
|
||||
putResult = fromOption(putResult);
|
||||
if (putResult !== null) {
|
||||
console.log("📗 File size is saved to IPFS:", putResult);
|
||||
}
|
||||
|
||||
let result = await remove_service(environment[2].peerId, service_id);
|
||||
console.log("📗 ProcessFiles service removed", result);
|
||||
return;
|
||||
}
|
||||
|
||||
function fromOption<T>(opt: T | T[] | null): T | null {
|
||||
if (Array.isArray(opt)) {
|
||||
if (opt.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
opt = opt[0];
|
||||
}
|
||||
if (opt === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return opt;
|
||||
}
|
||||
|
||||
let args = process.argv.slice(2);
|
||||
var environment: Node[];
|
||||
if (args.length >= 1 && args[0] == "testnet") {
|
||||
environment = testNet;
|
||||
console.log("📘 Will connect to testNet");
|
||||
} else if (args[0] == "stage") {
|
||||
environment = stage;
|
||||
console.log("📘 Will connect to stage");
|
||||
} else if (args[0] == "krasnodar") {
|
||||
environment = krasnodar;
|
||||
console.log("📘 Will connect to krasnodar");
|
||||
} else if (args[0] == "testnet") {
|
||||
environment = testNet;
|
||||
console.log("📘 Will connect to testNet");
|
||||
} else {
|
||||
throw "Specify environment";
|
||||
}
|
||||
|
||||
main(environment)
|
||||
.then(() => process.exit(0))
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
65
aqua-examples/aqua-ipfs-integration/nodejs/src/provider.ts
Normal file
65
aqua-examples/aqua-ipfs-integration/nodejs/src/provider.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import { create, CID } from "ipfs-http-client";
|
||||
import { AddResult } from "ipfs-core-types/src/root";
|
||||
import { Multiaddr, protocols } from "multiaddr";
|
||||
|
||||
import { FluencePeer } from "@fluencelabs/fluence";
|
||||
import {
|
||||
get_external_api_multiaddr,
|
||||
get_external_swarm_multiaddr,
|
||||
} from "@fluencelabs/ipfs-execution-aqua";
|
||||
|
||||
export async function provideFile(
|
||||
source: any,
|
||||
provider: FluencePeer
|
||||
): Promise<{ file: AddResult; swarmAddr: string; rpcAddr: string }> {
|
||||
const relayPeerId = provider.getStatus().relayPeerId!;
|
||||
let swarmAddr;
|
||||
let result = await get_external_swarm_multiaddr(
|
||||
provider,
|
||||
provider.getStatus().relayPeerId!,
|
||||
{ ttl: 20000 }
|
||||
);
|
||||
if (result.success) {
|
||||
swarmAddr = result.multiaddr;
|
||||
} else {
|
||||
console.error(
|
||||
"Failed to retrieve external swarm multiaddr from %s: ",
|
||||
provider.getStatus().relayPeerId!
|
||||
);
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
let rpcAddr;
|
||||
result = await get_external_api_multiaddr(provider, relayPeerId);
|
||||
if (result.success) {
|
||||
rpcAddr = result.multiaddr;
|
||||
} else {
|
||||
console.error(
|
||||
"Failed to retrieve external api multiaddr from %s: ",
|
||||
relayPeerId
|
||||
);
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
let rpcMaddr = new Multiaddr(rpcAddr).decapsulateCode(
|
||||
protocols.names.p2p.code
|
||||
);
|
||||
// HACK: `as any` is needed because ipfs-http-client forgot to add `| Multiaddr` to the `create` types
|
||||
const ipfs = create(rpcMaddr as any);
|
||||
console.log("📗 created ipfs client to %s", rpcMaddr);
|
||||
|
||||
await ipfs.id();
|
||||
console.log("📗 connected to ipfs");
|
||||
|
||||
const file = await ipfs.add(source);
|
||||
console.log("📗 uploaded file:", file);
|
||||
|
||||
// To download the file, uncomment the following code:
|
||||
// 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);
|
||||
// }
|
||||
|
||||
return { file, swarmAddr, rpcAddr };
|
||||
}
|
Reference in New Issue
Block a user