Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Last active May 13, 2019 15:30
Show Gist options
  • Save vasco-santos/bc89fe6e0acf8186f1137576135fbd49 to your computer and use it in GitHub Desktop.
Save vasco-santos/bc89fe6e0acf8186f1137576135fbd49 to your computer and use it in GitHub Desktop.
js-libp2p connection
const STATUS = {
OPEN: 0,
CLOSED: 1,
CLOSING: 2,
}
const ROLE = {
INITIATOR: 0,
RESPONDER: 1
}
class Connection {
constructor (peerInfo, remoteMa, isInitiator) {
/**
* Connection identifier
*/
this.id = (~~(Math.random() * 1e9)).toString(36) + Date.now()
/**
* Remote peer info
*/
this.peerInfo = peerInfo
/**
* Status of the connection
*/
this.status = STATUS.OPEN
/**
* Endpoints multiaddrs
*/
this.endpoints = {
local: undefined,
remote: remoteMa
}
/**
* Connection timeline
*/
this.timeline = {
openTs: Date.now(),
closeTs: undefined
}
/**
* Role in the connection, initiator or responder
*/
this.role = isInitiator ? ROLE.INITIATOR : ROLE.RESPONDER
/**
* The multiplexer being used
*/
this.multiplexer = undefined
/**
* The encryption method being used
*/
this.encryption = undefined
/**
* Connection streams
*/
this.streams = []
/**
* User provided tags
*/
this.tags = []
}
newStream () {
if (!this.multiplexer) {
// await hasMultiplexerOrErrored() // magic ensues
}
// ... new stream creation
}
getStreams () {
}
close () {
this.timeline.closeTs = Date.now()
}
setLocalAddress (ma) {
this.endpoints.local = ma
}
}
@jacobheun
Copy link

Ok, I agree with setting those properties by the responsible parties. Would you expect API methods for it, or directly modified? I am thinking about the best way to guarantee that it is not forgotten.

This should be internal logic to the Switch, so we wouldn't have to worry about users setting it. Each new connection would get passed to the upgrader, and would end up with all properties set (assuming everything worked).

Considering that we have a connEncryption module in the js-libp2p and we want to be able to see what encryption is being used in the introspection I think we need to keep it. Do you see a way to infer it?

So would this just be the protocol for encryption then? ie: "/secio/1.0.0" If so, I wonder if connection.tags is a better place to record that?

@vasco-santos
Copy link
Author

So would this just be the protocol for encryption then? ie: "/secio/1.0.0" If so, I wonder if connection.tags is a better place to record that?

Yes, it would be the encryption protocol! I only prefer to have its own property because it should be always present and the tags would probably be an array of string with no context (the encryption should be always easy to get from the connection)

@jacobheun
Copy link

I only prefer to have its own property because it should be always present and the tags would probably be an array of string with no context

Fair enough. We can always revisit when we add more than 1 encryption. Right now if the switch has encryption turned on it's going to be secio, otherwise it's plaintext. Once tls or quic support is added it will become more important for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment