diff --git a/CHANGELOG.md b/CHANGELOG.md index 644abc4..d4076ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [2.0.10](https://github.com/libp2p/js-libp2p-webrtc/compare/v2.0.9...v2.0.10) (2023-06-12) + + +### Bug Fixes + +* add browser-to-browser test for bi-directional communication ([#172](https://github.com/libp2p/js-libp2p-webrtc/issues/172)) ([1ec3d8a](https://github.com/libp2p/js-libp2p-webrtc/commit/1ec3d8a8b611d5227f430037e2547fd86d115eaa)) + ## [2.0.9](https://github.com/libp2p/js-libp2p-webrtc/compare/v2.0.8...v2.0.9) (2023-06-12) diff --git a/examples/browser-to-browser/tests/test.spec.js b/examples/browser-to-browser/tests/test.spec.js index bd796df..703ae68 100644 --- a/examples/browser-to-browser/tests/test.spec.js +++ b/examples/browser-to-browser/tests/test.spec.js @@ -19,7 +19,6 @@ const sendBtn = '#send' const output = '#output' const listeningAddresses = '#multiaddrs' -const message = 'hello' let url // we spawn a js libp2p relay @@ -68,37 +67,47 @@ test.describe('browser to browser example:', () => { await page.goto(url) }) - test('should connect to a relay node', async ({ page, context }) => { - // first page dials the relay - const relayedAddress = await dialRelay(page, relayNodeAddr) + test('should connect to a relay node', async ({ page: pageA, context }) => { + // load second page + const pageB = await context.newPage() + await pageB.goto(url) - // load second page and use `peer` as the connectAddr - const pageTwo = await context.newPage(); - await pageTwo.goto(url) - await dialPeerOverRelay(pageTwo, relayedAddress) + // connect both pages to the relay + const relayedAddressA = await dialRelay(pageA, relayNodeAddr) + const relayedAddressB = await dialRelay(pageB, relayNodeAddr) + + // dial first page from second page over relay + await dialPeerOverRelay(pageA, relayedAddressB) + await dialPeerOverRelay(pageB, relayedAddressA) // stop the relay await relayNode.stop() - // send the message to the peer over webRTC - await pageTwo.fill(messageInput, message) - await pageTwo.click(sendBtn) + await echoMessagePeer(pageB, 'hello B') - // check the message was echoed back - const outputLocator = pageTwo.locator(output) - await expect(outputLocator).toHaveText(/Sending message/) - await expect(outputLocator).toHaveText(/Received message/, { timeout: 60000 }) + await echoMessagePeer(pageA, 'hello A') }) }) +async function echoMessagePeer (page, message) { + // send the message to the peer over webRTC + await page.fill(messageInput, message) + await page.click(sendBtn) + + // check the message was echoed back + const outputLocator = page.locator(output) + await expect(outputLocator).toContainText(`Sending message '${message}'`) + await expect(outputLocator).toContainText(`Received message '${message}'`) +} + async function dialRelay (page, address) { // add the go libp2p multiaddress to the input field and submit await page.fill(connectAddr, address) await page.click(connectBtn) const outputLocator = page.locator(output) - await expect(outputLocator).toHaveText(/Dialing/) - await expect(outputLocator).toHaveText(/Connected/) + await expect(outputLocator).toContainText(`Dialing '${address}'`) + await expect(outputLocator).toContainText(`Connected to '${address}'`) const multiaddrsLocator = page.locator(listeningAddresses) await expect(multiaddrsLocator).toHaveText(/webrtc/) @@ -115,6 +124,6 @@ async function dialPeerOverRelay (page, address) { await page.click(connectBtn) const outputLocator = page.locator(output) - await expect(outputLocator).toHaveText(/Dialing/) - await expect(outputLocator).toHaveText(/Connected/) + await expect(outputLocator).toContainText(`Dialing '${address}'`) + await expect(outputLocator).toContainText(`Connected to '${address}'`) } diff --git a/package.json b/package.json index d58f473..1864d11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@libp2p/webrtc", - "version": "2.0.9", + "version": "2.0.10", "description": "A libp2p transport using WebRTC connections", "author": "", "license": "Apache-2.0 OR MIT", diff --git a/src/private-to-private/transport.ts b/src/private-to-private/transport.ts index bc3e946..196854d 100644 --- a/src/private-to-private/transport.ts +++ b/src/private-to-private/transport.ts @@ -119,6 +119,9 @@ export class WebRTCTransport implements Transport, Startable { // reset the stream in case of any error signalingStream.reset() throw err + } finally { + // Close the signaling connection + await connection.close() } } @@ -143,6 +146,9 @@ export class WebRTCTransport implements Transport, Startable { } catch (err) { stream.reset() throw err + } finally { + // Close the signaling connection + await connection.close() } } }