Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Last active May 13, 2019 15:37
Show Gist options
  • Save vasco-santos/a3edcf2bbb17e6ca62fff07be256e17b to your computer and use it in GitHub Desktop.
Save vasco-santos/a3edcf2bbb17e6ca62fff07be256e17b to your computer and use it in GitHub Desktop.
js-libp2p stream
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 () {
}
}
@vasco-santos
Copy link
Author

Thanks for the feedback @jacobheun , I now agree with you regarding the protocol!

Updated the gist code according to the suggestions

@jacobheun
Copy link

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.

@vasco-santos
Copy link
Author

No reason, I will change to the connection reference!

@vasco-santos
Copy link
Author

@alanshaw do you envision sink and source as you have in js-libp2p-websockets?

https://github.com/libp2p/js-libp2p-websockets/pull/82/files#diff-1fdf421c05c1140f6d71444ea2b27638R47

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