Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Feature/refactor and add tests and coverage #60

Merged
merged 19 commits into from
Dec 5, 2022

Conversation

ddimaria
Copy link
Collaborator

@ddimaria ddimaria commented Nov 18, 2022

  • Code-level documentation
  • Code coverage
  • Enhance README
  • More tests
  • Add browser-to-server example

Also, fixes #53

@@ -1,13 +1,6 @@
import errCode from 'err-code'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just alpha sorted the errors

constructor (streamLabel: string, errorMessage: string) {
super(`[stream: ${streamLabel}] data channel error: ${errorMessage}`)
this.name = 'WebRTC/DataChannelError'
export class OverStreamLimitError extends WebRTCTransportError {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Renamed this for consistency

src/error.ts Outdated Show resolved Hide resolved
@ddimaria ddimaria marked this pull request as ready for review December 1, 2022 21:05
@codecov-commenter
Copy link

codecov-commenter commented Dec 1, 2022

Codecov Report

❗ No coverage uploaded for pull request base (develop@b2e4f60). Click here to learn what that means.
Patch has no changes to coverable lines.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop       #60   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         9           
  Lines              ?         9           
  Branches           ?         0           
===========================================
  Hits               ?         9           
  Misses             ?         0           
  Partials           ?         0           
Flag Coverage Δ
chrome 100.00% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@ckousik ckousik left a comment

Choose a reason for hiding this comment

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

This looks good from a preliminary read. I will review it again shortly.


go 1.18

replace github.com/libp2p/go-libp2p => github.com/ckousik/go-libp2p v0.23.3-0.20221029141116-67b7c6290b11
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We will need to update this once the Go PR is merged. I'll add an issue for it.

Copy link

Choose a reason for hiding this comment

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

Link to the issue in a comment perhaps with a big fat TODO?

@ddimaria ddimaria requested a review from GlenDC December 2, 2022 15:04

## Hacking
![https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/interface-connection](https://raw.githubusercontent.com/libp2p/js-libp2p-interfaces/master/packages/interface-connection/img/badge.png)
Copy link

Choose a reason for hiding this comment

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

Suggested change
![https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/interface-connection](https://raw.githubusercontent.com/libp2p/js-libp2p-interfaces/master/packages/interface-connection/img/badge.png)
[![Connection interface for libp2p](https://raw.githubusercontent.com/libp2p/js-libp2p-interfaces/master/packages/interface-connection/img/badge.png)](https://github.com/libp2p/js-libp2p-interfaces/tree/master/packages/interface-connection)

Not more accessible?

```js
interface MultiaddrConnection extends Duplex<Uint8Array> {
close: (err?: Error) => Promise<void>
remoteAddr: Multiaddr
Copy link

Choose a reason for hiding this comment

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

Does it has to be writable or can it be readonly? Same question for the next property.


There is also `npm run autogen` which uses ProtoBuf's protoc to populate the generated code directory `proto_ts` based on `*.proto` files in src. Don't forget to run this step before `build` any time you make a change to any of the `*.proto` files.
class WebRTCMultiaddrConnection implements MultiaddrConnection { }
Copy link

Choose a reason for hiding this comment

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

Is there any value to this example as it:

  • neither implements the connection
  • nor does it get past a decent linter for several reasons

Just asking as people do tend to copy examples as is.

npm run go-libp2p-server
```

Copy the multiaddress in the output.
Copy link

Choose a reason for hiding this comment

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

might be useful to show an example of such output and multiaddress


The browser window will automatically open.
Using the copied multiaddress from the Go server, pasted that value into the `Server MultiAddress` input and click the `Connect` button.
Once the peer is connected, click the message section will appear. Enter a message and click the `Send` button.
Copy link

Choose a reason for hiding this comment

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

Suggested change
Once the peer is connected, click the message section will appear. Enter a message and click the `Send` button.
Once the peer is connected, click the message section once it appeared. Then enter a message and click the `Send` button.

?

import { fromString, toString } from "uint8arrays";
import { webRTC } from 'js-libp2p-webrtc'

let stream;
Copy link

Choose a reason for hiding this comment

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

Is this indention on purpose? o.O

Copy link

Choose a reason for hiding this comment

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

Or did you forget to define (async () => { on top?

Copy link

Choose a reason for hiding this comment

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

Even better, probably only want to do it once you know the DOM is initialised/ready?

connectionEncryption: [() => new Noise()],
});

await node.start()
Copy link

Choose a reason for hiding this comment

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

Does this even run? Since when can you have await in a non-async body, which the global body is AFAIK?

"type": "module",
"scripts": {
"start": "vite",
"go-libp2p-server": "cd ../go-libp2p-server && go build && ./go-libp2p-server"
Copy link

Choose a reason for hiding this comment

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

does npm run your scripts in a new shell? As otherwise this command would change your CWD implicitly, which might be confusing and not expected when the cmd exits?

libp2p.Ping(true),
)
if err != nil {
panic(err)
Copy link

Choose a reason for hiding this comment

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

given you already use log pkg anyway, you might perhaps want to use log.Fatal(err) instead?


/**
* The multiaddr address used to communicate with the remote peer
*/
remoteAddr: Multiaddr;
Copy link

Choose a reason for hiding this comment

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

does this property and the ones below have to be public? If so, do they have to writable? If so, can they be public only as a get(ter)?

log.error('error closing connection', err)
if (err !== undefined) {
log.error('error closing connection', err)
}
Copy link

Choose a reason for hiding this comment

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

might be nice to have a non-err log.debug equivalent for sanity checking while debugging?

src/muxer.ts Outdated Show resolved Hide resolved
readonly protocol: string = '/webrtc'

/**
* Array of streams in the data channel
Copy link

Choose a reason for hiding this comment

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

Any streams? Just the ones alive? Or once in the array always in the array?

// channelOptions?: WebRTCReceiverInit
}

export interface WebRTCListenerOptions extends CreateListenerOptions {}
Copy link

Choose a reason for hiding this comment

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

my linters do not accept such empty extends, I believe better practice is something like:

export { CreateListenerOptions as WebRTCListenerOptions } from '@libp2p/interface-transport'

@@ -76,7 +86,9 @@ export function toSupportedHashFunction (name: multihashes.HashName): string {
}
}

// Convert a multiaddr into a SDP
/**
* Convert a multiaddr into a SDP
Copy link

Choose a reason for hiding this comment

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

Might be nice to write fully what SDP means as <full name> (SDP), small effort but a cognitive ease of mind

export function fromMultiAddr (ma: Multiaddr, ufrag: string): RTCSessionDescriptionInit {
return {
type: 'answer',
sdp: ma2sdp(ma, ufrag)
}
}

// Replace the ufrag and password values in a SDP
/**
* Replace (munge) the ufrag and password values in a SDP
Copy link

Choose a reason for hiding this comment

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

Might just be me reading functions that I require pre-knowledge for, but I stil feels there is a lot of guess work required on my end to get what this function does without reading the actual implementation.

@@ -12,6 +12,9 @@ import * as pb from '../proto_ts/message.js'

const log = logger('libp2p:webrtc:stream')

/**
* Constructs a default StreamStat
Copy link

Choose a reason for hiding this comment

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

might be nice to expand a bit on what these defaults entail?

async dial (ma: Multiaddr, options: WebRTCDialOptions): Promise<Connection> {
const rawConn = await this._connect(ma, options)
log(`dialing address - ${ma.toString()}`)
return rawConn
}

/**
* Create transport listeners no supported by browsers
Copy link

Choose a reason for hiding this comment

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

this sentence reads a big ambiguous and unclear, can you expand it please? E.g. Creating transports listeners is not supported when used from within a browser, or something like that

import { WebRTCMultiaddrConnection } from './../src/maconn'

describe('Multiaddr Connection', () => {
it('can open and close', async () => {
Copy link

Choose a reason for hiding this comment

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

imho it doesn't really test that right? I mean it proves the close property changes when you call close() but it might as well be done using a dummy property which toggles.

Not better to next to what you already do, also write to it, and have it succeed proving you receive the value, with then the the write or so afterwards doesn't work and you instead get some specific error that indicates the conn is closed and that you also confirm the content is indeed not written.

And same for or instead of the reading part?

const maConn = new WebRTCMultiaddrConnection({
peerConnection: peerConnection,
remoteAddr,
timeline: {
Copy link

Choose a reason for hiding this comment

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

why not make this the default value, seems like a sane default if you ask me? Would allow you to omit it here as well

@@ -1,74 +1,68 @@
import * as underTest from '../src/stream'
import { expect, assert } from 'chai'

function setup (): { peerConnection: RTCPeerConnection, datachannel: RTCDataChannel, webrtcStream: underTest.WebRTCStream } {
const peerConnection = new RTCPeerConnection()
const datachannel = peerConnection.createDataChannel('whatever', { negotiated: true, id: 91 })
Copy link

Choose a reason for hiding this comment

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

why not make the data channel id optional and generate a random ID by default? Seems better to me than having to invent strings like this?

Copy link

@GlenDC GlenDC left a comment

Choose a reason for hiding this comment

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

Functionality it looks ok, even though that's harder for me to tell given I Am not yet familiar with libp2p and its specs. So my review is more generic, in that context I Requested some changes but feel free to ignore them all, as none of my comments indicate a functional flaw AFAIC

ddimaria and others added 2 commits December 5, 2022 08:22
Co-authored-by: Glen De Cauwsemaecker <glen.decauwsemaecker@otainsight.com>
Co-authored-by: Glen De Cauwsemaecker <glen.decauwsemaecker@otainsight.com>
@ddimaria ddimaria merged commit 0ea38dc into develop Dec 5, 2022
@github-actions
Copy link

🎉 This PR is included in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update "master" references in CI to use "main"
5 participants