mirror of
https://github.com/fluencelabs/examples
synced 2025-05-18 21:51:19 +00:00
* init project * update modules * update dirs * update http call * update aqua * add create func * update aqua
280 lines
7.8 KiB
TypeScript
280 lines
7.8 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-226
|
|
*
|
|
*/
|
|
import { Fluence, FluencePeer } from '@fluencelabs/fluence';
|
|
import {
|
|
ResultCodes,
|
|
RequestFlow,
|
|
RequestFlowBuilder,
|
|
CallParams,
|
|
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1';
|
|
|
|
|
|
// Services
|
|
|
|
export interface CeramicAdapterDef {
|
|
ceramic_request: (args: string[], callParams: CallParams<'args'>) => {ret_code:number;stderr:string;stdout:string};
|
|
create_schema: (schema: string, callParams: CallParams<'schema'>) => {ret_code:number;stderr:string;stdout:string};
|
|
create_stream: (payload: string, callParams: CallParams<'payload'>) => {ret_code:number;stderr:string;stdout:string};
|
|
show: (stream_id: string, callParams: CallParams<'stream_id'>) => {ret_code:number;stderr:string;stdout:string};
|
|
state: (stream_id: string, callParams: CallParams<'stream_id'>) => {ret_code:number;stderr:string;stdout:string};
|
|
update: (stream_id: string, payload: string, callParams: CallParams<'stream_id' | 'payload'>) => {ret_code:number;stderr:string;stdout:string};
|
|
}
|
|
|
|
export function registerCeramicAdapter(service: CeramicAdapterDef): void;
|
|
export function registerCeramicAdapter(serviceId: string, service: CeramicAdapterDef): void;
|
|
export function registerCeramicAdapter(peer: FluencePeer, service: CeramicAdapterDef): void;
|
|
export function registerCeramicAdapter(peer: FluencePeer, serviceId: string, service: CeramicAdapterDef): void;
|
|
export function registerCeramicAdapter(...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];
|
|
}
|
|
|
|
peer.internals.callServiceHandler.use((req, resp, next) => {
|
|
if (req.serviceId !== serviceId) {
|
|
next();
|
|
return;
|
|
}
|
|
|
|
|
|
if (req.fnName === 'ceramic_request') {
|
|
|
|
const callParams = {
|
|
...req.particleContext,
|
|
tetraplets: {
|
|
args: req.tetraplets[0]
|
|
},
|
|
};
|
|
resp.retCode = ResultCodes.success;
|
|
resp.result = service.ceramic_request(req.args[0], callParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.fnName === 'create_schema') {
|
|
|
|
const callParams = {
|
|
...req.particleContext,
|
|
tetraplets: {
|
|
schema: req.tetraplets[0]
|
|
},
|
|
};
|
|
resp.retCode = ResultCodes.success;
|
|
resp.result = service.create_schema(req.args[0], callParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.fnName === 'create_stream') {
|
|
|
|
const callParams = {
|
|
...req.particleContext,
|
|
tetraplets: {
|
|
payload: req.tetraplets[0]
|
|
},
|
|
};
|
|
resp.retCode = ResultCodes.success;
|
|
resp.result = service.create_stream(req.args[0], callParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.fnName === 'show') {
|
|
|
|
const callParams = {
|
|
...req.particleContext,
|
|
tetraplets: {
|
|
stream_id: req.tetraplets[0]
|
|
},
|
|
};
|
|
resp.retCode = ResultCodes.success;
|
|
resp.result = service.show(req.args[0], callParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.fnName === 'state') {
|
|
|
|
const callParams = {
|
|
...req.particleContext,
|
|
tetraplets: {
|
|
stream_id: req.tetraplets[0]
|
|
},
|
|
};
|
|
resp.retCode = ResultCodes.success;
|
|
resp.result = service.state(req.args[0], callParams)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.fnName === 'update') {
|
|
|
|
const callParams = {
|
|
...req.particleContext,
|
|
tetraplets: {
|
|
stream_id: req.tetraplets[0],payload: req.tetraplets[1]
|
|
},
|
|
};
|
|
resp.retCode = ResultCodes.success;
|
|
resp.result = service.update(req.args[0], req.args[1], callParams)
|
|
|
|
}
|
|
|
|
|
|
next();
|
|
});
|
|
}
|
|
|
|
|
|
// Functions
|
|
|
|
export function roundtrip(payload: string, payload_two: string, node: string, service_id: string, config?: {ttl?: number}) : Promise<[string, string]>;
|
|
export function roundtrip(peer: FluencePeer, payload: string, payload_two: string, node: string, service_id: string, config?: {ttl?: number}) : Promise<[string, string]>;
|
|
export function roundtrip(...args: any) {
|
|
let peer: FluencePeer;
|
|
let payload: any;
|
|
let payload_two: any;
|
|
let node: any;
|
|
let service_id: any;
|
|
let config: any;
|
|
if (FluencePeer.isInstance(args[0])) {
|
|
peer = args[0];
|
|
payload = args[1];
|
|
payload_two = args[2];
|
|
node = args[3];
|
|
service_id = args[4];
|
|
config = args[5];
|
|
} else {
|
|
peer = Fluence.getPeer();
|
|
payload = args[0];
|
|
payload_two = args[1];
|
|
node = args[2];
|
|
service_id = args[3];
|
|
config = args[4];
|
|
}
|
|
|
|
let request: RequestFlow;
|
|
const promise = new Promise<[string, 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" "payload") [] payload)
|
|
)
|
|
(call %init_peer_id% ("getDataSrv" "payload_two") [] payload_two)
|
|
)
|
|
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
|
)
|
|
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
|
|
)
|
|
(call -relay- ("op" "noop") [])
|
|
)
|
|
(xor
|
|
(seq
|
|
(seq
|
|
(seq
|
|
(call -relay- ("op" "noop") [])
|
|
(call node (service_id "create_stream") [payload] create_res)
|
|
)
|
|
(call node (service_id "show") [create_res.$.stdout!] show_res)
|
|
)
|
|
(call node (service_id "update") [create_res.$.stdout! payload_two] update_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") [create_res.$.stdout! update_res.$.stdout!])
|
|
(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', 'payload', () => {return payload;});
|
|
h.on('getDataSrv', 'payload_two', () => {return payload_two;});
|
|
h.on('getDataSrv', 'node', () => {return node;});
|
|
h.on('getDataSrv', 'service_id', () => {return service_id;});
|
|
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 roundtrip');
|
|
})
|
|
if(config && config.ttl) {
|
|
r.withTTL(config.ttl)
|
|
}
|
|
request = r.build();
|
|
});
|
|
peer.internals.initiateFlow(request!);
|
|
return promise;
|
|
}
|
|
|