Skip to content

Commit

Permalink
fix!: release majors of modules that had patches during v1.0 (#2286)
Browse files Browse the repository at this point in the history
Pre-v1.0 versions of libp2p are still depending on post-1.0 modules
which means they have a mixed set of versions.

The change here is to cause those modules to have major releases.

BREAKING CHANGE: requires libp2p v1
  • Loading branch information
achingbrain authored Dec 1, 2023
1 parent d34d330 commit 738dd40
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 49 deletions.
22 changes: 11 additions & 11 deletions packages/multistream-select/src/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import type { Duplex } from 'it-stream-types'
*/
export async function handle <Stream extends Duplex<any, any, any>> (stream: Stream, protocols: string | string[], options: MultistreamSelectInit): Promise<ProtocolStream<Stream>> {
protocols = Array.isArray(protocols) ? protocols : [protocols]
options?.log?.trace('handle: available protocols %s', protocols)
options.log.trace('handle: available protocols %s', protocols)

const lp = lpStream(stream, {
...options,
Expand All @@ -64,21 +64,21 @@ export async function handle <Stream extends Duplex<any, any, any>> (stream: Str
})

while (true) {
options?.log?.trace('handle: reading incoming string')
options.log.trace('handle: reading incoming string')
const protocol = await multistream.readString(lp, options)
options?.log?.trace('handle: read "%s"', protocol)
options.log.trace('handle: read "%s"', protocol)

if (protocol === PROTOCOL_ID) {
options?.log?.trace('handle: respond with "%s" for "%s"', PROTOCOL_ID, protocol)
options.log.trace('handle: respond with "%s" for "%s"', PROTOCOL_ID, protocol)
await multistream.write(lp, uint8ArrayFromString(`${PROTOCOL_ID}\n`), options)
options?.log?.trace('handle: responded with "%s" for "%s"', PROTOCOL_ID, protocol)
options.log.trace('handle: responded with "%s" for "%s"', PROTOCOL_ID, protocol)
continue
}

if (protocols.includes(protocol)) {
options?.log?.trace('handle: respond with "%s" for "%s"', protocol, protocol)
options.log.trace('handle: respond with "%s" for "%s"', protocol, protocol)
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
options?.log?.trace('handle: responded with "%s" for "%s"', protocol, protocol)
options.log.trace('handle: responded with "%s" for "%s"', protocol, protocol)

return { stream: lp.unwrap(), protocol }
}
Expand All @@ -90,14 +90,14 @@ export async function handle <Stream extends Duplex<any, any, any>> (stream: Str
uint8ArrayFromString('\n')
)

options?.log?.trace('handle: respond with "%s" for %s', protocols, protocol)
options.log.trace('handle: respond with "%s" for %s', protocols, protocol)
await multistream.write(lp, protos, options)
options?.log?.trace('handle: responded with "%s" for %s', protocols, protocol)
options.log.trace('handle: responded with "%s" for %s', protocols, protocol)
continue
}

options?.log?.('handle: respond with "na" for "%s"', protocol)
options.log('handle: respond with "na" for "%s"', protocol)
await multistream.write(lp, uint8ArrayFromString('na\n'), options)
options?.log?.('handle: responded with "na" for "%s"', protocol)
options.log('handle: responded with "na" for "%s"', protocol)
}
}
11 changes: 5 additions & 6 deletions packages/multistream-select/src/multistream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CodeError } from '@libp2p/interface'
import { type Uint8ArrayList } from 'uint8arraylist'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import type { MultistreamSelectInit } from '.'
import type { AbortOptions, LoggerOptions } from '@libp2p/interface'
import type { LengthPrefixedStream } from 'it-length-prefixed-stream'
import type { Duplex, Source } from 'it-stream-types'
Expand All @@ -12,25 +11,25 @@ const NewLine = uint8ArrayFromString('\n')
/**
* `write` encodes and writes a single buffer
*/
export async function write (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffer: Uint8Array | Uint8ArrayList, options?: MultistreamSelectInit): Promise<void> {
export async function write (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffer: Uint8Array | Uint8ArrayList, options?: AbortOptions): Promise<void> {
await writer.write(buffer, options)
}

/**
* `writeAll` behaves like `write`, except it encodes an array of items as a single write
*/
export async function writeAll (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffers: Uint8Array[], options?: MultistreamSelectInit): Promise<void> {
export async function writeAll (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffers: Uint8Array[], options?: AbortOptions): Promise<void> {
await writer.writeV(buffers, options)
}

/**
* Read a length-prefixed buffer from the passed stream, stripping the final newline character
*/
export async function read (reader: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, options?: AbortOptions & LoggerOptions): Promise<Uint8ArrayList> {
export async function read (reader: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, options: AbortOptions & LoggerOptions): Promise<Uint8ArrayList> {
const buf = await reader.read(options)

if (buf.byteLength === 0 || buf.get(buf.byteLength - 1) !== NewLine[0]) {
options?.log?.error('Invalid mss message - missing newline', buf)
options.log.error('Invalid mss message - missing newline', buf)
throw new CodeError('missing newline', 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
}

Expand All @@ -40,7 +39,7 @@ export async function read (reader: LengthPrefixedStream<Duplex<AsyncGenerator<U
/**
* Read a length-prefixed string from the passed stream, stripping the final newline character
*/
export async function readString (reader: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, options?: AbortOptions & LoggerOptions): Promise<string> {
export async function readString (reader: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, options: AbortOptions & LoggerOptions): Promise<string> {
const buf = await read(reader, options)

return uint8ArrayToString(buf.subarray())
Expand Down
42 changes: 21 additions & 21 deletions packages/multistream-select/src/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ export async function select <Stream extends SelectStream> (stream: Stream, prot
throw new Error('At least one protocol must be specified')
}

options?.log?.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol)
options.log.trace('select: write ["%s", "%s"]', PROTOCOL_ID, protocol)
const p1 = uint8ArrayFromString(`${PROTOCOL_ID}\n`)
const p2 = uint8ArrayFromString(`${protocol}\n`)
await multistream.writeAll(lp, [p1, p2], options)

options?.log?.trace('select: reading multistream-select header')
options.log.trace('select: reading multistream-select header')
let response = await multistream.readString(lp, options)
options?.log?.trace('select: read "%s"', response)
options.log.trace('select: read "%s"', response)

// Read the protocol response if we got the protocolId in return
if (response === PROTOCOL_ID) {
options?.log?.trace('select: reading protocol response')
options.log.trace('select: reading protocol response')
response = await multistream.readString(lp, options)
options?.log?.trace('select: read "%s"', response)
options.log.trace('select: read "%s"', response)
}

// We're done
Expand All @@ -101,11 +101,11 @@ export async function select <Stream extends SelectStream> (stream: Stream, prot

// We haven't gotten a valid ack, try the other protocols
for (const protocol of protocols) {
options?.log?.trace('select: write "%s"', protocol)
options.log.trace('select: write "%s"', protocol)
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
options?.log?.trace('select: reading protocol response')
options.log.trace('select: reading protocol response')
const response = await multistream.readString(lp, options)
options?.log?.trace('select: read "%s" for "%s"', response, protocol)
options.log.trace('select: read "%s" for "%s"', response, protocol)

if (response === protocol) {
return { stream: lp.unwrap(), protocol }
Expand Down Expand Up @@ -163,7 +163,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
if (!sentProtocol) {
sendingProtocol = true

options?.log?.trace('optimistic: write ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength)
options.log.trace('optimistic: write ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength)

const protocolString = `${protocol}\n`

Expand All @@ -176,7 +176,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
buf
).subarray()

options?.log?.trace('optimistic: wrote ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength)
options.log.trace('optimistic: wrote ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength)

sentProtocol = true
sendingProtocol = false
Expand All @@ -198,7 +198,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco

async function negotiate (): Promise<void> {
if (negotiating) {
options?.log?.trace('optimistic: already negotiating %s stream', protocol)
options.log.trace('optimistic: already negotiating %s stream', protocol)
await doneNegotiating.promise
return
}
Expand All @@ -208,13 +208,13 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
try {
// we haven't sent the protocol yet, send it now
if (!sentProtocol) {
options?.log?.trace('optimistic: doing send protocol for %s stream', protocol)
options.log.trace('optimistic: doing send protocol for %s stream', protocol)
await doSendProtocol()
}

// if we haven't read the protocol response yet, do it now
if (!readProtocol) {
options?.log?.trace('optimistic: doing read protocol for %s stream', protocol)
options.log.trace('optimistic: doing read protocol for %s stream', protocol)
await doReadProtocol()
}
} finally {
Expand All @@ -233,12 +233,12 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
sendingProtocol = true

try {
options?.log?.trace('optimistic: write ["%s", "%s", data] in source', PROTOCOL_ID, protocol)
options.log.trace('optimistic: write ["%s", "%s", data] in source', PROTOCOL_ID, protocol)
await lp.writeV([
uint8ArrayFromString(`${PROTOCOL_ID}\n`),
uint8ArrayFromString(`${protocol}\n`)
])
options?.log?.trace('optimistic: wrote ["%s", "%s", data] in source', PROTOCOL_ID, protocol)
options.log.trace('optimistic: wrote ["%s", "%s", data] in source', PROTOCOL_ID, protocol)
} finally {
sentProtocol = true
sendingProtocol = false
Expand All @@ -255,15 +255,15 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
readingProtocol = true

try {
options?.log?.trace('optimistic: reading multistream select header')
options.log.trace('optimistic: reading multistream select header')
let response = await multistream.readString(lp, options)
options?.log?.trace('optimistic: read multistream select header "%s"', response)
options.log.trace('optimistic: read multistream select header "%s"', response)

if (response === PROTOCOL_ID) {
response = await multistream.readString(lp, options)
}

options?.log?.trace('optimistic: read protocol "%s", expecting "%s"', response, protocol)
options.log.trace('optimistic: read protocol "%s", expecting "%s"', response, protocol)

if (response !== protocol) {
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL')
Expand All @@ -279,7 +279,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
// make sure we've done protocol negotiation before we read stream data
await negotiate()

options?.log?.trace('optimistic: reading data from "%s" stream', protocol)
options.log.trace('optimistic: reading data from "%s" stream', protocol)
yield * lp.unwrap().source
})()

Expand All @@ -291,7 +291,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
// this before closing the readable end of the stream
if (!negotiated) {
await negotiate().catch(err => {
options?.log?.error('could not negotiate protocol before close read', err)
options.log.error('could not negotiate protocol before close read', err)
})
}

Expand All @@ -308,7 +308,7 @@ function optimisticSelect <Stream extends SelectStream> (stream: Stream, protoco
// this before closing the writable end of the stream
if (!negotiated) {
await negotiate().catch(err => {
options?.log?.error('could not negotiate protocol before close write', err)
options.log.error('could not negotiate protocol before close write', err)
})
}

Expand Down
20 changes: 13 additions & 7 deletions packages/multistream-select/test/multistream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ describe('Multistream', () => {
const inputStream = lpStream(duplexes[0])
const outputStream = lpStream(duplexes[1])

void Multistream.write(inputStream, input, {
log: logger('mss:test')
})
void Multistream.write(inputStream, input)

const output = await outputStream.read()
expect(output.subarray()).to.equalBytes(input)
Expand All @@ -36,7 +34,9 @@ describe('Multistream', () => {

void inputStream.write(uint8ArrayFromString(`${input}\n`))

const output = await Multistream.read(outputStream)
const output = await Multistream.read(outputStream, {
log: logger('mss:test')
})
expect(output.subarray()).to.equalBytes(inputBuf)
})

Expand All @@ -50,7 +50,9 @@ describe('Multistream', () => {

void inputStream.write(inputBuf)

await expect(Multistream.read(outputStream)).to.eventually.be.rejected()
await expect(Multistream.read(outputStream, {
log: logger('mss:test')
})).to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
})

Expand All @@ -66,7 +68,9 @@ describe('Multistream', () => {

void inputStream.write(input)

await expect(Multistream.read(outputStream)).to.eventually.be.rejected()
await expect(Multistream.read(outputStream, {
log: logger('mss:test')
})).to.eventually.be.rejected()
.with.property('code', 'ERR_MSG_DATA_TOO_LONG')
})

Expand All @@ -79,7 +83,9 @@ describe('Multistream', () => {

void inputStream.write(input)

await expect(Multistream.read(outputStream)).to.eventually.be.rejected()
await expect(Multistream.read(outputStream, {
log: logger('mss:test')
})).to.eventually.be.rejected()
.with.property('code', 'ERR_INVALID_MULTISTREAM_SELECT_MESSAGE')
})

Expand Down
27 changes: 27 additions & 0 deletions packages/peer-collections/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@
* We can't use PeerIds as collection keys because collection keys are compared using same-value-zero equality, so this is just a group of collections that stringifies PeerIds before storing them.
*
* PeerIds cache stringified versions of themselves so this should be a cheap operation.
*
* @example Peer lists
*
* ```JavaScript
* import { peerList } from '@libp2p/peer-collections'
*
* const list = peerList()
* list.push(peerId)
* ```
*
* @example Peer maps
*
* ```JavaScript
* import { peerMap } from '@libp2p/peer-collections'
*
* const map = peerMap<string>()
* map.set(peerId, 'value')
* ```
*
* @example Peer sets
*
* ```JavaScript
* import { peerSet } from '@libp2p/peer-collections'
*
* const set = peerSet()
* set.add(peerId)
* ```
*/

export { PeerMap } from './map.js'
Expand Down
2 changes: 1 addition & 1 deletion packages/peer-id-factory/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @packageDocumentation
*
* Generate, import, and export PeerIDs, for use with [IPFS](https://github.com/ipfs/ipfs).
* Generate, import, and export PeerIDs.
*
* A Peer ID is the SHA-256 [multihash](https://github.com/multiformats/multihash) of a public key.
*
Expand Down
4 changes: 2 additions & 2 deletions packages/peer-record/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* You can read further about the envelope in [libp2p/specs#217](https://github.com/libp2p/specs/pull/217).
*
* @example
* @example Creating a peer record
*
* Create an envelope with an instance of an [interface-record](https://github.com/libp2p/js-libp2p/blob/main/packages/interface/src/record/index.ts) implementation and prepare it for being exchanged:
*
Expand Down Expand Up @@ -42,7 +42,7 @@
* const wireData = e.marshal()
* ```
*
* @example
* @example Consuming a peer record
*
* Consume a received envelope (`wireData`) and transform it back to a record:
*
Expand Down
6 changes: 6 additions & 0 deletions packages/peer-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @packageDocumentation
*
* The peer store is where libp2p stores data about the peers it has encountered on the network.
*/

import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
import all from 'it-all'
import { PersistentStore, type PeerUpdate } from './store.js'
Expand Down
2 changes: 1 addition & 1 deletion packages/pubsub-floodsub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Instead please use [gossipsub](https://www.npmjs.com/package/@chainsafe/libp2p-gossipsub) - a more complete implementation which is also compatible with floodsub.
*
* @example
* @example Configuring libp2p to use floodsub
*
* ```JavaScript
* import { createLibp2pNode } from 'libp2p'
Expand Down
1 change: 1 addition & 0 deletions packages/pubsub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* A set of components to be extended in order to create a pubsub implementation.
*
* @example
*
* ```javascript
* import { PubSubBaseProtocol } from '@libp2p/pubsub'
*
Expand Down

0 comments on commit 738dd40

Please sign in to comment.