2021-10-07 13:55:10 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 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
|
2021-10-19 13:25:29 +03:00
|
|
|
* Aqua version: 0.3.2-SNAPSHOT
|
2021-10-07 13:55:10 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
|
|
|
import {
|
|
|
|
ResultCodes,
|
|
|
|
RequestFlow,
|
|
|
|
RequestFlowBuilder,
|
2021-10-19 13:25:29 +03:00
|
|
|
CallParams
|
2021-10-07 13:55:10 +03:00
|
|
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
|
|
|
|
2021-10-19 13:25:29 +03:00
|
|
|
|
2021-10-07 13:55:10 +03:00
|
|
|
function missingFields(obj: any, fields: string[]): string[] {
|
2021-10-19 13:25:29 +03:00
|
|
|
return fields.filter(f => !(f in obj))
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Services
|
|
|
|
|
|
|
|
export interface ComplexServiceDef {
|
2021-10-19 13:25:29 +03:00
|
|
|
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; };
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
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;
|
2021-10-19 13:25:29 +03:00
|
|
|
|
|
|
|
|
2021-10-07 13:55:10 +03:00
|
|
|
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 {
|
2021-10-19 13:25:29 +03:00
|
|
|
serviceId = "op-ha"
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-10-19 13:25:29 +03:00
|
|
|
if (!(FluencePeer.isInstance(args[0])) && typeof args[0] === 'object') {
|
2021-10-07 13:55:10 +03:00
|
|
|
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) {
|
2021-10-19 13:25:29 +03:00
|
|
|
throw new Error("Error registering service ComplexService: missing functions: " + incorrectServiceDefinitions.map((d) => "'" + d + "'").join(", "))
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
|
|
|
if (req.serviceId !== serviceId) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.fnName === 'call') {
|
|
|
|
const callParams = {
|
|
|
|
...req.particleContext,
|
|
|
|
tetraplets: {
|
2021-10-19 13:25:29 +03:00
|
|
|
d: req.tetraplets[0],sd: req.tetraplets[1]
|
2021-10-07 13:55:10 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
resp.retCode = ResultCodes.success;
|
2021-10-19 13:25:29 +03:00
|
|
|
resp.result = service.call(req.args[0], req.args[1], callParams)
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:25:29 +03:00
|
|
|
if (req.fnName === 'identity') {
|
2021-10-07 13:55:10 +03:00
|
|
|
const callParams = {
|
|
|
|
...req.particleContext,
|
2021-10-19 13:25:29 +03:00
|
|
|
tetraplets: {
|
|
|
|
|
|
|
|
},
|
2021-10-07 13:55:10 +03:00
|
|
|
};
|
|
|
|
resp.retCode = ResultCodes.success;
|
2021-10-19 13:25:29 +03:00
|
|
|
resp.result = service.identity(callParams)
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2021-10-19 13:25:29 +03:00
|
|
|
|
2021-10-07 13:55:10 +03:00
|
|
|
// Functions
|
2021-10-19 13:25:29 +03:00
|
|
|
export type DoSmthArgD = { otherValue: number; value: string; }
|
|
|
|
export type DoSmthArgD2 = { otherValue: number; value: string; }
|
|
|
|
export type DoSmthArgSd = { complex: { someNum: number; someStr: string; }; value: string; }
|
|
|
|
export type DoSmthResult = { complex: { otherValue: number; value: string; }; value: string; }
|
|
|
|
export function doSmth(d: DoSmthArgD, d2: DoSmthArgD2, sd: DoSmthArgSd, 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<DoSmthResult>;
|
|
|
|
export function doSmth(peer: FluencePeer, d: DoSmthArgD, d2: DoSmthArgD2, sd: DoSmthArgSd, 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<DoSmthResult>;
|
2021-10-07 13:55:10 +03:00
|
|
|
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;
|
2021-10-19 13:25:29 +03:00
|
|
|
const promise = new Promise<DoSmthResult>((resolve, reject) => {
|
|
|
|
const r = new RequestFlowBuilder()
|
2021-10-07 13:55:10 +03:00
|
|
|
.disableInjections()
|
2021-10-19 13:25:29 +03:00
|
|
|
.withRawScript(`
|
2021-10-07 13:55:10 +03:00
|
|
|
(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;
|
|
|
|
});
|
2021-10-19 13:25:29 +03:00
|
|
|
h.on('getDataSrv', 'd', () => {return d;});
|
|
|
|
h.on('getDataSrv', 'd2', () => {return d2;});
|
|
|
|
h.on('getDataSrv', 'sd', () => {return sd;});
|
2021-10-07 13:55:10 +03:00
|
|
|
h.use((req, resp, next) => {
|
2021-10-19 13:25:29 +03:00
|
|
|
if(req.serviceId === 'callbackSrv' && req.fnName === 'c') {
|
2021-10-07 13:55:10 +03:00
|
|
|
const callParams = {
|
|
|
|
...req.particleContext,
|
|
|
|
tetraplets: {
|
2021-10-19 13:25:29 +03:00
|
|
|
arg0: req.tetraplets[0],arg1: req.tetraplets[1]
|
2021-10-07 13:55:10 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
resp.retCode = ResultCodes.success;
|
2021-10-19 13:25:29 +03:00
|
|
|
resp.result = c(req.args[0], req.args[1], callParams)
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
2021-10-19 13:25:29 +03:00
|
|
|
|
2021-10-07 13:55:10 +03:00
|
|
|
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');
|
2021-10-19 13:25:29 +03:00
|
|
|
})
|
2021-10-07 13:55:10 +03:00
|
|
|
|
2021-10-19 13:25:29 +03:00
|
|
|
if (config && config.ttl) {
|
|
|
|
r.withTTL(config.ttl)
|
|
|
|
}
|
2021-10-07 13:55:10 +03:00
|
|
|
|
2021-10-19 13:25:29 +03:00
|
|
|
request = r.build();
|
|
|
|
});
|
2021-10-07 13:55:10 +03:00
|
|
|
peer.internals.initiateFlow(request!);
|
|
|
|
return promise;
|
|
|
|
}
|