181 lines
5.5 KiB
TypeScript
Raw Normal View History

2021-07-28 16:24:07 +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-08-31 12:19:36 +03:00
* Aqua version: 0.2.0-SNAPSHOT
2021-07-28 16:24:07 +03:00
*
*/
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
// Services
//Srv
//defaultId = "srv"
//add_alias: (alias: string, service_id: string) => void
//create: (blueprint_id: string) => string
//get_interface: (service_id: string) => {function_signatures:{arguments:string[][];name:string;output_types:string[]}[];record_types:{fields:string[][];id:number;name:string}[]}
//list: () => {blueprint_id:string;id:string;owner_id:string}[]
//remove: (service_id: string) => void
//resolve_alias: (alias: string) => string
//END Srv
//Peer
//defaultId = "peer"
//connect: (id: string, multiaddrs: string[] | null) => boolean
//get_contact: (peer: string) => {addresses:string[];peer_id:string}
//identify: () => {external_addresses:string[]}
//is_connected: (peer: string) => boolean
//timestamp_ms: () => number
//timestamp_sec: () => number
//END Peer
//Op
//defaultId = "op"
//array: (a: string, b: string | null, c: string | null, d: string | null) => string[]
//array_length: (array: string[]) => number
//bytes_from_b58: (b: string) => number[]
//bytes_to_b58: (bs: number[]) => string
//concat: (a: string[], b: string[] | null, c: string[] | null, d: string[] | null) => string[]
//concat_strings: (a: string, b: string) => string
//identity: (s: string | null) => string | null
//noop: () => void
//sha256_string: (s: string) => string
//string_from_b58: (b: string) => string
//string_to_b58: (s: string) => string
//END Op
//Kademlia
//defaultId = "kad"
//merge: (target: string, left: string[], right: string[], count: number | null) => string[]
//neighborhood: (key: string, already_hashed: boolean | null, count: number | null) => string[]
//END Kademlia
//Script
//defaultId = "script"
//add: (air_script: string, interval: string | null) => string
//list: () => {failures:number;id:string;interval:string;owner:string;src:string}
//remove: (script_id: string) => boolean
//END Script
//CoService
//defaultId = "coservice-id"
//call: () => string
//END CoService
//Dist
//defaultId = "dist"
//add_blueprint: (blueprint: {dependencies:string[];name:string}) => string
//add_module: (wasm_b56_content: number[], conf: {name:string}) => string
//add_module_from_vault: (path: string, config: {name:string}) => string
//default_module_config: (module_name: string) => {name:string}
//get_interface: (module_id: string) => {function_signatures:{arguments:string[][];name:string;output_types:string[]}[];record_types:{fields:string[][];id:number;name:string}[]}
//list_blueprints: () => {dependencies:string[];id:string;name:string}[]
//list_modules: () => {config:{name:string};hash:string;name:string}[]
//make_blueprint: (name: string, dependencies: string[]) => {dependencies:string[];name:string}
//make_module_config: (name: string, mem_pages_count: number | null, logger_enabled: boolean | null, preopened_files: string[] | null, envs: string[][] | null, mapped_dirs: string[][] | null, mounted_binaries: string[][] | null, logging_mask: number | null) => {name:string}
//END Dist
// Functions
2021-07-28 16:24:07 +03:00
export async function coFunc(client: FluenceClient, node: string, c: (arg0: {external_addresses:string[]}) => void, config?: {ttl?: number}): Promise<void> {
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 client.relayPeerId!;
});
h.on('getDataSrv', 'node', () => {return node;});
h.on('callbackSrv', 'c', (args) => {c(args[0]); return {};});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
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();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}