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

refactor: use async / await #52

Merged
merged 12 commits into from
Sep 26, 2019
Merged

refactor: use async / await #52

merged 12 commits into from
Sep 26, 2019

Conversation

jacobheun
Copy link
Contributor

@jacobheun jacobheun commented Sep 20, 2019

This PR merges in the async/await refactor from https://github.com/alanshaw/it-multistream-select.

Notable changes from it-multistream-select are the repo references, example code referencing to it-mplex and updates to package.json and the readme to make them comply with the multistream-select repo.

Note: This does currently reference a few github tags as dependencies, rather than creating temporary npm releases of the modules. Once the PRs are merged & released we can bump the versions here.

alanshaw and others added 10 commits September 20, 2019 14:13
* feat: dream API for multistream-select

Here's some dream code! This is how the API could look in an async/await future. I would love to know what you think and if you have questions.

I **think** I have all my assumptions correct about how the current implementation works and is used 🤣!

* feat: select method

As per the dream code this function allows selecting a protocol from the passed list of protocols using the provided muxed stream.

We just iterate over the passed protocols and...

1. Write to the muxed stream sink with (length prefixed) protocol
2. Read response from muxed stream source
    * We use `it-reader` and `it-length-prefixed` and read single bytes until we have the whole response
    * I understand now why `pull-length-prefixed` has a `decodeFromReader` function and I'll add that to `it-length-prefixed` and use it here
        * TLDR; We only need to read single bytes until we have a valid varint!
3. Check if response is expected protocol (not "na")
4. If expected then return stream and protocol, if not then iterate

The interesting bit is where we construct the `selectedStream` (the stream we return to the caller). We need to use the sink from the muxed stream to write our multistream protocol(s) and _then_ we need to allow it to be used by whoever needs it. This is just a concat of two sources, but the tricky bit is that the second is not known until after we return the selected stream to the caller. Line 20-24 pass a source to the muxed stream sink that iterates over our writer stream and then waits for a source to be provided to the `selectedStream` sink. The `selectedStream` sink function just resolves this promise and the content from this source is then yielded from the source we passed to the muxed stream's sink.

The source for the `selectedStream` is just the `reader`. You can think of it as just a wrapper around the muxed stream's source. It is used to read responses during multistream negotiation but can then continue to be used by whoever needs to read the rest of the stream. This is because it's just an iterable who's next function optionally takes a number of bytes to read. See the [example](https://github.com/alanshaw/it-reader#usage) for more info.

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* feat: handle and ls

This PR supersedes #1 and #2 and adds `ls` and `handle` on top of `select` and adds 100% test coverage:

<img width="1281" alt="Screenshot 2019-08-14 at 12 21 52" src="https://user-images.githubusercontent.com/152863/63018364-89eba280-be90-11e9-833e-db5097b092c0.png">

API has changed a little from #2 because we actually do need to keep some state around for `ls`.

If you use `ls` you need to remember you've done the multistream-select handshake so that you can call `select` afterwards.

So the API is now:

```js
const dialer = new MSS.Dialer(muxedStream)
const protocols = await dialer.ls() // optional!
const { stream, protocol } = await dialer.select('/echo/1.0.0')

// --- and ---

const listener = new MSS.Listener(muxedStream)
const { stream, protocol } = await listener.handle('/echo/1.0.0')
```

This work also uncovered a possible bug in `go-multistream`  multiformats/go-multistream#41

I also addressed feedback from #2 here.

If this is approved I'll open a PR to `js-multistream-select`.

closes #1
closes #2

cc @jacobheun @vasco-santos @daviddias

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* fix: fixes and tests

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* docs: remove ramblings from README

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>

* docs: fix comment

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* fix: listener and dialer selection results

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
Copy link
Member

@vasco-santos vasco-santos 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 really great! Awesome work @alanshaw @jacobheun

Just left minor suggestions

README.md Outdated Show resolved Hide resolved
test/dialer.spec.js Show resolved Hide resolved
test/dialer.spec.js Show resolved Hide resolved
@jacobheun jacobheun merged commit 3d9cff9 into master Sep 26, 2019
@jacobheun jacobheun deleted the refactor/async-await branch September 26, 2019 12:20
@alanshaw
Copy link
Member

@jacobheun dependencies updated - enjoy 👍

jacobheun added a commit that referenced this pull request Oct 10, 2019
BREAKING CHANGE: The API has changed from callbacks to async/await. Additionally, the use has changed. See the README for usage guidance.

Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
jacobheun pushed a commit that referenced this pull request Oct 10, 2019
BREAKING CHANGE: The API has changed from callbacks to async/await. Additionally, the use has changed. See the README for usage guidance.
@jacobheun jacobheun mentioned this pull request Oct 10, 2019
jacobheun added a commit that referenced this pull request Oct 11, 2019
* refactor: use async / await (#52)

BREAKING CHANGE: The API has changed from callbacks to async/await. Additionally, the use has changed. See the README for usage guidance.

* feat: consolidate select write (#53)

* chore: update deps
* chore: remove package lock

* chore: update contributors

* chore: release version v0.15.0

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

Successfully merging this pull request may close these issues.

3 participants