mirror of
https://github.com/fluencelabs/aqua-playground
synced 2025-06-19 07:31:23 +00:00
Using jest to test examples (#14)
This commit is contained in:
14
jest-patched-jsdom.js
Normal file
14
jest-patched-jsdom.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
const Environment = require('jest-environment-jsdom');
|
||||||
|
|
||||||
|
module.exports = class CustomTestEnvironment extends Environment {
|
||||||
|
async setup() {
|
||||||
|
await super.setup();
|
||||||
|
if (typeof this.global.TextEncoder === 'undefined') {
|
||||||
|
const { TextEncoder, TextDecoder } = require('util');
|
||||||
|
this.global.TextEncoder = TextEncoder;
|
||||||
|
this.global.TextDecoder = TextDecoder;
|
||||||
|
this.global.Uint8Array = Uint8Array;
|
||||||
|
this.global.ArrayBuffer = ArrayBuffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
4
jest.config.js
Normal file
4
jest.config.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
module.exports = {
|
||||||
|
preset: 'ts-jest',
|
||||||
|
testEnvironment: './jest-patched-jsdom.js',
|
||||||
|
};
|
6342
package-lock.json
generated
6342
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -17,9 +17,10 @@
|
|||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"examples": "node -r ts-node/register src/run-examples.ts",
|
"test": "jest",
|
||||||
|
"examples": "jest",
|
||||||
"pubsub": "node -r ts-node/register src/pubsub.ts",
|
"pubsub": "node -r ts-node/register src/pubsub.ts",
|
||||||
"exec": "npm run compile-aqua && node -r ts-node/register src/index.ts",
|
"exec": "npm run compile-aqua && npm run prettify-compiled && node -r ts-node/register src/index.ts",
|
||||||
"run": "node -r ts-node/register src/index.ts",
|
"run": "node -r ts-node/register src/index.ts",
|
||||||
"compile-aqua": "aqua -i ./aqua/ -o ./src/compiled",
|
"compile-aqua": "aqua -i ./aqua/ -o ./src/compiled",
|
||||||
"compile-aqua:air": "aqua -i ./aqua/ -o ./compiled-air -a",
|
"compile-aqua:air": "aqua -i ./aqua/ -o ./compiled-air -a",
|
||||||
@ -29,6 +30,9 @@
|
|||||||
"@fluencelabs/aqua": "0.3.1-231",
|
"@fluencelabs/aqua": "0.3.1-231",
|
||||||
"@fluencelabs/aqua-dht": "0.1.36",
|
"@fluencelabs/aqua-dht": "0.1.36",
|
||||||
"@fluencelabs/aqua-lib": "0.1.14",
|
"@fluencelabs/aqua-lib": "0.1.14",
|
||||||
|
"@types/jest": "^27.0.2",
|
||||||
|
"jest": "^27.2.4",
|
||||||
|
"ts-jest": "^27.0.5",
|
||||||
"ts-node": "^9.1.1",
|
"ts-node": "^9.1.1",
|
||||||
"typescript": "^4.2.4"
|
"typescript": "^4.2.4"
|
||||||
},
|
},
|
||||||
|
234
src/__test__/examples.spec.ts
Normal file
234
src/__test__/examples.spec.ts
Normal file
@ -0,0 +1,234 @@
|
|||||||
|
import { Fluence, FluencePeer, setLogLevel } from '@fluencelabs/fluence';
|
||||||
|
import { krasnodar, testNet } from '@fluencelabs/fluence-network-environment';
|
||||||
|
import { registerPrintln } from '../compiled/examples/println';
|
||||||
|
import { callArrowCall } from '../examples/callArrowCall';
|
||||||
|
import { dataAliasCall } from '../examples/dataAliasCall';
|
||||||
|
import { onCall } from '../examples/onCall';
|
||||||
|
import { funcCall } from '../examples/funcCall';
|
||||||
|
import { helloWorldCall } from '../examples/helloWorldCall';
|
||||||
|
import { foldCall } from '../examples/foldCall';
|
||||||
|
import { ifCall } from '../examples/if';
|
||||||
|
import { parCall } from '../examples/parCall';
|
||||||
|
import { complexCall } from '../examples/complex';
|
||||||
|
import { constantsCall } from '../examples/constantsCall';
|
||||||
|
import { returnNilCall, returnNoneCall, streamCall } from '../examples/streamCall';
|
||||||
|
import { topologyCall } from '../examples/topologyCall';
|
||||||
|
import { foldJoinCall } from '../examples/foldJoinCall';
|
||||||
|
import { registerHandlers, returnNull, returnOptionalCall, useOptionalCall } from '../examples/useOptionalCall';
|
||||||
|
import { viaCall } from '../examples/viaCall';
|
||||||
|
import { nestedFuncsCall } from '../examples/nestedFuncsCall';
|
||||||
|
import { assignmentCall } from '../examples/assignment';
|
||||||
|
import { tryCatchCall } from '../examples/tryCatchCall';
|
||||||
|
import { tryOtherwiseCall } from '../examples/tryOtherwiseCall';
|
||||||
|
import { coCall } from '../examples/coCall';
|
||||||
|
import { passArgsCall } from '../examples/passArgsCall';
|
||||||
|
import { streamArgsCall } from '../examples/streamArgsCall';
|
||||||
|
import { streamResultsCall } from '../examples/streamResultsCall';
|
||||||
|
import { pushToStreamCall } from '../examples/pushToStreamCall';
|
||||||
|
import { literalCall } from '../examples/returnLiteralCall';
|
||||||
|
import { multiReturnCall } from '../examples/multiReturnCall';
|
||||||
|
import { declareCall } from '../examples/declareCall';
|
||||||
|
import { genOptions } from '../examples/optionsCall';
|
||||||
|
import { relays } from '../config';
|
||||||
|
|
||||||
|
var selfPeerId;
|
||||||
|
var peer2;
|
||||||
|
|
||||||
|
// setLogLevel('trace');
|
||||||
|
|
||||||
|
describe('Testing examples', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
await Fluence.start({ connectTo: relays[0] });
|
||||||
|
selfPeerId = Fluence.getStatus().peerId;
|
||||||
|
|
||||||
|
peer2 = new FluencePeer();
|
||||||
|
await peer2.start({ connectTo: relays[1] });
|
||||||
|
|
||||||
|
// this could be called from `println.aqua`
|
||||||
|
registerPrintln({
|
||||||
|
print: (arg0) => {
|
||||||
|
console.log('println: ' + arg0);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
if (peer2) {
|
||||||
|
Fluence.stop();
|
||||||
|
await peer2.stop();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('callArrow.aqua', async () => {
|
||||||
|
let callArrowResult = await callArrowCall();
|
||||||
|
|
||||||
|
expect(callArrowResult).toBe('Hello, callArrow call!');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fold.aqua', async () => {
|
||||||
|
let foldCallResult = await foldCall();
|
||||||
|
expect(foldCallResult).toStrictEqual(['/ip4/164.90.171.139/tcp/7770', '/ip4/164.90.171.139/tcp/9990/ws']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('if.aqua', async () => {
|
||||||
|
await ifCall();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(' par.aqua', async () => {
|
||||||
|
let parCallResult = await parCall();
|
||||||
|
expect(parCallResult).toBe('hello');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('helloWorld.aqua', async () => {
|
||||||
|
let helloWorldResult = await helloWorldCall();
|
||||||
|
expect(helloWorldResult).toBe('Hello, NAME!');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('func.aqua', async () => {
|
||||||
|
let funcCallResult = await funcCall();
|
||||||
|
expect(funcCallResult).toBe('some str');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('on.aqua', async () => {
|
||||||
|
let onCallResult = await onCall();
|
||||||
|
expect(onCallResult).toStrictEqual(['/ip4/164.90.171.139/tcp/7770', '/ip4/164.90.171.139/tcp/9990/ws']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('dataAlias.aqua', async () => {
|
||||||
|
let dataAliasResult = await dataAliasCall();
|
||||||
|
expect(dataAliasResult).toBe('peer id str');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('complex.aqua', async () => {
|
||||||
|
let complexCallResult = await complexCall();
|
||||||
|
expect(complexCallResult).toStrictEqual([
|
||||||
|
'some str',
|
||||||
|
'3',
|
||||||
|
'1',
|
||||||
|
'4',
|
||||||
|
'1',
|
||||||
|
'1',
|
||||||
|
'3',
|
||||||
|
'2',
|
||||||
|
'4',
|
||||||
|
'2',
|
||||||
|
'2',
|
||||||
|
selfPeerId,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('constants.aqua', async () => {
|
||||||
|
let constantCallResult = await constantsCall();
|
||||||
|
expect(constantCallResult).toStrictEqual(['1', 'ab']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('stream.aqua', async () => {
|
||||||
|
let streamResult = await streamCall();
|
||||||
|
expect(streamResult).toStrictEqual(['first updated', 'second updated', 'third updated', 'fourth updated']);
|
||||||
|
let returnNilResult = await returnNilCall();
|
||||||
|
expect(returnNilResult).toStrictEqual([]);
|
||||||
|
let returnNoneResult = await returnNoneCall();
|
||||||
|
expect(returnNoneResult).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('topology.aqua', async () => {
|
||||||
|
let topologyResult = await topologyCall(peer2);
|
||||||
|
expect(topologyResult).toBe('finish');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('foldJoin.aqua', async () => {
|
||||||
|
let foldJoinResult = await foldJoinCall();
|
||||||
|
expect(foldJoinResult).toHaveLength(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('option.aqua', async () => {
|
||||||
|
registerHandlers();
|
||||||
|
let optionResult = await useOptionalCall();
|
||||||
|
let optionalResult = await returnOptionalCall();
|
||||||
|
let noneResult = await returnNull();
|
||||||
|
expect(optionResult).toBe('hello');
|
||||||
|
expect(optionalResult).toBe('optional');
|
||||||
|
expect(noneResult).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('via.aqua', async () => {
|
||||||
|
let [res1, res2, res3, res4] = await viaCall();
|
||||||
|
expect(res1).toStrictEqual(res2);
|
||||||
|
expect(res2).toStrictEqual(res3);
|
||||||
|
expect(res3).toStrictEqual(res4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('nestedFuncs.aqua', async () => {
|
||||||
|
let nestedFuncsResult = await nestedFuncsCall();
|
||||||
|
expect(nestedFuncsResult).toBe('some-str');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('assignment.aqua', async () => {
|
||||||
|
let assignmentResult = await assignmentCall();
|
||||||
|
expect(assignmentResult).toStrictEqual(['abc', 'hello']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('tryOtherwise.aqua', async () => {
|
||||||
|
let tryOtherwiseResult = await tryOtherwiseCall();
|
||||||
|
expect(tryOtherwiseResult).toBe('error');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('tryCatch.aqua', async () => {
|
||||||
|
let tryCatchResult = await tryCatchCall();
|
||||||
|
expect(tryCatchResult).toHaveLength(2);
|
||||||
|
expect(tryCatchResult[0]).toMatch("Error: Service with id 'unex' not found");
|
||||||
|
expect(tryCatchResult[1]).toBe('/ip4/164.90.171.139/tcp/7770');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('coCall.aqua', async () => {
|
||||||
|
let coCallResult = await coCall();
|
||||||
|
expect(coCallResult).toStrictEqual(['/ip4/164.90.171.139/tcp/7770', '/ip4/164.90.171.139/tcp/9990/ws']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('passArgsCall.aqua', async () => {
|
||||||
|
let passArgsResult = await passArgsCall();
|
||||||
|
expect(passArgsResult).toBe('client-utilsid');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('streamArgs.aqua', async () => {
|
||||||
|
let streamArgsResult = await streamArgsCall();
|
||||||
|
expect(streamArgsResult).toStrictEqual([['peer_id', 'peer_id']]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('streamResults.aqua', async () => {
|
||||||
|
let streamResultsResult = await streamResultsCall();
|
||||||
|
expect(streamResultsResult).toStrictEqual(['new_name', 'new_name', 'new_name']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('pushToStream.aqua', async () => {
|
||||||
|
let pushToStreamResult = await pushToStreamCall();
|
||||||
|
expect(pushToStreamResult).toStrictEqual(['hello', 'get_string']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('literalCall.aqua', async () => {
|
||||||
|
let literalCallResult = await literalCall();
|
||||||
|
expect(literalCallResult).toBe('some literal');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('multiReturn.aqua', async () => {
|
||||||
|
let multiReturnResult = await multiReturnCall();
|
||||||
|
expect(multiReturnResult).toStrictEqual([
|
||||||
|
['some-str', 'random-str', 'some-str'],
|
||||||
|
5,
|
||||||
|
'some-str',
|
||||||
|
[1, 2],
|
||||||
|
null,
|
||||||
|
10,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('declare.aqua', async () => {
|
||||||
|
let declareResult = await declareCall();
|
||||||
|
expect(declareResult).toBe('small_foodeclare all barsmall_fooexport_constdeclare_constdeclare_const2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('option_gen.aqua', async () => {
|
||||||
|
let optionGenResult = await genOptions();
|
||||||
|
expect(optionGenResult).toStrictEqual(['none', 'some']);
|
||||||
|
});
|
||||||
|
});
|
714
src/compiled/dht/dht-example.ts
Normal file
714
src/compiled/dht/dht-example.ts
Normal file
@ -0,0 +1,714 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function put_value(initial_peer: string, value: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function put_value(
|
||||||
|
peer: FluencePeer,
|
||||||
|
initial_peer: string,
|
||||||
|
value: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string>;
|
||||||
|
export function put_value(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let initial_peer: any;
|
||||||
|
let value: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
initial_peer = args[1];
|
||||||
|
value = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
initial_peer = args[0];
|
||||||
|
value = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "initial_peer") [] initial_peer)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "value") [] value)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call initial_peer ("op" "string_to_b58") ["some-const3"] k)
|
||||||
|
(call initial_peer ("kad" "neighborhood") [k [] []] nodes)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(fold nodes n
|
||||||
|
(par
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call n ("peer" "timestamp_sec") [] t)
|
||||||
|
(call n ("aqua-dht" "register_key") ["some-const3" t false 0])
|
||||||
|
)
|
||||||
|
(call n ("aqua-dht" "put_value") ["some-const3" value t [] [] 0])
|
||||||
|
)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(next n)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["OK"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'initial_peer', () => {
|
||||||
|
return initial_peer;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'value', () => {
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
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 put_value');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerKeyPutValue(
|
||||||
|
node_id: string,
|
||||||
|
key: string,
|
||||||
|
value: string,
|
||||||
|
relay_id: string | null,
|
||||||
|
service_id: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string[]>;
|
||||||
|
export function registerKeyPutValue(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node_id: string,
|
||||||
|
key: string,
|
||||||
|
value: string,
|
||||||
|
relay_id: string | null,
|
||||||
|
service_id: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string[]>;
|
||||||
|
export function registerKeyPutValue(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let key: any;
|
||||||
|
let value: any;
|
||||||
|
let relay_id: any;
|
||||||
|
let service_id: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
key = args[2];
|
||||||
|
value = args[3];
|
||||||
|
relay_id = args[4];
|
||||||
|
service_id = args[5];
|
||||||
|
config = args[6];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
key = args[1];
|
||||||
|
value = args[2];
|
||||||
|
relay_id = args[3];
|
||||||
|
service_id = args[4];
|
||||||
|
config = args[5];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "key") [] key)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "value") [] value)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "relay_id") [] relay_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call key ("op" "string_to_b58") [node_id] k)
|
||||||
|
(call key ("kad" "neighborhood") [k [] []] nodes)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(fold nodes n
|
||||||
|
(par
|
||||||
|
(xor
|
||||||
|
(call n ("peer" "timestamp_sec") [] t)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(next n)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [nodes])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'key', () => {
|
||||||
|
return key;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'value', () => {
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'relay_id', () => {
|
||||||
|
return relay_id === null ? [] : [relay_id];
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'service_id', () => {
|
||||||
|
return service_id === null ? [] : [service_id];
|
||||||
|
});
|
||||||
|
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 registerKeyPutValue');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNeighbours(node_id: string, topic: string, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function getNeighbours(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node_id: string,
|
||||||
|
topic: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string[]>;
|
||||||
|
export function getNeighbours(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let topic: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
topic = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
topic = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call node_id ("op" "string_to_b58") [topic] k)
|
||||||
|
(call node_id ("kad" "neighborhood") [k [] []] nodes)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [nodes])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'topic', () => {
|
||||||
|
return topic;
|
||||||
|
});
|
||||||
|
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 getNeighbours');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findSubscribers(
|
||||||
|
node_id: string,
|
||||||
|
topic: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<
|
||||||
|
{
|
||||||
|
peer_id: string;
|
||||||
|
relay_id: string[];
|
||||||
|
service_id: string[];
|
||||||
|
set_by: string;
|
||||||
|
timestamp_created: number;
|
||||||
|
value: string;
|
||||||
|
weight: number;
|
||||||
|
}[]
|
||||||
|
>;
|
||||||
|
export function findSubscribers(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node_id: string,
|
||||||
|
topic: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<
|
||||||
|
{
|
||||||
|
peer_id: string;
|
||||||
|
relay_id: string[];
|
||||||
|
service_id: string[];
|
||||||
|
set_by: string;
|
||||||
|
timestamp_created: number;
|
||||||
|
value: string;
|
||||||
|
weight: number;
|
||||||
|
}[]
|
||||||
|
>;
|
||||||
|
export function findSubscribers(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let topic: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
topic = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
topic = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<
|
||||||
|
{
|
||||||
|
peer_id: string;
|
||||||
|
relay_id: string[];
|
||||||
|
service_id: string[];
|
||||||
|
set_by: string;
|
||||||
|
timestamp_created: number;
|
||||||
|
value: string;
|
||||||
|
weight: number;
|
||||||
|
}[]
|
||||||
|
>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call node_id ("op" "string_to_b58") [topic] k)
|
||||||
|
(call node_id ("kad" "neighborhood") [k [] []] nodes)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(fold nodes n
|
||||||
|
(par
|
||||||
|
(seq
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call n ("peer" "timestamp_sec") [] t)
|
||||||
|
(call n ("aqua-dht" "get_values") [topic t] $res)
|
||||||
|
)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
(call node_id ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(next n)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call node_id ("aqua-dht" "merge_two") [$res.$.[0].result! $res.$.[1].result!] v)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [v.$.result!])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'topic', () => {
|
||||||
|
return topic;
|
||||||
|
});
|
||||||
|
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 findSubscribers');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function initTopicAndSubscribe(
|
||||||
|
node_id: string,
|
||||||
|
topic: string,
|
||||||
|
value: string,
|
||||||
|
relay_id: string | null,
|
||||||
|
service_id: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function initTopicAndSubscribe(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node_id: string,
|
||||||
|
topic: string,
|
||||||
|
value: string,
|
||||||
|
relay_id: string | null,
|
||||||
|
service_id: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function initTopicAndSubscribe(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let topic: any;
|
||||||
|
let value: any;
|
||||||
|
let relay_id: any;
|
||||||
|
let service_id: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
topic = args[2];
|
||||||
|
value = args[3];
|
||||||
|
relay_id = args[4];
|
||||||
|
service_id = args[5];
|
||||||
|
config = args[6];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
topic = args[1];
|
||||||
|
value = args[2];
|
||||||
|
relay_id = args[3];
|
||||||
|
service_id = args[4];
|
||||||
|
config = args[5];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "value") [] value)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "relay_id") [] relay_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call node_id ("op" "string_to_b58") [topic] k)
|
||||||
|
(call node_id ("kad" "neighborhood") [k [] []] nodes)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(fold nodes n
|
||||||
|
(par
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call n ("peer" "timestamp_sec") [] t)
|
||||||
|
(call n ("aqua-dht" "register_key") [topic t false 0])
|
||||||
|
)
|
||||||
|
(call n ("aqua-dht" "put_value") [topic value t relay_id service_id 0])
|
||||||
|
)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(next n)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'topic', () => {
|
||||||
|
return topic;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'value', () => {
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'relay_id', () => {
|
||||||
|
return relay_id === null ? [] : [relay_id];
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'service_id', () => {
|
||||||
|
return service_id === null ? [] : [service_id];
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for initTopicAndSubscribe');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
242
src/compiled/examples/aliases.ts
Normal file
242
src/compiled/examples/aliases.ts
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface ComplexServiceDef {
|
||||||
|
call: (
|
||||||
|
d: { otherValue: number; value: string },
|
||||||
|
sd: { complex: { someNum: number; someStr: string }; value: string },
|
||||||
|
callParams: CallParams<'d' | 'sd'>,
|
||||||
|
) => { someNum: number; someStr: string };
|
||||||
|
identity: (callParams: CallParams<null>) => { complex: { someNum: number; someStr: string }; value: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerComplexService(service: ComplexServiceDef): void;
|
||||||
|
export function registerComplexService(serviceId: string, service: ComplexServiceDef): void;
|
||||||
|
export function registerComplexService(peer: FluencePeer, service: ComplexServiceDef): void;
|
||||||
|
export function registerComplexService(peer: FluencePeer, serviceId: string, service: ComplexServiceDef): void;
|
||||||
|
export function registerComplexService(...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 = 'op-ha';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['call', 'identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service ComplexService: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'call') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
d: req.tetraplets[0],
|
||||||
|
sd: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.call(req.args[0], req.args[1], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.identity(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function doSmth(
|
||||||
|
d: { otherValue: number; value: string },
|
||||||
|
d2: { otherValue: number; value: string },
|
||||||
|
sd: { complex: { someNum: number; someStr: string }; value: string },
|
||||||
|
c: (
|
||||||
|
arg0: { someNum: number; someStr: string },
|
||||||
|
arg1: { complex: { someNum: number; someStr: string }; value: string },
|
||||||
|
callParams: CallParams<'arg0' | 'arg1'>,
|
||||||
|
) => { complex: { otherValue: number; value: string }; value: string },
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ complex: { otherValue: number; value: string }; value: string }>;
|
||||||
|
export function doSmth(
|
||||||
|
peer: FluencePeer,
|
||||||
|
d: { otherValue: number; value: string },
|
||||||
|
d2: { otherValue: number; value: string },
|
||||||
|
sd: { complex: { someNum: number; someStr: string }; value: string },
|
||||||
|
c: (
|
||||||
|
arg0: { someNum: number; someStr: string },
|
||||||
|
arg1: { complex: { someNum: number; someStr: string }; value: string },
|
||||||
|
callParams: CallParams<'arg0' | 'arg1'>,
|
||||||
|
) => { complex: { otherValue: number; value: string }; value: string },
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ complex: { otherValue: number; value: string }; value: string }>;
|
||||||
|
export function doSmth(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let d: any;
|
||||||
|
let d2: any;
|
||||||
|
let sd: any;
|
||||||
|
let c: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
d = args[1];
|
||||||
|
d2 = args[2];
|
||||||
|
sd = args[3];
|
||||||
|
c = args[4];
|
||||||
|
config = args[5];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
d = args[0];
|
||||||
|
d2 = args[1];
|
||||||
|
sd = args[2];
|
||||||
|
c = args[3];
|
||||||
|
config = args[4];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<{ complex: { otherValue: number; value: string }; value: string }>(
|
||||||
|
(resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "d") [] d)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "d2") [] d2)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "sd") [] sd)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("op-ha" "call") [d sd] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "c") [res sd] init_call_res0)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [init_call_res0])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'd', () => {
|
||||||
|
return d;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'd2', () => {
|
||||||
|
return d2;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'sd', () => {
|
||||||
|
return sd;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'c') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
arg1: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = c(req.args[0], req.args[1], callParams);
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
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 doSmth');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
178
src/compiled/examples/assignment.ts
Normal file
178
src/compiled/examples/assignment.ts
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface OpHaDef {
|
||||||
|
array: (a: string, b: string, callParams: CallParams<'a' | 'b'>) => string[];
|
||||||
|
identity: (a: string, callParams: CallParams<'a'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOpHa(service: OpHaDef): void;
|
||||||
|
export function registerOpHa(serviceId: string, service: OpHaDef): void;
|
||||||
|
export function registerOpHa(peer: FluencePeer, service: OpHaDef): void;
|
||||||
|
export function registerOpHa(peer: FluencePeer, serviceId: string, service: OpHaDef): void;
|
||||||
|
export function registerOpHa(...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 = 'op';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['array', 'identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OpHa: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'array') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
a: req.tetraplets[0],
|
||||||
|
b: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.array(req.args[0], req.args[1], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
a: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.identity(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function doSmth(arg: { value: string }, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function doSmth(peer: FluencePeer, arg: { value: string }, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function doSmth(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let arg: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
arg = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
arg = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "arg") [] arg)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("op" "identity") [arg.$.value!] a)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("op" "array") [a "hello"] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'arg', () => {
|
||||||
|
return arg;
|
||||||
|
});
|
||||||
|
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 doSmth');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
152
src/compiled/examples/callArrow.ts
Normal file
152
src/compiled/examples/callArrow.ts
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function passFunctionAsArg(
|
||||||
|
node: string,
|
||||||
|
str: string,
|
||||||
|
c: (arg0: string, callParams: CallParams<'arg0'>) => string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function passFunctionAsArg(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node: string,
|
||||||
|
str: string,
|
||||||
|
c: (arg0: string, callParams: CallParams<'arg0'>) => string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function passFunctionAsArg(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node: any;
|
||||||
|
let str: any;
|
||||||
|
let c: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node = args[1];
|
||||||
|
str = args[2];
|
||||||
|
c = args[3];
|
||||||
|
config = args[4];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node = args[0];
|
||||||
|
str = args[1];
|
||||||
|
c = args[2];
|
||||||
|
config = args[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "str") [] str)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call node ("peer" "identify") [])
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "c") [str] init_call_res0)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call node ("peer" "identify") [])
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") [init_call_res0])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node', () => {
|
||||||
|
return node;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'str', () => {
|
||||||
|
return str;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'c') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = c(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for passFunctionAsArg');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
202
src/compiled/examples/co.ts
Normal file
202
src/compiled/examples/co.ts
Normal file
@ -0,0 +1,202 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface CoServiceDef {
|
||||||
|
call: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerCoService(service: CoServiceDef): void;
|
||||||
|
export function registerCoService(serviceId: string, service: CoServiceDef): void;
|
||||||
|
export function registerCoService(peer: FluencePeer, service: CoServiceDef): void;
|
||||||
|
export function registerCoService(peer: FluencePeer, serviceId: string, service: CoServiceDef): void;
|
||||||
|
export function registerCoService(...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 = 'coservice-id';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['call']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service CoService: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'call') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.call(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function coFunc(
|
||||||
|
node: string,
|
||||||
|
c: (arg0: { external_addresses: string[] }, callParams: CallParams<'arg0'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function coFunc(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node: string,
|
||||||
|
c: (arg0: { external_addresses: string[] }, callParams: CallParams<'arg0'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function coFunc(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node: any;
|
||||||
|
let c: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node = args[1];
|
||||||
|
c = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node = args[0];
|
||||||
|
c = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("coservice-id" "call") [] y)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call node ("peer" "identify") [] t)
|
||||||
|
(par
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "c") [t])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("coservice-id" "call") [] x)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node', () => {
|
||||||
|
return node;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'c') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
c(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for coFunc');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
326
src/compiled/examples/complex.ts
Normal file
326
src/compiled/examples/complex.ts
Normal file
@ -0,0 +1,326 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface TestSDef {
|
||||||
|
multiline: (a: string, b: string, c: boolean, callParams: CallParams<'a' | 'b' | 'c'>) => string;
|
||||||
|
t: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerTestS(service: TestSDef): void;
|
||||||
|
export function registerTestS(serviceId: string, service: TestSDef): void;
|
||||||
|
export function registerTestS(peer: FluencePeer, service: TestSDef): void;
|
||||||
|
export function registerTestS(peer: FluencePeer, serviceId: string, service: TestSDef): void;
|
||||||
|
export function registerTestS(...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 = 'some-id';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['multiline', 't']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service TestS: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'multiline') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
a: req.tetraplets[0],
|
||||||
|
b: req.tetraplets[1],
|
||||||
|
c: req.tetraplets[2],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.multiline(req.args[0], req.args[1], req.args[2], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 't') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.t(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function doStuff(
|
||||||
|
a: string,
|
||||||
|
b: string,
|
||||||
|
c: boolean,
|
||||||
|
d: boolean,
|
||||||
|
e: string[],
|
||||||
|
g: string[],
|
||||||
|
str: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string[]>;
|
||||||
|
export function doStuff(
|
||||||
|
peer: FluencePeer,
|
||||||
|
a: string,
|
||||||
|
b: string,
|
||||||
|
c: boolean,
|
||||||
|
d: boolean,
|
||||||
|
e: string[],
|
||||||
|
g: string[],
|
||||||
|
str: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string[]>;
|
||||||
|
export function doStuff(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let a: any;
|
||||||
|
let b: any;
|
||||||
|
let c: any;
|
||||||
|
let d: any;
|
||||||
|
let e: any;
|
||||||
|
let g: any;
|
||||||
|
let str: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
a = args[1];
|
||||||
|
b = args[2];
|
||||||
|
c = args[3];
|
||||||
|
d = args[4];
|
||||||
|
e = args[5];
|
||||||
|
g = args[6];
|
||||||
|
str = args[7];
|
||||||
|
config = args[8];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
a = args[0];
|
||||||
|
b = args[1];
|
||||||
|
c = args[2];
|
||||||
|
d = args[3];
|
||||||
|
e = args[4];
|
||||||
|
g = args[5];
|
||||||
|
str = args[6];
|
||||||
|
config = args[7];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "a") [] a)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "b") [] b)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "c") [] c)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "d") [] d)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "e") [] e)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "g") [] g)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "str") [] str)
|
||||||
|
)
|
||||||
|
(par
|
||||||
|
(par
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("some-id" "t") [str] $stream)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call b ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") [a])
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(xor
|
||||||
|
(call a ("peer" "identify") [])
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(xor
|
||||||
|
(match c true
|
||||||
|
(xor
|
||||||
|
(match d true
|
||||||
|
(xor
|
||||||
|
(fold e eEl
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(fold g gEl
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call b ("some-id" "t") [gEl] $stream)
|
||||||
|
(call b ("some-id" "t") [eEl] $stream)
|
||||||
|
)
|
||||||
|
(next gEl)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call b ("some-id" "t") [eEl] $stream)
|
||||||
|
)
|
||||||
|
(next eEl)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("some-id" "multiline") [a b c] $stream)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$stream])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'a', () => {
|
||||||
|
return a;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'b', () => {
|
||||||
|
return b;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'c', () => {
|
||||||
|
return c;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'd', () => {
|
||||||
|
return d;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'e', () => {
|
||||||
|
return e;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'g', () => {
|
||||||
|
return g;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'str', () => {
|
||||||
|
return str;
|
||||||
|
});
|
||||||
|
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 doStuff');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
226
src/compiled/examples/constants.ts
Normal file
226
src/compiled/examples/constants.ts
Normal file
@ -0,0 +1,226 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface GetterDef {
|
||||||
|
createStr: (arg0: number, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerGetter(service: GetterDef): void;
|
||||||
|
export function registerGetter(serviceId: string, service: GetterDef): void;
|
||||||
|
export function registerGetter(peer: FluencePeer, service: GetterDef): void;
|
||||||
|
export function registerGetter(peer: FluencePeer, serviceId: string, service: GetterDef): void;
|
||||||
|
export function registerGetter(...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 = 'test';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['createStr']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Getter: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'createStr') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.createStr(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OpODef {
|
||||||
|
identity: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOpO(service: OpODef): void;
|
||||||
|
export function registerOpO(serviceId: string, service: OpODef): void;
|
||||||
|
export function registerOpO(peer: FluencePeer, service: OpODef): void;
|
||||||
|
export function registerOpO(peer: FluencePeer, serviceId: string, service: OpODef): void;
|
||||||
|
export function registerOpO(...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 = 'op';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OpO: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.identity(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function callConstant(config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function callConstant(peer: FluencePeer, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function callConstant(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("test" "createStr") [5] $res)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("op" "identity") ["default-str"] $res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 callConstant');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
153
src/compiled/examples/dataAlias.ts
Normal file
153
src/compiled/examples/dataAlias.ts
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface NodeIdGetterDef {
|
||||||
|
get: (callParams: CallParams<null>) => { name: string; peerId: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerNodeIdGetter(service: NodeIdGetterDef): void;
|
||||||
|
export function registerNodeIdGetter(serviceId: string, service: NodeIdGetterDef): void;
|
||||||
|
export function registerNodeIdGetter(peer: FluencePeer, service: NodeIdGetterDef): void;
|
||||||
|
export function registerNodeIdGetter(peer: FluencePeer, serviceId: string, service: NodeIdGetterDef): void;
|
||||||
|
export function registerNodeIdGetter(...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 = 'somesrv';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['get']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service NodeIdGetter: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'get') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.get(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function getAliasedData(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function getAliasedData(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function getAliasedData(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("somesrv" "get") [] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res.$.peerId!])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 getAliasedData');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
319
src/compiled/examples/example.ts
Normal file
319
src/compiled/examples/example.ts
Normal file
@ -0,0 +1,319 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface PeerDef {
|
||||||
|
is_connected: (arg0: string, callParams: CallParams<'arg0'>) => boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerPeer(service: PeerDef): void;
|
||||||
|
export function registerPeer(serviceId: string, service: PeerDef): void;
|
||||||
|
export function registerPeer(peer: FluencePeer, service: PeerDef): void;
|
||||||
|
export function registerPeer(peer: FluencePeer, serviceId: string, service: PeerDef): void;
|
||||||
|
export function registerPeer(...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 = 'peer';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['is_connected']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Peer: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'is_connected') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.is_connected(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OpDef {
|
||||||
|
identity: (callParams: CallParams<null>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOp(service: OpDef): void;
|
||||||
|
export function registerOp(serviceId: string, service: OpDef): void;
|
||||||
|
export function registerOp(peer: FluencePeer, service: OpDef): void;
|
||||||
|
export function registerOp(peer: FluencePeer, serviceId: string, service: OpDef): void;
|
||||||
|
export function registerOp(...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 = 'op';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Op: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.identity(callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TestDef {
|
||||||
|
doSomething: (callParams: CallParams<null>) => boolean;
|
||||||
|
getUserList: (callParams: CallParams<null>) => { name: string; peer_id: string; relay_id: string }[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerTest(service: TestDef): void;
|
||||||
|
export function registerTest(serviceId: string, service: TestDef): void;
|
||||||
|
export function registerTest(peer: FluencePeer, service: TestDef): void;
|
||||||
|
export function registerTest(peer: FluencePeer, serviceId: string, service: TestDef): void;
|
||||||
|
export function registerTest(...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 = 'test';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['doSomething', 'getUserList']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Test: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'doSomething') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.doSomething(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getUserList') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.getUserList(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function betterMessage(relay: string, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function betterMessage(peer: FluencePeer, relay: string, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function betterMessage(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let relay: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
relay = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
relay = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "relay") [] relay)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call relay ("peer" "is_connected") [relay] isOnline)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(match isOnline true
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("test" "doSomething") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'relay', () => {
|
||||||
|
return relay;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for betterMessage');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
169
src/compiled/examples/fldist-sample.ts
Normal file
169
src/compiled/examples/fldist-sample.ts
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface ReturnDef {
|
||||||
|
run: (arg0: { external_addresses: string[] }, callParams: CallParams<'arg0'>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerReturn(service: ReturnDef): void;
|
||||||
|
export function registerReturn(serviceId: string, service: ReturnDef): void;
|
||||||
|
export function registerReturn(peer: FluencePeer, service: ReturnDef): void;
|
||||||
|
export function registerReturn(peer: FluencePeer, serviceId: string, service: ReturnDef): void;
|
||||||
|
export function registerReturn(...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 = 'returnService';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['run']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Return: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'run') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.run(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function test(node: string, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function test(peer: FluencePeer, node: string, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function test(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call node ("peer" "identify") [] res)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("returnService" "run") [res])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node', () => {
|
||||||
|
return node;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for test');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
199
src/compiled/examples/fold.ts
Normal file
199
src/compiled/examples/fold.ts
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function iterateAndPrint(strings: string[], config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function iterateAndPrint(peer: FluencePeer, strings: string[], config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function iterateAndPrint(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let strings: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
strings = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
strings = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "strings") [] strings)
|
||||||
|
)
|
||||||
|
(fold strings s
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") [s])
|
||||||
|
(next s)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'strings', () => {
|
||||||
|
return strings;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for iterateAndPrint');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function iterateAndPrintParallel(
|
||||||
|
nodes: string[],
|
||||||
|
c: (arg0: { external_addresses: string[] }, callParams: CallParams<'arg0'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function iterateAndPrintParallel(
|
||||||
|
peer: FluencePeer,
|
||||||
|
nodes: string[],
|
||||||
|
c: (arg0: { external_addresses: string[] }, callParams: CallParams<'arg0'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function iterateAndPrintParallel(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let nodes: any;
|
||||||
|
let c: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
nodes = args[1];
|
||||||
|
c = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
nodes = args[0];
|
||||||
|
c = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "nodes") [] nodes)
|
||||||
|
)
|
||||||
|
(fold nodes s
|
||||||
|
(par
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call s ("peer" "identify") [] ads)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "c") [ads])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(next s)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'nodes', () => {
|
||||||
|
return nodes;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'c') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
c(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for iterateAndPrintParallel');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
201
src/compiled/examples/foldJoin.ts
Normal file
201
src/compiled/examples/foldJoin.ts
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface Op2Def {
|
||||||
|
identity: (s: number, callParams: CallParams<'s'>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOp2(service: Op2Def): void;
|
||||||
|
export function registerOp2(serviceId: string, service: Op2Def): void;
|
||||||
|
export function registerOp2(peer: FluencePeer, service: Op2Def): void;
|
||||||
|
export function registerOp2(peer: FluencePeer, serviceId: string, service: Op2Def): void;
|
||||||
|
export function registerOp2(...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 = 'op';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Op2: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.identity(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function getTwoResults(relay: string, config?: { ttl?: number }): Promise<number[]>;
|
||||||
|
export function getTwoResults(peer: FluencePeer, relay: string, config?: { ttl?: number }): Promise<number[]>;
|
||||||
|
export function getTwoResults(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let relay: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
relay = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
relay = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<number[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "relay") [] relay)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call relay ("op" "string_to_b58") [%init_peer_id%] k)
|
||||||
|
(call relay ("kad" "neighborhood") [k [] []] nodes)
|
||||||
|
)
|
||||||
|
(fold nodes n
|
||||||
|
(par
|
||||||
|
(seq
|
||||||
|
(xor
|
||||||
|
(call n ("peer" "timestamp_sec") [] $res)
|
||||||
|
(null)
|
||||||
|
)
|
||||||
|
(call relay ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(next n)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call relay ("op" "identity") [$res.$.[0]!])
|
||||||
|
)
|
||||||
|
(call relay ("op" "identity") [$res.$.[1]!])
|
||||||
|
)
|
||||||
|
(call relay ("op" "identity") [$res.$.[2]!])
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'relay', () => {
|
||||||
|
return relay;
|
||||||
|
});
|
||||||
|
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 getTwoResults');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
153
src/compiled/examples/func.ts
Normal file
153
src/compiled/examples/func.ts
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface TestSrvDef {
|
||||||
|
str: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerTestSrv(service: TestSrvDef): void;
|
||||||
|
export function registerTestSrv(serviceId: string, service: TestSrvDef): void;
|
||||||
|
export function registerTestSrv(peer: FluencePeer, service: TestSrvDef): void;
|
||||||
|
export function registerTestSrv(peer: FluencePeer, serviceId: string, service: TestSrvDef): void;
|
||||||
|
export function registerTestSrv(...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 = 'test-service-id';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['str']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service TestSrv: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'str') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.str(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function testFunc(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function testFunc(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function testFunc(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("test-service-id" "str") [] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 testFunc');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
162
src/compiled/examples/helloWorld.ts
Normal file
162
src/compiled/examples/helloWorld.ts
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface StringExtraDef {
|
||||||
|
addNameToHello: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerStringExtra(service: StringExtraDef): void;
|
||||||
|
export function registerStringExtra(serviceId: string, service: StringExtraDef): void;
|
||||||
|
export function registerStringExtra(peer: FluencePeer, service: StringExtraDef): void;
|
||||||
|
export function registerStringExtra(peer: FluencePeer, serviceId: string, service: StringExtraDef): void;
|
||||||
|
export function registerStringExtra(...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 = 'service-id';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['addNameToHello']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service StringExtra: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'addNameToHello') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.addNameToHello(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function helloWorld(name: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function helloWorld(peer: FluencePeer, name: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function helloWorld(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let name: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
name = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
name = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "name") [] name)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("service-id" "addNameToHello") [name] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'name', () => {
|
||||||
|
return name;
|
||||||
|
});
|
||||||
|
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 helloWorld');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
163
src/compiled/examples/if.ts
Normal file
163
src/compiled/examples/if.ts
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function ifElseCall(condition: boolean, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function ifElseCall(peer: FluencePeer, condition: boolean, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function ifElseCall(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let condition: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
condition = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
condition = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "condition") [] condition)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(match condition true
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") ["it is true"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") ["it is false"])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'condition', () => {
|
||||||
|
return condition;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for ifElseCall');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ifElseNumCall(condition: number, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function ifElseNumCall(peer: FluencePeer, condition: number, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function ifElseNumCall(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let condition: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
condition = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
condition = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "condition") [] condition)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(match condition 1
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") ["it is 1"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") ["it is not 1"])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'condition', () => {
|
||||||
|
return condition;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for ifElseNumCall');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
89
src/compiled/examples/imports_exports/declare.ts
Normal file
89
src/compiled/examples/imports_exports/declare.ts
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface SuperFooDef {
|
||||||
|
small_foo: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerSuperFoo(service: SuperFooDef): void;
|
||||||
|
export function registerSuperFoo(serviceId: string, service: SuperFooDef): void;
|
||||||
|
export function registerSuperFoo(peer: FluencePeer, service: SuperFooDef): void;
|
||||||
|
export function registerSuperFoo(peer: FluencePeer, serviceId: string, service: SuperFooDef): void;
|
||||||
|
export function registerSuperFoo(...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 = 'super_foo';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['small_foo']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service SuperFoo: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'small_foo') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.small_foo(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
150
src/compiled/examples/imports_exports/exports.ts
Normal file
150
src/compiled/examples/imports_exports/exports.ts
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface MyExportSrvDef {
|
||||||
|
another_str: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerMyExportSrv(service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(serviceId: string, service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(peer: FluencePeer, service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(peer: FluencePeer, serviceId: string, service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(...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 = 'my_export_srv';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['another_str']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service MyExportSrv: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'another_str') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.another_str(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function string_from_lib(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function string_from_lib(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function string_from_lib(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["some_string_func"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 string_from_lib');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
86
src/compiled/examples/imports_exports/gen/OneMore.ts
Normal file
86
src/compiled/examples/imports_exports/gen/OneMore.ts
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface OneMoreDef {
|
||||||
|
more_call: (callParams: CallParams<null>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOneMore(serviceId: string, service: OneMoreDef): void;
|
||||||
|
export function registerOneMore(peer: FluencePeer, serviceId: string, service: OneMoreDef): void;
|
||||||
|
export function registerOneMore(...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];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['more_call']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OneMore: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'more_call') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.more_call(callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
157
src/compiled/examples/imports_exports/import2.ts
Normal file
157
src/compiled/examples/imports_exports/import2.ts
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function wrap(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function wrap(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function wrap(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("hello" "more_call") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("ohmygod" "more_call") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["I am MyFooBar foo"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 wrap');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function barfoo(config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function barfoo(peer: FluencePeer, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function barfoo(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(ap "I am MyFooBar foo" $res)
|
||||||
|
)
|
||||||
|
(ap " I am MyFooBar bar" $res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 barfoo');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
84
src/compiled/examples/imports_exports/import3.ts
Normal file
84
src/compiled/examples/imports_exports/import3.ts
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function foo_wrapper(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function foo_wrapper(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function foo_wrapper(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["I am MyFooBar foo"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 foo_wrapper');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
629
src/compiled/examples/imports_exports/imports-empty.ts
Normal file
629
src/compiled/examples/imports_exports/imports-empty.ts
Normal file
@ -0,0 +1,629 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface NoopDef {
|
||||||
|
array: (
|
||||||
|
a: string,
|
||||||
|
b: string | null,
|
||||||
|
c: string | null,
|
||||||
|
d: string | null,
|
||||||
|
callParams: CallParams<'a' | 'b' | 'c' | 'd'>,
|
||||||
|
) => string[];
|
||||||
|
array_length: (array: string[], callParams: CallParams<'array'>) => number;
|
||||||
|
bytes_from_b58: (b: string, callParams: CallParams<'b'>) => number[];
|
||||||
|
bytes_to_b58: (bs: number[], callParams: CallParams<'bs'>) => string;
|
||||||
|
concat: (
|
||||||
|
a: string[],
|
||||||
|
b: string[] | null,
|
||||||
|
c: string[] | null,
|
||||||
|
d: string[] | null,
|
||||||
|
callParams: CallParams<'a' | 'b' | 'c' | 'd'>,
|
||||||
|
) => string[];
|
||||||
|
concat_strings: (a: string, b: string, callParams: CallParams<'a' | 'b'>) => string;
|
||||||
|
identity: (s: string | null, callParams: CallParams<'s'>) => string | null;
|
||||||
|
noop: (callParams: CallParams<null>) => void;
|
||||||
|
sha256_string: (s: string, callParams: CallParams<'s'>) => string;
|
||||||
|
string_from_b58: (b: string, callParams: CallParams<'b'>) => string;
|
||||||
|
string_to_b58: (s: string, callParams: CallParams<'s'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerNoop(service: NoopDef): void;
|
||||||
|
export function registerNoop(serviceId: string, service: NoopDef): void;
|
||||||
|
export function registerNoop(peer: FluencePeer, service: NoopDef): void;
|
||||||
|
export function registerNoop(peer: FluencePeer, serviceId: string, service: NoopDef): void;
|
||||||
|
export function registerNoop(...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 = 'op';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, [
|
||||||
|
'array',
|
||||||
|
'array_length',
|
||||||
|
'bytes_from_b58',
|
||||||
|
'bytes_to_b58',
|
||||||
|
'concat',
|
||||||
|
'concat_strings',
|
||||||
|
'identity',
|
||||||
|
'noop',
|
||||||
|
'sha256_string',
|
||||||
|
'string_from_b58',
|
||||||
|
'string_to_b58',
|
||||||
|
]);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Noop: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'array') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
a: req.tetraplets[0],
|
||||||
|
b: req.tetraplets[1],
|
||||||
|
c: req.tetraplets[2],
|
||||||
|
d: req.tetraplets[3],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.array(
|
||||||
|
req.args[0],
|
||||||
|
req.args[1].length === 0 ? null : req.args[1][0],
|
||||||
|
req.args[2].length === 0 ? null : req.args[2][0],
|
||||||
|
req.args[3].length === 0 ? null : req.args[3][0],
|
||||||
|
callParams,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'array_length') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
array: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.array_length(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'bytes_from_b58') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
b: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.bytes_from_b58(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'bytes_to_b58') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
bs: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.bytes_to_b58(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'concat') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
a: req.tetraplets[0],
|
||||||
|
b: req.tetraplets[1],
|
||||||
|
c: req.tetraplets[2],
|
||||||
|
d: req.tetraplets[3],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.concat(
|
||||||
|
req.args[0],
|
||||||
|
req.args[1].length === 0 ? null : req.args[1][0],
|
||||||
|
req.args[2].length === 0 ? null : req.args[2][0],
|
||||||
|
req.args[3].length === 0 ? null : req.args[3][0],
|
||||||
|
callParams,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'concat_strings') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
a: req.tetraplets[0],
|
||||||
|
b: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.concat_strings(req.args[0], req.args[1], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
|
||||||
|
var respResult = service.identity(req.args[0].length === 0 ? null : req.args[0][0], callParams);
|
||||||
|
resp.result = respResult === null ? [] : [respResult];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'noop') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.noop(callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'sha256_string') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.sha256_string(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'string_from_b58') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
b: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.string_from_b58(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'string_to_b58') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.string_to_b58(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MyExportSrvDef {
|
||||||
|
another_str: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerMyExportSrv(service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(serviceId: string, service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(peer: FluencePeer, service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(peer: FluencePeer, serviceId: string, service: MyExportSrvDef): void;
|
||||||
|
export function registerMyExportSrv(...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 = 'my_export_srv';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['another_str']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service MyExportSrv: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'another_str') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.another_str(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function some_str(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function some_str(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function some_str(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["some_string_func"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 some_str');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function some_string(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function some_string(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function some_string(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["some_string_func"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 some_string');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decl_foo(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function decl_foo(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function decl_foo(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("super_foo" "small_foo") [] res1)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res1])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 decl_foo');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decl_bar(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function decl_bar(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function decl_bar(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["declare all bar"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 decl_bar');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function some_random_func(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function some_random_func(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function some_random_func(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["wow, so random"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 some_random_func');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
174
src/compiled/examples/imports_exports/imports.ts
Normal file
174
src/compiled/examples/imports_exports/imports.ts
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface StringServiceDef {
|
||||||
|
concat: (a: string, b: string, callParams: CallParams<'a' | 'b'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerStringService(service: StringServiceDef): void;
|
||||||
|
export function registerStringService(serviceId: string, service: StringServiceDef): void;
|
||||||
|
export function registerStringService(peer: FluencePeer, service: StringServiceDef): void;
|
||||||
|
export function registerStringService(peer: FluencePeer, serviceId: string, service: StringServiceDef): void;
|
||||||
|
export function registerStringService(...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 = 'string_service';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['concat']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service StringService: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'concat') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
a: req.tetraplets[0],
|
||||||
|
b: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.concat(req.args[0], req.args[1], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function concat_foobars(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function concat_foobars(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function concat_foobars(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("super_foo" "small_foo") [] res1)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("string_service" "concat") [res1 "declare all bar"] res3)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("super_foo" "small_foo") [] res4)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("string_service" "concat") [res3 res4] res5)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("string_service" "concat") [res5 "export_const"] res6)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("string_service" "concat") [res6 "declare_const"] res7)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("string_service" "concat") [res7 "declare_const2"] res8)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res8])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 concat_foobars');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
155
src/compiled/examples/imports_exports/subImport.ts
Normal file
155
src/compiled/examples/imports_exports/subImport.ts
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface SubServiceDef {
|
||||||
|
sub: (s: string, callParams: CallParams<'s'>) => { one: string; two: number };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerSubService(service: SubServiceDef): void;
|
||||||
|
export function registerSubService(serviceId: string, service: SubServiceDef): void;
|
||||||
|
export function registerSubService(peer: FluencePeer, service: SubServiceDef): void;
|
||||||
|
export function registerSubService(peer: FluencePeer, serviceId: string, service: SubServiceDef): void;
|
||||||
|
export function registerSubService(...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 = 'sub_service';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['sub']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service SubService: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'sub') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.sub(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function subImport(config?: { ttl?: number }): Promise<{ one: string; two: number }>;
|
||||||
|
export function subImport(peer: FluencePeer, config?: { ttl?: number }): Promise<{ one: string; two: number }>;
|
||||||
|
export function subImport(...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<{ one: string; two: number }>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("sub_service" "sub") ["some thing"] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 subImport');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
333
src/compiled/examples/multiReturn.ts
Normal file
333
src/compiled/examples/multiReturn.ts
Normal file
@ -0,0 +1,333 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface GetStrDef {
|
||||||
|
retStr: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerGetStr(service: GetStrDef): void;
|
||||||
|
export function registerGetStr(serviceId: string, service: GetStrDef): void;
|
||||||
|
export function registerGetStr(peer: FluencePeer, service: GetStrDef): void;
|
||||||
|
export function registerGetStr(peer: FluencePeer, serviceId: string, service: GetStrDef): void;
|
||||||
|
export function registerGetStr(...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 = 'multiret-test';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['retStr']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service GetStr: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'retStr') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.retStr(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GetNumDef {
|
||||||
|
retNum: (callParams: CallParams<null>) => number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerGetNum(service: GetNumDef): void;
|
||||||
|
export function registerGetNum(serviceId: string, service: GetNumDef): void;
|
||||||
|
export function registerGetNum(peer: FluencePeer, service: GetNumDef): void;
|
||||||
|
export function registerGetNum(peer: FluencePeer, serviceId: string, service: GetNumDef): void;
|
||||||
|
export function registerGetNum(...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 = 'multiret-num';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['retNum']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service GetNum: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'retNum') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.retNum(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function tupleFunc(config?: { ttl?: number }): Promise<[string, number]>;
|
||||||
|
export function tupleFunc(peer: FluencePeer, config?: { ttl?: number }): Promise<[string, number]>;
|
||||||
|
export function tupleFunc(...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, number]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("multiret-test" "retStr") ["some-str"] str)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("multiret-num" "retNum") [] n)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [str n])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let opt: any = args;
|
||||||
|
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for tupleFunc');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function multiReturnFunc(
|
||||||
|
somethingToReturn: number[],
|
||||||
|
smthOption: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<[string[], number, string, number[], string | null, number]>;
|
||||||
|
export function multiReturnFunc(
|
||||||
|
peer: FluencePeer,
|
||||||
|
somethingToReturn: number[],
|
||||||
|
smthOption: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<[string[], number, string, number[], string | null, number]>;
|
||||||
|
export function multiReturnFunc(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let somethingToReturn: any;
|
||||||
|
let smthOption: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
somethingToReturn = args[1];
|
||||||
|
smthOption = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
somethingToReturn = args[0];
|
||||||
|
smthOption = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<[string[], number, string, number[], string | null, number]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "somethingToReturn") [] somethingToReturn)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "smthOption") [] smthOption)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("multiret-test" "retStr") ["some-str"] $res)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("multiret-test" "retStr") ["random-str"] $res)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("multiret-test" "retStr") ["some-str"] str)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("multiret-num" "retNum") [] n)
|
||||||
|
)
|
||||||
|
(ap str $res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$res 5 "some-str" somethingToReturn smthOption n])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'somethingToReturn', () => {
|
||||||
|
return somethingToReturn;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'smthOption', () => {
|
||||||
|
return smthOption === null ? [] : [smthOption];
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let opt: any = args;
|
||||||
|
if (Array.isArray(opt[4])) {
|
||||||
|
if (opt[4].length === 0) {
|
||||||
|
opt[4] = null;
|
||||||
|
} else {
|
||||||
|
opt[4] = opt[4][0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for multiReturnFunc');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
233
src/compiled/examples/nestedFuncs.ts
Normal file
233
src/compiled/examples/nestedFuncs.ts
Normal file
@ -0,0 +1,233 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface OpHDef {
|
||||||
|
identity: (s: string, callParams: CallParams<'s'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOpH(service: OpHDef): void;
|
||||||
|
export function registerOpH(serviceId: string, service: OpHDef): void;
|
||||||
|
export function registerOpH(peer: FluencePeer, service: OpHDef): void;
|
||||||
|
export function registerOpH(peer: FluencePeer, serviceId: string, service: OpHDef): void;
|
||||||
|
export function registerOpH(...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 = 'opa';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OpH: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.identity(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function a(b: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function a(peer: FluencePeer, b: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function a(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let b: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
b = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
b = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "b") [] b)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("opa" "identity") [b] c)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [c])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'b', () => {
|
||||||
|
return b;
|
||||||
|
});
|
||||||
|
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 a');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function d(e: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function d(peer: FluencePeer, e: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function d(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let e: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
e = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
e = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "e") [] e)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("opa" "identity") [e] c)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [c])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'e', () => {
|
||||||
|
return e;
|
||||||
|
});
|
||||||
|
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 d');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
639
src/compiled/examples/new-api-full.ts
Normal file
639
src/compiled/examples/new-api-full.ts
Normal file
@ -0,0 +1,639 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface ServiceWithDefaultIdDef {
|
||||||
|
hello: (s: string, callParams: CallParams<'s'>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerServiceWithDefaultId(service: ServiceWithDefaultIdDef): void;
|
||||||
|
export function registerServiceWithDefaultId(serviceId: string, service: ServiceWithDefaultIdDef): void;
|
||||||
|
export function registerServiceWithDefaultId(peer: FluencePeer, service: ServiceWithDefaultIdDef): void;
|
||||||
|
export function registerServiceWithDefaultId(
|
||||||
|
peer: FluencePeer,
|
||||||
|
serviceId: string,
|
||||||
|
service: ServiceWithDefaultIdDef,
|
||||||
|
): void;
|
||||||
|
export function registerServiceWithDefaultId(...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 = 'defaultId';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['hello']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service ServiceWithDefaultId: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'hello') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.hello(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceWithOUTDefaultIdDef {
|
||||||
|
hello: (s: string, callParams: CallParams<'s'>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerServiceWithOUTDefaultId(serviceId: string, service: ServiceWithOUTDefaultIdDef): void;
|
||||||
|
export function registerServiceWithOUTDefaultId(
|
||||||
|
peer: FluencePeer,
|
||||||
|
serviceId: string,
|
||||||
|
service: ServiceWithOUTDefaultIdDef,
|
||||||
|
): void;
|
||||||
|
export function registerServiceWithOUTDefaultId(...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];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['hello']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service ServiceWithOUTDefaultId: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'hello') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.hello(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MoreMembersDef {
|
||||||
|
member1: (callParams: CallParams<null>) => void;
|
||||||
|
member2: (s1: string, callParams: CallParams<'s1'>) => void;
|
||||||
|
member3: (s1: string, s2: string, callParams: CallParams<'s1' | 's2'>) => void;
|
||||||
|
member4: (s1: string, s2: string, i: number, callParams: CallParams<'s1' | 's2' | 'i'>) => number;
|
||||||
|
member5: (s1: string, s2: string, i: number, callParams: CallParams<'s1' | 's2' | 'i'>) => number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerMoreMembers(serviceId: string, service: MoreMembersDef): void;
|
||||||
|
export function registerMoreMembers(peer: FluencePeer, serviceId: string, service: MoreMembersDef): void;
|
||||||
|
export function registerMoreMembers(...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];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['member1', 'member2', 'member3', 'member4', 'member5']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service MoreMembers: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'member1') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.member1(callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'member2') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s1: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.member2(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'member3') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s1: req.tetraplets[0],
|
||||||
|
s2: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.member3(req.args[0], req.args[1], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'member4') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s1: req.tetraplets[0],
|
||||||
|
s2: req.tetraplets[1],
|
||||||
|
i: req.tetraplets[2],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.member4(req.args[0], req.args[1], req.args[2], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'member5') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s1: req.tetraplets[0],
|
||||||
|
s2: req.tetraplets[1],
|
||||||
|
i: req.tetraplets[2],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.member5(req.args[0], req.args[1], req.args[2], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function f1(
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function f1(
|
||||||
|
peer: FluencePeer,
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function f1(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let callback: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
callback = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
callback = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
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-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "callback") ["hello, world" 42])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'callback') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
arg1: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
callback(req.args[0], req.args[1], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for f1');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function f2(
|
||||||
|
num: number,
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function f2(
|
||||||
|
peer: FluencePeer,
|
||||||
|
num: number,
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function f2(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let num: any;
|
||||||
|
let callback: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
num = args[1];
|
||||||
|
callback = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
num = args[0];
|
||||||
|
callback = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "num") [] num)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "callback") ["hello, world" 42])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'num', () => {
|
||||||
|
return num;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'callback') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
arg1: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
callback(req.args[0], req.args[1], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for f2');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function f3(
|
||||||
|
num: number,
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string>;
|
||||||
|
export function f3(
|
||||||
|
peer: FluencePeer,
|
||||||
|
num: number,
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string>;
|
||||||
|
export function f3(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let num: any;
|
||||||
|
let callback: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
num = args[1];
|
||||||
|
callback = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
num = args[0];
|
||||||
|
callback = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "num") [] num)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "callback") ["hello, world" 42])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["hello world"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'num', () => {
|
||||||
|
return num;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'callback') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
arg1: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
callback(req.args[0], req.args[1], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
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 f3');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function callBackZeroArgs(
|
||||||
|
callback: (callParams: CallParams<null>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function callBackZeroArgs(
|
||||||
|
peer: FluencePeer,
|
||||||
|
callback: (callParams: CallParams<null>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function callBackZeroArgs(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let callback: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
callback = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
callback = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
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-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "callback") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'callback') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
callback(callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for callBackZeroArgs');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
184
src/compiled/examples/new-api.ts
Normal file
184
src/compiled/examples/new-api.ts
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface HelloWorldDef {
|
||||||
|
getNumber: (callParams: CallParams<null>) => number;
|
||||||
|
sayHello: (s: string, callParams: CallParams<'s'>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerHelloWorld(service: HelloWorldDef): void;
|
||||||
|
export function registerHelloWorld(serviceId: string, service: HelloWorldDef): void;
|
||||||
|
export function registerHelloWorld(peer: FluencePeer, service: HelloWorldDef): void;
|
||||||
|
export function registerHelloWorld(peer: FluencePeer, serviceId: string, service: HelloWorldDef): void;
|
||||||
|
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 = 'default';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['getNumber', 'sayHello']);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getNumber') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.getNumber(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'sayHello') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.sayHello(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function callMeBack(
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function callMeBack(
|
||||||
|
peer: FluencePeer,
|
||||||
|
callback: (arg0: string, arg1: number, callParams: CallParams<'arg0' | 'arg1'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function callMeBack(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let callback: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
callback = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
callback = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
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-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "callback") ["hello, world" 42])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'callback') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
arg1: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
callback(req.args[0], req.args[1], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for callMeBack');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
219
src/compiled/examples/on.ts
Normal file
219
src/compiled/examples/on.ts
Normal file
@ -0,0 +1,219 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function getPeerExternalAddresses(otherNodePeerId: string, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function getPeerExternalAddresses(
|
||||||
|
peer: FluencePeer,
|
||||||
|
otherNodePeerId: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string[]>;
|
||||||
|
export function getPeerExternalAddresses(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let otherNodePeerId: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
otherNodePeerId = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
otherNodePeerId = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "otherNodePeerId") [] otherNodePeerId)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call otherNodePeerId ("peer" "identify") [] res)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res.$.external_addresses!])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'otherNodePeerId', () => {
|
||||||
|
return otherNodePeerId;
|
||||||
|
});
|
||||||
|
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 getPeerExternalAddresses');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getDistantAddresses(target: string, viaNode: string, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function getDistantAddresses(
|
||||||
|
peer: FluencePeer,
|
||||||
|
target: string,
|
||||||
|
viaNode: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string[]>;
|
||||||
|
export function getDistantAddresses(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let target: any;
|
||||||
|
let viaNode: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
target = args[1];
|
||||||
|
viaNode = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
target = args[0];
|
||||||
|
viaNode = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "target") [] target)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "viaNode") [] viaNode)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call viaNode ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call target ("peer" "identify") [] res)
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call viaNode ("op" "noop") [])
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call viaNode ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res.$.external_addresses!])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'target', () => {
|
||||||
|
return target;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'viaNode', () => {
|
||||||
|
return viaNode;
|
||||||
|
});
|
||||||
|
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 getDistantAddresses');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
336
src/compiled/examples/option.ts
Normal file
336
src/compiled/examples/option.ts
Normal file
@ -0,0 +1,336 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface SomeSDef {
|
||||||
|
getStr: (arg0: string | null, callParams: CallParams<'arg0'>) => string | null;
|
||||||
|
getStr1: (callParams: CallParams<null>) => string | null;
|
||||||
|
getStr2: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerSomeS(service: SomeSDef): void;
|
||||||
|
export function registerSomeS(serviceId: string, service: SomeSDef): void;
|
||||||
|
export function registerSomeS(peer: FluencePeer, service: SomeSDef): void;
|
||||||
|
export function registerSomeS(peer: FluencePeer, serviceId: string, service: SomeSDef): void;
|
||||||
|
export function registerSomeS(...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 = 'test2';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['getStr', 'getStr1', 'getStr2']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service SomeS: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getStr') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
|
||||||
|
var respResult = service.getStr(req.args[0].length === 0 ? null : req.args[0][0], callParams);
|
||||||
|
resp.result = respResult === null ? [] : [respResult];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getStr1') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
|
||||||
|
var respResult = service.getStr1(callParams);
|
||||||
|
resp.result = respResult === null ? [] : [respResult];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getStr2') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.getStr2(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function useOptional(opt: string | null, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function useOptional(peer: FluencePeer, opt: string | null, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function useOptional(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let opt: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
opt = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
opt = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "opt") [] opt)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("test2" "getStr") [opt] res)
|
||||||
|
)
|
||||||
|
(fold opt i
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("test2" "getStr2") [i])
|
||||||
|
(next i)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res.$.[0]!])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'opt', () => {
|
||||||
|
return opt === null ? [] : [opt];
|
||||||
|
});
|
||||||
|
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 useOptional');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function returnOptional(config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function returnOptional(peer: FluencePeer, config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function returnOptional(...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 | null>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("test2" "getStr1") [] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let [opt] = args;
|
||||||
|
if (Array.isArray(opt)) {
|
||||||
|
if (opt.length === 0) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
opt = opt[0];
|
||||||
|
}
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for returnOptional');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function returnNone(config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function returnNone(peer: FluencePeer, config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function returnNone(...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 | null>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$result])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let [opt] = args;
|
||||||
|
if (Array.isArray(opt)) {
|
||||||
|
if (opt.length === 0) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
opt = opt[0];
|
||||||
|
}
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for returnNone');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
373
src/compiled/examples/options/option_gen.ts
Normal file
373
src/compiled/examples/options/option_gen.ts
Normal file
@ -0,0 +1,373 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface OptionStringDef {
|
||||||
|
checkOption: (str: string | null, callParams: CallParams<'str'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOptionString(service: OptionStringDef): void;
|
||||||
|
export function registerOptionString(serviceId: string, service: OptionStringDef): void;
|
||||||
|
export function registerOptionString(peer: FluencePeer, service: OptionStringDef): void;
|
||||||
|
export function registerOptionString(peer: FluencePeer, serviceId: string, service: OptionStringDef): void;
|
||||||
|
export function registerOptionString(...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 = 'opt_str';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['checkOption']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OptionString: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'checkOption') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
str: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.checkOption(req.args[0].length === 0 ? null : req.args[0][0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function emptyString(config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function emptyString(peer: FluencePeer, config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function emptyString(...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 | null>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$valueEmpty])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let [opt] = args;
|
||||||
|
if (Array.isArray(opt)) {
|
||||||
|
if (opt.length === 0) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
opt = opt[0];
|
||||||
|
}
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for emptyString');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkEmpty(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function checkEmpty(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function checkEmpty(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("opt_str" "checkOption") [$valueEmpty] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 checkEmpty');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stringAsOption(str: string, config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function stringAsOption(peer: FluencePeer, str: string, config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function stringAsOption(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let str: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
str = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
str = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string | null>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "str") [] str)
|
||||||
|
)
|
||||||
|
(ap str $valueEmpty)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$valueEmpty])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'str', () => {
|
||||||
|
return str;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let [opt] = args;
|
||||||
|
if (Array.isArray(opt)) {
|
||||||
|
if (opt.length === 0) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
opt = opt[0];
|
||||||
|
}
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for stringAsOption');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkNoneEmpty(str: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function checkNoneEmpty(peer: FluencePeer, str: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function checkNoneEmpty(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let str: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
str = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
str = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "str") [] str)
|
||||||
|
)
|
||||||
|
(ap str $valueEmpty)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("opt_str" "checkOption") [$valueEmpty] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'str', () => {
|
||||||
|
return str;
|
||||||
|
});
|
||||||
|
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 checkNoneEmpty');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
199
src/compiled/examples/par.ts
Normal file
199
src/compiled/examples/par.ts
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface ParServiceDef {
|
||||||
|
call: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerParService(service: ParServiceDef): void;
|
||||||
|
export function registerParService(serviceId: string, service: ParServiceDef): void;
|
||||||
|
export function registerParService(peer: FluencePeer, service: ParServiceDef): void;
|
||||||
|
export function registerParService(peer: FluencePeer, serviceId: string, service: ParServiceDef): void;
|
||||||
|
export function registerParService(...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 = 'parservice-id';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['call']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service ParService: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'call') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.call(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function parFunc(
|
||||||
|
node: string,
|
||||||
|
c: (arg0: { external_addresses: string[] }, callParams: CallParams<'arg0'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function parFunc(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node: string,
|
||||||
|
c: (arg0: { external_addresses: string[] }, callParams: CallParams<'arg0'>) => void,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function parFunc(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node: any;
|
||||||
|
let c: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node = args[1];
|
||||||
|
c = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node = args[0];
|
||||||
|
c = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||||
|
)
|
||||||
|
(par
|
||||||
|
(par
|
||||||
|
(call %init_peer_id% ("parservice-id" "call") [] y)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call node ("peer" "identify") [] t)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "c") [t])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("parservice-id" "call") [] x)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node', () => {
|
||||||
|
return node;
|
||||||
|
});
|
||||||
|
h.use((req, resp, next) => {
|
||||||
|
if (req.serviceId === 'callbackSrv' && req.fnName === 'c') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
c(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for parFunc');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
269
src/compiled/examples/passArgs.ts
Normal file
269
src/compiled/examples/passArgs.ts
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface AquaDHTDef {
|
||||||
|
put_host_value: (
|
||||||
|
key: string,
|
||||||
|
value: string,
|
||||||
|
service_id: string[],
|
||||||
|
callParams: CallParams<'key' | 'value' | 'service_id'>,
|
||||||
|
) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerAquaDHT(service: AquaDHTDef): void;
|
||||||
|
export function registerAquaDHT(serviceId: string, service: AquaDHTDef): void;
|
||||||
|
export function registerAquaDHT(peer: FluencePeer, service: AquaDHTDef): void;
|
||||||
|
export function registerAquaDHT(peer: FluencePeer, serviceId: string, service: AquaDHTDef): void;
|
||||||
|
export function registerAquaDHT(...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 = 'test-dht';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['put_host_value']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service AquaDHT: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'put_host_value') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
key: req.tetraplets[0],
|
||||||
|
value: req.tetraplets[1],
|
||||||
|
service_id: req.tetraplets[2],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.put_host_value(req.args[0], req.args[1], req.args[2], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function putHostValue(
|
||||||
|
key: string,
|
||||||
|
value: string,
|
||||||
|
service_id: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string>;
|
||||||
|
export function putHostValue(
|
||||||
|
peer: FluencePeer,
|
||||||
|
key: string,
|
||||||
|
value: string,
|
||||||
|
service_id: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string>;
|
||||||
|
export function putHostValue(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let key: any;
|
||||||
|
let value: any;
|
||||||
|
let service_id: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
key = args[1];
|
||||||
|
value = args[2];
|
||||||
|
service_id = args[3];
|
||||||
|
config = args[4];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
key = args[0];
|
||||||
|
value = args[1];
|
||||||
|
service_id = args[2];
|
||||||
|
config = args[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "key") [] key)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "value") [] value)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("test-dht" "put_host_value") [key value service_id] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'key', () => {
|
||||||
|
return key;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'value', () => {
|
||||||
|
return value;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'service_id', () => {
|
||||||
|
return service_id === null ? [] : [service_id];
|
||||||
|
});
|
||||||
|
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 putHostValue');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function create_client_util(service_id: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function create_client_util(peer: FluencePeer, service_id: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function create_client_util(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let service_id: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
service_id = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
service_id = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("test-dht" "put_host_value") ["client-util" service_id []] res)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [res])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'service_id', () => {
|
||||||
|
return service_id;
|
||||||
|
});
|
||||||
|
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 create_client_util');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
154
src/compiled/examples/println.ts
Normal file
154
src/compiled/examples/println.ts
Normal file
@ -0,0 +1,154 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface PrintlnDef {
|
||||||
|
print: (arg0: string, callParams: CallParams<'arg0'>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerPrintln(service: PrintlnDef): void;
|
||||||
|
export function registerPrintln(serviceId: string, service: PrintlnDef): void;
|
||||||
|
export function registerPrintln(peer: FluencePeer, service: PrintlnDef): void;
|
||||||
|
export function registerPrintln(peer: FluencePeer, serviceId: string, service: PrintlnDef): void;
|
||||||
|
export function registerPrintln(...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 = 'println-service-id';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['print']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Println: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'print') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.print(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function print(str: string, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function print(peer: FluencePeer, str: string, config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function print(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let str: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
str = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
str = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "str") [] str)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("println-service-id" "print") [str])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'str', () => {
|
||||||
|
return str;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for print');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
159
src/compiled/examples/pushToStream.ts
Normal file
159
src/compiled/examples/pushToStream.ts
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface OpADef {
|
||||||
|
get_str: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOpA(service: OpADef): void;
|
||||||
|
export function registerOpA(serviceId: string, service: OpADef): void;
|
||||||
|
export function registerOpA(peer: FluencePeer, service: OpADef): void;
|
||||||
|
export function registerOpA(peer: FluencePeer, serviceId: string, service: OpADef): void;
|
||||||
|
export function registerOpA(...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 = 'pop';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['get_str']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OpA: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'get_str') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.get_str(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function get_results(config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function get_results(peer: FluencePeer, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function get_results(...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
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(ap "hello" $results)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("pop" "get_str") [] str)
|
||||||
|
)
|
||||||
|
(ap str $results)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$results])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 get_results');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
84
src/compiled/examples/returnLiteral.ts
Normal file
84
src/compiled/examples/returnLiteral.ts
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function returnLiteral(config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function returnLiteral(peer: FluencePeer, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function returnLiteral(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["some literal"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 returnLiteral');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
429
src/compiled/examples/stream.ts
Normal file
429
src/compiled/examples/stream.ts
Normal file
@ -0,0 +1,429 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface StringerDef {
|
||||||
|
returnString: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerStringer(service: StringerDef): void;
|
||||||
|
export function registerStringer(serviceId: string, service: StringerDef): void;
|
||||||
|
export function registerStringer(peer: FluencePeer, service: StringerDef): void;
|
||||||
|
export function registerStringer(peer: FluencePeer, serviceId: string, service: StringerDef): void;
|
||||||
|
export function registerStringer(...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 = 'stringer-id';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['returnString']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Stringer: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'returnString') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.returnString(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function returnNone(config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function returnNone(peer: FluencePeer, config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function returnNone(...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 | null>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$valueNone])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let [opt] = args;
|
||||||
|
if (Array.isArray(opt)) {
|
||||||
|
if (opt.length === 0) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
opt = opt[0];
|
||||||
|
}
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for returnNone');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stringNone(config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function stringNone(peer: FluencePeer, config?: { ttl?: number }): Promise<string | null>;
|
||||||
|
export function stringNone(...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 | null>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$valueNone])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {
|
||||||
|
let [opt] = args;
|
||||||
|
if (Array.isArray(opt)) {
|
||||||
|
if (opt.length === 0) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
opt = opt[0];
|
||||||
|
}
|
||||||
|
return resolve(opt);
|
||||||
|
});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for stringNone');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function returnNil(config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function returnNil(peer: FluencePeer, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function returnNil(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$valueNil])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 returnNil');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stringNil(config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function stringNil(peer: FluencePeer, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function stringNil(...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
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$valueNil])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(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 stringNil');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkStreams(ch: string[], config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function checkStreams(peer: FluencePeer, ch: string[], config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function checkStreams(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let ch: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
ch = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
ch = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "ch") [] ch)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("stringer-id" "returnString") ["first"] $stream)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("stringer-id" "returnString") ["second"] $stream)
|
||||||
|
)
|
||||||
|
(fold ch b
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("stringer-id" "returnString") [b] $stream)
|
||||||
|
(next b)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$stream])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'ch', () => {
|
||||||
|
return ch;
|
||||||
|
});
|
||||||
|
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 checkStreams');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
246
src/compiled/examples/streamArgs.ts
Normal file
246
src/compiled/examples/streamArgs.ts
Normal file
@ -0,0 +1,246 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface TestServiceDef {
|
||||||
|
get_records: (key: string, callParams: CallParams<'key'>) => string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerTestService(service: TestServiceDef): void;
|
||||||
|
export function registerTestService(serviceId: string, service: TestServiceDef): void;
|
||||||
|
export function registerTestService(peer: FluencePeer, service: TestServiceDef): void;
|
||||||
|
export function registerTestService(peer: FluencePeer, serviceId: string, service: TestServiceDef): void;
|
||||||
|
export function registerTestService(...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 = 'test-service';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['get_records']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service TestService: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'get_records') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
key: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.get_records(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function append_records(peer_: string, srum: string[][], config?: { ttl?: number }): Promise<void>;
|
||||||
|
export function append_records(
|
||||||
|
peer: FluencePeer,
|
||||||
|
peer_: string,
|
||||||
|
srum: string[][],
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<void>;
|
||||||
|
export function append_records(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let peer_: any;
|
||||||
|
let srum: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
peer_ = args[1];
|
||||||
|
srum = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
peer_ = args[0];
|
||||||
|
srum = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "peer") [] peer)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "srum") [] srum-iter)
|
||||||
|
)
|
||||||
|
(fold srum-iter srum-item
|
||||||
|
(seq
|
||||||
|
(ap srum-item $srum)
|
||||||
|
(next srum-item)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("test-service" "get_records") [peer] $srum)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'peer', () => {
|
||||||
|
return peer_;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'srum', () => {
|
||||||
|
return srum;
|
||||||
|
});
|
||||||
|
h.onEvent('callbackSrv', 'response', (args) => {});
|
||||||
|
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||||
|
const [err] = args;
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.handleScriptError(reject)
|
||||||
|
.handleTimeout(() => {
|
||||||
|
reject('Request timed out for append_records');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return Promise.race([promise, Promise.resolve()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function retrieve_records(peer_: string, config?: { ttl?: number }): Promise<string[][]>;
|
||||||
|
export function retrieve_records(peer: FluencePeer, peer_: string, config?: { ttl?: number }): Promise<string[][]>;
|
||||||
|
export function retrieve_records(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let peer_: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
peer_ = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
peer_ = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[][]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "peer") [] peer)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("test-service" "get_records") [peer] $records)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$records])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'peer', () => {
|
||||||
|
return peer_;
|
||||||
|
});
|
||||||
|
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 retrieve_records');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
248
src/compiled/examples/streamResults.ts
Normal file
248
src/compiled/examples/streamResults.ts
Normal file
@ -0,0 +1,248 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface DTGetterDef {
|
||||||
|
get_dt: (s: string, callParams: CallParams<'s'>) => { field: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerDTGetter(service: DTGetterDef): void;
|
||||||
|
export function registerDTGetter(serviceId: string, service: DTGetterDef): void;
|
||||||
|
export function registerDTGetter(peer: FluencePeer, service: DTGetterDef): void;
|
||||||
|
export function registerDTGetter(peer: FluencePeer, serviceId: string, service: DTGetterDef): void;
|
||||||
|
export function registerDTGetter(...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 = 'get-dt';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['get_dt']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service DTGetter: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'get_dt') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.get_dt(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function use_name1(name: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function use_name1(peer: FluencePeer, name: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function use_name1(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let name: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
name = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
name = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "name") [] name)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("get-dt" "get_dt") [name] results)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [results.$.field!])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'name', () => {
|
||||||
|
return name;
|
||||||
|
});
|
||||||
|
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 use_name1');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function use_name2(name: string, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function use_name2(peer: FluencePeer, name: string, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function use_name2(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let name: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
name = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
name = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "name") [] name)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("get-dt" "get_dt") [name] results0)
|
||||||
|
)
|
||||||
|
(ap results0.$.field! $results)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("get-dt" "get_dt") [name] results1)
|
||||||
|
)
|
||||||
|
(ap results1.$.field! $results)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("get-dt" "get_dt") [name] results2)
|
||||||
|
)
|
||||||
|
(ap results2.$.field! $results)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$results])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'name', () => {
|
||||||
|
return name;
|
||||||
|
});
|
||||||
|
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 use_name2');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
177
src/compiled/examples/subImportUsage.ts
Normal file
177
src/compiled/examples/subImportUsage.ts
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface ConcatSubsDef {
|
||||||
|
get_some: (
|
||||||
|
s: string,
|
||||||
|
sr: { one: string; two: number },
|
||||||
|
callParams: CallParams<'s' | 'sr'>,
|
||||||
|
) => { one: string; two: number };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerConcatSubs(service: ConcatSubsDef): void;
|
||||||
|
export function registerConcatSubs(serviceId: string, service: ConcatSubsDef): void;
|
||||||
|
export function registerConcatSubs(peer: FluencePeer, service: ConcatSubsDef): void;
|
||||||
|
export function registerConcatSubs(peer: FluencePeer, serviceId: string, service: ConcatSubsDef): void;
|
||||||
|
export function registerConcatSubs(...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 = 'concat_subs';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['get_some']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service ConcatSubs: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'get_some') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
sr: req.tetraplets[1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.get_some(req.args[0], req.args[1], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function subImportUsage(s: string, config?: { ttl?: number }): Promise<{ one: string; two: number }>;
|
||||||
|
export function subImportUsage(
|
||||||
|
peer: FluencePeer,
|
||||||
|
s: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ one: string; two: number }>;
|
||||||
|
export function subImportUsage(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let s: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
s = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
s = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<{ one: string; two: number }>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "s") [] s)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("sub_service" "sub") [s] sr1)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("sub_service" "sub") ["some thing"] res)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("concat_subs" "get_some") [sr1.$.one! res] result)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [result])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 's', () => {
|
||||||
|
return s;
|
||||||
|
});
|
||||||
|
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 subImportUsage');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
301
src/compiled/examples/topology.ts
Normal file
301
src/compiled/examples/topology.ts
Normal file
@ -0,0 +1,301 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface TestoDef {
|
||||||
|
getString: (arg0: string, callParams: CallParams<'arg0'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerTesto(service: TestoDef): void;
|
||||||
|
export function registerTesto(serviceId: string, service: TestoDef): void;
|
||||||
|
export function registerTesto(peer: FluencePeer, service: TestoDef): void;
|
||||||
|
export function registerTesto(peer: FluencePeer, serviceId: string, service: TestoDef): void;
|
||||||
|
export function registerTesto(...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 = 'testo';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['getString']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Testo: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getString') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.getString(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LocalPrintDef {
|
||||||
|
print: (arg0: string, callParams: CallParams<'arg0'>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerLocalPrint(service: LocalPrintDef): void;
|
||||||
|
export function registerLocalPrint(serviceId: string, service: LocalPrintDef): void;
|
||||||
|
export function registerLocalPrint(peer: FluencePeer, service: LocalPrintDef): void;
|
||||||
|
export function registerLocalPrint(peer: FluencePeer, serviceId: string, service: LocalPrintDef): void;
|
||||||
|
export function registerLocalPrint(...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 = 'lp';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['print']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service LocalPrint: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'print') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
arg0: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
service.print(req.args[0], callParams);
|
||||||
|
resp.result = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function topologyTest(
|
||||||
|
me: string,
|
||||||
|
myRelay: string,
|
||||||
|
friend: string,
|
||||||
|
friendRelay: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string>;
|
||||||
|
export function topologyTest(
|
||||||
|
peer: FluencePeer,
|
||||||
|
me: string,
|
||||||
|
myRelay: string,
|
||||||
|
friend: string,
|
||||||
|
friendRelay: string,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<string>;
|
||||||
|
export function topologyTest(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let me: any;
|
||||||
|
let myRelay: any;
|
||||||
|
let friend: any;
|
||||||
|
let friendRelay: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
me = args[1];
|
||||||
|
myRelay = args[2];
|
||||||
|
friend = args[3];
|
||||||
|
friendRelay = args[4];
|
||||||
|
config = args[5];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
me = args[0];
|
||||||
|
myRelay = args[1];
|
||||||
|
friend = args[2];
|
||||||
|
friendRelay = args[3];
|
||||||
|
config = args[4];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "me") [] me)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "myRelay") [] myRelay)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "friend") [] friend)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "friendRelay") [] friendRelay)
|
||||||
|
)
|
||||||
|
(par
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call friendRelay ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call friend ("testo" "getString") ["friends string via"] str2)
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call friendRelay ("op" "noop") [])
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call friendRelay ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("lp" "print") ["my string in par"])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("lp" "print") [str2])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") ["finish"])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'me', () => {
|
||||||
|
return me;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'myRelay', () => {
|
||||||
|
return myRelay;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'friend', () => {
|
||||||
|
return friend;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'friendRelay', () => {
|
||||||
|
return friendRelay;
|
||||||
|
});
|
||||||
|
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 topologyTest');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
249
src/compiled/examples/tryCatch.ts
Normal file
249
src/compiled/examples/tryCatch.ts
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface UnexistedDef {
|
||||||
|
getStr: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerUnexisted(service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(serviceId: string, service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(peer: FluencePeer, service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(peer: FluencePeer, serviceId: string, service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(...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 = 'unex';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['getStr']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Unexisted: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getStr') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.getStr(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OpADef {
|
||||||
|
identity: (s: string, callParams: CallParams<'s'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOpA(service: OpADef): void;
|
||||||
|
export function registerOpA(serviceId: string, service: OpADef): void;
|
||||||
|
export function registerOpA(peer: FluencePeer, service: OpADef): void;
|
||||||
|
export function registerOpA(peer: FluencePeer, serviceId: string, service: OpADef): void;
|
||||||
|
export function registerOpA(...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 = 'op';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OpA: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.identity(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function tryCatchTest(node_id: string, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function tryCatchTest(peer: FluencePeer, node_id: string, config?: { ttl?: number }): Promise<string[]>;
|
||||||
|
export function tryCatchTest(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(xor
|
||||||
|
(call node_id ("unex" "getStr") [] $f)
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call node_id ("op" "identity") [%last_error%.$.msg!] $f)
|
||||||
|
(call node_id ("peer" "identify") [] i)
|
||||||
|
)
|
||||||
|
(call node_id ("op" "identity") [i.$.external_addresses.[0]!] $f)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$f])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
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 tryCatchTest');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
243
src/compiled/examples/tryOtherwise.ts
Normal file
243
src/compiled/examples/tryOtherwise.ts
Normal file
@ -0,0 +1,243 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface UnexistedDef {
|
||||||
|
getStr: (callParams: CallParams<null>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerUnexisted(service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(serviceId: string, service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(peer: FluencePeer, service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(peer: FluencePeer, serviceId: string, service: UnexistedDef): void;
|
||||||
|
export function registerUnexisted(...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 = 'unex';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['getStr']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service Unexisted: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'getStr') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.getStr(callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OpEDef {
|
||||||
|
identity: (s: string, callParams: CallParams<'s'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerOpE(service: OpEDef): void;
|
||||||
|
export function registerOpE(serviceId: string, service: OpEDef): void;
|
||||||
|
export function registerOpE(peer: FluencePeer, service: OpEDef): void;
|
||||||
|
export function registerOpE(peer: FluencePeer, serviceId: string, service: OpEDef): void;
|
||||||
|
export function registerOpE(...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 = 'op';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['identity']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service OpE: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'identity') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.identity(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function tryOtherwiseTest(node_id: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function tryOtherwiseTest(peer: FluencePeer, node_id: string, config?: { ttl?: number }): Promise<string>;
|
||||||
|
export function tryOtherwiseTest(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
config = args[2];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
config = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<string>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(xor
|
||||||
|
(call node_id ("unex" "getStr") [] $f)
|
||||||
|
(call node_id ("op" "identity") ["error"] $f)
|
||||||
|
)
|
||||||
|
(seq
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [$f.$.[0]!])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
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 tryOtherwiseTest');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
494
src/compiled/examples/via.ts
Normal file
494
src/compiled/examples/via.ts
Normal file
@ -0,0 +1,494 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.1-231
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
|
import {
|
||||||
|
ResultCodes,
|
||||||
|
RequestFlow,
|
||||||
|
RequestFlowBuilder,
|
||||||
|
CallParams,
|
||||||
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
||||||
|
|
||||||
|
function missingFields(obj: any, fields: string[]): string[] {
|
||||||
|
return fields.filter((f) => !(f in obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Services
|
||||||
|
|
||||||
|
export interface CustomIdDef {
|
||||||
|
id: (s: string, callParams: CallParams<'s'>) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerCustomId(service: CustomIdDef): void;
|
||||||
|
export function registerCustomId(serviceId: string, service: CustomIdDef): void;
|
||||||
|
export function registerCustomId(peer: FluencePeer, service: CustomIdDef): void;
|
||||||
|
export function registerCustomId(peer: FluencePeer, serviceId: string, service: CustomIdDef): void;
|
||||||
|
export function registerCustomId(...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 = 'cid';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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, ['id']);
|
||||||
|
if (!!incorrectServiceDefinitions.length) {
|
||||||
|
throw new Error(
|
||||||
|
'Error registering service CustomId: missing functions: ' +
|
||||||
|
incorrectServiceDefinitions.map((d) => "'" + d + "'").join(', '),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
||||||
|
if (req.serviceId !== serviceId) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.fnName === 'id') {
|
||||||
|
const callParams = {
|
||||||
|
...req.particleContext,
|
||||||
|
tetraplets: {
|
||||||
|
s: req.tetraplets[0],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
resp.retCode = ResultCodes.success;
|
||||||
|
resp.result = service.id(req.args[0], callParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
|
||||||
|
export function viaArr(
|
||||||
|
node_id: string,
|
||||||
|
viaAr: string[],
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ external_addresses: string[] }>;
|
||||||
|
export function viaArr(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node_id: string,
|
||||||
|
viaAr: string[],
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ external_addresses: string[] }>;
|
||||||
|
export function viaArr(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let viaAr: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
viaAr = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
viaAr = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<{ external_addresses: string[] }>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "viaAr") [] viaAr)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(fold viaAr -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call node_id ("peer" "identify") [] p)
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(fold viaAr -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(fold viaAr -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [p])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'viaAr', () => {
|
||||||
|
return viaAr;
|
||||||
|
});
|
||||||
|
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 viaArr');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function viaStream(
|
||||||
|
node_id: string,
|
||||||
|
viaStr: string[],
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ external_addresses: string[] }>;
|
||||||
|
export function viaStream(
|
||||||
|
peer: FluencePeer,
|
||||||
|
node_id: string,
|
||||||
|
viaStr: string[],
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ external_addresses: string[] }>;
|
||||||
|
export function viaStream(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let node_id: any;
|
||||||
|
let viaStr: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
viaStr = args[2];
|
||||||
|
config = args[3];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
node_id = args[0];
|
||||||
|
viaStr = args[1];
|
||||||
|
config = args[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<{ external_addresses: string[] }>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "viaStr") [] viaStr-iter)
|
||||||
|
)
|
||||||
|
(fold viaStr-iter viaStr-item
|
||||||
|
(seq
|
||||||
|
(ap viaStr-item $viaStr)
|
||||||
|
(next viaStr-item)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(fold $viaStr -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call node_id ("peer" "identify") [] p)
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(fold $viaStr -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(fold $viaStr -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [p])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'viaStr', () => {
|
||||||
|
return viaStr;
|
||||||
|
});
|
||||||
|
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 viaStream');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function viaOpt(
|
||||||
|
relay: string,
|
||||||
|
node_id: string,
|
||||||
|
viaOpt: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ external_addresses: string[] }>;
|
||||||
|
export function viaOpt(
|
||||||
|
peer: FluencePeer,
|
||||||
|
relay: string,
|
||||||
|
node_id: string,
|
||||||
|
viaOpt: string | null,
|
||||||
|
config?: { ttl?: number },
|
||||||
|
): Promise<{ external_addresses: string[] }>;
|
||||||
|
export function viaOpt(...args: any) {
|
||||||
|
let peer: FluencePeer;
|
||||||
|
let relay: any;
|
||||||
|
let node_id: any;
|
||||||
|
let viaOpt: any;
|
||||||
|
let config: any;
|
||||||
|
if (FluencePeer.isInstance(args[0])) {
|
||||||
|
peer = args[0];
|
||||||
|
relay = args[1];
|
||||||
|
node_id = args[2];
|
||||||
|
viaOpt = args[3];
|
||||||
|
config = args[4];
|
||||||
|
} else {
|
||||||
|
peer = Fluence.getPeer();
|
||||||
|
relay = args[0];
|
||||||
|
node_id = args[1];
|
||||||
|
viaOpt = args[2];
|
||||||
|
config = args[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
let request: RequestFlow;
|
||||||
|
const promise = new Promise<{ external_addresses: string[] }>((resolve, reject) => {
|
||||||
|
const r = new RequestFlowBuilder()
|
||||||
|
.disableInjections()
|
||||||
|
.withRawScript(
|
||||||
|
`
|
||||||
|
(xor
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "relay") [] relay)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("getDataSrv" "viaOpt") [] viaOpt)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(fold viaOpt -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call node_id ("peer" "identify") [] p)
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(seq
|
||||||
|
(fold viaOpt -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(fold viaOpt -via-peer-
|
||||||
|
(seq
|
||||||
|
(call -via-peer- ("op" "noop") [])
|
||||||
|
(next -via-peer-)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call -relay- ("op" "noop") [])
|
||||||
|
)
|
||||||
|
(xor
|
||||||
|
(call %init_peer_id% ("callbackSrv" "response") [p])
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.configHandler((h) => {
|
||||||
|
h.on('getDataSrv', '-relay-', () => {
|
||||||
|
return peer.getStatus().relayPeerId;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'relay', () => {
|
||||||
|
return relay;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'node_id', () => {
|
||||||
|
return node_id;
|
||||||
|
});
|
||||||
|
h.on('getDataSrv', 'viaOpt', () => {
|
||||||
|
return viaOpt === null ? [] : [viaOpt];
|
||||||
|
});
|
||||||
|
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 viaOpt');
|
||||||
|
});
|
||||||
|
|
||||||
|
if (config && config.ttl) {
|
||||||
|
r.withTTL(config.ttl);
|
||||||
|
}
|
||||||
|
|
||||||
|
request = r.build();
|
||||||
|
});
|
||||||
|
peer.internals.initiateFlow(request!);
|
||||||
|
return promise;
|
||||||
|
}
|
4
src/config.ts
Normal file
4
src/config.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import { krasnodar, testNet } from '@fluencelabs/fluence-network-environment';
|
||||||
|
|
||||||
|
// export const relays = krasnodar;
|
||||||
|
export const relays = testNet;
|
@ -1,7 +1,6 @@
|
|||||||
import { Fluence } from '@fluencelabs/fluence';
|
import { Fluence } from '@fluencelabs/fluence';
|
||||||
import { krasnodar } from '@fluencelabs/fluence-network-environment';
|
|
||||||
import { viaArr, viaOpt, viaStream, registerCustomId } from '../compiled/examples/via';
|
import { viaArr, viaOpt, viaStream, registerCustomId } from '../compiled/examples/via';
|
||||||
import {relays} from "../run-examples";
|
import { relays } from '../config';
|
||||||
|
|
||||||
export async function viaCall(): Promise<string[][]> {
|
export async function viaCall(): Promise<string[][]> {
|
||||||
const relayPeerId = Fluence.getPeer().getStatus().relayPeerId;
|
const relayPeerId = Fluence.getPeer().getStatus().relayPeerId;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
||||||
import { krasnodar } from '@fluencelabs/fluence-network-environment';
|
import { krasnodar } from '@fluencelabs/fluence-network-environment';
|
||||||
import { helloWorld, registerStringExtra } from './compiled/examples/helloWorld';
|
import { helloWorld, registerStringExtra } from './compiled/examples/helloWorld';
|
||||||
|
import { runExamples } from './run-examples';
|
||||||
|
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
// each compiled aqua function require a connected client
|
// each compiled aqua function require a connected client
|
||||||
@ -23,6 +24,9 @@ const main = async () => {
|
|||||||
const result = await helloWorld('NAME');
|
const result = await helloWorld('NAME');
|
||||||
console.log(result);
|
console.log(result);
|
||||||
|
|
||||||
|
// uncomment to play with examples
|
||||||
|
// await runExamples()
|
||||||
|
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -31,36 +31,13 @@ import { literalCall } from './examples/returnLiteralCall';
|
|||||||
import { multiReturnCall } from './examples/multiReturnCall';
|
import { multiReturnCall } from './examples/multiReturnCall';
|
||||||
import { declareCall } from './examples/declareCall';
|
import { declareCall } from './examples/declareCall';
|
||||||
import { genOptions } from './examples/optionsCall';
|
import { genOptions } from './examples/optionsCall';
|
||||||
let deepEqual = require('deep-equal');
|
import { relays } from './config';
|
||||||
|
|
||||||
function checkCall(name: string, actual: any, expected: any, callBackOnError: () => void) {
|
export const runExamples = async () => {
|
||||||
console.log('-- checking ' + name + ' --');
|
// stop the currently running peer to start with the clean state
|
||||||
if (!deepEqual(expected, actual)) {
|
await Fluence.stop();
|
||||||
console.error(`${name} call has the wrong result`);
|
|
||||||
console.error('actual: ');
|
|
||||||
console.dir(actual);
|
|
||||||
console.error('expected: ');
|
|
||||||
console.dir(expected);
|
|
||||||
callBackOnError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkCallBy(name: string, actual: any, by: (res: any) => boolean, callBackOnError: () => void) {
|
|
||||||
console.log('-- checking ' + name + ' --');
|
|
||||||
if (!by(actual)) {
|
|
||||||
console.error(`${name} call has the wrong result`);
|
|
||||||
console.error('actual: ' + actual);
|
|
||||||
callBackOnError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const relays = testNet
|
|
||||||
|
|
||||||
const main = async () => {
|
|
||||||
// setLogLevel("trace")
|
|
||||||
|
|
||||||
await Fluence.start({ connectTo: relays[0] });
|
await Fluence.start({ connectTo: relays[0] });
|
||||||
const selfPeerId = Fluence.getStatus().peerId;
|
|
||||||
|
|
||||||
const peer2 = new FluencePeer();
|
const peer2 = new FluencePeer();
|
||||||
await peer2.start({ connectTo: relays[1] });
|
await peer2.start({ connectTo: relays[1] });
|
||||||
@ -77,168 +54,97 @@ const main = async () => {
|
|||||||
// these calls return void, so they could be executed at any time,
|
// these calls return void, so they could be executed at any time,
|
||||||
// because promise waits only a fact that particle was sent
|
// because promise waits only a fact that particle was sent
|
||||||
|
|
||||||
let success = true;
|
|
||||||
let cb: () => void = () => {
|
|
||||||
success = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
// callArrow.aqua
|
// callArrow.aqua
|
||||||
let callArrowResult = await callArrowCall();
|
let callArrowResult = await callArrowCall();
|
||||||
checkCall('callArrow', callArrowResult, 'Hello, callArrow call!', cb);
|
|
||||||
|
|
||||||
// fold.aqua
|
// fold.aqua
|
||||||
let foldCallResult = await foldCall();
|
let foldCallResult = await foldCall();
|
||||||
checkCall('foldCall', foldCallResult, ['/ip4/164.90.171.139/tcp/7770', '/ip4/164.90.171.139/tcp/9990/ws'], cb);
|
|
||||||
|
|
||||||
//if.aqua
|
//if.aqua
|
||||||
await ifCall();
|
await ifCall();
|
||||||
|
|
||||||
// par.aqua
|
// par.aqua
|
||||||
let parCallResult = await parCall();
|
let parCallResult = await parCall();
|
||||||
checkCall('parArrow', parCallResult, 'hello', cb);
|
|
||||||
|
|
||||||
// these calls waiting for a result, so it will be called sequentially
|
// these calls waiting for a result, so it will be called sequentially
|
||||||
|
|
||||||
// helloWorld.aqua
|
// helloWorld.aqua
|
||||||
let helloWorldResult = await helloWorldCall();
|
let helloWorldResult = await helloWorldCall();
|
||||||
checkCall('helloWorldCall', helloWorldResult, 'Hello, NAME!', cb);
|
|
||||||
|
|
||||||
// func.aqua
|
// func.aqua
|
||||||
let funcCallResult = await funcCall();
|
let funcCallResult = await funcCall();
|
||||||
checkCall('funcCall', funcCallResult, 'some str', cb);
|
|
||||||
|
|
||||||
// on.aqua
|
// on.aqua
|
||||||
let onCallResult = await onCall();
|
let onCallResult = await onCall();
|
||||||
checkCall('onCall', onCallResult, ['/ip4/164.90.171.139/tcp/7770', '/ip4/164.90.171.139/tcp/9990/ws'], cb);
|
|
||||||
|
|
||||||
// dataAlias.aqua
|
// dataAlias.aqua
|
||||||
let dataAliasResult = await dataAliasCall();
|
let dataAliasResult = await dataAliasCall();
|
||||||
checkCall('dataAliasCall', dataAliasResult, 'peer id str', cb);
|
|
||||||
|
|
||||||
// complex.aqua
|
// complex.aqua
|
||||||
let complexCallResult = await complexCall();
|
let complexCallResult = await complexCall();
|
||||||
checkCall(
|
|
||||||
'complexCall',
|
|
||||||
complexCallResult,
|
|
||||||
['some str', '3', '1', '4', '1', '1', '3', '2', '4', '2', '2', selfPeerId],
|
|
||||||
cb,
|
|
||||||
);
|
|
||||||
|
|
||||||
// constants.aqua
|
// constants.aqua
|
||||||
let constantCallResult = await constantsCall();
|
let constantCallResult = await constantsCall();
|
||||||
checkCall('constantCall', constantCallResult, ['1', 'ab'], cb);
|
|
||||||
|
|
||||||
// stream.aqua
|
// stream.aqua
|
||||||
let streamResult = await streamCall();
|
let streamResult = await streamCall();
|
||||||
checkCall('streamCall', streamResult, ['first updated', 'second updated', 'third updated', 'fourth updated'], cb);
|
|
||||||
let returnNilResult = await returnNilCall();
|
let returnNilResult = await returnNilCall();
|
||||||
checkCall('returnNilCall', returnNilResult, [], cb);
|
|
||||||
let returnNoneResult = await returnNoneCall();
|
let returnNoneResult = await returnNoneCall();
|
||||||
checkCall('returnNoneCall', returnNoneResult, null, cb);
|
|
||||||
|
|
||||||
// topology.aqua
|
// topology.aqua
|
||||||
let topologyResult = await topologyCall(peer2);
|
let topologyResult = await topologyCall(peer2);
|
||||||
checkCall('topologyCall', topologyResult, 'finish', cb);
|
|
||||||
|
|
||||||
// foldJoin.aqua
|
// foldJoin.aqua
|
||||||
let foldJoinResult = await foldJoinCall();
|
let foldJoinResult = await foldJoinCall();
|
||||||
checkCallBy('foldJoinCall', foldJoinResult, (res) => res.length == 3, cb);
|
|
||||||
|
|
||||||
// option.aqua
|
// option.aqua
|
||||||
registerHandlers();
|
registerHandlers();
|
||||||
let optionResult = await useOptionalCall();
|
let optionResult = await useOptionalCall();
|
||||||
let optionalResult = await returnOptionalCall();
|
let optionalResult = await returnOptionalCall();
|
||||||
let noneResult = await returnNull();
|
let noneResult = await returnNull();
|
||||||
checkCall('useOptional', optionResult, 'hello', cb);
|
|
||||||
checkCall('returnOptional', optionalResult, 'optional', cb);
|
|
||||||
checkCall('returnNone', noneResult, null, cb);
|
|
||||||
|
|
||||||
// via.aqua
|
// via.aqua
|
||||||
let viaResult = await viaCall();
|
let viaResult = await viaCall();
|
||||||
checkCallBy('via', viaResult, (res) => res.every((val, i, arr) => deepEqual(val, arr[0])), cb);
|
|
||||||
|
|
||||||
// nestedFuncs.aqua
|
// nestedFuncs.aqua
|
||||||
let nestedFuncsResult = await nestedFuncsCall();
|
let nestedFuncsResult = await nestedFuncsCall();
|
||||||
checkCall('nestedFuncsCall', nestedFuncsResult, 'some-str', cb);
|
|
||||||
|
|
||||||
// assignment.aqua
|
// assignment.aqua
|
||||||
let assignmentResult = await assignmentCall();
|
let assignmentResult = await assignmentCall();
|
||||||
checkCall('assignmentCall', assignmentResult, ['abc', 'hello'], cb);
|
|
||||||
|
|
||||||
// tryOtherwise.aqua
|
// tryOtherwise.aqua
|
||||||
let tryOtherwiseResult = await tryOtherwiseCall();
|
let tryOtherwiseResult = await tryOtherwiseCall();
|
||||||
checkCall('tryOtherwiseCall', tryOtherwiseResult, 'error', cb);
|
|
||||||
|
|
||||||
// tryCatch.aqua
|
// tryCatch.aqua
|
||||||
let tryCatchResult = await tryCatchCall();
|
let tryCatchResult = await tryCatchCall();
|
||||||
checkCallBy(
|
|
||||||
'tryCatchCall',
|
|
||||||
tryCatchResult,
|
|
||||||
(res) => {
|
|
||||||
return (
|
|
||||||
(res[0] as string).includes("Error: Service with id 'unex' not found") &&
|
|
||||||
res[1] === '/ip4/164.90.171.139/tcp/7770'
|
|
||||||
);
|
|
||||||
},
|
|
||||||
cb,
|
|
||||||
);
|
|
||||||
|
|
||||||
// coCall.aqua
|
// coCall.aqua
|
||||||
let coCallResult = await coCall();
|
let coCallResult = await coCall();
|
||||||
checkCall('coCall', coCallResult, ['/ip4/164.90.171.139/tcp/7770', '/ip4/164.90.171.139/tcp/9990/ws'], cb);
|
|
||||||
|
|
||||||
// passArgsCall.aqua
|
// passArgsCall.aqua
|
||||||
let passArgsResult = await passArgsCall();
|
let passArgsResult = await passArgsCall();
|
||||||
checkCall('passArgsCall', passArgsResult, 'client-utilsid', cb);
|
|
||||||
|
|
||||||
// streamArgs.aqua
|
// streamArgs.aqua
|
||||||
let streamArgsResult = await streamArgsCall();
|
let streamArgsResult = await streamArgsCall();
|
||||||
checkCall('streamArgsCall', streamArgsResult, [['peer_id', 'peer_id']], cb);
|
|
||||||
|
|
||||||
// streamResults.aqua
|
// streamResults.aqua
|
||||||
let streamResultsResult = await streamResultsCall();
|
let streamResultsResult = await streamResultsCall();
|
||||||
checkCall('streamResultsCall', streamResultsResult, ['new_name', 'new_name', 'new_name'], cb);
|
|
||||||
|
|
||||||
// pushToStream.aqua
|
// pushToStream.aqua
|
||||||
let pushToStreamResult = await pushToStreamCall();
|
let pushToStreamResult = await pushToStreamCall();
|
||||||
checkCall('pushToStreamCall', pushToStreamResult, ['hello', 'get_string'], cb);
|
|
||||||
|
|
||||||
// literalCall.aqua
|
// literalCall.aqua
|
||||||
let literalCallResult = await literalCall();
|
let literalCallResult = await literalCall();
|
||||||
checkCall('literalCall', literalCallResult, 'some literal', cb);
|
|
||||||
|
|
||||||
// multiReturn.aqua
|
// multiReturn.aqua
|
||||||
let multiReturnResult = await multiReturnCall();
|
let multiReturnResult = await multiReturnCall();
|
||||||
checkCall(
|
|
||||||
'multiReturnResult',
|
|
||||||
multiReturnResult,
|
|
||||||
[['some-str', 'random-str', 'some-str'], 5, 'some-str', [1, 2], null, 10],
|
|
||||||
cb,
|
|
||||||
);
|
|
||||||
|
|
||||||
// declare.aqua
|
// declare.aqua
|
||||||
let declareResult = await declareCall();
|
let declareResult = await declareCall();
|
||||||
checkCall(
|
|
||||||
'declareResult',
|
|
||||||
declareResult,
|
|
||||||
'small_foodeclare all barsmall_fooexport_constdeclare_constdeclare_const2',
|
|
||||||
cb,
|
|
||||||
);
|
|
||||||
|
|
||||||
// option_gen.aqua
|
// option_gen.aqua
|
||||||
let optionGenResult = await genOptions();
|
let optionGenResult = await genOptions();
|
||||||
checkCall('optionGenResult', optionGenResult, ['none', 'some'], cb);
|
|
||||||
|
|
||||||
await Fluence.stop();
|
await Fluence.stop();
|
||||||
await peer2.stop();
|
await peer2.stop();
|
||||||
|
|
||||||
if (success) {
|
|
||||||
process.exit(0);
|
|
||||||
} else {
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
main().catch((err) => {
|
|
||||||
console.log(err);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
|
Reference in New Issue
Block a user