Update hello-world example

This commit is contained in:
Pavel Murygin 2021-10-21 23:11:45 +03:00
parent 6cb48bef18
commit 1cb4613c30
4 changed files with 121 additions and 222 deletions

View File

@ -13,7 +13,7 @@
"@fluencelabs/fluence-network-environment": "1.0.10"
},
"devDependencies": {
"@fluencelabs/aqua": "^0.3.2-233",
"@fluencelabs/aqua": "^0.4.0-234",
"@fluencelabs/aqua-lib": "^0.1.14",
"chokidar-cli": "^3.0.0",
"ts-node": "^10.2.1",
@ -73,9 +73,9 @@
}
},
"node_modules/@fluencelabs/aqua": {
"version": "0.3.2-233",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.2-233.tgz",
"integrity": "sha512-LxwNt/O2ijHA2bG7+qdmnFT//kpIVj7mACBp28LxRpb2kJYMcVDNb9VfyWDiDdoX10PAT96OpkiYvVJPs0mpEw==",
"version": "0.4.0-234",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.4.0-234.tgz",
"integrity": "sha512-7ysLPh3R4gAfXgO24OGT7rP5UGKUHs7ipfx4HTJsYZahZ0rMbWA98uC/aM3+q0WH1WEowyV16zvGACONFLZ5RA==",
"dev": true,
"dependencies": {
"@fluencelabs/fluence": "0.12.1"
@ -3292,9 +3292,9 @@
}
},
"@fluencelabs/aqua": {
"version": "0.3.2-233",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.2-233.tgz",
"integrity": "sha512-LxwNt/O2ijHA2bG7+qdmnFT//kpIVj7mACBp28LxRpb2kJYMcVDNb9VfyWDiDdoX10PAT96OpkiYvVJPs0mpEw==",
"version": "0.4.0-234",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.4.0-234.tgz",
"integrity": "sha512-7ysLPh3R4gAfXgO24OGT7rP5UGKUHs7ipfx4HTJsYZahZ0rMbWA98uC/aM3+q0WH1WEowyV16zvGACONFLZ5RA==",
"dev": true,
"requires": {
"@fluencelabs/fluence": "0.12.1"

View File

@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@fluencelabs/aqua": "^0.3.2-233",
"@fluencelabs/aqua": "^0.4.0-234",
"@fluencelabs/aqua-lib": "^0.1.14",
"chokidar-cli": "^3.0.0",
"ts-node": "^10.2.1",

View File

@ -3,27 +3,22 @@
* 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.3.2-233
* Aqua version: 0.4.0-234
*
*/
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
import {
ResultCodes,
RequestFlow,
RequestFlowBuilder,
CallParams
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
CallParams,
callFunction,
registerService,
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v2';
function missingFields(obj: any, fields: string[]): string[] {
return fields.filter(f => !(f in obj))
}
// Services
export interface HelloWorldDef {
getFortune: (callParams: CallParams<null>) => string;
hello: (str: string, callParams: CallParams<'str'>) => void;
getFortune: (callParams: CallParams<null>) => string | Promise<string>;
hello: (str: string, callParams: CallParams<'str'>) => void | Promise<void>;
}
export function registerHelloWorld(service: HelloWorldDef): void;
export function registerHelloWorld(serviceId: string, service: HelloWorldDef): void;
@ -32,71 +27,36 @@ export function registerHelloWorld(peer: FluencePeer, serviceId: string, service
export function registerHelloWorld(...args: any) {
let peer: FluencePeer;
let serviceId: any;
let service: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
} else {
peer = Fluence.getPeer();
}
if (typeof args[0] === 'string') {
serviceId = args[0];
} else if (typeof args[1] === 'string') {
serviceId = args[1];
} else {
serviceId = "hello-world"
}
// Figuring out which overload is the service.
// If the first argument is not Fluence Peer and it is an object, then it can only be the service def
// If the first argument is peer, we are checking further. The second argument might either be
// an object, that it must be the service object
// or a string, which is the service id. In that case the service is the third argument
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
service = args[0];
} else if (typeof args[1] === 'object') {
service = args[1];
} else {
service = args[2];
}
const incorrectServiceDefinitions = missingFields(service, ['getFortune', 'hello']);
if (!!incorrectServiceDefinitions.length) {
throw new Error("Error registering service HelloWorld: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
}
peer.internals.callServiceHandler.use((req, resp, next) => {
if (req.serviceId !== serviceId) {
next();
return;
registerService(
args,
{
"defaultServiceId" : "hello-world",
"functions" : [
{
"functionName" : "getFortune",
"argDefs" : [
],
"returnType" : {
"tag" : "primitive"
}
},
{
"functionName" : "hello",
"argDefs" : [
{
"name" : "str",
"argType" : {
"tag" : "primitive"
}
}
],
"returnType" : {
"tag" : "void"
}
}
if (req.fnName === 'getFortune') {
const callParams = {
...req.particleContext,
tetraplets: {
},
};
resp.retCode = ResultCodes.success;
resp.result = service.getFortune(callParams)
}
if (req.fnName === 'hello') {
const callParams = {
...req.particleContext,
tetraplets: {
str: req.tetraplets[0]
},
};
resp.retCode = ResultCodes.success;
service.hello(req.args[0], callParams); resp.result = {}
}
next();
});
]
}
);
}
// Functions
@ -105,57 +65,37 @@ if (req.fnName === 'hello') {
export function sayHello(config?: {ttl?: number}): Promise<void>;
export function sayHello(peer: FluencePeer, config?: {ttl?: number}): Promise<void>;
export function sayHello(...args: any) {
let peer: FluencePeer;
let config: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
config = args[1];
} else {
peer = Fluence.getPeer();
config = args[0];
}
let request: RequestFlow;
const promise = new Promise<void>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(`
(xor
let script = `
(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-', () => {
return peer.getStatus().relayPeerId;
});
h.onEvent('callbackSrv', 'response', (args) => {
});
h.onEvent('errorHandlingSrv', 'error', (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();
});
peer.internals.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
`
return callFunction(
args,
{
"functionName" : "sayHello",
"returnType" : {
"tag" : "void"
},
"argDefs" : [
],
"names" : {
"relay" : "-relay-",
"getDataSrv" : "getDataSrv",
"callbackSrv" : "callbackSrv",
"responseSrv" : "callbackSrv",
"responseFnName" : "response",
"errorHandlingSrv" : "errorHandlingSrv",
"errorFnName" : "error"
}
},
script
)
}
@ -163,23 +103,9 @@ export function sayHello(...args: any) {
export function tellFortune(config?: {ttl?: number}): Promise<string>;
export function tellFortune(peer: FluencePeer, config?: {ttl?: number}): Promise<string>;
export function tellFortune(...args: any) {
let peer: FluencePeer;
let config: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
config = args[1];
} else {
peer = Fluence.getPeer();
config = args[0];
}
let request: RequestFlow;
const promise = new Promise<string>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(`
(xor
let script = `
(xor
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
@ -192,35 +118,28 @@ export function tellFortune(...args: any) {
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return peer.getStatus().relayPeerId;
});
h.onEvent('callbackSrv', 'response', (args) => {
const [res] = args;
resolve(res);
});
h.onEvent('errorHandlingSrv', 'error', (args) => {
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for tellFortune');
})
if (config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
peer.internals.initiateFlow(request!);
return promise;
`
return callFunction(
args,
{
"functionName" : "tellFortune",
"returnType" : {
"tag" : "primitive"
},
"argDefs" : [
],
"names" : {
"relay" : "-relay-",
"getDataSrv" : "getDataSrv",
"callbackSrv" : "callbackSrv",
"responseSrv" : "callbackSrv",
"responseFnName" : "response",
"errorHandlingSrv" : "errorHandlingSrv",
"errorFnName" : "error"
}
},
script
)
}
@ -228,23 +147,9 @@ export function tellFortune(...args: any) {
export function getRelayTime(config?: {ttl?: number}): Promise<number>;
export function getRelayTime(peer: FluencePeer, config?: {ttl?: number}): Promise<number>;
export function getRelayTime(...args: any) {
let peer: FluencePeer;
let config: any;
if (FluencePeer.isInstance(args[0])) {
peer = args[0];
config = args[1];
} else {
peer = Fluence.getPeer();
config = args[0];
}
let request: RequestFlow;
const promise = new Promise<number>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(`
(xor
let script = `
(xor
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
@ -260,33 +165,26 @@ export function getRelayTime(...args: any) {
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return peer.getStatus().relayPeerId;
});
h.onEvent('callbackSrv', 'response', (args) => {
const [res] = args;
resolve(res);
});
h.onEvent('errorHandlingSrv', 'error', (args) => {
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for getRelayTime');
})
if (config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
peer.internals.initiateFlow(request!);
return promise;
`
return callFunction(
args,
{
"functionName" : "getRelayTime",
"returnType" : {
"tag" : "primitive"
},
"argDefs" : [
],
"names" : {
"relay" : "-relay-",
"getDataSrv" : "getDataSrv",
"callbackSrv" : "callbackSrv",
"responseSrv" : "callbackSrv",
"responseFnName" : "response",
"errorHandlingSrv" : "errorHandlingSrv",
"errorFnName" : "error"
}
},
script
)
}

View File

@ -15,10 +15,11 @@ async function main() {
console.log(str);
},
getFortune: async () => {
await new Promise(resolve => {
setTimeout(resolve, 1000)
})
return "Wealth awaits you very soon."
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
return "Wealth awaits you very soon.";
},
});
await sayHello();