Last active
May 13, 2019 15:37
-
-
Save vasco-santos/a3edcf2bbb17e6ca62fff07be256e17b to your computer and use it in GitHub Desktop.
js-libp2p stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ROLE = { | |
INITIATOR: 0, | |
RESPONDER: 1 | |
} | |
class Stream { | |
constructor(iterableDuplex, connection, isInitiator = true) { | |
/** | |
* Stream identifier | |
*/ | |
this.id = (~~(Math.random() * 1e9)).toString(36) + Date.now() | |
/** | |
* Streaming iterable duplex object | |
*/ | |
this._iterableDuplex = iterableDuplex | |
/** | |
* Stream parent connection | |
*/ | |
this.connection = connection | |
/** | |
* Role in the stream, initiator or responder | |
*/ | |
this.role = isInitiator ? ROLE.INITIATOR : ROLE.RESPONDER | |
/** | |
* Stream timeline | |
*/ | |
this.timeline = { | |
openTs: Date.now(), | |
closeTs: undefined | |
} | |
/** | |
* User provided tags | |
*/ | |
this.tags = [] | |
/** | |
* Stream protocol | |
*/ | |
this._protocol = undefined | |
} | |
sink () { | |
} | |
source () { | |
} | |
close () { | |
} | |
} |
Thanks for the feedback @jacobheun , I now agree with you regarding the protocol!
Updated the gist code according to the suggestions
this.connectionId = connectionId
Any reason to not just make this the actual connection reference? Just thinking about usability here. If I have a stream and want to get the connection, I'll have to take the connectionId
and then look up the connection via the Switch. I don't think it's critical, just a potential usability improvement.
No reason, I will change to the connection reference!
@alanshaw do you envision sink
and source
as you have in js-libp2p-websockets
?
In this case, we need to pass the options to this stream class for the abortable, and in newStream
for the connection.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Correct, but this is at a higher level. The flow of this might look like:
So while the user doesn't care, if we are returning a
Stream
from the multiplexer, it won't know about the protocol yet. I haven't thought through the multistream api too much yet, so that could potentially be improved as well.We could just use the same logic webrtc-star uses for intent ids. The timestamp, plus the random string should have a pretty reliable non collision rate.