Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"scripts": {
"test": "jest --watch",
"test:all": "jest",
"test:unit": "jest --testPathPattern=src/__test__/unit",
"test:integration": "jest --testPathPattern=src/__test__/integration",
"test": "jest --env=jsdom --watch",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's strange. jsom should be the default option

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added it just in case. Explicit here is better than implicit I think. I also think that we should run tests for both env node and env jsdom

"test:all": "jest --env=jsdom",
"test:unit": "jest --env=jsdom --testPathPattern=src/__test__/unit",
"test:integration": "jest --env=jsdom --testPathPattern=src/__test__/integration",
"build": "tsc"
},
"repository": "https://github.com/fluencelabs/fluence-js",
Expand Down
27 changes: 27 additions & 0 deletions src/__test__/unit/WsTransport.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {FluenceConnection} from "../../internal/FluenceConnection";
import Peer from "libp2p";
import Multiaddr = require("multiaddr");
import {generatePeerId} from "../../internal/peerIdUtils";

describe('Ws Transport', () => {
// TODO: fix this test
test.skip('Should work with ws schema', async () => {
// arrange
let multiaddr = new Multiaddr("/ip4/127.0.0.1/tcp/1234/ws/p2p/12D3KooWMJ78GJrtCxVUpjLEedbPtnLDxkFQJ2wuefEdrxq6zwSs");
let peerId = await generatePeerId();
const connection = new FluenceConnection(
multiaddr,
peerId,
peerId,
_ => {},
);
await (connection as any).createPeer();
let node = (connection as any).node as Peer;

// act
let transport = node.transportManager.transportForMultiaddr(multiaddr);

// assert
expect(transport).not.toBeDefined();
});
});
34 changes: 22 additions & 12 deletions src/internal/FluenceConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { parseParticle, Particle, toPayload } from './particle';
import { NOISE } from 'libp2p-noise';
import PeerId from 'peer-id';
import Multiaddr from 'multiaddr';
import { options } from 'libp2p/src/keychain';
import { all as allow_all } from 'libp2p-websockets/src/filters';

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

Expand Down Expand Up @@ -76,30 +76,40 @@ export class FluenceConnection {
}

async connect(options?: FluenceConnectionOptions) {
let peerInfo = this.selfPeerId;
await this.createPeer(options);
await this.startReceiving();
}

isConnected() {
return this.status === Status.Connected;
}

// connection status. If `Disconnected`, it cannot be reconnected
private status: Status = Status.Initializing;

private async createPeer(options?: FluenceConnectionOptions) {
const peerInfo = this.selfPeerId;
const transportKey = Websockets.prototype[Symbol.toStringTag]
this.node = await Peer.create({
peerId: peerInfo,
config: {},
modules: {
transport: [Websockets],
streamMuxer: [Mplex],
connEncryption: [NOISE],
},
config: {
transport: {
[transportKey]: {
filter: allow_all
}
}
},
dialer: {
timeout: options?.dialTimeout,
},
});

await this.startReceiving();
}

isConnected() {
return this.status === Status.Connected;
}

// connection status. If `Disconnected`, it cannot be reconnected
private status: Status = Status.Initializing;

private async startReceiving() {
if (this.status === Status.Initializing) {
await this.node.start();
Expand Down