Skip to content

Commit

Permalink
deps: update libp2p to release version
Browse files Browse the repository at this point in the history
Makes necessary refactors to use `libp2p@0.40.x`
  • Loading branch information
achingbrain committed Oct 17, 2022
1 parent bc96c4b commit dbd0237
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,25 @@ npm i @libp2p/webrtc
## Usage

```js
import { WebRTCTransport } from '@libp2p/webrtc';
import { multiaddr } from '@multiformats/multiaddr';
import { pipe } from 'it-pipe';
import all from 'it-all';

const webrtc = new WebRTCTransport();
const addr = multiaddr('/ip4/0.0.0.0/udp/56093/webrtc/certhash/uEiByaEfNSLBexWBNFZy_QB1vAKEj7JAXDizRs4_SnTflsQ');
const socket = await webrtc.dial(addr);
const values = await pipe(socket, all);
import { createLibp2pNode } from 'libp2p'
import { webRTC } from '@libp2p/webrtc'
import { noise } from '@chainsafe/libp2p-noise'
import { multiaddr } from '@multiformats/multiaddr'
import { pipe } from 'it-pipe'
import all from 'it-all'

const node = await createLibp2pNode({
transports: [
webRTC()
],
connectionEncryption: [
noise()
]
})

const addr = multiaddr('/ip4/0.0.0.0/udp/56093/webrtc/certhash/uEiByaEfNSLBexWBNFZy_QB1vAKEj7JAXDizRs4_SnTflsQ')
const { stream } = await node.dialProtocol(addr, '/my-protocol/1.0.0')
const values = await pipe(stream, all)
```
## API

Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,23 @@
"release": "aegir release"
},
"devDependencies": {
"@libp2p/interface-mocks": "^6.1.0",
"@libp2p/peer-id-factory": "^1.0.18",
"@libp2p/interface-mocks": "^7.0.2",
"@libp2p/peer-id-factory": "^1.0.19",
"@types/uuid": "^8.3.4",
"@typescript-eslint/parser": "^5.32.0",
"aegir": "^37.4.6",
"chai-bytes": "^0.1.2",
"it-all": "^1.0.6",
"it-first": "^1.0.7",
"libp2p": "^0.39.5",
"it-all": "^2.0.0",
"it-first": "^2.0.0",
"libp2p": "^0.40.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"typescript": "^4.7.4",
"uint8arrays": "^3.1.0",
"uint8arrays": "^4.0.2",
"wait-on": "^6.0.1"
},
"dependencies": {
"@chainsafe/libp2p-noise": "^9.0.0",
"@libp2p/components": "^3.1.1",
"@libp2p/interface-connection": "^3.0.2",
"@libp2p/interface-registrar": "^2.0.3",
"@libp2p/interface-stream-muxer": "^3.0.0",
Expand All @@ -70,7 +69,7 @@
"@protobuf-ts/runtime": "^2.8.0",
"abortable-iterator": "^4.0.2",
"err-code": "^3.0.1",
"it-merge": "^1.0.4",
"it-merge": "^2.0.0",
"multiformats": "^10.0.0",
"multihashes": "^4.0.3",
"p-defer": "^4.0.0",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
export {};
import { Transport } from '@libp2p/interface-transport'
import { WebRTCTransport, WebRTCTransportComponents } from './transport.js'

export function webRTC (): (components: WebRTCTransportComponents) => Transport {
return (components: WebRTCTransportComponents) => new WebRTCTransport(components)
}
22 changes: 9 additions & 13 deletions src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import * as p from '@libp2p/peer-id';
import {WebRTCDialOptions} from './options';
import {WebRTCStream} from './stream';
import {Noise} from '@chainsafe/libp2p-noise';
import {Components, Initializable} from '@libp2p/components';
import {Connection} from '@libp2p/interface-connection';
import type {PeerId} from '@libp2p/interface-peer-id';
import {CreateListenerOptions, Listener, symbol, Transport} from '@libp2p/interface-transport';
import {logger} from '@libp2p/logger';
import {Multiaddr} from '@multiformats/multiaddr';
import {v4 as genUuid} from 'uuid';
import defer, {DeferredPromise} from 'p-defer';
import defer from 'p-defer';
import {fromString as uint8arrayFromString} from 'uint8arrays/from-string';
import {concat} from 'uint8arrays/concat';
import * as multihashes from 'multihashes';
Expand All @@ -22,13 +21,15 @@ import {DataChannelMuxerFactory} from './muxer';
const log = logger('libp2p:webrtc:transport');
const HANDSHAKE_TIMEOUT_MS = 10000;

export class WebRTCTransport implements Transport, Initializable {
private componentsPromise: DeferredPromise<void> = defer();
private components: Components | undefined;
export interface WebRTCTransportComponents {
peerId: PeerId
}

export class WebRTCTransport implements Transport {
private components: WebRTCTransportComponents

init(components: Components): void {
constructor (components: WebRTCTransportComponents) {
this.components = components
this.componentsPromise.resolve()
}

async dial(ma: Multiaddr, options: WebRTCDialOptions): Promise<Connection> {
Expand Down Expand Up @@ -102,7 +103,7 @@ export class WebRTCTransport implements Transport, Initializable {
// wait for peerconnection.onopen to fire, or for the datachannel to open
await dataChannelOpenPromise.promise;

const myPeerId = await this.getPeerId();
const myPeerId = this.components.peerId;
const theirPeerId = p.peerIdFromString(rps);

// do noise handshake
Expand Down Expand Up @@ -173,11 +174,6 @@ export class WebRTCTransport implements Transport, Initializable {

return concat([prefix, ...fps]);
}

public async getPeerId(): Promise<PeerId> {
await this.componentsPromise.promise;
return this.components!.getPeerId();
}
}

const WEBRTC_CODE: number = 280;
Expand Down
34 changes: 18 additions & 16 deletions test/transport.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as underTest from '../src/transport.js';
import {UnimplementedError} from '../src/error.js';
import {Components} from '@libp2p/components';
import {webRTC} from '../src/index.js';
import {mockUpgrader} from '@libp2p/interface-mocks';
import {CreateListenerOptions, symbol} from '@libp2p/interface-transport';
import {multiaddr, Multiaddr} from '@multiformats/multiaddr';
Expand All @@ -10,6 +10,7 @@ import {createLibp2p} from 'libp2p';
import {fromString as uint8arrayFromString} from 'uint8arrays/from-string';
import {pipe} from 'it-pipe';
import first from 'it-first';
import {createEd25519PeerId} from '@libp2p/peer-id-factory';

const {expect, assert} = require('chai').use(require('chai-bytes'));

Expand All @@ -21,19 +22,21 @@ function ignoredDialOption(): CreateListenerOptions {
}

describe('basic transport tests', () => {
let components: underTest.WebRTCTransportComponents

before(async () => {
components = {
peerId: await createEd25519PeerId()
}
})

it('Can construct', () => {
let t = new underTest.WebRTCTransport();
let t = new underTest.WebRTCTransport(components);
expect(t.constructor.name).to.equal('WebRTCTransport');
});

it('init does not throw', () => {
let t = new underTest.WebRTCTransport();
t.init(new Components());
});

it('createListner does throw', () => {
let t = new underTest.WebRTCTransport();
let t = new underTest.WebRTCTransport(components);
try {
t.createListener(ignoredDialOption());
expect('Should have thrown').to.equal('but did not');
Expand All @@ -43,19 +46,19 @@ describe('basic transport tests', () => {
});

it('toString includes the toStringTag', () => {
let t = new underTest.WebRTCTransport();
let t = new underTest.WebRTCTransport(components);
let s = t.toString();
expect(s).to.contain('@libp2p/webrtc');
});

it('toString property getter', () => {
let t = new underTest.WebRTCTransport();
let t = new underTest.WebRTCTransport(components);
let s = t[Symbol.toStringTag];
expect(s).to.equal('@libp2p/webrtc');
});

it('symbol property getter', () => {
let t = new underTest.WebRTCTransport();
let t = new underTest.WebRTCTransport(components);
let s = t[symbol];
expect(s).to.equal(true);
});
Expand All @@ -69,7 +72,7 @@ describe('basic transport tests', () => {
].map((s) => {
return multiaddr(s);
});
let t = new underTest.WebRTCTransport();
let t = new underTest.WebRTCTransport(components);
let result = t.filter(mas);
let expected: Multiaddr[] = [
multiaddr('/ip4/1.2.3.4/udp/1234/webrtc/certhash/uEiAUqV7kzvM1wI5DYDc1RbcekYVmXli_Qprlw3IkiEg6tQ/p2p/12D3KooWGDMwwqrpcYKpKCgxuKT2NfqPqa94QnkoBBpqvCaiCzWd'),
Expand All @@ -83,7 +86,7 @@ describe('basic transport tests', () => {

it('throws appropriate error when dialing someone without a peer ID', async () => {
let ma = multiaddr('/ip4/1.2.3.4/udp/1234/webrtc/certhash/uEiAUqV7kzvM1wI5DYDc1RbcekYVmXli_Qprlw3IkiEg6tQ');
let t = new underTest.WebRTCTransport();
let t = new underTest.WebRTCTransport(components);
try {
let conn = await t.dial(ma, ignoredDialOption());
expect(conn.toString()).to.equal('Should have thrown');
Expand All @@ -105,10 +108,9 @@ describe('Transport interoperability tests', () => {
console.log('Will not test connecting to an external server, as we do not appear to have one.');
return;
}
const tpt = new underTest.WebRTCTransport();
const node = await createLibp2p({
transports: [tpt],
connectionEncryption: [new Noise()],
transports: [webRTC()],
connectionEncryption: [() => new Noise()],
});
await node.start()
const ma = multiaddr(SERVER_MULTIADDR)
Expand Down

0 comments on commit dbd0237

Please sign in to comment.