mirror of
https://github.com/fluencelabs/aqua-ipfs
synced 2025-04-24 23:42:14 +00:00
Add builtin on_start script (#8)
This commit is contained in:
parent
7a05a39b5a
commit
1eb0e5ce09
@ -4,12 +4,12 @@ import "ipfs.aqua"
|
||||
alias Multiaddr: string
|
||||
alias PeerId: string
|
||||
|
||||
func put(node: PeerId, path: string) -> IpfsResult:
|
||||
func put(node: PeerId, path: string) -> IpfsPutResult:
|
||||
on node:
|
||||
result <- Ipfs.put(path)
|
||||
<- result
|
||||
|
||||
func get_from(node: PeerId, hash: string, from: Multiaddr) -> IpfsResult:
|
||||
func get_from(node: PeerId, hash: string, from: Multiaddr) -> IpfsGetFromResult:
|
||||
on node:
|
||||
result <- Ipfs.get_from(hash, from)
|
||||
<- result
|
||||
|
4691
aqua/package-lock.json
generated
4691
aqua/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -65,7 +65,7 @@ async function main() {
|
||||
let getResult = await get_from(fluence, testNet[2].peerId, file.cid.toString(), ipfsMultiaddr, { ttl: 10000 });
|
||||
console.log("Ipfs.get", getResult);
|
||||
|
||||
let putResult = await put(fluence, testNet[2].peerId, getResult.result, { ttl: 10000 });
|
||||
let putResult = await put(fluence, testNet[2].peerId, getResult.path, { ttl: 10000 });
|
||||
console.log("Ipfs.put", putResult);
|
||||
|
||||
return;
|
||||
|
@ -1,158 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* 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.1.8-160
|
||||
*
|
||||
*/
|
||||
import { FluenceClient, PeerIdB58 } from '@fluencelabs/fluence';
|
||||
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable';
|
||||
import { RequestFlow } from '@fluencelabs/fluence/dist/internal/RequestFlow';
|
||||
|
||||
|
||||
|
||||
export async function put(client: FluenceClient, node: string, path: string, config?: {ttl?: number}): Promise<{result:string;success:boolean}> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{result:string;success:boolean}>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withTTL(config?.ttl || 5000)
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "path") [] path)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(call node ("ipfs-adapter" "put") [path] result)
|
||||
(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") [result])
|
||||
(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 client.relayPeerId!;
|
||||
});
|
||||
h.on('getDataSrv', 'node', () => {return node;});
|
||||
h.on('getDataSrv', 'path', () => {return path;});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for put');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export async function get_from(client: FluenceClient, node: string, hash: string, from: string, config?: {ttl?: number}): Promise<{result:string;success:boolean}> {
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<{result:string;success:boolean}>((resolve, reject) => {
|
||||
request = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withTTL(config?.ttl || 5000)
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(call %init_peer_id% ("getDataSrv" "node") [] node)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "hash") [] hash)
|
||||
)
|
||||
(call %init_peer_id% ("getDataSrv" "from") [] from)
|
||||
)
|
||||
(call -relay- ("op" "noop") [])
|
||||
)
|
||||
(xor
|
||||
(call node ("ipfs-adapter" "get_from") [hash from] result)
|
||||
(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") [result])
|
||||
(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 client.relayPeerId!;
|
||||
});
|
||||
h.on('getDataSrv', 'node', () => {return node;});
|
||||
h.on('getDataSrv', 'hash', () => {return hash;});
|
||||
h.on('getDataSrv', 'from', () => {return from;});
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
// assuming error is the single argument
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for get_from');
|
||||
})
|
||||
.build();
|
||||
});
|
||||
await client.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
19
service/artifacts/on_start.air
Normal file
19
service/artifacts/on_start.air
Normal file
@ -0,0 +1,19 @@
|
||||
(seq
|
||||
(seq
|
||||
(xor
|
||||
(call relay ("ipfs-adapter" "set_local_api_multiaddr") [local_api_multiaddr])
|
||||
(call %init_peer_id% ("op" "return") [%last_error%.$.instruction])
|
||||
)
|
||||
(xor
|
||||
(call relay ("ipfs-adapter" "set_external_api_multiaddr") [external_api_multiaddr])
|
||||
(call %init_peer_id% ("op" "return") [%last_error%.$.instruction])
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(seq
|
||||
(call relay ("ipfs-adapter" "set_external_swarm_multiaddr") [external_swarm_multiaddr])
|
||||
(call %init_peer_id% ("op" "return") ["ok"])
|
||||
)
|
||||
(call %init_peer_id% ("op" "return") [%last_error%.$.instruction])
|
||||
)
|
||||
)
|
5
service/artifacts/on_start.json
Normal file
5
service/artifacts/on_start.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"external_swarm_multiaddr": "$FLUENCE_ENV_IPFS_ADAPTER_EXTERNAL_SWARM_MULTIADDR",
|
||||
"local_api_multiaddr": "$FLUENCE_ENV_IPFS_ADAPTER_LOCAL_API_MULTIADDR",
|
||||
"external_api_multiaddr": "$FLUENCE_ENV_IPFS_ADAPTER_EXTERNAL_API_MULTIADDR"
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user