mirror of
https://github.com/fluencelabs/examples
synced 2025-04-25 02:32:16 +00:00
Putting code for hello-world example under source control
This commit is contained in:
parent
43a5e307f4
commit
69b33158da
5
js-sdk/hello-world/aqua/hello-world.aqua
Normal file
5
js-sdk/hello-world/aqua/hello-world.aqua
Normal file
@ -0,0 +1,5 @@
|
||||
service HelloWorld("hello-world"):
|
||||
hello(str: string)
|
||||
|
||||
func sayHello():
|
||||
HelloWorld.hello("Hello, world!")
|
2554
js-sdk/hello-world/package-lock.json
generated
Normal file
2554
js-sdk/hello-world/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
24
js-sdk/hello-world/package.json
Normal file
24
js-sdk/hello-world/package.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "hello-world",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"exec": "node -r ts-node/register src/index.ts",
|
||||
"compile-aqua": "aqua-cli --import . -i ./aqua/ -o ./src/_aqua",
|
||||
"watch-aqua": "chokidar \"**/*.aqua\" -c \"npm run compile-aqua\"",
|
||||
"tmp": "java -jar aqua-cli.jar -m node_modules --import . -i ./aqua/ -o ./src/_aqua"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@fluencelabs/aqua-cli": "^0.2.1-219",
|
||||
"chokidar-cli": "^3.0.0",
|
||||
"ts-node": "^10.2.1",
|
||||
"typescript": "^4.4.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluencelabs/fluence": "0.10.0-new-js-sdk-api.14",
|
||||
"@fluencelabs/fluence-network-environment": "1.0.10"
|
||||
}
|
||||
}
|
147
js-sdk/hello-world/src/_aqua/hello-world.ts
Normal file
147
js-sdk/hello-world/src/_aqua/hello-world.ts
Normal file
@ -0,0 +1,147 @@
|
||||
/**
|
||||
*
|
||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||
* Aqua version: 0.2.0-SNAPSHOT
|
||||
*
|
||||
*/
|
||||
import { FluencePeer } from "@fluencelabs/fluence";
|
||||
import {
|
||||
ResultCodes,
|
||||
RequestFlow,
|
||||
RequestFlowBuilder,
|
||||
CallParams,
|
||||
} from "@fluencelabs/fluence/dist/internal/compilerSupport/v1";
|
||||
|
||||
// Services
|
||||
|
||||
export function registerHelloWorld(service: {
|
||||
hello: (str: string, callParams: CallParams<"str">) => Promise<void>;
|
||||
}): void;
|
||||
export function registerHelloWorld(
|
||||
serviceId: string,
|
||||
service: {
|
||||
hello: (str: string, callParams: CallParams<"str">) => Promise<void>;
|
||||
}
|
||||
): void;
|
||||
export function registerHelloWorld(
|
||||
peer: FluencePeer,
|
||||
service: {
|
||||
hello: (str: string, callParams: CallParams<"str">) => Promise<void>;
|
||||
}
|
||||
): void;
|
||||
export function registerHelloWorld(
|
||||
peer: FluencePeer,
|
||||
serviceId: string,
|
||||
service: {
|
||||
hello: (str: string, callParams: CallParams<"str">) => Promise<void>;
|
||||
}
|
||||
): void;
|
||||
export function registerHelloWorld(...args) {
|
||||
let peer: FluencePeer;
|
||||
let serviceId;
|
||||
let service;
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
} else {
|
||||
peer = FluencePeer.default;
|
||||
}
|
||||
|
||||
if (typeof args[0] === "string") {
|
||||
serviceId = args[0];
|
||||
} else if (typeof args[1] === "string") {
|
||||
serviceId = args[1];
|
||||
} else {
|
||||
serviceId = "hello-world";
|
||||
}
|
||||
|
||||
if (!(args[0] instanceof FluencePeer) && typeof args[0] === "object") {
|
||||
service = args[0];
|
||||
} else if (typeof args[1] === "object") {
|
||||
service = args[1];
|
||||
} else {
|
||||
service = args[2];
|
||||
}
|
||||
|
||||
peer.callServiceHandler.use(async (req, resp, next) => {
|
||||
if (req.serviceId !== serviceId) {
|
||||
await next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.fnName === "hello") {
|
||||
const callParams = {
|
||||
...req.particleContext,
|
||||
tetraplets: {
|
||||
str: req.tetraplets[0],
|
||||
},
|
||||
};
|
||||
resp.retCode = ResultCodes.success;
|
||||
await service.hello(req.args[0], callParams);
|
||||
resp.result = {};
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
export async function sayHello(config?: { ttl?: number }): Promise<void>;
|
||||
export async function sayHello(
|
||||
peer: FluencePeer,
|
||||
config?: { ttl?: number }
|
||||
): Promise<void>;
|
||||
export async function sayHello(...args) {
|
||||
let peer: FluencePeer;
|
||||
|
||||
let config;
|
||||
if (args[0] instanceof FluencePeer) {
|
||||
peer = args[0];
|
||||
config = args[1];
|
||||
} else {
|
||||
peer = FluencePeer.default;
|
||||
config = args[0];
|
||||
}
|
||||
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<void>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("hello-world" "hello") ["Hello, world!"])
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
|
||||
`
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on("getDataSrv", "-relay-", async () => {
|
||||
return peer.connectionInfo.connectedRelays[0] || null;
|
||||
});
|
||||
|
||||
h.onEvent("callbackSrv", "response", async (args) => {});
|
||||
|
||||
h.onEvent("errorHandlingSrv", "error", async (args) => {
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject("Request timed out for sayHello");
|
||||
});
|
||||
if (config && config.ttl) {
|
||||
r.withTTL(config.ttl);
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
await peer.initiateFlow(request!);
|
||||
return Promise.race([promise, Promise.resolve()]);
|
||||
}
|
19
js-sdk/hello-world/src/index.ts
Normal file
19
js-sdk/hello-world/src/index.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { FluencePeer, randomPeerId } from "@fluencelabs/fluence";
|
||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
import { registerHelloWorld, sayHello } from "./_aqua/hello-world";
|
||||
|
||||
async function main() {
|
||||
await FluencePeer.default.init();
|
||||
|
||||
registerHelloWorld({
|
||||
hello: async (str) => {
|
||||
console.log(str);
|
||||
},
|
||||
});
|
||||
|
||||
await sayHello();
|
||||
|
||||
await FluencePeer.default.uninit();
|
||||
}
|
||||
|
||||
main();
|
7
js-sdk/hello-world/tsconfig.json
Normal file
7
js-sdk/hello-world/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2015",
|
||||
"module": "CommonJS",
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user