mirror of
https://github.com/fluencelabs/aqua-playground
synced 2025-05-12 03:47:13 +00:00
187 lines
5.2 KiB
TypeScript
187 lines
5.2 KiB
TypeScript
/**
|
|
*
|
|
* 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.0-SNAPSHOT
|
|
*
|
|
*/
|
|
import { FluencePeer } from '@fluencelabs/fluence';
|
|
import {
|
|
ResultCodes,
|
|
RequestFlow,
|
|
RequestFlowBuilder,
|
|
CallParams,
|
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
|
|
|
// 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) {
|
|
let peer: FluencePeer;
|
|
let serviceId;
|
|
let service;
|
|
if (args[0] instanceof FluencePeer) {
|
|
peer = args[0];
|
|
} else {
|
|
peer = FluencePeer.default;
|
|
}
|
|
|
|
if (typeof args[0] === 'string') {
|
|
serviceId = args[0];
|
|
} else if (typeof args[1] === 'string') {
|
|
serviceId = args[1];
|
|
} else {
|
|
serviceId = 'coservice-id';
|
|
}
|
|
|
|
if (!(args[0] instanceof FluencePeer) && typeof args[0] === 'object') {
|
|
service = args[0];
|
|
} else if (typeof args[1] === 'object') {
|
|
service = args[1];
|
|
} else {
|
|
service = args[2];
|
|
}
|
|
|
|
peer.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) {
|
|
let peer: FluencePeer;
|
|
let node;
|
|
let c;
|
|
let config;
|
|
if (args[0] instanceof FluencePeer) {
|
|
peer = args[0];
|
|
node = args[1];
|
|
c = args[2];
|
|
config = args[3];
|
|
} else {
|
|
peer = FluencePeer.default;
|
|
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.connectionInfo.connectedRelay;
|
|
});
|
|
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()]);
|
|
}
|