mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-04-25 01:42:13 +00:00
Check if connection is available on creating a client (#23)
Check if connection is available on creating a client
This commit is contained in:
parent
78ed8ab46c
commit
c10cd1942a
@ -12,6 +12,7 @@ import {
|
||||
} from '../../internal/builtins';
|
||||
import { ModuleConfig } from '../../internal/moduleConfig';
|
||||
import { createConnectedClient } from '../util';
|
||||
import {checkConnection} from "../../api";
|
||||
|
||||
const dev2multiaddr = '/dns4/dev.fluence.dev/tcp/19003/wss/p2p/12D3KooWBUJifCTgaxAUrcM9JysqCcS4CS8tiYH5hExbdWCAoNwb';
|
||||
const dev3multiaddr = '/dns4/dev.fluence.dev/tcp/19004/wss/p2p/12D3KooWJbJFaZ3k5sNd8DjQgg3aERoKtBAnirEvPV8yp76kEXHB';
|
||||
@ -43,6 +44,14 @@ describe('Builtins usage suite', () => {
|
||||
expect(bpList).not.toBeUndefined;
|
||||
});
|
||||
|
||||
it('check_connection', async function () {
|
||||
const client = await createConnectedClient(dev2multiaddr);
|
||||
|
||||
let isConnected = await checkConnection(client);
|
||||
|
||||
expect(isConnected).toEqual(true);
|
||||
});
|
||||
|
||||
it('upload_modules', async function () {
|
||||
const client = await createConnectedClient(dev2multiaddr);
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { encode } from 'bs58';
|
||||
import { generatePeerId, peerIdToSeed, seedToPeerId } from '../../internal/peerIdUtils';
|
||||
import { FluenceClientImpl } from '../../internal/FluenceClientImpl';
|
||||
import { createConnectedClient } from '../util';
|
||||
import {encode} from 'bs58';
|
||||
import {generatePeerId, peerIdToSeed, seedToPeerId} from '../../internal/peerIdUtils';
|
||||
import {FluenceClientImpl} from '../../internal/FluenceClientImpl';
|
||||
import {createConnectedClient} from '../util';
|
||||
import log from 'loglevel';
|
||||
import { createClient } from '../../api';
|
||||
import {createClient} from '../../api';
|
||||
import Multiaddr from 'multiaddr';
|
||||
|
||||
const devNodeAddress = '/dns4/dev.fluence.dev/tcp/19001/wss/p2p/12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9';
|
||||
@ -40,8 +40,7 @@ describe('Typescript usage suite', () => {
|
||||
|
||||
await client.sendScript(script, data);
|
||||
|
||||
const res = await resMakingPromise;
|
||||
return res;
|
||||
return await resMakingPromise;
|
||||
};
|
||||
|
||||
it('address as string', async function () {
|
||||
@ -198,13 +197,8 @@ describe('Typescript usage suite', () => {
|
||||
|
||||
it('two clients should work inside the same time browser', async function () {
|
||||
// arrange
|
||||
const pid1 = await generatePeerId();
|
||||
const client1 = new FluenceClientImpl(pid1);
|
||||
await client1.connect(devNodeAddress);
|
||||
|
||||
const pid2 = await generatePeerId();
|
||||
const client2 = new FluenceClientImpl(pid2);
|
||||
await client2.connect(devNodeAddress);
|
||||
const client1 = await createConnectedClient(devNodeAddress);
|
||||
const client2 = await createConnectedClient(devNodeAddress);
|
||||
|
||||
let resMakingPromise = new Promise((resolve) => {
|
||||
client2.registerCallback('test', 'test', (args, _) => {
|
||||
@ -216,7 +210,7 @@ describe('Typescript usage suite', () => {
|
||||
let script = `
|
||||
(seq
|
||||
(call "${client1.relayPeerId}" ("op" "identity") [])
|
||||
(call "${pid2.toB58String()}" ("test" "test") [a b c d])
|
||||
(call "${client2.selfPeerId}" ("test" "test") [a b c d])
|
||||
)
|
||||
`;
|
||||
|
||||
@ -234,13 +228,8 @@ describe('Typescript usage suite', () => {
|
||||
|
||||
it('event registration should work', async function () {
|
||||
// arrange
|
||||
const pid1 = await generatePeerId();
|
||||
const client1 = new FluenceClientImpl(pid1);
|
||||
await client1.connect(devNodeAddress);
|
||||
|
||||
const pid2 = await generatePeerId();
|
||||
const client2 = new FluenceClientImpl(pid2);
|
||||
await client2.connect(devNodeAddress);
|
||||
const client1 = await createConnectedClient(devNodeAddress);
|
||||
const client2 = await createConnectedClient(devNodeAddress);
|
||||
|
||||
client2.registerEvent('event_stream', 'test');
|
||||
const resMakingPromise = new Promise((resolve) => {
|
||||
@ -249,7 +238,7 @@ describe('Typescript usage suite', () => {
|
||||
|
||||
// act
|
||||
let script = `
|
||||
(call "${pid2.toB58String()}" ("event_stream" "test") [hello])
|
||||
(call "${client2.selfPeerId}" ("event_stream" "test") [hello])
|
||||
`;
|
||||
|
||||
let data: Map<string, any> = new Map();
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { generatePeerId } from '..';
|
||||
import { FluenceClientImpl } from '../internal/FluenceClientImpl';
|
||||
import {generatePeerId} from '..';
|
||||
import {FluenceClientImpl} from '../internal/FluenceClientImpl';
|
||||
|
||||
export const createLocalClient = async () => {
|
||||
const peerId = await generatePeerId();
|
||||
|
42
src/api.ts
42
src/api.ts
@ -5,6 +5,7 @@ import Multiaddr from 'multiaddr';
|
||||
import PeerId, { isPeerId } from 'peer-id';
|
||||
import { generatePeerId, seedToPeerId } from './internal/peerIdUtils';
|
||||
import { FluenceClientImpl } from './internal/FluenceClientImpl';
|
||||
import log from "loglevel";
|
||||
|
||||
type Node = {
|
||||
peerId: string;
|
||||
@ -44,6 +45,9 @@ export const createClient = async (
|
||||
}
|
||||
|
||||
await client.connect(theAddress);
|
||||
if (!await checkConnection(client)) {
|
||||
throw new Error("Connection check failed. Check if the node is working or try to connect to another node")
|
||||
}
|
||||
}
|
||||
|
||||
return client;
|
||||
@ -147,7 +151,43 @@ export const sendParticleAsFetch = async <T>(
|
||||
}, particle.ttl);
|
||||
});
|
||||
|
||||
sendParticle(client, particle);
|
||||
await sendParticle(client, particle);
|
||||
|
||||
return promise;
|
||||
};
|
||||
|
||||
export const checkConnection = async (client: FluenceClient): Promise<boolean> => {
|
||||
let msg = Math.random().toString(36).substring(7);
|
||||
let callbackFn = "checkConnection"
|
||||
let callbackService = "_callback"
|
||||
|
||||
const particle = new Particle(
|
||||
`
|
||||
(seq
|
||||
(call __relay ("op" "identity") [msg] result)
|
||||
(call myPeerId ("${callbackService}" "${callbackFn}") [result])
|
||||
)
|
||||
`,
|
||||
{
|
||||
__relay: client.relayPeerId,
|
||||
myPeerId: client.selfPeerId,
|
||||
msg
|
||||
},
|
||||
3000
|
||||
);
|
||||
|
||||
if (!client.isConnected) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
let result = await sendParticleAsFetch<string[][]>(client, particle, callbackFn, callbackService)
|
||||
if (result[0][0] != msg) {
|
||||
log.warn("unexpected behavior. 'identity' must return arguments the passed arguments.")
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
log.error("Error on establishing connection: ", e)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user