JS SDK 0.11, Aqua 0.3 (#53)

This commit is contained in:
folex 2021-09-13 12:32:36 +03:00 committed by GitHub
parent 04b00967f9
commit 899d049ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 4853 additions and 9968 deletions

5
.gitignore vendored
View File

@ -1,7 +1,12 @@
service/target
**/*.rs.bk
**/.idea
**/artifacts
**/.DS_Store
**/node_modules
**/dist
# Remove after https://github.com/fluencelabs/aqua/issues/287
aqua/target/typescript/**
example/src/generated/**

View File

@ -1,15 +1,17 @@
module Aqua.DHT.Scheduled declares *
import "dht.aqua"
import "@fluencelabs/aqua-lib/builtin.aqua"
-- clears expired records
func clearExpired_86400():
on host_peer_id:
on HOST_PEER_ID:
t <- Peer.timestamp_sec()
AquaDHT.clear_expired(t)
-- get all old records and replicate it by keys
func replicate_3600():
on host_peer_id:
on HOST_PEER_ID:
t <- Peer.timestamp_sec()
res <- AquaDHT.evict_stale(t)
for r <- res.results par:

View File

@ -1,3 +1,5 @@
module Aqua.DHT declares *
data ClearExpiredResult:
success: bool
error: string

37
aqua/package-lock.json generated
View File

@ -1,13 +1,42 @@
{
"name": "@fluencelabs/aqua-dht",
"version": "0.2.0",
"lockfileVersion": 1,
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@fluencelabs/aqua-dht",
"version": "0.2.0",
"license": "MIT",
"dependencies": {
"@fluencelabs/aqua-lib": "^0.1.14"
},
"devDependencies": {
"@fluencelabs/aqua": "0.3.0-222"
}
},
"node_modules/@fluencelabs/aqua": {
"version": "0.3.0-222",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.0-222.tgz",
"integrity": "sha512-dwBPNoE490LX9CRssNyz3nHKgpxwYvkRIhmTbjbFYXknzZWN18vx6WseD9tuzIFnlXdsJ/MoUhNZAxmT+VPhiw==",
"dev": true,
"bin": {
"aqua": "index.js",
"aqua-cli": "error.js",
"aqua-j": "index-java.js"
}
},
"node_modules/@fluencelabs/aqua-lib": {
"version": "0.1.14",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-lib/-/aqua-lib-0.1.14.tgz",
"integrity": "sha512-H2Q4gIvociUxc4J2mwmH0D+mrU2N2Z+enKCHgBCanMVEE2wZDsZ80GTbDKsQjEq+gpqbnJIk8lJBYW6lyvLJTg=="
}
},
"dependencies": {
"@fluencelabs/aqua": {
"version": "0.2.0-208",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.2.0-208.tgz",
"integrity": "sha512-jopKhD3oeMQ4tenzfGs/v+GOZw/2frxGHh4E1dYlXyQO8o/yu0UZlLuI14xSC1Vo3c7KXmRTXRtF0mzcqF2kew==",
"version": "0.3.0-222",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.0-222.tgz",
"integrity": "sha512-dwBPNoE490LX9CRssNyz3nHKgpxwYvkRIhmTbjbFYXknzZWN18vx6WseD9tuzIFnlXdsJ/MoUhNZAxmT+VPhiw==",
"dev": true
},
"@fluencelabs/aqua-lib": {

View File

@ -9,7 +9,7 @@
"@fluencelabs/aqua-lib": "^0.1.14"
},
"scripts": {
"compile-aqua": "aqua -i ./ -o ./target/typescript/src",
"compile-aqua": "aqua -i . -o ./target/typescript",
"generate-aqua": "../service/build.sh",
"build": "npm run compile-aqua"
},
@ -29,6 +29,6 @@
},
"homepage": "https://github.com/fluencelabs/aqua-dht",
"devDependencies": {
"@fluencelabs/aqua": "^0.2.0-208"
"@fluencelabs/aqua": "0.3.0-224"
}
}

View File

@ -1,10 +1,11 @@
module Aqua.DHT.PubSub declares *
import "dht.aqua"
import "@fluencelabs/aqua-lib/builtin.aqua"
-- Get peers closest to the topic's hash in Kademlia network
-- These peers are expected to store list of subscribers of this topic
func getNeighbours(node_id: PeerId, topic: string) -> []PeerId:
on node_id:
func getNeighbours(topic: string) -> []PeerId:
k <- Op.string_to_b58(topic)
nodes <- Kademlia.neighborhood(k, nil, nil)
<- nodes
@ -12,15 +13,15 @@ func getNeighbours(node_id: PeerId, topic: string) -> []PeerId:
-- If this peer have set node_id as a subscriber for topic,
-- this call will prevent subscriber from re-subscribing
-- so that eventually it will disappear from the subscribers list
func removeSubscriber(node_id: PeerId, topic: string):
on node_id:
func removeSubscriber(topic: string):
on HOST_PEER_ID:
t <- Peer.timestamp_sec()
AquaDHT.clear_host_value(topic, t)
-- Create a topic: register it on the closest peers
-- node_id is a peer with Kademlia access to start with
func initTopic(node_id: PeerId, topic: string):
nodes <- getNeighbours(node_id, topic)
func initTopic(topic: string):
on HOST_PEER_ID:
nodes <- getNeighbours(topic)
for n <- nodes par:
on n:
try:
@ -29,8 +30,9 @@ func initTopic(node_id: PeerId, topic: string):
-- Create a topic and subscribe to it
-- %init_peer_id% (current client) will become a subscriber
func initTopicAndSubscribe(node_id: PeerId, topic: string, value: string, relay_id: ?PeerId, service_id: ?string):
nodes <- getNeighbours(node_id, topic)
func initTopicAndSubscribe(topic: string, value: string, relay_id: ?PeerId, service_id: ?string):
on HOST_PEER_ID:
nodes <- getNeighbours(topic)
for n <- nodes par:
on n:
try:
@ -44,7 +46,7 @@ func initTopicAndSubscribeNode(subscriber_node_id: PeerId, topic: string, value:
t <- Peer.timestamp_sec()
AquaDHT.register_key(topic, t, false, 0)
r <- AquaDHT.put_host_value(topic, value, t, nil, service_id, 0)
nodes <- getNeighbours(subscriber_node_id, topic)
nodes <- getNeighbours(topic)
for n <- nodes par:
on n:
try:
@ -54,8 +56,9 @@ func initTopicAndSubscribeNode(subscriber_node_id: PeerId, topic: string, value:
-- Subscribe to a topic
-- Note: topic must be already initiated
func subscribe(node_id: PeerId, topic: string, value: string, relay_id: ?PeerId, service_id: ?string):
nodes <- getNeighbours(node_id, topic)
func subscribe(topic: string, value: string, relay_id: ?PeerId, service_id: ?string):
on HOST_PEER_ID:
nodes <- getNeighbours(topic)
for n <- nodes par:
on n:
try:
@ -68,7 +71,7 @@ func subscribeNode(subscriber_node_id: PeerId, topic: string, value: string, ser
on subscriber_node_id:
t <- Peer.timestamp_sec()
r <- AquaDHT.put_host_value(topic, value, t, nil, service_id, 0)
nodes <- getNeighbours(subscriber_node_id, topic)
nodes <- getNeighbours(topic)
for n <- nodes par:
on n:
try:
@ -77,22 +80,22 @@ func subscribeNode(subscriber_node_id: PeerId, topic: string, value: string, ser
AquaDHT.propagate_host_value(r, tt, 0)
-- Find the list of subscribers for the given topic
func findSubscribers(node_id: PeerId, topic: string) -> []Record:
nodes <- getNeighbours(node_id, topic)
func findSubscribers(topic: string) -> []Record:
on HOST_PEER_ID:
nodes <- getNeighbours(topic)
res: *GetValuesResult
for n <- nodes par:
on n:
try:
t <- Peer.timestamp_sec()
res <- AquaDHT.get_values(topic, t)
on node_id:
v <- AquaDHT.merge_two(res!.result, res!1.result)
<- v.result
-- Execute the given code on subscribers
-- Note that you can provide another Aqua function as an argument to this one
func executeOnSubscribers(node_id: PeerId, topic: string, call: Record -> ()):
subs <- findSubscribers(node_id, topic)
func executeOnSubscribers(topic: string, call: Record -> ()):
subs <- findSubscribers(topic)
for r <- subs par:
on r.peer_id via r.relay_id:
call(r)

View File

@ -1,7 +0,0 @@
# AquaDHT TypeScript API
This is a compiled version of `@fluencelabs/aqua-dht`.
Aqua API is defined in [Aqua scripts](/aqua/).
## Documentation
See [Aqua Book](https://fluence.dev/aqua-book/libraries/aqua-dht).

File diff suppressed because it is too large Load Diff

View File

@ -1,34 +0,0 @@
{
"name": "@fluencelabs/aqua-dht-ts",
"version": "0.1.0",
"description": "Aqua DHT library compiled to TS",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"files": [
"dist/*"
],
"dependencies": {
"@fluencelabs/fluence": "^0.10.0"
},
"scripts": {
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fluencelabs/aqua-dht.git",
"directory": "aqua/target/typescript"
},
"keywords": [
"aqua",
"fluence"
],
"author": "Fluence Labs",
"license": "MIT",
"bugs": {
"url": "https://github.com/fluencelabs/aqua-dht/issues"
},
"homepage": "https://github.com/fluencelabs/aqua-dht",
"devDependencies": {
"typescript": "^3.9.5"
}
}

View File

@ -1,137 +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.9-165
*
*/
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 clearExpired(client: FluenceClient, config?: {ttl?: number}): Promise<void> {
let request: RequestFlow;
const promise = new Promise<void>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("peer" "timestamp_sec") [] t)
)
(call %init_peer_id% ("aqua-dht" "clear_expired") [t])
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for clearExpired');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function replicate(client: FluenceClient, 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
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("peer" "timestamp_sec") [] t)
)
(call %init_peer_id% ("aqua-dht" "evict_stale") [t] res)
)
(fold res.$.results! r
(par
(seq
(seq
(call %init_peer_id% ("op" "string_to_b58") [r.$.key.key!] k)
(call %init_peer_id% ("kad" "neighborhood") [k $nil $nil] nodes)
)
(fold nodes n
(par
(seq
(call -relay- ("op" "noop") [])
(xor
(seq
(call n ("aqua-dht" "republish_key") [r.$.key! t])
(call n ("aqua-dht" "republish_values") [r.$.key.key! r.$.records! t])
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(next n)
)
)
)
(next r)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for replicate');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}

View File

@ -1,18 +0,0 @@
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './pubsub';
export * from './dht-scheduled-scripts';

View File

@ -1,894 +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.9-165
*
*/
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 removeSubscriber(client: FluenceClient, node_id: string, topic: string, 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
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("peer" "timestamp_sec") [] t)
(call node_id ("aqua-dht" "clear_host_value") [topic t])
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for removeSubscriber');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function subscribeNode(client: FluenceClient, subscriber_node_id: string, topic: string, value: string, service_id: string | null, 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
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "subscriber_node_id") [] subscriber_node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call %init_peer_id% ("getDataSrv" "value") [] value)
)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(seq
(call subscriber_node_id ("peer" "timestamp_sec") [] t)
(call subscriber_node_id ("aqua-dht" "put_host_value") [topic value t $nil service_id 0] r)
)
(xor
(seq
(call subscriber_node_id ("op" "string_to_b58") [topic] k)
(call subscriber_node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(xor
(seq
(seq
(call n ("peer" "timestamp_sec") [] tt)
(call n ("aqua-dht" "register_key") [topic tt false 0])
)
(call n ("aqua-dht" "propagate_host_value") [r tt 0])
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'subscriber_node_id', () => {return subscriber_node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.on('getDataSrv', 'value', () => {return value;});
h.on('getDataSrv', 'service_id', () => {return service_id === null ? [] : [service_id];});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for subscribeNode');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function executeOnSubscribers(client: FluenceClient, node_id: string, topic: string, call: (arg0: {peer_id:string;relay_id:string[];service_id:string[];set_by:string;timestamp_created:number;value:string;weight:number}) => 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
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(seq
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
(call -relay- ("op" "noop") [])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(seq
(xor
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("aqua-dht" "get_values") [topic t] $res)
)
(null)
)
(call node_id ("op" "noop") [])
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(xor
(call node_id ("aqua-dht" "merge_two") [$res.$.[0].result! $res.$.[1].result!] v)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold v.$.result! r
(par
(seq
(fold r.$.relay_id! -via-peer-
(seq
(call -via-peer- ("op" "noop") [])
(next -via-peer-)
)
)
(xor
(seq
(seq
(fold r.$.relay_id! -via-peer-
(seq
(call -via-peer- ("op" "noop") [])
(next -via-peer-)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(call %init_peer_id% ("callbackSrv" "call") [r])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
)
)
(seq
(call -relay- ("op" "noop") [])
(next r)
)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 5])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.on('callbackSrv', 'call', (args) => {call(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 executeOnSubscribers');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function subscribe(client: FluenceClient, node_id: string, topic: string, value: string, relay_id: string | null, service_id: string | null, 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
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call %init_peer_id% ("getDataSrv" "value") [] value)
)
(call %init_peer_id% ("getDataSrv" "relay_id") [] relay_id)
)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(xor
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("aqua-dht" "put_value") [topic value t relay_id service_id 0])
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.on('getDataSrv', 'value', () => {return value;});
h.on('getDataSrv', 'relay_id', () => {return relay_id === null ? [] : [relay_id];});
h.on('getDataSrv', 'service_id', () => {return service_id === null ? [] : [service_id];});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for subscribe');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function getNeighbours(client: FluenceClient, node_id: string, topic: string, config?: {ttl?: number}): Promise<string[]> {
let request: RequestFlow;
const promise = new Promise<string[]>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(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") [nodes])
(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_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
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 getNeighbours');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return promise;
}
export async function initTopicAndSubscribeNode(client: FluenceClient, subscriber_node_id: string, topic: string, value: string, service_id: string | null, 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
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "subscriber_node_id") [] subscriber_node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call %init_peer_id% ("getDataSrv" "value") [] value)
)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(seq
(seq
(call subscriber_node_id ("peer" "timestamp_sec") [] t)
(call subscriber_node_id ("aqua-dht" "register_key") [topic t false 0])
)
(call subscriber_node_id ("aqua-dht" "put_host_value") [topic value t $nil service_id 0] r)
)
(xor
(seq
(call subscriber_node_id ("op" "string_to_b58") [topic] k)
(call subscriber_node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(xor
(seq
(seq
(call n ("peer" "timestamp_sec") [] tt)
(call n ("aqua-dht" "register_key") [topic tt false 0])
)
(call n ("aqua-dht" "propagate_host_value") [r tt 0])
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'subscriber_node_id', () => {return subscriber_node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.on('getDataSrv', 'value', () => {return value;});
h.on('getDataSrv', 'service_id', () => {return service_id === null ? [] : [service_id];});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for initTopicAndSubscribeNode');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function initTopic(client: FluenceClient, node_id: string, topic: string, 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
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(xor
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("aqua-dht" "register_key") [topic t false 0])
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for initTopic');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}
export async function findSubscribers(client: FluenceClient, node_id: string, topic: string, config?: {ttl?: number}): Promise<{peer_id:string;relay_id:string[];service_id:string[];set_by:string;timestamp_created:number;value:string;weight:number}[]> {
let request: RequestFlow;
const promise = new Promise<{peer_id:string;relay_id:string[];service_id:string[];set_by:string;timestamp_created:number;value:string;weight:number}[]>((resolve, reject) => {
const r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(seq
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
(call -relay- ("op" "noop") [])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(seq
(xor
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("aqua-dht" "get_values") [topic t] $res)
)
(null)
)
(call node_id ("op" "noop") [])
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(xor
(call node_id ("aqua-dht" "merge_two") [$res.$.[0].result! $res.$.[1].result!] v)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [v.$.result!])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
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 findSubscribers');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return promise;
}
export async function initTopicAndSubscribe(client: FluenceClient, node_id: string, topic: string, value: string, relay_id: string | null, service_id: string | null, 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
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call %init_peer_id% ("getDataSrv" "value") [] value)
)
(call %init_peer_id% ("getDataSrv" "relay_id") [] relay_id)
)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k $nil $nil] nodes)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(xor
(seq
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("aqua-dht" "register_key") [topic t false 0])
)
(call n ("aqua-dht" "put_value") [topic value t relay_id service_id 0])
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId!;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.on('getDataSrv', 'value', () => {return value;});
h.on('getDataSrv', 'relay_id', () => {return relay_id === null ? [] : [relay_id];});
h.on('getDataSrv', 'service_id', () => {return service_id === null ? [] : [service_id];});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for initTopicAndSubscribe');
})
if(config && config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request!);
return Promise.race([promise, Promise.resolve()]);
}

View File

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

4730
example/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,8 @@
"description": "An example of how to use AquaDHT in TypeScript",
"main": "dist/example.js",
"scripts": {
"build": "tsc",
"compile-aqua": "aqua -i ./src/aqua -o ./src/generated",
"build": "npm run compile-aqua && tsc",
"prestart": "npm run build",
"start": "node dist/example.js"
},
@ -16,8 +17,10 @@
"author": "Fluence Labs",
"license": "MIT",
"dependencies": {
"@fluencelabs/aqua-dht-ts": "../../aqua/target/typescript",
"@fluencelabs/fluence": "^0.10.0",
"@fluencelabs/aqua-dht": "../aqua",
"@fluencelabs/aqua-lib": "0.1.14",
"@fluencelabs/aqua": "0.3.0-224",
"@fluencelabs/fluence": "^0.11.0",
"@fluencelabs/fluence-network-environment": "^1.0.10"
}
}

View File

@ -24,9 +24,10 @@ func call_subscriber(sub: Record, event: Event):
SubscriberAPI.receive_event(event)
-- send event to every subscriber
func send_everyone(relay: PeerId, topic: string, event: Event):
func send_everyone(topic: string, event: Event):
on HOST_PEER_ID:
-- retrieve all subscribers of a topic
subscribers <- findSubscribers(relay, topic)
subscribers <- findSubscribers(topic)
-- iterate through them
for sub <- subscribers par:
call_subscriber(sub, event)

View File

@ -1,5 +1,3 @@
import initTopicAndSubscribe, findSubscribers from "@fluencelabs/aqua-dht/pubsub.aqua"
export initTopicAndSubscribe, findSubscribers
alias A: string

View File

@ -1,16 +1,18 @@
import { initTopicAndSubscribe, findSubscribers } from "@fluencelabs/aqua-dht-ts";
import { createClient } from "@fluencelabs/fluence";
import { FluencePeer } from "@fluencelabs/fluence";
import { krasnodar } from "@fluencelabs/fluence-network-environment";
import { initTopicAndSubscribe, findSubscribers } from "./generated/export";
async function main() {
// connect to the Fluence network
const client = await createClient(krasnodar[1]);
const peer = FluencePeer.default;
await peer.init({ connectTo: krasnodar[1] });
let topic = "myTopic";
let value = "myValue";
// create topic (if not exists) and subscribe on it
await initTopicAndSubscribe(client, client.relayPeerId!, topic, value, client.relayPeerId!, null);
let relay = peer.connectionInfo.connectedRelay!;
await initTopicAndSubscribe(peer, topic, value, relay, null);
// find other peers subscribed to that topic
let subscribers = await findSubscribers(client, client.relayPeerId!, topic);
let subscribers = await findSubscribers(peer, topic);
console.log("found subscribers:", subscribers);
}

View File

@ -1 +0,0 @@
example.ts

View File

@ -1,26 +0,0 @@
{
"name": "aqua-example",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@fluencelabs/aqua": {
"version": "0.1.14-207",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.1.14-207.tgz",
"integrity": "sha512-Qz5082Pf8nYIitNN5RIGxn6ALsAQSXums5xWpB4eRI+FRtuVP+wvElTwn6PnpYvR61SUCkOcpBYPsABcfDaTCA=="
},
"@fluencelabs/aqua-dht": {
"version": "0.1.37",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-dht/-/aqua-dht-0.1.37.tgz",
"integrity": "sha512-q7SLC00NhddrR3i+oFnw64u6m+T01HTA5oVulI1hKW2ULT/ygRNV7GajeI9fKrNyHHh3Vg9+UDeWUFNUJU/0ow==",
"requires": {
"@fluencelabs/aqua-lib": "^0.1.10"
}
},
"@fluencelabs/aqua-lib": {
"version": "0.1.14",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-lib/-/aqua-lib-0.1.14.tgz",
"integrity": "sha512-H2Q4gIvociUxc4J2mwmH0D+mrU2N2Z+enKCHgBCanMVEE2wZDsZ80GTbDKsQjEq+gpqbnJIk8lJBYW6lyvLJTg=="
}
}
}

View File

@ -1,15 +0,0 @@
{
"name": "aqua-example",
"version": "1.0.0",
"description": "An example of how to use aqua-dht in Aqua",
"author": "Fluence Labs",
"license": "MIT",
"scripts": {
"compile-aqua": "aqua -i . -o .",
"build": "npm run compile-aqua"
},
"dependencies": {
"@fluencelabs/aqua-dht": "latest",
"@fluencelabs/aqua": "0.1.14-207"
}
}

View File

@ -1,22 +0,0 @@
import { initTopicAndSubscribe, findSubscribers } from "./export.js";
import { createClient } from "@fluencelabs/fluence";
import { krasnodar } from "@fluencelabs/fluence-network-environment";
async function main() {
// connect to the Fluence network
const client = await createClient(krasnodar[1]);
let topic = "myTopic";
let value = "myValue";
// create topic (if not exists) and subscribe on it
await initTopicAndSubscribe(client, client.relayPeerId, topic, value, client.relayPeerId, null);
// find other peers subscribed to that topic
let subscribers = await findSubscribers(client, client.relayPeerId, topic);
console.log("found subscribers:", subscribers);
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});

View File

@ -1,221 +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: Unknown (JS)
*
*/
import { RequestFlowBuilder } from '@fluencelabs/fluence/dist/api.unstable.js';
export async function initTopicAndSubscribe(client, node_id, topic, value, relay_id, service_id, config) {
let request;
config = config || {};
const promise = new Promise((resolve, reject) => {
var r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call %init_peer_id% ("getDataSrv" "value") [] value)
)
(call %init_peer_id% ("getDataSrv" "relay_id") [] relay_id)
)
(call %init_peer_id% ("getDataSrv" "service_id") [] service_id)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k [] []] nodes)
)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(xor
(seq
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("aqua-dht" "register_key") [topic t false 0])
)
(call n ("aqua-dht" "put_value") [topic value t relay_id service_id 0])
)
(null)
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
h.on('getDataSrv', 'value', () => {return value;});
h.on('getDataSrv', 'relay_id', () => {return relay_id === null ? [] : [relay_id];});
h.on('getDataSrv', 'service_id', () => {return service_id === null ? [] : [service_id];});
h.onEvent('errorHandlingSrv', 'error', (args) => {
// assuming error is the single argument
const [err] = args;
reject(err);
});
})
.handleScriptError(reject)
.handleTimeout(() => {
reject('Request timed out for initTopicAndSubscribe');
})
if(config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request);
return Promise.race([promise, Promise.resolve()]);
}
export async function findSubscribers(client, node_id, topic, config) {
let request;
config = config || {};
const promise = new Promise((resolve, reject) => {
var r = new RequestFlowBuilder()
.disableInjections()
.withRawScript(
`
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
(call %init_peer_id% ("getDataSrv" "node_id") [] node_id)
)
(call %init_peer_id% ("getDataSrv" "topic") [] topic)
)
(call -relay- ("op" "noop") [])
)
(xor
(seq
(call node_id ("op" "string_to_b58") [topic] k)
(call node_id ("kad" "neighborhood") [k [] []] nodes)
)
(seq
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
)
(call -relay- ("op" "noop") [])
)
)
)
(call -relay- ("op" "noop") [])
)
(fold nodes n
(par
(seq
(xor
(seq
(call n ("peer" "timestamp_sec") [] t)
(call n ("aqua-dht" "get_values") [topic t] $res)
)
(null)
)
(call node_id ("op" "noop") [])
)
(seq
(call -relay- ("op" "noop") [])
(next n)
)
)
)
)
(xor
(call node_id ("aqua-dht" "merge_two") [$res.$.[0].result! $res.$.[1].result!] v)
(seq
(call -relay- ("op" "noop") [])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
)
)
)
(call -relay- ("op" "noop") [])
)
(xor
(call %init_peer_id% ("callbackSrv" "response") [v.$.result!])
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
)
)
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 4])
)
`,
)
.configHandler((h) => {
h.on('getDataSrv', '-relay-', () => {
return client.relayPeerId;
});
h.on('getDataSrv', 'node_id', () => {return node_id;});
h.on('getDataSrv', 'topic', () => {return topic;});
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 findSubscribers');
})
if(config.ttl) {
r.withTTL(config.ttl)
}
request = r.build();
});
await client.initiateFlow(request);
return promise;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +0,0 @@
{
"name": "example",
"version": "1.0.0",
"description": "An example of how to use AquaDHT in TypeScript",
"main": "example.js",
"type": "module",
"scripts": {
"compile-aqua": "aqua-js --js -i . -o .",
"start": "node example.js"
},
"keywords": [
"aqua",
"dht",
"p2p"
],
"author": "Fluence Labs",
"license": "MIT",
"dependencies": {
"@fluencelabs/fluence": "^0.10.0",
"@fluencelabs/fluence-network-environment": "^1.0.10"
},
"devDependencies": {
"@fluencelabs/aqua-dht": "0.1.37",
"@fluencelabs/aqua": "0.2.1-219"
}
}

File diff suppressed because it is too large Load Diff