mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-04-25 09:52:12 +00:00
dump WIP
This commit is contained in:
parent
40c881be5c
commit
25c43364a9
@ -13,7 +13,7 @@
|
|||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"compile-aqua": "aqua -i ./aqua/ -o ./src/internal/_aqua",
|
"compile-aqua": "aqua -i ./aqua/ -o ./src/internal/_aqua",
|
||||||
"test:smoke": "node ./dist/js-peer/__test__/integration/smokeTest.js",
|
"test:smoke": "node ./dist/js-peer/__test__/integration/smokeTest.js",
|
||||||
"test": "NODE_OPTIONS=--experimental-vm-modules pnpm jest",
|
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
|
||||||
"test:unit": "NODE_OPTIONS=--experimental-vm-modules pnpm jest --testPathPattern=src/__test__/unit",
|
"test:unit": "NODE_OPTIONS=--experimental-vm-modules pnpm jest --testPathPattern=src/__test__/unit",
|
||||||
"test:integration": "NODE_OPTIONS=--experimental-vm-modules pnpm jest --testPathPattern=src/__test__/integration"
|
"test:integration": "NODE_OPTIONS=--experimental-vm-modules pnpm jest --testPathPattern=src/__test__/integration"
|
||||||
},
|
},
|
||||||
|
@ -133,36 +133,38 @@ export class RelayConnection extends FluenceConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async connect(onIncomingParticle: ParticleHandler) {
|
async connect(onIncomingParticle: ParticleHandler) {
|
||||||
await this._lib2p2Peer.start();
|
|
||||||
|
|
||||||
this._lib2p2Peer.handle([PROTOCOL_NAME], async ({ connection, stream }) => {
|
|
||||||
pipe(
|
|
||||||
stream.source,
|
|
||||||
// @ts-ignore
|
|
||||||
decode(),
|
|
||||||
// @ts-ignore
|
|
||||||
(source) => map(source, (buf) => toString(buf.subarray())),
|
|
||||||
async (source) => {
|
|
||||||
try {
|
|
||||||
for await (const msg of source) {
|
|
||||||
try {
|
|
||||||
onIncomingParticle(msg);
|
|
||||||
} catch (e) {
|
|
||||||
log.error('error on handling a new incoming message: ' + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
log.debug('connection closed: ' + e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId.toString());
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await this._lib2p2Peer.start();
|
||||||
|
|
||||||
|
this._lib2p2Peer.handle([PROTOCOL_NAME], async ({ connection, stream }) => {
|
||||||
|
pipe(
|
||||||
|
stream.source,
|
||||||
|
// @ts-ignore
|
||||||
|
decode(),
|
||||||
|
// @ts-ignore
|
||||||
|
(source) => map(source, (buf) => toString(buf.subarray())),
|
||||||
|
async (source) => {
|
||||||
|
try {
|
||||||
|
for await (const msg of source) {
|
||||||
|
try {
|
||||||
|
onIncomingParticle(msg);
|
||||||
|
} catch (e) {
|
||||||
|
log.error('error on handling a new incoming message: ' + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.debug('connection closed: ' + e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
log.debug(`dialing to the node with client's address: ` + this._lib2p2Peer.peerId.toString());
|
||||||
|
|
||||||
this._connection = await this._lib2p2Peer.dial(this._relayAddress);
|
this._connection = await this._lib2p2Peer.dial(this._relayAddress);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
console.log("ERROR IN CONNECTION: " + e)
|
||||||
|
console.log(e)
|
||||||
if (e.name === 'AggregateError' && e._errors?.length === 1) {
|
if (e.name === 'AggregateError' && e._errors?.length === 1) {
|
||||||
const error = e._errors[0];
|
const error = e._errors[0];
|
||||||
throw new Error(`Error dialing node ${this._relayAddress}:\n${error.code}\n${error.message}`);
|
throw new Error(`Error dialing node ${this._relayAddress}:\n${error.code}\n${error.message}`);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Particle } from '../../Particle.js';
|
import { Particle } from '../../Particle.js';
|
||||||
import { doNothing } from '../../utils.js';
|
import { doNothing } from '../../utils.js';
|
||||||
import { FluencePeer } from '../../FluencePeer.js';
|
import {createClient} from "../../../../../../client/js-client.node";
|
||||||
import { mkTestPeer } from '../util.js';
|
import {FluencePeer} from "../../FluencePeer";
|
||||||
|
|
||||||
let peer: FluencePeer;
|
let peer: FluencePeer;
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ describe('Sig service test suite', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
peer = mkTestPeer();
|
peer = createClient();
|
||||||
await peer.start();
|
await peer.start();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ describe('Sig service test suite', () => {
|
|||||||
)
|
)
|
||||||
`;
|
`;
|
||||||
const promise = new Promise<any>((resolve) => {
|
const promise = new Promise<any>((resolve) => {
|
||||||
peer.internals.regHandler.common('res', 'res', (req) => {
|
peer.internals.regHandler.common('res', 'res', (req: any) => {
|
||||||
resolve(req.args);
|
resolve(req.args);
|
||||||
return {
|
return {
|
||||||
result: {},
|
result: {},
|
@ -1,32 +1,14 @@
|
|||||||
import { nodes } from '../connection.js';
|
import { nodes } from '../connection.js';
|
||||||
import { checkConnection, doNothing, handleTimeout } from '../../utils.js';
|
import { checkConnection, doNothing, handleTimeout } from '../../utils.js';
|
||||||
import { registerHandlersHelper, mkTestPeer, withPeer, withConnectedPeer } from '../util.js';
|
import {registerHandlersHelper, mkTestPeer, withPeer, withConnectedPeer} from '../util.js';
|
||||||
import { FluencePeer } from '../../FluencePeer.js';
|
import {FluencePeer} from "../../FluencePeer";
|
||||||
|
import {createClient} from "../../../../../../client/js-client.node";
|
||||||
|
|
||||||
describe('Typescript usage suite', () => {
|
describe('Typescript usage suite', () => {
|
||||||
it('should perform test for FluencePeer class correctly', () => {
|
|
||||||
// arrange
|
|
||||||
const peer = mkTestPeer();
|
|
||||||
const number = 1;
|
|
||||||
const object = { str: 'Hello!' };
|
|
||||||
const undefinedVal = undefined;
|
|
||||||
|
|
||||||
// act
|
|
||||||
const isPeerPeer = FluencePeer.isInstance(peer);
|
|
||||||
const isNumberPeer = FluencePeer.isInstance(number);
|
|
||||||
const isObjectPeer = FluencePeer.isInstance(object);
|
|
||||||
const isUndefinedPeer = FluencePeer.isInstance(undefinedVal);
|
|
||||||
|
|
||||||
// act
|
|
||||||
expect(isPeerPeer).toBe(true);
|
|
||||||
expect(isNumberPeer).toBe(false);
|
|
||||||
expect(isObjectPeer).toBe(false);
|
|
||||||
expect(isUndefinedPeer).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Should expose correct peer status', () => {
|
describe('Should expose correct peer status', () => {
|
||||||
it('Should expose correct status for uninitialized peer', () => {
|
it('Should expose correct status for uninitialized peer', () => {
|
||||||
const peer = mkTestPeer();
|
const peer = createClient();
|
||||||
const status = peer.getStatus();
|
const status = peer.getStatus();
|
||||||
|
|
||||||
expect(status.isConnected).toBe(false);
|
expect(status.isConnected).toBe(false);
|
||||||
@ -52,16 +34,21 @@ describe('Typescript usage suite', () => {
|
|||||||
|
|
||||||
it('Should expose correct status for connected peer', async () => {
|
it('Should expose correct status for connected peer', async () => {
|
||||||
await withConnectedPeer(async (peer) => {
|
await withConnectedPeer(async (peer) => {
|
||||||
// arrange
|
try {
|
||||||
|
// arrange
|
||||||
|
|
||||||
// act
|
// act
|
||||||
const status = peer.getStatus();
|
const status = peer.getStatus();
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(status.isConnected).toBe(true);
|
expect(status.isConnected).toBe(true);
|
||||||
expect(status.isInitialized).toBe(true);
|
expect(status.isInitialized).toBe(true);
|
||||||
expect(status.peerId).not.toBe(null);
|
expect(status.peerId).not.toBe(null);
|
||||||
expect(status.relayPeerId).not.toBe(null);
|
expect(status.relayPeerId).not.toBe(null);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("ERROR: " + e)
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -110,7 +97,7 @@ describe('Typescript usage suite', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
peer.internals.initiateParticle(particle, handleTimeout(reject));
|
peer.internals.initiateParticle(particle, handleTimeout(reject));
|
||||||
});
|
}).catch((err) => console.log("AAAAAAAAAAAZZAAAAAA: " + err));
|
||||||
|
|
||||||
expect(result).toBe('hello world!');
|
expect(result).toBe('hello world!');
|
||||||
});
|
});
|
||||||
@ -136,7 +123,7 @@ describe('Typescript usage suite', () => {
|
|||||||
await withConnectedPeer(async (peer1) => {
|
await withConnectedPeer(async (peer1) => {
|
||||||
await withConnectedPeer(async (peer2) => {
|
await withConnectedPeer(async (peer2) => {
|
||||||
const res = new Promise((resolve) => {
|
const res = new Promise((resolve) => {
|
||||||
peer2.internals.regHandler.common('test', 'test', (req) => {
|
peer2.internals.regHandler.common('test', 'test', (req: any) => {
|
||||||
resolve(req.args[0]);
|
resolve(req.args[0]);
|
||||||
return {
|
return {
|
||||||
result: {},
|
result: {},
|
||||||
@ -196,7 +183,7 @@ describe('Typescript usage suite', () => {
|
|||||||
|
|
||||||
expect(isConnected).toBeTruthy();
|
expect(isConnected).toBeTruthy();
|
||||||
},
|
},
|
||||||
{ connectTo: nodes[0], dialTimeoutMs: 100000 },
|
{ relay: nodes[0], connectionOptions: { dialTimeoutMs: 100000 }},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -207,7 +194,7 @@ describe('Typescript usage suite', () => {
|
|||||||
|
|
||||||
expect(isConnected).toBeTruthy();
|
expect(isConnected).toBeTruthy();
|
||||||
},
|
},
|
||||||
{ connectTo: nodes[0], skipCheckConnection: true },
|
{ relay: nodes[0], connectionOptions: { dialTimeoutMs: 100000 }},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -218,7 +205,7 @@ describe('Typescript usage suite', () => {
|
|||||||
|
|
||||||
expect(isConnected).toBeTruthy();
|
expect(isConnected).toBeTruthy();
|
||||||
},
|
},
|
||||||
{ connectTo: nodes[0], checkConnectionTimeoutMs: 1000 },
|
{ relay: nodes[0], connectionOptions: { dialTimeoutMs: 1000 }},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -229,7 +216,7 @@ describe('Typescript usage suite', () => {
|
|||||||
|
|
||||||
expect(isConnected).toBeFalsy();
|
expect(isConnected).toBeFalsy();
|
||||||
},
|
},
|
||||||
{ connectTo: nodes[0], defaultTtlMs: 1 },
|
{ relay: nodes[0], defaultTtlMs: 1},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
@ -1,19 +1,21 @@
|
|||||||
import { mkTestPeer } from '../util.js';
|
import {createClient} from "../../../../../../client/js-client.node";
|
||||||
|
|
||||||
const peer = mkTestPeer();
|
|
||||||
|
|
||||||
describe('Parse ast tests', () => {
|
describe('Parse ast tests', () => {
|
||||||
|
|
||||||
|
let somePeer: any;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
await peer.start();
|
somePeer = await createClient();
|
||||||
});
|
await somePeer.start();
|
||||||
|
}, 10000);
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await peer.stop();
|
await somePeer.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Correct ast should be parsed correctly', async function () {
|
it('Correct ast should be parsed correctly', async function () {
|
||||||
const air = `(null)`;
|
const air = `(null)`;
|
||||||
const res = await peer.internals.parseAst(air);
|
const res = await somePeer.internals.parseAst(air);
|
||||||
|
|
||||||
expect(res).toStrictEqual({
|
expect(res).toStrictEqual({
|
||||||
success: true,
|
success: true,
|
||||||
@ -23,7 +25,7 @@ describe('Parse ast tests', () => {
|
|||||||
|
|
||||||
it('Incorrect ast should result in corresponding error', async function () {
|
it('Incorrect ast should result in corresponding error', async function () {
|
||||||
const air = `(null`;
|
const air = `(null`;
|
||||||
const res = await peer.internals.parseAst(air);
|
const res = await somePeer.internals.parseAst(air);
|
||||||
|
|
||||||
expect(res).toStrictEqual({
|
expect(res).toStrictEqual({
|
||||||
success: false,
|
success: false,
|
@ -1,7 +1,7 @@
|
|||||||
import * as api from '@fluencelabs/aqua-api/aqua-api.js';
|
import * as api from '@fluencelabs/aqua-api/aqua-api.js';
|
||||||
|
|
||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from 'fs';
|
||||||
import { FluencePeer, PeerConfig } from '../FluencePeer.js';
|
import {FluencePeer, PeerConfig} from '../FluencePeer.js';
|
||||||
import { Particle } from '../Particle.js';
|
import { Particle } from '../Particle.js';
|
||||||
import { MakeServiceCall } from '../utils.js';
|
import { MakeServiceCall } from '../utils.js';
|
||||||
import { avmModuleLoader, controlModuleLoader } from '../utilsForNode.js';
|
import { avmModuleLoader, controlModuleLoader } from '../utilsForNode.js';
|
||||||
@ -12,7 +12,8 @@ import { marineLogFunction } from '../utils.js';
|
|||||||
import { MarineBackgroundRunner } from '../../marine/worker/index.js';
|
import { MarineBackgroundRunner } from '../../marine/worker/index.js';
|
||||||
import { MarineBasedAvmRunner } from '../avm.js';
|
import { MarineBasedAvmRunner } from '../avm.js';
|
||||||
import { nodes } from './connection.js';
|
import { nodes } from './connection.js';
|
||||||
import { WorkerLoaderFromFs } from '../../marine/deps-loader/node.js';
|
import {WorkerLoaderFromFs} from '../../marine/deps-loader/node.js';
|
||||||
|
import {createClient} from "../../../../../client/js-client.node";
|
||||||
|
|
||||||
export const registerHandlersHelper = (
|
export const registerHandlersHelper = (
|
||||||
peer: FluencePeer,
|
peer: FluencePeer,
|
||||||
@ -66,12 +67,17 @@ export const mkTestPeer = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const withPeer = async (action: (p: FluencePeer) => Promise<void>, config?: PeerConfig) => {
|
export const withPeer = async (action: (p: FluencePeer) => Promise<void>, config?: PeerConfig) => {
|
||||||
const p = mkTestPeer();
|
const p = createClient()
|
||||||
try {
|
try {
|
||||||
await p.start(config);
|
console.log("connecting to: ")
|
||||||
await action(p);
|
console.log(config)
|
||||||
|
await p.start(config).catch((e: any) => console.log("connection error: " + e));
|
||||||
|
console.log("connected")
|
||||||
|
await action(p).catch((e) => console.log("ERRORRRRR: " + e));
|
||||||
} finally {
|
} finally {
|
||||||
|
console.log("stopping")
|
||||||
await p!.stop();
|
await p!.stop();
|
||||||
|
console.log("stoped")
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,9 +18,9 @@ import log from 'loglevel';
|
|||||||
|
|
||||||
import { Buffer } from 'buffer';
|
import { Buffer } from 'buffer';
|
||||||
import { CallServiceData, CallServiceResult, CallServiceResultType, ResultCodes } from '../interfaces/commonTypes.js';
|
import { CallServiceData, CallServiceResult, CallServiceResultType, ResultCodes } from '../interfaces/commonTypes.js';
|
||||||
import { FluencePeer } from './FluencePeer.js';
|
|
||||||
import { ParticleExecutionStage } from './Particle.js';
|
import { ParticleExecutionStage } from './Particle.js';
|
||||||
import { LogFunction } from '@fluencelabs/marine-js/dist/types';
|
import { LogFunction } from '@fluencelabs/marine-js/dist/types';
|
||||||
|
import {FluencePeer} from "./FluencePeer";
|
||||||
|
|
||||||
export const MakeServiceCall =
|
export const MakeServiceCall =
|
||||||
(fn: (args: any[]) => CallServiceResultType) =>
|
(fn: (args: any[]) => CallServiceResultType) =>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user