Skip to content

Commit 480c8aa

Browse files
authored
Allow ws transport (#37)
1 parent 15e645c commit 480c8aa

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"main": "./dist/index.js",
66
"typings": "./dist/index.d.ts",
77
"scripts": {
8-
"test": "jest --watch",
9-
"test:all": "jest",
10-
"test:unit": "jest --testPathPattern=src/__test__/unit",
11-
"test:integration": "jest --testPathPattern=src/__test__/integration",
8+
"test": "jest --env=jsdom --watch",
9+
"test:all": "jest --env=jsdom",
10+
"test:unit": "jest --env=jsdom --testPathPattern=src/__test__/unit",
11+
"test:integration": "jest --env=jsdom --testPathPattern=src/__test__/integration",
1212
"build": "tsc"
1313
},
1414
"repository": "https://github.com/fluencelabs/fluence-js",

src/__test__/unit/WsTransport.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {FluenceConnection} from "../../internal/FluenceConnection";
2+
import Peer from "libp2p";
3+
import Multiaddr = require("multiaddr");
4+
import {generatePeerId} from "../../internal/peerIdUtils";
5+
6+
describe('Ws Transport', () => {
7+
// TODO: fix this test
8+
test.skip('Should work with ws schema', async () => {
9+
// arrange
10+
let multiaddr = new Multiaddr("/ip4/127.0.0.1/tcp/1234/ws/p2p/12D3KooWMJ78GJrtCxVUpjLEedbPtnLDxkFQJ2wuefEdrxq6zwSs");
11+
let peerId = await generatePeerId();
12+
const connection = new FluenceConnection(
13+
multiaddr,
14+
peerId,
15+
peerId,
16+
_ => {},
17+
);
18+
await (connection as any).createPeer();
19+
let node = (connection as any).node as Peer;
20+
21+
// act
22+
let transport = node.transportManager.transportForMultiaddr(multiaddr);
23+
24+
// assert
25+
expect(transport).not.toBeDefined();
26+
});
27+
});

src/internal/FluenceConnection.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { parseParticle, Particle, toPayload } from './particle';
2424
import { NOISE } from 'libp2p-noise';
2525
import PeerId from 'peer-id';
2626
import Multiaddr from 'multiaddr';
27-
import { options } from 'libp2p/src/keychain';
27+
import { all as allow_all } from 'libp2p-websockets/src/filters';
2828

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

@@ -76,30 +76,40 @@ export class FluenceConnection {
7676
}
7777

7878
async connect(options?: FluenceConnectionOptions) {
79-
let peerInfo = this.selfPeerId;
79+
await this.createPeer(options);
80+
await this.startReceiving();
81+
}
82+
83+
isConnected() {
84+
return this.status === Status.Connected;
85+
}
86+
87+
// connection status. If `Disconnected`, it cannot be reconnected
88+
private status: Status = Status.Initializing;
89+
90+
private async createPeer(options?: FluenceConnectionOptions) {
91+
const peerInfo = this.selfPeerId;
92+
const transportKey = Websockets.prototype[Symbol.toStringTag]
8093
this.node = await Peer.create({
8194
peerId: peerInfo,
82-
config: {},
8395
modules: {
8496
transport: [Websockets],
8597
streamMuxer: [Mplex],
8698
connEncryption: [NOISE],
8799
},
100+
config: {
101+
transport: {
102+
[transportKey]: {
103+
filter: allow_all
104+
}
105+
}
106+
},
88107
dialer: {
89108
timeout: options?.dialTimeout,
90109
},
91110
});
92-
93-
await this.startReceiving();
94-
}
95-
96-
isConnected() {
97-
return this.status === Status.Connected;
98111
}
99112

100-
// connection status. If `Disconnected`, it cannot be reconnected
101-
private status: Status = Status.Initializing;
102-
103113
private async startReceiving() {
104114
if (this.status === Status.Initializing) {
105115
await this.node.start();

0 commit comments

Comments
 (0)