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 ConcatSubsDef {
|
2021-10-19 13:25:29 +03:00
|
|
|
get_some: (s: string, sr: { one: string; two: number; }, callParams: CallParams<'s' | 'sr'>) => { one: string; two: number; };
|
2021-10-07 13:55:10 +03:00
|
|
|
}
|
|
|
|
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;
|
2021-10-19 13:25:29 +03:00
|
|
|
|
|
|
|
|
2021-10-07 13:55:10 +03:00
|
|
|
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 {
|
2021-10-19 13:25:29 +03:00
|
|
|
serviceId = "concat_subs"
|
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, ['get_some']);
|
|
|
|
if (!!incorrectServiceDefinitions.length) {
|
2021-10-19 13:25:29 +03:00
|
|
|
throw new Error("Error registering service ConcatSubs: 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 === 'get_some') {
|
|
|
|
const callParams = {
|
|
|
|
...req.particleContext,
|
|
|
|
tetraplets: {
|
2021-10-19 13:25:29 +03:00
|
|
|
s: req.tetraplets[0],sr: 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.get_some(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
|
|
|
// Functions
|
2021-10-19 13:25:29 +03:00
|
|
|
|
|
|
|
export type SubImportUsageResult = { one: string; two: number; }
|
|
|
|
export function subImportUsage(s: string, config?: {ttl?: number}): Promise<SubImportUsageResult>;
|
|
|
|
export function subImportUsage(peer: FluencePeer, s: string, config?: {ttl?: number}): Promise<SubImportUsageResult>;
|
2021-10-07 13:55:10 +03:00
|
|
|
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;
|
2021-10-19 13:25:29 +03:00
|
|
|
const promise = new Promise<SubImportUsageResult>((resolve, reject) => {
|
2021-10-07 13:55:10 +03:00
|
|
|
const r = new RequestFlowBuilder()
|
2021-10-19 13:25:29 +03:00
|
|
|
.disableInjections()
|
|
|
|
.withRawScript(`
|
2021-10-07 13:55:10 +03:00
|
|
|
(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])
|
|
|
|
)
|
|
|
|
`,
|
2021-10-19 13:25:29 +03:00
|
|
|
)
|
|
|
|
.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');
|
|
|
|
})
|
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;
|
|
|
|
}
|