Skip to content

Commit 32627c8

Browse files
authored
docs: update comments in interface module and elsewhere (#3107)
Fixes #2112
1 parent 213a54a commit 32627c8

File tree

26 files changed

+254
-41
lines changed

26 files changed

+254
-41
lines changed

packages/config/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ repo and examine the changes made.
2424
2525
-->
2626

27+
Utilities to make working with libp2p configuration simpler.
28+
2729
## Example - Load or create the "self" private key in a datastore
2830

2931
Most nodes will want to persist the same private key between restarts so this
@@ -33,7 +35,7 @@ will create a new one and save it in the keystore.
3335
The options you pass to this function should be the same as those passed to
3436
the `@libp2p/keychain` service you configure your node with.
3537

36-
```ts
38+
```TypeScript
3739
import { loadOrCreateSelfKey } from '@libp2p/config'
3840
import { keychain } from '@libp2p/keychain'
3941
import { LevelDatastore } from 'datastore-level'

packages/config/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* @packageDocumentation
33
*
4+
* Utilities to make working with libp2p configuration simpler.
5+
*
46
* @example Load or create the "self" private key in a datastore
57
*
68
* Most nodes will want to persist the same private key between restarts so this
@@ -10,7 +12,7 @@
1012
* The options you pass to this function should be the same as those passed to
1113
* the `@libp2p/keychain` service you configure your node with.
1214
*
13-
* ```ts
15+
* ```TypeScript
1416
* import { loadOrCreateSelfKey } from '@libp2p/config'
1517
* import { keychain } from '@libp2p/keychain'
1618
* import { LevelDatastore } from 'datastore-level'

packages/connection-encrypter-plaintext/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ repo and examine the changes made.
2424
2525
-->
2626

27-
A connection encrypter that does no connection encryption.
27+
A connection encrypter that does no connection encryption and trusts the
28+
remote peer to provide the correct PeerId.
2829

29-
This should not be used in production should be used for research purposes only.
30+
This should not be used in production and is for research purposes only.
3031

3132
## Example
3233

packages/connection-encrypter-plaintext/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
22
* @packageDocumentation
33
*
4-
* A connection encrypter that does no connection encryption.
4+
* A connection encrypter that does no connection encryption and trusts the
5+
* remote peer to provide the correct PeerId.
56
*
6-
* This should not be used in production should be used for research purposes only.
7+
* This should not be used in production and is for research purposes only.
78
*
89
* @example
910
*

packages/crypto/src/ciphers/aes-gcm.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { concat } from 'uint8arrays/concat'
22
import { fromString } from 'uint8arrays/from-string'
33
import webcrypto from '../webcrypto/index.js'
4-
import type { CreateOptions, AESCipher } from './interface.js'
4+
import type { CreateAESCipherOptions, AESCipher } from './interface.js'
55

66
// WebKit on Linux does not support deriving a key from an empty PBKDF2 key.
77
// So, as a workaround, we provide the generated key as a constant. We test that
@@ -24,7 +24,7 @@ export const derivedEmptyPasswordKey = {
2424

2525
// Based off of code from https://github.com/luke-park/SecureCompatibleEncryptionExamples
2626

27-
export function create (opts?: CreateOptions): AESCipher {
27+
export function create (opts?: CreateAESCipherOptions): AESCipher {
2828
const algorithm = opts?.algorithm ?? 'AES-GCM'
2929
let keyLength = opts?.keyLength ?? 16
3030
const nonceLength = opts?.nonceLength ?? 12

packages/crypto/src/ciphers/aes-gcm.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import crypto from 'crypto'
22
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
33
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4-
import type { CreateOptions, AESCipher } from './interface.js'
4+
import type { CreateAESCipherOptions, AESCipher } from './interface.js'
5+
6+
export type { AESCipher, CreateAESCipherOptions }
57

68
// Based off of code from https://github.com/luke-park/SecureCompatibleEncryptionExamples
79

8-
export function create (opts?: CreateOptions): AESCipher {
10+
export function create (opts?: CreateAESCipherOptions): AESCipher {
911
const algorithm = opts?.algorithm ?? 'aes-128-gcm'
1012
const keyLength = opts?.keyLength ?? 16
1113
const nonceLength = opts?.nonceLength ?? 12

packages/crypto/src/ciphers/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface CreateOptions {
1+
export interface CreateAESCipherOptions {
22
algorithm?: string
33
nonceLength?: number
44
keyLength?: number
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import Sinon from 'sinon'
22
import type { PeerId } from '@libp2p/interface'
33
import type { Multiaddr } from '@multiformats/multiaddr'
4+
import type { SinonMatcher } from 'sinon'
45

56
/**
67
* @deprecated PeerIds can be passed to sinon matchers directly
78
*/
8-
export function matchPeerId (peerId: PeerId): Sinon.SinonMatcher {
9+
export function matchPeerId (peerId: PeerId): SinonMatcher {
910
return Sinon.match(p => p.toString() === peerId.toString())
1011
}
1112

1213
/**
1314
* @deprecated Multiaddrs can be passed to sinon matchers directly
1415
*/
15-
export function matchMultiaddr (ma: Multiaddr): Sinon.SinonMatcher {
16+
export function matchMultiaddr (ma: Multiaddr): SinonMatcher {
1617
return Sinon.match(m => m.toString() === ma.toString())
1718
}

packages/interface/src/content-routing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CID } from 'multiformats/cid'
44

55
/**
66
* Any object that implements this Symbol as a property should return a
7-
* ContentRouting instance as the property value, similar to how
7+
* Partial<ContentRouting> instance as the property value, similar to how
88
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
99
*
1010
* @example
@@ -28,7 +28,7 @@ export const contentRoutingSymbol = Symbol.for('@libp2p/content-routing')
2828
* interested callers.
2929
*/
3030
export interface ContentRoutingProvider {
31-
[contentRoutingSymbol]: ContentRouting
31+
[contentRoutingSymbol]: Partial<ContentRouting>
3232
}
3333

3434
export interface ContentRouting {

packages/interface/src/errors.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export class TooManyOutboundProtocolStreamsError extends Error {
353353
}
354354

355355
/**
356-
* Thrown when and attempt to operate on an unsupported key was made
356+
* Thrown when an attempt to operate on an unsupported key was made
357357
*/
358358
export class UnsupportedKeyTypeError extends Error {
359359
static name = 'UnsupportedKeyTypeError'
@@ -363,3 +363,15 @@ export class UnsupportedKeyTypeError extends Error {
363363
this.name = 'UnsupportedKeyTypeError'
364364
}
365365
}
366+
367+
/**
368+
* Thrown when an operation has not been implemented
369+
*/
370+
export class NotImplementedError extends Error {
371+
static name = 'NotImplementedError'
372+
373+
constructor (message = 'Not implemented') {
374+
super(message)
375+
this.name = 'NotImplementedError'
376+
}
377+
}

packages/interface/src/event-target.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ interface Listener {
1010
}
1111

1212
/**
13-
* Adds types to the EventTarget class. Hopefully this won't be necessary forever.
13+
* Adds types to the EventTarget class. Hopefully this won't be necessary
14+
* forever.
1415
*
1516
* https://github.com/microsoft/TypeScript/issues/28357
1617
* https://github.com/microsoft/TypeScript/issues/43477
@@ -31,7 +32,6 @@ export interface TypedEventTarget <EventMap extends Record<string, any>> extends
3132

3233
/**
3334
* An implementation of a typed event target
34-
* etc
3535
*/
3636
export class TypedEventEmitter<EventMap extends Record<string, any>> extends EventTarget implements TypedEventTarget<EventMap> {
3737
readonly #listeners = new Map<any, Listener[]>()

packages/interface/src/index.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export interface SignedPeerRecord {
5353
seq: bigint
5454
}
5555

56+
/**
57+
* A certificate that can be used to secure connections
58+
*/
5659
export interface TLSCertificate {
5760
/**
5861
* The private key that corresponds to the certificate in PEM format
@@ -130,9 +133,43 @@ export interface Logger {
130133
}
131134

132135
/**
133-
* Peer logger component for libp2p
136+
* Peer logger component for libp2p. This can be used to create loggers that are
137+
* scoped to individual system components or services.
138+
*
139+
* To see logs, run your app with `DEBUG` set as an env var or for browsers, in
140+
* `localStorage`:
141+
*
142+
* ```console
143+
* $ DEBUG=libp2p* node index.js
144+
* libp2p:my-service hello +0ms
145+
* ```
134146
*/
135147
export interface ComponentLogger {
148+
/**
149+
* Returns a logger for the specified component.
150+
*
151+
* @example
152+
*
153+
* ```TypeScript
154+
* import { ComponentLogger, Logger } from '@libp2p/interface'
155+
*
156+
* interface MyServiceComponents {
157+
* logger: ComponentLogger
158+
* }
159+
*
160+
* class MyService {
161+
* private readonly log: Logger
162+
*
163+
* constructor (components) {
164+
* this.log = components.logger.forComponent('libp2p:my-service')
165+
*
166+
* this.log('hello')
167+
* // logs:
168+
* // libp2p:my-service hello +0ms
169+
* }
170+
* }
171+
* ```
172+
*/
136173
forComponent(name: string): Logger
137174
}
138175

packages/interface/src/metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ export interface Metrics {
510510
/**
511511
* Infer the yielded type of an (async)iterable
512512
*/
513-
type YieldType<T extends AsyncIterator<any> | Iterator<any>> = T extends AsyncIterator<infer Y> ? Y : T extends Iterator<infer Y, any, any> ? Y : never
513+
export type YieldType<T extends AsyncIterator<any> | Iterator<any>> = T extends AsyncIterator<infer Y> ? Y : T extends Iterator<infer Y, any, any> ? Y : never
514514

515515
export type TraceAttributes = Record<string, number | string | boolean | number[] | string[] | boolean[]>
516516

packages/interface/src/peer-discovery.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ export interface PeerDiscoveryEvents {
3434
'peer': CustomEvent<PeerInfo>
3535
}
3636

37+
/**
38+
* A class that implements the `PeerDiscovery` interface uses an
39+
* implementation-specific method to discover peers. These peers are then added
40+
* to the peer store for use by other system components and services.
41+
*/
3742
export interface PeerDiscovery extends TypedEventTarget<PeerDiscoveryEvents> {}

packages/interface/src/peer-routing.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { PeerInfo } from './peer-info.js'
44

55
/**
66
* Any object that implements this Symbol as a property should return a
7-
* PeerRouting instance as the property value, similar to how
8-
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
7+
* PeerRouting instance as the property value, similar to how `Symbol.Iterable`
8+
* can be used to return an `Iterable` from an `Iterator`.
99
*
1010
* @example
1111
*
@@ -28,7 +28,7 @@ export const peerRoutingSymbol = Symbol.for('@libp2p/peer-routing')
2828
* interested callers.
2929
*/
3030
export interface PeerRoutingProvider {
31-
[peerRoutingSymbol]: PeerRouting
31+
[peerRoutingSymbol]: Partial<PeerRouting>
3232
}
3333

3434
export interface PeerRouting {

packages/interface/src/pubsub.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ export interface TopicValidatorFn {
153153
(peer: PeerId, message: Message): TopicValidatorResult | Promise<TopicValidatorResult>
154154
}
155155

156+
/**
157+
* @deprecated This will be removed from `@libp2p/interface` in a future release, pubsub implementations should declare their own types
158+
*/
156159
export interface PubSub<Events extends Record<string, any> = PubSubEvents> extends TypedEventTarget<Events> {
157160
/**
158161
* The global signature policy controls whether or not we sill send and receive

packages/interface/src/record.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,42 @@ export interface Record {
2323
equals(other: Record): boolean
2424
}
2525

26+
/**
27+
* A message with a signed payload.
28+
*/
2629
export interface Envelope {
30+
/**
31+
* The public key of the keypair used to sign the payload
32+
*/
2733
publicKey: PublicKey
34+
35+
/**
36+
* How the payload should be interpreted
37+
*/
2838
payloadType: Uint8Array | Uint8ArrayList
39+
40+
/**
41+
* The contents of the envelope
42+
*/
2943
payload: Uint8Array
44+
45+
/**
46+
* A signature that can be used to verify the payload is intact
47+
*/
3048
signature: Uint8Array | Uint8ArrayList
3149

50+
/**
51+
* Serialize the envelope to a binary format
52+
*/
3253
marshal(): Uint8Array
54+
55+
/**
56+
* Use the public key to validate that the payload is intact
57+
*/
3358
validate(domain: string): Promise<boolean>
34-
equals(other: Envelope): boolean
59+
60+
/**
61+
* Returns true if this envelope has equivalency with the passed object
62+
*/
63+
equals(other?: Envelope): boolean
3564
}

0 commit comments

Comments
 (0)