Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 0ff10a2

Browse files
authored
update aqua version (#33)
* Bump aquamarine version * Add connection options
1 parent f732a30 commit 0ff10a2

File tree

10 files changed

+101
-21
lines changed

10 files changed

+101
-21
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"author": "Fluence Labs",
1717
"license": "Apache-2.0",
1818
"dependencies": {
19-
"@fluencelabs/aquamarine-interpreter": "0.7.2",
19+
"@fluencelabs/aquamarine-interpreter": "0.7.9",
2020
"async": "3.2.0",
2121
"base64-js": "1.3.1",
2222
"bs58": "4.0.1",

src/FluenceClient.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PeerId, { isPeerId } from 'peer-id';
55
import { AquaCallHandler } from './internal/AquaHandler';
66
import { ClientImpl } from './internal/ClientImpl';
77
import { PeerIdB58 } from './internal/commonTypes';
8+
import { FluenceConnectionOptions } from './internal/FluenceConnection';
89
import { generatePeerId, seedToPeerId } from './internal/peerIdUtils';
910
import { RequestFlow } from './internal/RequestFlow';
1011
import { RequestFlowBuilder } from './internal/RequestFlowBuilder';
@@ -63,11 +64,13 @@ type Node = {
6364
* Creates a Fluence client. If the `connectTo` is specified connects the client to the network
6465
* @param { string | Multiaddr | Node } [connectTo] - Node in Fluence network to connect to. If not specified client will not be connected to the n
6566
* @param { PeerId | string } [peerIdOrSeed] - The Peer Id of the created client. Specified either as PeerId structure or as seed string. Will be generated randomly if not specified
67+
* @param { FluenceConnectionOptions } [options] - additional configuraton options for Fluence Connection made with the client
6668
* @returns { Promise<FluenceClient> } Promise which will be resolved with the created FluenceClient
6769
*/
6870
export const createClient = async (
6971
connectTo?: string | Multiaddr | Node,
7072
peerIdOrSeed?: PeerId | string,
73+
options?: FluenceConnectionOptions,
7174
): Promise<FluenceClient> => {
7275
let peerId;
7376
if (!peerIdOrSeed) {
@@ -92,9 +95,14 @@ export const createClient = async (
9295
theAddress = new Multiaddr(connectTo as string);
9396
}
9497

95-
await client.connect(theAddress);
96-
if (!(await checkConnection(client))) {
97-
throw new Error('Connection check failed. Check if the node is working or try to connect to another node');
98+
await client.connect(theAddress, options);
99+
100+
if (options?.skipCheckConnection) {
101+
if (!(await checkConnection(client, options.checkConnectionTTL))) {
102+
throw new Error(
103+
'Connection check failed. Check if the node is working or try to connect to another node',
104+
);
105+
}
98106
}
99107
}
100108

@@ -105,7 +113,7 @@ export const createClient = async (
105113
* Checks the network connection by sending a ping-like request to relat node
106114
* @param { FluenceClient } client - The Fluence Client instance.
107115
*/
108-
export const checkConnection = async (client: FluenceClient): Promise<boolean> => {
116+
export const checkConnection = async (client: FluenceClient, ttl?: number): Promise<boolean> => {
109117
if (!client.isConnected) {
110118
return false;
111119
}
@@ -121,6 +129,7 @@ export const checkConnection = async (client: FluenceClient): Promise<boolean> =
121129
(call %init_peer_id% ("${callbackService}" "${callbackFn}") [result])
122130
)`,
123131
)
132+
.withTTL(ttl)
124133
.withVariables({
125134
msg,
126135
})

src/__test__/integration/builtins.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('Builtins usage suite', () => {
7474

7575
let bpIdReturned = await addBlueprint(client, 'test_broken_blueprint', ['test_broken_module'], bpId);
7676
let allBps = await getBlueprints(client);
77-
const allBpIds = allBps.map(x => x.id);
77+
const allBpIds = allBps.map((x) => x.id);
7878

7979
expect(allBpIds).toContain(bpIdReturned);
8080
});
@@ -85,7 +85,7 @@ describe('Builtins usage suite', () => {
8585
let promise = createService(client, 'test_broken_blueprint');
8686

8787
await expect(promise).rejects.toMatchObject({
88-
error: expect.stringContaining("Blueprint wasn't found at"),
88+
error: expect.stringContaining("Blueprint 'test_broken_blueprint' wasn't found"),
8989
instruction: expect.stringContaining('blueprint_id'),
9090
});
9191
});

src/__test__/integration/client.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ describe('Typescript usage suite', () => {
4242
expect(isConnected).toEqual(true);
4343
});
4444

45+
it('check connection should work with ttl', async function () {
46+
client = await createClient();
47+
await client.connect(nodes[0].multiaddr);
48+
49+
let isConnected = await checkConnection(client, 10000);
50+
51+
expect(isConnected).toEqual(true);
52+
});
53+
4554
it('two clients should work inside the same time browser', async () => {
4655
// arrange
4756
const client1 = await createClient(nodes[0].multiaddr);
@@ -136,6 +145,42 @@ describe('Typescript usage suite', () => {
136145
// assert
137146
expect(isConnected).toBeTruthy;
138147
});
148+
149+
it('With connection options: dialTimeout', async () => {
150+
// arrange
151+
const addr = nodes[0].multiaddr;
152+
153+
// act
154+
client = await createClient(addr, undefined, { dialTimeout: 100000 });
155+
const isConnected = await checkConnection(client);
156+
157+
// assert
158+
expect(isConnected).toBeTruthy;
159+
});
160+
161+
it('With connection options: skipCheckConnection', async () => {
162+
// arrange
163+
const addr = nodes[0].multiaddr;
164+
165+
// act
166+
client = await createClient(addr, undefined, { skipCheckConnection: true });
167+
const isConnected = await checkConnection(client);
168+
169+
// assert
170+
expect(isConnected).toBeTruthy;
171+
});
172+
173+
it('With connection options: checkConnectionTTL', async () => {
174+
// arrange
175+
const addr = nodes[0].multiaddr;
176+
177+
// act
178+
client = await createClient(addr, undefined, { checkConnectionTTL: 1000 });
179+
const isConnected = await checkConnection(client);
180+
181+
// assert
182+
expect(isConnected).toBeTruthy;
183+
});
139184
});
140185

141186
it('xor handling should work with connected client', async function () {

src/__test__/unit/air.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('== AIR suite', () => {
7575
const script = `(null)`;
7676
// prettier-ignore
7777
const [request, promise] = new RequestFlowBuilder()
78-
.withTTL(0)
78+
.withTTL(1)
7979
.withRawScript(script)
8080
.buildAsFetch();
8181

src/__test__/unit/ast.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ describe('== AST parsing suite', () => {
44
it('parse simple script and return ast', async function () {
55
const interpreter = await AquamarineInterpreter.create({} as any);
66
let ast = interpreter.parseAir(`
7-
(call node ("service" "function") [1 2 3 arg] output)
7+
(call "node" ("service" "function") [1 2 3] output)
88
`);
99

10+
console.log(ast);
1011
ast = JSON.parse(ast);
1112

1213
expect(ast).toEqual({
1314
Call: {
14-
peer_part: { PeerPk: { Variable: 'node' } },
15+
peer_part: { PeerPk: { Literal: 'node' } },
1516
function_part: { ServiceIdWithFuncName: [{ Literal: 'service' }, { Literal: 'function' }] },
1617
args: [
1718
{
@@ -29,7 +30,6 @@ describe('== AST parsing suite', () => {
2930
Int: 3,
3031
},
3132
},
32-
{ Variable: 'arg' },
3333
],
3434
output: { Scalar: 'output' },
3535
},

src/internal/ClientImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import * as PeerId from 'peer-id';
1818
import Multiaddr from 'multiaddr';
19-
import { FluenceConnection } from './FluenceConnection';
19+
import { FluenceConnection, FluenceConnectionOptions } from './FluenceConnection';
2020

2121
import { CallServiceResult, ParticleHandler, PeerIdB58, SecurityTetraplet } from './commonTypes';
2222
import { FluenceClient } from '../FluenceClient';
@@ -77,7 +77,7 @@ export class ClientImpl implements FluenceClient {
7777
});
7878
}
7979

80-
async connect(multiaddr: string | Multiaddr): Promise<void> {
80+
async connect(multiaddr: string | Multiaddr, options?: FluenceConnectionOptions): Promise<void> {
8181
multiaddr = Multiaddr(multiaddr);
8282

8383
const nodePeerId = multiaddr.getPeerId();
@@ -96,7 +96,7 @@ export class ClientImpl implements FluenceClient {
9696
this.selfPeerIdFull,
9797
this.executeIncomingParticle.bind(this),
9898
);
99-
await connection.connect();
99+
await connection.connect(options);
100100
this.connection = connection;
101101
this.initWatchDog();
102102
}

src/internal/FluenceConnection.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import * as log from 'loglevel';
2323
import { parseParticle, Particle, toPayload } from './particle';
2424
import { NOISE } from 'libp2p-noise';
2525
import PeerId from 'peer-id';
26-
import Multiaddr from 'multiaddr'
26+
import Multiaddr from 'multiaddr';
27+
import { options } from 'libp2p/src/keychain';
2728

2829
export const PROTOCOL_NAME = '/fluence/faas/1.0.0';
2930

@@ -33,6 +34,26 @@ enum Status {
3334
Disconnected = 'Disconnected',
3435
}
3536

37+
/**
38+
* Options to configure fluence connection
39+
*/
40+
export interface FluenceConnectionOptions {
41+
/**
42+
* @property {number} [checkConnectionTTL] - TTL for the check connection request in ms
43+
*/
44+
checkConnectionTTL?: number;
45+
46+
/**
47+
* @property {number} [checkConnectionTTL] - set to true to skip check connection request completely
48+
*/
49+
skipCheckConnection?: boolean;
50+
51+
/**
52+
* @property {number} [dialTimeout] - How long a dial attempt is allowed to take.
53+
*/
54+
dialTimeout?: number;
55+
}
56+
3657
export class FluenceConnection {
3758
private readonly selfPeerId: PeerId;
3859
private node: Peer;
@@ -54,7 +75,7 @@ export class FluenceConnection {
5475
this.nodePeerId = hostPeerId;
5576
}
5677

57-
async connect() {
78+
async connect(options?: FluenceConnectionOptions) {
5879
let peerInfo = this.selfPeerId;
5980
this.node = await Peer.create({
6081
peerId: peerInfo,
@@ -64,6 +85,9 @@ export class FluenceConnection {
6485
streamMuxer: [Mplex],
6586
connEncryption: [NOISE],
6687
},
88+
dialer: {
89+
timeout: options?.dialTimeout,
90+
},
6791
});
6892

6993
await this.startReceiving();

src/internal/RequestFlowBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ export class RequestFlowBuilder {
127127
return this;
128128
}
129129

130-
withTTL(ttl: number): RequestFlowBuilder {
131-
this.ttl = ttl;
130+
withTTL(ttl?: number): RequestFlowBuilder {
131+
if (ttl) {
132+
this.ttl = ttl;
133+
}
132134
return this;
133135
}
134136

0 commit comments

Comments
 (0)