diff --git a/package-lock.json b/package-lock.json index c51bece7..25688146 100644 --- a/package-lock.json +++ b/package-lock.json @@ -435,9 +435,9 @@ } }, "@fluencelabs/aquamarine-interpreter": { - "version": "0.7.9", - "resolved": "https://registry.npmjs.org/@fluencelabs/aquamarine-interpreter/-/aquamarine-interpreter-0.7.9.tgz", - "integrity": "sha512-VXbHm0d05XMjTSzOTcb+spVRrIuMcrw8/3dl197wH0jx1C3Wou+vAapQLvGNcKzqDhktPOOzJTE4UARYd0lFMw==" + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@fluencelabs/aquamarine-interpreter/-/aquamarine-interpreter-0.8.0.tgz", + "integrity": "sha512-EoHqReMk6gqcBR8ZJuurXbCyB3t6vrOjsEBX5XgVhdfSYrUvmBvmKy4Elq5ONY2JVec9HCR/n/SfTuxYsnY8PQ==" }, "@istanbuljs/load-nyc-config": { "version": "1.1.0", diff --git a/package.json b/package.json index dd659f5a..10835ff7 100644 --- a/package.json +++ b/package.json @@ -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", + "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", diff --git a/src/__test__/unit/WsTransport.spec.ts b/src/__test__/unit/WsTransport.spec.ts new file mode 100644 index 00000000..70e99186 --- /dev/null +++ b/src/__test__/unit/WsTransport.spec.ts @@ -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(); + }); +}); diff --git a/src/internal/FluenceConnection.ts b/src/internal/FluenceConnection.ts index f5c81156..7b85d618 100644 --- a/src/internal/FluenceConnection.ts +++ b/src/internal/FluenceConnection.ts @@ -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'; @@ -76,20 +76,7 @@ export class FluenceConnection { } async connect(options?: FluenceConnectionOptions) { - let peerInfo = this.selfPeerId; - this.node = await Peer.create({ - peerId: peerInfo, - config: {}, - modules: { - transport: [Websockets], - streamMuxer: [Mplex], - connEncryption: [NOISE], - }, - dialer: { - timeout: options?.dialTimeout, - }, - }); - + await this.createPeer(options); await this.startReceiving(); } @@ -100,6 +87,29 @@ export class FluenceConnection { // 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, + modules: { + transport: [Websockets], + streamMuxer: [Mplex], + connEncryption: [NOISE], + }, + config: { + transport: { + [transportKey]: { + filter: allow_all + } + } + }, + dialer: { + timeout: options?.dialTimeout, + }, + }); + } + private async startReceiving() { if (this.status === Status.Initializing) { await this.node.start();