Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert to typescript #1172

Merged
merged 20 commits into from
Mar 28, 2022
Merged

feat: convert to typescript #1172

merged 20 commits into from
Mar 28, 2022

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Mar 18, 2022

Converts this module to typescript.

  • Ecosystem modules renamed from (e.g.) libp2p-tcp to @libp2p/tcp
  • Ecosystem module now have named exports
  • Configuration has been updated, now pass instances of modules instead of classes:
  • Some configuration keys have been renamed to make them more descriptive. transport -> transports, connEncryption -> connectionEncryption. In general where we pass multiple things, the key is now plural, e.g. streamMuxer -> streamMuxers, contentRouting -> contentRouters, etc. Where we are configuring a singleton the config key is singular, e.g. connProtector -> connectionProtector etc.
  • Properties of the modules config key have been moved to the root
  • Properties of the config config key have been moved to the root
// before
import Libp2p from 'libp2p'
import TCP from 'libp2p-tcp'

await Libp2p.create({
  modules: {
    transport: [
      TCP
    ],
  }
  config: {
    transport: {
      [TCP.tag]: {
        foo: 'bar'
      }
    },
    relay: {
      enabled: true,
      hop: {
        enabled: true,
        active: true
      }
    }
  }
})
// after
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'

await createLibp2p({
  transports: [
    new TCP({ foo: 'bar' })
  ],
  relay: {
    enabled: true,
    hop: {
      enabled: true,
      active: true
    }
  }
})
  • Use of enabled flag has been reduced - previously you could pass a module but disable it with config. Now if you don't want a feature, just don't pass an implementation. Eg:
// before
await Libp2p.create({
  modules: {
    transport: [
      TCP
    ],
    pubsub: Gossipsub
  },
  config: {
    pubsub: {
      enabled: false
    }
  }
})
// after
await createLibp2p({
  transports: [
    new TCP()
  ]
})
  • .multiaddrs renamed to .getMultiaddrs() because it's not a property accessor, work is done by that method to calculate announce addresses, observed addresses, etc
  • /p2p/${peerId} is now appended to all addresses returned by .getMultiaddrs() so they can be used opaquely (every consumer has to append the peer ID to the address to actually use it otherwise). If you need low-level unadulterated addresses, call methods on the address manager.

BREAKING CHANGE: types are no longer hand crafted, this module is now ESM only

@BigLep BigLep requested a review from wemeetagain March 22, 2022 15:47
@BigLep
Copy link
Contributor

BigLep commented Mar 22, 2022

2022-03-22 conversation for someone reviewing this code:

  1. Check it out
  2. See if can integrate it with another project.

@achingbrain achingbrain marked this pull request as ready for review March 25, 2022 07:07
.github/workflows/main.yml Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
src/circuit/auto-relay.ts Show resolved Hide resolved
import errCode from 'err-code'
import type { RecursivePartial } from '@libp2p/interfaces'

const DefaultConfig: Partial<Libp2pInit> = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come you put Partial here?

Shouldn't we enforce that whoever adds a new module must put defaults here? Maybe even create some type which would make everything mandatory here 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without Partial is becomes necessary to define things things like .peerId and .datastore that aren't simple values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const DefaultConfig: Partial<Libp2pInit> = {
const DefaultConfig: Required<Omit<Libp2pInit, "peer" | "datastore">> & Libp2pInit = {

would probably do the trick but I don't have a strong preference, it might end up being too hard to maintain what to keep optional.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it starts getting a bit fiddly like this, maybe let's look at it again later.

src/errors.ts Outdated Show resolved Hide resolved
@BigLep BigLep linked an issue Mar 25, 2022 that may be closed by this pull request
43 tasks
@achingbrain achingbrain merged commit 199395d into master Mar 28, 2022
@achingbrain achingbrain deleted the feat/convert-to-typescript branch March 28, 2022 13:30
Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update LGTM. Some small comments regarding code snippets in docs. We should consider adding something like https://github.com/bbc/typescript-docs-verifier to the CI pipeline.


const node = await createLibp2p({
transports: [
TCP,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this doesn't work anymore, same below where classes are used instead of instances.

import { Mplex } from '@libp2p/mplex'
import { Noise } from '@chainsafe/libp2p-noise'

const { FaultTolerance } from 'libp2p/src/transport-manager')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalid syntax

@@ -22,8 +22,8 @@
Sometimes you may need to wrap an existing duplex stream in order to perform incoming and outgoing [transforms](#transform) on data. This type of wrapping is commonly used in stream encryption/decryption. Using [it-pair][it-pair] and [it-pipe][it-pipe], we can do this rather easily, given an existing [duplex iterable](#duplex).

```js
const duplexPair = require('it-pair/duplex')
const pipe = require('it-pipe')
const duplexPair from 'it-pair/duplex')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax error

@@ -80,7 +82,7 @@ libp2p.dialProtocol(peerInfo, '/echo/1.0.0', (err, conn) => {

**After**
```js
const pipe = require('it-pipe')
const pipe from 'it-pipe')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax

achingbrain added a commit that referenced this pull request Mar 29, 2022
Addresses PR comments from #1172 - fixes syntax of examples in docs,
adds the transport manager to the exports map and renames fault
tolerance enum for consistency.
@achingbrain
Copy link
Member Author

@wemeetagain thanks for your review - I've addressed the points in #1182

bbc/typescript-docs-verifier - this looks really useful, I didn't know it existed. Would you like to give adding it to CI a go?

@wemeetagain
Copy link
Member

Ok, I'll open a PR

achingbrain added a commit that referenced this pull request Mar 29, 2022
Addresses PR comments from #1172 - fixes syntax of examples in docs, adds the transport manager to the exports map and renames fault tolerance enum for consistency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Tracker] js-libp2p getting full Type Script support
4 participants