Skip to content

Commit

Permalink
deps: bump it-stream-types from 1.0.5 to 2.0.1 (libp2p#58)
Browse files Browse the repository at this point in the history
* deps: bump it-stream-types from 1.0.5 to 2.0.1

Bumps [it-stream-types](https://github.com/achingbrain/it-stream-types) from 1.0.5 to 2.0.1.
- [Release notes](https://github.com/achingbrain/it-stream-types/releases)
- [Changelog](https://github.com/achingbrain/it-stream-types/blob/master/CHANGELOG.md)
- [Commits](achingbrain/it-stream-types@v1.0.5...v2.0.1)

---
updated-dependencies:
- dependency-name: it-stream-types
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore: fix types

* chore: demand updated handshake

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Alex Potsides <alex@achingbrain.net>
  • Loading branch information
dependabot[bot] and achingbrain authored Apr 18, 2023
1 parent d49203d commit 0b0ebca
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 104 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dist
node_modules
package-lock.json
yarn.lock
.vscode
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ TLDR; multistream-select is protocol multiplexing per connection/stream. [Full s

The caller will send "interactive" messages, expecting for some acknowledgement from the callee, which will "select" the handler for the desired and supported protocol:

```
< /multistream-select/0.3.0 # i speak multistream-select/0.3.0
> /multistream-select/0.3.0 # ok, let's speak multistream-select/0.3.0
> /ipfs-dht/0.2.3 # i want to speak ipfs-dht/0.2.3
< na # ipfs-dht/0.2.3 is not available
> /ipfs-dht/0.1.9 # What about ipfs-dht/0.1.9 ?
< /ipfs-dht/0.1.9 # ok let's speak ipfs-dht/0.1.9 -- in a sense acts as an ACK
> <dht-message>
> <dht-message>
> <dht-message>
```
< /multistream-select/0.3.0 # i speak multistream-select/0.3.0
> /multistream-select/0.3.0 # ok, let's speak multistream-select/0.3.0
> /ipfs-dht/0.2.3 # i want to speak ipfs-dht/0.2.3
< na # ipfs-dht/0.2.3 is not available
> /ipfs-dht/0.1.9 # What about ipfs-dht/0.1.9 ?
< /ipfs-dht/0.1.9 # ok let's speak ipfs-dht/0.1.9 -- in a sense acts as an ACK
> <dht-message>
> <dht-message>
> <dht-message>

## API Docs

Expand Down
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,13 @@
"@libp2p/logger": "^2.0.0",
"abortable-iterator": "^4.0.2",
"it-first": "^3.0.1",
"it-handshake": "^4.1.2",
"it-handshake": "^4.1.3",
"it-length-prefixed": "^9.0.0",
"it-merge": "^3.0.0",
"it-pipe": "^3.0.0",
"it-pushable": "^3.1.0",
"it-reader": "^6.0.1",
"it-stream-types": "^1.0.4",
"p-defer": "^4.0.0",
"it-stream-types": "^2.0.1",
"uint8arraylist": "^2.3.1",
"uint8arrays": "^4.0.2"
},
Expand All @@ -164,10 +163,8 @@
"iso-random-stream": "^2.0.2",
"it-all": "^3.0.1",
"it-map": "^3.0.2",
"it-pair": "^2.0.3",
"it-pair": "^2.0.6",
"p-timeout": "^6.0.0",
"timeout-abort-controller": "^3.0.0",
"util": "^0.12.4",
"varint": "^6.0.0"
}
}
8 changes: 4 additions & 4 deletions src/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { handshake } from 'it-handshake'
import { PROTOCOL_ID } from './constants.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { Uint8ArrayList } from 'uint8arraylist'
import type { Duplex } from 'it-stream-types'
import type { Duplex, Source } from 'it-stream-types'
import type { ByteArrayInit, ByteListInit, MultistreamSelectInit, ProtocolStream } from './index.js'

const log = logger('libp2p:mss:handle')
Expand Down Expand Up @@ -55,9 +55,9 @@ const log = logger('libp2p:mss:handle')
* })
* ```
*/
export async function handle (stream: Duplex<Uint8Array>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>
export async function handle (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
export async function handle (stream: Duplex<any>, protocols: string | string[], options?: MultistreamSelectInit): Promise<ProtocolStream<any>> {
export async function handle (stream: Duplex<Source<Uint8Array>, Source<Uint8Array>>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>
export async function handle (stream: Duplex<Source<Uint8ArrayList | Uint8Array>, Source<Uint8ArrayList | Uint8Array>>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
export async function handle (stream: any, protocols: string | string[], options?: MultistreamSelectInit): Promise<ProtocolStream<any>> {
protocols = Array.isArray(protocols) ? protocols : [protocols]
const { writer, reader, rest, stream: shakeStream } = handshake(stream)

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PROTOCOL_ID } from './constants.js'
import type { Duplex } from 'it-stream-types'
import type { Duplex, Source } from 'it-stream-types'
import type { AbortOptions } from '@libp2p/interfaces'

export { PROTOCOL_ID }

export interface ProtocolStream<TSource, TSink = TSource> {
stream: Duplex<TSource, TSink>
stream: Duplex<AsyncGenerator<TSource>, Source<TSink>>
protocol: string
}

Expand Down
12 changes: 6 additions & 6 deletions src/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as multistream from './multistream.js'
import { handshake } from 'it-handshake'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { PROTOCOL_ID } from './index.js'
import type { Duplex } from 'it-stream-types'
import type { Duplex, Source } from 'it-stream-types'
import { Uint8ArrayList } from 'uint8arraylist'
import { pushable } from 'it-pushable'
import merge from 'it-merge'
Expand Down Expand Up @@ -56,9 +56,9 @@ const log = logger('libp2p:mss:select')
* // }
* ```
*/
export async function select (stream: Duplex<Uint8Array>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>
export async function select (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
export async function select (stream: Duplex<any>, protocols: string | string[], options: MultistreamSelectInit = {}): Promise<ProtocolStream<any>> {
export async function select (stream: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>, protocols: string | string[], options: ByteArrayInit): Promise<ProtocolStream<Uint8Array>>
export async function select (stream: Duplex<AsyncGenerator<Uint8ArrayList | Uint8Array>, Source<Uint8ArrayList | Uint8Array>>, protocols: string | string[], options?: ByteListInit): Promise<ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>>
export async function select (stream: any, protocols: string | string[], options: MultistreamSelectInit = {}): Promise<ProtocolStream<any>> {
protocols = Array.isArray(protocols) ? [...protocols] : [protocols]
const { reader, writer, rest, stream: shakeStream } = handshake(stream)

Expand Down Expand Up @@ -113,8 +113,8 @@ export async function select (stream: Duplex<any>, protocols: string | string[],
*
* Use when it is known that the receiver supports the desired protocol.
*/
export function lazySelect (stream: Duplex<Uint8Array>, protocol: string): ProtocolStream<Uint8Array>
export function lazySelect (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>, protocol: string): ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>
export function lazySelect (stream: Duplex<Source<Uint8Array>, Source<Uint8Array>>, protocol: string): ProtocolStream<Uint8Array>
export function lazySelect (stream: Duplex<Source<Uint8ArrayList | Uint8Array>, Source<Uint8ArrayList | Uint8Array>>, protocol: string): ProtocolStream<Uint8ArrayList, Uint8ArrayList | Uint8Array>
export function lazySelect (stream: Duplex<any>, protocol: string): ProtocolStream<any> {
// This is a signal to write the multistream headers if the consumer tries to
// read from the source
Expand Down
27 changes: 16 additions & 11 deletions test/dialer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import { expect } from 'aegir/chai'
import { pipe } from 'it-pipe'
import all from 'it-all'
import { Uint8ArrayList } from 'uint8arraylist'
import { pair } from './fixtures/pair.js'
import { pair } from 'it-pair'
import { reader } from 'it-reader'
import pTimeout from 'p-timeout'
import randomBytes from 'iso-random-stream/src/random.js'
import * as Multistream from '../src/multistream.js'
import * as mss from '../src/index.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import type { Duplex } from 'it-stream-types'

describe('Dialer', () => {
describe('dialer.select', () => {
it('should select from single protocol', async () => {
const protocol = '/echo/1.0.0'
const duplex = pair()
const duplex = pair<Uint8Array>()

const selection = await mss.select(duplex, protocol)
const selection = await mss.select(duplex, protocol, {
writeBytes: true
})
expect(selection.protocol).to.equal(protocol)

// Ensure stream is usable after selection
Expand All @@ -32,13 +33,17 @@ describe('Dialer', () => {
it('should fail to select twice', async () => {
const protocol = '/echo/1.0.0'
const protocol2 = '/echo/2.0.0'
const duplex = pair()
const duplex = pair<Uint8Array>()

const selection = await mss.select(duplex, protocol)
const selection = await mss.select(duplex, protocol, {
writeBytes: true
})
expect(selection.protocol).to.equal(protocol)

// A second select will timeout
await pTimeout(mss.select(duplex, protocol2), {
await pTimeout(mss.select(duplex, protocol2, {
writeBytes: true
}), {
milliseconds: 1e3
})
.then(() => expect.fail('should have timed out'), (err) => {
Expand All @@ -49,8 +54,8 @@ describe('Dialer', () => {
it('should select from multiple protocols', async () => {
const protocols = ['/echo/2.0.0', '/echo/1.0.0']
const selectedProtocol = protocols[protocols.length - 1]
const stream = pair()
const duplex: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> = {
const stream = pair<Uint8ArrayList | Uint8Array>()
const duplex = {
sink: stream.sink,
source: (async function * () {
const source = reader(stream.source)
Expand Down Expand Up @@ -91,7 +96,7 @@ describe('Dialer', () => {

it('should throw if protocol selection fails', async () => {
const protocol = ['/echo/2.0.0', '/echo/1.0.0']
const stream = pair()
const stream = pair<Uint8ArrayList | Uint8Array>()
const duplex = {
sink: stream.sink,
source: (async function * () {
Expand Down Expand Up @@ -120,7 +125,7 @@ describe('Dialer', () => {
describe('dialer.lazySelect', () => {
it('should lazily select a single protocol', async () => {
const protocol = '/echo/1.0.0'
const duplex = pair()
const duplex = pair<Uint8Array>()

const selection = mss.lazySelect(duplex, protocol)
expect(selection.protocol).to.equal(protocol)
Expand Down
18 changes: 0 additions & 18 deletions test/fixtures/duplex-pair.ts

This file was deleted.

28 changes: 0 additions & 28 deletions test/fixtures/pair.ts

This file was deleted.

21 changes: 8 additions & 13 deletions test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import { expect } from 'aegir/chai'
import { pipe } from 'it-pipe'
import all from 'it-all'
import { Uint8ArrayList } from 'uint8arraylist'
import { duplexPair } from './fixtures/duplex-pair.js'
import randomBytes from 'iso-random-stream/src/random.js'
import * as mss from '../src/index.js'
import { duplexPair as byteStreamPair } from 'it-pair/duplex'
import { duplexPair } from 'it-pair/duplex'

describe('Dialer and Listener integration', () => {
it('should handle and select', async () => {
const protocols = ['/echo/2.0.0', '/echo/1.0.0']
const selectedProtocol = protocols[protocols.length - 1]
const pair = duplexPair()
const pair = duplexPair<Uint8Array | Uint8ArrayList>()

const [dialerSelection, listenerSelection] = await Promise.all([
mss.select(pair[0], protocols),
Expand All @@ -35,7 +34,7 @@ describe('Dialer and Listener integration', () => {
it('should handle, ls and select', async () => {
const protocols = ['/echo/2.0.0', '/echo/1.0.0']
const selectedProtocol = protocols[protocols.length - 1]
const pair = duplexPair()
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

const [listenerSelection, dialerSelection] = await Promise.all([
mss.handle(pair[1], selectedProtocol),
Expand All @@ -57,15 +56,11 @@ describe('Dialer and Listener integration', () => {
it('should handle and select with Uint8Array streams', async () => {
const protocols = ['/echo/2.0.0', '/echo/1.0.0']
const selectedProtocol = protocols[protocols.length - 1]
const pair = byteStreamPair<Uint8Array>()
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

const [dialerSelection, listenerSelection] = await Promise.all([
mss.select(pair[0], protocols, {
writeBytes: true
}),
mss.handle(pair[1], selectedProtocol, {
writeBytes: true
})
mss.select(pair[0], protocols),
mss.handle(pair[1], selectedProtocol)
])

expect(dialerSelection.protocol).to.equal(selectedProtocol)
Expand All @@ -82,7 +77,7 @@ describe('Dialer and Listener integration', () => {

it('should handle and lazySelect', async () => {
const protocol = '/echo/1.0.0'
const pair = duplexPair()
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

const dialerSelection = mss.lazySelect(pair[0], protocol)
expect(dialerSelection.protocol).to.equal(protocol)
Expand All @@ -103,7 +98,7 @@ describe('Dialer and Listener integration', () => {

it('should abort an unhandled lazySelect', async () => {
const protocol = '/echo/1.0.0'
const pair = duplexPair()
const pair = duplexPair<Uint8ArrayList | Uint8Array>()

const dialerSelection = mss.lazySelect(pair[0], protocol)
expect(dialerSelection.protocol).to.equal(protocol)
Expand Down
10 changes: 5 additions & 5 deletions test/listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import * as mss from '../src/index.js'
import map from 'it-map'
import type { Duplex } from 'it-stream-types'
import type { Duplex, Source } from 'it-stream-types'

describe('Listener', () => {
describe('listener.handle', () => {
Expand All @@ -21,7 +21,7 @@ describe('Listener', () => {
const input = [new Uint8ArrayList(randomBytes(10), randomBytes(64), randomBytes(3))]
let output: Uint8ArrayList[] = []

const duplex: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> = {
const duplex: Duplex<AsyncGenerator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>> = {
sink: async source => {
const read = reader(source)
let msg: string
Expand All @@ -37,7 +37,7 @@ describe('Listener', () => {
// Rest is data
output = await all(read)
},
source: (function * () {
source: (async function * () {
yield Multistream.encode(uint8ArrayFromString(mss.PROTOCOL_ID))
yield Multistream.encode(uint8ArrayFromString(protocol))
yield * input
Expand All @@ -59,7 +59,7 @@ describe('Listener', () => {
const input = [new Uint8ArrayList(randomBytes(10), randomBytes(64), randomBytes(3))]
let output: Uint8ArrayList[] = []

const duplex: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> = {
const duplex: Duplex<Generator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>> = {
sink: async source => {
const read = reader(source)
let msg: string
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('Listener', () => {
const input = [new Uint8ArrayList(randomBytes(10), randomBytes(64), randomBytes(3))]
let output: Uint8ArrayList[] = []

const duplex: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> = {
const duplex: Duplex<Generator<Uint8ArrayList>, Source<Uint8ArrayList | Uint8Array>> = {
sink: async source => {
const read = reader(source)
let msg: string
Expand Down

0 comments on commit 0b0ebca

Please sign in to comment.